Skip to content

Commit

Permalink
Look for AWS_ACCESS_KEY and AWS_SECRET_ACCESS_KEY as well.
Browse files Browse the repository at this point in the history
Fixes aws#50.
  • Loading branch information
Stephen Haberman committed Sep 20, 2013
1 parent 6d88d2e commit 4d4c0a4
Showing 1 changed file with 22 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,29 +18,42 @@

/**
* {@link AWSCredentialsProvider} implementation that provides credentials
* by looking at the <code>AWS_ACCESS_KEY_ID</code> and
* <code>AWS_SECRET_KEY</code> environment variables.
* by looking at the: <code>AWS_ACCESS_KEY_ID</code> (or <code>AWS_ACCESS_KEY</code>) and
* <code>AWS_SECRET_ACCESS_KEY</code> (or <code>AWS_SECRET_KEY</code>) environment variables.
*/
public class EnvironmentVariableCredentialsProvider implements AWSCredentialsProvider {

/** Environment variable name for the AWS access key ID */
private static final String ACCESS_KEY_ENV_VAR = "AWS_ACCESS_KEY_ID";

/** Alternate environment variable name for the AWS access key ID */
private static final String ACCESS_KEY_ENV_VAR_2 = "AWS_ACCESS_KEY";

/** Environment variable name for the AWS secret key */
private static final String SECRET_KEY_ENV_VAR = "AWS_SECRET_KEY";
private static final String SECRET_KEY_ENV_VAR = "AWS_SECRET_ACCESS_KEY";

/** Alternate environment variable name for the AWS secret key */
private static final String SECRET_KEY_ENV_VAR_2 = "AWS_SECRET_KEY";

public AWSCredentials getCredentials() {
if (System.getenv(ACCESS_KEY_ENV_VAR) != null &&
System.getenv(SECRET_KEY_ENV_VAR) != null) {
String accessKey = System.getenv(ACCESS_KEY_ENV_VAR);
if (accessKey == null) {
accessKey = System.getenv(ACCESS_KEY_ENV_VAR_2);
}

String secretKey = System.getenv(SECRET_KEY_ENV_VAR);
if (secretKey == null) {
secretKey = System.getenv(SECRET_KEY_ENV_VAR_2);
}

return new BasicAWSCredentials(
System.getenv(ACCESS_KEY_ENV_VAR),
System.getenv(SECRET_KEY_ENV_VAR));
if (accessKey != null && secretKey != null) {
return new BasicAWSCredentials(accessKey, secretKey);
}

throw new AmazonClientException(
"Unable to load AWS credentials from environment variables " +
"(" + ACCESS_KEY_ENV_VAR + " and " + SECRET_KEY_ENV_VAR + ")");
"(" + ACCESS_KEY_ENV_VAR + " (or " + ACCESS_KEY_ENV_VAR_2 + ") and " +
SECRET_KEY_ENV_VAR + " (or " + SECRET_KEY_ENV_VAR_2 + "))");
}

public void refresh() {}
Expand Down

0 comments on commit 4d4c0a4

Please sign in to comment.