18
18
import static software .amazon .awssdk .utils .StringUtils .trimToNull ;
19
19
20
20
import java .util .Objects ;
21
+ import java .util .Optional ;
21
22
import software .amazon .awssdk .annotations .Immutable ;
22
23
import software .amazon .awssdk .annotations .SdkInternalApi ;
23
24
import software .amazon .awssdk .annotations .SdkPublicApi ;
24
25
import software .amazon .awssdk .utils .ToString ;
25
26
import software .amazon .awssdk .utils .Validate ;
26
27
27
28
/**
28
- * Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These
29
- * credentials are used to securely sign requests to services (e.g., AWS services) that use them for authentication.
29
+ * Provides access to the AWS credentials used for accessing services: AWS access key ID and secret access key. These credentials
30
+ * are used to securely sign requests to services (e.g., AWS services) that use them for authentication.
30
31
*
31
32
* <p>For more details on AWS access keys, see:
32
33
* <a href="https://docs.aws.amazon.com/general/latest/gr/aws-sec-cred-types.html#access-keys-and-secret-access-keys">
39
40
public final class AwsBasicCredentials implements AwsCredentials {
40
41
/**
41
42
* A set of AWS credentials without an access key or secret access key, indicating that anonymous access should be used.
42
- *
43
+ * <p>
43
44
* This should be accessed via {@link AnonymousCredentialsProvider#resolveCredentials()}.
44
45
*/
45
46
@ SdkInternalApi
46
- static final AwsBasicCredentials ANONYMOUS_CREDENTIALS = new AwsBasicCredentials ( null , null , false );
47
+ static final AwsBasicCredentials ANONYMOUS_CREDENTIALS = builder (). validateCredentials ( false ). build ( );
47
48
48
49
private final String accessKeyId ;
49
50
private final String secretAccessKey ;
51
+ private final String accountId ;
52
+
53
+ private AwsBasicCredentials (Builder builder ) {
54
+ this .accessKeyId = trimToNull (builder .accessKeyId );
55
+ this .secretAccessKey = trimToNull (builder .secretAccessKey );
56
+
57
+ if (builder .validateCredentials ) {
58
+ Validate .notNull (this .accessKeyId , "Access key ID cannot be blank." );
59
+ Validate .notNull (this .secretAccessKey , "Secret access key cannot be blank." );
60
+ }
61
+ this .accountId = builder .accountId ;
62
+ }
50
63
51
64
/**
52
65
* Constructs a new credentials object, with the specified AWS access key and AWS secret key.
@@ -55,27 +68,25 @@ public final class AwsBasicCredentials implements AwsCredentials {
55
68
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
56
69
*/
57
70
protected AwsBasicCredentials (String accessKeyId , String secretAccessKey ) {
58
- this (accessKeyId , secretAccessKey , true );
71
+ this (builder ().accessKeyId (accessKeyId )
72
+ .secretAccessKey (secretAccessKey )
73
+ .validateCredentials (true ));
59
74
}
60
75
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
- }
76
+ public static Builder builder () {
77
+ return new Builder ();
69
78
}
70
79
71
80
/**
72
81
* Constructs a new credentials object, with the specified AWS access key and AWS secret key.
73
82
*
74
- * @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
83
+ * @param accessKeyId The AWS access key, used to identify the user interacting with AWS.
75
84
* @param secretAccessKey The AWS secret access key, used to authenticate the user interacting with AWS.
76
- * * /
85
+ */
77
86
public static AwsBasicCredentials create (String accessKeyId , String secretAccessKey ) {
78
- return new AwsBasicCredentials (accessKeyId , secretAccessKey );
87
+ return builder ().accessKeyId (accessKeyId )
88
+ .secretAccessKey (secretAccessKey )
89
+ .build ();
79
90
}
80
91
81
92
/**
@@ -94,10 +105,19 @@ public String secretAccessKey() {
94
105
return secretAccessKey ;
95
106
}
96
107
108
+ /**
109
+ * Retrieve the AWS account id associated with this credentials identity, if found.
110
+ */
111
+ @ Override
112
+ public Optional <String > accountId () {
113
+ return Optional .ofNullable (accountId );
114
+ }
115
+
97
116
@ Override
98
117
public String toString () {
99
118
return ToString .builder ("AwsCredentials" )
100
119
.add ("accessKeyId" , accessKeyId )
120
+ .add ("accountId" , accountId )
101
121
.build ();
102
122
}
103
123
@@ -111,14 +131,59 @@ public boolean equals(Object o) {
111
131
}
112
132
AwsBasicCredentials that = (AwsBasicCredentials ) o ;
113
133
return Objects .equals (accessKeyId , that .accessKeyId ) &&
114
- Objects .equals (secretAccessKey , that .secretAccessKey );
134
+ Objects .equals (secretAccessKey , that .secretAccessKey ) &&
135
+ Objects .equals (accountId , that .accountId ().orElse (null ));
115
136
}
116
137
117
138
@ Override
118
139
public int hashCode () {
119
140
int hashCode = 1 ;
120
141
hashCode = 31 * hashCode + Objects .hashCode (accessKeyId ());
121
142
hashCode = 31 * hashCode + Objects .hashCode (secretAccessKey ());
143
+ hashCode = 31 * hashCode + Objects .hashCode (accountId );
122
144
return hashCode ;
123
145
}
146
+
147
+ public static final class Builder {
148
+ private String accessKeyId ;
149
+ private String secretAccessKey ;
150
+ private String accountId ;
151
+ private boolean validateCredentials = true ;
152
+
153
+ /**
154
+ * The AWS access key, used to identify the user interacting with services.
155
+ */
156
+ public Builder accessKeyId (String accessKeyId ) {
157
+ this .accessKeyId = accessKeyId ;
158
+ return this ;
159
+ }
160
+
161
+ /**
162
+ * The AWS secret access key, used to authenticate the user interacting with services.
163
+ */
164
+ public Builder secretAccessKey (String secretAccessKey ) {
165
+ this .secretAccessKey = secretAccessKey ;
166
+ return this ;
167
+ }
168
+
169
+ /**
170
+ * The AWS account id associated with this credentials identity.
171
+ */
172
+ public Builder accountId (String accountId ) {
173
+ this .accountId = accountId ;
174
+ return this ;
175
+ }
176
+
177
+ /**
178
+ * Whether this class should verify that accessKeyId and secretAccessKey are not null.
179
+ */
180
+ Builder validateCredentials (Boolean validateCredentials ) {
181
+ this .validateCredentials = validateCredentials ;
182
+ return this ;
183
+ }
184
+
185
+ public AwsBasicCredentials build () {
186
+ return new AwsBasicCredentials (this );
187
+ }
188
+ }
124
189
}
0 commit comments