Skip to content

ProfileFileSupplier aggregate files in order #3754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 10, 2023
Merged
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
6 changes: 6 additions & 0 deletions .changes/next-release/bugfix-AWSSDKforJavav2-6d1d0aa.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"category": "AWS SDK for Java v2",
"contributor": "",
"type": "bugfix",
"description": "Keep precedence of options when passed to ProfileFileSupplier.aggregate"
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@
package software.amazon.awssdk.profiles;

import java.nio.file.Path;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.Supplier;
import software.amazon.awssdk.annotations.SdkPublicApi;
Expand Down Expand Up @@ -119,7 +121,8 @@ static ProfileFileSupplier aggregate(ProfileFileSupplier... suppliers) {
return new ProfileFileSupplier() {

final AtomicReference<ProfileFile> currentAggregateProfileFile = new AtomicReference<>();
final ConcurrentHashMap<Supplier<ProfileFile>, ProfileFile> currentValuesBySupplier = new ConcurrentHashMap<>();
final Map<Supplier<ProfileFile>, ProfileFile> currentValuesBySupplier
= Collections.synchronizedMap(new LinkedHashMap<>());

@Override
public ProfileFile get() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,56 @@ void aggregate_supplierReturnsSameInstanceMultipleTimesAggregatingProfileFileSup
assertThat(suppliedProfileFiles).isEqualTo(distinctAggregateProfileFiles);
}

@Test
void aggregate_duplicateOptionsGivenFixedProfileFirst_preservesPrecedence() {
ProfileFile configFile1 = configFile("profile default", Pair.of("aws_access_key_id", "config-key"));
Path credentialsFilePath = generateTestCredentialsFile("defaultAccessKey", "defaultSecretAccessKey");

ProfileFileSupplier supplier = ProfileFileSupplier.aggregate(
ProfileFileSupplier.fixedProfileFile(configFile1),
ProfileFileSupplier.reloadWhenModified(credentialsFilePath, ProfileFile.Type.CREDENTIALS));

ProfileFile profileFile = supplier.get();
String accessKeyId = profileFile.profile("default").get().property("aws_access_key_id").get();

assertThat(accessKeyId).isEqualTo("config-key");

generateTestCredentialsFile("defaultAccessKey2", "defaultSecretAccessKey2");

profileFile = supplier.get();
accessKeyId = profileFile.profile("default").get().property("aws_access_key_id").get();

assertThat(accessKeyId).isEqualTo("config-key");
}

@Test
void aggregate_duplicateOptionsGivenReloadingProfileFirst_preservesPrecedence() {
AdjustableClock clock = new AdjustableClock();

ProfileFile configFile1 = configFile("profile default", Pair.of("aws_access_key_id", "config-key"));
Path credentialsFilePath = generateTestCredentialsFile("defaultAccessKey", "defaultSecretAccessKey");

ProfileFileSupplier supplier = ProfileFileSupplier.aggregate(
builderWithClock(clock)
.reloadWhenModified(credentialsFilePath, ProfileFile.Type.CREDENTIALS)
.build(),
ProfileFileSupplier.fixedProfileFile(configFile1));

ProfileFile profileFile = supplier.get();
String accessKeyId = profileFile.profile("default").get().property("aws_access_key_id").get();

assertThat(accessKeyId).isEqualTo("defaultAccessKey");

generateTestCredentialsFile("defaultAccessKey2", "defaultSecretAccessKey2");

clock.tickForward(Duration.ofMillis(1_000));

profileFile = supplier.get();
accessKeyId = profileFile.profile("default").get().property("aws_access_key_id").get();

assertThat(accessKeyId).isEqualTo("defaultAccessKey2");
}

@Test
void fixedProfileFile_nullProfileFile_returnsNonNullSupplier() {
ProfileFile file = null;
Expand Down