Class: Awful::IAM

Inherits:
Cli show all
Defined in:
lib/awful/iam.rb

Instance Method Summary collapse

Methods inherited from Cli

#initialize

Constructor Details

This class inherits a constructor from Awful::Cli

Instance Method Details

#mfaObject



33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/awful/iam.rb', line 33

def mfa
  iam.list_virtual_mfa_devices.virtual_mfa_devices.output do |devices|
    if options[:long]
      print_table devices.map { |d|
        user_name = d.user ? d.user.user_name : '-'
        [user_name, d.serial_number, d.enable_date]
      }
    else
      puts devices.map(&:serial_number)
    end
  end
end

#policy(type, name, policy = nil) ⇒ Object



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'lib/awful/iam.rb', line 71

def policy(type, name, policy = nil)

  ## first matching role, group or user
  thing_name = iam.send("list_#{type}s").send("#{type}s").find do |thing|
    thing.send("#{type}_name").match(name)
  end.send("#{type}_name")

  ## policies for this role, group or user
  policies = iam.send("list_#{type}_policies", "#{type}_name".to_sym => thing_name).policy_names

  if policy.nil?            # just list policies
    policies.output(&method(:puts))
  else                      #  get policy document
    policy_name = policies.find { |p| p.match(/#{policy}/i) }
    doc = iam.send("get_#{type}_policy", "#{type}_name".to_sym => thing_name, policy_name: policy_name).policy_document
    URI.unescape(doc).output do |str|
      if options[:pretty]
        puts JSON.pretty_generate(JSON.parse(str))
      else
        puts str
      end
    end
  end
end

#roles(name = /./) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/awful/iam.rb', line 49

def roles(name = /./)
  iam.list_roles.roles.select do |role|
    role.role_name.match(name)
  end.output do |roles|
    name_method = options[:arns] ? :arn : :role_name
    if options[:long]
      print_table roles.map { |r|
        [
          r.send(name_method),
          r.role_id,
          r.create_date,
          options[:arns] ? r.arn : nil
        ]
      }
    else
      puts roles.map(&name_method)
    end
  end
end

#usersObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/awful/iam.rb', line 15

def users
  iam.list_users.users.output do |users|
    if options[:long]
      print_table users.map { |u| [u.user_name, u.user_id, u.create_date, u.password_last_used] }
    elsif options[:mfa]
      mfa = iam.list_virtual_mfa_devices.virtual_mfa_devices.each_with_object({}) do |m,h|
        next unless m.user
        h[m.user.user_name] = m.enable_date
      end
      print_table users.map { |u| [u.user_name, mfa.fetch(u.user_name, '-')] }
    else
      puts users.map(&:user_name)
    end
  end
end