11require  'singleton' 
22
33module  Mysql2 
4+   # Generates and caches AWS IAM Authentication tokens to use in place of MySQL user passwords 
45  class  AwsTokenAuth 
56    include  Singleton 
67
78    # Tokens are valid for up to 15 minutes. 
89    # 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 
1011
1112    def  initialize 
1213      begin 
@@ -25,19 +26,18 @@ def initialize
2526      @generator  =  Aws ::RDS ::AuthTokenGenerator . new 
2627    end 
2728
28-     def  password ( user ,  host  port ,  opts ) 
29+     def  password ( user ,  host ,  port ,  opts ) 
2930      params  =  to_params ( user ,  host ,  port ,  opts ) 
3031      key  =  key_from_params ( params ) 
3132      passwd  =  nil 
3233      AwsTokenAuth . instance . mutex . synchronize  do 
3334        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 ] 
3736        rescue  KeyError 
37+           passwd  =  nil 
3838        end 
3939      end 
40-       if  passwd  return  passwd 
40+       return  passwd  unless  passwd . nil? 
4141
4242      AwsTokenAuth . instance . mutex . synchronize  do 
4343        @passwords [ key ]  =  { } 
@@ -59,8 +59,7 @@ def to_params(user, host, port, opts)
5959    end 
6060
6161    def  key_from_params ( params ) 
62-       return   "#{ params [ :user_name ] }  /#{ params [ :endpoint ] }  /#{ params [ :region ] }  " 
62+       "#{ params [ :user_name ] }  /#{ params [ :endpoint ] }  /#{ params [ :region ] }  " 
6363    end 
64- 
6564  end 
6665end 
0 commit comments