Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: add java example code for secrets manager #318

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/develop/integrations/aws-secrets-manager.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,65 @@ async function run() {
run();
```

</TabItem>
<TabItem value="java" label="Java" default>

```java
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey this is really cool! thanks for doing it. I would like to show you something about our docs tomorrow morning, I think there might be a better place for us to put this code. I'll ping you and we can find a time to zoom

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool! have also added a new helper on the sdk package examples with similar logic and tested that it worked succesfully. Currently waiting on
the previous PR on the sdk package to be approved and merged before I cherry-pick the secrets manager commit on my local.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cprice404 so we are holding off on this PR until you get this code into the SDK repo and be able to link back to this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that is what I want to discuss with Pratik, yes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes am closing this and will generate a new PR with pages just linking to the examples under SDK folders.

import java.time.Duration;
import momento.sdk.CacheClient;
import momento.sdk.auth.CredentialProvider;
import momento.sdk.config.Configurations;
import momento.sdk.exceptions.SdkException;
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;

public class MomentoCacheClient {
private static final String AUTH_TOKEN_VAR = "MOMENTO_AUTH_TOKEN";

private static CredentialProvider getCredentialsFromSecretsManagerAuthToken() {
final Region region = Region.of("us-east-1");

// Create a Secrets Manager client
final SecretsManagerClient client =
SecretsManagerClient.builder()
.region(region)
// make sure your AWS credentials are configured in the default profile to use this
.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);
}

public static void main(String[] args) {
try (final CacheClient cacheClient =
CacheClient.builder(
getCredentialsFromSecretsManagerAuthToken(),
Configurations.Laptop.v1(),
Duration.ofSeconds(60))
.build()) {
// ...refer Java SDK cheat sheet for interacting with Momento!
// https://docs.momentohq.com/develop/sdks/java/cheat-sheet
}
}
}
```
</TabItem>
<TabItem value="typescript" label="TypeScript" default>

Expand Down