Class: Awscli::Iam::User
- Inherits:
-
Object
- Object
- Awscli::Iam::User
- Defined in:
- lib/awscli/iam.rb
Instance Method Summary collapse
- #add_user_to_group(username, groupname) ⇒ Object
- #assign_password(username, password) ⇒ Object
- #create(options) ⇒ Object
- #create_user_access_key(username) ⇒ Object
- #delete(options) ⇒ Object
- #delete_user_access_key(username, access_key_id) ⇒ Object
- #generate_password(username) ⇒ Object
-
#initialize(connection) ⇒ User
constructor
A new instance of User.
- #list(path) ⇒ Object
- #list_groups_for_user(username) ⇒ Object
- #list_user_access_keys(username) ⇒ Object
- #remove_password(username) ⇒ Object
- #remove_user_from_group(username, groupname) ⇒ Object
- #update_user(options) ⇒ Object
Constructor Details
#initialize(connection) ⇒ User
Returns a new instance of User.
7 8 9 |
# File 'lib/awscli/iam.rb', line 7 def initialize(connection) @conn = connection end |
Instance Method Details
#add_user_to_group(username, groupname) ⇒ Object
122 123 124 125 126 127 |
# File 'lib/awscli/iam.rb', line 122 def add_user_to_group(username, groupname) @conn.add_user_to_group(groupname, username) puts "Added user: #{username}, to group: #{groupname}" rescue Fog::AWS::IAM::NotFound puts "[Error]: #{$!}" end |
#assign_password(username, password) ⇒ Object
143 144 145 146 147 148 149 150 151 152 153 154 |
# File 'lib/awscli/iam.rb', line 143 def assign_password(username, password) @conn.create_login_profile(username, password) puts "Assigned user #{username} password: #{password}" rescue Fog::AWS::IAM::NotFound, Fog::AWS::IAM::ValidationError puts "[Error]: #{$!}" rescue Fog::AWS::IAM::Error puts "[Error]: #{$!}" if $!.to_s =~ /PasswordPolicyViolation/ #TODO: show password policy, this is not available in fog puts 'Password policy is violated, please revisit your password policies' end end |
#create(options) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/awscli/iam.rb', line 18 def create() username = [:user_name] @conn.create_user(username, [:path] ||= '/') puts "Created User: #{username}" if [:password] #Assign a password for the user generate_password username end if [:group] #add user to the group add_user_to_group username, [:group] end if [:access_key] #create a access_key for the user create_user_access_key username end if [:policy] #upload the policy document document = [:policy_doc] policy_name = "User-#{username}-Custom" #validate json document doc_path = File.(document) abort "Invalid file path: #{document}" unless File.exist?(doc_path) json_string = File.read(doc_path) abort "Invalid JSON format found in the document: #{document}" unless valid_json?(json_string) @conn.put_user_policy(username, policy_name, JSON.parse(json_string) #json parsed to hash ) puts "Added policy: #{policy_name} to user: #{username}" puts "Added Policy #{policy_name} from #{document}" else #create set of basic policy to the user created user_arn = @conn.users.get(username).arn @conn.put_user_policy( username, "User#{username}Policy", { 'Statement' => [ { 'Effect' => 'Allow', 'Action' => 'iam:*AccessKey*', 'Resource' => user_arn }, { 'Effect' => 'Allow', 'Action' => ['ec2:Describe*', 's3:Get*', 's3:List*'], 'Resource' => '*' } ] } ) puts 'User policy for accessing/managing keys of their own and read-access is in place' end rescue Fog::AWS::IAM::ValidationError puts "ValidationError: #{$!}" rescue Fog::AWS::IAM::EntityAlreadyExists puts "[Error] User Exists: #{$!}" rescue Fog::AWS::IAM::NotFound, Fog::AWS::IAM::Error puts "[Error]: #{$!}" end |
#create_user_access_key(username) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/awscli/iam.rb', line 80 def create_user_access_key(username) data = @conn.create_access_key('UserName' => username) access_key_id = data.body['AccessKey']['AccessKeyId'] secret_access_key = data.body['AccessKey']['SecretAccessKey'] #keystatus = data.body['AccessKey']['Status'] puts 'Store the following access id and secret key:' puts "AccessKey: #{access_key_id}" puts "SecretAccessKey: #{secret_access_key}" rescue Fog::AWS::IAM::NotFound puts "[Error]: #{$!}" end |
#delete(options) ⇒ Object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/awscli/iam.rb', line 181 def delete() username = [:user_name] user = @conn.users.get(username) if user if [:force] #ask user to confirm deletion if agree('Are you sure you want to delete user and users associated login_profile, access_keys, policies ? ') #check if user has login profile begin @conn.get_login_profile(username) user_profile = true rescue Fog::AWS::IAM::NotFound user_profile = false end remove_password username if user_profile #check if user has access_keys access_keys = user.access_keys.map { |access_key| access_key.id } unless access_keys.empty? #delete access_keys access_keys.each do |access_key| delete_user_access_key username, access_key end end #check if user belongs to a group groups = @conn.list_groups_for_user(username).body['GroupsForUser'].map { |k| k['GroupName'] } unless groups.empty? #delete user_groups groups.each do |group| remove_user_from_group username, group end end #check if user has policies policies = user.policies.map { |policy| policy.id } unless policies.empty? policies.each do |policy| @conn.delete_user_policy username, policy end end end end @conn.delete_user(username) else abort "No such user: #{username}" end rescue Fog::AWS::IAM::NotFound, Fog::AWS::IAM::Error puts "[Error]: #{$!}" else puts "Deleted User: #{username}" end |
#delete_user_access_key(username, access_key_id) ⇒ Object
98 99 100 101 102 103 |
# File 'lib/awscli/iam.rb', line 98 def delete_user_access_key(username, access_key_id) @conn.delete_access_key(access_key_id, 'UserName' => username) puts "Deleted AccessKey for user: #{username}" rescue Fog::AWS::IAM::NotFound puts "[Error]: #{$!}" end |
#generate_password(username) ⇒ Object
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/awscli/iam.rb', line 156 def generate_password(username) tries ||= 3 password = ((33..126).map { |i| i.chr }).to_a.shuffle[0..14].join @conn.create_login_profile(username, password) rescue Fog::AWS::IAM::NotFound, Fog::AWS::IAM::ValidationError puts "[Error]: #{$!}" rescue Fog::AWS::IAM::Error puts "[Error]: #{$!}" if $!.to_s =~ /PasswordPolicyViolation/ #TODO: show password policy, this is not available in fog #if password policy is violated, then our generated password might be weak, retry 3 times before failing retry if (tries -= 1) > 0 end else puts "Assigned password: '#{password}' for user #{username}" puts 'Store this password, this cannot be retrieved again' end |
#list(path) ⇒ Object
11 12 13 14 15 16 |
# File 'lib/awscli/iam.rb', line 11 def list(path) users = @conn.list_users('PathPrefix' => path).body['Users'] Formatador.display_table(users) rescue Fog::AWS::IAM::ValidationError puts "ValidationError: #{$!}" end |
#list_groups_for_user(username) ⇒ Object
136 137 138 139 140 141 |
# File 'lib/awscli/iam.rb', line 136 def list_groups_for_user(username) groups = @conn.list_groups_for_user(username).body['GroupsForUser'] Formatador.display_table(groups) rescue Fog::AWS::IAM::NotFound puts "[Error]: #{$!}" end |
#list_user_access_keys(username) ⇒ Object
92 93 94 95 96 |
# File 'lib/awscli/iam.rb', line 92 def list_user_access_keys(username) @conn.access_keys(:username => username).table rescue Fog::AWS::IAM::NotFound puts "[Error]: #{$!}" end |
#remove_password(username) ⇒ Object
174 175 176 177 178 179 |
# File 'lib/awscli/iam.rb', line 174 def remove_password(username) @conn.delete_login_profile(username) puts "Deleted login profile for user: #{username}" rescue Fog::AWS::IAM::Error, Fog::AWS::IAM::NotFound puts "[Error]: #{$!}" end |
#remove_user_from_group(username, groupname) ⇒ Object
129 130 131 132 133 134 |
# File 'lib/awscli/iam.rb', line 129 def remove_user_from_group(username, groupname) @conn.remove_user_from_group(groupname, username) puts "Removed user: #{username}, from group: #{groupname}" rescue Fog::AWS::IAM::NotFound puts "[Error]: #{$!}" end |
#update_user(options) ⇒ Object
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/awscli/iam.rb', line 105 def update_user() opts = Marshal.load(Marshal.dump()) opts.reject! { |k| k == 'user_name' } if new_user_name = opts.delete(:new_user_name) opts.merge!('NewUserName' => new_user_name) end if new_path = opts.delete(:new_path) opts.merge!('NewPath' => new_path) end @conn.update_user([:user_name], opts) puts 'Updated user details' rescue Fog::AWS::IAM::EntityAlreadyExists puts '[Error] User already exists, pass in a different username' rescue Fog::AWS::IAM::ValidationError puts "ValidationError: #{$!}" end |