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 @@ -20,6 +20,15 @@ private UCHadoopConf() {
public static final String AZURE_INIT_SAS_TOKEN_EXPIRED_TIME =
"fs.azure.init.sas.token.expired.time";

// Copy keys from hadoop-azure here, because we don't want to add extra hadoop-azure dependency.
// Same as org.apache.hadoop.fs.azurebfs.constants#FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME
public static final String FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME =
"fs.azure.account.auth.type";
// Same as org.apache.hadoop.fs.azurebfs.constants#FS_AZURE_ACCOUNT_IS_HNS_ENABLED.
public static final String FS_AZURE_ACCOUNT_IS_HNS_ENABLED = "fs.azure.account.hns.enabled";
// Same as org.apache.hadoop.fs.azurebfs.constants#FS_AZURE_SAS_TOKEN_PROVIDER_TYPE.
public static final String FS_AZURE_SAS_TOKEN_PROVIDER_TYPE = "fs.azure.sas.token.provider.type";

// Keys for the initialized Google Cloud Storage OAuth token.
public static final String GCS_INIT_OAUTH_TOKEN = "fs.gs.init.oauth.token";
public static final String GCS_INIT_OAUTH_TOKEN_EXPIRATION_TIME =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package io.unitycatalog.spark.auth;

import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME;

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 the CredPropsUtil is a common code path that will also be used by AwsVendedTokenProvider, so we definitely don't want to add the hadoop-azure when runing spark job on top of the aws s3.

That's why I removed all those hadoop-azure dependency, so that we won't mess up the cloud vendor 's runtime.

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.

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.

That makes complete sense. thank you for catching that.

How do we make sure that we never break this in the future? today every test was passing using these imports. That means that we are compiling with azure libraries as a compile-time dependency... isnt it? if so, should we change it to not dependent on azure/aws/gcs libraries and everything should still compile and work?

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.

One simple thought, is adding an checkstyle enforcement rules in the checkstyle-dev.xml to explicitly forbidden any aws, azure, gcp filesystem imports in the production code base.

import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_ACCOUNT_IS_HNS_ENABLED;
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_SAS_TOKEN_PROVIDER_TYPE;
import static io.unitycatalog.spark.UCHadoopConf.FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME;
import static io.unitycatalog.spark.UCHadoopConf.FS_AZURE_ACCOUNT_IS_HNS_ENABLED;
import static io.unitycatalog.spark.UCHadoopConf.FS_AZURE_SAS_TOKEN_PROVIDER_TYPE;

import io.unitycatalog.client.model.AwsCredentials;
import io.unitycatalog.client.model.AzureUserDelegationSAS;
Expand Down Expand Up @@ -108,6 +108,21 @@ protected GcsPropsBuilder self() {
}
}

private static class AbfsPropsBuilder extends PropsBuilder<AbfsPropsBuilder> {

AbfsPropsBuilder() {
set(FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME, "SAS");
set(FS_AZURE_ACCOUNT_IS_HNS_ENABLED, "true");
set("fs.abfs.impl.disable.cache", "true");
set("fs.abfss.impl.disable.cache", "true");
}

@Override
protected AbfsPropsBuilder self() {
return this;
}
}

private static Map<String, String> s3FixedCredProps(TemporaryCredentials tempCreds) {
AwsCredentials awsCred = tempCreds.getAwsTempCredentials();
return new S3PropsBuilder()
Expand Down Expand Up @@ -226,15 +241,57 @@ private static Map<String, String> gsPathTempCredProps(
.build();
}

private static Map<String, String> abfsProps(TemporaryCredentials tempCreds) {
AzureUserDelegationSAS sas = tempCreds.getAzureUserDelegationSas();
return ImmutableMap.<String, String>builder()
.put(FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME, "SAS")
.put(FS_AZURE_ACCOUNT_IS_HNS_ENABLED, "true")
.put(FS_AZURE_SAS_TOKEN_PROVIDER_TYPE, AbfsVendedTokenProvider.class.getName())
.put(AbfsVendedTokenProvider.ACCESS_TOKEN_KEY, sas.getSasToken())
.put("fs.abfs.impl.disable.cache", "true")
.put("fs.abfss.impl.disable.cache", "true")
private static Map<String, String> abfsFixedCredProps(TemporaryCredentials tempCreds) {
AzureUserDelegationSAS azureSas = tempCreds.getAzureUserDelegationSas();
return new AbfsPropsBuilder()
.set(AbfsVendedTokenProvider.ACCESS_TOKEN_KEY, azureSas.getSasToken())
.build();
}

private static AbfsPropsBuilder abfsTempCredPropsBuilder(
String uri,
String token,
TemporaryCredentials tempCreds) {
AzureUserDelegationSAS azureSas = tempCreds.getAzureUserDelegationSas();
AbfsPropsBuilder builder = new AbfsPropsBuilder()
.set(FS_AZURE_SAS_TOKEN_PROVIDER_TYPE, AbfsVendedTokenProvider.class.getName())
.uri(uri)
.token(token)
.uid(UUID.randomUUID().toString())
.set(UCHadoopConf.AZURE_INIT_SAS_TOKEN, azureSas.getSasToken());

// For the static credential case, nullable expiration time is possible.
if (tempCreds.getExpirationTime() != null) {
builder.set(UCHadoopConf.AZURE_INIT_SAS_TOKEN_EXPIRED_TIME,
String.valueOf(tempCreds.getExpirationTime()));
}

return builder;
}

private static Map<String, String> abfsTableTempCredProps(
String uri,
String token,
String tableId,
TableOperation tableOp,
TemporaryCredentials tempCreds) {
return abfsTempCredPropsBuilder(uri, token, tempCreds)
.credentialType(UCHadoopConf.UC_CREDENTIALS_TYPE_TABLE_VALUE)
.tableId(tableId)
.tableOperation(tableOp)
.build();
}

private static Map<String, String> abfsPathTempCredProps(
String uri,
String token,
String path,
PathOperation pathOp,
TemporaryCredentials tempCreds) {
return abfsTempCredPropsBuilder(uri, token, tempCreds)
.credentialType(UCHadoopConf.UC_CREDENTIALS_TYPE_PATH_VALUE)
.path(path)
.pathOperation(pathOp)
.build();
}

Expand All @@ -261,7 +318,11 @@ public static Map<String, String> createTableCredProps(
}
case "abfss":
case "abfs":
return abfsProps(tempCreds);
if (renewCredEnabled) {
return abfsTableTempCredProps(uri, token, tableId, tableOp, tempCreds);
} else {
return abfsFixedCredProps(tempCreds);
}
default:
return ImmutableMap.of();
}
Expand Down Expand Up @@ -290,7 +351,11 @@ public static Map<String, String> createPathCredProps(
}
case "abfss":
case "abfs":
return abfsProps(tempCreds);
if (renewCredEnabled) {
return abfsPathTempCredProps(uri, token, path, pathOp, tempCreds);
} else {
return abfsFixedCredProps(tempCreds);
}
default:
return ImmutableMap.of();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,61 @@
package io.unitycatalog.spark;

import static io.unitycatalog.spark.UCHadoopConf.FS_AZURE_SAS_TOKEN_PROVIDER_TYPE;
import static org.assertj.core.api.Assertions.assertThat;

import io.unitycatalog.spark.auth.AbfsVendedTokenProvider;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.sparkproject.guava.base.Objects;

public class AzureCredentialTestFileSystem extends CredentialTestFileSystem {
private volatile AbfsVendedTokenProvider provider;

@Override
protected void checkCredentials(Path f) {
Configuration conf = getConf();
String host = f.toUri().getHost();
if (credentialCheckEnabled) {
String mockToken = conf.get(AbfsVendedTokenProvider.ACCESS_TOKEN_KEY);
if ("test-bucket0".equals(host)) {
assertThat(mockToken).isEqualTo("tenantId0/clientId0/clientSecret0");
provider = accessProvider(conf);
if (provider == null) {
String mockToken = conf.get(AbfsVendedTokenProvider.ACCESS_TOKEN_KEY);
assertThat(mockToken).isEqualTo("tenantId0/clientId0/clientSecret0");
} else {
String sasToken = provider.getSASToken("testAccount", "testFs", "testPath", "testOp");
assertThat(sasToken).isEqualTo("tenantId0/clientId0/clientSecret0");
}
} else if ("test-bucket1".equals(host)) {
assertThat(mockToken).isEqualTo("tenantId1/clientId1/clientSecret1");
provider = accessProvider(conf);
if (provider == null) {
String mockToken = conf.get(AbfsVendedTokenProvider.ACCESS_TOKEN_KEY);
assertThat(mockToken).isEqualTo("tenantId1/clientId1/clientSecret1");
} else {
String sasToken = provider.getSASToken("testAccount", "testFs", "testPath", "testOp");
assertThat(sasToken).isEqualTo("tenantId1/clientId1/clientSecret1");
}
} else {
throw new RuntimeException("invalid path: " + f);
}
}
}

private AbfsVendedTokenProvider accessProvider(Configuration conf) {
if (provider == null) {
synchronized (this) {
if (provider == null) {
String clazz = conf.get(FS_AZURE_SAS_TOKEN_PROVIDER_TYPE);
if (Objects.equal(clazz, AbfsVendedTokenProvider.class.getName())) {
provider = new AbfsVendedTokenProvider();
provider.initialize(conf, "testAccountName");
}
}
}
}

return provider;
}

@Override
protected String scheme() {
return "abfs:";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public void testTimeTravelDeltaTable() throws ApiException, IOException {
}

@ParameterizedTest
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false"})
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false", "abfs, true"})
public void testCredentialParquet(String scheme, boolean renewCredEnabled)
throws ApiException, IOException {
SparkSession session = createSparkSessionWithCatalogs(renewCredEnabled, SPARK_CATALOG);
Expand All @@ -188,7 +188,7 @@ public void testCredentialParquet(String scheme, boolean renewCredEnabled)
}

@ParameterizedTest
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false"})
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false", "abfs, true"})
public void testCredentialDelta(String scheme, boolean renewCredEnabled)
throws ApiException, IOException {
SparkSession session =
Expand All @@ -215,7 +215,7 @@ public void testCredentialDelta(String scheme, boolean renewCredEnabled)
}

@ParameterizedTest
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false"})
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false", "abfs, true"})
public void testCredentialCreateDeltaTable(String scheme, boolean renewCredEnabled)
throws IOException {
SparkSession session =
Expand Down Expand Up @@ -252,7 +252,7 @@ public void testCredentialCreateDeltaTable(String scheme, boolean renewCredEnabl
}

@ParameterizedTest
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false"})
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false", "abfs, true"})
public void testDeleteDeltaTable(String scheme, boolean renewCredEnabled)
throws ApiException, IOException {
SparkSession session = createSparkSessionWithCatalogs(renewCredEnabled, SPARK_CATALOG);
Expand All @@ -270,7 +270,7 @@ public void testDeleteDeltaTable(String scheme, boolean renewCredEnabled)
}

@ParameterizedTest
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false"})
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false", "abfs, true"})
public void testMergeDeltaTable(String scheme, boolean renewCredEnabled)
throws ApiException, IOException {
SparkSession session =
Expand Down Expand Up @@ -298,7 +298,7 @@ public void testMergeDeltaTable(String scheme, boolean renewCredEnabled)
}

@ParameterizedTest
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false"})
@CsvSource({"s3, false", "s3, true", "gs, false", "abfs, false", "abfs, true"})
public void testUpdateDeltaTable(String scheme, boolean renewCredEnabled)
throws ApiException, IOException {
SparkSession session = createSparkSessionWithCatalogs(renewCredEnabled, SPARK_CATALOG);
Expand Down