1
1
require 'singleton'
2
2
3
3
module Mysql2
4
+ # Generates and caches AWS IAM Authentication tokens to use in place of MySQL user passwords
4
5
class AwsTokenAuth
5
6
include Singleton
6
7
7
8
# Tokens are valid for up to 15 minutes.
8
9
# We will assume ours expire in 14 minutes to be safe.
9
- TOKEN_EXPIRES_IN = ( 60 * 14 ) # 14 minutes
10
+ TOKEN_EXPIRES_IN = ( 60 * 14 ) # 14 minutes
10
11
11
12
def initialize
12
13
begin
@@ -25,19 +26,18 @@ def initialize
25
26
@generator = Aws ::RDS ::AuthTokenGenerator . new
26
27
end
27
28
28
- def password ( user , host port , opts )
29
+ def password ( user , host , port , opts )
29
30
params = to_params ( user , host , port , opts )
30
31
key = key_from_params ( params )
31
32
passwd = nil
32
33
AwsTokenAuth . instance . mutex . synchronize do
33
34
begin
34
- if @passwords [ key ] [ :password ] && Time . now . utc < @passwords [ key ] [ :expires_at ]
35
- passwd = @passwords [ key ] [ :password ]
36
- end
35
+ passwd = @passwords [ key ] [ :password ] if @passwords [ key ] [ :password ] && Time . now . utc < @passwords [ key ] [ :expires_at ]
37
36
rescue KeyError
37
+ passwd = nil
38
38
end
39
39
end
40
- if passwd return passwd
40
+ return passwd unless passwd . nil?
41
41
42
42
AwsTokenAuth . instance . mutex . synchronize do
43
43
@passwords [ key ] = { }
@@ -59,8 +59,7 @@ def to_params(user, host, port, opts)
59
59
end
60
60
61
61
def key_from_params ( params )
62
- return "#{ params [ :user_name ] } /#{ params [ :endpoint ] } /#{ params [ :region ] } "
62
+ "#{ params [ :user_name ] } /#{ params [ :endpoint ] } /#{ params [ :region ] } "
63
63
end
64
-
65
64
end
66
65
end
0 commit comments