XJDKC commented on code in PR #2782: URL: https://github.com/apache/polaris/pull/2782#discussion_r2421965864
########## runtime/service/src/main/java/org/apache/polaris/service/credentials/connection/OAuthConnectionCredentialVendor.java: ########## @@ -0,0 +1,91 @@ +/* + * 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.connection; + +import com.google.common.base.Joiner; +import com.google.common.base.Preconditions; +import jakarta.annotation.Nonnull; +import jakarta.annotation.Priority; +import jakarta.enterprise.context.RequestScoped; +import jakarta.inject.Inject; +import java.time.Instant; +import org.apache.polaris.core.connection.AuthenticationType; +import org.apache.polaris.core.connection.ConnectionConfigInfoDpo; +import org.apache.polaris.core.connection.OAuthClientCredentialsParametersDpo; +import org.apache.polaris.core.credentials.connection.CatalogAccessProperty; +import org.apache.polaris.core.credentials.connection.ConnectionCredentialVendor; +import org.apache.polaris.core.credentials.connection.ConnectionCredentials; +import org.apache.polaris.core.secrets.UserSecretsManager; + +/** + * Connection credential vendor for OAuth 2.0 Client Credentials authentication. + * + * <p>This vendor handles OAuth 2.0 client credentials flow by reading the client secret from the + * secrets manager and formatting it as an OAuth credential for connecting to external catalogs. + * + * <p>The vendor provides only the OAuth credential (formatted as "clientId:clientSecret"). When + * connecting to the remote catalog, Iceberg SDK will use this credential to fetch OAuth tokens + * automatically. + * + * <p>This is the default implementation with {@code @Priority(100)}. Custom implementations can + * override this by providing a higher priority value. + */ +@RequestScoped +@AuthType(AuthenticationType.OAUTH) +@Priority(100) +public class OAuthConnectionCredentialVendor implements ConnectionCredentialVendor { + + private static final Joiner COLON_JOINER = Joiner.on(":"); + + private final UserSecretsManager secretsManager; + + @Inject + public OAuthConnectionCredentialVendor(UserSecretsManager secretsManager) { + this.secretsManager = secretsManager; + } + + @Override + public @Nonnull ConnectionCredentials getConnectionCredentials( + @Nonnull ConnectionConfigInfoDpo connectionConfig) { + + // Validate authentication parameters type + Preconditions.checkArgument( + connectionConfig.getAuthenticationParameters() + instanceof OAuthClientCredentialsParametersDpo, + "Expected OAuthClientCredentialsParametersDpo, got: %s", + connectionConfig.getAuthenticationParameters().getClass().getName()); + + OAuthClientCredentialsParametersDpo oauthParams = + (OAuthClientCredentialsParametersDpo) connectionConfig.getAuthenticationParameters(); + + // Read the client secret from secrets manager + String clientSecret = secretsManager.readSecret(oauthParams.getClientSecretReference()); + + // Format credential as "clientId:clientSecret" + String credential = COLON_JOINER.join(oauthParams.getClientId(), clientSecret); + + // Return the OAuth credential + // OAuth credentials don't expire - set expiration to Instant.MAX to indicate infinite validity + // If the credential expires, users need to update the catalog entity to rotate the credential. + return ConnectionCredentials.builder() + .putCredential(CatalogAccessProperty.OAUTH2_CREDENTIAL.getPropertyName(), credential) Review Comment: Yes, you're right, this class currently just retrieves the OAuth credential from `UserSecretsManager` and returns it. The `PolarisCredentialManager` will then cache it in `AccessCredsCache` (not yet implemented, I plan to add that in a follow-up PR) so that we don't need to fetch the credential from `UserSecretsManager` every time we make a call, especially if the secret manager doesn't have its own cache. The main goal of this PR is to make `PolarisCredentialManager` the single entry point for credential retrieval so we can centralize the caching logic in one place. Regarding your suggestion, I agree, it's still beneficial to include OAuth in the class name to make the purpose of this class clear at a glance. Right now, the class only returns the stored OAuth credential, but in the future, we could extend it to **perform the client credentials flow to obtain a temporary token directly here**, rather than relying on the Iceberg SDK to fetch a new token for every request. WDYT? -- 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]
