Class: Ec2Iam::IamConfig

Inherits:
Object
  • Object
show all
Defined in:
lib/ec2iam/iam_config.rb

Constant Summary collapse

GROUP_NAME =
'EC2ReadOnly'
CONFIG =
YAML.load_file(File.join(Dir.home, '.aws/iam')).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(account_key) ⇒ IamConfig

Returns a new instance of IamConfig.

Raises:



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/ec2iam/iam_config.rb', line 10

def initialize()
  @profile = 
  raise AccountKeyNotFound if CONFIG[@profile] == nil

  @iam = AWS::IAM.new(
    access_key_id: CONFIG[@profile]['access_key_id'],
    secret_access_key: CONFIG[@profile]['secret_access_key']
  )

  @group = @iam.groups[GROUP_NAME].exists? ? @iam.groups[GROUP_NAME] : create_ec2_read_only_group
end

Instance Attribute Details

#groupObject (readonly)

Returns the value of attribute group.



5
6
7
# File 'lib/ec2iam/iam_config.rb', line 5

def group
  @group
end

#iamObject (readonly)

Returns the value of attribute iam.



5
6
7
# File 'lib/ec2iam/iam_config.rb', line 5

def iam
  @iam
end

#profileObject (readonly)

Returns the value of attribute profile.



5
6
7
# File 'lib/ec2iam/iam_config.rb', line 5

def profile
  @profile
end

Class Method Details

.format_key(profile, key) ⇒ Object



22
23
24
25
26
27
28
# File 'lib/ec2iam/iam_config.rb', line 22

def self.format_key(profile, key)
"aws_keys(\n  \#{profile}: { access_key_id: '\#{key[:access_key_id]}', secret_access_key: '\#{key[:secret_access_key]}' }\n)\n"
end

.write_key(user_name, formatted_str) ⇒ Object



42
43
44
45
46
# File 'lib/ec2iam/iam_config.rb', line 42

def self.write_key(user_name, formatted_str)
  File.open("#{Dir.home}/.aws/#{user_name}.keys", "a") do |f|
    f.write(formatted_str)
  end
end

.write_keys(user_name, array) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ec2iam/iam_config.rb', line 48

def self.write_keys(user_name, array)
  str = "aws_keys(\n"

  array.each do |hash|

str << "  \#{hash[:profile]}: { access_key_id: '\#{hash[:credentials][:access_key_id]}', secret_access_key: '\#{hash[:credentials][:secret_access_key]}' },\n"
  end

  str << ")\n"

  write_key(user_name, str)
end

Instance Method Details

#create_ec2_read_only_groupObject



30
31
32
33
34
35
36
37
38
39
40
# File 'lib/ec2iam/iam_config.rb', line 30

def create_ec2_read_only_group
  policy = AWS::IAM::Policy.new do |p|
    p.allow(
      actions: ["ec2:Describe*"],
      resources: "*"
    )
  end
  group = @iam.groups.create(GROUP_NAME)
  group.policies[GROUP_NAME] = policy
  group
end