Skip to content

Commit ac838e0

Browse files
authored
Add new Identity interfaces (#3773)
* Add new Identity interfaces * Move Impl classes to own files * Add missing @immutable * Address feedback - interface override and javadoc edits * Mark the new interfaces @threadsafe * Remove static create methods in AwsCredentialsIdentity and impls * Add japicmp excludes for methods moved to parent interface Was getting these errors otherwise: ``` [ERROR] Failed to execute goal com.github.siom79.japicmp:japicmp-maven-plugin:0.15.6:cmp (default) on project auth: There is at least one incompatibility: software.amazon.awssdk.auth.credentials.AwsCredentials.accessKeyId():METHOD_REMOVED,software.amazon.awssdk.auth.credentials.AwsCredentials.secretAccessKey():METHOD_REMOVED,software.amazon.awssdk.auth.token.credentials.SdkToken.expirationTime():METHOD_REMOVED,software.amazon.awssdk.auth.token.credentials.SdkToken.token():METHOD_REMOVED -> [Help 1] ```
1 parent 5db07c8 commit ac838e0

File tree

13 files changed

+416
-53
lines changed

13 files changed

+416
-53
lines changed

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

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,18 @@
1616
package software.amazon.awssdk.auth.credentials;
1717

1818
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
1920

2021
/**
21-
* Provides access to the AWS credentials used for accessing AWS services: AWS access key ID and secret access key. These
22-
* credentials are used to securely sign requests to AWS services.
22+
* Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These
23+
* credentials are used to securely sign requests to services (e.g., AWS services) that use them for authentication.
2324
*
2425
* <p>For more details on AWS access keys, see:
25-
* <a href="http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys">
26-
* http://docs.amazonwebservices.com/AWSSecurityCredentials/1.0/AboutAWSCredentials.html#AccessKeys</a></p>
26+
* <a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys">
27+
* https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys</a></p>
2728
*
2829
* @see AwsCredentialsProvider
2930
*/
3031
@SdkPublicApi
31-
public interface AwsCredentials {
32-
33-
/**
34-
* Retrieve the AWS access key, used to identify the user interacting with AWS.
35-
*/
36-
String accessKeyId();
37-
38-
/**
39-
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
40-
*/
41-
String secretAccessKey();
32+
public interface AwsCredentials extends AwsCredentialsIdentity {
4233
}

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,11 @@
1515

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

18+
import java.util.concurrent.CompletableFuture;
1819
import software.amazon.awssdk.annotations.SdkPublicApi;
20+
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
21+
import software.amazon.awssdk.identity.spi.IdentityProvider;
22+
import software.amazon.awssdk.identity.spi.ResolveIdentityRequest;
1923

2024
/**
2125
* Interface for loading {@link AwsCredentials} that are used for authentication.
@@ -27,7 +31,7 @@
2731
*/
2832
@FunctionalInterface
2933
@SdkPublicApi
30-
public interface AwsCredentialsProvider {
34+
public interface AwsCredentialsProvider extends IdentityProvider<AwsCredentialsIdentity> {
3135
/**
3236
* Returns {@link AwsCredentials} that can be used to authorize an AWS request. Each implementation of AWSCredentialsProvider
3337
* can choose its own strategy for loading credentials. For example, an implementation might load credentials from an existing
@@ -39,4 +43,14 @@ public interface AwsCredentialsProvider {
3943
* @return AwsCredentials which the caller can use to authorize an AWS request.
4044
*/
4145
AwsCredentials resolveCredentials();
46+
47+
@Override
48+
default Class<AwsCredentialsIdentity> identityType() {
49+
return AwsCredentialsIdentity.class;
50+
}
51+
52+
@Override
53+
default CompletableFuture<AwsCredentialsIdentity> resolveIdentity(ResolveIdentityRequest request) {
54+
return CompletableFuture.completedFuture(resolveCredentials());
55+
}
4256
}

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

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.util.Objects;
1919
import software.amazon.awssdk.annotations.Immutable;
2020
import software.amazon.awssdk.annotations.SdkPublicApi;
21+
import software.amazon.awssdk.identity.spi.AwsSessionCredentialsIdentity;
2122
import software.amazon.awssdk.utils.ToString;
2223
import software.amazon.awssdk.utils.Validate;
2324

@@ -28,7 +29,7 @@
2829
*/
2930
@Immutable
3031
@SdkPublicApi
31-
public final class AwsSessionCredentials implements AwsCredentials {
32+
public final class AwsSessionCredentials implements AwsCredentials, AwsSessionCredentialsIdentity {
3233

3334
private final String accessKeyId;
3435
private final String secretAccessKey;
@@ -52,26 +53,17 @@ public static AwsSessionCredentials create(String accessKey, String secretKey, S
5253
return new AwsSessionCredentials(accessKey, secretKey, sessionToken);
5354
}
5455

55-
/**
56-
* Retrieve the AWS access key, used to identify the user interacting with AWS.
57-
*/
5856
@Override
5957
public String accessKeyId() {
6058
return accessKeyId;
6159
}
6260

63-
/**
64-
* Retrieve the AWS secret access key, used to authenticate the user interacting with AWS.
65-
*/
6661
@Override
6762
public String secretAccessKey() {
6863
return secretAccessKey;
6964
}
7065

71-
/**
72-
* Retrieve the AWS session token. This token is retrieved from an AWS token service, and is used for authenticating that this
73-
* user has received temporary permission to access some resource.
74-
*/
66+
@Override
7567
public String sessionToken() {
7668
return sessionToken;
7769
}

core/auth/src/main/java/software/amazon/awssdk/auth/token/credentials/SdkToken.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,34 +15,18 @@
1515

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

18-
import java.time.Instant;
19-
import java.util.Optional;
2018
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.identity.spi.TokenIdentity;
2120

2221
/**
23-
* Provides token which is used to securely authorize requests to AWS services.
24-
* A token is a string that the OAuth client uses to make requests to the resource server.
22+
* Provides token which is used to securely authorize requests to services that use token based auth, e.g., OAuth.
2523
*
26-
* <p>For more details on tokens, see:
24+
* <p>For more details on OAuth tokens, see:
2725
* <a href="https://oauth.net/2/access-tokens">
2826
* https://oauth.net/2/access-tokens</a></p>
2927
*
3028
* @see SdkTokenProvider
3129
*/
32-
3330
@SdkPublicApi
34-
public interface SdkToken {
35-
36-
37-
/**
38-
* Retrieves string field representing the literal token string.
39-
* A token is a string that the OAuth client uses to make requests to the resource server.
40-
*/
41-
String token();
42-
43-
44-
/**
45-
* Retrieves the time at which the token expires.
46-
*/
47-
Optional<Instant> expirationTime();
31+
public interface SdkToken extends TokenIdentity {
4832
}

core/auth/src/main/java/software/amazon/awssdk/auth/token/credentials/SdkTokenProvider.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,19 @@
1515

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

18+
import java.util.concurrent.CompletableFuture;
1819
import software.amazon.awssdk.annotations.SdkPublicApi;
19-
import software.amazon.awssdk.auth.token.credentials.SdkToken;
20+
import software.amazon.awssdk.identity.spi.IdentityProvider;
21+
import software.amazon.awssdk.identity.spi.ResolveIdentityRequest;
22+
import software.amazon.awssdk.identity.spi.TokenIdentity;
2023

2124
/**
2225
* Interface for loading {@link SdkToken} that are used for authentication.
2326
*
2427
*/
2528
@FunctionalInterface
2629
@SdkPublicApi
27-
public interface SdkTokenProvider {
30+
public interface SdkTokenProvider extends IdentityProvider<TokenIdentity> {
2831
/**
2932
* Returns an {@link SdkToken} that can be used to authorize a request. Each implementation of SdkTokenProvider
3033
* can choose its own strategy for loading token. For example, an implementation might load token from an existing
@@ -34,4 +37,14 @@ public interface SdkTokenProvider {
3437
* @return AwsToken which the caller can use to authorize an AWS request using token authorization for a request.
3538
*/
3639
SdkToken resolveToken();
40+
41+
@Override
42+
default Class<TokenIdentity> identityType() {
43+
return TokenIdentity.class;
44+
}
45+
46+
@Override
47+
default CompletableFuture<TokenIdentity> resolveIdentity(ResolveIdentityRequest request) {
48+
return CompletableFuture.completedFuture(resolveToken());
49+
}
3750
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 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.identity.spi;
17+
18+
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.annotations.ThreadSafe;
20+
21+
/**
22+
* Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These
23+
* credentials are used to securely sign requests to services (e.g., AWS services) that use them for authentication.
24+
*
25+
* <p>For more details on AWS access keys, see:
26+
* <a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys">
27+
* https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys</a></p>
28+
*/
29+
@SdkPublicApi
30+
@ThreadSafe
31+
public interface AwsCredentialsIdentity extends Identity {
32+
33+
/**
34+
* Retrieve the AWS access key, used to identify the user interacting with services.
35+
*/
36+
String accessKeyId();
37+
38+
/**
39+
* Retrieve the AWS secret access key, used to authenticate the user interacting with services.
40+
*/
41+
String secretAccessKey();
42+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
* Copyright 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.identity.spi;
17+
18+
import software.amazon.awssdk.annotations.SdkPublicApi;
19+
import software.amazon.awssdk.annotations.ThreadSafe;
20+
21+
/**
22+
* A special type of {@link AwsCredentialsIdentity} that provides a session token to be used in service authentication. Session
23+
* tokens are typically provided by a token broker service, like AWS Security Token Service, and provide temporary access to an
24+
* AWS service.
25+
*/
26+
@SdkPublicApi
27+
@ThreadSafe
28+
public interface AwsSessionCredentialsIdentity extends AwsCredentialsIdentity {
29+
30+
/**
31+
* Retrieve the AWS session token. This token is retrieved from an AWS token service, and is used for authenticating that this
32+
* user has received temporary permission to access some resource.
33+
*/
34+
String sessionToken();
35+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 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.identity.spi;
17+
18+
import java.time.Instant;
19+
import java.util.Optional;
20+
import software.amazon.awssdk.annotations.SdkPublicApi;
21+
import software.amazon.awssdk.annotations.ThreadSafe;
22+
23+
/**
24+
* Interface to represent <b>who</b> is using the SDK, i.e., the identity of the caller, used for authentication.
25+
*
26+
* <p>Examples include {@link AwsCredentialsIdentity} and {@link TokenIdentity}.</p>
27+
*
28+
* @see IdentityProvider
29+
*/
30+
@SdkPublicApi
31+
@ThreadSafe
32+
public interface Identity {
33+
/**
34+
* The time after which this identity will no longer be valid. If this is empty,
35+
* an expiration time is not known (but the identity may still expire at some
36+
* time in the future).
37+
*/
38+
default Optional<Instant> expirationTime() {
39+
return Optional.empty();
40+
}
41+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Copyright 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.identity.spi;
17+
18+
import java.util.Objects;
19+
import software.amazon.awssdk.annotations.Immutable;
20+
import software.amazon.awssdk.annotations.SdkProtectedApi;
21+
import software.amazon.awssdk.annotations.ThreadSafe;
22+
import software.amazon.awssdk.utils.ToString;
23+
import software.amazon.awssdk.utils.Validate;
24+
25+
/**
26+
* A strongly-typed property for input to an {@link IdentityProvider}.
27+
* @param <T> The type of the attribute.
28+
*/
29+
@SdkProtectedApi
30+
@Immutable
31+
@ThreadSafe
32+
public final class IdentityProperty<T> {
33+
private final Class<T> clazz;
34+
private final String name;
35+
36+
private IdentityProperty(Class<T> clazz, String name) {
37+
Validate.paramNotNull(clazz, "clazz");
38+
Validate.paramNotBlank(name, "name");
39+
40+
this.clazz = clazz;
41+
this.name = name;
42+
}
43+
44+
public static <T> IdentityProperty<T> create(Class<T> clazz, String name) {
45+
return new IdentityProperty<>(clazz, name);
46+
}
47+
48+
@Override
49+
public String toString() {
50+
return ToString.builder("IdentityProperty")
51+
.add("clazz", clazz)
52+
.add("name", name)
53+
.build();
54+
}
55+
56+
@Override
57+
public boolean equals(Object o) {
58+
if (this == o) {
59+
return true;
60+
}
61+
if (o == null || getClass() != o.getClass()) {
62+
return false;
63+
}
64+
65+
IdentityProperty<?> that = (IdentityProperty<?>) o;
66+
67+
return Objects.equals(clazz, that.clazz) &&
68+
Objects.equals(name, that.name);
69+
}
70+
71+
@Override
72+
public int hashCode() {
73+
int hashCode = 1;
74+
hashCode = 31 * hashCode + Objects.hashCode(clazz);
75+
hashCode = 31 * hashCode + Objects.hashCode(name);
76+
return hashCode;
77+
}
78+
}

0 commit comments

Comments
 (0)