-
Notifications
You must be signed in to change notification settings - Fork 2
/
aws_mfa_token.rb
executable file
·51 lines (40 loc) · 1.68 KB
/
aws_mfa_token.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'json'
require 'iniparse'
unless defined?(IniParse)
puts 'Please install gem `iniparse`'
exit(1)
end
SESSION_DURATION = 129_600 # 36 hours
AWS_CRERENTIALS_FILE = "#{Dir.home}/.aws/credentials"
AWS_MFA_PROFILE = 'with-mfa'
aws_token = ARGV[0]
aws_profile = ARGV[1] || 'default'
unless File.exist?(AWS_CRERENTIALS_FILE)
puts 'Configure your AWS credentials first.'
exit
end
if aws_token.nil? || aws_token.empty?
puts 'Usage: `./aws-mfa-token <MFA-TOKEN> [<PROFILE>]'
puts ' - MFA-TOKEN needs to be enable at the AWS Account'
exit
end
def json_path(content, *path)
json = JSON.parse(content)
json.dig(*path)
end
mfa_device_code = json_path(`aws iam list-mfa-devices --profile #{aws_profile}`, 'MFADevices', 0, 'SerialNumber')
get_session = "aws sts get-session-token --profile #{aws_profile} --duration-seconds #{SESSION_DURATION} " \
"--serial-number #{mfa_device_code} --token-code #{aws_token}"
new_session = `#{get_session}`
aws_access_key_id = json_path(new_session, 'Credentials', 'AccessKeyId')
aws_secret_access_key = json_path(new_session, 'Credentials', 'SecretAccessKey')
aws_session_token = json_path(new_session, 'Credentials', 'SessionToken')
credentials = IniParse.parse(File.read(AWS_CRERENTIALS_FILE))
credentials.section(AWS_MFA_PROFILE)
credentials[AWS_MFA_PROFILE]['aws_access_key_id'] = aws_access_key_id
credentials[AWS_MFA_PROFILE]['aws_secret_access_key'] = aws_secret_access_key
credentials[AWS_MFA_PROFILE]['aws_session_token'] = aws_session_token
credentials.save(AWS_CRERENTIALS_FILE)
puts "Session profile 'with-mfa' will expire on #{json_path(new_session, 'Credentials', 'Expiration')}"