aws-rotate 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: adf6a451f691065bc62e8feb7b43704bc603a438b05b9a3f092bc336ea8e3288
4
+ data.tar.gz: fa9607d7253c854924f471fec41e6526239a7c1b0e1609590b6d07691fcc6e25
5
+ SHA512:
6
+ metadata.gz: c981cc84a81b3efe9e7901a2942f155f36723f51afd4996e55285839127cc4865283844cf6e16987afe305e08b3d9d208fe1ccb7d2a31827652a4f767afa4623
7
+ data.tar.gz: 2be376dcc9cb074db6b214c83c6e74b794185b50bb144dcd58e5607360a78f67bbb59c5d17497c32fc0b33282fbc8e39f300ee6c5f4c1d48806c78ee56698416
@@ -0,0 +1,16 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ _yardoc
7
+ coverage
8
+ doc/
9
+ InstalledFiles
10
+ lib/bundler/man
11
+ pkg
12
+ rdoc
13
+ spec/reports
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --require spec_helper
2
+ --color
3
+ --format documentation
@@ -0,0 +1,7 @@
1
+ # Change Log
2
+
3
+ All notable changes to this project will be documented in this file.
4
+ This project *tries* to adhere to [Semantic Versioning](http://semver.org/), even before v1.0.
5
+
6
+ ## [0.1.0]
7
+ - Initial release.
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem dependencies in aws-rotate.gemspec
4
+ gemspec
5
+
6
+ group :test do
7
+ gem "codeclimate-test-reporter", require: nil
8
+ gem "webmock"
9
+ end
@@ -0,0 +1,95 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ aws-rotate (0.1.0)
5
+ activesupport
6
+ aws-sdk-core
7
+ aws-sdk-iam
8
+ rainbow
9
+ thor
10
+ zeitwerk
11
+
12
+ GEM
13
+ remote: https://rubygems.org/
14
+ specs:
15
+ activesupport (5.2.3)
16
+ concurrent-ruby (~> 1.0, >= 1.0.2)
17
+ i18n (>= 0.7, < 2)
18
+ minitest (~> 5.1)
19
+ tzinfo (~> 1.1)
20
+ addressable (2.6.0)
21
+ public_suffix (>= 2.0.2, < 4.0)
22
+ aws-eventstream (1.0.3)
23
+ aws-partitions (1.201.0)
24
+ aws-sdk-core (3.62.0)
25
+ aws-eventstream (~> 1.0, >= 1.0.2)
26
+ aws-partitions (~> 1.0)
27
+ aws-sigv4 (~> 1.1)
28
+ jmespath (~> 1.0)
29
+ aws-sdk-iam (1.29.0)
30
+ aws-sdk-core (~> 3, >= 3.61.1)
31
+ aws-sigv4 (~> 1.1)
32
+ aws-sigv4 (1.1.0)
33
+ aws-eventstream (~> 1.0, >= 1.0.2)
34
+ byebug (11.0.1)
35
+ cli_markdown (0.1.0)
36
+ codeclimate-test-reporter (1.0.9)
37
+ simplecov (<= 0.13)
38
+ concurrent-ruby (1.1.5)
39
+ crack (0.4.3)
40
+ safe_yaml (~> 1.0.0)
41
+ diff-lcs (1.3)
42
+ docile (1.1.5)
43
+ hashdiff (1.0.0)
44
+ i18n (1.6.0)
45
+ concurrent-ruby (~> 1.0)
46
+ jmespath (1.4.0)
47
+ json (2.2.0)
48
+ minitest (5.11.3)
49
+ public_suffix (3.1.1)
50
+ rainbow (3.0.0)
51
+ rake (12.3.3)
52
+ rspec (3.8.0)
53
+ rspec-core (~> 3.8.0)
54
+ rspec-expectations (~> 3.8.0)
55
+ rspec-mocks (~> 3.8.0)
56
+ rspec-core (3.8.2)
57
+ rspec-support (~> 3.8.0)
58
+ rspec-expectations (3.8.4)
59
+ diff-lcs (>= 1.2.0, < 2.0)
60
+ rspec-support (~> 3.8.0)
61
+ rspec-mocks (3.8.1)
62
+ diff-lcs (>= 1.2.0, < 2.0)
63
+ rspec-support (~> 3.8.0)
64
+ rspec-support (3.8.2)
65
+ safe_yaml (1.0.5)
66
+ simplecov (0.13.0)
67
+ docile (~> 1.1.0)
68
+ json (>= 1.8, < 3)
69
+ simplecov-html (~> 0.10.0)
70
+ simplecov-html (0.10.2)
71
+ thor (0.20.3)
72
+ thread_safe (0.3.6)
73
+ tzinfo (1.2.5)
74
+ thread_safe (~> 0.1)
75
+ webmock (3.6.2)
76
+ addressable (>= 2.3.6)
77
+ crack (>= 0.3.2)
78
+ hashdiff (>= 0.4.0, < 2.0.0)
79
+ zeitwerk (2.1.9)
80
+
81
+ PLATFORMS
82
+ ruby
83
+
84
+ DEPENDENCIES
85
+ aws-rotate!
86
+ bundler
87
+ byebug
88
+ cli_markdown
89
+ codeclimate-test-reporter
90
+ rake
91
+ rspec
92
+ webmock
93
+
94
+ BUNDLED WITH
95
+ 2.0.2
@@ -0,0 +1,19 @@
1
+ guard "bundler", cmd: "bundle" do
2
+ watch("Gemfile")
3
+ watch(/^.+\.gemspec/)
4
+ end
5
+
6
+ guard :rspec, cmd: "bundle exec rspec" do
7
+ require "guard/rspec/dsl"
8
+ dsl = Guard::RSpec::Dsl.new(self)
9
+
10
+ # RSpec files
11
+ rspec = dsl.rspec
12
+ watch(rspec.spec_helper) { rspec.spec_dir }
13
+ watch(rspec.spec_support) { rspec.spec_dir }
14
+ watch(rspec.spec_files)
15
+
16
+ # Ruby files
17
+ ruby = dsl.ruby
18
+ dsl.watch_spec_files_for(ruby.lib_files)
19
+ end
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2019 Tung Nguyen
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.
@@ -0,0 +1,103 @@
1
+ # AwsRotate
2
+
3
+ [![Gem Version](https://badge.fury.io/rb/aws-rotate.png)](http://badge.fury.io/rb/aws-rotate)
4
+ [![Support](https://img.shields.io/badge/get-support-blue.svg)](https://boltops.com?utm_source=badge&utm_medium=badge&utm_campaign=aws-rotate)
5
+
6
+ Rotates your AWS keys configured in `~/.aws/credentials`.
7
+
8
+ ## Usage
9
+
10
+ aws-rotate list # list profiles in ~/.aws
11
+ aws-rotate key # rotates single key. Uses AWS_PROFILE env var
12
+ aws-rotate keys # rotates **all** keys for all profiles in ~/.aws/credentials
13
+
14
+ ## aws-rotate keys
15
+
16
+ IMPORTANT: The `aws-rotate keys` command will update **all** the profiles found in `~/.aws/credentials`. You may want to run an `--noop` to first test. Example:
17
+
18
+ aws-rotate keys --noop
19
+
20
+ ### select filter option
21
+
22
+ If you would like to selectively update profiles, you can use the `--select` option. The `-s` option is also shorthand for the `--select` option. Example:
23
+
24
+ aws-rotate keys --select dev-
25
+
26
+ The `--select dev-` results in only profiles with the `dev-` found in the profile name to be updated. Example:
27
+
28
+ ~/.aws/credentials:
29
+
30
+ [my-dev-account1]
31
+ aws_access_key_id=EXAMPLE1
32
+ aws_secret_access_key=EXAMPLE1
33
+
34
+ [my-dev-account2]
35
+ aws_access_key_id=EXAMPLE2
36
+ aws_secret_access_key=EXAMPLE2
37
+
38
+ [my-prod-account]
39
+ aws_access_key_id=EXAMPLE3
40
+ aws_secret_access_key=EXAMPLE3
41
+
42
+ Will only update `my-dev-account1` and `my-dev-account1`, since they both include the `dev-` pattern.
43
+
44
+ The select option can take multiple selects. Example:
45
+
46
+ aws-rotate keys --select dev- test-
47
+
48
+ Also, the select option is internally converted to an ruby regexp. So you can use patterns. Example:
49
+
50
+ aws-rotate keys --select ^dev-
51
+
52
+ In this case the match is stricter and must start with "dev"
53
+
54
+ ### reject filter option
55
+
56
+ There is also a `--reject` and `-r` option that does the opposite of the `--select` option.
57
+
58
+ aws-rotate keys --reject ^prod-
59
+
60
+ Will rotate all profiles that do not match `^prod-`.
61
+
62
+ You can use both `--select` and `--reject` options together.
63
+
64
+ ## Backups
65
+
66
+ A backup of your `~/.aws/credentials` file is taken and stored in `~/.aws/credentials-bak-[timestamp]` before it is updated. However, please take precaution and take your own backup measures. You can also disable backups with the `--no-backup` option.
67
+
68
+ ## Assume Roles
69
+
70
+ Note: assumed role profiles are skipped as they don't have access keys.
71
+
72
+ ## Automatically Updating with Cron
73
+
74
+ You can add a crontab to your system to automatically rotate the keys:
75
+
76
+ crontab -e
77
+
78
+ You can add something like this:
79
+
80
+ 30 20 * * * bash -l -c 'aws-rotate keys --select dev-aws-profile test-aws-profile --no-backup >> /var/log/cron-aws-rotate.log 2>&1' # rotate AWS keys daily
81
+
82
+ Create a `/var/log/cron/aws-rotate.log` that is writable with your user:
83
+
84
+ sudo touch /var/log/cron-aws-rotate.log
85
+ sudo chown `whoami`:`whoami` /var/log/cron-aws-rotate.log
86
+
87
+ ## Installation
88
+
89
+ Add this line to your application's Gemfile:
90
+
91
+ gem "aws-rotate"
92
+
93
+ ## Requirements
94
+
95
+ The [aws cli](https://aws.amazon.com/cli/) is use to set the access keys and is required.
96
+
97
+ ## Contributing
98
+
99
+ 1. Fork it
100
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
101
+ 3. Commit your changes (`git commit -am "Add some feature"`)
102
+ 4. Push to the branch (`git push origin my-new-feature`)
103
+ 5. Create new Pull Request
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ task default: :spec
5
+
6
+ RSpec::Core::RakeTask.new
7
+
8
+ require_relative "lib/aws-rotate"
9
+ require "cli_markdown"
10
+ desc "Generates cli reference docs as markdown"
11
+ task :docs do
12
+ mkdir_p "docs/_includes"
13
+ CliMarkdown::Creator.create_all(cli_class: AwsRotate::CLI, cli_name: "aws-rotate")
14
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "aws_rotate/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "aws-rotate"
8
+ spec.version = AwsRotate::VERSION
9
+ spec.authors = ["Tung Nguyen"]
10
+ spec.email = ["tongueroo@gmail.com"]
11
+ spec.summary = "Easy way to rotate all your AWS keys in your ~/.aws/credentials"
12
+ spec.homepage = "https://github.com/tongueroo/aws-rotate"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files`.split($/)
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "activesupport"
22
+ spec.add_dependency "aws-sdk-iam"
23
+ spec.add_dependency "aws-sdk-core" # for sts
24
+ spec.add_dependency "rainbow"
25
+ spec.add_dependency "thor"
26
+ spec.add_dependency "zeitwerk"
27
+
28
+ spec.add_development_dependency "bundler"
29
+ spec.add_development_dependency "byebug"
30
+ spec.add_development_dependency "cli_markdown"
31
+ spec.add_development_dependency "rake"
32
+ spec.add_development_dependency "rspec"
33
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # Trap ^C
4
+ Signal.trap("INT") {
5
+ puts "\nCtrl-C detected. Exiting..."
6
+ sleep 0.1
7
+ exit
8
+ }
9
+
10
+ $:.unshift(File.expand_path("../../lib", __FILE__))
11
+ require "aws-rotate"
12
+ require "aws_rotate/cli"
13
+
14
+ AwsRotate::CLI.start(ARGV)
@@ -0,0 +1 @@
1
+ require_relative "aws_rotate"
@@ -0,0 +1,10 @@
1
+ $:.unshift(File.expand_path("../", __FILE__))
2
+ require "aws_rotate/version"
3
+ require "rainbow/ext/string"
4
+
5
+ require "aws_rotate/autoloader"
6
+ AwsRotate::Autoloader.setup
7
+
8
+ module AwsRotate
9
+ class Error < StandardError; end
10
+ end
@@ -0,0 +1,22 @@
1
+ require "zeitwerk"
2
+
3
+ module AwsRotate
4
+ class Autoloader
5
+ class Inflector < Zeitwerk::Inflector
6
+ def camelize(basename, _abspath)
7
+ map = { cli: "CLI", version: "VERSION" }
8
+ map[basename.to_sym] || super
9
+ end
10
+ end
11
+
12
+ class << self
13
+ def setup
14
+ loader = Zeitwerk::Loader.new
15
+ loader.inflector = Inflector.new
16
+ loader.push_dir(File.dirname(__dir__)) # lib
17
+ loader.ignore("#{File.dirname(__dir__)}/aws-rotate.rb")
18
+ loader.setup
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,17 @@
1
+ require "aws-sdk-iam"
2
+ require "aws-sdk-sts"
3
+
4
+ module AwsRotate
5
+ module AwsServices
6
+ # Memoization takes into account different AWS_PROFILE
7
+ @@iam = {}
8
+ def iam
9
+ @@iam[ENV['AWS_PROFILE']] ||= Aws::IAM::Client.new
10
+ end
11
+
12
+ @@sts = {}
13
+ def sts
14
+ @@sts[ENV['AWS_PROFILE']] ||= Aws::STS::Client.new
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,15 @@
1
+ module AwsRotate
2
+ class Backup < Base
3
+ def run
4
+ return if @options[:noop] || @options[:backup] == false
5
+ return unless credentials_exist?
6
+ backup_path = @credentials_path + ".bak-#{Time.now.strftime("%F-%T")}"
7
+ FileUtils.cp(@credentials_path, backup_path)
8
+ puts "Backed up credentials file at: #{backup_path}"
9
+ end
10
+
11
+ def credentials_exist?
12
+ File.exist?(@credentials_path)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,30 @@
1
+ module AwsRotate
2
+ class Base
3
+ include AwsServices
4
+
5
+ def initialize(options={})
6
+ @options = options
7
+ @config_path = options[:config] || "#{ENV['HOME']}/.aws/config"
8
+ @credentials_path = options[:credentials] || "#{ENV['HOME']}/.aws/credentials"
9
+ @profile = ENV['AWS_PROFILE'] || default_profile
10
+ end
11
+
12
+ private
13
+ def default_profile
14
+ if ENV['AWS_PROFILE'].nil?
15
+ lines = IO.readlines(@credentials_path)
16
+ default_found = lines.detect { |l| l =~ /\[default\]/ }
17
+ 'default'
18
+ else
19
+ abort("AWS_PROFILE must be set")
20
+ end
21
+ end
22
+
23
+ def sh(command)
24
+ # no puts so we dont puts out the secret key value
25
+ # puts "=> #{command}" # uncomment to debug
26
+ success = system(command)
27
+ raise unless success
28
+ end
29
+ end
30
+ end