Class: Ec2Iam::IamConfig
- Inherits:
-
Object
- Object
- Ec2Iam::IamConfig
- 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
-
#group ⇒ Object
readonly
Returns the value of attribute group.
-
#iam ⇒ Object
readonly
Returns the value of attribute iam.
-
#profile ⇒ Object
readonly
Returns the value of attribute profile.
Class Method Summary collapse
- .format_key(profile, key) ⇒ Object
- .write_key(user_name, formatted_str) ⇒ Object
- .write_keys(user_name, array) ⇒ Object
Instance Method Summary collapse
- #create_ec2_read_only_group ⇒ Object
-
#initialize(account_key) ⇒ IamConfig
constructor
A new instance of IamConfig.
Constructor Details
#initialize(account_key) ⇒ IamConfig
Returns a new instance of IamConfig.
10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/ec2iam/iam_config.rb', line 10 def initialize(account_key) @profile = account_key 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
#group ⇒ Object (readonly)
Returns the value of attribute group.
5 6 7 |
# File 'lib/ec2iam/iam_config.rb', line 5 def group @group end |
#iam ⇒ Object (readonly)
Returns the value of attribute iam.
5 6 7 |
# File 'lib/ec2iam/iam_config.rb', line 5 def iam @iam end |
#profile ⇒ Object (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_group ⇒ Object
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 |