Skip to content

aws credentials cleanup #568

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 1 commit into from
Jun 22, 2018
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
5 changes: 5 additions & 0 deletions .changes/next-release/feature-AWSSDKforJavav2-758c891.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"category": "AWS SDK for Java v2",
"type": "feature",
"description": "- Updated `AwsCredentials` to interface implemented by `AwsBasicCredentials` and `AwsSessionCredentials` - Renamed `AwsCredentialsProvider.getCredentials()` to `AwsCredentialsProvider.resolveCredentials()`."
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public void testSessionCredentials() throws Exception {
mockServer.setAvailableSecurityCredentials("aws-dr-tools-test");

InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create();
AwsSessionCredentials credentials = (AwsSessionCredentials) credentialsProvider.getCredentials();
AwsSessionCredentials credentials = (AwsSessionCredentials) credentialsProvider.resolveCredentials();

assertEquals("ACCESS_KEY_ID", credentials.accessKeyId());
assertEquals("SECRET_ACCESS_KEY", credentials.secretAccessKey());
Expand All @@ -72,7 +72,7 @@ public void testSessionCredentials_MultipleInstanceProfiles() throws Exception {

AwsSessionCredentials credentials;
try (InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create()) {
credentials = (AwsSessionCredentials) credentialsProvider.getCredentials();
credentials = (AwsSessionCredentials) credentialsProvider.resolveCredentials();
}

assertEquals("ACCESS_KEY_ID", credentials.accessKeyId());
Expand All @@ -92,7 +92,7 @@ public void testNoInstanceProfiles() throws Exception {
try (InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create()) {

try {
credentialsProvider.getCredentials();
credentialsProvider.resolveCredentials();
fail("Expected an SdkClientException, but wasn't thrown");
} catch (SdkClientException ace) {
assertNotNull(ace.getMessage());
Expand All @@ -107,7 +107,7 @@ public void ec2MetadataDisabled_shouldReturnNull() {

try (InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create()) {
System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property(), "true");
credentialsProvider.getCredentials();
credentialsProvider.resolveCredentials();
} finally {
System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public static AnonymousCredentialsProvider create() {
}

@Override
public AwsCredentials getCredentials() {
return AwsCredentials.ANONYMOUS_CREDENTIALS;
public AwsCredentials resolveCredentials() {
return AwsBasicCredentials.ANONYMOUS_CREDENTIALS;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
/*
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License").
* You may not use this file except in compliance with the License.
* A copy of the License is located at
*
* http://aws.amazon.com/apache2.0
*
* or in the "license" file accompanying this file. This file is distributed
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
* express or implied. See the License for the specific language governing
* permissions and limitations under the License.
*/

package software.amazon.awssdk.auth.credentials;

import static software.amazon.awssdk.utils.StringUtils.trimToNull;

import java.util.Objects;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

/**
* Provides access to the AWS credentials used for accessing AWS services: AWS access key ID and secret access key. These
* credentials are used to securely sign requests to AWS services.
*
* <p>For more details on AWS access keys, see:
* <a href="http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys">
* http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
*
* @see AwsCredentialsProvider
*/
@Immutable
@SdkPublicApi
public final class AwsBasicCredentials implements AwsCredentials {
/**
* A set of AWS credentials without an access key or secret access key, indicating that anonymous access should be used.
*
* This should be accessed via {@link AnonymousCredentialsProvider#resolveCredentials()}.
*/
@SdkInternalApi
static final AwsBasicCredentials ANONYMOUS_CREDENTIALS = new AwsBasicCredentials(null, null, false);

private final String accessKeyId;
private final String secretAccessKey;

/**
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
*
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
*/
protected AwsBasicCredentials(String accessKeyId, String secretAccessKey) {
this(accessKeyId, secretAccessKey, true);
}

private AwsBasicCredentials(String accessKeyId, String secretAccessKey, boolean validateCredentials) {
this.accessKeyId = trimToNull(accessKeyId);
this.secretAccessKey = trimToNull(secretAccessKey);

if (validateCredentials) {
Validate.notNull(this.accessKeyId, "Access key ID cannot be blank.");
Validate.notNull(this.secretAccessKey, "Secret access key cannot be blank.");
}
}

/**
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
*
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
* */
public static AwsBasicCredentials create(String accessKeyId, String secretAccessKey) {
return new AwsBasicCredentials(accessKeyId, secretAccessKey);
}

/**
* Retrieve the AWS access key, used to identify the user interacting with AWS.
*/
public String accessKeyId() {
return accessKeyId;
}

/**
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
*/
public String secretAccessKey() {
return secretAccessKey;
}

@Override
public String toString() {
return ToString.builder("AwsCredentials")
.add("accessKeyId", accessKeyId)
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final AwsBasicCredentials that = (AwsBasicCredentials) o;
return Objects.equals(accessKeyId, that.accessKeyId) &&
Objects.equals(secretAccessKey, that.secretAccessKey);
}

@Override
public int hashCode() {
int hashCode = 1;
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
return hashCode;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,105 +15,28 @@

package software.amazon.awssdk.auth.credentials;

import static software.amazon.awssdk.utils.StringUtils.trimToNull;

import java.util.Objects;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkInternalApi;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

/**
* Provides access to the AWS credentials used for accessing AWS services: AWS access key ID and secret access key. These
* credentials are used to securely sign requests to AWS services.
*
* <p>For more details on AWS access keys, see:
* <a href="http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys">
* http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
* http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
*
* @see AwsCredentialsProvider
*/
@Immutable
@SdkPublicApi
public class AwsCredentials {
/**
* A set of AWS credentials without an access key or secret access key, indicating that anonymous access should be used.
*
* This should be accessed via {@link AnonymousCredentialsProvider#getCredentials()}.
*/
@SdkInternalApi
static final AwsCredentials ANONYMOUS_CREDENTIALS = new AwsCredentials(null, null, false);

private final String accessKeyId;
private final String secretAccessKey;

/**
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
*
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
*/
protected AwsCredentials(String accessKeyId, String secretAccessKey) {
this(accessKeyId, secretAccessKey, true);
}

private AwsCredentials(String accessKeyId, String secretAccessKey, boolean validateCredentials) {
this.accessKeyId = trimToNull(accessKeyId);
this.secretAccessKey = trimToNull(secretAccessKey);

if (validateCredentials) {
Validate.notNull(this.accessKeyId, "Access key ID cannot be blank.");
Validate.notNull(this.secretAccessKey, "Secret access key cannot be blank.");
}
}

/**
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
*
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
* */
public static AwsCredentials create(String accessKeyId, String secretAccessKey) {
return new AwsCredentials(accessKeyId, secretAccessKey);
}
public interface AwsCredentials {

/**
* Retrieve the AWS access key, used to identify the user interacting with AWS.
*/
public final String accessKeyId() {
return accessKeyId;
}
String accessKeyId();

/**
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
*/
public final String secretAccessKey() {
return secretAccessKey;
}

@Override
public String toString() {
return ToString.builder("AwsCredentials")
.add("accessKeyId", accessKeyId)
.build();
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
final AwsCredentials that = (AwsCredentials) o;
return Objects.equals(accessKeyId, that.accessKeyId) &&
Objects.equals(secretAccessKey, that.secretAccessKey);
}

@Override
public int hashCode() {
return Objects.hash(accessKeyId, secretAccessKey);
}
String secretAccessKey();
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,5 +38,5 @@ public interface AwsCredentialsProvider {
*
* @return AwsCredentials which the caller can use to authorize an AWS request.
*/
AwsCredentials getCredentials();
AwsCredentials resolveCredentials();
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,15 +81,15 @@ public static AwsCredentialsProviderChain of(AwsCredentialsProvider... awsCreden
}

@Override
public AwsCredentials getCredentials() {
public AwsCredentials resolveCredentials() {
if (reuseLastProviderEnabled && lastUsedProvider != null) {
return lastUsedProvider.getCredentials();
return lastUsedProvider.resolveCredentials();
}

List<String> exceptionMessages = null;
for (AwsCredentialsProvider provider : credentialsProviders) {
try {
AwsCredentials credentials = provider.getCredentials();
AwsCredentials credentials = provider.resolveCredentials();

log.debug("Loading credentials from {}", provider.toString());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,22 +16,28 @@
package software.amazon.awssdk.auth.credentials;

import java.util.Objects;
import software.amazon.awssdk.annotations.Immutable;
import software.amazon.awssdk.annotations.SdkPublicApi;
import software.amazon.awssdk.utils.ToString;
import software.amazon.awssdk.utils.Validate;

/**
* A special type of {@link AwsCredentials} that also provides a session token to be used in service authentication. Session
* A special type of {@link AwsCredentials} that provides a session token to be used in service authentication. Session
* tokens are typically provided by a token broker service, like AWS Security Token Service, and provide temporary access to an
* AWS service.
*/
@Immutable
@SdkPublicApi
public final class AwsSessionCredentials extends AwsCredentials {
public final class AwsSessionCredentials implements AwsCredentials {

private final String accessKeyId;
private final String secretAccessKey;
private final String sessionToken;

private AwsSessionCredentials(String accessKey, String secretKey, String sessionToken) {
super(accessKey, secretKey);
this.sessionToken = Validate.notNull(sessionToken, "Session token cannot be null.");
this.accessKeyId = Validate.paramNotNull(accessKey, "accessKey");
this.secretAccessKey = Validate.paramNotNull(secretKey, "secretKey");
this.sessionToken = Validate.paramNotNull(sessionToken, "sessionToken");
}

/**
Expand All @@ -40,12 +46,28 @@ private AwsSessionCredentials(String accessKey, String secretKey, String session
* @param accessKey The AWS access key, used to identify the user interacting with AWS.
* @param secretKey The AWS secret access key, used to authenticate the user interacting with AWS.
* @param sessionToken The AWS session token, retrieved from an AWS token service, used for authenticating that this user has
* received temporary permission to access some resource.
* received temporary permission to access some resource.
*/
public static AwsSessionCredentials create(String accessKey, String secretKey, String sessionToken) {
return new AwsSessionCredentials(accessKey, secretKey, sessionToken);
}

/**
* Retrieve the AWS access key, used to identify the user interacting with AWS.
*/
@Override
public String accessKeyId() {
return accessKeyId;
}

/**
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
*/
@Override
public String secretAccessKey() {
return secretAccessKey;
}

/**
* Retrieve the AWS session token. This token is retrieved from an AWS token service, and is used for authenticating that this
* user has received temporary permission to access some resource.
Expand Down Expand Up @@ -73,11 +95,17 @@ public boolean equals(Object o) {
return false;
}
final AwsSessionCredentials that = (AwsSessionCredentials) o;
return Objects.equals(sessionToken, that.sessionToken);
return Objects.equals(accessKeyId, that.accessKeyId) &&
Objects.equals(secretAccessKey, that.secretAccessKey) &&
Objects.equals(sessionToken, that.sessionToken);
}

@Override
public int hashCode() {
return Objects.hash(super.hashCode(), sessionToken);
int hashCode = 1;
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
hashCode = 31 * hashCode + Objects.hashCode(sessionToken());
return hashCode;
}
}
Loading