Skip to content

Commit 600eb81

Browse files
committed
Fix NPE due to concurrent access of ClientOverrideConfiguration
1 parent 49e61ab commit 600eb81

File tree

2 files changed

+41
-3
lines changed

2 files changed

+41
-3
lines changed

src/main/java/software/amazon/awssdk/s3accessgrants/plugin/S3AccessGrantsPlugin.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,14 @@ String userAgent() {
7272
return this.userAgent;
7373
}
7474

75-
ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder()
76-
.putAdvancedOption(SdkAdvancedClientOption.USER_AGENT_PREFIX, userAgent()).build();
77-
7875
/**
7976
* Change the configuration on the S3Clients to use S3 Access Grants specific AuthScheme and identityProviders.
8077
* @param config the existing configuration on the clients. Passed by the SDK on request path.
8178
* */
8279
@Override
8380
public void configureClient(SdkServiceClientConfiguration.Builder config) {
81+
ClientOverrideConfiguration overrideConfig = ClientOverrideConfiguration.builder()
82+
.putAdvancedOption(SdkAdvancedClientOption.USER_AGENT_PREFIX, userAgent()).build();
8483
logger.info(() -> "Configuring S3 Clients to use S3 Access Grants as a permission layer!");
8584
logger.info(() -> "Running the S3 Access grants plugin with fallback setting enabled : "+enableFallback());
8685
if(!enableFallback()) {

src/test/java/software/amazon/awssdk/s3accessgrants/plugin/S3AccessGrantsPluginTests.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,11 @@
2424
import software.amazon.awssdk.services.s3.auth.scheme.S3AuthSchemeProvider;
2525
import software.amazon.awssdk.auth.credentials.DefaultCredentialsProvider;
2626
import software.amazon.awssdk.regions.Region;
27+
import java.util.concurrent.CountDownLatch;
28+
import java.util.concurrent.ExecutorService;
29+
import java.util.concurrent.Executors;
30+
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.atomic.AtomicInteger;
2732

2833
public class S3AccessGrantsPluginTests {
2934

@@ -156,4 +161,38 @@ public void create_access_grants_plugin_without_userAgent_specified() {
156161
public void create_access_grants_plugin_with_not_permitted_userAgent_name() {
157162
Assertions.assertThatThrownBy(() -> S3AccessGrantsPlugin.builder().userAgent("testUserAgent/").build()).isInstanceOf(IllegalArgumentException.class);
158163
}
164+
165+
@Test
166+
public void configureClient_concurrent_access_should_not_throw_null_pointer_exception() throws InterruptedException {
167+
S3AccessGrantsPlugin accessGrantsPlugin = S3AccessGrantsPlugin.builder().build();
168+
int threadCount = 10;
169+
ExecutorService executor = Executors.newFixedThreadPool(threadCount);
170+
CountDownLatch latch = new CountDownLatch(threadCount);
171+
AtomicInteger successCount = new AtomicInteger(0);
172+
AtomicInteger exceptionCount = new AtomicInteger(0);
173+
174+
for (int i = 0; i < threadCount; i++) {
175+
executor.submit(() -> {
176+
try {
177+
SdkServiceClientConfiguration.Builder config = S3ServiceClientConfiguration.builder()
178+
.authSchemeProvider(S3AuthSchemeProvider.defaultProvider())
179+
.credentialsProvider(DefaultCredentialsProvider.create())
180+
.region(Region.US_EAST_2);
181+
182+
accessGrantsPlugin.configureClient(config);
183+
successCount.incrementAndGet();
184+
} catch (Exception e) {
185+
exceptionCount.incrementAndGet();
186+
} finally {
187+
latch.countDown();
188+
}
189+
});
190+
}
191+
192+
latch.await(10, TimeUnit.SECONDS);
193+
executor.shutdown();
194+
195+
Assertions.assertThat(successCount.get()).isEqualTo(threadCount);
196+
Assertions.assertThat(exceptionCount.get()).isEqualTo(0);
197+
}
159198
}

0 commit comments

Comments
 (0)