forked from aws/aws-sdk-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebIdentityFederationSessionCredentialsProvider.java
More file actions
276 lines (246 loc) · 10.9 KB
/
WebIdentityFederationSessionCredentialsProvider.java
File metadata and controls
276 lines (246 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
* Copyright 2011-2013 Amazon Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at:
*
* http://aws.amazon.com/apache2.0
*
* This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and
* limitations under the License.
*/
package com.amazonaws.auth;
import java.util.Date;
import com.amazonaws.ClientConfiguration;
import com.amazonaws.services.securitytoken.AWSSecurityTokenService;
import com.amazonaws.services.securitytoken.AWSSecurityTokenServiceClient;
import com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest;
import com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityResult;
import com.amazonaws.services.securitytoken.model.Credentials;
/**
* AWSCredentialsProvider implementation that uses the AWS Security Token
* Service to create temporary, short-lived sessions to use for authentication.
*/
public class WebIdentityFederationSessionCredentialsProvider implements AWSCredentialsProvider {
/** Default duration for started sessions */
public static final int DEFAULT_DURATION_SECONDS = 3600;
/** Default threshold for refreshing session credentials */
public static final int DEFAULT_THRESHOLD_SECONDS = 500;
/** The client for starting STS sessions */
private final AWSSecurityTokenService securityTokenService;
/** The current session credentials */
private AWSSessionCredentials sessionCredentials;
/** The expiration time for the current session credentials */
private Date sessionCredentialsExpiration;
private final String wifToken;
private final String wifProvider;
private final String roleArn;
private int sessionDuration;
private int refreshThreshold;
private String subjectFromWIF;
/**
* Constructs a new WebIdentityFederationSessionCredentialsProvider, which will use the
* specified 3rd-party web identity provider to make a request to the AWS
* Security Token Service (STS) to request short lived session credentials,
* which will then be returned by this class's {@link #getCredentials()}
* method.
*
* @param wifToken
* The OAuth/OpenID token from the the Identity Provider
* @param wifProvider
* The name of the Identity Provider (null for OpenID providers)
* @param roleArn
* The ARN of the IAM Role that will be assumed
*/
public WebIdentityFederationSessionCredentialsProvider(String wifToken, String wifProvider, String roleArn) {
this(wifToken, wifProvider, roleArn, new ClientConfiguration());
}
/**
* Constructs a new WebIdentityFederationSessionCredentialsProvider, which will use the
* specified 3rd-party web identity provider to make a request to the AWS
* Security Token Service (STS) to request short lived session credentials,
* which will then be returned by this class's {@link #getCredentials()}
* method.
*
* @param wifToken
* The OAuth/OpenID token from the the Identity Provider
* @param wifProvider
* The name of the Identity Provider (null for OpenID providers)
* @param roleArn
* The ARN of the IAM Role that will be assumed
* @param clientConfiguation
* Configuration to apply to STS client created
*/
public WebIdentityFederationSessionCredentialsProvider(String wifToken, String wifProvider, String roleArn, ClientConfiguration clientConfiguration) {
this(wifToken, wifProvider, roleArn, new AWSSecurityTokenServiceClient(new AnonymousAWSCredentials(), clientConfiguration));
}
/**
* Constructs a new WebIdentityFederationSessionCredentialsProvider, which will use the
* specified 3rd-party web identity provider to make a request to the AWS
* Security Token Service (STS) using the provided client to request short
* lived session credentials, which will then be returned by this class's
* {@link #getCredentials()} method.
*
* @param wifToken
* The OAuth/OpenID token from the the Identity Provider
* @param wifProvider
* The name of the Identity Provider (null for OpenID providers)
* @param roleArn
* The ARN of the IAM Role that will be assumed
* @param stsClient
* Preconfigured STS client to make requests with
*/
public WebIdentityFederationSessionCredentialsProvider(String wifToken, String wifProvider, String roleArn, AWSSecurityTokenService stsClient) {
this.securityTokenService = stsClient;
this.wifProvider = wifProvider;
this.wifToken = wifToken;
this.roleArn = roleArn;
this.sessionDuration = DEFAULT_DURATION_SECONDS;
this.refreshThreshold = DEFAULT_THRESHOLD_SECONDS;
}
@Override
public AWSCredentials getCredentials() {
if (needsNewSession()) startSession();
return sessionCredentials;
}
@Override
public void refresh() {
startSession();
}
/**
* Set the duration of the session credentials created by this client in
* seconds. Values must be supported by AssumeRoleWithWebIdentityRequest.
*
* @see com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
*
* @param sessionDuration
* The new duration for session credentials created by this
* provider
*/
public void setSessionDuration(int sessionDuration) {
this.sessionDuration = sessionDuration;
}
/**
* Set the duration of the session credentials created by this client in
* seconds. Values must be supported by AssumeRoleWithWebIdentityRequest.
* Returns refreence to object so methods can be chained together.
*
* @see com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
*
* @param sessionDuration
* The new duration for session credentials created by this
* provider
*
* @return A reference to this updated object so that method calls
* can be chained together.
*
*/
public WebIdentityFederationSessionCredentialsProvider withSessionDuration(int sessionDuration) {
this.setSessionDuration(sessionDuration);
return this;
}
/**
* Get the duration of the session credentials created by this client in
* seconds. Values must be supported by AssumeRoleWithWebIdentityRequest.
*
* @see com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
*
* @return The duration for session credentials created by this provider
*/
public int getSessionDuration() {
return this.sessionDuration;
}
/**
* Set the refresh threshold for the session credentials created by this client in
* seconds. This value will be used internally to determine if new
* credentials should be fetched from STS.
*
* @see com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
*
* @param refreshThreshold
* The new refresh threshold for session credentials created by this
* provider
*/
public void setRefreshThreshold(int refreshThreshold) {
this.refreshThreshold = refreshThreshold;
}
/**
* Set the refresh threshold for the session credentials created by this client in
* seconds. This value will be used internally to determine if new
* credentials should be fetched from STS. Returns a refrence to the object
* so methods can be chained.
*
* @see com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
*
* @param refreshThreshold
* The new refresh threshold for session credentials created by this
* provider
*
* @return A reference to this updated object so that method calls
* can be chained together.
*
*/
public WebIdentityFederationSessionCredentialsProvider withRefreshThreshold(int refreshThreshold) {
this.setRefreshThreshold(refreshThreshold);
return this;
}
/**
* Get the refresh threshold for the session credentials created by this client in
* seconds. This value will be used internally to determine if new
* credentials should be fetched from STS.
*
* @see com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityRequest
*
* @return The refresh threshold for session credentials created by this provider
*/
public int getRefreshThreshold() {
return this.refreshThreshold;
}
/**
* Get the identifier returned from the Identity Provider for the
* authenticated user. This value is returned as part of the
* AssumeRoleWithIdentityResult
*
* @see com.amazonaws.services.securitytoken.model.AssumeRoleWithWebIdentityResult
*
* @return The identifier returned from Identity Provider
*/
public String getSubjectFromWIF() {
return this.subjectFromWIF;
}
/**
* Starts a new session by sending a request to the AWS Security Token
* Service (STS) with the long lived AWS credentials. This class then vends
* the short lived session credentials sent back from STS.
*/
private void startSession() {
AssumeRoleWithWebIdentityResult sessionTokenResult = securityTokenService
.assumeRoleWithWebIdentity(new AssumeRoleWithWebIdentityRequest().withWebIdentityToken(wifToken)
.withProviderId(wifProvider)
.withRoleArn(roleArn)
.withRoleSessionName("ProviderSession")
.withDurationSeconds(this.sessionDuration));
Credentials stsCredentials = sessionTokenResult.getCredentials();
subjectFromWIF = sessionTokenResult.getSubjectFromWebIdentityToken();
sessionCredentials = new BasicSessionCredentials(
stsCredentials.getAccessKeyId(),
stsCredentials.getSecretAccessKey(),
stsCredentials.getSessionToken());
sessionCredentialsExpiration = stsCredentials.getExpiration();
}
/**
* Returns true if a new STS session needs to be started. A new STS session
* is needed when no session has been started yet, or if the last session is
* within the configured refresh threshold.
*
* @return True if a new STS session needs to be started.
*/
private boolean needsNewSession() {
if (sessionCredentials == null) return true;
long timeRemaining = sessionCredentialsExpiration.getTime() - System.currentTimeMillis();
return timeRemaining < (this.refreshThreshold * 1000);
}
}