From 0f4cf76f37b0242100dd564353549ad9c64d6059 Mon Sep 17 00:00:00 2001 From: Ronja Quensel <72978761+ronjaquensel@users.noreply.github.com> Date: Thu, 28 Nov 2024 10:07:39 +0100 Subject: [PATCH] fix: iamAsyncClient without endpointOverride (#492) --- .../edc/aws/s3/AwsClientProviderImpl.java | 5 +- .../edc/aws/s3/AwsClientProviderImplTest.java | 77 +++++++++++++++++++ 2 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 extensions/common/aws/aws-s3-core/src/test/java/org/eclipse/edc/aws/s3/AwsClientProviderImplTest.java diff --git a/extensions/common/aws/aws-s3-core/src/main/java/org/eclipse/edc/aws/s3/AwsClientProviderImpl.java b/extensions/common/aws/aws-s3-core/src/main/java/org/eclipse/edc/aws/s3/AwsClientProviderImpl.java index 424b00a1..f5bfc10f 100644 --- a/extensions/common/aws/aws-s3-core/src/main/java/org/eclipse/edc/aws/s3/AwsClientProviderImpl.java +++ b/extensions/common/aws/aws-s3-core/src/main/java/org/eclipse/edc/aws/s3/AwsClientProviderImpl.java @@ -10,6 +10,7 @@ * Contributors: * Bayerische Motoren Werke Aktiengesellschaft (BMW AG) - Initial implementation * ZF Friedrichshafen AG - Initial implementation + * Cofinity-X - fix iamAsyncClient without endpointOverride * */ @@ -42,6 +43,8 @@ import static software.amazon.awssdk.core.client.config.SdkAdvancedAsyncClientOption.FUTURE_COMPLETION_EXECUTOR; public class AwsClientProviderImpl implements AwsClientProvider { + + private static final String NO_ENDPOINT_OVERRIDE = "default"; private final AwsCredentialsProvider credentialsProvider; private final AwsClientProviderConfiguration configuration; @@ -70,7 +73,7 @@ public S3AsyncClient s3AsyncClient(S3ClientRequest clientRequest) { @Override public IamAsyncClient iamAsyncClient(S3ClientRequest clientRequest) { - var key = clientRequest.endpointOverride(); + var key = clientRequest.endpointOverride() != null ? clientRequest.endpointOverride() : NO_ENDPOINT_OVERRIDE; return iamAsyncClients.computeIfAbsent(key, s -> createIamAsyncClient(clientRequest.endpointOverride())); } diff --git a/extensions/common/aws/aws-s3-core/src/test/java/org/eclipse/edc/aws/s3/AwsClientProviderImplTest.java b/extensions/common/aws/aws-s3-core/src/test/java/org/eclipse/edc/aws/s3/AwsClientProviderImplTest.java new file mode 100644 index 00000000..d8240805 --- /dev/null +++ b/extensions/common/aws/aws-s3-core/src/test/java/org/eclipse/edc/aws/s3/AwsClientProviderImplTest.java @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2024 Cofinity-X + * + * This program and the accompanying materials are made available under the + * terms of the Apache License, Version 2.0 which is available at + * https://www.apache.org/licenses/LICENSE-2.0 + * + * SPDX-License-Identifier: Apache-2.0 + * + * Contributors: + * Cofinity-X - initial API and implementation + * + */ + +package org.eclipse.edc.aws.s3; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider; + +import java.net.URI; + +import static org.assertj.core.api.Assertions.assertThat; + +class AwsClientProviderImplTest { + + private AwsClientProvider clientProvider; + + @BeforeEach + void setUp() { + var config = AwsClientProviderConfiguration.Builder.newInstance() + .credentialsProvider(DefaultCredentialsProvider.create()) + .build(); + clientProvider = new AwsClientProviderImpl(config); + } + + @Test + void iamAsyncClient_noEndpointOverride_shouldReturnClient() { + var clientRequest = S3ClientRequest.from("region", null); + + var client = clientProvider.iamAsyncClient(clientRequest); + + assertThat(client).isNotNull(); + assertThat(client.serviceClientConfiguration().endpointOverride()).isEmpty(); + } + + @Test + void iamAsyncClient_requestMultipleTimesNoEndpointOverride_shouldReturnSameClient() { + var clientRequest = S3ClientRequest.from("region", null); + + var client1 = clientProvider.iamAsyncClient(clientRequest); + var client2 = clientProvider.iamAsyncClient(clientRequest); + + assertThat(client1).isSameAs(client2); + } + + @Test + void iamAsyncClient_withEndpointOverride_shouldReturnClient() { + var endpointOverride = "https://endpointOverride"; + var clientRequest = S3ClientRequest.from("region", endpointOverride); + + var client = clientProvider.iamAsyncClient(clientRequest); + + assertThat(client).isNotNull(); + assertThat(client.serviceClientConfiguration().endpointOverride()).contains(URI.create(endpointOverride)); + } + + @Test + void iamAsyncClient_requestMultipleTimesWithEndpointOverride_shouldReturnSameClient() { + var clientRequest = S3ClientRequest.from("region", "https://endpointOverride"); + + var client1 = clientProvider.iamAsyncClient(clientRequest); + var client2 = clientProvider.iamAsyncClient(clientRequest); + + assertThat(client1).isSameAs(client2); + } +}