aws_role_creds 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fd22edec6ed2ef8b88158dad1699d24ffbe9204f
4
+ data.tar.gz: 5b2113869f655c4f7d2d490a8592885d05c3f870
5
+ SHA512:
6
+ metadata.gz: 2524764e052f74bb59767dcca9c43d0d7caf3793d75c74eedb0a0584c29be5f6a02c7212cc41cc695722577283c9b0e39a11f9ba3dfd43886d3190457607df36
7
+ data.tar.gz: fa250abf2b813b45da0bb433bb5d92bb751c7a087e54684f2277f8c7382517f362a945ec1c2df719ccd81764c76298def76f28ee9610cfe4f4e0d812f06f74d6
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in aws_role_creds.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Jack Thomas
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,51 @@
1
+ # AwsRoleCreds
2
+
3
+ Have several AWS accounts that you access through delegation? Want a id/key combo for each one? Need to use MFA? But want to use the cli?
4
+
5
+ It can get frustrating managing so many accounts. If you have one (or even more) 'master' account that you assume roles in other accounts then this script will handle generating profiles and temporary session credentials, and keeping your MFA logins to a minimum.
6
+
7
+ ## Installation
8
+
9
+ Install with
10
+
11
+ $ gem install aws_role_creds
12
+
13
+ ## Usage
14
+
15
+ Create a YAML file to manage your profiles, and MFA device, at `~/.aws/config.yaml`
16
+
17
+ ```
18
+ ---
19
+ default:
20
+ - name:
21
+ id:
22
+ key:
23
+ mfa_arn: (optional)
24
+ region: (optional)
25
+ profiles:
26
+ - name:
27
+ id:
28
+ key:
29
+ role_arn:
30
+ region: (optional)
31
+ default: default profile to use
32
+ ```
33
+
34
+ Run `aws_role_creds` and it will get credentials for your default accounts. It will then use these credentials to Assume Roles and get credentials for each of your profiles.
35
+
36
+ Default accounts get creds lasting 24 hours, and assumed role profiles can last an hour. If your credentials expire, run the script again and it will refresh them. It will only ask for you MFA if it is required, i.e. your session credentials have expired.
37
+
38
+ ## Development
39
+
40
+ After checking out the repo, run `bin/setup` to install dependencies.
41
+
42
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
43
+
44
+ ## Contributing
45
+
46
+ Bug reports and pull requests are welcome on GitHub at https://github.com/MrPrimate/aws_role_creds.
47
+
48
+
49
+ ## License
50
+
51
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'aws_role_creds/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws_role_creds"
8
+ spec.version = AwsRoleCreds::VERSION
9
+ spec.authors = ["Jack Thomas"]
10
+ spec.email = ["jackdavidthomas@gmail.com"]
11
+
12
+ spec.description = %q{Used to fetch multiple AWS Role Credential Keys using different Session Keys}
13
+ spec.summary = %q{Manage AWS STS credentials with MFA}
14
+ spec.homepage = "https://github.com/MrPrimate/aws_role_keys"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "bin"
19
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency "aws-sdk"
23
+ spec.add_runtime_dependency "inifile"
24
+ spec.add_development_dependency "bundler", "~> 1.12"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ end
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env ruby
2
+ require 'aws_role_creds'
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,151 @@
1
+ require "aws_role_creds/version"
2
+ require 'aws-sdk'
3
+ require 'yaml'
4
+ require 'time'
5
+ require 'inifile'
6
+ require 'fileutils'
7
+
8
+ module AwsRoleCreds
9
+
10
+ IN_FILE = "#{ENV['HOME']}/.aws/config.yaml"
11
+ # The config file we write out
12
+ OUT_FILE = ["#{ENV['HOME']}/.aws/config", "#{ENV['HOME']}/.aws/credentials"]
13
+ SESSION_CREDS_FILE = "#{ENV['HOME']}/.aws/session.yaml"
14
+ SESSION_DURATION = 86400
15
+ ROLE_DURATION = 3600
16
+ REGION = 'eu-west-1'
17
+
18
+ if File.exists?( IN_FILE )
19
+ @config = YAML::load( File.open( IN_FILE ) )
20
+ else
21
+ puts "Please create a yaml config file in #{INFILE}"
22
+ exit!(1)
23
+ end
24
+
25
+ if File.exists?(SESSION_CREDS_FILE)
26
+ @session_credentials = YAML::load( File.open( SESSION_CREDS_FILE ) ) || {}
27
+ else
28
+ @session_credentials = {}
29
+ end
30
+
31
+ @role_credentials = {}
32
+
33
+ # Get session credentials for each 'master' account
34
+ @config['default'].each do |p|
35
+ name = p['name']
36
+ region = p['region'] || REGION
37
+ duration = p['duration'] || SESSION_DURATION
38
+ if @session_credentials.key?(name)
39
+ break if @session_credentials[name]['expiration'] > Time.now
40
+ end
41
+ puts "#{name}"
42
+
43
+ if p['id'] and p['key']
44
+ client = Aws::STS::Client.new(
45
+ access_key_id: p['id'],
46
+ secret_access_key: p['key'],
47
+ region: region
48
+ )
49
+ else
50
+ client = Aws::STS::Client.new(region: region)
51
+ end
52
+
53
+ if p['mfa_arn']
54
+ puts "Enter MFA token code for #{name} using #{p['mfa_arn']}"
55
+ token = gets
56
+
57
+ session_credentials = client.get_session_token(
58
+ duration_seconds: duration,
59
+ serial_number: p['mfa_arn'],
60
+ token_code: token.chomp
61
+ )
62
+ else
63
+ session_credentials = client.get_session_token(
64
+ duration_seconds: duration
65
+ )
66
+ end
67
+
68
+ @session_credentials[name] = {
69
+ 'access_key_id' => session_credentials.credentials.access_key_id,
70
+ 'secret_access_key' => session_credentials.credentials.secret_access_key,
71
+ 'session_token' => session_credentials.credentials.session_token,
72
+ 'expiration' => session_credentials.credentials.expiration,
73
+ 'region' => region
74
+ }
75
+ end
76
+
77
+ # Cache session credentials
78
+ File.open( SESSION_CREDS_FILE, 'w' ) { |f|
79
+ f.write @session_credentials.to_yaml
80
+ }
81
+
82
+ # For each role we want to assume grab some assumed credentials using approriate session
83
+ @config['profiles'].each do |p|
84
+ name = p['name']
85
+ default = p['default']
86
+ region = p['region'] || REGION
87
+ duration = p['duration'] || ROLE_DURATION
88
+ session_credentials = @session_credentials[default]
89
+ puts "Getting credentials for #{name} using #{p['role_arn']}"
90
+
91
+ client = Aws::STS::Client.new(
92
+ access_key_id: session_credentials['access_key_id'],
93
+ secret_access_key: session_credentials['secret_access_key'],
94
+ session_token: session_credentials['session_token'],
95
+ region: region
96
+ )
97
+
98
+ role_credentials = client.assume_role(
99
+ role_arn: p['role_arn'],
100
+ role_session_name: name,
101
+ duration_seconds: duration,
102
+ )
103
+
104
+ @role_credentials[name] = {
105
+ 'role' => p['role_arn'],
106
+ 'access_key_id' => role_credentials.credentials.access_key_id,
107
+ 'secret_access_key' => role_credentials.credentials.secret_access_key,
108
+ 'session_token' => role_credentials.credentials.session_token,
109
+ 'expiration' => role_credentials.credentials.expiration,
110
+ 'region' => region
111
+ }
112
+ end
113
+
114
+
115
+ # Write out config file
116
+ # first make a backup
117
+ OUT_FILE.each do |o|
118
+ FileUtils.cp( o, "#{o}.backup" )
119
+
120
+ # create a new ini file object
121
+ config = IniFile.new
122
+ config.filename = o
123
+
124
+ config['default'] = { "region" => REGION }
125
+
126
+ # set properties
127
+ @session_credentials.each do |k, c|
128
+ config["profile #{k}"] = {
129
+ "aws_access_key_id" => "#{c['access_key_id']}",
130
+ "aws_secret_access_key" => "#{c['secret_access_key']}",
131
+ "aws_security_token" => "#{c['session_token']}",
132
+ "region" => "#{c['region']}",
133
+ }
134
+ puts "Profile #{k} created"
135
+ end
136
+
137
+ @role_credentials.each do |k, c|
138
+ config["profile #{k}"] = {
139
+ "aws_access_key_id" => "#{c['access_key_id']}",
140
+ "aws_secret_access_key" => "#{c['secret_access_key']}",
141
+ "aws_security_token" => "#{c['session_token']}",
142
+ "region" => "#{c['region']}",
143
+ }
144
+ puts "Profile #{k} created for role #{c['role']}"
145
+ end
146
+
147
+ # save file
148
+ config.write()
149
+ end
150
+
151
+ end
@@ -0,0 +1,3 @@
1
+ module AwsRoleCreds
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,113 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: aws_role_creds
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jack Thomas
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-07-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: aws-sdk
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: inifile
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
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: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.12'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description: Used to fetch multiple AWS Role Credential Keys using different Session
70
+ Keys
71
+ email:
72
+ - jackdavidthomas@gmail.com
73
+ executables:
74
+ - aws_role_creds
75
+ - setup
76
+ extensions: []
77
+ extra_rdoc_files: []
78
+ files:
79
+ - ".gitignore"
80
+ - Gemfile
81
+ - LICENSE.txt
82
+ - README.md
83
+ - Rakefile
84
+ - aws_role_creds.gemspec
85
+ - bin/aws_role_creds
86
+ - bin/setup
87
+ - lib/aws_role_creds.rb
88
+ - lib/aws_role_creds/version.rb
89
+ homepage: https://github.com/MrPrimate/aws_role_keys
90
+ licenses:
91
+ - MIT
92
+ metadata: {}
93
+ post_install_message:
94
+ rdoc_options: []
95
+ require_paths:
96
+ - lib
97
+ required_ruby_version: !ruby/object:Gem::Requirement
98
+ requirements:
99
+ - - ">="
100
+ - !ruby/object:Gem::Version
101
+ version: '0'
102
+ required_rubygems_version: !ruby/object:Gem::Requirement
103
+ requirements:
104
+ - - ">="
105
+ - !ruby/object:Gem::Version
106
+ version: '0'
107
+ requirements: []
108
+ rubyforge_project:
109
+ rubygems_version: 2.5.1
110
+ signing_key:
111
+ specification_version: 4
112
+ summary: Manage AWS STS credentials with MFA
113
+ test_files: []