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

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ private UCHadoopConf() {
// Expired time in milliseconds.
public static final String S3A_INIT_CRED_EXPIRED_TIME = "fs.s3a.init.credential.expired.time";

// Keys for the initialized Azure Blob Storage token.
public static final String AZURE_INIT_SAS_TOKEN = "fs.azure.init.sas.token";
public static final String AZURE_INIT_SAS_TOKEN_EXPIRED_TIME =
"fs.azure.init.sas.token.expired.time";

// Keys for the UnityCatalog client.
public static final String UC_URI_KEY = "unitycatalog.uri";
public static final String UC_TOKEN_KEY = "unitycatalog.token";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package io.unitycatalog.spark
import io.unitycatalog.client.{ApiClient, ApiException}
import io.unitycatalog.client.api.{SchemasApi, TablesApi, TemporaryCredentialsApi}
import io.unitycatalog.client.model.{ColumnInfo, ColumnTypeName, CreateSchema, CreateTable, DataSourceFormat, GenerateTemporaryPathCredential, GenerateTemporaryTableCredential, ListTablesResponse, PathOperation, SchemaInfo, TableOperation, TableType, TemporaryCredentials}
import io.unitycatalog.spark.auth.AbfsVendedTokenProvider

import java.net.URI
import java.util
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package io.unitycatalog.spark.auth;

import io.unitycatalog.client.model.AzureUserDelegationSAS;
import io.unitycatalog.spark.UCHadoopConf;
import org.apache.hadoop.fs.azurebfs.extensions.SASTokenProvider;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.shaded.com.google.common.base.Preconditions;

public class AbfsVendedTokenProvider extends GenericCredentialProvider implements SASTokenProvider {
Comment thread
tdas marked this conversation as resolved.
Resolved
public static final String ACCESS_TOKEN_KEY = "fs.azure.sas.fixed.token";

@Override
public void initialize(Configuration conf, String accountName) {
initialize(conf);
}

@Override
public GenericCredential initGenericCredential(Configuration conf) {
if (conf.get(UCHadoopConf.AZURE_INIT_SAS_TOKEN) != null
&& conf.get(UCHadoopConf.AZURE_INIT_SAS_TOKEN_EXPIRED_TIME) != null) {

String sasToken = conf.get(UCHadoopConf.AZURE_INIT_SAS_TOKEN);
Preconditions.checkNotNull(sasToken, "Azure SAS token not set, please check " +
"'%s' in hadoop configuration", UCHadoopConf.AZURE_INIT_SAS_TOKEN);

long expiredTimeMillis = conf.getLong(UCHadoopConf.AZURE_INIT_SAS_TOKEN_EXPIRED_TIME, 0L);
Preconditions.checkState(expiredTimeMillis > 0,
"Azure SAS token expired time must be greater than 0, please check '%s' in hadoop " +
"configuration", UCHadoopConf.AZURE_INIT_SAS_TOKEN_EXPIRED_TIME);

return GenericCredential.forAzure(sasToken, expiredTimeMillis);
} else {
return null;
}
}

@Override
public String getSASToken(String account, String fileSystem, String path, String operation) {
GenericCredential generic = accessCredentials();

AzureUserDelegationSAS azureSAS = generic.temporaryCredentials().getAzureUserDelegationSas();
Preconditions.checkNotNull(azureSAS, "Azure SAS of generic credential cannot be null");

return azureSAS.getSasToken();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class AwsVendedTokenProvider extends GenericCredentialProvider
* Constructor for the hadoop's CredentialProviderListFactory#buildAWSProviderList to initialize.
*/
public AwsVendedTokenProvider(Configuration conf) {
super(conf);
initialize(conf);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.unitycatalog.spark.auth;

import io.unitycatalog.client.model.AwsCredentials;
import io.unitycatalog.client.model.AzureUserDelegationSAS;
import io.unitycatalog.client.model.TemporaryCredentials;
import java.util.Objects;

Expand Down Expand Up @@ -30,6 +31,17 @@ public static GenericCredential forAws(
return new GenericCredential(tempCred);
}

public static GenericCredential forAzure(String sasToken, long expiredTimeMillis) {
AzureUserDelegationSAS azureSAS = new AzureUserDelegationSAS();
azureSAS.setSasToken(sasToken);

TemporaryCredentials tempCred = new TemporaryCredentials();
tempCred.setAzureUserDelegationSas(azureSAS);
tempCred.setExpirationTime(expiredTimeMillis);

return new GenericCredential(tempCred);
}

public TemporaryCredentials temporaryCredentials() {
return tempCred;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,28 @@ public abstract class GenericCredentialProvider {
// The credential cache, for saving QPS to unity catalog server.
static final Cache<String, GenericCredential> globalCache;
private static final String UC_CREDENTIAL_CACHE_MAX_SIZE =
"unitycatalog.credential.cache.max.size";
"unitycatalog.credential.cache.maxSize";
private static final long UC_CREDENTIAL_CACHE_MAX_SIZE_DEFAULT = 1024;

static {
long maxSize = Long.getLong(UC_CREDENTIAL_CACHE_MAX_SIZE, UC_CREDENTIAL_CACHE_MAX_SIZE_DEFAULT);
globalCache = CacheBuilder.newBuilder().maximumSize(maxSize).build();
}

private final Configuration conf;
private final URI ucUri;
private final String ucToken;
private final String credUid;
private final boolean credCacheEnabled;
private Configuration conf;
private URI ucUri;
private String ucToken;
private String credUid;
private boolean credCacheEnabled;

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

public GenericCredentialProvider(Configuration conf) {
public GenericCredentialProvider() {
}

protected void initialize(Configuration conf) {

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 azure don't allow the new X(conf) constructor to initialize the token provider instance, it just use the new X() constructor , and then use the x.initialize(conf) to do the initialization.

So here I made the constructor and initialaztion into two steps for the GenericCredentialProvider

  1. no arg constructor, to new the constructor.
  2. initialize(conf) to initialze the fields.

For the AwsVendedTokenProvider, it only allows the new X(conf) to initialize the constructor. Then it should be simple, since we can call new X() then call initialize(conf) inside the constructor.

see also: https://github.com/unitycatalog/unitycatalog/pull/1109/files#diff-7e8f6bba96ad4c72fb501672ee3e863ad3ff4faf0d04bf9df3dbc4067102d807R17

this.conf = conf;

String ucUriStr = conf.get(UCHadoopConf.UC_URI_KEY);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package io.unitycatalog.spark.auth;

import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME;
import static org.apache.hadoop.fs.azurebfs.constants.ConfigurationKeys.FS_AZURE_SAS_TOKEN_PROVIDER_TYPE;
import static org.apache.hadoop.fs.azurebfs.services.AuthType.SAS;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;

import io.unitycatalog.client.api.TemporaryCredentialsApi;
import io.unitycatalog.client.model.AzureUserDelegationSAS;
import io.unitycatalog.client.model.TemporaryCredentials;
import io.unitycatalog.spark.UCHadoopConf;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.azurebfs.AbfsConfiguration;
import org.apache.hadoop.fs.azurebfs.extensions.SASTokenProvider;
import org.junit.jupiter.api.Test;

public class AbfsVendedTokenProviderTest extends BaseTokenProviderTest<AbfsVendedTokenProvider> {
@Override
protected AbfsVendedTokenProvider createTestProvider(
Configuration conf, TemporaryCredentialsApi mockApi) {
return new TestAbfsVendedTokenProvider(conf, mockApi);
}

static class TestAbfsVendedTokenProvider extends AbfsVendedTokenProvider {
private final TemporaryCredentialsApi mockApi;

TestAbfsVendedTokenProvider(Configuration conf, TemporaryCredentialsApi mockApi) {
initialize(conf);
this.mockApi = mockApi;
}

@Override
protected TemporaryCredentialsApi temporaryCredentialsApi() {
return mockApi;
}
}

@Override
protected TemporaryCredentials newTempCred(String id, long expirationMillis) {
AzureUserDelegationSAS sas = new AzureUserDelegationSAS();
sas.setSasToken("sasToken" + id);

TemporaryCredentials tempCred = new TemporaryCredentials();
tempCred.setAzureUserDelegationSas(sas);
tempCred.setExpirationTime(expirationMillis);
return tempCred;
}

@Override
protected void setInitialCred(Configuration conf, TemporaryCredentials cred) {
assertThat(cred.getAzureUserDelegationSas()).isNotNull();
assertThat(cred.getExpirationTime()).isNotNull();
conf.set(UCHadoopConf.AZURE_INIT_SAS_TOKEN, cred.getAzureUserDelegationSas().getSasToken());
conf.setLong(UCHadoopConf.AZURE_INIT_SAS_TOKEN_EXPIRED_TIME, cred.getExpirationTime());
}

@Override
protected void assertCred(AbfsVendedTokenProvider provider, TemporaryCredentials expected) {
String sasToken = provider.getSASToken("account", "fileSystem", "path", "operation");

assertThat(expected.getAzureUserDelegationSas()).isNotNull();
AzureUserDelegationSAS expectedSAS = expected.getAzureUserDelegationSas();

assertThat(expectedSAS.getSasToken()).isEqualTo(sasToken);
}

@Test
public void testConstructor() {
Configuration conf = new Configuration();
AbfsVendedTokenProvider provider = new AbfsVendedTokenProvider();

// Verify the UC URI validation error message.
assertThatThrownBy(() -> provider.initialize(conf))
.isInstanceOf(NullPointerException.class)
.hasMessage("'%s' is not set in hadoop configuration", UCHadoopConf.UC_URI_KEY);

// Verify the UC Token validation error message.
conf.set(UCHadoopConf.UC_URI_KEY, "http://localhost:8080");
assertThatThrownBy(() -> provider.initialize(conf))
.isInstanceOf(NullPointerException.class)
.hasMessage("'%s' is not set in hadoop configuration", UCHadoopConf.UC_TOKEN_KEY);

// Verify the UID validation error message.
conf.set(UCHadoopConf.UC_TOKEN_KEY, "unity-catalog-token");
assertThatThrownBy(() -> provider.initialize(conf))
.isInstanceOf(IllegalStateException.class)
.hasMessage(
"Credential UID cannot be null or empty, '%s' is not set in hadoop configuration",
UCHadoopConf.UC_CREDENTIALS_UID_KEY);
}

@Test
public void testLoadProvider() throws Exception {
Configuration conf = newTableBasedConf();
conf.setEnum(FS_AZURE_ACCOUNT_AUTH_TYPE_PROPERTY_NAME, SAS);
conf.set(FS_AZURE_SAS_TOKEN_PROVIDER_TYPE, AbfsVendedTokenProvider.class.getName());

AbfsConfiguration abfsConf = new AbfsConfiguration(conf, "account");

SASTokenProvider provider = abfsConf.getSASTokenProvider();
assertThat(provider).isInstanceOf(AbfsVendedTokenProvider.class);
}
}
Loading