Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"type": "bugfix",
"category": "AWS SDK for Java v2",
"contributor": "",
"description": "Fixed a race condition that could cause concurrent initial calls to `ProfileCredentialsProvider` to fail while loading the default profile files."
}
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@ public AwsCredentials resolveCredentials() {
throw loadException;
}

if (credentialsProvider == null) {
synchronized (credentialsProviderLock) {
if (credentialsProvider == null) {
ProfileFile profileFile = refreshProfileFile();
currentProfileFile = profileFile;
handleProfileFileReload(profileFile);
}
}

return credentialsProvider.resolveCredentials();
}

ProfileFile cachedOrRefreshedProfileFile = refreshProfileFile();
if (shouldUpdateCredentialsProvider(cachedOrRefreshedProfileFile)) {
synchronized (credentialsProviderLock) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Optional;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
Expand Down Expand Up @@ -271,6 +277,52 @@ void resolveCredentials_defaultProfileFileSupplier_refreshesCredentials() {
});
}

@Test
void resolveCredentials_concurrentInitialCalls_loadProfileFileSerially() throws Exception {
ProfileFile file = profileFile("[default]\naws_access_key_id = akid\n"
+ "aws_secret_access_key = sak\n");
AtomicInteger supplierCalls = new AtomicInteger();
CountDownLatch firstSupplierCallStarted = new CountDownLatch(1);
CountDownLatch secondSupplierCallStarted = new CountDownLatch(1);
CountDownLatch releaseFirstSupplierCall = new CountDownLatch(1);
CountDownLatch secondTaskStarted = new CountDownLatch(1);
Supplier<ProfileFile> supplier = () -> {
int call = supplierCalls.incrementAndGet();
if (call == 1) {
firstSupplierCallStarted.countDown();
await(releaseFirstSupplierCall);
} else if (call == 2) {
secondSupplierCallStarted.countDown();
}
return file;
};
ProfileCredentialsProvider provider = ProfileCredentialsProvider.builder()
.profileFile(supplier)
.profileName("default")
.build();
ExecutorService executor = Executors.newFixedThreadPool(2);

try {
Future<AwsCredentials> firstResult = executor.submit(provider::resolveCredentials);
assertThat(firstSupplierCallStarted.await(5, TimeUnit.SECONDS)).isTrue();
Future<AwsCredentials> secondResult = executor.submit(() -> {
secondTaskStarted.countDown();
return provider.resolveCredentials();
});
assertThat(secondTaskStarted.await(5, TimeUnit.SECONDS)).isTrue();

assertThat(secondSupplierCallStarted.await(1, TimeUnit.SECONDS)).isFalse();
releaseFirstSupplierCall.countDown();

assertThat(firstResult.get(5, TimeUnit.SECONDS).accessKeyId()).isEqualTo("akid");
assertThat(secondResult.get(5, TimeUnit.SECONDS).accessKeyId()).isEqualTo("akid");
assertThat(supplierCalls).hasValue(1);
} finally {
releaseFirstSupplierCall.countDown();
executor.shutdownNow();
}
}

@Test
void create_noProfileName_returnsProfileCredentialsProviderToResolveWithDefaults() {
ProfileCredentialsProvider provider = ProfileCredentialsProvider.create();
Expand Down Expand Up @@ -346,6 +398,15 @@ private ProfileFile profileFile(String string) {
return ProfileFile.builder().content(new StringInputStream(string)).type(ProfileFile.Type.CONFIGURATION).build();
}

private static void await(CountDownLatch latch) {
try {
latch.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException(e);
}
}

private Path generateTestFile(String contents, String filename) {
try {
Files.createDirectories(testDirectory);
Expand Down