Skip to content

Commit

Permalink
Adding helper for profile credential provider that allows passing pro…
Browse files Browse the repository at this point in the history
…file name to create.

Seemed like a common enough use-case to warrant a helper method.
  • Loading branch information
kiiadi committed Dec 12, 2017
1 parent a93ff62 commit a5ca461
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changes/next-release/feature-AWSSDKforJava-f236f16.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "AWS SDK for Java",
"type": "feature",
"description": "Added `ProfileCredentialsProvider.create(\"profile-name\")` helper to `ProfileCredentialsProvider` to account for common use-case where only profile name is provided."
}
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,16 @@ private ProfileCredentialsProvider(Builder builder) {
* defining a custom {@link ProfileCredentialsProvider}.
*/
public static ProfileCredentialsProvider create() {
return new ProfileCredentialsProvider(builder());
return builder().build();
}

/**
* Create a {@link ProfileCredentialsProvider} using the given profileName and default configuration file. Use
* {@link #builder()} for defining a custom {@link ProfileCredentialsProvider}.
* @param profileName the name of the profile from the default configuraiton file to use
*/
public static ProfileCredentialsProvider create(String profileName) {
return builder().profileName(profileName).build();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@

package software.amazon.awssdk.core.auth;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static software.amazon.awssdk.core.AwsSystemSetting.AWS_CONFIG_FILE;

import java.io.File;
import java.lang.reflect.Field;
import java.util.Map;
Expand All @@ -25,6 +28,7 @@
import software.amazon.awssdk.core.SdkClientException;
import software.amazon.awssdk.core.auth.profile.ProfileResourceLoader;
import software.amazon.awssdk.core.auth.profile.ProfilesConfigFile;
import software.amazon.awssdk.testutils.EnvironmentVariableHelper;

public class ProfileCredentialsProviderTest {
private static File profileLocation = null;
Expand Down Expand Up @@ -228,6 +232,16 @@ public void testAssumeRoleWithSourceAfterRole() throws Exception {
Assert.assertEquals("sessionSecretKey", credentials.secretAccessKey());
}

@Test
public void testProfileNameSimpleMethod() throws Exception {
EnvironmentVariableHelper.run(helper -> {
helper.set(AWS_CONFIG_FILE, profileLocation.getAbsolutePath());
assertThatThrownBy(() -> ProfileCredentialsProvider.create("non-existent-profile").getCredentials())
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("non-existent-profile");
});
}

@SuppressWarnings("unchecked")
private Map<String, String> getMutableSystemEnvironment() throws Exception {
Map<String, String> immutableEnv = System.getenv();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.security.PrivilegedExceptionAction;
import java.util.HashMap;
import java.util.Map;
import java.util.function.Consumer;
import org.junit.rules.ExternalResource;
import software.amazon.awssdk.utils.SystemSetting;

Expand Down Expand Up @@ -90,4 +91,30 @@ private PrivilegedExceptionAction<Void> setAccessible(Field f) {
return null;
};
}

/**
* Static run method that allows for "single-use" environment variable modification.
*
* Example use:
* <pre>
* <code>
* EnvironmentVariableHelper.run(helper -> {
* helper.set("variable", "value");
* //run some test that uses "variable"
* });
* </code>
* </pre>
*
* Will call {@link #reset} at the end of the block (even if the block exits exceptionally).
*
* @param helperConsumer a code block to run that gets an {@link EnvironmentVariableHelper} as an argument
*/
public static void run(Consumer<EnvironmentVariableHelper> helperConsumer) {
EnvironmentVariableHelper helper = new EnvironmentVariableHelper();
try {
helperConsumer.accept(helper);
} finally {
helper.reset();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,32 +16,56 @@
package software.amazon.awssdk.testutils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.mockito.Mockito.*;

import java.util.HashMap;
import java.util.Map;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.RuleChain;
import org.junit.experimental.runners.Enclosed;
import org.junit.runner.RunWith;

@RunWith(Enclosed.class)
public class EnvironmentVariableHelperTest {

private static Map<String, String> environmentVariables = new HashMap<>(System.getenv());

@Rule
public EnvironmentVariableHelper helper = new EnvironmentVariableHelper();

@AfterClass
public static void ensureCleanup() {
assertThat(System.getenv()).hasSameSizeAs(environmentVariables).containsAllEntriesOf(environmentVariables);
}

@Test
public void helperAsRuleIsResetAfterEachUse() {
helper.set("yo yo yo", "blah");
public static class Normal {
@Test
public void testCanUseStaticRun() {
assertThat(System.getenv("hello")).isEqualTo(null);
EnvironmentVariableHelper.run(helper -> {
helper.set("hello", "world");
assertThat(System.getenv("hello")).isEqualTo("world");
});
assertThat(System.getenv("hello")).isEqualTo(null);
}

@Test
public void testCanManuallyReset() {
EnvironmentVariableHelper helper = new EnvironmentVariableHelper();
assertThat(System.getenv("hello")).isEqualTo(null);
helper.set("hello", "world");
assertThat(System.getenv("hello")).isEqualTo("world");
helper.reset();
assertThat(System.getenv("hello")).isEqualTo(null);
}
}

public static class AsRule {

@Rule
public EnvironmentVariableHelper helper = new EnvironmentVariableHelper();

@Test
public void helperAsRuleIsResetAfterEachUse() {
helper.set("yo yo yo", "blah");
}

}
}

0 comments on commit a5ca461

Please sign in to comment.