Skip to content

Commit d41c517

Browse files
authored
Add create() for new AwsCredentials Identity types (#3884)
* Add create() for new AwsCredentials Identity types * Add `@see AwsSessionCredentialsIdentity` to AwsCredentialsIdentity Will make it easier to discover `AwsSessionCredentialsIdentity.create` if looking at `AwsCredentialsIdentity` and wondering how to create a session credentials.
1 parent 365bab1 commit d41c517

File tree

4 files changed

+126
-39
lines changed

4 files changed

+126
-39
lines changed

core/auth/src/test/java/software/amazon/awssdk/auth/credentials/CredentialUtilsTest.java

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public void isAnonymous_AwsCredentialsIdentity_true() {
4141

4242
@Test
4343
public void isAnonymous_AwsCredentialsIdentity_false() {
44-
assertThat(CredentialUtils.isAnonymous((AwsCredentialsIdentity) AwsBasicCredentials.create("akid", "skid"))).isFalse();
44+
assertThat(CredentialUtils.isAnonymous(AwsCredentialsIdentity.create("akid", "skid"))).isFalse();
4545
}
4646

4747
@Test
@@ -59,22 +59,8 @@ public void toCredentials_AwsSessionCredentials_doesNotCreateNewObject() {
5959

6060
@Test
6161
public void toCredentials_AwsSessionCredentialsIdentity_returnsAwsSessionCredentials() {
62-
AwsCredentials awsCredentials = CredentialUtils.toCredentials(new AwsSessionCredentialsIdentity() {
63-
@Override
64-
public String accessKeyId() {
65-
return "akid";
66-
}
67-
68-
@Override
69-
public String secretAccessKey() {
70-
return "skid";
71-
}
72-
73-
@Override
74-
public String sessionToken() {
75-
return "session";
76-
}
77-
});
62+
AwsCredentials awsCredentials = CredentialUtils.toCredentials(AwsSessionCredentialsIdentity.create(
63+
"akid", "skid", "session"));
7864

7965
assertThat(awsCredentials).isInstanceOf(AwsSessionCredentials.class);
8066
AwsSessionCredentials awsSessionCredentials = (AwsSessionCredentials) awsCredentials;
@@ -92,17 +78,7 @@ public void toCredentials_AwsCredentials_returnsAsIs() {
9278

9379
@Test
9480
public void toCredentials_AwsCredentialsIdentity_returnsAwsCredentials() {
95-
AwsCredentials awsCredentials = CredentialUtils.toCredentials(new AwsCredentialsIdentity() {
96-
@Override
97-
public String accessKeyId() {
98-
return "akid";
99-
}
100-
101-
@Override
102-
public String secretAccessKey() {
103-
return "skid";
104-
}
105-
});
81+
AwsCredentials awsCredentials = CredentialUtils.toCredentials(AwsCredentialsIdentity.create("akid", "skid"));
10682

10783
assertThat(awsCredentials.accessKeyId()).isEqualTo("akid");
10884
assertThat(awsCredentials.secretAccessKey()).isEqualTo("skid");

core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/AwsCredentialsIdentity.java

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
package software.amazon.awssdk.identity.spi;
1717

18+
import java.util.Objects;
1819
import software.amazon.awssdk.annotations.SdkPublicApi;
1920
import software.amazon.awssdk.annotations.ThreadSafe;
21+
import software.amazon.awssdk.utils.ToString;
22+
import software.amazon.awssdk.utils.Validate;
2023

2124
/**
2225
* Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These
@@ -25,6 +28,8 @@
2528
* <p>For more details on AWS access keys, see:
2629
* <a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys">
2730
* https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys</a></p>
31+
*
32+
* @see AwsSessionCredentialsIdentity
2833
*/
2934
@SdkPublicApi
3035
@ThreadSafe
@@ -39,4 +44,55 @@ public interface AwsCredentialsIdentity extends Identity {
3944
* Retrieve the AWS secret access key, used to authenticate the user interacting with services.
4045
*/
4146
String secretAccessKey();
47+
48+
/**
49+
* Constructs a new credentials object, with the specified AWS access key and AWS secret key.
50+
*
51+
* @param accessKeyId The AWS access key, used to identify the user interacting with services.
52+
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with services.
53+
* */
54+
static AwsCredentialsIdentity create(String accessKeyId, String secretAccessKey) {
55+
Validate.paramNotNull(accessKeyId, "accessKeyId");
56+
Validate.paramNotNull(secretAccessKey, "secretAccessKey");
57+
58+
return new AwsCredentialsIdentity() {
59+
@Override
60+
public String accessKeyId() {
61+
return accessKeyId;
62+
}
63+
64+
@Override
65+
public String secretAccessKey() {
66+
return secretAccessKey;
67+
}
68+
69+
@Override
70+
public String toString() {
71+
return ToString.builder("AwsCredentialsIdentity")
72+
.add("accessKeyId", accessKeyId)
73+
.build();
74+
}
75+
76+
@Override
77+
public boolean equals(Object o) {
78+
if (this == o) {
79+
return true;
80+
}
81+
if (o == null || getClass() != o.getClass()) {
82+
return false;
83+
}
84+
AwsCredentialsIdentity that = (AwsCredentialsIdentity) o;
85+
return Objects.equals(accessKeyId, that.accessKeyId()) &&
86+
Objects.equals(secretAccessKey, that.secretAccessKey());
87+
}
88+
89+
@Override
90+
public int hashCode() {
91+
int hashCode = 1;
92+
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
93+
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
94+
return hashCode;
95+
}
96+
};
97+
}
4298
}

core/identity-spi/src/main/java/software/amazon/awssdk/identity/spi/AwsSessionCredentialsIdentity.java

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515

1616
package software.amazon.awssdk.identity.spi;
1717

18+
import java.util.Objects;
1819
import software.amazon.awssdk.annotations.SdkPublicApi;
1920
import software.amazon.awssdk.annotations.ThreadSafe;
21+
import software.amazon.awssdk.utils.ToString;
22+
import software.amazon.awssdk.utils.Validate;
2023

2124
/**
2225
* A special type of {@link AwsCredentialsIdentity} that provides a session token to be used in service authentication. Session
@@ -32,4 +35,66 @@ public interface AwsSessionCredentialsIdentity extends AwsCredentialsIdentity {
3235
* user has received temporary permission to access some resource.
3336
*/
3437
String sessionToken();
38+
39+
/**
40+
* Constructs a new session credentials object, with the specified AWS access key, AWS secret key and AWS session token.
41+
*
42+
* @param accessKeyId The AWS access key, used to identify the user interacting with services.
43+
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with services.
44+
* @param sessionToken The AWS session token, retrieved from an AWS token service, used for authenticating that this user has
45+
* received temporary permission to access some resource.
46+
*/
47+
static AwsSessionCredentialsIdentity create(String accessKeyId, String secretAccessKey, String sessionToken) {
48+
Validate.paramNotNull(accessKeyId, "accessKeyId");
49+
Validate.paramNotNull(secretAccessKey, "secretAccessKey");
50+
Validate.paramNotNull(sessionToken, "sessionToken");
51+
52+
return new AwsSessionCredentialsIdentity() {
53+
@Override
54+
public String accessKeyId() {
55+
return accessKeyId;
56+
}
57+
58+
@Override
59+
public String secretAccessKey() {
60+
return secretAccessKey;
61+
}
62+
63+
@Override
64+
public String sessionToken() {
65+
return sessionToken;
66+
}
67+
68+
@Override
69+
public String toString() {
70+
return ToString.builder("AwsSessionCredentialsIdentity")
71+
.add("accessKeyId", accessKeyId())
72+
.build();
73+
}
74+
75+
@Override
76+
public boolean equals(Object o) {
77+
if (this == o) {
78+
return true;
79+
}
80+
if (o == null || getClass() != o.getClass()) {
81+
return false;
82+
}
83+
84+
AwsSessionCredentialsIdentity that = (AwsSessionCredentialsIdentity) o;
85+
return Objects.equals(accessKeyId, that.accessKeyId()) &&
86+
Objects.equals(secretAccessKey, that.secretAccessKey()) &&
87+
Objects.equals(sessionToken, that.sessionToken());
88+
}
89+
90+
@Override
91+
public int hashCode() {
92+
int hashCode = 1;
93+
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
94+
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
95+
hashCode = 31 * hashCode + Objects.hashCode(sessionToken());
96+
return hashCode;
97+
}
98+
};
99+
}
35100
}

services/s3/src/test/java/software/amazon/awssdk/services/s3/internal/crt/CrtCredentialProviderAdapterTest.java

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -69,17 +69,7 @@ void crtCredentials_withoutSession_shouldConvert() {
6969
@Test
7070
void crtCredentials_provideAwsCredentials_shouldInvokeResolveAndClose() {
7171
IdentityProvider<? extends AwsCredentialsIdentity> awsCredentialsProvider = Mockito.mock(HttpCredentialsProvider.class);
72-
AwsCredentialsIdentity credentials = new AwsCredentialsIdentity() {
73-
@Override
74-
public String accessKeyId() {
75-
return "foo";
76-
}
77-
78-
@Override
79-
public String secretAccessKey() {
80-
return "bar";
81-
}
82-
};
72+
AwsCredentialsIdentity credentials = AwsCredentialsIdentity.create("foo", "bar");
8373
when(awsCredentialsProvider.resolveIdentity()).thenAnswer(invocation -> CompletableFuture.completedFuture(credentials));
8474

8575
CrtCredentialsProviderAdapter adapter = new CrtCredentialsProviderAdapter(awsCredentialsProvider);

0 commit comments

Comments
 (0)