Skip to content

Commit ebfc53d

Browse files
(SIMP-10456) Better password randomization (#264)
- Fixed - Increased randomization in simplib::gen_random_password SIMP-10456 #comment better password randomization for FIPS mode
1 parent 39ad4b0 commit ebfc53d

4 files changed

Lines changed: 40 additions & 16 deletions

File tree

CHANGELOG

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
* Thu Sep 23 2021 Trevor Vaughan <tvaughan@onyxpoint.com> - 4.10.1
2+
- Fixed
3+
- Increased randomization in simplib::gen_random_password
4+
15
* Tue Aug 03 2021 Trevor Vaughan <tvaughan@onyxpoint.com> - 4.10.0
26
- Fixed
37
- simplib::cron::hour_entry now supports comma separated lists

lib/puppet/functions/simplib/gen_random_password.rb

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,44 +31,64 @@ def gen_random_password(length, complexity=nil, complex_only=false, timeout_seco
3131
require 'timeout'
3232
passwd = ''
3333
Timeout::timeout(timeout_seconds) do
34-
default_charlist = ('a'..'z').to_a + ('A'..'Z').to_a + ('0'..'9').to_a
35-
specific_charlist = nil
34+
lower_charlist = ('a'..'z').to_a
35+
upper_charlist = ('A'..'Z').to_a
36+
digit_charlist = ('0'..'9').to_a
37+
symbol_charlist = nil
3638
case complexity
3739
when 1
38-
specific_charlist = ['@','%','-','_','+','=','~']
40+
symbol_charlist = ['@','%','-','_','+','=','~']
3941
when 2
40-
specific_charlist = (' '..'/').to_a + ('['..'`').to_a + ('{'..'~').to_a
42+
symbol_charlist = (' '..'/').to_a + ('['..'`').to_a + ('{'..'~').to_a
4143
else
4244
end
4345

44-
unless specific_charlist.nil?
46+
unless symbol_charlist.nil?
4547
if complex_only == true
4648
charlists = [
47-
specific_charlist
49+
symbol_charlist
4850
]
4951
else
5052
charlists = [
51-
default_charlist,
52-
specific_charlist
53+
lower_charlist,
54+
upper_charlist,
55+
digit_charlist,
56+
symbol_charlist
5357
]
5458
end
5559

5660
else
5761
charlists = [
58-
default_charlist
62+
lower_charlist,
63+
upper_charlist,
64+
digit_charlist
5965
]
6066
end
6167

62-
index = 0
68+
last_list_rand = nil
69+
last_char_rand = nil
6370
Integer(length).times do |i|
64-
passwd += charlists[index][rand(charlists[index].length-1)]
65-
index += 1
66-
index = 0 if index == charlists.length
71+
rand_list_index = rand(charlists.length).floor
72+
73+
if rand_list_index == last_list_rand
74+
rand_list_index = rand_list_index-1
75+
end
76+
77+
last_list_rand = rand_list_index
78+
79+
rand_index = rand(charlists[rand_list_index].length).floor
80+
81+
if rand_index == last_char_rand
82+
rand_index = rand_index-1
83+
end
84+
85+
passwd += charlists[rand_list_index][rand_index]
86+
87+
last_char_rand = rand_index
6788
end
6889
end
6990

7091
return passwd
7192
end
72-
7393
end
7494
# vim: set expandtab ts=2 sw=2:

metadata.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "simp-simplib",
3-
"version": "4.10.0",
3+
"version": "4.10.1",
44
"author": "SIMP Team",
55
"summary": "A collection of common SIMP functions, facts, and types",
66
"license": "Apache-2.0",

spec/functions/simplib/gen_random_password_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
expect(result).not_to match(/(#{(unsafe_special_chars).join('|')})/)
4646
end
4747

48-
it 'should return a password that contains all special characters if complexity is 2' do
48+
it 'should return a password that contains all normal and special characters if complexity is 2' do
4949
result = subject.execute(32, 2)
5050
expect(result.length).to eql(32)
5151
expect(result).to match(/(#{default_chars.join('|')})/)

0 commit comments

Comments
 (0)