Skip to content

Commit 11210ca

Browse files
authored
Fix NPE in ProfileFileSupplier.defaultSupplier (#6150)
* Fix NPE in ProfileFileSupplier.defaultSupplier * Add changelog * Cleanup builder validation
1 parent 3831d9e commit 11210ca

File tree

5 files changed

+37
-2
lines changed

5 files changed

+37
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "bugfix",
3+
"category": "AWS SDK for Java v2",
4+
"contributor": "",
5+
"description": "Fix NPE in `ProfileFileSupplier.defaultSupplier` when both credentials and config files do not exist."
6+
}

core/profiles/src/main/java/software/amazon/awssdk/profiles/ProfileFile.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ public static Aggregator aggregator() {
8585
return new Aggregator();
8686
}
8787

88+
/**
89+
* Create an empty profile file.
90+
*/
91+
static ProfileFile empty() {
92+
return new ProfileFile(Collections.emptyMap());
93+
}
94+
8895
/**
8996
* Get the default profile file, using the credentials file from "~/.aws/credentials", the config file from "~/.aws/config"
9097
* and the "default" profile. This default behavior can be customized using the
@@ -310,8 +317,10 @@ public void setType(Type type) {
310317

311318
@Override
312319
public ProfileFile build() {
320+
Validate.isTrue(content != null || contentLocation != null,
321+
"content or contentLocation must be set.");
313322
InputStream stream = content != null ? content :
314-
FunctionalUtils.invokeSafely(() -> Files.newInputStream(contentLocation));
323+
FunctionalUtils.invokeSafely(() -> Files.newInputStream(contentLocation));
315324

316325
Validate.paramNotNull(type, "type");
317326
Validate.paramNotNull(stream, "content");

core/profiles/src/main/java/software/amazon/awssdk/profiles/ProfileFileSupplier.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,15 @@ static ProfileFileSupplier defaultSupplier() {
5555
= ProfileFileLocation.configurationFileLocation()
5656
.map(path -> reloadWhenModified(path, ProfileFile.Type.CONFIGURATION));
5757

58-
ProfileFileSupplier supplier = () -> ProfileFile.builder().build();
58+
ProfileFileSupplier supplier;
5959
if (credentialsSupplierOptional.isPresent() && configurationSupplierOptional.isPresent()) {
6060
supplier = aggregate(credentialsSupplierOptional.get(), configurationSupplierOptional.get());
6161
} else if (credentialsSupplierOptional.isPresent()) {
6262
supplier = credentialsSupplierOptional.get();
6363
} else if (configurationSupplierOptional.isPresent()) {
6464
supplier = configurationSupplierOptional.get();
65+
} else {
66+
supplier = fixedProfileFile(ProfileFile.empty());
6567
}
6668

6769
return supplier;

core/profiles/src/test/java/software/amazon/awssdk/profiles/ProfileFileSupplierTest.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.junit.jupiter.api.Test;
4949
import org.junit.jupiter.api.condition.EnabledForJreRange;
5050
import org.junit.jupiter.api.condition.JRE;
51+
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;
5152
import software.amazon.awssdk.utils.Pair;
5253
import software.amazon.awssdk.utils.StringInputStream;
5354
import software.amazon.awssdk.testutils.LogCaptor;
@@ -581,6 +582,15 @@ public void checkPermission(Permission perm) {
581582
}
582583
}
583584

585+
@Test
586+
public void defaultSupplier_noCredentialsFiles_returnsEmptyProvider() {
587+
EnvironmentVariableHelper.run(environmentVariableHelper -> {
588+
environmentVariableHelper.set(ProfileFileSystemSetting.AWS_SHARED_CREDENTIALS_FILE, "no-such-file");
589+
environmentVariableHelper.set(ProfileFileSystemSetting.AWS_CONFIG_FILE, "no-such-file");
590+
ProfileFileSupplier supplier = ProfileFileSupplier.defaultSupplier();
591+
assertThat(supplier.get().profiles()).isEmpty();
592+
});
593+
}
584594

585595
private Path writeTestFile(String contents, Path path) {
586596
try {

core/profiles/src/test/java/software/amazon/awssdk/profiles/ProfileFileTest.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -570,6 +570,14 @@ public void returnsEmptyMap_when_AwsFilesDoNotExist() {
570570
assertThat(missingProfile.profiles()).isInstanceOf(Map.class);
571571
}
572572

573+
@Test
574+
public void builderValidatesContentRequired() {
575+
assertThatThrownBy(() -> ProfileFile.builder().type(ProfileFile.Type.CONFIGURATION).build())
576+
.isInstanceOf(IllegalArgumentException.class)
577+
.hasMessageContaining("content or contentLocation must be set.");
578+
579+
}
580+
573581
private ProfileFile configFile(String configFile) {
574582
return ProfileFile.builder()
575583
.content(configFile)

0 commit comments

Comments
 (0)