XJDKC commented on code in PR #2759:
URL: https://github.com/apache/polaris/pull/2759#discussion_r2411839449


##########
runtime/service/src/main/java/org/apache/polaris/service/credentials/DefaultPolarisCredentialManager.java:
##########
@@ -0,0 +1,109 @@
+/*
+ * 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.polaris.service.credentials;
+
+import io.smallrye.common.annotation.Identifier;
+import jakarta.annotation.Nonnull;
+import jakarta.enterprise.context.RequestScoped;
+import jakarta.enterprise.inject.Any;
+import jakarta.enterprise.inject.Instance;
+import jakarta.inject.Inject;
+import org.apache.polaris.core.connection.AuthenticationType;
+import org.apache.polaris.core.connection.ConnectionConfigInfoDpo;
+import org.apache.polaris.core.context.RealmContext;
+import org.apache.polaris.core.credentials.PolarisCredentialManager;
+import 
org.apache.polaris.core.credentials.connection.ConnectionCredentialVendor;
+import org.apache.polaris.core.credentials.connection.ConnectionCredentials;
+import org.apache.polaris.service.credentials.connection.AuthType;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Default implementation of {@link PolarisCredentialManager} responsible for 
retrieving credentials
+ * used by Polaris to access external systems such as remote catalogs or cloud 
storage.
+ *
+ * <p>This implementation delegates to {@link ConnectionCredentialVendor} 
implementations selected
+ * via CDI based on the authentication type. Each vendor handles the 
credential transformation logic
+ * for a specific authentication mechanism (e.g., SigV4, OAuth).
+ *
+ * <p>This bean is request-scoped and realm-aware, delegating all credential 
generation to
+ * CDI-managed vendors.
+ *
+ * <p>Flow:
+ *
+ * <ol>
+ *   <li>Selects the appropriate {@link ConnectionCredentialVendor} based on 
the authentication type
+ *   <li>Delegates to the vendor to generate the final connection credentials 
(the vendor will
+ *       resolve the service identity internally)
+ * </ol>
+ */
+@RequestScoped
+@Identifier("default")
+public class DefaultPolarisCredentialManager implements 
PolarisCredentialManager {
+  private static final Logger LOGGER =
+      LoggerFactory.getLogger(DefaultPolarisCredentialManager.class);
+
+  private final RealmContext realmContext;
+  private final Instance<ConnectionCredentialVendor> credentialVendors;
+
+  @Inject
+  public DefaultPolarisCredentialManager(
+      RealmContext realmContext, @Any Instance<ConnectionCredentialVendor> 
credentialVendors) {
+    this.realmContext = realmContext;
+    this.credentialVendors = credentialVendors;
+  }
+
+  public RealmContext getRealmContext() {
+    return realmContext;
+  }
+
+  @Override
+  public @Nonnull ConnectionCredentials getConnectionCredentials(
+      @Nonnull ConnectionConfigInfoDpo connectionConfig) {
+
+    // Select the appropriate vendor based on authentication type
+    AuthenticationType authType =
+        connectionConfig.getAuthenticationParameters().getAuthenticationType();
+    Instance<ConnectionCredentialVendor> selectedVendor =
+        credentialVendors.select(AuthType.Literal.of(authType));
+
+    if (selectedVendor.isUnsatisfied()) {

Review Comment:
   I think I have explained here:
   
   > However, there are several benefits to using this CDI-based delegation 
approach:
   > * **Cleaner dependency management**: PolarisCredentialManager would no 
longer need to hold objects specific to a particular ConnectionCredentialVendor 
that may never be used.
   >     * For example, in 
[PolarisStorageIntegrationProviderImpl.java](https://github.com/apache/polaris/blob/main/runtime/service/src/main/java/org/apache/polaris/service/storage/PolarisStorageIntegrationProviderImpl.java),
 we currently keep `stsClientProvider`, `stsCredentials`, and 
`gcpCredsProvider`. But if the storage happens to be GCP, the STS-related 
fields are unnecessary.
           * Moreover, the class uses a switch-case statement to instantiate 
the correct storage integration, and use new to construct the object. With the 
CDI-based approach, the implementation becomes cleaner and provides better 
isolation, also it's easier to extend.
           * The overall goal is to encapsulate all authentication- or 
storage-specific logic within their respective `ConnectionCredentialVendor` or 
`StorageCredentialVendor`. The `PolarisCredentialManager` would simply delegate 
credential retrieval to the appropriate vendor, acting primarily as a 
redirection and caching layer.
   > * **Simpler extensibility**: if a Polaris vendor only wants to override 
the logic for a specific auth type, this structure makes that much easier.
   
   > As for handling errors, what do you think about adding a validation step in
   
[ProductionReadinessChecks.java](https://github.com/apache/polaris/blob/main/runtime/service/src/main/java/org/apache/polaris/service/config/ProductionReadinessChecks.java)
   
   If you feel strongly about it, I'm happy to go with the old approach similar 
to `PolarisStorageIntegrationProviderImpl`. I think both approaches have their 
pros and cons, I chose the current implementation mainly because I felt it was 
a bit clearer: `PolarisCredentialManager` doesn't need to understand the 
underlying logic of each `CredentialVendor`. The vendors can still reuse the 
common logic in `PolarisCredentialManager` (such as caching and multiplexing); 
they only need to override the parts specific to their own credential handling.



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