Repository navigation
Make the AbfsVendedTokenProvider support token renewal automatically #1109
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
c05835c
beb31c5
693c6f2
b24340e
f76ad9a
a9ebd7d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| 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 { | ||
| 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 |
|---|---|---|
|
|
@@ -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) { | ||
|
Collaborator
Author
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. Since the azure don't allow the So here I made the constructor and initialaztion into two steps for the
For the |
||
| this.conf = conf; | ||
|
|
||
| String ucUriStr = conf.get(UCHadoopConf.UC_URI_KEY); | ||
|
|
||
| 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); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.