Skip to content

Commit

Permalink
chore: extend examples to fetch momento auth token from secrets manager
Browse files Browse the repository at this point in the history
  • Loading branch information
pratik151192 committed Jun 29, 2023
1 parent 0d89498 commit c9cb723
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 10 deletions.
2 changes: 2 additions & 0 deletions examples/lib/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ repositories {

dependencies {
implementation("software.momento.java:sdk:1.0.0")
// For examples to store secrets in AWS Secrets Manager
implementation("software.amazon.awssdk:secretsmanager:2.20.93")

// Logging framework to log and enable logging in the Momento client.
implementation("ch.qos.logback:logback-classic:1.4.7")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class BasicExample {
public static void main(String[] args) {
logStartBanner(logger);

final CredentialProvider credentialProvider = AuthUtil.getCredentials();
final CredentialProvider credentialProvider = AuthUtil.getCredentialsFromEnvironmentVariable();

try (final CacheClient client =
CacheClient.builder(credentialProvider, Configurations.Laptop.latest(), DEFAULT_ITEM_TTL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class DictionaryExample {
public static void main(String[] args) {
logStartBanner(logger);

final CredentialProvider credentialProvider = AuthUtil.getCredentials();
final CredentialProvider credentialProvider = AuthUtil.getCredentialsFromEnvironmentVariable();

try (final CacheClient client =
CacheClient.builder(credentialProvider, Configurations.Laptop.latest(), DEFAULT_ITEM_TTL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ListExample {
public static void main(String[] args) {
logStartBanner(logger);

final CredentialProvider credentialProvider = AuthUtil.getCredentials();
final CredentialProvider credentialProvider = AuthUtil.getCredentialsFromEnvironmentVariable();

try (final CacheClient client =
CacheClient.builder(credentialProvider, Configurations.Laptop.latest(), DEFAULT_ITEM_TTL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public class SetExample {
public static void main(String[] args) {
logStartBanner(logger);

final CredentialProvider credentialProvider = AuthUtil.getCredentials();
final CredentialProvider credentialProvider = AuthUtil.getCredentialsFromEnvironmentVariable();

try (final CacheClient client =
CacheClient.builder(credentialProvider, Configurations.Laptop.latest(), DEFAULT_ITEM_TTL)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class SortedSetExample {
public static void main(String[] args) {
logStartBanner(logger);

final CredentialProvider credentialProvider = AuthUtil.getCredentials();
final CredentialProvider credentialProvider = AuthUtil.getCredentialsFromEnvironmentVariable();

try (final CacheClient client =
CacheClient.builder(credentialProvider, Configurations.Laptop.latest(), DEFAULT_ITEM_TTL)
Expand Down
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);
}
}

0 comments on commit c9cb723

Please sign in to comment.