Skip to content

Commit e93f48d

Browse files
committed
Updated AwsCrendentials to interface implemented by AwsBasicCredentials and AwsSessionCredentials. Rename getCredentials to resolveCredentials
1 parent 0ac5f8e commit e93f48d

File tree

67 files changed

+319
-224
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+319
-224
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"category": "AWS SDK for Java v2",
3+
"type": "feature",
4+
"description": "- Updated `AwsCredentials` to interface implemented by `AwsBasicCredentials` and `AwsSessionCredentials` - Renamed `AwsCredentialsProvider.getCredentials()` to `AwsCredentialsProvider.resolveCredentials()`."
5+
}

auth/src/it/java/software/amazon/awssdk/auth/credentials/InstanceProfileCredentialsProviderIntegrationTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testSessionCredentials() throws Exception {
5454
mockServer.setAvailableSecurityCredentials("aws-dr-tools-test");
5555

5656
InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create();
57-
AwsSessionCredentials credentials = (AwsSessionCredentials) credentialsProvider.getCredentials();
57+
AwsSessionCredentials credentials = (AwsSessionCredentials) credentialsProvider.resolveCredentials();
5858

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

7373
AwsSessionCredentials credentials;
7474
try (InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create()) {
75-
credentials = (AwsSessionCredentials) credentialsProvider.getCredentials();
75+
credentials = (AwsSessionCredentials) credentialsProvider.resolveCredentials();
7676
}
7777

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

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

108108
try (InstanceProfileCredentialsProvider credentialsProvider = InstanceProfileCredentialsProvider.create()) {
109109
System.setProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property(), "true");
110-
credentialsProvider.getCredentials();
110+
credentialsProvider.resolveCredentials();
111111
} finally {
112112
System.clearProperty(SdkSystemSetting.AWS_EC2_METADATA_DISABLED.property());
113113
}

auth/src/main/java/software/amazon/awssdk/auth/credentials/AnonymousCredentialsProvider.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ public static AnonymousCredentialsProvider create() {
3333
}
3434

3535
@Override
36-
public AwsCredentials getCredentials() {
37-
return AwsCredentials.ANONYMOUS_CREDENTIALS;
36+
public AwsCredentials resolveCredentials() {
37+
return AwsBasicCredentials.ANONYMOUS_CREDENTIALS;
3838
}
3939

4040
@Override
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
/*
2+
* Copyright 2010-2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License").
5+
* You may not use this file except in compliance with the License.
6+
* A copy of the License is located at
7+
*
8+
* http://aws.amazon.com/apache2.0
9+
*
10+
* or in the "license" file accompanying this file. This file is distributed
11+
* on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
12+
* express or implied. See the License for the specific language governing
13+
* permissions and limitations under the License.
14+
*/
15+
16+
package software.amazon.awssdk.auth.credentials;
17+
18+
import static software.amazon.awssdk.utils.StringUtils.trimToNull;
19+
20+
import java.util.Objects;
21+
import software.amazon.awssdk.annotations.Immutable;
22+
import software.amazon.awssdk.annotations.SdkInternalApi;
23+
import software.amazon.awssdk.annotations.SdkPublicApi;
24+
import software.amazon.awssdk.utils.ToString;
25+
import software.amazon.awssdk.utils.Validate;
26+
27+
/**
28+
* Provides access to the AWS credentials used for accessing AWS services: AWS access key ID and secret access key. These
29+
* credentials are used to securely sign requests to AWS services.
30+
*
31+
* <p>For more details on AWS access keys, see:
32+
* <a href="http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys">
33+
* http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
34+
*
35+
* @see AwsCredentialsProvider
36+
*/
37+
@Immutable
38+
@SdkPublicApi
39+
public final class AwsBasicCredentials implements AwsCredentials {
40+
/**
41+
* A set of AWS credentials without an access key or secret access key, indicating that anonymous access should be used.
42+
*
43+
* This should be accessed via {@link AnonymousCredentialsProvider#resolveCredentials()}.
44+
*/
45+
@SdkInternalApi
46+
static final AwsBasicCredentials ANONYMOUS_CREDENTIALS = new AwsBasicCredentials(null, null, false);
47+
48+
private final String accessKeyId;
49+
private final String secretAccessKey;
50+
51+
/**
52+
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
53+
*
54+
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
55+
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
56+
*/
57+
protected AwsBasicCredentials(String accessKeyId, String secretAccessKey) {
58+
this(accessKeyId, secretAccessKey, true);
59+
}
60+
61+
private AwsBasicCredentials(String accessKeyId, String secretAccessKey, boolean validateCredentials) {
62+
this.accessKeyId = trimToNull(accessKeyId);
63+
this.secretAccessKey = trimToNull(secretAccessKey);
64+
65+
if (validateCredentials) {
66+
Validate.notNull(this.accessKeyId, "Access key ID cannot be blank.");
67+
Validate.notNull(this.secretAccessKey, "Secret access key cannot be blank.");
68+
}
69+
}
70+
71+
/**
72+
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
73+
*
74+
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
75+
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
76+
* */
77+
public static AwsBasicCredentials create(String accessKeyId, String secretAccessKey) {
78+
return new AwsBasicCredentials(accessKeyId, secretAccessKey);
79+
}
80+
81+
/**
82+
* Retrieve the AWS access key, used to identify the user interacting with AWS.
83+
*/
84+
public String accessKeyId() {
85+
return accessKeyId;
86+
}
87+
88+
/**
89+
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
90+
*/
91+
public String secretAccessKey() {
92+
return secretAccessKey;
93+
}
94+
95+
@Override
96+
public String toString() {
97+
return ToString.builder("AwsCredentials")
98+
.add("accessKeyId", accessKeyId)
99+
.build();
100+
}
101+
102+
@Override
103+
public boolean equals(Object o) {
104+
if (this == o) {
105+
return true;
106+
}
107+
if (o == null || getClass() != o.getClass()) {
108+
return false;
109+
}
110+
final AwsBasicCredentials that = (AwsBasicCredentials) o;
111+
return Objects.equals(accessKeyId, that.accessKeyId) &&
112+
Objects.equals(secretAccessKey, that.secretAccessKey);
113+
}
114+
115+
@Override
116+
public int hashCode() {
117+
int hashCode = 1;
118+
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
119+
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
120+
return hashCode;
121+
}
122+
}

auth/src/main/java/software/amazon/awssdk/auth/credentials/AwsCredentials.java

Lines changed: 4 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -15,105 +15,28 @@
1515

1616
package software.amazon.awssdk.auth.credentials;
1717

18-
import static software.amazon.awssdk.utils.StringUtils.trimToNull;
19-
20-
import java.util.Objects;
21-
import software.amazon.awssdk.annotations.Immutable;
22-
import software.amazon.awssdk.annotations.SdkInternalApi;
2318
import software.amazon.awssdk.annotations.SdkPublicApi;
24-
import software.amazon.awssdk.utils.ToString;
25-
import software.amazon.awssdk.utils.Validate;
2619

2720
/**
2821
* Provides access to the AWS credentials used for accessing AWS services: AWS access key ID and secret access key. These
2922
* credentials are used to securely sign requests to AWS services.
3023
*
3124
* <p>For more details on AWS access keys, see:
3225
* <a href="http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys">
33-
* http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
26+
* http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
3427
*
3528
* @see AwsCredentialsProvider
3629
*/
37-
@Immutable
3830
@SdkPublicApi
39-
public class AwsCredentials {
40-
/**
41-
* A set of AWS credentials without an access key or secret access key, indicating that anonymous access should be used.
42-
*
43-
* This should be accessed via {@link AnonymousCredentialsProvider#getCredentials()}.
44-
*/
45-
@SdkInternalApi
46-
static final AwsCredentials ANONYMOUS_CREDENTIALS = new AwsCredentials(null, null, false);
47-
48-
private final String accessKeyId;
49-
private final String secretAccessKey;
50-
51-
/**
52-
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
53-
*
54-
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
55-
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
56-
*/
57-
protected AwsCredentials(String accessKeyId, String secretAccessKey) {
58-
this(accessKeyId, secretAccessKey, true);
59-
}
60-
61-
private AwsCredentials(String accessKeyId, String secretAccessKey, boolean validateCredentials) {
62-
this.accessKeyId = trimToNull(accessKeyId);
63-
this.secretAccessKey = trimToNull(secretAccessKey);
64-
65-
if (validateCredentials) {
66-
Validate.notNull(this.accessKeyId, "Access key ID cannot be blank.");
67-
Validate.notNull(this.secretAccessKey, "Secret access key cannot be blank.");
68-
}
69-
}
70-
71-
/**
72-
* Constructs a new credentials object, with the specified AWS access key, AWS secret key and AWS session token.
73-
*
74-
* @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
75-
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
76-
* */
77-
public static AwsCredentials create(String accessKeyId, String secretAccessKey) {
78-
return new AwsCredentials(accessKeyId, secretAccessKey);
79-
}
31+
public interface AwsCredentials {
8032

8133
/**
8234
* Retrieve the AWS access key, used to identify the user interacting with AWS.
8335
*/
84-
public final String accessKeyId() {
85-
return accessKeyId;
86-
}
36+
String accessKeyId();
8737

8838
/**
8939
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
9040
*/
91-
public final String secretAccessKey() {
92-
return secretAccessKey;
93-
}
94-
95-
@Override
96-
public String toString() {
97-
return ToString.builder("AwsCredentials")
98-
.add("accessKeyId", accessKeyId)
99-
.build();
100-
}
101-
102-
@Override
103-
public boolean equals(Object o) {
104-
if (this == o) {
105-
return true;
106-
}
107-
if (o == null || getClass() != o.getClass()) {
108-
return false;
109-
}
110-
final AwsCredentials that = (AwsCredentials) o;
111-
return Objects.equals(accessKeyId, that.accessKeyId) &&
112-
Objects.equals(secretAccessKey, that.secretAccessKey);
113-
}
114-
115-
@Override
116-
public int hashCode() {
117-
return Objects.hash(accessKeyId, secretAccessKey);
118-
}
41+
String secretAccessKey();
11942
}

auth/src/main/java/software/amazon/awssdk/auth/credentials/AwsCredentialsProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,5 @@ public interface AwsCredentialsProvider {
3838
*
3939
* @return AwsCredentials which the caller can use to authorize an AWS request.
4040
*/
41-
AwsCredentials getCredentials();
41+
AwsCredentials resolveCredentials();
4242
}

auth/src/main/java/software/amazon/awssdk/auth/credentials/AwsCredentialsProviderChain.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,15 @@ public static AwsCredentialsProviderChain of(AwsCredentialsProvider... awsCreden
8181
}
8282

8383
@Override
84-
public AwsCredentials getCredentials() {
84+
public AwsCredentials resolveCredentials() {
8585
if (reuseLastProviderEnabled && lastUsedProvider != null) {
86-
return lastUsedProvider.getCredentials();
86+
return lastUsedProvider.resolveCredentials();
8787
}
8888

8989
List<String> exceptionMessages = null;
9090
for (AwsCredentialsProvider provider : credentialsProviders) {
9191
try {
92-
AwsCredentials credentials = provider.getCredentials();
92+
AwsCredentials credentials = provider.resolveCredentials();
9393

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

auth/src/main/java/software/amazon/awssdk/auth/credentials/AwsSessionCredentials.java

Lines changed: 35 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,22 +16,28 @@
1616
package software.amazon.awssdk.auth.credentials;
1717

1818
import java.util.Objects;
19+
import software.amazon.awssdk.annotations.Immutable;
1920
import software.amazon.awssdk.annotations.SdkPublicApi;
2021
import software.amazon.awssdk.utils.ToString;
2122
import software.amazon.awssdk.utils.Validate;
2223

2324
/**
24-
* A special type of {@link AwsCredentials} that also provides a session token to be used in service authentication. Session
25+
* A special type of {@link AwsCredentials} that provides a session token to be used in service authentication. Session
2526
* tokens are typically provided by a token broker service, like AWS Security Token Service, and provide temporary access to an
2627
* AWS service.
2728
*/
29+
@Immutable
2830
@SdkPublicApi
29-
public final class AwsSessionCredentials extends AwsCredentials {
31+
public final class AwsSessionCredentials implements AwsCredentials {
32+
33+
private final String accessKeyId;
34+
private final String secretAccessKey;
3035
private final String sessionToken;
3136

3237
private AwsSessionCredentials(String accessKey, String secretKey, String sessionToken) {
33-
super(accessKey, secretKey);
34-
this.sessionToken = Validate.notNull(sessionToken, "Session token cannot be null.");
38+
this.accessKeyId = Validate.paramNotNull(accessKey, "accessKey");
39+
this.secretAccessKey = Validate.paramNotNull(secretKey, "secretKey");
40+
this.sessionToken = Validate.paramNotNull(sessionToken, "sessionToken");
3541
}
3642

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

55+
/**
56+
* Retrieve the AWS access key, used to identify the user interacting with AWS.
57+
*/
58+
@Override
59+
public String accessKeyId() {
60+
return accessKeyId;
61+
}
62+
63+
/**
64+
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
65+
*/
66+
@Override
67+
public String secretAccessKey() {
68+
return secretAccessKey;
69+
}
70+
4971
/**
5072
* Retrieve the AWS session token. This token is retrieved from an AWS token service, and is used for authenticating that this
5173
* user has received temporary permission to access some resource.
@@ -73,11 +95,17 @@ public boolean equals(Object o) {
7395
return false;
7496
}
7597
final AwsSessionCredentials that = (AwsSessionCredentials) o;
76-
return Objects.equals(sessionToken, that.sessionToken);
98+
return Objects.equals(accessKeyId, that.accessKeyId) &&
99+
Objects.equals(secretAccessKey, that.secretAccessKey) &&
100+
Objects.equals(sessionToken, that.sessionToken);
77101
}
78102

79103
@Override
80104
public int hashCode() {
81-
return Objects.hash(super.hashCode(), sessionToken);
105+
int hashCode = 1;
106+
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
107+
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
108+
hashCode = 31 * hashCode + Objects.hashCode(sessionToken());
109+
return hashCode;
82110
}
83111
}

0 commit comments

Comments
 (0)