aws-rotate 0.1.0
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 +7 -0
- data/.gitignore +16 -0
- data/.rspec +3 -0
- data/CHANGELOG.md +7 -0
- data/Gemfile +9 -0
- data/Gemfile.lock +95 -0
- data/Guardfile +19 -0
- data/LICENSE.txt +22 -0
- data/README.md +103 -0
- data/Rakefile +14 -0
- data/aws-rotate.gemspec +33 -0
- data/exe/aws-rotate +14 -0
- data/lib/aws-rotate.rb +1 -0
- data/lib/aws_rotate.rb +10 -0
- data/lib/aws_rotate/autoloader.rb +22 -0
- data/lib/aws_rotate/aws_services.rb +17 -0
- data/lib/aws_rotate/backup.rb +15 -0
- data/lib/aws_rotate/base.rb +30 -0
- data/lib/aws_rotate/cache_key.rb +3 -0
- data/lib/aws_rotate/cli.rb +47 -0
- data/lib/aws_rotate/command.rb +82 -0
- data/lib/aws_rotate/completer.rb +159 -0
- data/lib/aws_rotate/completer/script.rb +6 -0
- data/lib/aws_rotate/completer/script.sh +10 -0
- data/lib/aws_rotate/help.rb +9 -0
- data/lib/aws_rotate/help/completion.md +22 -0
- data/lib/aws_rotate/help/completion_script.md +3 -0
- data/lib/aws_rotate/help/key.md +4 -0
- data/lib/aws_rotate/help/keys.md +16 -0
- data/lib/aws_rotate/help/list.md +3 -0
- data/lib/aws_rotate/key.rb +150 -0
- data/lib/aws_rotate/keys.rb +42 -0
- data/lib/aws_rotate/list.rb +20 -0
- data/lib/aws_rotate/version.rb +3 -0
- data/spec/fixtures/home/.aws/config +15 -0
- data/spec/fixtures/home/.aws/credentials +15 -0
- data/spec/lib/cli_spec.rb +12 -0
- data/spec/lib/key_spec.rb +52 -0
- data/spec/lib/keys_spec.rb +60 -0
- data/spec/spec_helper.rb +33 -0
- metadata +243 -0
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
module AwsRotate
|
|
2
|
+
class CLI < Command
|
|
3
|
+
class_option :verbose, type: :boolean
|
|
4
|
+
class_option :noop, type: :boolean
|
|
5
|
+
|
|
6
|
+
desc "list", "list profiles in ~/.aws"
|
|
7
|
+
long_desc Help.text(:list)
|
|
8
|
+
def list
|
|
9
|
+
List.new(options).run
|
|
10
|
+
end
|
|
11
|
+
|
|
12
|
+
desc "key", "rotate key for AWS_PROFILE profile"
|
|
13
|
+
long_desc Help.text(:key)
|
|
14
|
+
option :backup, type: :boolean, default: true, desc: "Enable backup"
|
|
15
|
+
def key
|
|
16
|
+
Backup.new(options).run
|
|
17
|
+
Key.new(options).run
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
desc "keys", "rotate keys for all profiles in ~/.aws/credentials"
|
|
21
|
+
long_desc Help.text(:keys)
|
|
22
|
+
option :select, aliases: :s, type: :array, desc: "Select filter. List of patterns to select profiles for updating"
|
|
23
|
+
option :reject, aliases: :r, type: :array, desc: "Reject filter. List of patterns to reject profiles for updating"
|
|
24
|
+
option :backup, type: :boolean, default: true, desc: "Enable backup"
|
|
25
|
+
def keys
|
|
26
|
+
Backup.new(options).run
|
|
27
|
+
Keys.new(options).run
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
desc "completion *PARAMS", "Prints words for auto-completion."
|
|
31
|
+
long_desc Help.text("completion")
|
|
32
|
+
def completion(*params)
|
|
33
|
+
Completer.new(CLI, *params).run
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
desc "completion_script", "Generates a script that can be eval to setup auto-completion."
|
|
37
|
+
long_desc Help.text("completion_script")
|
|
38
|
+
def completion_script
|
|
39
|
+
Completer::Script.generate
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
desc "version", "prints version"
|
|
43
|
+
def version
|
|
44
|
+
puts VERSION
|
|
45
|
+
end
|
|
46
|
+
end
|
|
47
|
+
end
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
require "thor"
|
|
2
|
+
|
|
3
|
+
# Override thor's long_desc identation behavior
|
|
4
|
+
# https://github.com/erikhuda/thor/issues/398
|
|
5
|
+
class Thor
|
|
6
|
+
module Shell
|
|
7
|
+
class Basic
|
|
8
|
+
def print_wrapped(message, options = {})
|
|
9
|
+
message = "\n#{message}" unless message[0] == "\n"
|
|
10
|
+
stdout.puts message
|
|
11
|
+
end
|
|
12
|
+
end
|
|
13
|
+
end
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
module AwsRotate
|
|
17
|
+
class Command < Thor
|
|
18
|
+
class << self
|
|
19
|
+
def dispatch(m, args, options, config)
|
|
20
|
+
# Allow calling for help via:
|
|
21
|
+
# aws-rotate command help
|
|
22
|
+
# aws-rotate command -h
|
|
23
|
+
# aws-rotate command --help
|
|
24
|
+
# aws-rotate command -D
|
|
25
|
+
#
|
|
26
|
+
# as well thor's normal way:
|
|
27
|
+
#
|
|
28
|
+
# aws-rotate help command
|
|
29
|
+
help_flags = Thor::HELP_MAPPINGS + ["help"]
|
|
30
|
+
if args.length > 1 && !(args & help_flags).empty?
|
|
31
|
+
args -= help_flags
|
|
32
|
+
args.insert(-2, "help")
|
|
33
|
+
end
|
|
34
|
+
|
|
35
|
+
# aws-rotate version
|
|
36
|
+
# aws-rotate --version
|
|
37
|
+
# aws-rotate -v
|
|
38
|
+
version_flags = ["--version", "-v"]
|
|
39
|
+
if args.length == 1 && !(args & version_flags).empty?
|
|
40
|
+
args = ["version"]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
super
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Override command_help to include the description at the top of the
|
|
47
|
+
# long_description.
|
|
48
|
+
def command_help(shell, command_name)
|
|
49
|
+
meth = normalize_command_name(command_name)
|
|
50
|
+
command = all_commands[meth]
|
|
51
|
+
alter_command_description(command)
|
|
52
|
+
super
|
|
53
|
+
end
|
|
54
|
+
|
|
55
|
+
def alter_command_description(command)
|
|
56
|
+
return unless command
|
|
57
|
+
|
|
58
|
+
# Add description to beginning of long_description
|
|
59
|
+
long_desc = if command.long_description
|
|
60
|
+
"#{command.description}\n\n#{command.long_description}"
|
|
61
|
+
else
|
|
62
|
+
command.description
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# add reference url to end of the long_description
|
|
66
|
+
unless website.empty?
|
|
67
|
+
full_command = [command.ancestor_name, command.name].compact.join('-')
|
|
68
|
+
url = "#{website}/reference/aws-rotate-#{full_command}"
|
|
69
|
+
long_desc += "\n\nHelp also available at: #{url}"
|
|
70
|
+
end
|
|
71
|
+
|
|
72
|
+
command.long_description = long_desc
|
|
73
|
+
end
|
|
74
|
+
private :alter_command_description
|
|
75
|
+
|
|
76
|
+
# meant to be overriden
|
|
77
|
+
def website
|
|
78
|
+
""
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
@@ -0,0 +1,159 @@
|
|
|
1
|
+
=begin
|
|
2
|
+
Code Explanation:
|
|
3
|
+
|
|
4
|
+
There are 3 types of things to auto-complete:
|
|
5
|
+
|
|
6
|
+
1. command: the command itself
|
|
7
|
+
2. parameters: command parameters.
|
|
8
|
+
3. options: command options
|
|
9
|
+
|
|
10
|
+
Here's an example:
|
|
11
|
+
|
|
12
|
+
mycli hello name --from me
|
|
13
|
+
|
|
14
|
+
* command: hello
|
|
15
|
+
* parameters: name
|
|
16
|
+
* option: --from
|
|
17
|
+
|
|
18
|
+
When command parameters are done processing, the remaining completion words will be options. We can tell that the command params are completed based on the method arity.
|
|
19
|
+
|
|
20
|
+
## Arity
|
|
21
|
+
|
|
22
|
+
For example, say you had a method for a CLI command with the following form:
|
|
23
|
+
|
|
24
|
+
ufo scale service count --cluster development
|
|
25
|
+
|
|
26
|
+
It's equivalent ruby method:
|
|
27
|
+
|
|
28
|
+
scale(service, count) = has an arity of 2
|
|
29
|
+
|
|
30
|
+
So typing:
|
|
31
|
+
|
|
32
|
+
ufo scale service count [TAB] # there are 3 parameters including the "scale" command according to Thor's CLI processing.
|
|
33
|
+
|
|
34
|
+
So the completion should only show options, something like this:
|
|
35
|
+
|
|
36
|
+
--noop --verbose --cluster
|
|
37
|
+
|
|
38
|
+
## Splat Arguments
|
|
39
|
+
|
|
40
|
+
When the ruby method has a splat argument, it's arity is negative. Here are some example methods and their arities.
|
|
41
|
+
|
|
42
|
+
ship(service) = 1
|
|
43
|
+
scale(service, count) = 2
|
|
44
|
+
ships(*services) = -1
|
|
45
|
+
foo(example, *rest) = -2
|
|
46
|
+
|
|
47
|
+
Fortunately, negative and positive arity values are processed the same way. So we take simply take the absolute value of the arity and process it the same.
|
|
48
|
+
|
|
49
|
+
Here are some test cases, hit TAB after typing the command:
|
|
50
|
+
|
|
51
|
+
aws-rotate completion
|
|
52
|
+
aws-rotate completion hello
|
|
53
|
+
aws-rotate completion hello name
|
|
54
|
+
aws-rotate completion hello name --
|
|
55
|
+
aws-rotate completion hello name --noop
|
|
56
|
+
|
|
57
|
+
aws-rotate completion
|
|
58
|
+
aws-rotate completion sub:goodbye
|
|
59
|
+
aws-rotate completion sub:goodbye name
|
|
60
|
+
|
|
61
|
+
## Subcommands and Thor::Group Registered Commands
|
|
62
|
+
|
|
63
|
+
Sometimes the commands are not simple thor commands but are subcommands or Thor::Group commands. A good specific example is the ufo tool.
|
|
64
|
+
|
|
65
|
+
* regular command: ufo ship
|
|
66
|
+
* subcommand: ufo docker
|
|
67
|
+
* Thor::Group command: ufo init
|
|
68
|
+
|
|
69
|
+
Auto-completion accounts for each of these type of commands.
|
|
70
|
+
=end
|
|
71
|
+
module AwsRotate
|
|
72
|
+
class Completer
|
|
73
|
+
def initialize(command_class, *params)
|
|
74
|
+
@params = params
|
|
75
|
+
@current_command = @params[0]
|
|
76
|
+
@command_class = command_class # CLI initiall
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
def run
|
|
80
|
+
if subcommand?(@current_command)
|
|
81
|
+
subcommand_class = @command_class.subcommand_classes[@current_command]
|
|
82
|
+
@params.shift # destructive
|
|
83
|
+
Completer.new(subcommand_class, *@params).run # recursively use subcommand
|
|
84
|
+
return
|
|
85
|
+
end
|
|
86
|
+
|
|
87
|
+
# full command has been found!
|
|
88
|
+
unless found?(@current_command)
|
|
89
|
+
puts all_commands
|
|
90
|
+
return
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
# will only get to here if command aws found (above)
|
|
94
|
+
arity = @command_class.instance_method(@current_command).arity.abs
|
|
95
|
+
if @params.size > arity or thor_group_command?
|
|
96
|
+
puts options_completion
|
|
97
|
+
else
|
|
98
|
+
puts params_completion
|
|
99
|
+
end
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def subcommand?(command)
|
|
103
|
+
@command_class.subcommands.include?(command)
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
# hacky way to detect that command is a registered Thor::Group command
|
|
107
|
+
def thor_group_command?
|
|
108
|
+
command_params(raw=true) == [[:rest, :args]]
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def found?(command)
|
|
112
|
+
public_methods = @command_class.public_instance_methods(false)
|
|
113
|
+
command && public_methods.include?(command.to_sym)
|
|
114
|
+
end
|
|
115
|
+
|
|
116
|
+
# all top-level commands
|
|
117
|
+
def all_commands
|
|
118
|
+
commands = @command_class.all_commands.reject do |k,v|
|
|
119
|
+
v.is_a?(Thor::HiddenCommand)
|
|
120
|
+
end
|
|
121
|
+
commands.keys
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def command_params(raw=false)
|
|
125
|
+
params = @command_class.instance_method(@current_command).parameters
|
|
126
|
+
# Example:
|
|
127
|
+
# >> Sub.instance_method(:goodbye).parameters
|
|
128
|
+
# => [[:req, :name]]
|
|
129
|
+
# >>
|
|
130
|
+
raw ? params : params.map!(&:last)
|
|
131
|
+
end
|
|
132
|
+
|
|
133
|
+
def params_completion
|
|
134
|
+
offset = @params.size - 1
|
|
135
|
+
offset_params = command_params[offset..-1]
|
|
136
|
+
command_params[offset..-1].first
|
|
137
|
+
end
|
|
138
|
+
|
|
139
|
+
def options_completion
|
|
140
|
+
used = ARGV.select { |a| a.include?('--') } # so we can remove used options
|
|
141
|
+
|
|
142
|
+
method_options = @command_class.all_commands[@current_command].options.keys
|
|
143
|
+
class_options = @command_class.class_options.keys
|
|
144
|
+
|
|
145
|
+
all_options = method_options + class_options + ['help']
|
|
146
|
+
|
|
147
|
+
all_options.map! { |o| "--#{o.to_s.gsub('_','-')}" }
|
|
148
|
+
filtered_options = all_options - used
|
|
149
|
+
filtered_options.uniq
|
|
150
|
+
end
|
|
151
|
+
|
|
152
|
+
# Useful for debugging. Using puts messes up completion.
|
|
153
|
+
def log(msg)
|
|
154
|
+
File.open("/tmp/complete.log", "a") do |file|
|
|
155
|
+
file.puts(msg)
|
|
156
|
+
end
|
|
157
|
+
end
|
|
158
|
+
end
|
|
159
|
+
end
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
_aws-rotate() {
|
|
2
|
+
COMPREPLY=()
|
|
3
|
+
local word="${COMP_WORDS[COMP_CWORD]}"
|
|
4
|
+
local words=("${COMP_WORDS[@]}")
|
|
5
|
+
unset words[0]
|
|
6
|
+
local completion=$(aws-rotate completion ${words[@]})
|
|
7
|
+
COMPREPLY=( $(compgen -W "$completion" -- "$word") )
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
complete -F _aws-rotate aws-rotate
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
Example:
|
|
2
|
+
|
|
3
|
+
aws-rotate completion
|
|
4
|
+
|
|
5
|
+
Prints words for TAB auto-completion.
|
|
6
|
+
|
|
7
|
+
Examples:
|
|
8
|
+
|
|
9
|
+
aws-rotate completion
|
|
10
|
+
aws-rotate completion hello
|
|
11
|
+
aws-rotate completion hello name
|
|
12
|
+
|
|
13
|
+
To enable, TAB auto-completion add the following to your profile:
|
|
14
|
+
|
|
15
|
+
eval $(aws-rotate completion_script)
|
|
16
|
+
|
|
17
|
+
Auto-completion example usage:
|
|
18
|
+
|
|
19
|
+
aws-rotate [TAB]
|
|
20
|
+
aws-rotate hello [TAB]
|
|
21
|
+
aws-rotate hello name [TAB]
|
|
22
|
+
aws-rotate hello name --[TAB]
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
## Examples
|
|
2
|
+
|
|
3
|
+
aws-rotate keys
|
|
4
|
+
AWS_PROFILE=my-profile aws-rotate keys
|
|
5
|
+
|
|
6
|
+
## Select Filter Option
|
|
7
|
+
|
|
8
|
+
aws-rotate keys --select dev-
|
|
9
|
+
aws-rotate keys -s dev- # shorthand
|
|
10
|
+
aws-rotate keys -s ^dev- ^test- # multiple patterns
|
|
11
|
+
|
|
12
|
+
## Reject Filter Option
|
|
13
|
+
|
|
14
|
+
aws-rotate keys --reject prod-
|
|
15
|
+
aws-rotate keys -r prod- # shorthand
|
|
16
|
+
aws-rotate keys -r ^prod- ^production- # multiple patterns
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
module AwsRotate
|
|
2
|
+
class Key < Base
|
|
3
|
+
class MaxKeysError < StandardError; end
|
|
4
|
+
class GetIamUserError < StandardError; end
|
|
5
|
+
|
|
6
|
+
def run
|
|
7
|
+
# Note: It is nice to always call get_iam_user first as it'll check access. We rescue exceptions
|
|
8
|
+
# and report errors early on. The noop check happens after this initial check.
|
|
9
|
+
# Also with this we can filter for only the keys thats that have associated users and will be updated.
|
|
10
|
+
# Only the profiles with IAM users will be shown as "Updating..."
|
|
11
|
+
@user = get_iam_user # will only rotate keys that belong to an actual IAM user
|
|
12
|
+
return unless @user
|
|
13
|
+
|
|
14
|
+
check_max_keys_limit
|
|
15
|
+
message = "Updating access key for AWS_PROFILE=#{@profile}"
|
|
16
|
+
message = "NOOP: #{message}" if @options[:noop]
|
|
17
|
+
puts message.color(:green)
|
|
18
|
+
return false if @options[:noop]
|
|
19
|
+
|
|
20
|
+
key = cache_access_key || create_access_key
|
|
21
|
+
update_aws_credentials_file(key.access_key_id, key.secret_access_key)
|
|
22
|
+
delete_old_access_key
|
|
23
|
+
patience_message
|
|
24
|
+
aws_environment_variables_warning
|
|
25
|
+
true
|
|
26
|
+
end
|
|
27
|
+
|
|
28
|
+
# Returns IAM username.
|
|
29
|
+
# Returns nil unless this profile is actually associated with an user.
|
|
30
|
+
# Skips assume role profiles.
|
|
31
|
+
def get_iam_user
|
|
32
|
+
resp = sts.get_caller_identity
|
|
33
|
+
arn = resp.arn
|
|
34
|
+
# Example arns:
|
|
35
|
+
#
|
|
36
|
+
# arn:aws:iam::112233445566:user/tung - iam user
|
|
37
|
+
# arn arn:aws:sts::112233445566:assumed-role/Admin/default_session - assume role
|
|
38
|
+
#
|
|
39
|
+
if arn.include?(':user/')
|
|
40
|
+
arn.split('/').last
|
|
41
|
+
end
|
|
42
|
+
rescue Aws::Errors::MissingRegionError => e
|
|
43
|
+
puts "The AWS_PROFILE=#{@profile} may not exist. Please double check it.".color(:red)
|
|
44
|
+
puts "#{e.class} #{e.message}"
|
|
45
|
+
raise GetIamUserError
|
|
46
|
+
rescue Aws::STS::Errors::InvalidClientTokenId => e
|
|
47
|
+
puts "The AWS_PROFILE=#{@profile} profile does not have access to IAM. Please double check it.".color(:red)
|
|
48
|
+
puts "#{e.class} #{e.message}"
|
|
49
|
+
raise GetIamUserError
|
|
50
|
+
rescue Aws::STS::Errors::SignatureDoesNotMatch => e
|
|
51
|
+
puts "The AWS_PROFILE=#{@profile} profile seems to have invalid secret keys. Please double check it.".color(:red)
|
|
52
|
+
puts "#{e.class} #{e.message}"
|
|
53
|
+
raise GetIamUserError
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# Check if there are 2 keys, cannot rotate if there are 2 keys already.
|
|
57
|
+
# Raise error if there are 2 keys.
|
|
58
|
+
MAX_KEYS = 2
|
|
59
|
+
def check_max_keys_limit!
|
|
60
|
+
resp = iam.list_access_keys(user_name: @user)
|
|
61
|
+
return if resp.access_key_metadata.size < MAX_KEYS
|
|
62
|
+
raise MaxKeysError
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Check if there are 2 keys, cannot rotate if there are 2 keys already.
|
|
66
|
+
# Display info message for user to reduce it to 1 key.
|
|
67
|
+
def check_max_keys_limit
|
|
68
|
+
check_max_keys_limit!
|
|
69
|
+
rescue MaxKeysError
|
|
70
|
+
puts <<~EOL.color(:red)
|
|
71
|
+
This user #{@user} in the AWS_PROFILE=#{@profile} has 2 access keys. This is the max number of keys allowed.
|
|
72
|
+
Please remove at least one of the keys so aws-rotate can rotate the key.
|
|
73
|
+
EOL
|
|
74
|
+
exit 1
|
|
75
|
+
end
|
|
76
|
+
|
|
77
|
+
@@cache = {}
|
|
78
|
+
def cache_access_key
|
|
79
|
+
old_key_id = aws_configure_get(:aws_access_key_id)
|
|
80
|
+
return unless old_key_id
|
|
81
|
+
@@cache[old_key_id]
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
# Returns:
|
|
85
|
+
#
|
|
86
|
+
# #<struct Aws::IAM::Types::AccessKey
|
|
87
|
+
# user_name="tung",
|
|
88
|
+
# access_key_id="AKIAXZ6ODJLQUU6O3FD2",
|
|
89
|
+
# status="Active",
|
|
90
|
+
# secret_access_key="8eEnLLdR7gQE9fkFiDVuemi3qPf3mBMXxEXAMPLE",
|
|
91
|
+
# create_date=2019-08-13 21:14:35 UTC>>
|
|
92
|
+
#
|
|
93
|
+
def create_access_key
|
|
94
|
+
resp = iam.create_access_key
|
|
95
|
+
key = resp.access_key
|
|
96
|
+
|
|
97
|
+
# store in cache to help with multiple profiles using the same aws access key
|
|
98
|
+
old_key_id = aws_configure_get(:aws_access_key_id)
|
|
99
|
+
@@cache[old_key_id] = CacheKey.new(old_key_id, key.access_key_id, key.secret_access_key)
|
|
100
|
+
|
|
101
|
+
puts "Created new access key: #{key.access_key_id}"
|
|
102
|
+
key
|
|
103
|
+
end
|
|
104
|
+
|
|
105
|
+
def update_aws_credentials_file(aws_access_key_id, aws_secret_access_key)
|
|
106
|
+
aws_configure_set(aws_access_key_id: aws_access_key_id)
|
|
107
|
+
aws_configure_set(aws_secret_access_key: aws_secret_access_key)
|
|
108
|
+
puts "Updated profile #{@profile} in #{@credentials_path} with new key: #{aws_access_key_id}"
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def delete_old_access_key
|
|
112
|
+
resp = iam.list_access_keys
|
|
113
|
+
access_keys = resp.access_key_metadata
|
|
114
|
+
# Important: only delete if there are keys 2. The reason this is possible is because multiple profiles can use
|
|
115
|
+
# the same aws_access_key_id. In this case, an additional key is not created but we use the key from the @@cache
|
|
116
|
+
return if access_keys.size <= 1
|
|
117
|
+
|
|
118
|
+
old_key = access_keys.sort_by(&:create_date).first
|
|
119
|
+
iam.delete_access_key(access_key_id: old_key.access_key_id)
|
|
120
|
+
puts "Old access key deleted: #{old_key.access_key_id}"
|
|
121
|
+
end
|
|
122
|
+
|
|
123
|
+
def patience_message
|
|
124
|
+
puts "Please note, it sometimes take a few seconds or even minutes before the new IAM access key is usable."
|
|
125
|
+
end
|
|
126
|
+
|
|
127
|
+
def aws_environment_variables_warning
|
|
128
|
+
return unless ENV['AWS_ACCESS_KEY_ID'] || ENV['AWS_SECRET_ACCESS_KEY']
|
|
129
|
+
|
|
130
|
+
puts <<~EOL
|
|
131
|
+
WARN: The AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables are also set in your shell.
|
|
132
|
+
You must update those yourself. This tool only updates thethe keys in ~/.aws.
|
|
133
|
+
EOL
|
|
134
|
+
end
|
|
135
|
+
|
|
136
|
+
private
|
|
137
|
+
|
|
138
|
+
# Use the aws cli to spare coding work from parsing it.
|
|
139
|
+
def aws_configure_set(options={})
|
|
140
|
+
k, v = options.keys.first, options.values.first
|
|
141
|
+
sh "aws configure set #{k} #{v} --profile #{@profile}"
|
|
142
|
+
end
|
|
143
|
+
|
|
144
|
+
def aws_configure_get(k)
|
|
145
|
+
out = `aws configure get #{k} --profile #{@profile}` # use backtick to grab output
|
|
146
|
+
out.strip!
|
|
147
|
+
out == '' ? nil : out
|
|
148
|
+
end
|
|
149
|
+
end
|
|
150
|
+
end
|