FANNG1 commented on code in PR #5737: URL: https://github.com/apache/gravitino/pull/5737#discussion_r1870473509
########## bundles/azure-bundle/src/main/java/org/apache/gravitino/abs/credential/ADLSTokenProvider.java: ########## @@ -0,0 +1,105 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.gravitino.abs.credential; + +import com.azure.core.credential.TokenCredential; +import com.azure.identity.ClientSecretCredentialBuilder; +import com.azure.storage.file.datalake.DataLakeFileSystemClientBuilder; +import com.azure.storage.file.datalake.DataLakeServiceClient; +import com.azure.storage.file.datalake.DataLakeServiceClientBuilder; +import com.azure.storage.file.datalake.models.UserDelegationKey; +import com.azure.storage.file.datalake.sas.DataLakeServiceSasSignatureValues; +import com.azure.storage.file.datalake.sas.PathSasPermission; +import java.time.OffsetDateTime; +import java.util.Map; +import org.apache.gravitino.credential.ADLSTokenCredential; +import org.apache.gravitino.credential.Credential; +import org.apache.gravitino.credential.CredentialConstants; +import org.apache.gravitino.credential.CredentialContext; +import org.apache.gravitino.credential.CredentialProvider; +import org.apache.gravitino.credential.PathBasedCredentialContext; +import org.apache.gravitino.credential.config.ADLSCredentialConfig; + +/** Generates ADLS token to access ADLS data. */ +public class ADLSTokenProvider implements CredentialProvider { + private String storageAccountName; + private String tenantId; + private String clientId; + private String clientSecret; + private String endpoint; + private Integer tokenExpireSecs; + + @Override + public void initialize(Map<String, String> properties) { + ADLSCredentialConfig adlsCredentialConfig = new ADLSCredentialConfig(properties); + this.storageAccountName = adlsCredentialConfig.storageAccountName(); + this.tenantId = adlsCredentialConfig.tenantId(); + this.clientId = adlsCredentialConfig.clientId(); + this.clientSecret = adlsCredentialConfig.clientSecret(); + this.endpoint = String.format("https://%s.dfs.core.windows.net", storageAccountName); + this.tokenExpireSecs = adlsCredentialConfig.tokenExpireInSecs(); + } + + @Override + public void close() {} + + @Override + public String credentialType() { + return CredentialConstants.ADLS_TOKEN_CREDENTIAL_PROVIDER_TYPE; + } + + @Override + public Credential getCredential(CredentialContext context) { + if (!(context instanceof PathBasedCredentialContext)) { + return null; + } + + TokenCredential tokenCredential = + new ClientSecretCredentialBuilder() + .tenantId(tenantId) + .clientId(clientId) + .clientSecret(clientSecret) + .build(); + + DataLakeServiceClient dataLakeServiceClient = + new DataLakeServiceClientBuilder() + .endpoint(endpoint) + .credential(tokenCredential) + .buildClient(); + + OffsetDateTime start = OffsetDateTime.now(); + OffsetDateTime expiry = start.plusSeconds(tokenExpireSecs); + UserDelegationKey userDelegationKey = dataLakeServiceClient.getUserDelegationKey(start, expiry); + + // Define Path SAS permissions, read, write, delete, list + PathSasPermission permission = PathSasPermission.parse("rwdl"); + DataLakeServiceSasSignatureValues signatureValues = + new DataLakeServiceSasSignatureValues(expiry, permission); Review Comment: 1. It's critical to restrict the access path in azure tokens for security. 2. We could get the path information in `PathBasedCredentialContext`, as it only supports one location, we could add a check for the path, throw exception if there are multi paths, since in most cases, there should be only one path. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: commits-unsubscr...@gravitino.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org