You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2022/05/11 18:45:20 UTC
[GitHub] [iceberg] amogh-jahagirdar commented on a diff in pull request #4695: AWS: Add Cache for LakeFormation Credentials
amogh-jahagirdar commented on code in PR #4695:
URL: https://github.com/apache/iceberg/pull/4695#discussion_r870640744
##########
aws/src/main/java/org/apache/iceberg/aws/lakeformation/LakeFormationAwsClientFactory.java:
##########
@@ -132,27 +145,90 @@ private LakeFormationClient lakeFormation() {
.build();
}
+ static class LakeFormationTemporaryCredentials {
+
+ private final AwsCredentials credentials;
+ private final Instant sessionCredentialsExpiration;
+
+ LakeFormationTemporaryCredentials(AwsCredentials credentials, Instant expiration) {
+ this.credentials = credentials;
+ this.sessionCredentialsExpiration = expiration;
+ }
+
+ AwsCredentials credentials() {
+ return credentials;
+ }
+
+ Instant sessionCredentialsExpiration() {
+ return sessionCredentialsExpiration;
+ }
+ }
+
static class LakeFormationCredentialsProvider implements AwsCredentialsProvider {
private LakeFormationClient client;
private String tableArn;
+ private final long expiryLeadTimeForRefreshInSecs;
+
+ private LakeFormationTemporaryCredentials cachedCredentials;
+ private final Cache<String, LakeFormationTemporaryCredentials> cache;
- LakeFormationCredentialsProvider(LakeFormationClient lakeFormationClient, String tableArn) {
+ LakeFormationCredentialsProvider(LakeFormationClient lakeFormationClient,
+ String tableArn, long expiryLeadTimeForRefreshInSecs) {
this.client = lakeFormationClient;
this.tableArn = tableArn;
+ this.expiryLeadTimeForRefreshInSecs = expiryLeadTimeForRefreshInSecs;
+ this.cache = Caffeine.newBuilder()
+ .expireAfter(new Expiry<String, LakeFormationTemporaryCredentials>() {
+ @Override
+ public long expireAfterCreate(@NonNull String key,
+ @NonNull LakeFormationTemporaryCredentials value, long currentTime) {
+ return getExpiryDuration(value);
+ }
+
+ @Override
+ public long expireAfterUpdate(@NonNull String key, @NonNull LakeFormationTemporaryCredentials value,
+ long currentTime, @NonNegative long currentDuration) {
+ return getExpiryDuration(value);
+ }
+
+ @Override
+ public long expireAfterRead(@NonNull String key, @NonNull LakeFormationTemporaryCredentials value,
+ long currentTime, @NonNegative long currentDuration) {
+ return currentDuration;
+ }
+ })
+ .initialCapacity(1)
+ .build();
}
@Override
public AwsCredentials resolveCredentials() {
- GetTemporaryGlueTableCredentialsRequest getTemporaryGlueTableCredentialsRequest =
- GetTemporaryGlueTableCredentialsRequest.builder()
- .tableArn(tableArn)
- // Now only two permission types (COLUMN_PERMISSION and CELL_FILTER_PERMISSION) are supported
- // and Iceberg only supports COLUMN_PERMISSION at this time
- .supportedPermissionTypes(PermissionType.COLUMN_PERMISSION)
- .build();
- GetTemporaryGlueTableCredentialsResponse response =
- client.getTemporaryGlueTableCredentials(getTemporaryGlueTableCredentialsRequest);
- return AwsSessionCredentials.create(response.accessKeyId(), response.secretAccessKey(), response.sessionToken());
+ LakeFormationTemporaryCredentials credentials = cache.get(tableArn, key -> {
+ GetTemporaryGlueTableCredentialsRequest getTemporaryGlueTableCredentialsRequest =
+ GetTemporaryGlueTableCredentialsRequest.builder()
+ .tableArn(key)
+ // Now only two permission types (COLUMN_PERMISSION and CELL_FILTER_PERMISSION) are supported
+ // and Iceberg only supports COLUMN_PERMISSION at this time
+ .supportedPermissionTypes(PermissionType.COLUMN_PERMISSION)
+ .build();
+ GetTemporaryGlueTableCredentialsResponse response =
+ client.getTemporaryGlueTableCredentials(getTemporaryGlueTableCredentialsRequest);
+
+ AwsSessionCredentials awsSessionCredentials = AwsSessionCredentials
+ .create(response.accessKeyId(), response.secretAccessKey(), response.sessionToken());
+
+ cachedCredentials = new LakeFormationTemporaryCredentials(awsSessionCredentials, response.expiration());
+ return cachedCredentials;
+ });
+
+ return credentials.credentials();
+ }
+
+ private long getExpiryDuration(LakeFormationTemporaryCredentials value) {
Review Comment:
In Iceberg, please avoid using getter https://github.com/apache/iceberg/blob/master/CONTRIBUTING.md#style
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org