dimas-b commented on code in PR #2782: URL: https://github.com/apache/polaris/pull/2782#discussion_r2418218843
########## runtime/service/src/test/java/org/apache/polaris/service/credentials/connection/BearerConnectionCredentialVendorTest.java: ########## @@ -0,0 +1,108 @@ +/* + * 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 static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.time.Instant; +import org.apache.polaris.core.connection.BearerAuthenticationParametersDpo; +import org.apache.polaris.core.connection.iceberg.IcebergRestConnectionConfigInfoDpo; +import org.apache.polaris.core.credentials.connection.CatalogAccessProperty; +import org.apache.polaris.core.credentials.connection.ConnectionCredentials; +import org.apache.polaris.core.secrets.SecretReference; +import org.apache.polaris.core.secrets.UserSecretsManager; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Tests for {@link BearerConnectionCredentialVendor}. */ +public class BearerConnectionCredentialVendorTest { + + private BearerConnectionCredentialVendor bearerVendor; + private UserSecretsManager mockSecretsManager; + + @BeforeEach + void setup() { + mockSecretsManager = mock(UserSecretsManager.class); + bearerVendor = new BearerConnectionCredentialVendor(mockSecretsManager); + } + + @Test + public void testGetConnectionCredentials() { + // Setup + SecretReference bearerTokenRef = + new SecretReference("urn:polaris-secret:test:bearer-token", java.util.Map.of()); + when(mockSecretsManager.readSecret(bearerTokenRef)).thenReturn("my-bearer-token-value"); + + BearerAuthenticationParametersDpo authParams = + new BearerAuthenticationParametersDpo(bearerTokenRef); + + IcebergRestConnectionConfigInfoDpo connectionConfig = + new IcebergRestConnectionConfigInfoDpo( + "https://catalog.example.com", authParams, null, "test-catalog"); + + // Execute + ConnectionCredentials credentials = bearerVendor.getConnectionCredentials(connectionConfig); + + // Verify - only bearer token is provided + Assertions.assertThat(credentials.credentials()) + .hasSize(1) + .containsEntry( + CatalogAccessProperty.BEARER_TOKEN.getPropertyName(), "my-bearer-token-value"); + Assertions.assertThat(credentials.expiresAt()).contains(Instant.MAX); + } + + @Test + public void testGetConnectionCredentialsWithWrongAuthType() { + // Setup - use a mock with wrong authentication type + org.apache.polaris.core.connection.ConnectionConfigInfoDpo mockConfig = Review Comment: import? ########## polaris-core/src/main/java/org/apache/polaris/core/credentials/connection/CatalogAccessProperty.java: ########## @@ -28,15 +29,23 @@ * Catalog service. */ public enum CatalogAccessProperty { + // OAuth + OAUTH2_CREDENTIAL(String.class, OAuth2Properties.CREDENTIAL, "the OAuth2 credential", true), + + // Bearer + BEARER_TOKEN(String.class, OAuth2Properties.TOKEN, "the bearer token", true), + + // SigV4 AWS_ACCESS_KEY_ID(String.class, AwsProperties.REST_ACCESS_KEY_ID, "the aws access key id", true), AWS_SECRET_ACCESS_KEY( - String.class, AwsProperties.REST_SECRET_ACCESS_KEY, "the aws access key secret", true), - AWS_SESSION_TOKEN( - String.class, AwsProperties.REST_SESSION_TOKEN, "the aws scoped access token", true), + String.class, AwsProperties.REST_SECRET_ACCESS_KEY, "the aws secret access key", true), + AWS_SESSION_TOKEN(String.class, AwsProperties.REST_SESSION_TOKEN, "the aws session token", true), + + // Metadata EXPIRATION_TIME( Review Comment: This enum value does not appear to be used anywhere. ########## runtime/service/src/main/java/org/apache/polaris/service/credentials/connection/SigV4ConnectionCredentialVendor.java: ########## @@ -58,7 +58,7 @@ * <p>This is the default implementation with {@code @Priority(100)}. Custom implementations can * override this by providing a higher priority value. */ -@ApplicationScoped +@RequestScoped Review Comment: Why do we need this change? ########## runtime/service/src/test/java/org/apache/polaris/service/credentials/connection/OAuthConnectionCredentialVendorTest.java: ########## @@ -0,0 +1,115 @@ +/* + * 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 static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.time.Instant; +import java.util.List; +import org.apache.polaris.core.connection.OAuthClientCredentialsParametersDpo; +import org.apache.polaris.core.connection.iceberg.IcebergRestConnectionConfigInfoDpo; +import org.apache.polaris.core.credentials.connection.CatalogAccessProperty; +import org.apache.polaris.core.credentials.connection.ConnectionCredentials; +import org.apache.polaris.core.secrets.SecretReference; +import org.apache.polaris.core.secrets.UserSecretsManager; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Tests for {@link OAuthConnectionCredentialVendor}. */ +public class OAuthConnectionCredentialVendorTest { + + private OAuthConnectionCredentialVendor oauthVendor; + private UserSecretsManager mockSecretsManager; + + @BeforeEach + void setup() { + mockSecretsManager = mock(UserSecretsManager.class); + oauthVendor = new OAuthConnectionCredentialVendor(mockSecretsManager); + } + + @Test + public void testGetConnectionCredentials() { + // Setup + SecretReference clientSecretRef = + new SecretReference("urn:polaris-secret:test:oauth-client-secret", java.util.Map.of()); + when(mockSecretsManager.readSecret(clientSecretRef)).thenReturn("my-client-secret"); + + OAuthClientCredentialsParametersDpo authParams = + new OAuthClientCredentialsParametersDpo( + "https://auth.example.com/token", + "my-client-id", + clientSecretRef, + List.of("catalog", "read:data")); + + IcebergRestConnectionConfigInfoDpo connectionConfig = + new IcebergRestConnectionConfigInfoDpo( + "https://catalog.example.com", authParams, null, "test-catalog"); + + // Execute + ConnectionCredentials credentials = oauthVendor.getConnectionCredentials(connectionConfig); + + // Verify - only OAuth credential is provided + Assertions.assertThat(credentials.credentials()) + .hasSize(1) + .containsEntry( + CatalogAccessProperty.OAUTH2_CREDENTIAL.getPropertyName(), + "my-client-id:my-client-secret"); + Assertions.assertThat(credentials.expiresAt()).contains(Instant.MAX); + } + + @Test + public void testGetConnectionCredentialsWithWrongAuthType() { + // Setup - use a mock with wrong authentication type + org.apache.polaris.core.connection.ConnectionConfigInfoDpo mockConfig = Review Comment: import? ########## runtime/service/src/test/java/org/apache/polaris/service/credentials/connection/ImplicitConnectionCredentialVendorTest.java: ########## @@ -0,0 +1,74 @@ +/* + * 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 static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +import java.time.Instant; +import org.apache.polaris.core.connection.ImplicitAuthenticationParametersDpo; +import org.apache.polaris.core.connection.iceberg.IcebergRestConnectionConfigInfoDpo; +import org.apache.polaris.core.credentials.connection.ConnectionCredentials; +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** Tests for {@link ImplicitConnectionCredentialVendor}. */ +public class ImplicitConnectionCredentialVendorTest { + + private ImplicitConnectionCredentialVendor implicitVendor; + + @BeforeEach + void setup() { + implicitVendor = new ImplicitConnectionCredentialVendor(); + } + + @Test + public void testGetConnectionCredentials() { + // Setup + ImplicitAuthenticationParametersDpo authParams = new ImplicitAuthenticationParametersDpo(); + + IcebergRestConnectionConfigInfoDpo connectionConfig = + new IcebergRestConnectionConfigInfoDpo( + "https://catalog.example.com", authParams, null, "test-catalog"); + + // Execute + ConnectionCredentials credentials = implicitVendor.getConnectionCredentials(connectionConfig); + + // Verify - no credentials are provided, but expiration is set to infinite + Assertions.assertThat(credentials.credentials()).isEmpty(); + Assertions.assertThat(credentials.expiresAt()).contains(Instant.MAX); + } + + @Test + public void testGetConnectionCredentialsWithWrongAuthType() { + // Setup - use a mock with wrong authentication type + org.apache.polaris.core.connection.ConnectionConfigInfoDpo mockConfig = Review Comment: import? ########## 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: There is nothing in this class that actually relates to the OAuth2 spec as far as I can tell. It only relates to the Iceberg `OAuth2Properties.CREDENTIAL` constant, which again has very little to do with OAuth2 RFCs as far as connection configuration is concerned... It's just a property name plus format spec 🤷 What the client does with this property (whether it's OAuth2 or something else) is beyond the scope of this credential vendor. At this level we do not know how the credential is used. I'd propose to rename this to "Iceberg Credential Vendor". -- 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]
