Skip to content

Commit

Permalink
Merge pull request #62 from gorup/master
Browse files Browse the repository at this point in the history
 Add new STS Session credential pulling utility for help use of CodeDeploy IAM Session support
  • Loading branch information
yubangxi authored Dec 28, 2016
2 parents 258df05 + de05b06 commit 6109349
Show file tree
Hide file tree
Showing 9 changed files with 649 additions and 0 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ These fall into one of a few different categories, including:
- Integrations with load-balancers like Elastic Load Balancing
- Sample hooks for version control systems like Git.

Also included is a utility to pull STS credentials for CodeDeploy's IAM Session support

Note: THe STS Credentials tool may move to its own repo in the future.

License
=======

Expand Down
177 changes: 177 additions & 0 deletions utilities/aws-codedeploy-session-helper/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@

Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/

TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION

1. Definitions.

"License" shall mean the terms and conditions for use, reproduction,
and distribution as defined by Sections 1 through 9 of this document.

"Licensor" shall mean the copyright owner or entity authorized by
the copyright owner that is granting the License.

"Legal Entity" shall mean the union of the acting entity and all
other entities that control, are controlled by, or are under common
control with that entity. For the purposes of this definition,
"control" means (i) the power, direct or indirect, to cause the
direction or management of such entity, whether by contract or
otherwise, or (ii) ownership of fifty percent (50%) or more of the
outstanding shares, or (iii) beneficial ownership of such entity.

"You" (or "Your") shall mean an individual or Legal Entity
exercising permissions granted by this License.

"Source" form shall mean the preferred form for making modifications,
including but not limited to software source code, documentation
source, and configuration files.

"Object" form shall mean any form resulting from mechanical
transformation or translation of a Source form, including but
not limited to compiled object code, generated documentation,
and conversions to other media types.

"Work" shall mean the work of authorship, whether in Source or
Object form, made available under the License, as indicated by a
copyright notice that is included in or attached to the work
(an example is provided in the Appendix below).

"Derivative Works" shall mean any work, whether in Source or Object
form, that is based on (or derived from) the Work and for which the
editorial revisions, annotations, elaborations, or other modifications
represent, as a whole, an original work of authorship. For the purposes
of this License, Derivative Works shall not include works that remain
separable from, or merely link (or bind by name) to the interfaces of,
the Work and Derivative Works thereof.

"Contribution" shall mean any work of authorship, including
the original version of the Work and any modifications or additions
to that Work or Derivative Works thereof, that is intentionally
submitted to Licensor for inclusion in the Work by the copyright owner
or by an individual or Legal Entity authorized to submit on behalf of
the copyright owner. For the purposes of this definition, "submitted"
means any form of electronic, verbal, or written communication sent
to the Licensor or its representatives, including but not limited to
communication on electronic mailing lists, source code control systems,
and issue tracking systems that are managed by, or on behalf of, the
Licensor for the purpose of discussing and improving the Work, but
excluding communication that is conspicuously marked or otherwise
designated in writing by the copyright owner as "Not a Contribution."

"Contributor" shall mean Licensor and any individual or Legal Entity
on behalf of whom a Contribution has been received by Licensor and
subsequently incorporated within the Work.

2. Grant of Copyright License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
copyright license to reproduce, prepare Derivative Works of,
publicly display, publicly perform, sublicense, and distribute the
Work and such Derivative Works in Source or Object form.

3. Grant of Patent License. Subject to the terms and conditions of
this License, each Contributor hereby grants to You a perpetual,
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
(except as stated in this section) patent license to make, have made,
use, offer to sell, sell, import, and otherwise transfer the Work,
where such license applies only to those patent claims licensable
by such Contributor that are necessarily infringed by their
Contribution(s) alone or by combination of their Contribution(s)
with the Work to which such Contribution(s) was submitted. If You
institute patent litigation against any entity (including a
cross-claim or counterclaim in a lawsuit) alleging that the Work
or a Contribution incorporated within the Work constitutes direct
or contributory patent infringement, then any patent licenses
granted to You under this License for that Work shall terminate
as of the date such litigation is filed.

4. Redistribution. You may reproduce and distribute copies of the
Work or Derivative Works thereof in any medium, with or without
modifications, and in Source or Object form, provided that You
meet the following conditions:

(a) You must give any other recipients of the Work or
Derivative Works a copy of this License; and

(b) You must cause any modified files to carry prominent notices
stating that You changed the files; and

(c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and
attribution notices from the Source form of the Work,
excluding those notices that do not pertain to any part of
the Derivative Works; and

(d) If the Work includes a "NOTICE" text file as part of its
distribution, then any Derivative Works that You distribute must
include a readable copy of the attribution notices contained
within such NOTICE file, excluding those notices that do not
pertain to any part of the Derivative Works, in at least one
of the following places: within a NOTICE text file distributed
as part of the Derivative Works; within the Source form or
documentation, if provided along with the Derivative Works; or,
within a display generated by the Derivative Works, if and
wherever such third-party notices normally appear. The contents
of the NOTICE file are for informational purposes only and
do not modify the License. You may add Your own attribution
notices within Derivative Works that You distribute, alongside
or as an addendum to the NOTICE text from the Work, provided
that such additional attribution notices cannot be construed
as modifying the License.

You may add Your own copyright statement to Your modifications and
may provide additional or different license terms and conditions
for use, reproduction, or distribution of Your modifications, or
for any such Derivative Works as a whole, provided Your use,
reproduction, and distribution of the Work otherwise complies with
the conditions stated in this License.

5. Submission of Contributions. Unless You explicitly state otherwise,
any Contribution intentionally submitted for inclusion in the Work
by You to the Licensor shall be under the terms and conditions of
this License, without any additional terms or conditions.
Notwithstanding the above, nothing herein shall supersede or modify
the terms of any separate license agreement you may have executed
with Licensor regarding such Contributions.

6. Trademarks. This License does not grant permission to use the trade
names, trademarks, service marks, or product names of the Licensor,
except as required for reasonable and customary use in describing the
origin of the Work and reproducing the content of the NOTICE file.

7. Disclaimer of Warranty. Unless required by applicable law or
agreed to in writing, Licensor provides the Work (and each
Contributor provides its Contributions) on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
implied, including, without limitation, any warranties or conditions
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
PARTICULAR PURPOSE. You are solely responsible for determining the
appropriateness of using or redistributing the Work and assume any
risks associated with Your exercise of permissions under this License.

8. Limitation of Liability. In no event and under no legal theory,
whether in tort (including negligence), contract, or otherwise,
unless required by applicable law (such as deliberate and grossly
negligent acts) or agreed to in writing, shall any Contributor be
liable to You for damages, including any direct, indirect, special,
incidental, or consequential damages of any character arising as a
result of this License or out of the use or inability to use the
Work (including but not limited to damages for loss of goodwill,
work stoppage, computer failure or malfunction, or any and all
other commercial damages or losses), even if such Contributor
has been advised of the possibility of such damages.

9. Accepting Warranty or Additional Liability. While redistributing
the Work or Derivative Works thereof, You may choose to offer,
and charge a fee for, acceptance of support, warranty, indemnity,
or other liability obligations and/or rights consistent with this
License. However, in accepting such obligations, You may act only
on Your own behalf and on Your sole responsibility, not on behalf
of any other Contributor, and only if You agree to indemnify,
defend, and hold each Contributor harmless for any liability
incurred by, or claims asserted against, such Contributor by reason
of your accepting any such warranty or additional liability.

END OF TERMS AND CONDITIONS
100 changes: 100 additions & 0 deletions utilities/aws-codedeploy-session-helper/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# aws-codedeploy-session-helper Tool | get_sts_creds
##Overview
The aws-codedeploy-session-helper get_sts_creds tool enables customers to generate AWS STS credentials and write them to a file. Just provide the aws-codedeploy-session-helper tool some existing credentials and a file location to write the files to, and the tool will retrieve the credential and write them to the requested file. This tool is best paired with static credentials on the box and a cron job to automatically pull the STS credentials.
##Usage
`bin/get_sts_creds` will use existing credentials to retrieve STS credentials, then write the credentials to a file location of your choosing. By default, the session name of the STS credentials is the HOSTNAME of the instance executing the tool. More information about STS credentials can be found [here](http://docs.aws.amazon.com/STS/latest/APIReference/API_AssumeRole.html).
```
$ bin/get_sts_creds --help
<displays help>
$ bin/get_sts_creds --role-arn arn:aws:iam::123456789012:role/foo --file /var/tmp/foo
$ cat /var/tmp/foo
[default]
aws_access_key_id = AKIAIOSFODNN7EXAMPLE
aws_secret_access_key = wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY
aws_session_token = AQoEXAMPLEH4aoAH0gNCAPyJxz4BlCFFxWNE1OPTgk5TthT+FvwqnKwRcOIfrRh3c/LTo6UDdyJwOOvEVPvLXCrrrUtdnniCEXAMPLE/IvU1dYUg2RVAJBanLiHb4IgRmpRV3zrkuWJOgQs8IZZaIv2BXIa2R4OlgkBN9bkUDNCJiBeb/AXlzBBko7b15fjrBs2+cTQtpZ3CYWFXG8C5zqx37wnOE49mRl/+OtkIKGO7fAE
$
```
### Providing credentials to the tool
Credentials can be provided to the tool in a few different ways. Following the AWS Ruby SDK's [documentation](http://docs.aws.amazon.com/sdk-for-ruby/v2/developer-guide/setup-config.html), credentials can be provided by exporting environment variables or having the credentials present at a specific file on the box.
#### Environment variables
Linux
```
export AWS_ACCESS_KEY_ID=your_access_key_id
export AWS_SECRET_ACCESS_KEY=your_secret_access_key
```
Windows
```
set AWS_ACCESS_KEY_ID=your_access_key_id
set AWS_SECRET_ACCESS_KEY=your_secret_access_key
```
#### File
File format
```
[default]
aws_access_key_id = your_access_key_id
aws_secret_access_key = your_secret_access_key
```
File location - Linux
```
~/.aws/credentials
```
File location - Windows
```
%HOMEPATH%\.aws\credentials
```
### Working with AWS CodeDeploy
This tool will help customers of [AWS CodeDeploy](https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=1&cad=rja&uact=8&ved=0ahUKEwjqoYOU8-LPAhXLsFQKHV4ZDzAQFggeMAA&url=https%3A%2F%2Faws.amazon.com%2Fcodedeploy%2F&usg=AFQjCNFJWFZ0JuuOFbyE390fglFUUi9sXA&sig2=fDXnI7wC1k6B578J-zPDuw)'s OnPremises instance deployment feature by enabling the retrieval and refreshment of the STS credentials that the on premises instance needs. Customers can quickly integrate on premises instances with AWS CodeDeploy by using the AWS CodeDeploy IAM Session support feature [LINK].

You can use this tool with a centralized STS credential broker service distribute credentials to other on premises instances, or deploy a fixed IAM User to each host, then have each host use that IAM user's credentials with this tool to retrieve STS credentials.

When using the `--print-session-arn` flag, you can see the IAM Session ARN that the host will assume. Use this ARN when registering with CodeDeploy. This ARN does not change as long as the arguments to the tool do not change, or the host the tool is running on does not change, as the tool uses the HOSTNAME of the host by default.

## Installation
This tool only requires the Ruby and the aws-sdk-core gem to be installed. This tool can be installed as a ruby gem, or just ran once unzipped via `git clone`.

This has been tested to work with Ruby versions 2.3.1, 2.3.0, 2.2.2, 2.1.10, 2.0.0, and 1.9.3 on Linux.

Note: When using the gem for installation, get_sts_creds will be added to your PATH.
### Amazon Linux
```
// Amazon Linux should have git, ruby, and gem installed
$ gem install aws-sdk-core
$ git clone https://github.com/awslabs/aws-codedeploy-samples.git OR gem install aws-codedeploy-session-helper
$ utilities/aws-codedeploy-session-helper/bin/get_sts_creds <args>
```
### Ubuntu
```
$ sudo apt-get install ruby // if not installed
$ sudo apt-get install rubygems // if not installed. For 14.04, try rubygems-integration
$ sudo apt-get install git // if not installed and using git clone to install instead of gem
$ sudo gem install aws-sdk-core
$ git clone https://github.com/awslabs/aws-codedeploy-samples.git OR gem install aws-codedeploy-session-helper
$ utilities/aws-codedeploy-session-helper/bin/get_sts_creds <args>
```
### Windows
1. Install Ruby from rubyinstaller.org (the 2.3 version comes w/ Gem)
1. `> gem install aws-sdk-core`
1. Install Git from https://git-scm.com/download/win
1. `> git clone https://github.com/awslabs/aws-codedeploy-samples.git` OR `> gem install aws-codedeploy-session-helper`
1. `> chdir <install dir>`
1. `> ruby utilities/aws-codedeploy-session-helper/bin/get_sts_creds <args>`
## Automation
For a Linux machine, you can automate the execution of this tool with the following cron
```
0,15,30,45 * * * * /path/to/tool/bin/get_sts_creds <args>
```

## Testing / Development
If you wish to execute the tests for this tool, you must have the `rspec` and `simplecov` gems installed. Then execute
```
$ cd <aws-codedeploy-session-helper dir>
$ rake
```
The rake task executes syntax checks and tests that cover the CLI's failure cases in addition to the tool's backing library.

To build the gem, just run

```
$ cd <aws-codedeploy-session-helper dir>
$ gem build aws-codedeploy-session-helper.gemspec
```
25 changes: 25 additions & 0 deletions utilities/aws-codedeploy-session-helper/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'rubygems'
require 'rspec/core/rake_task'

task :syntax do
dirs = [ 'bin/*', 'lib/*', 'spec/*']
dirs.each do |d|
ret = check_syntax(d)
exit_code = $?
puts ret
if !(/\ASyntax OK\Z/ =~ ret) or exit_code != 0
puts "Build failed, see above syntax error or warning"
exit(1)
end
end
end

def check_syntax(dir)
return `ruby --verbose -c #{dir} 2>&1`
end

RSpec::Core::RakeTask.new(:spec) do |t|
t.pattern = 'spec/**/*.spec'
end

task :default => [:syntax, :spec]
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
Gem::Specification.new do |s|
s.name = 'aws-codedeploy-session-helper'
s.version = '0.9.0'
s.date = '2016-12-27'
s.summary = "This tool helps grab AWS STS credentials, useful when using CodeDeploy OnPremises instances with IAM Sessions."
s.description = "See readme of the code on GitHub, https://github.com/aws-labs/aws-codedeploy-samples/tree/master/utilities/aws-codedeploy-session-helper"
s.authors = ["Ryan Gorup"]
s.email = 'gorup@amazon.com'
s.files = ["lib/STSCredentialsProvider.rb"]
s.executables = ["get_sts_creds"]
s.homepage =
'https://github.com/aws-labs/aws-codedeploy-samples/tree/master/utilities/aws-codedeploy-session-helper'
s.license = 'Apache-2.0'

s.add_runtime_dependency('aws-sdk-core', '~> 2.6')
s.add_development_dependency('rake', '~> 0.9')
s.add_development_dependency('simplecov', '~> 0.12')
s.add_development_dependency('rspec', '~> 3.5')
end
44 changes: 44 additions & 0 deletions utilities/aws-codedeploy-session-helper/bin/get_sts_creds
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/usr/bin/env ruby

require 'optparse'
require File.expand_path('../../lib/STSCredentialsProvider', __FILE__)

# Setup parser
options = {}
OptionParser.new do |parser|
parser.banner = "Usage: get_sts_creds --role-arn ROLE_ARN --file FILEPATH [options]"
parser.on("-h", "--help", "Show this help message", "\tNote: Pass credentials using environment variables or credentials file, see http://docs.aws.amazon.com/sdk-for-ruby/v2/developer-guide/setup-config.html") do ||
puts parser
exit
end

parser.on("--role-arn ROLE_ARN", "[required] The IAM ARN that the AWS IAM Role that the session credentials will assume",
"\tExample: '--role-arn arn:aws:iam::123456789012:role/FooRole'") do |v|
options[:role] = v
end
parser.on("--file FILEPATH", "[required] The fully qualified file path that the session credentials will be writen to",
"\tExample: '--file /foo/baz/aws_session_creds'") do |v|
options[:creds_file] = v
end
parser.on("--region REGION", "The AWS region for configuring outbound AWS calls",
"By default, this tool will check the AWS_REGION environment variable", "\tExample: '--region us-east-1'") do |v|
options[:region] = v
end
parser.on("--session-name-override SESSION_NAME", "The name for the IAM Session that will be included in the AWS STS Session ARN",
"By default, this tool will use the HOSTNAME of the current.",
"\tExample: '--session-name-override test-session'") do |v|
options[:session_name] = v
end
parser.on("--duration SECONDS", "The duration (in seconds) of the life of the session credentials that this tool generates",
"Defaults to 3600, or one hour. Value must be between 900 and 3600",
"\tExample: '--duration 1800'") do |v|
options[:duration] = v
end
parser.on("--print-session-arn", "Provide this flag to have the tool output (STDOUT) the ARN of the STS Session",
"Defaults to not printing arn") do |v|
options[:output_arn] = v
end
end.parse!

provider = STSCredentialsProvider.new(options)
provider.get
Loading

0 comments on commit 6109349

Please sign in to comment.