17
17
18
18
import java .time .Duration ;
19
19
import java .time .Instant ;
20
+ import java .util .Optional ;
20
21
import java .util .function .Function ;
21
22
import software .amazon .awssdk .annotations .NotThreadSafe ;
22
23
import software .amazon .awssdk .annotations .SdkInternalApi ;
31
32
import software .amazon .awssdk .utils .cache .NonBlocking ;
32
33
import software .amazon .awssdk .utils .cache .RefreshResult ;
33
34
35
+
34
36
/**
35
37
* An implementation of {@link AwsCredentialsProvider} that is extended within this package to provide support for periodically-
36
38
* updating session credentials. When credentials get close to expiration, this class will attempt to update them asynchronously
40
42
@ ThreadSafe
41
43
@ SdkInternalApi
42
44
abstract class StsCredentialsProvider implements AwsCredentialsProvider , SdkAutoCloseable {
45
+
46
+ private static final Duration DEFAULT_STALE_TIME = Duration .ofMinutes (1 );
47
+ private static final Duration DEFAULT_PREFETCH_TIME = Duration .ofMinutes (5 );
48
+
43
49
/**
44
50
* The STS client that should be used for periodically updating the session credentials in the background.
45
51
*/
@@ -50,9 +56,15 @@ abstract class StsCredentialsProvider implements AwsCredentialsProvider, SdkAuto
50
56
*/
51
57
private final CachedSupplier <SessionCredentialsHolder > sessionCache ;
52
58
59
+ private final Duration staleTime ;
60
+ private final Duration prefetchTime ;
61
+
53
62
protected StsCredentialsProvider (BaseBuilder <?, ?> builder , String asyncThreadName ) {
54
63
this .stsClient = Validate .notNull (builder .stsClient , "STS client must not be null." );
55
64
65
+ this .staleTime = Optional .ofNullable (builder .staleTime ).orElse (DEFAULT_STALE_TIME );
66
+ this .prefetchTime = Optional .ofNullable (builder .prefetchTime ).orElse (DEFAULT_PREFETCH_TIME );
67
+
56
68
CachedSupplier .Builder <SessionCredentialsHolder > cacheBuilder = CachedSupplier .builder (this ::updateSessionCredentials );
57
69
if (builder .asyncCredentialUpdateEnabled ) {
58
70
cacheBuilder .prefetchStrategy (new NonBlocking (asyncThreadName ));
@@ -67,9 +79,10 @@ protected StsCredentialsProvider(BaseBuilder<?, ?> builder, String asyncThreadNa
67
79
private RefreshResult <SessionCredentialsHolder > updateSessionCredentials () {
68
80
SessionCredentialsHolder credentials = new SessionCredentialsHolder (getUpdatedCredentials (stsClient ));
69
81
Instant actualTokenExpiration = credentials .getSessionCredentialsExpiration ().toInstant ();
82
+
70
83
return RefreshResult .builder (credentials )
71
- .staleTime (actualTokenExpiration .minus (Duration . ofMinutes ( 1 ) ))
72
- .prefetchTime (actualTokenExpiration .minus (Duration . ofMinutes ( 5 ) ))
84
+ .staleTime (actualTokenExpiration .minus (staleTime ))
85
+ .prefetchTime (actualTokenExpiration .minus (prefetchTime ))
73
86
.build ();
74
87
}
75
88
@@ -83,6 +96,21 @@ public void close() {
83
96
sessionCache .close ();
84
97
}
85
98
99
+ /**
100
+ * The amount of time, relative to STS token expiration, that the cached credentials are considered stale and should no longer be used.
101
+ * All threads will block until the value is updated.
102
+ */
103
+ public Duration staleTime () {
104
+ return staleTime ;
105
+ }
106
+
107
+ /**
108
+ * The amount of time, relative to STS token expiration, that the cached credentials are considered close to stale and should be updated.
109
+ */
110
+ public Duration prefetchTime () {
111
+ return prefetchTime ;
112
+ }
113
+
86
114
/**
87
115
* Implemented by a child class to call STS and get a new set of credentials to be used by this provider.
88
116
*/
@@ -97,6 +125,8 @@ protected abstract static class BaseBuilder<B extends BaseBuilder<B, T>, T> {
97
125
98
126
private Boolean asyncCredentialUpdateEnabled = false ;
99
127
private StsClient stsClient ;
128
+ private Duration staleTime ;
129
+ private Duration prefetchTime ;
100
130
101
131
protected BaseBuilder (Function <B , T > providerConstructor ) {
102
132
this .providerConstructor = providerConstructor ;
@@ -127,6 +157,31 @@ public B asyncCredentialUpdateEnabled(Boolean asyncCredentialUpdateEnabled) {
127
157
return (B ) this ;
128
158
}
129
159
160
+ /**
161
+ * Configure the amount of time, relative to STS token expiration, that the cached credentials are considered stale and should no longer be used.
162
+ * All threads will block until the value is updated.
163
+ *
164
+ * <p>By default, this is 1 minute.</p>
165
+ */
166
+ @ SuppressWarnings ("unchecked" )
167
+ public B staleTime (Duration staleTime ) {
168
+ this .staleTime = staleTime ;
169
+ return (B ) this ;
170
+ }
171
+
172
+ /**
173
+ * Configure the amount of time, relative to STS token expiration, that the cached credentials are considered close to stale and should be updated.
174
+ * See {@link #asyncCredentialUpdateEnabled}.
175
+ *
176
+ * <p>By default, this is 5 minutes.</p>
177
+ */
178
+ @ SuppressWarnings ("unchecked" )
179
+ public B prefetchTime (Duration prefetchTime ) {
180
+ this .prefetchTime = prefetchTime ;
181
+ return (B ) this ;
182
+ }
183
+
184
+
130
185
/**
131
186
* Build the credentials provider using the configuration applied to this builder.
132
187
*/
0 commit comments