Repository navigation
Add AwsVendedTokenProvider to renew the correct credentials. #1098
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
60a880b
8ef13e4
be6f0ff
7bf38f9
8ba4031
e29714b
ba6fd3b
b0ee925
2d2521f
39eea03
59c43a6
c848457
e400b8f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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"; | ||
|
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; | ||
|
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 we will need to deploy this But in this PR, I don't want to block it. So I temporarily use the hadoop's relocated guava. FYI @tdas
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. Filed a separate issue for this : #1102
Contributor
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. doesnt spark already have with guava as a transitive dependency? cant we just use that guava?
Contributor
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. 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?
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. I think it's indirectly using the hadoop's shaded
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. I think it should be okay to use the But in future, if the For this PR, I will suggest to keep it is. can be a separate task for further UC level guava shading.
Contributor
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. agreed.
Contributor
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. does spark depend on guava? we can always use Spark's guava as UC-Spark will always definitely have Spark? |
||
| 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); | ||
|
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() { | ||
|
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); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.