ec2iam 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b7f3018580e9e16cd6157bdd8fee04fe5d384f5b
4
+ data.tar.gz: db15ea61540a5a9d687aa1077adbfbacde0ad294
5
+ SHA512:
6
+ metadata.gz: ceaeb93d6aa4b041da037052ba435b92eee368aac4d27e6d997e363b7e323e22a9012bb00de9be5ee0a0938091fab0a52704503c8f221f4ebe4b5cfa763e1d4b
7
+ data.tar.gz: 24b83430fffa68888e4553543d945f7886bb9bf8a4c1f9e652135b5f18be5d7096003139717fb6a766522222ac8ab5048e67c85c888014fed9a1b3157d3f2f92
data/.gitignore ADDED
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ vendor/bundle
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.2
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ec2iam.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 reizist
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,94 @@
1
+ ## Introduce
2
+
3
+ ec2iam is a iam user manager for Amazon EC2.
4
+ Especially, very friendly for [ec2ssh](https://github.com/mirakui/ec2ssh)
5
+
6
+ ## How to use
7
+ ### 1. Prepare Administrator Account on AWS.
8
+ This gem use administrator account's access_key_id and secret_access_key for manage iam function on aws.
9
+
10
+ ### 2. Edit ``` ~/.aws/iam ```
11
+ For accessing aws, please make ``` ~/.aws/iam ``` and set access key info like below.
12
+
13
+ ``` ruby
14
+ default: { access_key_id: 'WURLWEKRJWEIRLSKDJF', secret_access_key: '8fjjwlergJU/fhafHgsdfaoLfl/HsdleiO' }
15
+ ```
16
+
17
+ By default, each command try to access by using ``` default ``` key,
18
+ but if you want to switch aws account, you can set others access key like below.
19
+
20
+ ``` ruby
21
+ default: { access_key_id: 'WURLWEKRJWEIRLSKDJF', secret_access_key: '8fjjwlergJU/fhafHgsdfaoLfl/HsdleiO' }
22
+ another_profile: { access_key_id: 'HOEUWLFJSDLFSUIARF', secret_access_key: '7f78LFDLGh/FJDojhg23dklsdHSDldkdi7' }
23
+ ```
24
+
25
+
26
+ ### 3. Bundle
27
+
28
+ ## Commands
29
+
30
+ ``` sh
31
+ Commands:
32
+ ec2iam create user_name # create iam user with name 'user_name' who belongs to ReadOnly group.
33
+ ec2iam delete! user_name # delete iam user with user_name perfectly.
34
+ ec2iam help [COMMAND] # Describe available commands or one specific command
35
+ ec2iam list # list iam users on the account.
36
+
37
+ Options:
38
+ [--all-profiles], [--no-all-profiles] # Run with all profiles
39
+ -p, [--profile=PROFILE] # Run with specify profile
40
+ ```
41
+
42
+ ### options
43
+
44
+ ##### --profile (aliases is '-p')
45
+ Each command can use ``` --profile ``` option to choose aws account on config file ``` iam ```
46
+
47
+ ``` shell
48
+ $ ec2iam list --profile=another_profile
49
+ ```
50
+
51
+ or
52
+
53
+ ``` shell
54
+ $ ec2iam list -p another_profile
55
+ ```
56
+
57
+ ##### --all-profiles
58
+ When you handle multiple accounts(profiles), you are able to exec on all profiles like below.
59
+
60
+ ``` shell
61
+ $ ec2iam create hoge --all-profiles
62
+
63
+ On default:
64
+ create hoge done.
65
+ aws_keys(
66
+ default: { access_key_id: 'AKIAJCDTOJHTU26GVFTQ', secret_access_key: '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' }
67
+ )
68
+ On another_profile:
69
+ create hoge done.
70
+ aws_keys(
71
+ another_profile: { access_key_id: 'AKIATWUADITSTEPVLR7A', secret_access_key: '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' }
72
+ )
73
+
74
+ ```
75
+
76
+ #### On create command
77
+ ##### --save
78
+ If you add `--save` option, created values are written in ``` ~/.aws/<username>.keys ```.
79
+
80
+ ## ec2ssh
81
+ * Create IAM credentials.
82
+ ``` ec2iam create hoge --save ```
83
+
84
+ * Copy each credentials on ``` ~/.aws/hoge.keys ``` to ``` ~/.ec2ssh ```
85
+
86
+ * Run ``` ec2ssh update ```
87
+
88
+ ## Contributing
89
+
90
+ 1. Fork it ( https://github.com/reizist/ec2iam/fork )
91
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
92
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
93
+ 4. Push to the branch (`git push origin my-new-feature`)
94
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,7 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
7
+
data/bin/ec2iam ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'ec2iam'
4
+ require 'ec2iam/iam_config'
5
+
6
+ Ec2Iam::CLI.start
data/ec2iam.gemspec ADDED
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'ec2iam/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ec2iam"
8
+ spec.version = Ec2Iam::VERSION
9
+ spec.authors = ["reizist"]
10
+ spec.email = ["reizist@gmail.com"]
11
+ spec.summary = %q{manage iam user on aws}
12
+ spec.description = %q{manage iam user on aws}
13
+ spec.homepage = "https://github.com/reizist/ec2iam"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rspec"
24
+
25
+ spec.add_dependency 'thor'
26
+ spec.add_dependency 'aws-sdk'
27
+ end
data/iam.sample ADDED
@@ -0,0 +1,2 @@
1
+ default: { access_key_id: '', secret_access_key: '' }
2
+ another_profile: { access_key_id: '', secret_access_key: '' }
data/lib/ec2iam/cli.rb ADDED
@@ -0,0 +1,108 @@
1
+ require 'ec2iam'
2
+ require 'thor'
3
+ require 'aws-sdk'
4
+ require 'ec2iam/iam_config'
5
+ include Ec2Iam
6
+
7
+ module Ec2Iam
8
+ class CLI < Thor
9
+ class_option 'all-profiles', type: :boolean, desc: 'Run with all profiles'
10
+ class_option :profile, type: :string ,aliases: '-p', desc: 'Run with specify profile'
11
+
12
+ desc "create user_name", "create iam user with name 'user_name' who belongs to ReadOnly group."
13
+ option :save, type: :boolean, desc: "save credentials to file"
14
+ def create(user_name)
15
+ if options['all-profiles']
16
+ keys_array = []
17
+ IamConfig::CONFIG.each do |profile, credentials|
18
+ set_client(profile)
19
+ say("On #{profile}:")
20
+ keys_array << { profile: profile, credentials: create_user(user_name) }
21
+ end
22
+
23
+ IamConfig.write_keys(user_name, keys_array) if options[:save]
24
+ else
25
+ options[:profile] ? set_client(options[:profile]) : set_client
26
+
27
+ if options[:save]
28
+ IamConfig.write_key(user_name, IamConfig.format_key(@client.profile, create_user(user_name)))
29
+ else
30
+ create_user(user_name)
31
+ end
32
+ end
33
+ end
34
+
35
+ desc "delete! user_name", "delete iam user with user_name perfectly."
36
+ long_desc <<-LONGDESC
37
+ "Deletes the current user, after:
38
+ * deleting its login profile
39
+ * removing it from all groups
40
+ * deleting all of its access keys
41
+ * deleting its mfa devices * deleting its signing certificates"
42
+ LONGDESC
43
+ def delete!(user_name)
44
+ if options['all-profiles']
45
+ IamConfig::CONFIG.each do |profile, credentials|
46
+ set_client(profile)
47
+ say("On #{profile}:")
48
+ delete_user!(user_name)
49
+ end
50
+ else
51
+ options[:profile] ? set_client(options[:profile]) : set_client
52
+ delete_user!(user_name)
53
+ end
54
+ end
55
+
56
+ desc "list", "list iam users on the account."
57
+ def list
58
+ if options['all-profiles']
59
+ IamConfig::CONFIG.each do |profile, credentials|
60
+ set_client(profile)
61
+ say("On #{profile}:")
62
+ list_user
63
+ end
64
+ else
65
+ options[:profile] ? set_client(options[:profile]) : set_client
66
+ list_user
67
+ end
68
+ end
69
+
70
+ no_tasks do
71
+ def set_client(profile='default')
72
+ begin
73
+ @client = IamConfig.new(profile)
74
+ rescue AccountKeyNotFound
75
+ say("account_key #{profile} was not found on iam.yml", :red)
76
+ exit(1)
77
+ end
78
+ end
79
+
80
+ def create_user(user_name)
81
+ begin
82
+ user = @client.iam.users.create(user_name)
83
+ user.groups.add(@client.group)
84
+ say("create #{user_name} done.", :green)
85
+ access_key = user.access_keys.create
86
+ credentials = access_key.credentials
87
+ say("#{IamConfig.format_key(@client.profile, credentials)}", :green)
88
+ credentials
89
+ rescue AWS::IAM::Errors::EntityAlreadyExists
90
+ say("User '#{user_name}' has already exists. Please retry with another name.", :red)
91
+ end
92
+ end
93
+
94
+ def delete_user!(user_name)
95
+ begin
96
+ @client.iam.users[user_name].delete!
97
+ say("delete #{user_name}.", :red)
98
+ rescue AWS::IAM::Errors::NoSuchEntity
99
+ say("User '#{user_name}' Not Found. Please retry with another name.", :red)
100
+ end
101
+ end
102
+
103
+ def list_user
104
+ @client.iam.users.each {|u| puts u.name}
105
+ end
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,63 @@
1
+ require 'yaml'
2
+ module Ec2Iam
3
+ class AccountKeyNotFound < StandardError; end
4
+ class IamConfig
5
+ attr_reader :iam, :group, :profile
6
+
7
+ GROUP_NAME = 'EC2ReadOnly'
8
+ CONFIG = YAML.load_file(File.join(Dir.home, '.aws/iam.yml')).freeze
9
+
10
+ def initialize(account_key)
11
+ @profile = account_key
12
+ raise AccountKeyNotFound if CONFIG[@profile] == nil
13
+
14
+ @iam = AWS::IAM.new(
15
+ access_key_id: CONFIG[@profile]['access_key_id'],
16
+ secret_access_key: CONFIG[@profile]['secret_access_key']
17
+ )
18
+
19
+ @group = @iam.groups[GROUP_NAME].exists? ? @iam.groups[GROUP_NAME] : create_ec2_read_only_group
20
+ end
21
+
22
+ def self.format_key(profile, key)
23
+ <<-KEY
24
+ aws_keys(
25
+ #{profile}: { access_key_id: '#{key[:access_key_id]}', secret_access_key: '#{key[:secret_access_key]}' }
26
+ )
27
+ KEY
28
+ end
29
+
30
+ def create_ec2_read_only_group
31
+ policy = AWS::IAM::Policy.new do |p|
32
+ p.allow(
33
+ actions: ["ec2:Describe*"],
34
+ resources: "*"
35
+ )
36
+ end
37
+ group = @iam.groups.create(GROUP_NAME)
38
+ group.policies[GROUP_NAME] = policy
39
+ group
40
+ end
41
+
42
+ def self.write_key(user_name, formatted_str)
43
+ File.open("#{Dir.home}/.aws/#{user_name}.keys", "a") do |f|
44
+ f.write(formatted_str)
45
+ end
46
+ end
47
+
48
+ def self.write_keys(user_name, array)
49
+ str = "aws_keys(\n"
50
+
51
+ array.each do |hash|
52
+
53
+ str << <<-KEYS
54
+ #{hash[:profile]}: { access_key_id: '#{hash[:credentials][:access_key_id]}', secret_access_key: '#{hash[:credentials][:secret_access_key]}' },
55
+ KEYS
56
+ end
57
+
58
+ str << ")\n"
59
+
60
+ write_key(user_name, str)
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,3 @@
1
+ module Ec2Iam
2
+ VERSION = "0.1.1"
3
+ end
data/lib/ec2iam.rb ADDED
@@ -0,0 +1,3 @@
1
+ require "ec2iam/version"
2
+ require "ec2iam/cli"
3
+ require "ec2iam/iam_config"
@@ -0,0 +1,7 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ec2Iam do
4
+ it 'has a version number' do
5
+ expect(Ec2Iam::VERSION).not_to be nil
6
+ end
7
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'ec2iam'
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ec2iam
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - reizist
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-11-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: thor
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: aws-sdk
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: manage iam user on aws
84
+ email:
85
+ - reizist@gmail.com
86
+ executables:
87
+ - ec2iam
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - ".rspec"
93
+ - ".travis.yml"
94
+ - Gemfile
95
+ - LICENSE.txt
96
+ - README.md
97
+ - Rakefile
98
+ - bin/ec2iam
99
+ - ec2iam.gemspec
100
+ - iam.sample
101
+ - lib/ec2iam.rb
102
+ - lib/ec2iam/cli.rb
103
+ - lib/ec2iam/iam_config.rb
104
+ - lib/ec2iam/version.rb
105
+ - spec/ec2iam_spec.rb
106
+ - spec/spec_helper.rb
107
+ homepage: https://github.com/reizist/ec2iam
108
+ licenses:
109
+ - MIT
110
+ metadata: {}
111
+ post_install_message:
112
+ rdoc_options: []
113
+ require_paths:
114
+ - lib
115
+ required_ruby_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ required_rubygems_version: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ requirements: []
126
+ rubyforge_project:
127
+ rubygems_version: 2.2.2
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: manage iam user on aws
131
+ test_files:
132
+ - spec/ec2iam_spec.rb
133
+ - spec/spec_helper.rb