@@ -17,45 +17,28 @@ def initialize(options = {})
17
17
18
18
@profile = options [ :profile ] || ENV [ 'AWS_PROFILE' ] || ENV [ 'AWS_DEFAULT_PROFILE' ] || 'default'
19
19
@session_profile = @profile + '_session'
20
- @expiration = begin
21
- Aws . shared_config . expiration ( profile : @session_profile )
22
- rescue StandardError
23
- nil
24
- end
25
- @credentials = begin
26
- Aws . shared_config . credentials ( profile : @session_profile )
27
- rescue StandardError
28
- nil
29
- end
20
+ @expiration = Aws . shared_config . expiration ( profile : @session_profile ) rescue nil
21
+ @credentials = Aws . shared_config . credentials ( profile : @session_profile ) rescue nil
30
22
refresh_if_near_expiration
31
23
end
32
24
33
25
def refresh_if_near_expiration
34
- return unless near_expiration? ( SYNC_EXPIRATION_LENGTH )
35
-
36
- @mutex . synchronize do
37
- if near_expiration? ( SYNC_EXPIRATION_LENGTH )
38
- refresh
39
- write_credentials
26
+ if near_expiration? ( SYNC_EXPIRATION_LENGTH )
27
+ @mutex . synchronize do
28
+ if near_expiration? ( SYNC_EXPIRATION_LENGTH )
29
+ refresh
30
+ write_credentials
31
+ end
40
32
end
41
33
end
42
34
end
43
35
44
36
# Write credentials and expiration to AWS credentials file.
45
37
def write_credentials
46
- # Ensure the AWS CLI is available before attempting to write credentials .
38
+ # AWS CLI is needed because writing AWS credentials is not supported by the AWS Ruby SDK .
47
39
return unless system ( 'which aws >/dev/null 2>&1' )
48
-
49
- # Manually map the credentials to the keys used by AWS CLI
50
- credentials_map = {
51
- 'aws_access_key_id' => @credentials . access_key_id ,
52
- 'aws_secret_access_key' => @credentials . secret_access_key ,
53
- 'aws_session_token' => @credentials . session_token ,
54
- 'expiration' => @expiration
55
- }
56
-
57
- # Use the AWS CLI to set the credentials in the session profile
58
- credentials_map . each do |key , value |
40
+ Aws ::SharedCredentials ::KEY_MAP . transform_values ( &@credentials . method ( :send ) ) .
41
+ merge ( expiration : @expiration ) . each do |key , value |
59
42
system ( "aws configure set #{ key } #{ value } --profile #{ @session_profile } " )
60
43
end
61
44
end
0 commit comments