Skip to content

Commit b2ffac9

Browse files
committed
Optimizing profile file loading
1 parent aa091a1 commit b2ffac9

File tree

5 files changed

+16
-34
lines changed

5 files changed

+16
-34
lines changed

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/InstanceProfileCredentialsProvider.java

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ public final class InstanceProfileCredentialsProvider
7878
private final Clock clock;
7979
private final String endpoint;
8080
private final Ec2MetadataConfigProvider configProvider;
81+
private final Ec2MetadataDisableV1Resolver ec2MetadataDisableV1Resolver;
8182
private final HttpCredentialsLoader httpCredentialsLoader;
8283
private final CachedSupplier<AwsCredentials> credentialsCache;
8384

@@ -89,6 +90,7 @@ public final class InstanceProfileCredentialsProvider
8990

9091
private final String profileName;
9192

93+
9294
/**
9395
* @see #builder()
9496
*/
@@ -97,15 +99,18 @@ private InstanceProfileCredentialsProvider(BuilderImpl builder) {
9799
this.endpoint = builder.endpoint;
98100
this.asyncCredentialUpdateEnabled = builder.asyncCredentialUpdateEnabled;
99101
this.asyncThreadName = builder.asyncThreadName;
100-
this.profileFile = builder.profileFile;
101-
this.profileName = builder.profileName;
102+
this.profileFile = Optional.ofNullable(builder.profileFile)
103+
.orElseGet(() -> ProfileFileSupplier.fixedProfileFile(ProfileFile.defaultProfileFile()));
104+
this.profileName = Optional.ofNullable(builder.profileName)
105+
.orElseGet(ProfileFileSystemSetting.AWS_PROFILE::getStringValueOrThrow);
102106

103107
this.httpCredentialsLoader = HttpCredentialsLoader.create();
104108
this.configProvider =
105109
Ec2MetadataConfigProvider.builder()
106-
.profileFile(builder.profileFile)
107-
.profileName(builder.profileName)
110+
.profileFile(profileFile)
111+
.profileName(profileName)
108112
.build();
113+
this.ec2MetadataDisableV1Resolver = Ec2MetadataDisableV1Resolver.create(profileFile, profileName);
109114

110115
if (Boolean.TRUE.equals(builder.asyncCredentialUpdateEnabled)) {
111116
Validate.paramNotBlank(builder.asyncThreadName, "asyncThreadName");
@@ -268,7 +273,7 @@ private String handleTokenErrorResponse(Exception e) {
268273
}
269274

270275
private boolean isInsecureFallbackDisabled() {
271-
return Ec2MetadataDisableV1Resolver.create(profileFile, profileName).resolve();
276+
return ec2MetadataDisableV1Resolver.resolve();
272277
}
273278

274279
private String[] getSecurityCredentials(String imdsHostname, String metadataToken) {

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/internal/Ec2MetadataDisableV1Resolver.java

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import software.amazon.awssdk.annotations.SdkInternalApi;
2121
import software.amazon.awssdk.core.SdkSystemSetting;
2222
import software.amazon.awssdk.profiles.ProfileFile;
23-
import software.amazon.awssdk.profiles.ProfileFileSystemSetting;
2423
import software.amazon.awssdk.profiles.ProfileProperty;
2524
import software.amazon.awssdk.utils.OptionalUtils;
2625

@@ -49,27 +48,9 @@ private static Optional<Boolean> fromSystemSettings() {
4948
}
5049

5150
private static Optional<Boolean> fromProfileFile(Supplier<ProfileFile> profileFile, String profileName) {
52-
profileFile = profileFile != null ? profileFile : ProfileFile::defaultProfileFile;
53-
profileName = profileName != null ? profileName : ProfileFileSystemSetting.AWS_PROFILE.getStringValueOrThrow();
54-
if (profileFile.get() == null) {
55-
return Optional.empty();
56-
}
5751
return profileFile.get()
5852
.profile(profileName)
59-
.flatMap(p -> p.property(ProfileProperty.EC2_METADATA_V1_DISABLED))
60-
.map(Ec2MetadataDisableV1Resolver::safeProfileStringToBoolean);
61-
}
62-
63-
private static boolean safeProfileStringToBoolean(String value) {
64-
if (value.equalsIgnoreCase("true")) {
65-
return true;
66-
}
67-
if (value.equalsIgnoreCase("false")) {
68-
return false;
69-
}
70-
71-
throw new IllegalStateException("Profile property '" + ProfileProperty.EC2_METADATA_V1_DISABLED + "', "
72-
+ "was defined as '" + value + "', but should be 'false' or 'true'");
53+
.flatMap(p -> p.booleanProperty(ProfileProperty.EC2_METADATA_V1_DISABLED));
7354
}
7455

7556
}

core/auth/src/main/java/software/amazon/awssdk/auth/credentials/internal/ProfileCredentialsUtils.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,7 @@ private AwsCredentialsProvider credentialSourceCredentialProvider(CredentialSour
262262
case ECS_CONTAINER:
263263
return ContainerCredentialsProvider.builder().build();
264264
case EC2_INSTANCE_METADATA:
265-
// The IMDS credentials provider should source the endpoint config properties from the currently active profile
266-
Ec2MetadataConfigProvider configProvider = Ec2MetadataConfigProvider.builder()
267-
.profileFile(() -> profileFile)
268-
.profileName(name)
269-
.build();
270265
return InstanceProfileCredentialsProvider.builder()
271-
.endpoint(configProvider.getEndpoint())
272266
.profileFile(profileFile)
273267
.profileName(name)
274268
.build();

core/auth/src/test/java/software/amazon/awssdk/auth/credentials/internal/Ec2MetadataDisableV1ResolverTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,13 @@ public void resolveDisableValue_whenBoolean_resolvesCorrectly(
5555
}
5656

5757
private static Stream<Arguments> booleanConfigValues() {
58+
ProfileFile emptyProfile = configFile("profile test", Pair.of("foo", "bar"));
59+
5860
Function<String, ProfileFile> profileDisableValues =
5961
s -> configFile("profile test", Pair.of(ProfileProperty.EC2_METADATA_V1_DISABLED, s));
6062

6163
return Stream.of(
62-
Arguments.of(null, null, null, false),
64+
Arguments.of(null, null, emptyProfile, false),
6365
Arguments.of("false", null, null, false),
6466
Arguments.of("true", null, null, true),
6567
Arguments.of(null, "false", null, false),
@@ -84,8 +86,7 @@ public void resolveDisableValue_whenNonBoolean_throws(
8486
setUpSystemSettings(systemProperty, envVar);
8587

8688
Ec2MetadataDisableV1Resolver resolver = Ec2MetadataDisableV1Resolver.create(() -> profileFile, "test");
87-
assertThatThrownBy(resolver::resolve).isInstanceOf(IllegalStateException.class)
88-
.hasMessageContaining("but should be 'false' or 'true'");
89+
assertThatThrownBy(resolver::resolve).isInstanceOf(IllegalStateException.class);
8990
}
9091

9192
private static Stream<Arguments> nonBooleanConfigValues() {

core/regions/src/test/java/software/amazon/awssdk/regions/internal/util/EC2MetadataUtilsTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class EC2MetadataUtilsTest {
5555
public void methodSetup() {
5656
System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_SERVICE_ENDPOINT.property(), "http://localhost:" + mockMetadataEndpoint.port());
5757
EC2MetadataUtils.clearCache();
58+
System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_V1_DISABLED.property());
5859
}
5960

6061
@Test

0 commit comments

Comments
 (0)