-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Connect to Redis With the ElastiCache IAM Credential Provider #361
(#365) Co-authored-by: Aliaksandr Stsiapanay <[email protected]>
- Loading branch information
1 parent
3afa1ae
commit cc55a5f
Showing
7 changed files
with
262 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/main/java/com/epam/aidial/core/cache/AwsCredentialsResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.epam.aidial.core.cache; | ||
|
||
import com.amazonaws.auth.AWSCredentialsProvider; | ||
import org.redisson.config.Credentials; | ||
import org.redisson.config.CredentialsResolver; | ||
|
||
import java.net.InetSocketAddress; | ||
import java.net.URISyntaxException; | ||
import java.util.concurrent.CompletableFuture; | ||
import java.util.concurrent.CompletionStage; | ||
|
||
/** | ||
* Source of implementation <a href="https://redisson.org/articles/how-to-connect-to-redis-using-elasticache-iam-credential-provider.html"> | ||
* How to Connect to Redis With the ElastiCache IAM Credential Provider</a>. | ||
*/ | ||
public class AwsCredentialsResolver implements CredentialsResolver { | ||
|
||
private static final long TOKEN_EXPIRY_MS = 900_000; | ||
|
||
private final String userName; | ||
private final IamAuthTokenRequest iamAuthTokenRequest; | ||
private final AWSCredentialsProvider awsCredentialsProvider; | ||
|
||
private volatile CompletionStage<Credentials> future; | ||
private volatile long lastTime = System.currentTimeMillis(); | ||
|
||
public AwsCredentialsResolver(String userName, IamAuthTokenRequest iamAuthTokenRequest, | ||
AWSCredentialsProvider awsCredentialsProvider) { | ||
this.userName = userName; | ||
this.iamAuthTokenRequest = iamAuthTokenRequest; | ||
this.awsCredentialsProvider = awsCredentialsProvider; | ||
} | ||
|
||
@Override | ||
public CompletionStage<Credentials> resolve(InetSocketAddress address) { | ||
if (System.currentTimeMillis() - lastTime > TOKEN_EXPIRY_MS || future == null) { | ||
try { | ||
String token = iamAuthTokenRequest.toSignedRequestUri(awsCredentialsProvider.getCredentials()); | ||
future = CompletableFuture.completedFuture(new Credentials(userName, token)); | ||
lastTime = System.currentTimeMillis(); | ||
} catch (URISyntaxException e) { | ||
throw new IllegalArgumentException(e); | ||
} | ||
} | ||
return future; | ||
} | ||
} |
Oops, something went wrong.