Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import io.unitycatalog.client.auth.TokenProvider;
import io.unitycatalog.client.internal.ApiClientUtils;
import io.unitycatalog.client.internal.Clock;
import io.unitycatalog.client.internal.Preconditions;
import io.unitycatalog.hadoop.UCCredentialHadoopConfs;
import io.unitycatalog.hadoop.internal.auth.AwsCredential;
import io.unitycatalog.hadoop.internal.auth.CredentialCache;
import io.unitycatalog.hadoop.internal.auth.CredentialCache.RenewableCredential;
import io.unitycatalog.hadoop.internal.auth.GenericCredential;
Expand Down Expand Up @@ -194,13 +196,34 @@ private static Map<String, String> createCredProps(
List<GenericCredential> credentials =
fetchGenericCredentials(
hadoopConf, apiClient, catalogUri, tokenProvider, appVersions, credId);
// A table's credentials are assumed to be all with keys or all without: this connector does
// not yet support mixing them, as it sets one S3A credentials provider for every path, while a
// location vended only its S3 endpoint relies on the engine's own keys.
// TODO: support a table whose credentials mix storage, by processing each credential on its
// own, for example with a per-bucket fs.s3a.bucket.<bucket>.aws.credentials.provider, as the
// endpoint and region already are.
boolean anyHasKeys = credentials.stream().anyMatch(CredPropsUtil::hasKeys);
Preconditions.checkArgument(
!anyHasKeys || credentials.stream().allMatch(CredPropsUtil::hasKeys),
"Unity Catalog vended an S3 endpoint without keys together with credentials with keys for"
+ " %s; such a set cannot be served",
credId);
CredPropsBuilder builder =
CredPropsBuilder.forCloud(cloudType.get(), hadoopConf)
// Its cache tells apart the storage and the engine's own keys behind an S3 endpoint
// (FileSystemCredId), so it serves credentials without keys too.
.credScopedFsEnabled(credScopedFsEnabled)
.initialCredentials(credentials)
.credId(credId);

if (renewCredEnabled) {
// Renew when a credential expires. A single key pair without a lifetime has nothing to renew,
// so it uses fixed keys instead; credentials without keys leave the engine's own in use.
// Several credentials with keys always take the vended provider, which picks the one covering
// each path.
if (renewCredEnabled
&& anyHasKeys
&& (credentials.size() > 1
|| credentials.stream().anyMatch(GenericCredential::isRenewable))) {
builder.enableRenewCred(catalogUri, tokenProvider, credId, appVersions);
}

Expand Down Expand Up @@ -281,4 +304,8 @@ private static ApiClient createApiClient(
UCHadoopConfConstants.createRequestRetryPolicy(null),
appVersions);
}

private static boolean hasKeys(GenericCredential credential) {
return !(credential instanceof AwsCredential) || ((AwsCredential) credential).hasKeys();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,9 @@ public static GenericCredential toGenericCredential(TemporaryCredentials tempCre
aws.getSecretAccessKey(),
aws.getSessionToken(),
expiry,
tempCred.getUrl());
tempCred.getUrl(),
aws.getS3Endpoint(),
aws.getS3Region());
} else if (tempCred.getAzureUserDelegationSas() != null) {
return new AzureCredential(
tempCred.getAzureUserDelegationSas().getSasToken(), expiry, tempCred.getUrl());
Expand All @@ -70,7 +72,9 @@ public static GenericCredential toGenericCredential(DeltaStorageCredential cred)
config.getS3SecretAccessKey(),
config.getS3SessionToken(),
expiry,
cred.getPrefix());
cred.getPrefix(),
config.getS3Endpoint(),
config.getClientRegion());
} else if (isAzureConfig(config)) {
return new AzureCredential(config.getAzureSasToken(), expiry, cred.getPrefix());
} else {
Expand Down Expand Up @@ -155,9 +159,11 @@ private static DeltaStorageCredentialConfig requireSingleCloudConfig(
}

private static boolean isS3Config(DeltaStorageCredentialConfig c) {
// An S3 endpoint alone is an S3 config: UC vends only the endpoint when it does not vend keys.
return c.getS3AccessKeyId() != null
|| c.getS3SecretAccessKey() != null
|| c.getS3SessionToken() != null;
|| c.getS3SessionToken() != null
|| c.getS3Endpoint() != null;
}

private static boolean isAzureConfig(DeltaStorageCredentialConfig c) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,19 @@ private UCHadoopConfConstants() {}
public static final String S3A_INIT_ACCESS_KEY = "fs.s3a.init.access.key";
public static final String S3A_INIT_SECRET_KEY = "fs.s3a.init.secret.key";
public static final String S3A_INIT_SESSION_TOKEN = "fs.s3a.init.session.token";
// The S3 endpoint of S3-compatible storage; when set, AwsVendedTokenProvider accepts an initial
// key pair without a session token.
public static final String S3A_INIT_S3_ENDPOINT = "fs.s3a.init.s3.endpoint";
// The region vended with that endpoint, so the initial credential matches later renewals.
public static final String S3A_INIT_S3_REGION = "fs.s3a.init.s3.region";
// The S3 endpoint and region Unity Catalog vended for a bucket when the table was loaded, each
// followed by the bucket name; empty for AWS S3. AwsVendedTokenProvider refuses a renewal for
// other storage, since the file system keeps sending requests where it was set up to.
public static final String UC_VENDED_S3_ENDPOINT_PREFIX = "fs.unitycatalog.vended.s3.endpoint.";
public static final String UC_VENDED_S3_REGION_PREFIX = "fs.unitycatalog.vended.s3.region.";
// Whether Unity Catalog vended keys for the bucket, followed by the bucket name: without them the
// engine's own S3 credentials sign, which may be per Hadoop user.
public static final String UC_VENDED_S3_KEYS_PREFIX = "fs.unitycatalog.vended.s3.keys.";
// Expired time in milliseconds.
public static final String S3A_INIT_CRED_EXPIRED_TIME = "fs.s3a.init.credential.expired.time";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,95 @@ public final class AwsCredential extends GenericCredential {
private final String accessKeyId;
private final String secretAccessKey;
private final String sessionToken;
// S3 endpoint of S3-compatible storage; null for AWS S3.
private final String s3Endpoint;
// Region to sign requests to s3Endpoint with; null if not known.
private final String s3Region;

public AwsCredential(
String accessKeyId,
String secretAccessKey,
String sessionToken,
Long expirationTimeMillis,
String prefix) {
this(accessKeyId, secretAccessKey, sessionToken, expirationTimeMillis, prefix, null);
}

/**
* For S3-compatible storage the keys may be absent, when Unity Catalog vends only the S3
* endpoint, and the session token may be absent, for a key pair, even one with a lifetime.
*
* @param accessKeyId the access key ID
* @param secretAccessKey the secret access key
* @param sessionToken the session token
* @param expirationTimeMillis the expiration time, or null or {@code Long.MAX_VALUE} if it does
* not expire
* @param prefix the storage path the credential covers
* @param s3Endpoint the S3 endpoint of S3-compatible storage, or null for AWS S3
*/
public AwsCredential(
String accessKeyId,
String secretAccessKey,
String sessionToken,
Long expirationTimeMillis,
String prefix,
String s3Endpoint) {
this(
accessKeyId,
secretAccessKey,
sessionToken,
expirationTimeMillis,
prefix,
s3Endpoint,
/* s3Region= */ null);
}

/**
* Like {@link #AwsCredential(String, String, String, Long, String, String)}, with the region of
* the S3-compatible storage.
*
* @param s3Region the region to sign requests to {@code s3Endpoint} with, or null if not known
*/
public AwsCredential(
String accessKeyId,
String secretAccessKey,
String sessionToken,
Long expirationTimeMillis,
String prefix,
String s3Endpoint,
String s3Region) {
super(expirationTimeMillis, prefix);
Preconditions.checkArgument(
accessKeyId != null && !accessKeyId.isEmpty(), "AWS access key is missing");
Preconditions.checkArgument(
secretAccessKey != null && !secretAccessKey.isEmpty(), "AWS secret key is missing");
Preconditions.checkArgument(
sessionToken != null && !sessionToken.isEmpty(), "AWS session token is missing");
// Empty is absent, as for a credential read back from the configuration: the API declares these
// as plain strings, and an empty endpoint would otherwise name S3-compatible storage.
sessionToken = emptyToNull(sessionToken);
s3Endpoint = emptyToNull(s3Endpoint);
s3Region = emptyToNull(s3Region);
boolean isEndpointOnly =
s3Endpoint != null
&& accessKeyId == null
&& secretAccessKey == null
&& sessionToken == null;
if (!isEndpointOnly) {
Preconditions.checkArgument(
accessKeyId != null && !accessKeyId.isEmpty(), "AWS access key is missing");
Preconditions.checkArgument(
secretAccessKey != null && !secretAccessKey.isEmpty(), "AWS secret key is missing");
// Only S3-compatible storage vends a key pair without a session token.
Preconditions.checkArgument(
s3Endpoint != null
? sessionToken == null || !sessionToken.isEmpty()
: sessionToken != null && !sessionToken.isEmpty(),
"AWS session token is missing");
}
this.accessKeyId = accessKeyId;
this.secretAccessKey = secretAccessKey;
this.sessionToken = sessionToken;
this.s3Endpoint = s3Endpoint;
this.s3Region = s3Region;
}

private static String emptyToNull(String value) {
return value == null || value.isEmpty() ? null : value;
}

public String accessKeyId() {
Expand All @@ -38,6 +110,34 @@ public String sessionToken() {
return sessionToken;
}

/** The S3 endpoint of S3-compatible storage, or null for AWS S3. */
public String s3Endpoint() {
return s3Endpoint;
}

/** The region to sign requests to {@link #s3Endpoint()} with, or null if not known. */
public String s3Region() {
return s3Region;
}

/**
* Whether this carries keys; an S3-endpoint-only credential leaves the engine's own keys in use.
*/
public boolean hasKeys() {
return accessKeyId != null;
}

/**
* Session credentials, and a key pair vended with a lifetime, are renewed so that an update of
* the key pair reaches the engine; a key pair without a lifetime, or only an S3 endpoint, is not.
*/
@Override
public boolean isRenewable() {
Long expiration = expirationTimeMillis();
return sessionToken != null
|| (hasKeys() && expiration != null && expiration != Long.MAX_VALUE);
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand All @@ -49,11 +149,14 @@ public boolean equals(Object o) {
AwsCredential that = (AwsCredential) o;
return Objects.equals(accessKeyId, that.accessKeyId)
&& Objects.equals(secretAccessKey, that.secretAccessKey)
&& Objects.equals(sessionToken, that.sessionToken);
&& Objects.equals(sessionToken, that.sessionToken)
&& Objects.equals(s3Endpoint, that.s3Endpoint)
&& Objects.equals(s3Region, that.s3Region);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), accessKeyId, secretAccessKey, sessionToken);
return Objects.hash(
super.hashCode(), accessKeyId, secretAccessKey, sessionToken, s3Endpoint, s3Region);
}
}
Loading
Loading