Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update password manager #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/subiam/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def walk_login_profile(user_name, expected_login_profile, actual_login_profile)
end

if expected_login_profile and not actual_login_profile
expected_login_profile[:password] ||= @password_manager.identify(user_name, :login_profile)
expected_login_profile[:password] ||= @password_manager.identify(user_name, :login_profile, @driver.password_policy)
@driver.create_login_profile(user_name, expected_login_profile)
updated = true
elsif not expected_login_profile and actual_login_profile
Expand Down
8 changes: 8 additions & 0 deletions lib/subiam/driver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,14 @@ def update_managed_policy(policy_name, policy_document, old_policy_document)
end
end

def password_policy
return @password_policy if instance_variable_defined?(:@password_policy)

@password_policy = @iam.get_account_password_policy.password_policy
rescue Aws::IAM::Errors::NoSuchEntity
@password_policy = nil
end

private

def encode_document(policy_document)
Expand Down
27 changes: 23 additions & 4 deletions lib/subiam/password_manager.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
class Subiam::PasswordManager
include Subiam::Logger::Helper

LOWERCASES = ('a'..'z').to_a
UPPERCASES = ('A'..'Z').to_a
NUMBERS = ('0'..'9').to_a
SYMBOLS = "!@\#$%^&*()_+-=[]{}|'".split(//)

def initialize(output, options = {})
@output = output
@options = options
end

def identify(user, type)
password = mkpasswd
def identify(user, type, policy)
password = mkpasswd(policy)
log(:debug, "mkpasswd: #{password}")
puts_password(user, type, password)
password
end
Expand All @@ -22,8 +28,21 @@ def puts_password(user, type, password)

private

def mkpasswd(len = 8)
[*1..9, *'A'..'Z', *'a'..'z'].shuffle.slice(0, len).join
def mkpasswd(policy)
chars = []
len = 8

if policy
len = policy.minimum_password_length if policy.minimum_password_length > len
chars << LOWERCASES.shuffle.first if policy.require_lowercase_characters
chars << UPPERCASES.shuffle.first if policy.require_uppercase_characters
chars << NUMBERS.shuffle.first if policy.require_numbers
chars << SYMBOLS.shuffle.first if policy.require_symbols

len -= chars.length
end

(chars + [*1..9, *'A'..'Z', *'a'..'z'].shuffle.slice(0, len)).shuffle.join
end

def open_output
Expand Down