19
19
20
20
import java .nio .file .Path ;
21
21
import java .nio .file .Paths ;
22
+ import java .time .Duration ;
22
23
import software .amazon .awssdk .annotations .SdkPublicApi ;
23
24
import software .amazon .awssdk .auth .credentials .internal .WebIdentityCredentialsUtils ;
24
25
import software .amazon .awssdk .auth .credentials .internal .WebIdentityTokenCredentialProperties ;
31
32
import software .amazon .awssdk .utils .builder .ToCopyableBuilder ;
32
33
33
34
/**
34
- * A credential provider that will read web identity token file path, aws role arn
35
- * and aws session name from system properties or environment variables for using
36
- * web identity token credentials with STS.
35
+ * A credential provider that will read web identity token file path, aws role arn and aws session name from system properties or
36
+ * environment variables for using web identity token credentials with STS.
37
37
* <p>
38
- * Use of this credentials provider requires the 'sts' module to be on the classpath.
38
+ * Use of this credentials provider requires the 'sts' module to be on the classpath.
39
39
* </p>
40
40
* <p>
41
- * StsWebIdentityTokenFileCredentialsProvider in sts package can be used instead of this class if any one of following is required
42
- *<ul>
41
+ * StsWebIdentityTokenFileCredentialsProvider in sts package can be used instead of this class if any one of following is
42
+ * required
43
+ * <ul>
43
44
* <li>Pass a custom StsClient to the provider. </li>
44
45
* <li>Periodically update credentials </li>
45
- *</ul>
46
+ * </ul>
47
+ *
46
48
* @see AwsCredentialsProvider
47
49
*/
48
50
@ SdkPublicApi
@@ -62,13 +64,22 @@ public class WebIdentityTokenFileCredentialsProvider
62
64
63
65
private final Boolean asyncCredentialUpdateEnabled ;
64
66
67
+ private final Duration prefetchTime ;
68
+
69
+ private final Duration staleTime ;
70
+
71
+ private final Duration roleSessionDuration ;
72
+
65
73
private WebIdentityTokenFileCredentialsProvider (BuilderImpl builder ) {
66
74
AwsCredentialsProvider credentialsProvider = null ;
67
75
RuntimeException loadException = null ;
68
76
String roleArn = null ;
69
77
String roleSessionName = null ;
70
78
Path webIdentityTokenFile = null ;
71
79
Boolean asyncCredentialUpdateEnabled = null ;
80
+ Duration prefetchTime = null ;
81
+ Duration staleTime = null ;
82
+ Duration roleSessionDuration = null ;
72
83
73
84
try {
74
85
webIdentityTokenFile =
@@ -77,7 +88,7 @@ private WebIdentityTokenFileCredentialsProvider(BuilderImpl builder) {
77
88
.getStringValueOrThrow ()));
78
89
79
90
roleArn = builder .roleArn != null ? builder .roleArn
80
- : trim (SdkSystemSetting .AWS_ROLE_ARN .getStringValueOrThrow ());
91
+ : trim (SdkSystemSetting .AWS_ROLE_ARN .getStringValueOrThrow ());
81
92
82
93
roleSessionName =
83
94
builder .roleSessionName != null ? builder .roleSessionName
@@ -86,12 +97,19 @@ private WebIdentityTokenFileCredentialsProvider(BuilderImpl builder) {
86
97
asyncCredentialUpdateEnabled =
87
98
builder .asyncCredentialUpdateEnabled != null ? builder .asyncCredentialUpdateEnabled : false ;
88
99
100
+ prefetchTime = builder .prefetchTime ;
101
+ staleTime = builder .staleTime ;
102
+ roleSessionDuration = builder .roleSessionDuration ;
103
+
89
104
WebIdentityTokenCredentialProperties credentialProperties =
90
105
WebIdentityTokenCredentialProperties .builder ()
91
106
.roleArn (roleArn )
92
107
.roleSessionName (roleSessionName )
93
108
.webIdentityTokenFile (webIdentityTokenFile )
94
109
.asyncCredentialUpdateEnabled (asyncCredentialUpdateEnabled )
110
+ .prefetchTime (prefetchTime )
111
+ .staleTime (staleTime )
112
+ .roleSessionDuration (roleSessionDuration )
95
113
.build ();
96
114
97
115
credentialsProvider = WebIdentityCredentialsUtils .factory ().create (credentialProperties );
@@ -108,10 +126,12 @@ private WebIdentityTokenFileCredentialsProvider(BuilderImpl builder) {
108
126
this .roleSessionName = roleSessionName ;
109
127
this .webIdentityTokenFile = webIdentityTokenFile ;
110
128
this .asyncCredentialUpdateEnabled = asyncCredentialUpdateEnabled ;
129
+ this .prefetchTime = prefetchTime ;
130
+ this .staleTime = staleTime ;
131
+ this .roleSessionDuration = roleSessionDuration ;
111
132
}
112
133
113
134
public static WebIdentityTokenFileCredentialsProvider create () {
114
-
115
135
return WebIdentityTokenFileCredentialsProvider .builder ().build ();
116
136
}
117
137
@@ -165,9 +185,33 @@ public interface Builder extends CopyableBuilder<Builder, WebIdentityTokenFileCr
165
185
/**
166
186
* Define whether the provider should fetch credentials asynchronously in the background.
167
187
*/
168
-
169
188
Builder asyncCredentialUpdateEnabled (Boolean asyncCredentialUpdateEnabled );
170
189
190
+ /**
191
+ * Configure the amount of time, relative to STS token expiration, that the cached credentials are considered close to
192
+ * stale and should be updated.
193
+ *
194
+ * <p>Prefetch updates will occur between the specified time and the stale time of the provider. Prefetch
195
+ * updates may be asynchronous. See {@link #asyncCredentialUpdateEnabled}.
196
+ *
197
+ * <p>By default, this is 5 minutes.
198
+ */
199
+ Builder prefetchTime (Duration prefetchTime );
200
+
201
+ /**
202
+ * Configure the amount of time, relative to STS token expiration, that the cached credentials are considered stale and
203
+ * must be updated. All threads will block until the value is updated.
204
+ *
205
+ * <p>By default, this is 1 minute.
206
+ */
207
+ Builder staleTime (Duration staleTime );
208
+
209
+ /**
210
+ * @param sessionDuration
211
+ * @return
212
+ */
213
+ Builder roleSessionDuration (Duration sessionDuration );
214
+
171
215
/**
172
216
* Create a {@link WebIdentityTokenFileCredentialsProvider} using the configuration applied to this builder.
173
217
*/
@@ -179,6 +223,9 @@ static final class BuilderImpl implements Builder {
179
223
private String roleSessionName ;
180
224
private Path webIdentityTokenFile ;
181
225
private Boolean asyncCredentialUpdateEnabled ;
226
+ private Duration prefetchTime ;
227
+ private Duration staleTime ;
228
+ private Duration roleSessionDuration ;
182
229
183
230
BuilderImpl () {
184
231
}
@@ -188,6 +235,9 @@ private BuilderImpl(WebIdentityTokenFileCredentialsProvider provider) {
188
235
this .roleSessionName = provider .roleSessionName ;
189
236
this .webIdentityTokenFile = provider .webIdentityTokenFile ;
190
237
this .asyncCredentialUpdateEnabled = provider .asyncCredentialUpdateEnabled ;
238
+ this .prefetchTime = provider .prefetchTime ;
239
+ this .staleTime = provider .staleTime ;
240
+ this .roleSessionDuration = provider .roleSessionDuration ;
191
241
}
192
242
193
243
@ Override
@@ -230,6 +280,36 @@ public void setAsyncCredentialUpdateEnabled(Boolean asyncCredentialUpdateEnabled
230
280
asyncCredentialUpdateEnabled (asyncCredentialUpdateEnabled );
231
281
}
232
282
283
+ @ Override
284
+ public Builder prefetchTime (Duration prefetchTime ) {
285
+ this .prefetchTime = prefetchTime ;
286
+ return this ;
287
+ }
288
+
289
+ public void setPrefetchTime (Duration prefetchTime ) {
290
+ prefetchTime (prefetchTime );
291
+ }
292
+
293
+ @ Override
294
+ public Builder staleTime (Duration staleTime ) {
295
+ this .staleTime = staleTime ;
296
+ return this ;
297
+ }
298
+
299
+ public void setStaleTime (Duration staleTime ) {
300
+ staleTime (staleTime );
301
+ }
302
+
303
+ @ Override
304
+ public Builder roleSessionDuration (Duration sessionDuration ) {
305
+ this .roleSessionDuration = sessionDuration ;
306
+ return this ;
307
+ }
308
+
309
+ public void setRoleSessionDuration (Duration roleSessionDuration ) {
310
+ roleSessionDuration (roleSessionDuration );
311
+ }
312
+
233
313
@ Override
234
314
public WebIdentityTokenFileCredentialsProvider build () {
235
315
return new WebIdentityTokenFileCredentialsProvider (this );
0 commit comments