nssalian commented on code in PR #1386: URL: https://github.com/apache/polaris/pull/1386#discussion_r2049661167
########## service/common/src/test/java/org/apache/polaris/service/storage/StorageConfigurationTest.java: ########## @@ -0,0 +1,156 @@ +/* + * 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.storage; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.*; + +import com.google.auth.oauth2.AccessToken; +import com.google.auth.oauth2.GoogleCredentials; +import java.time.Duration; +import java.time.Instant; +import java.util.Optional; +import java.util.function.Supplier; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider; +import software.amazon.awssdk.services.sts.StsClient; + +public class StorageConfigurationTest { + + private static final String TEST_ACCESS_KEY = "test-access-key"; + private static final String TEST_GCP_TOKEN = "ya29.test-token"; + private static final String TEST_REGION = "us-west-2"; + private static final String TEST_SECRET_KEY = "test-secret-key"; + private static final Duration TEST_TOKEN_LIFESPAN = Duration.ofMinutes(20); + + @BeforeEach + public void setUpAwsRegion() { + System.setProperty("aws.region", TEST_REGION); + } + + @AfterEach + public void tearDownAwsRegion() { + System.clearProperty("aws.region"); + } + + private StorageConfiguration configWithAwsCredentialsAndGcpToken() { + return new StorageConfiguration() { + @Override + public Optional<String> awsAccessKey() { + return Optional.of(TEST_ACCESS_KEY); + } + + @Override + public Optional<String> awsSecretKey() { + return Optional.of(TEST_SECRET_KEY); + } + + @Override + public Optional<String> gcpAccessToken() { + return Optional.of(TEST_GCP_TOKEN); + } + + @Override + public Optional<Duration> gcpAccessTokenLifespan() { + return Optional.of(TEST_TOKEN_LIFESPAN); + } + }; + } + + private StorageConfiguration configWithoutGcpToken() { + return new StorageConfiguration() { + @Override + public Optional<String> awsAccessKey() { + return Optional.empty(); + } + + @Override + public Optional<String> awsSecretKey() { + return Optional.empty(); + } + + @Override + public Optional<String> gcpAccessToken() { + return Optional.empty(); + } + + @Override + public Optional<Duration> gcpAccessTokenLifespan() { + return Optional.empty(); + } + }; + } + + @Test + public void testSingletonStsClientWithStaticCredentials() { + Supplier<StsClient> supplier = configWithAwsCredentialsAndGcpToken().stsClientSupplier(); + + StsClient client1 = supplier.get(); + StsClient client2 = supplier.get(); + + assertThat(client1).isSameAs(client2); + assertThat(client1).isNotNull(); + + assertThat(client1.serviceClientConfiguration().credentialsProvider()) + .isInstanceOf(StaticCredentialsProvider.class); + assertThat(client1.serviceClientConfiguration().region().toString()).isEqualTo(TEST_REGION); Review Comment: Yeah, I'm happy to remove this line if it is overkill. -- 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: issues-unsubscr...@polaris.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org