Skip to content

Commit 15f92d3

Browse files
authored
Merge branch 'aws:master' into bmaizels/improved-entity-generics
2 parents 87a7001 + d1d1ee1 commit 15f92d3

File tree

12 files changed

+206
-90
lines changed

12 files changed

+206
-90
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"type": "feature",
3+
"category": "AWS STS",
4+
"contributor": "",
5+
"description": "Updates the core STS credential provider logic to return AwsSessionCredentials instead of an STS-specific class, and adds expirationTime to AwsSessionCredentials"
6+
}

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

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@
1515

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

18+
import java.time.Instant;
1819
import java.util.Objects;
20+
import java.util.Optional;
1921
import software.amazon.awssdk.annotations.Immutable;
2022
import software.amazon.awssdk.annotations.SdkPublicApi;
2123
import software.amazon.awssdk.utils.ToString;
@@ -34,10 +36,20 @@ public final class AwsSessionCredentials implements AwsCredentials {
3436
private final String secretAccessKey;
3537
private final String sessionToken;
3638

37-
private AwsSessionCredentials(String accessKey, String secretKey, String sessionToken) {
38-
this.accessKeyId = Validate.paramNotNull(accessKey, "accessKey");
39-
this.secretAccessKey = Validate.paramNotNull(secretKey, "secretKey");
40-
this.sessionToken = Validate.paramNotNull(sessionToken, "sessionToken");
39+
private final Instant expirationTime;
40+
41+
private AwsSessionCredentials(Builder builder) {
42+
this.accessKeyId = Validate.paramNotNull(builder.accessKeyId, "accessKey");
43+
this.secretAccessKey = Validate.paramNotNull(builder.secretAccessKey, "secretKey");
44+
this.sessionToken = Validate.paramNotNull(builder.sessionToken, "sessionToken");
45+
this.expirationTime = builder.expirationTime;
46+
}
47+
48+
/**
49+
* Returns a builder for this object.
50+
*/
51+
public static Builder builder() {
52+
return new Builder();
4153
}
4254

4355
/**
@@ -49,7 +61,7 @@ private AwsSessionCredentials(String accessKey, String secretKey, String session
4961
* received temporary permission to access some resource.
5062
*/
5163
public static AwsSessionCredentials create(String accessKey, String secretKey, String sessionToken) {
52-
return new AwsSessionCredentials(accessKey, secretKey, sessionToken);
64+
return builder().accessKeyId(accessKey).secretAccessKey(secretKey).sessionToken(sessionToken).build();
5365
}
5466

5567
/**
@@ -68,6 +80,13 @@ public String secretAccessKey() {
6880
return secretAccessKey;
6981
}
7082

83+
/**
84+
* Retrieve the expiration time of these credentials, if it exists.
85+
*/
86+
public Optional<Instant> expirationTime() {
87+
return Optional.ofNullable(expirationTime);
88+
}
89+
7190
/**
7291
* Retrieve the AWS session token. This token is retrieved from an AWS token service, and is used for authenticating that this
7392
* user has received temporary permission to access some resource.
@@ -95,7 +114,8 @@ public boolean equals(Object o) {
95114
AwsSessionCredentials that = (AwsSessionCredentials) o;
96115
return Objects.equals(accessKeyId, that.accessKeyId) &&
97116
Objects.equals(secretAccessKey, that.secretAccessKey) &&
98-
Objects.equals(sessionToken, that.sessionToken);
117+
Objects.equals(sessionToken, that.sessionToken) &&
118+
Objects.equals(expirationTime, that.expirationTime().orElse(null));
99119
}
100120

101121
@Override
@@ -104,6 +124,57 @@ public int hashCode() {
104124
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId());
105125
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey());
106126
hashCode = 31 * hashCode + Objects.hashCode(sessionToken());
127+
hashCode = 31 * hashCode + Objects.hashCode(expirationTime);
107128
return hashCode;
108129
}
130+
131+
/**
132+
* A builder for creating an instance of {@link AwsSessionCredentials}. This can be created with the static
133+
* {@link #builder()} method.
134+
*/
135+
public static final class Builder {
136+
private String accessKeyId;
137+
private String secretAccessKey;
138+
private String sessionToken;
139+
private Instant expirationTime;
140+
141+
/**
142+
* The AWS access key, used to identify the user interacting with services. Required.
143+
*/
144+
public Builder accessKeyId(String accessKeyId) {
145+
this.accessKeyId = accessKeyId;
146+
return this;
147+
}
148+
149+
/**
150+
* The AWS secret access key, used to authenticate the user interacting with services. Required
151+
*/
152+
public Builder secretAccessKey(String secretAccessKey) {
153+
this.secretAccessKey = secretAccessKey;
154+
return this;
155+
}
156+
157+
/**
158+
* The AWS session token, retrieved from an AWS token service, used for authenticating that this user has
159+
* received temporary permission to access some resource. Required
160+
*/
161+
public Builder sessionToken(String sessionToken) {
162+
this.sessionToken = sessionToken;
163+
return this;
164+
}
165+
166+
/**
167+
* The time after which this identity will no longer be valid. If this is empty,
168+
* an expiration time is not known (but the identity may still expire at some
169+
* time in the future).
170+
*/
171+
public Builder expirationTime(Instant expirationTime) {
172+
this.expirationTime = expirationTime;
173+
return this;
174+
}
175+
176+
public AwsSessionCredentials build() {
177+
return new AwsSessionCredentials(this);
178+
}
179+
}
109180
}

core/auth/src/test/java/software/amazon/awssdk/auth/credentials/internal/AwsSessionCredentialsTest.java

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,64 @@
1515

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

18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
import static org.junit.jupiter.api.Assertions.assertThrows;
1820

19-
import static org.assertj.core.api.Assertions.assertThat;
20-
21+
import nl.jqno.equalsverifier.EqualsVerifier;
2122
import org.junit.jupiter.api.Test;
2223
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
2324

2425
public class AwsSessionCredentialsTest {
2526

27+
private static final String ACCESS_KEY_ID = "accessKeyId";
28+
private static final String SECRET_ACCESS_KEY = "secretAccessKey";
29+
private static final String SESSION_TOKEN = "sessionToken";
30+
31+
public void equalsHashcode() {
32+
EqualsVerifier.forClass(AwsSessionCredentials.class)
33+
.verify();
34+
}
35+
36+
@Test
37+
public void emptyBuilder_ThrowsException() {
38+
assertThrows(NullPointerException.class, () -> AwsSessionCredentials.builder().build());
39+
}
40+
41+
@Test
42+
public void builderMissingSessionToken_ThrowsException() {
43+
assertThrows(NullPointerException.class, () -> AwsSessionCredentials.builder()
44+
.accessKeyId(ACCESS_KEY_ID)
45+
.secretAccessKey(SECRET_ACCESS_KEY)
46+
.build());
47+
}
2648

2749
@Test
28-
public void equalsHashCode() {
29-
AwsSessionCredentials credentials =
30-
AwsSessionCredentials.create("test", "key", "sessionToken");
31-
32-
AwsSessionCredentials anotherCredentials =
33-
AwsSessionCredentials.create("test", "key", "sessionToken");
34-
assertThat(credentials).isEqualTo(anotherCredentials);
35-
assertThat(credentials.hashCode()).isEqualTo(anotherCredentials.hashCode());
50+
public void builderMissingAccessKeyId_ThrowsException() {
51+
assertThrows(NullPointerException.class, () -> AwsSessionCredentials.builder()
52+
.secretAccessKey(SECRET_ACCESS_KEY)
53+
.sessionToken(SESSION_TOKEN)
54+
.build());
3655
}
3756

57+
@Test
58+
public void create_isSuccessful() {
59+
AwsSessionCredentials identity = AwsSessionCredentials.create(ACCESS_KEY_ID,
60+
SECRET_ACCESS_KEY,
61+
SESSION_TOKEN);
62+
assertEquals(ACCESS_KEY_ID, identity.accessKeyId());
63+
assertEquals(SECRET_ACCESS_KEY, identity.secretAccessKey());
64+
assertEquals(SESSION_TOKEN, identity.sessionToken());
65+
}
66+
67+
@Test
68+
public void build_isSuccessful() {
69+
AwsSessionCredentials identity = AwsSessionCredentials.builder()
70+
.accessKeyId(ACCESS_KEY_ID)
71+
.secretAccessKey(SECRET_ACCESS_KEY)
72+
.sessionToken(SESSION_TOKEN)
73+
.build();
74+
assertEquals(ACCESS_KEY_ID, identity.accessKeyId());
75+
assertEquals(SECRET_ACCESS_KEY, identity.secretAccessKey());
76+
assertEquals(SESSION_TOKEN, identity.sessionToken());
77+
}
3878
}

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/SessionCredentialsHolder.java

Lines changed: 0 additions & 48 deletions
This file was deleted.

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsAssumeRoleCredentialsProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515

1616
package software.amazon.awssdk.services.sts.auth;
1717

18+
import static software.amazon.awssdk.services.sts.internal.StsAuthUtils.toAwsSessionCredentials;
19+
1820
import java.util.function.Consumer;
1921
import java.util.function.Supplier;
2022
import software.amazon.awssdk.annotations.NotThreadSafe;
2123
import software.amazon.awssdk.annotations.SdkPublicApi;
2224
import software.amazon.awssdk.annotations.ThreadSafe;
2325
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
26+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
2427
import software.amazon.awssdk.services.sts.StsClient;
2528
import software.amazon.awssdk.services.sts.model.AssumeRoleRequest;
26-
import software.amazon.awssdk.services.sts.model.Credentials;
2729
import software.amazon.awssdk.utils.ToString;
2830
import software.amazon.awssdk.utils.Validate;
2931
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
@@ -65,10 +67,10 @@ public static Builder builder() {
6567
}
6668

6769
@Override
68-
protected Credentials getUpdatedCredentials(StsClient stsClient) {
70+
protected AwsSessionCredentials getUpdatedCredentials(StsClient stsClient) {
6971
AssumeRoleRequest assumeRoleRequest = assumeRoleRequestSupplier.get();
7072
Validate.notNull(assumeRoleRequest, "Assume role request must not be null.");
71-
return stsClient.assumeRole(assumeRoleRequest).credentials();
73+
return toAwsSessionCredentials(stsClient.assumeRole(assumeRoleRequest).credentials());
7274
}
7375

7476
@Override

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsAssumeRoleWithSamlCredentialsProvider.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,17 @@
1515

1616
package software.amazon.awssdk.services.sts.auth;
1717

18+
import static software.amazon.awssdk.services.sts.internal.StsAuthUtils.toAwsSessionCredentials;
19+
1820
import java.util.function.Consumer;
1921
import java.util.function.Supplier;
2022
import software.amazon.awssdk.annotations.NotThreadSafe;
2123
import software.amazon.awssdk.annotations.SdkPublicApi;
2224
import software.amazon.awssdk.annotations.ThreadSafe;
2325
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
26+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
2427
import software.amazon.awssdk.services.sts.StsClient;
2528
import software.amazon.awssdk.services.sts.model.AssumeRoleWithSamlRequest;
26-
import software.amazon.awssdk.services.sts.model.Credentials;
2729
import software.amazon.awssdk.utils.ToString;
2830
import software.amazon.awssdk.utils.Validate;
2931
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
@@ -66,10 +68,10 @@ public static Builder builder() {
6668
}
6769

6870
@Override
69-
protected Credentials getUpdatedCredentials(StsClient stsClient) {
71+
protected AwsSessionCredentials getUpdatedCredentials(StsClient stsClient) {
7072
AssumeRoleWithSamlRequest assumeRoleWithSamlRequest = assumeRoleWithSamlRequestSupplier.get();
7173
Validate.notNull(assumeRoleWithSamlRequest, "Assume role with saml request must not be null.");
72-
return stsClient.assumeRoleWithSAML(assumeRoleWithSamlRequest).credentials();
74+
return toAwsSessionCredentials(stsClient.assumeRoleWithSAML(assumeRoleWithSamlRequest).credentials());
7375
}
7476

7577
@Override

services/sts/src/main/java/software/amazon/awssdk/services/sts/auth/StsAssumeRoleWithWebIdentityCredentialsProvider.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
package software.amazon.awssdk.services.sts.auth;
1717

18+
import static software.amazon.awssdk.services.sts.internal.StsAuthUtils.toAwsSessionCredentials;
1819
import static software.amazon.awssdk.utils.Validate.notNull;
1920

2021
import java.util.function.Consumer;
@@ -23,9 +24,9 @@
2324
import software.amazon.awssdk.annotations.SdkPublicApi;
2425
import software.amazon.awssdk.annotations.ThreadSafe;
2526
import software.amazon.awssdk.auth.credentials.AwsCredentialsProvider;
27+
import software.amazon.awssdk.auth.credentials.AwsSessionCredentials;
2628
import software.amazon.awssdk.services.sts.StsClient;
2729
import software.amazon.awssdk.services.sts.model.AssumeRoleWithWebIdentityRequest;
28-
import software.amazon.awssdk.services.sts.model.Credentials;
2930
import software.amazon.awssdk.utils.ToString;
3031
import software.amazon.awssdk.utils.builder.ToCopyableBuilder;
3132

@@ -67,10 +68,10 @@ public static Builder builder() {
6768
}
6869

6970
@Override
70-
protected Credentials getUpdatedCredentials(StsClient stsClient) {
71+
protected AwsSessionCredentials getUpdatedCredentials(StsClient stsClient) {
7172
AssumeRoleWithWebIdentityRequest request = assumeRoleWithWebIdentityRequest.get();
7273
notNull(request, "AssumeRoleWithWebIdentityRequest can't be null");
73-
return stsClient.assumeRoleWithWebIdentity(request).credentials();
74+
return toAwsSessionCredentials(stsClient.assumeRoleWithWebIdentity(request).credentials());
7475
}
7576

7677
@Override

0 commit comments

Comments
 (0)