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
2 changes: 2 additions & 0 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,7 @@ lazy val spark = (project in file("connectors/spark"))
"org.antlr" % "antlr4" % "4.13.1",
"com.google.cloud.bigdataoss" % "util-hadoop" % "3.0.2" % Provided,
"org.apache.hadoop" % "hadoop-azure" % "3.4.0" % Provided,
"software.amazon.awssdk" % "auth" % "2.25.37" % Provided,
Comment thread
tdas marked this conversation as resolved.
Resolved
),
libraryDependencies ++= Seq(
// Test dependencies
Expand All @@ -581,6 +582,7 @@ lazy val spark = (project in file("connectors/spark"))
"org.mockito" % "mockito-junit-jupiter" % "5.12.0" % Test,
"net.aichler" % "jupiter-interface" % JupiterKeys.jupiterVersion.value % Test,
"org.apache.hadoop" % "hadoop-client-runtime" % "3.4.0",
"org.apache.hadoop" % "hadoop-aws" % "3.4.0" % Test,
Comment thread
openinx marked this conversation as resolved.
Resolved
"io.delta" %% "delta-spark" % deltaVersion % Test,
),
dependencyOverrides ++= Seq(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.unitycatalog.spark;

import io.unitycatalog.client.ApiClient;

import java.net.URI;

public class ApiClientFactory {
private ApiClientFactory() {}

public static ApiClient createApiClient(URI url, String token) {
ApiClient apiClient = new ApiClient()
.setHost(url.getHost())
.setPort(url.getPort())
.setScheme(url.getScheme());

if (token != null && !token.isEmpty()) {
apiClient = apiClient.setRequestInterceptor(
request -> request.header("Authorization", "Bearer " + token)
);
}

return apiClient;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package io.unitycatalog.spark;

public class UCHadoopConf {
private UCHadoopConf() {
}

// Keys for the initialized aws s3 credentials.
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";
// Expired time in milliseconds.
public static final String S3A_INIT_CRED_EXPIRED_TIME = "fs.s3a.init.credential.expired.time";
Comment thread
tdas marked this conversation as resolved.
Resolved

// Keys for the UnityCatalog client.
public static final String UC_URI_KEY = "unitycatalog.uri";
public static final String UC_TOKEN_KEY = "unitycatalog.token";

// Keys for table based temporary credential requests
public static final String UC_TABLE_ID_KEY = "unitycatalog.table.id";
public static final String UC_TABLE_OPERATION_KEY = "unitycatalog.table.operation";

// Keys for path based temporary credential requests.
public static final String UC_PATH_KEY = "unitycatalog.path";
public static final String UC_PATH_OPERATION_KEY = "unitycatalog.path.operation";

// Key indicating the credential request type, table or path.
public static final String UC_CREDENTIALS_TYPE_KEY = "unitycatalog.credentials.type";
public static final String UC_CREDENTIALS_TYPE_TABLE_VALUE = "table";
public static final String UC_CREDENTIALS_TYPE_PATH_VALUE = "path";
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package io.unitycatalog.spark.auth;

import io.unitycatalog.spark.UCHadoopConf;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.shaded.com.google.common.base.Preconditions;

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Since we will need to deploy this unitycatalog-spark jar and users need to add it as the dependency, then I will suggest to use the unity catalog relocated guava for the OSS-unitycatalog release.

But in this PR, I don't want to block it. So I temporarily use the hadoop's relocated guava. FYI @tdas

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Filed a separate issue for this : #1102

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

doesnt spark already have with guava as a transitive dependency? cant we just use that guava?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

and i dont see you changing the build file in this PR. if this compiling, then this unitycatalog-spark project already has guava in its dependency .. either directly or via spark. isnt it?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it's indirectly using the hadoop's shaded guava, that's why it currently works fine.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think it should be okay to use the hadoop's guava here, especially the filesystem's CredentialsProvider or TokenProvider, because it's natively the hadoop filesystem plugin.

But in future, if the UCSingleCatalog wants to use the guava, or other place. Then it should be better to have our own shaded guava dep.

For this PR, I will suggest to keep it is. can be a separate task for further UC level guava shading.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

agreed.

@tdas tdas Oct 8, 2025 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

does spark depend on guava? we can always use Spark's guava as UC-Spark will always definitely have Spark?
either ways .. not a blocker for this PR.

import software.amazon.awssdk.auth.credentials.AwsCredentials;
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;

public class AwsVendedTokenProvider extends GenericCredentialProvider
implements AwsCredentialsProvider {

/**
* Constructor for the hadoop's CredentialProviderListFactory#buildAWSProviderList to initialize.
*/
public AwsVendedTokenProvider(Configuration conf) {
super(conf);
}

@Override
public GenericCredential initGenericCredential(Configuration conf) {
if (conf.get(UCHadoopConf.S3A_INIT_ACCESS_KEY) != null
&& conf.get(UCHadoopConf.S3A_INIT_SECRET_KEY) != null
&& conf.get(UCHadoopConf.S3A_INIT_SESSION_TOKEN) != null
&& conf.get(UCHadoopConf.S3A_INIT_CRED_EXPIRED_TIME) != null) {

String accessKey = conf.get(UCHadoopConf.S3A_INIT_ACCESS_KEY);
String secretKey = conf.get(UCHadoopConf.S3A_INIT_SECRET_KEY);
String sessionToken = conf.get(UCHadoopConf.S3A_INIT_SESSION_TOKEN);

long expiredTimeMillis = conf.getLong(UCHadoopConf.S3A_INIT_CRED_EXPIRED_TIME, 0L);
Comment thread
openinx marked this conversation as resolved.
Resolved
Preconditions.checkState(expiredTimeMillis > 0, "Expired time %s must be greater than 0, " +
"please check configure key '%s'", expiredTimeMillis, UCHadoopConf.S3A_INIT_CRED_EXPIRED_TIME);

return GenericCredential.forAws(accessKey, secretKey, sessionToken, expiredTimeMillis);
} else {
return null;
}
}

@Override
public AwsCredentials resolveCredentials() {
GenericCredential generic = accessCredentials();

// Wrap the GenericCredential as an AwsCredentials.
io.unitycatalog.client.model.AwsCredentials awsTempCred = generic
.temporaryCredentials()
.getAwsTempCredentials();
Preconditions.checkNotNull(awsTempCred,
"AWS temp credential of generic credentials cannot be null");

return AwsSessionCredentials.builder()
.accessKeyId(awsTempCred.getAccessKeyId())
.secretAccessKey(awsTempCred.getSecretAccessKey())
.sessionToken(awsTempCred.getSessionToken())
.build();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package io.unitycatalog.spark.auth;

import io.unitycatalog.client.model.AwsCredentials;
import io.unitycatalog.client.model.TemporaryCredentials;

public class GenericCredential {
private final TemporaryCredentials tempCred;

public GenericCredential(TemporaryCredentials tempCred) {
this.tempCred = tempCred;
}

public static GenericCredential forAws(
String accessKey,
String secretKey,
String sessionToken,
long expiredTimeMillis) {
// Initialize the aws credentials.
AwsCredentials awsCredentials = new AwsCredentials();
awsCredentials.setAccessKeyId(accessKey);
awsCredentials.setSecretAccessKey(secretKey);
awsCredentials.setSessionToken(sessionToken);

// Initialize the unity catalog's temporary credentials.
TemporaryCredentials tempCred = new TemporaryCredentials();
tempCred.setAwsTempCredentials(awsCredentials);
tempCred.setExpirationTime(expiredTimeMillis);

return new GenericCredential(tempCred);
}

public TemporaryCredentials temporaryCredentials() {
return tempCred;
}

/**
* Decide whether it's time to renew the credential/token in advance.
*
* @param renewalLeadTimeMillis The amount of time before something expires when the renewal
* process should start.
* @return true if it's ready to renew.
*/
public boolean readyToRenew(long renewalLeadTimeMillis) {
return tempCred.getExpirationTime() != null &&
tempCred.getExpirationTime() <= System.currentTimeMillis() + renewalLeadTimeMillis;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package io.unitycatalog.spark.auth;

import io.unitycatalog.client.ApiException;
import io.unitycatalog.client.api.TemporaryCredentialsApi;
import io.unitycatalog.client.model.GenerateTemporaryPathCredential;
import io.unitycatalog.client.model.GenerateTemporaryTableCredential;
import io.unitycatalog.client.model.PathOperation;
import io.unitycatalog.client.model.TableOperation;
import io.unitycatalog.client.model.TemporaryCredentials;
import io.unitycatalog.spark.ApiClientFactory;
import io.unitycatalog.spark.UCHadoopConf;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.shaded.com.google.common.base.Preconditions;

public abstract class GenericCredentialProvider {
// The time remaining until expiration, we will try to renew the credential before the expiration
// time.
private static final long DEFAULT_RENEWAL_LEAD_TIME_MILLIS = 30 * 1000;

private final Configuration conf;
private final URI ucUri;
private final String ucToken;

private volatile long renewalLeadTimeMillis = DEFAULT_RENEWAL_LEAD_TIME_MILLIS;
private volatile GenericCredential credential;
private volatile TemporaryCredentialsApi tempCredApi;

public GenericCredentialProvider(Configuration conf) {
this.conf = conf;

String ucUriStr = conf.get(UCHadoopConf.UC_URI_KEY);
Preconditions.checkNotNull(ucUriStr,
"'%s' is not set in hadoop configuration", UCHadoopConf.UC_URI_KEY);
this.ucUri = URI.create(ucUriStr);

String ucTokenStr = conf.get(UCHadoopConf.UC_TOKEN_KEY);
Preconditions.checkNotNull(ucTokenStr,
"'%s' is not set in hadoop configuration", UCHadoopConf.UC_TOKEN_KEY);
this.ucToken = conf.get(UCHadoopConf.UC_TOKEN_KEY);

// The initialized credentials passing-through the hadoop configuration.
this.credential = initGenericCredential(conf);
}

public abstract GenericCredential initGenericCredential(Configuration conf);

public GenericCredential accessCredentials() {
if (credential == null || credential.readyToRenew(renewalLeadTimeMillis)) {
synchronized (this) {
if (credential == null || credential.readyToRenew(renewalLeadTimeMillis)) {
try {
credential = createGenericCredentials();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
}

return credential;
}

// For testing purpose only.
void setRenewalLeadTimeMillis(long renewalLeadTimeMillis) {
this.renewalLeadTimeMillis = renewalLeadTimeMillis;
}

protected TemporaryCredentialsApi temporaryCredentialsApi() {
Comment thread
tdas marked this conversation as resolved.
Resolved
if (tempCredApi == null) {
synchronized (this) {
if (tempCredApi == null) {
tempCredApi = new TemporaryCredentialsApi(
ApiClientFactory.createApiClient(ucUri, ucToken));
}
}
}

return tempCredApi;
}

private GenericCredential createGenericCredentials() throws ApiException {
TemporaryCredentialsApi tempCredApi = temporaryCredentialsApi();

// Generate the temporary credential via requesting UnityCatalog.
TemporaryCredentials tempCred;
String type = conf.get(UCHadoopConf.UC_CREDENTIALS_TYPE_KEY);
// TODO We will need to retry the temporary credential request if any recoverable failure, for
// more robustness.
if (UCHadoopConf.UC_CREDENTIALS_TYPE_PATH_VALUE.equals(type)) {
String path = conf.get(UCHadoopConf.UC_PATH_KEY);
String pathOperation = conf.get(UCHadoopConf.UC_PATH_OPERATION_KEY);

tempCred = tempCredApi.generateTemporaryPathCredentials(
new GenerateTemporaryPathCredential()
.url(path)
.operation(PathOperation.fromValue(pathOperation))
);
} else if (UCHadoopConf.UC_CREDENTIALS_TYPE_TABLE_VALUE.equals(type)) {
String tableId = conf.get(UCHadoopConf.UC_TABLE_ID_KEY);
String tableOperation = conf.get(UCHadoopConf.UC_TABLE_OPERATION_KEY);

tempCred = tempCredApi.generateTemporaryTableCredentials(
new GenerateTemporaryTableCredential()
.tableId(tableId)
.operation(TableOperation.fromValue(tableOperation))
);
} else {
throw new IllegalArgumentException(String.format(
"Unsupported unity catalog temporary credentials type '%s', please check '%s'",
type, UCHadoopConf.UC_CREDENTIALS_TYPE_KEY));
}

return new GenericCredential(tempCred);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ public void testTimeTravelDeltaTable() throws ApiException, IOException {

// Time-travel to before the last insert, we should only see the first inserted row.
validateTimeTravelDeltaTable(session.sql("SELECT * FROM " + t1 + " VERSION AS OF 1"));
validateTimeTravelDeltaTable(session.sql("SELECT * FROM " + t1 + " TIMESTAMP AS OF '" + timestamp + "'"));
validateTimeTravelDeltaTable(
Comment thread
tdas marked this conversation as resolved.
Resolved
session.sql("SELECT * FROM " + t1 + " TIMESTAMP AS OF '" + timestamp + "'"));
validateTimeTravelDeltaTable(session.read().option("versionAsOf", 1).table(t1));
validateTimeTravelDeltaTable(session.read().option("timestampAsOf", timestamp).table(t1));

Expand Down
Loading