Copilot commented on code in PR #11294:
URL: https://github.com/apache/gravitino/pull/11294#discussion_r3346516374


##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/IcebergRESTUtils.java:
##########
@@ -40,7 +41,15 @@
 import javax.ws.rs.core.Response.Status;
 import org.apache.commons.lang3.StringUtils;
 import org.apache.gravitino.NameIdentifier;
+import org.apache.gravitino.credential.ADLSTokenCredential;
+import org.apache.gravitino.credential.AwsIrsaCredential;
+import org.apache.gravitino.credential.Credential;
+import org.apache.gravitino.credential.CredentialPropertyUtils;
+import org.apache.gravitino.credential.GCSTokenCredential;
+import org.apache.gravitino.credential.OSSTokenCredential;
+import org.apache.gravitino.credential.S3TokenCredential;

Review Comment:
   These imports appear to be referenced only from Javadoc and not from code. 
Many builds (e.g., via Checkstyle/Spotless) fail on unused imports; consider 
removing these imports and using fully-qualified names in Javadoc links (or 
referencing the types in code if needed).



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/CatalogWrapperForREST.java:
##########
@@ -198,32 +208,60 @@ public LoadTableResponse updateTable(
    */
   public LoadCredentialsResponse getTableCredentials(
       TableIdentifier identifier, CredentialPrivilege privilege) {
+    if (isRESTCatalog()) {
+      return getRESTTableCredentials((RESTCatalog) getCatalog(), identifier);
+    } else {
+      return getLocalTableCredentials(identifier, privilege);
+    }
+  }

Review Comment:
   In the REST-catalog branch, the `privilege` parameter is ignored, while it 
affects the local-credential branch. If privileges are intended to influence 
the credential payload, thread `privilege` into the REST request (e.g., as a 
query parameter/header, if the upstream API supports it) or explicitly 
document/rename this method to avoid implying privilege-sensitive behavior for 
REST catalogs.



##########
iceberg/iceberg-rest-server/src/main/java/org/apache/gravitino/iceberg/service/CatalogWrapperForREST.java:
##########
@@ -198,32 +208,60 @@ public LoadTableResponse updateTable(
    */
   public LoadCredentialsResponse getTableCredentials(
       TableIdentifier identifier, CredentialPrivilege privilege) {
+    if (isRESTCatalog()) {
+      return getRESTTableCredentials((RESTCatalog) getCatalog(), identifier);
+    } else {
+      return getLocalTableCredentials(identifier, privilege);
+    }
+  }
+
+  private LoadCredentialsResponse getLocalTableCredentials(
+      TableIdentifier identifier, CredentialPrivilege privilege) {
     try {
       LoadTableResponse loadTableResponse = super.loadTable(identifier);
-      Credential credential = getCredential(loadTableResponse, privilege);
-      org.apache.iceberg.rest.credentials.Credential icebergCredential =
-          new org.apache.iceberg.rest.credentials.Credential() {
-            @Override
-            public String prefix() {
-              return "";
-            }
-
-            @Override
-            public Map<String, String> config() {
-              // Convert Gravitino credentials to the Iceberg REST credential 
payload format.
-              return CredentialPropertyUtils.toIcebergProperties(credential);
-            }
-
-            @Override
-            public void validate() {}
-          };
-      return 
ImmutableLoadCredentialsResponse.builder().addCredentials(icebergCredential).build();
+      Credential credential = getCredential(loadTableResponse.tableMetadata(), 
privilege);
+      return ImmutableLoadCredentialsResponse.builder()
+          .addCredentials(
+              IcebergRESTUtils.toRESTCredential(
+                  catalogCredentialManager.catalogName(),
+                  identifier,
+                  credential,
+                  loadTableResponse.tableMetadata()))
+          .build();
     } catch (ServiceUnavailableException e) {
       LOG.warn("Service unavailable when loading table credentials for table: 
{}", identifier, e);
       return ImmutableLoadCredentialsResponse.builder().build();
     }
   }
 
+  private static LoadCredentialsResponse getRESTTableCredentials(
+      RESTCatalog restCatalog, TableIdentifier identifier) {
+    Map<String, String> properties = Maps.newHashMap(restCatalog.properties());
+    String credentialsPath =
+        ResourcePaths.forCatalogProperties(properties).table(identifier) + 
"/credentials";
+
+    try (AuthManager authManager = 
AuthManagers.loadAuthManager(restCatalog.name(), properties);
+        RESTClient client =
+            HTTPClient.builder(properties)
+                .uri(properties.get(CatalogProperties.URI))
+                .withHeaders(RESTUtil.configHeaders(properties))
+                .build();
+        AuthSession authSession = authManager.catalogSession(client, 
properties)) {
+      return client
+          .withAuthSession(authSession)
+          .get(
+              credentialsPath,
+              LoadCredentialsResponse.class,
+              Collections.emptyMap(),
+              ErrorHandlers.tableErrorHandler());
+    } catch (IOException e) {
+      throw new RESTException(
+          e,
+          "Failed to close REST client resources when loading credentials for 
table: %s",
+          identifier);
+    }

Review Comment:
   With try-with-resources, an `IOException` thrown during resource closing 
will cause this method to fail even if the GET request succeeded and a valid 
`LoadCredentialsResponse` was obtained. Consider suppressing/only logging 
close-time IOExceptions (or closing explicitly and not failing the request on 
close errors) so credential vending remains reliable when cleanup fails.



##########
api/src/main/java/org/apache/gravitino/credential/AwsIrsaCredential.java:
##########
@@ -118,12 +118,15 @@ public String sessionToken() {
     return sessionToken;
   }
 
-  private void validate(String accessKeyId, String secretAccessKey, String 
sessionToken) {
+  private void validate(
+      String accessKeyId, String secretAccessKey, String sessionToken, long 
expireTimeInMs) {
     Preconditions.checkArgument(
         StringUtils.isNotBlank(accessKeyId), "Access key Id should not be 
empty");
     Preconditions.checkArgument(
         StringUtils.isNotBlank(secretAccessKey), "Secret access key should not 
be empty");
     Preconditions.checkArgument(
         StringUtils.isNotBlank(sessionToken), "Session token should not be 
empty");
+    Preconditions.checkArgument(
+        expireTimeInMs > 0, "The expire time of AwsIrsaCredential should be 
greater than 0");

Review Comment:
   The error message uses 'expire time' which is grammatically awkward; 
consider changing it to 'expiration time' for clarity.



-- 
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