Elasticache with IAM Authentication authenticate Faild #3182
-
BackgroundWe have enabled IAM authentication for our Replication Group, but now we need to connect to it using a Ruby script. According to the documentation, we need to generate a signature. However, when I tried implementing the same method in Ruby, the authentication failed and returned an error. Details about generating the tokenthe following code to generate a token: def generate_token(elasticache_name, connect_user, credentials, region)
signer = Aws::Sigv4::Signer.new(
service: 'elasticache',
region: "#{region}",
credentials_provider: credentials
)
query_params = {
"Action" => "connect",
"User" => connect_user
}
uri = URI("http://#{elasticache_name}/")
uri.query = URI.encode_www_form(query_params)
signer.presign_url(
http_method: 'GET',
url: uri.to_s,
expires_in: 3600,
headers: {
"host" => elasticache_name,
}
).to_s.sub('http://', '')
end The result has the following approximate structure:
Connect to replication group use redis-rb clientredis = Redis.new(
host: redis_host,
port: redis_port,
ssl: true,
password: token,
username: elasticache_user
)
redis.ping What happenedHowever, after running the code, I encountered an error with the following message:
Some additional testsWhen I generate a token using Java code and use it in the redis-rb client to connect, the connection is successful. Therefore, it can be inferred that the issue lies with the token generated by Ruby. There might be something wrong with how I'm using it, but I'm not sure how to fix it. Could you please review my approach and point out any issues? Best wishes~~ |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
Have you tried using a shorter expires in, like 900 instead of the 3600? Whats the structure of the token generated by java and is there anything different about it? |
Beta Was this translation helpful? Give feedback.
-
Glad that suggestion worked! Expiration time on presigned URLs is a bit tricky and usually needs to consider both the expiration time of the credentials and the security posture of the service. Our utilities to generate presigned URLs are fairly permissive and I think one week may be the absolute maximum time accepted anywhere, but individual services may enforce lower limits. I'll look at updating our documentation to make that more clear. |
Beta Was this translation helpful? Give feedback.
Have you tried using a shorter expires in, like 900 instead of the 3600?
Additionally, you may want to try not adding the
host
header.Whats the structure of the token generated by java and is there anything different about it?