Skip to content

Commit 1f2ac6f

Browse files
authored
Refactors credential identity with separate implementing classes (#4024)
* refactors credential identity to create separate classes * Minor changes to default identity classes
1 parent 64d5212 commit 1f2ac6f

File tree

6 files changed

+412
-92
lines changed

6 files changed

+412
-92
lines changed

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

Lines changed: 19 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515

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

18-
import java.util.Objects;
1918
import software.amazon.awssdk.annotations.SdkPublicApi;
2019
import software.amazon.awssdk.annotations.ThreadSafe;
21-
import software.amazon.awssdk.utils.ToString;
22-
import software.amazon.awssdk.utils.Validate;
20+
import software.amazon.awssdk.identity.spi.internal.DefaultAwsCredentialsIdentity;
2321

2422
/**
2523
* Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These
@@ -45,54 +43,33 @@ public interface AwsCredentialsIdentity extends Identity {
4543
*/
4644
String secretAccessKey();
4745

46+
static Builder builder() {
47+
return DefaultAwsCredentialsIdentity.builder();
48+
}
49+
4850
/**
4951
* Constructs a new credentials object, with the specified AWS access key and AWS secret key.
5052
*
5153
* @param accessKeyId The AWS access key, used to identify the user interacting with services.
5254
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with services.
5355
*/
5456
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-
}
57+
return builder().accessKeyId(accessKeyId)
58+
.secretAccessKey(secretAccessKey)
59+
.build();
60+
}
6861

69-
@Override
70-
public String toString() {
71-
return ToString.builder("AwsCredentialsIdentity")
72-
.add("accessKeyId", accessKeyId)
73-
.build();
74-
}
62+
interface Builder {
63+
/**
64+
* The AWS access key, used to identify the user interacting with services.
65+
*/
66+
Builder accessKeyId(String accessKeyId);
7567

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-
}
68+
/**
69+
* The AWS secret access key, used to authenticate the user interacting with services.
70+
*/
71+
Builder secretAccessKey(String secretAccessKey);
8872

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-
};
73+
AwsCredentialsIdentity build();
9774
}
9875
}

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

Lines changed: 22 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,9 @@
1515

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

18-
import java.util.Objects;
1918
import software.amazon.awssdk.annotations.SdkPublicApi;
2019
import software.amazon.awssdk.annotations.ThreadSafe;
21-
import software.amazon.awssdk.utils.ToString;
22-
import software.amazon.awssdk.utils.Validate;
20+
import software.amazon.awssdk.identity.spi.internal.DefaultAwsSessionCredentialsIdentity;
2321

2422
/**
2523
* A special type of {@link AwsCredentialsIdentity} that provides a session token to be used in service authentication. Session
@@ -36,6 +34,10 @@ public interface AwsSessionCredentialsIdentity extends AwsCredentialsIdentity {
3634
*/
3735
String sessionToken();
3836

37+
static AwsSessionCredentialsIdentity.Builder builder() {
38+
return DefaultAwsSessionCredentialsIdentity.builder();
39+
}
40+
3941
/**
4042
* Constructs a new session credentials object, with the specified AWS access key, AWS secret key and AWS session token.
4143
*
@@ -45,56 +47,26 @@ public interface AwsSessionCredentialsIdentity extends AwsCredentialsIdentity {
4547
* received temporary permission to access some resource.
4648
*/
4749
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-
}
50+
return builder().accessKeyId(accessKeyId)
51+
.secretAccessKey(secretAccessKey)
52+
.sessionToken(sessionToken)
53+
.build();
54+
}
6755

68-
@Override
69-
public String toString() {
70-
return ToString.builder("AwsSessionCredentialsIdentity")
71-
.add("accessKeyId", accessKeyId())
72-
.build();
73-
}
56+
interface Builder extends AwsCredentialsIdentity.Builder {
57+
@Override
58+
Builder accessKeyId(String accessKeyId);
7459

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-
}
60+
@Override
61+
Builder secretAccessKey(String secretAccessKey);
8362

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-
}
63+
/**
64+
* The AWS session token, retrieved from an AWS token service, used for authenticating that this user has
65+
* received temporary permission to access some resource.
66+
*/
67+
Builder sessionToken(String sessionToken);
8968

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-
};
69+
@Override
70+
AwsSessionCredentialsIdentity build();
9971
}
10072
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
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.internal;
17+
18+
import java.util.Objects;
19+
import software.amazon.awssdk.annotations.SdkInternalApi;
20+
import software.amazon.awssdk.identity.spi.AwsCredentialsIdentity;
21+
import software.amazon.awssdk.utils.ToString;
22+
import software.amazon.awssdk.utils.Validate;
23+
24+
@SdkInternalApi
25+
public final class DefaultAwsCredentialsIdentity implements AwsCredentialsIdentity {
26+
27+
private final String accessKeyId;
28+
private final String secretAccessKey;
29+
30+
private DefaultAwsCredentialsIdentity(Builder builder) {
31+
this.accessKeyId = builder.accessKeyId;
32+
this.secretAccessKey = builder.secretAccessKey;
33+
34+
Validate.paramNotNull(accessKeyId, "accessKeyId");
35+
Validate.paramNotNull(secretAccessKey, "secretAccessKey");
36+
}
37+
38+
public static AwsCredentialsIdentity.Builder builder() {
39+
return new Builder();
40+
}
41+
42+
@Override
43+
public String accessKeyId() {
44+
return accessKeyId;
45+
}
46+
47+
@Override
48+
public String secretAccessKey() {
49+
return secretAccessKey;
50+
}
51+
52+
@Override
53+
public String toString() {
54+
return ToString.builder("AwsCredentialsIdentity")
55+
.add("accessKeyId", accessKeyId)
56+
.build();
57+
}
58+
59+
@Override
60+
public boolean equals(Object o) {
61+
if (this == o) {
62+
return true;
63+
}
64+
if (o == null || getClass() != o.getClass()) {
65+
return false;
66+
}
67+
AwsCredentialsIdentity that = (AwsCredentialsIdentity) o;
68+
return Objects.equals(accessKeyId, that.accessKeyId()) &&
69+
Objects.equals(secretAccessKey, that.secretAccessKey());
70+
}
71+
72+
@Override
73+
public int hashCode() {
74+
int hashCode = 1;
75+
hashCode = 31 * hashCode + Objects.hashCode(accessKeyId);
76+
hashCode = 31 * hashCode + Objects.hashCode(secretAccessKey);
77+
return hashCode;
78+
}
79+
80+
private static final class Builder implements AwsCredentialsIdentity.Builder {
81+
private String accessKeyId;
82+
private String secretAccessKey;
83+
84+
private Builder() {
85+
}
86+
87+
@Override
88+
public Builder accessKeyId(String accessKeyId) {
89+
this.accessKeyId = accessKeyId;
90+
return this;
91+
}
92+
93+
@Override
94+
public Builder secretAccessKey(String secretAccessKey) {
95+
this.secretAccessKey = secretAccessKey;
96+
return this;
97+
}
98+
99+
@Override
100+
public AwsCredentialsIdentity build() {
101+
return new DefaultAwsCredentialsIdentity(this);
102+
}
103+
}
104+
}

0 commit comments

Comments
 (0)