-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: extend examples to fetch momento auth token from secrets manager
- Loading branch information
1 parent
0d89498
commit c9cb723
Showing
7 changed files
with
43 additions
and
10 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
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
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
41 changes: 36 additions & 5 deletions
41
examples/lib/src/main/java/momento/client/example/util/AuthUtil.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 |
---|---|---|
@@ -1,22 +1,53 @@ | ||
package momento.client.example.util; | ||
|
||
import momento.sdk.auth.CredentialProvider; | ||
import momento.sdk.auth.EnvVarCredentialProvider; | ||
import momento.sdk.exceptions.SdkException; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; | ||
import software.amazon.awssdk.regions.Region; | ||
import software.amazon.awssdk.services.secretsmanager.SecretsManagerClient; | ||
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueRequest; | ||
import software.amazon.awssdk.services.secretsmanager.model.GetSecretValueResponse; | ||
|
||
/** Small utility class to vend credentials */ | ||
public class AuthUtil { | ||
private static final String AUTH_TOKEN_ENV_VAR = "MOMENTO_AUTH_TOKEN"; | ||
private static final String AUTH_TOKEN_VAR = "MOMENTO_AUTH_TOKEN"; | ||
private static final Logger logger = LoggerFactory.getLogger(AuthUtil.class); | ||
|
||
public static CredentialProvider getCredentials() { | ||
public static CredentialProvider getCredentialsFromEnvironmentVariable() { | ||
try { | ||
return new EnvVarCredentialProvider(AUTH_TOKEN_ENV_VAR); | ||
return CredentialProvider.fromEnvVar(AUTH_TOKEN_VAR); | ||
} catch (SdkException e) { | ||
logger.error("Unable to load credential from environment variable " + AUTH_TOKEN_ENV_VAR, e); | ||
logger.error("Unable to load credential from environment variable " + AUTH_TOKEN_VAR, e); | ||
throw e; | ||
} | ||
} | ||
|
||
public static CredentialProvider getCredentialsFromSecretsManagerAuthToken() { | ||
final Region region = Region.of("us-east-1"); | ||
|
||
// Create a Secrets Manager client | ||
final SecretsManagerClient client = | ||
SecretsManagerClient.builder() | ||
.region(region) | ||
.credentialsProvider(DefaultCredentialsProvider.create()) | ||
.build(); | ||
|
||
final GetSecretValueRequest getSecretValueRequest = | ||
GetSecretValueRequest.builder().secretId(AUTH_TOKEN_VAR).build(); | ||
|
||
final GetSecretValueResponse getSecretValueResponse; | ||
|
||
try { | ||
getSecretValueResponse = client.getSecretValue(getSecretValueRequest); | ||
} catch (Exception e) { | ||
// For a list of exceptions thrown, see | ||
// https://docs.aws.amazon.com/secretsmanager/latest/apireference/API_GetSecretValue.html | ||
throw e; | ||
} | ||
|
||
final String secret = getSecretValueResponse.secretString(); | ||
return CredentialProvider.fromString(secret); | ||
} | ||
} |