Skip to content

Commit 7687414

Browse files
fixes so simple tests work
1 parent e6534de commit 7687414

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

lib/mysql2.rb

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
require 'mysql2/client'
3939
require 'mysql2/field'
4040
require 'mysql2/statement'
41+
require 'mysql2/aws_iam_auth'
4142

4243
# = Mysql2
4344
#

lib/mysql2/aws_iam_auth.rb

+8-5
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
module Mysql2
44
# Generates and caches AWS IAM Authentication tokens to use in place of MySQL user passwords
5-
class AwsTokenAuth
5+
class AwsIamAuth
66
include Singleton
7+
attr_reader :mutex
8+
attr_accessor :passwords
79

810
# Tokens are valid for up to 15 minutes.
911
# We will assume ours expire in 14 minutes to be safe.
@@ -23,23 +25,24 @@ def initialize
2325
# :password is the token value
2426
# :expires_at is (just before) the token was generated plus 14 minutes
2527
@passwords = {}
26-
@generator = Aws::RDS::AuthTokenGenerator.new
28+
instance_credentials = Aws::InstanceProfileCredentials.new
29+
@generator = Aws::RDS::AuthTokenGenerator.new(:credentials => instance_credentials)
2730
end
2831

2932
def password(user, host, port, opts)
3033
params = to_params(user, host, port, opts)
3134
key = key_from_params(params)
3235
passwd = nil
33-
AwsTokenAuth.instance.mutex.synchronize do
36+
AwsIamAuth.instance.mutex.synchronize do
3437
begin
35-
passwd = @passwords[key][:password] if @passwords[key][:password] && Time.now.utc < @passwords[key][:expires_at]
38+
passwd = @passwords[key][:password] if @passwords.dig(key, :password) && Time.now.utc < @passwords.dig(key, :expires_at)
3639
rescue KeyError
3740
passwd = nil
3841
end
3942
end
4043
return passwd unless passwd.nil?
4144

42-
AwsTokenAuth.instance.mutex.synchronize do
45+
AwsIamAuth.instance.mutex.synchronize do
4346
@passwords[key] = {}
4447
@passwords[key][:expires_at] = Time.now.utc + TOKEN_EXPIRES_IN
4548
@passwords[key][:password] = password_from_iam(params)

lib/mysql2/client.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def initialize(opts = {})
9595
conn_attrs = parse_connect_attrs(opts[:connect_attrs])
9696

9797
if opts[:use_iam_authentication]
98-
aws = Mysql2::AwsTokenAuth.new
98+
aws = Mysql2::AwsIamAuth.instance
9999
pass = aws.password(user, host, port, opts)
100100
end
101101

0 commit comments

Comments
 (0)