Repository navigation
[Server] Vend credentials for S3-compatible storage #2172
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| import io.unitycatalog.server.observability.StorageCredentialCacheMetrics; | ||
| import io.unitycatalog.server.observability.UnityCatalogMetrics; | ||
| import io.unitycatalog.server.persist.dao.CredentialDAO; | ||
| import io.unitycatalog.server.persist.dao.CredentialDAO.CredentialType; | ||
| import io.unitycatalog.server.service.credential.aws.AwsCredentialVendor; | ||
| import io.unitycatalog.server.service.credential.azure.AzureCredentialVendor; | ||
| import io.unitycatalog.server.service.credential.gcp.GcpCredentialVendor; | ||
|
|
@@ -21,6 +22,7 @@ | |
| import java.util.Objects; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.TimeUnit; | ||
| import lombok.SneakyThrows; | ||
|
|
||
|
|
@@ -31,10 +33,12 @@ | |
| * binding before calling it, so only the cloud vend is skipped; the binding is re-read on every | ||
| * request. | ||
| * | ||
| * <p>An entry is keyed by {@code (locations, privileges, roleArn, externalId)}. It is served until | ||
| * {@code min(vendTime + maxAge, credentialExpiry - expiryMargin)}, where {@code vendTime} is read | ||
| * before the cloud call; a static credential with no expiry is bounded by the max age alone. | ||
| * Callers always receive a copy, so they cannot change a cached credential. | ||
| * <p>An entry is keyed by {@code (locations, privileges, roleArn, externalId)}, plus, for | ||
| * S3-compatible storage, the storage credential's version, its S3 endpoint and region, and whether | ||
| * the server or a client uses it. It is served until {@code min(vendTime + maxAge, credentialExpiry | ||
| * - expiryMargin)}, where {@code vendTime} is read before the cloud call; a static credential with | ||
| * no expiry is bounded by the max age alone. Callers always receive a copy, so they cannot change a | ||
| * cached credential. | ||
| * | ||
| * <p>Each request is recorded as a hit or a miss in {@link StorageCredentialCacheMetrics}. | ||
| */ | ||
|
|
@@ -130,32 +134,57 @@ private long now() { | |
|
|
||
| /** | ||
| * The resolved credential binding. Changing the role or external ID of a location's credential | ||
| * changes the key, so credentials cached under the previous binding are never reused. | ||
| * changes the key, so credentials cached under the previous binding are never reused. An {@code | ||
| * s3_compatible} credential is identified by the fingerprint of its stored payload instead, which | ||
| * every change of its key pair, endpoint or role changes. Server and client vends of such a | ||
| * credential can differ: without {@code assume_role}, the server gets the key pair without an | ||
| * expiration, and clients get only the endpoint ({@code endpoint-only}) or the key pair with a | ||
| * refetch time ({@code static-key}). So for every {@code s3_compatible} credential they never | ||
| * share an entry; {@code server.s3-compatible-storage.static-key-vending} is read once at | ||
| * startup, so entries never span two modes. | ||
| */ | ||
| private record Key( | ||
| List<NormalizedURL> locations, | ||
| Set<CredentialContext.Privilege> privileges, | ||
| String roleArn, | ||
| String externalId) { | ||
| String externalId, | ||
| Optional<CredentialVersion> s3CompatibleVersion, | ||
| Optional<String> s3Endpoint, | ||
| Optional<String> s3Region, | ||
| boolean isServerAccess) { | ||
| Key { | ||
| // AWS and GCP scope a credential to every location in the context, so the key has them all. | ||
| locations = List.copyOf(locations); | ||
| privileges = Set.copyOf(privileges); | ||
| } | ||
|
|
||
| static Key of(CredentialContext context) { | ||
| // Per-bucket config vends have no credential DAO, so both role fields are null. AWS IAM role | ||
| // is the only DB-backed credential type today; a new type must add its binding to the key. | ||
| // Per-bucket config vends have no credential DAO, so both role fields are null. A new | ||
| // DB-backed credential type must add its binding to the key. | ||
| Optional<AwsIamRoleResponse> awsIamRole = | ||
| context.getCredentialDAO().map(CredentialDAO::getAwsIamRoleResponse); | ||
| context | ||
| .getCredentialDAO() | ||
| .filter(dao -> dao.getCredentialType() == CredentialType.AWS_IAM_ROLE) | ||
| .map(CredentialDAO::getAwsIamRoleResponse); | ||
| Optional<CredentialVersion> s3CompatibleVersion = | ||
| context | ||
| .getCredentialDAO() | ||
| .filter(dao -> dao.getCredentialType() == CredentialType.S3_COMPATIBLE) | ||
| .map(dao -> new CredentialVersion(dao.getId(), dao.getCredentialFingerprint())); | ||
| return new Key( | ||
| context.getLocations(), | ||
| context.getPrivileges(), | ||
| awsIamRole.map(AwsIamRoleResponse::getRoleArn).orElse(null), | ||
| awsIamRole.map(AwsIamRoleResponse::getExternalId).orElse(null)); | ||
| awsIamRole.map(AwsIamRoleResponse::getExternalId).orElse(null), | ||
| s3CompatibleVersion, | ||
| context.getS3Endpoint(), | ||
| context.getS3Region(), | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit (not a blocker): the cache key's region and the Arguments.of(
"s3_compatible payload",
s3CompatibleCtx("fp-1", false),
s3CompatibleCtx("fp-2", false)),
// The storage reports its region later than a fallback, at the same endpoint.
Arguments.of(
"reported region",
s3CompatibleCtx("fp-1", false, "us-east-1"),
s3CompatibleCtx("fp-1", false, "garage")));with an @Test
public void testFingerprintChangesWithTheStoredPayload() {
String one = "{\"access_key_id\":\"key-id-1\",\"s3_endpoint\":\"" + S3_ENDPOINT + "\"}";
String two = "{\"access_key_id\":\"key-id-2\",\"s3_endpoint\":\"" + S3_ENDPOINT + "\"}";
assertThat(CredentialDAO.builder().credential(one).build().getCredentialFingerprint())
.isEqualTo(CredentialDAO.builder().credential(one).build().getCredentialFingerprint())
.isNotEqualTo(CredentialDAO.builder().credential(two).build().getCredentialFingerprint());
}
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1, could we add the region and endpoint cases plus a real-dao fingerprint check? |
||
| context.isServerAccess() && s3CompatibleVersion.isPresent()); | ||
| } | ||
| } | ||
|
|
||
| private record CredentialVersion(UUID id, String fingerprint) {} | ||
|
|
||
| private record Entry(TemporaryCredentials credential, long serveUntilMs) {} | ||
|
|
||
| /** Expires each entry at its serve deadline; Caffeine's time comes from {@link #clock}. */ | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
could an iceberg reload keep returning 304 after we rotate the keys or change the endpoint? the etag only covers metadata and permissions, and these modes have no credential refresh endpoint. should we include the credential version and storage settings in the etag, or bypass 304 for these responses, with a regression test? fine to track this as a follow-up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point. Agreed to take this as a follow-up: I will open a separate PR to cover the credential version and storage settings in the ETag (or skip 304 for these responses), with a regression test.