aws_su 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +12 -0
- data/.ruby-version +1 -0
- data/.travis.yml +7 -0
- data/CHANGELOG.md +11 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +21 -0
- data/README.md +83 -0
- data/Rakefile +10 -0
- data/aws_su.gemspec +47 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/aws_su/version.rb +5 -0
- data/lib/aws_su.rb +222 -0
- data/lib/matches.rb +6 -0
- data/lib/matches_aws_access_key_id.rb +7 -0
- data/lib/matches_aws_profile.rb +7 -0
- data/lib/matches_aws_secret_access_key.rb +7 -0
- data/lib/matches_aws_security_token.rb +7 -0
- data/lib/matches_aws_session_token.rb +7 -0
- data/lib/matches_aws_token_etl.rb +7 -0
- metadata +127 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 7c94982b2139e90c3f36bb7b8e7f4d2989b05a2e
|
4
|
+
data.tar.gz: b6de06567ecb9a29348ecce20a12d4b93600ad19
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 412fb1c85c563d71b74e6aca277ef5c3031ad7503aafbeb6baccc58dde436b81fe5c7755ac0d71095af878cff2d1062b3570868624cd7f8041782f32f006561b
|
7
|
+
data.tar.gz: de13c38ada25aeada402abff4465d24f91346b032365ebaef4c02f5992b20bdb503332caab3b8a9800d9a58571ed977a2779e72a541550fcb4a95df812a9580a
|
data/.gitignore
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.4.2
|
data/.travis.yml
ADDED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
# Changelog
|
2
|
+
All notable changes to this project will be documented in this file.
|
3
|
+
|
4
|
+
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
5
|
+
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
6
|
+
|
7
|
+
|
8
|
+
## [0.1.0] - 01-12-2018
|
9
|
+
### Added
|
10
|
+
- Ability to authenticate and switch role via a single authenticate() method
|
11
|
+
- Gemspec file updated to enable push to Rubygems.org
|
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at Bradley.Atkins@bjss.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Bradley Atkins
|
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,83 @@
|
|
1
|
+
# AwsSu
|
2
|
+
|
3
|
+
AwsSu is a gem developed for a specific use case, where the user has an ID setuo in an AWS master account and wants to
|
4
|
+
assume a role in another account that they have permission to assume.
|
5
|
+
|
6
|
+
## Installation
|
7
|
+
|
8
|
+
Add this line to your application's Gemfile:
|
9
|
+
|
10
|
+
```ruby
|
11
|
+
gem 'aws_su'
|
12
|
+
```
|
13
|
+
|
14
|
+
And then execute:
|
15
|
+
|
16
|
+
$ bundle
|
17
|
+
|
18
|
+
Or install it yourself as:
|
19
|
+
|
20
|
+
$ gem install aws_su
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
Implemented as a Ruby Module, the gem can be included into any class that needs to authenticate and assume
|
25
|
+
a role prior to calling one of the aws client methods like so:
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
require 'aws_su'
|
29
|
+
|
30
|
+
class Runner
|
31
|
+
include AwsSu
|
32
|
+
end
|
33
|
+
|
34
|
+
runner = Runner.new
|
35
|
+
runner.authenticate('my-profile')
|
36
|
+
runner.ec2_client.describe_vpcs
|
37
|
+
```
|
38
|
+
|
39
|
+
The gem expects to find the standard aws secrets files:
|
40
|
+
|
41
|
+
- ~/.aws/credentials
|
42
|
+
- ~/.aws/config
|
43
|
+
|
44
|
+
With the former containing the master account secrets:
|
45
|
+
|
46
|
+
```[master]
|
47
|
+
aws_access_key_id = XXXXXXXXXXXXXX
|
48
|
+
aws_secret_access_key = XXXXXXXXXXXXXXXXXXXX
|
49
|
+
```
|
50
|
+
|
51
|
+
And the latter containing the details of the role to be assumed:
|
52
|
+
|
53
|
+
```
|
54
|
+
[profile my-profile]
|
55
|
+
source_profile=master
|
56
|
+
mfa_serial=arn:aws:iam::1234567890:mfa/bradley.atkins@bjss.com
|
57
|
+
role_arn=arn:aws:iam::1234567890:role/MY-NONPROD-TESTER-ROLE
|
58
|
+
```
|
59
|
+
|
60
|
+
AwsSu also configures the current shell with the necessary environment variables to allow system calls to
|
61
|
+
be made without further authentication:
|
62
|
+
|
63
|
+
```ruby
|
64
|
+
system('aws ec2 describe-vpcs --region eu-west-2')
|
65
|
+
```
|
66
|
+
|
67
|
+
## Development
|
68
|
+
|
69
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
70
|
+
|
71
|
+
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).
|
72
|
+
|
73
|
+
## Contributing
|
74
|
+
|
75
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/aws_sudo. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
80
|
+
|
81
|
+
## Code of Conduct
|
82
|
+
|
83
|
+
Everyone interacting in the AwsSudo project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/aws_sudo/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/aws_su.gemspec
ADDED
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
lib = File.expand_path('../lib', __FILE__)
|
4
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
5
|
+
require 'aws_su/version'
|
6
|
+
|
7
|
+
Gem::Specification.new do |spec|
|
8
|
+
spec.name = 'aws_su'
|
9
|
+
spec.version = AwsSu::VERSION
|
10
|
+
spec.authors = ['Bradley Atkins']
|
11
|
+
spec.email = ['Bradley.Atkins@bjss.com']
|
12
|
+
|
13
|
+
spec.summary = 'Gem to wrap helper methods around AWS authentication API'
|
14
|
+
spec.description = 'Developed for a specific use case: ' \
|
15
|
+
'User has an AWS id in a master account and wants to assume'\
|
16
|
+
' a role in another account. This module exposes a single'\
|
17
|
+
'authenticate() method that handles authentication and switching role '\
|
18
|
+
'by referencing the user\'s aws secrets.'
|
19
|
+
spec.homepage = 'https://github.com/museadmin'
|
20
|
+
spec.license = 'MIT'
|
21
|
+
|
22
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
23
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
24
|
+
if spec.respond_to?(:metadata)
|
25
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
26
|
+
spec.metadata['source_code_uri'] = 'https://github.com/museadmin/aws_su'
|
27
|
+
spec.metadata['changelog_uri'] = "Put your gem's CHANGELOG.md URL here."
|
28
|
+
else
|
29
|
+
raise 'RubyGems >= 2.0 is required to protect against public gem pushes'
|
30
|
+
end
|
31
|
+
|
32
|
+
# Specify which files should be added to the gem when it is released.
|
33
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
34
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
35
|
+
`git ls-files -z`
|
36
|
+
.split("\x0")
|
37
|
+
.reject { |f| f.match(%r{^(test|spec|features)/}) }
|
38
|
+
end
|
39
|
+
spec.bindir = 'exe'
|
40
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
41
|
+
spec.require_paths = ['lib']
|
42
|
+
|
43
|
+
spec.add_development_dependency 'awsecrets'
|
44
|
+
spec.add_development_dependency 'bundler', '~> 1.17'
|
45
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
46
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
47
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "aws_su"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start(__FILE__)
|
data/bin/setup
ADDED
data/lib/aws_su.rb
ADDED
@@ -0,0 +1,222 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'aws_config'
|
4
|
+
require 'matches'
|
5
|
+
require 'awsecrets'
|
6
|
+
require 'aws_sudo/version'
|
7
|
+
|
8
|
+
# Set up the AWS authentication environment
|
9
|
+
module AwsSu
|
10
|
+
class Error < StandardError; end
|
11
|
+
|
12
|
+
AWS_SUDO_FILE = Dir.home + '/.awssudo'
|
13
|
+
AWS_CONFIG_FILE = Dir.home + '/.aws/config'
|
14
|
+
DURATION = '28800'
|
15
|
+
@profile = nil # name of current profile
|
16
|
+
@token_ttl = nil # The session expiry
|
17
|
+
@session = nil # Name of the active session
|
18
|
+
@duration = DURATION # Session duration in seconds
|
19
|
+
@master_config = Awsecrets.load # AWS config for master account
|
20
|
+
|
21
|
+
# Authenticate user for the session
|
22
|
+
def authenticate(profile, duration = DURATION)
|
23
|
+
@session = "awssudo-session-#{Time.now.to_i}"
|
24
|
+
@token_ttl = calculate_session_expiry(duration)
|
25
|
+
@profile = profile
|
26
|
+
@duration = duration
|
27
|
+
export_aws_sudo_file
|
28
|
+
assume_role
|
29
|
+
end
|
30
|
+
|
31
|
+
# Configure the ec2 client
|
32
|
+
def ec2_client
|
33
|
+
Aws::EC2::Client.new
|
34
|
+
end
|
35
|
+
|
36
|
+
# Configure the elb client
|
37
|
+
def elb_client
|
38
|
+
Aws::ElasticLoadBalancing::Client.new
|
39
|
+
end
|
40
|
+
|
41
|
+
# Configure the IAM client
|
42
|
+
def iam_client
|
43
|
+
Aws::IAM::Client.new
|
44
|
+
end
|
45
|
+
|
46
|
+
# Configure the S3 client
|
47
|
+
def s3_client
|
48
|
+
Aws::S3::Client.new
|
49
|
+
end
|
50
|
+
|
51
|
+
# Get an STS client so we can request a session token
|
52
|
+
def sts_client
|
53
|
+
Aws::STS::Client.new(
|
54
|
+
credentials: load_secrets,
|
55
|
+
region: 'eu-west-2'
|
56
|
+
)
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
# Assume a role
|
61
|
+
# @param duration A string integer representing the session duration
|
62
|
+
def assume_role(duration = DURATION)
|
63
|
+
# For the benefit of anything downstream we are running
|
64
|
+
export_aws_sudo_file
|
65
|
+
|
66
|
+
if session_valid?
|
67
|
+
# Recover persisted session and use that to update AWS.config
|
68
|
+
Aws.config.update(
|
69
|
+
credentials: Aws::Credentials.new(
|
70
|
+
parse_access_key,
|
71
|
+
parse_secret_access_key,
|
72
|
+
parse_session_token
|
73
|
+
)
|
74
|
+
)
|
75
|
+
else
|
76
|
+
# Session has expired so auth again
|
77
|
+
assume_role_with_mfa_token(duration)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
# Assume a role using an MFA Token
|
82
|
+
def assume_role_with_mfa_token(duration, mfa_code = nil)
|
83
|
+
mfa_code = prompt_for_mfa_code if mfa_code.nil?
|
84
|
+
role_creds = sts_client.assume_role(
|
85
|
+
role_arn: AWSConfig[@profile]['role_arn'],
|
86
|
+
role_session_name: @session,
|
87
|
+
duration_seconds: duration.to_i,
|
88
|
+
serial_number: AWSConfig[@profile]['mfa_serial'],
|
89
|
+
token_code: mfa_code.to_s
|
90
|
+
)
|
91
|
+
update_aws_config(role_creds)
|
92
|
+
persist_aws_sudo(role_creds)
|
93
|
+
end
|
94
|
+
|
95
|
+
# Calculate the session expiration
|
96
|
+
def calculate_session_expiry(duration = DURATION)
|
97
|
+
(Time.now + duration.to_i).strftime('%Y-%m-%d %H:%M:%S')
|
98
|
+
end
|
99
|
+
|
100
|
+
# Get the values for AWS secrets etc and export them to the environment
|
101
|
+
def export_aws_sudo_file
|
102
|
+
return unless File.exists?(AWS_SUDO_FILE)
|
103
|
+
|
104
|
+
File.readlines(AWS_SUDO_FILE).each do |line|
|
105
|
+
case line
|
106
|
+
when MatchesAwsAccessKeyId
|
107
|
+
ENV['AWS_ACCESS_KEY_ID'] = line.split('=')[1].strip
|
108
|
+
when MatchesAwsSecretAccessKey
|
109
|
+
ENV['AWS_SECRET_ACCESS_KEY'] = line.split('=')[1].strip
|
110
|
+
when MatchesAwsSessionToken
|
111
|
+
ENV['AWS_SESSION_TOKEN'] = line.split('=')[1].strip
|
112
|
+
when MatchesAwsSecurityToken
|
113
|
+
ENV['AWS_SECURITY_TOKEN'] = line.split('=')[1].strip
|
114
|
+
when MatchesAwsTokenEtl
|
115
|
+
ENV['AWS_TOKEN_TTL'] = line.split('=')[1].strip
|
116
|
+
when MatchesAwsProfile
|
117
|
+
ENV['AWS_PROFILE'] = line.split('=')[1].strip
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
# Export the AWS values to the ENV
|
123
|
+
def export_config_to_environment(config)
|
124
|
+
ENV['AWS_ACCESS_KEY_ID'] = config.credentials.access_key_id
|
125
|
+
ENV['AWS_SECRET_ACCESS_KEY'] = config.credentials.secret_access_key
|
126
|
+
ENV['AWS_SESSION_TOKEN'] = config.credentials.session_token
|
127
|
+
ENV['AWS_SECURITY_TOKEN'] = config.credentials.session_token
|
128
|
+
ENV['AWS_TOKEN_TTL'] = @token_ttl
|
129
|
+
ENV['AWS_PROFILE'] = @profile
|
130
|
+
end
|
131
|
+
|
132
|
+
# Load the user's AWS Secrets
|
133
|
+
def load_secrets
|
134
|
+
Awsecrets.load
|
135
|
+
end
|
136
|
+
|
137
|
+
# Parse the secret access key from awssudo
|
138
|
+
def parse_access_key
|
139
|
+
File.readlines(AWS_SUDO_FILE).each do |line|
|
140
|
+
return line.split('=')[1].chomp if line.include?('AWS_ACCESS_KEY')
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
# Parse the secret access key from awssudo
|
145
|
+
def parse_secret_access_key
|
146
|
+
File.readlines(AWS_SUDO_FILE).each do |line|
|
147
|
+
return line.split('=')[1].chomp if line.include?('AWS_SECRET_ACCESS_KEY')
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
# Recover the role_arn from the AWS config file
|
152
|
+
def parse_role_arn
|
153
|
+
File.readlines(AWS_CONFIG_FILE).each do |line|
|
154
|
+
return line.split('=')[1].chomp if line.include?('role_arn')
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
# Recover the mfa serial number from AWS config file
|
159
|
+
def parse_mfa_serial
|
160
|
+
File.readlines(AWS_CONFIG_FILE).each do |line|
|
161
|
+
return line.split('=')[1].chomp if line.include?('mfa_serial')
|
162
|
+
end
|
163
|
+
end
|
164
|
+
|
165
|
+
# Parse the session token from awssudo
|
166
|
+
def parse_session_token
|
167
|
+
File.readlines(AWS_SUDO_FILE).each do |line|
|
168
|
+
return line.split('=')[1].chomp if line.include?('AWS_SESSION_TOKEN')
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
# Recover the persisted session timeout from AWSSUDO file
|
173
|
+
def parse_ttl_timeout
|
174
|
+
File.readlines(AWS_SUDO_FILE).each do |line|
|
175
|
+
return line.split('=')[1].chomp if line.include?('AWS_TOKEN_TTL')
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
179
|
+
# Persist the config to the awssudo file
|
180
|
+
def persist_aws_sudo(config, file = AWS_SUDO_FILE)
|
181
|
+
File.open(file, 'w') do |file|
|
182
|
+
file.puts('AWS_ACCESS_KEY_ID=' + config.credentials.access_key_id)
|
183
|
+
file.puts('AWS_SECRET_ACCESS_KEY=' + config.credentials.secret_access_key)
|
184
|
+
file.puts('AWS_SESSION_TOKEN=' + config.credentials.session_token)
|
185
|
+
file.puts('AWS_SECURITY_TOKEN=' + config.credentials.session_token)
|
186
|
+
file.puts('AWS_TOKEN_TTL=' + @token_ttl)
|
187
|
+
file.puts('AWS_PROFILE=' + @profile)
|
188
|
+
end
|
189
|
+
end
|
190
|
+
|
191
|
+
# Prompt the user to supply and MFA code
|
192
|
+
def prompt_for_mfa_code
|
193
|
+
puts 'Enter MFA code: '
|
194
|
+
mfa_token_code = gets.chomp
|
195
|
+
return mfa_token_code unless mfa_token_code.nil? || mfa_token_code.empty?
|
196
|
+
raise(Error, 'No code supplied, aborting')
|
197
|
+
end
|
198
|
+
|
199
|
+
# See if we have a valid session or if it has expired
|
200
|
+
def session_valid?
|
201
|
+
return false unless File.exists?(AWS_SUDO_FILE)
|
202
|
+
File.readlines(AWS_SUDO_FILE).each do |line|
|
203
|
+
next unless line.include?('AWS_TOKEN_TTL')
|
204
|
+
aws_token_ttl = line.split('=')[1]
|
205
|
+
return true if Time.parse(aws_token_ttl) > Time.now
|
206
|
+
return false
|
207
|
+
end
|
208
|
+
false
|
209
|
+
end
|
210
|
+
|
211
|
+
# Update the Aws.config
|
212
|
+
def update_aws_config(role_creds)
|
213
|
+
Aws.config.update(
|
214
|
+
credentials: Aws::Credentials.new(
|
215
|
+
role_creds.credentials.access_key_id,
|
216
|
+
role_creds.credentials.secret_access_key,
|
217
|
+
role_creds.credentials.session_token
|
218
|
+
)
|
219
|
+
)
|
220
|
+
end
|
221
|
+
|
222
|
+
end
|
data/lib/matches.rb
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
require_relative 'matches_aws_access_key_id'
|
2
|
+
require_relative 'matches_aws_profile'
|
3
|
+
require_relative 'matches_aws_secret_access_key'
|
4
|
+
require_relative 'matches_aws_session_token'
|
5
|
+
require_relative 'matches_aws_security_token'
|
6
|
+
require_relative 'matches_aws_token_etl'
|
metadata
ADDED
@@ -0,0 +1,127 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: aws_su
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Bradley Atkins
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-12-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: awsecrets
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :development
|
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: bundler
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '1.17'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '1.17'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '5.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.0'
|
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: 'Developed for a specific use case: User has an AWS id in a master account
|
70
|
+
and wants to assume a role in another account. This module exposes a singleauthenticate()
|
71
|
+
method that handles authentication and switching role by referencing the user''s
|
72
|
+
aws secrets.'
|
73
|
+
email:
|
74
|
+
- Bradley.Atkins@bjss.com
|
75
|
+
executables: []
|
76
|
+
extensions: []
|
77
|
+
extra_rdoc_files: []
|
78
|
+
files:
|
79
|
+
- ".gitignore"
|
80
|
+
- ".ruby-version"
|
81
|
+
- ".travis.yml"
|
82
|
+
- CHANGELOG.md
|
83
|
+
- CODE_OF_CONDUCT.md
|
84
|
+
- Gemfile
|
85
|
+
- LICENSE.txt
|
86
|
+
- README.md
|
87
|
+
- Rakefile
|
88
|
+
- aws_su.gemspec
|
89
|
+
- bin/console
|
90
|
+
- bin/setup
|
91
|
+
- lib/aws_su.rb
|
92
|
+
- lib/aws_su/version.rb
|
93
|
+
- lib/matches.rb
|
94
|
+
- lib/matches_aws_access_key_id.rb
|
95
|
+
- lib/matches_aws_profile.rb
|
96
|
+
- lib/matches_aws_secret_access_key.rb
|
97
|
+
- lib/matches_aws_security_token.rb
|
98
|
+
- lib/matches_aws_session_token.rb
|
99
|
+
- lib/matches_aws_token_etl.rb
|
100
|
+
homepage: https://github.com/museadmin
|
101
|
+
licenses:
|
102
|
+
- MIT
|
103
|
+
metadata:
|
104
|
+
homepage_uri: https://github.com/museadmin
|
105
|
+
source_code_uri: https://github.com/museadmin/aws_su
|
106
|
+
changelog_uri: Put your gem's CHANGELOG.md URL here.
|
107
|
+
post_install_message:
|
108
|
+
rdoc_options: []
|
109
|
+
require_paths:
|
110
|
+
- lib
|
111
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
112
|
+
requirements:
|
113
|
+
- - ">="
|
114
|
+
- !ruby/object:Gem::Version
|
115
|
+
version: '0'
|
116
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
117
|
+
requirements:
|
118
|
+
- - ">="
|
119
|
+
- !ruby/object:Gem::Version
|
120
|
+
version: '0'
|
121
|
+
requirements: []
|
122
|
+
rubyforge_project:
|
123
|
+
rubygems_version: 2.6.13
|
124
|
+
signing_key:
|
125
|
+
specification_version: 4
|
126
|
+
summary: Gem to wrap helper methods around AWS authentication API
|
127
|
+
test_files: []
|