AWS Secrets Manager から Momento 認証トークンを取得する
Momentoの認証トークンは安全に保管するのがベストプラクティスです。AWS を使用している場合は、AWS Secrets Manager に認証トークンを安全に保存できます。これにより、正しい IAM 認証情報で実行されているコードのみが認証トークンを取得し、Momento Cache や Momento Topics にアクセスするために使用できるようになります。
備考
Momento の CacheClient
オブジェクトと TopicClient
オブジェクトを可能な限り再利用するのと同様に、AWS Secrets Manager からの Momento 認証トークンも再利用する必要があります。そうしないと、Momento API を呼び出すたびに、AWS Secrets Manager との往復に時間とコストがかかることになります。コストを抑え、AWS Secrets Manager によるスロットリングを避けるには、この AWS ブログ にあるように、クライアントサイドで認証情報をキャッシュするのがベストです。
AWS Secrets Managerのエントリ
Momentoの認証トークンをAWS Secrets Managerに挿入するときは、このスクリーンショットのようにJSONを含まないプレーンテキストにします。(トークンはセキュリティのためにぼかしてあります)
AWS Secrets Managerのコード例
- Java
final Region region = Region.of("us-east-1");
// Create a Secrets Manager client
final SecretsManagerClient client =
SecretsManagerClient.builder()
.region(region)
// make sure to configure aws credentials in order to use the default provider
// https://sdk.amazonaws.com/java/api/latest/software/amazon/awssdk/auth/credentials/DefaultCredentialsProvider.html
.credentialsProvider(DefaultCredentialsProvider.create())
.build();
final GetSecretValueRequest getSecretValueRequest =
GetSecretValueRequest.builder().secretId("AUTH_TOKEN_SECRET_NAME").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();
try {
// store variable here
CredentialProvider.fromString(secret);
} catch (SdkException e) {
throw new RuntimeException(
"An error occured while parsing the secrets manager vended" + " authentication token", e);
}
備考
Full example code and imports can be found here
FAQ
こうしなければなりませんか?
Momentoの認証トークンを環境変数やファイルに保存することはできますが、AWS Secrets Managerなどに保存するよりも安全ではないため、ベストプラクティスではありません。