FANNG1 commented on code in PR #5224:
URL: https://github.com/apache/gravitino/pull/5224#discussion_r1814761543


##########
core/src/main/java/org/apache/gravitino/credential/config/GCSCredentialConfig.java:
##########
@@ -0,0 +1,47 @@
+/*
+ *  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.credential.config;
+
+import java.util.Map;
+import javax.annotation.Nullable;
+import org.apache.gravitino.Config;
+import org.apache.gravitino.config.ConfigBuilder;
+import org.apache.gravitino.config.ConfigConstants;
+import org.apache.gravitino.config.ConfigEntry;
+import org.apache.gravitino.storage.GCSProperties;
+
+public class GCSCredentialConfig extends Config {
+  public static final ConfigEntry<String> GCS_CREDENTIAL_FILE_PATH =
+      new ConfigBuilder(GCSProperties.GRAVITINO_GCS_CREDENTIAL_FILE_PATH)

Review Comment:
   it's more extensible, different catalogs could use different credentials.



##########
bundles/gcp-bundle/src/main/java/org/apache/gravitino/gcs/credential/GCSTokenProvider.java:
##########
@@ -0,0 +1,205 @@
+/*
+ *  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.gcs.credential;
+
+import com.google.auth.oauth2.AccessToken;
+import com.google.auth.oauth2.CredentialAccessBoundary;
+import com.google.auth.oauth2.DownscopedCredentials;
+import com.google.auth.oauth2.GoogleCredentials;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.net.URI;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Stream;
+import org.apache.commons.lang3.StringUtils;
+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.GCSTokenCredential;
+import org.apache.gravitino.credential.PathBasedCredentialContext;
+import org.apache.gravitino.credential.config.GCSCredentialConfig;
+
+/** Generate GCS access token according to the read and write paths. */
+public class GCSTokenProvider implements CredentialProvider {
+
+  private static final String INITIAL_SCOPE = 
"https://www.googleapis.com/auth/cloud-platform";;
+
+  private GoogleCredentials sourceCredentials;
+
+  @Override
+  public void initialize(Map<String, String> properties) {
+    GCSCredentialConfig gcsCredentialConfig = new 
GCSCredentialConfig(properties);
+    try {
+      this.sourceCredentials =
+          
getSourceCredentials(gcsCredentialConfig).createScoped(INITIAL_SCOPE);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  @Override
+  public void close() {}
+
+  @Override
+  public String credentialType() {
+    return CredentialConstants.GCS_TOKEN_CREDENTIAL_PROVIDER_TYPE;
+  }
+
+  @Override
+  public Credential getCredential(CredentialContext context) {
+    if (!(context instanceof PathBasedCredentialContext)) {
+      return null;
+    }
+    PathBasedCredentialContext pathBasedCredentialContext = 
(PathBasedCredentialContext) context;
+    try {
+      AccessToken accessToken =
+          getToken(
+              pathBasedCredentialContext.getReadPaths(),
+              pathBasedCredentialContext.getWritePaths());
+      String tokenValue = accessToken.getTokenValue();
+      long expireTime = 
accessToken.getExpirationTime().toInstant().toEpochMilli();
+      return new GCSTokenCredential(tokenValue, expireTime);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  private AccessToken getToken(Set<String> readLocations, Set<String> 
writeLocations)
+      throws IOException {
+    DownscopedCredentials downscopedCredentials =
+        DownscopedCredentials.newBuilder()
+            .setSourceCredential(sourceCredentials)
+            .setCredentialAccessBoundary(getAccessBoundary(readLocations, 
writeLocations))
+            .build();
+    return downscopedCredentials.refreshAccessToken();
+  }
+
+  private CredentialAccessBoundary getAccessBoundary(
+      Set<String> readLocations, Set<String> writeLocations) {
+    // bucketName -> read resource expressions
+    Map<String, List<String>> readExpressions = new HashMap<>();
+    // bucketName -> write resource expressions
+    Map<String, List<String>> writeExpressions = new HashMap<>();
+
+    // Construct read and write resource expressions
+    HashSet<String> readBuckets = new HashSet<>();
+    HashSet<String> writeBuckets = new HashSet<>();
+    Stream.concat(readLocations.stream(), writeLocations.stream())
+        .distinct()
+        .forEach(
+            location -> {
+              URI uri = URI.create(location);
+              String bucketName = getBucketName(uri);
+              readBuckets.add(bucketName);
+              String resourcePath = uri.getPath().substring(1);
+              List<String> resourceExpressions =
+                  readExpressions.computeIfAbsent(bucketName, key -> new 
ArrayList<>());
+              // add read privilege
+              resourceExpressions.add(
+                  String.format(
+                      
"resource.name.startsWith('projects/_/buckets/%s/objects/%s')",
+                      bucketName, resourcePath));
+              // add list privilege
+              resourceExpressions.add(
+                  String.format(
+                      
"api.getAttribute('storage.googleapis.com/objectListPrefix', 
'').startsWith('%s')",
+                      resourcePath));
+              if (writeLocations.contains(location)) {
+                writeBuckets.add(bucketName);
+                resourceExpressions =
+                    writeExpressions.computeIfAbsent(bucketName, key -> new 
ArrayList<>());

Review Comment:
   ok



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to