Repository navigation
Expand file tree
/
Copy pathDownscopedCredentialsTest.java
More file actions
373 lines (313 loc) · 16.2 KB
/
Copy pathDownscopedCredentialsTest.java
File metadata and controls
373 lines (313 loc) · 16.2 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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/*
* Copyright 2021 Google LLC
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following disclaimer
* in the documentation and/or other materials provided with the
* distribution.
*
* * Neither the name of Google LLC nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package com.google.auth.oauth2;
import static com.google.auth.Credentials.GOOGLE_DEFAULT_UNIVERSE;
import static com.google.auth.oauth2.OAuth2Utils.TOKEN_EXCHANGE_URL_FORMAT;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import com.google.api.client.http.HttpTransport;
import com.google.auth.TestUtils;
import com.google.auth.http.HttpTransportFactory;
import java.io.IOException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.junit.jupiter.api.Test;
/** Tests for {@link DownscopedCredentials}. */
class DownscopedCredentialsTest extends BaseSerializationTest {
private static final String SA_PRIVATE_KEY_PKCS8 =
"-----BEGIN PRIVATE KEY-----\n"
+ "MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBALX0PQoe1igW12i"
+ "kv1bN/r9lN749y2ijmbc/mFHPyS3hNTyOCjDvBbXYbDhQJzWVUikh4mvGBA07qTj79Xc3yBDfKP2IeyYQIFe0t0"
+ "zkd7R9Zdn98Y2rIQC47aAbDfubtkU1U72t4zL11kHvoa0/RuFZjncvlr42X7be7lYh4p3NAgMBAAECgYASk5wDw"
+ "4Az2ZkmeuN6Fk/y9H+Lcb2pskJIXjrL533vrDWGOC48LrsThMQPv8cxBky8HFSEklPpkfTF95tpD43iVwJRB/Gr"
+ "CtGTw65IfJ4/tI09h6zGc4yqvIo1cHX/LQ+SxKLGyir/dQM925rGt/VojxY5ryJR7GLbCzxPnJm/oQJBANwOCO6"
+ "D2hy1LQYJhXh7O+RLtA/tSnT1xyMQsGT+uUCMiKS2bSKx2wxo9k7h3OegNJIu1q6nZ6AbxDK8H3+d0dUCQQDTrP"
+ "SXagBxzp8PecbaCHjzNRSQE2in81qYnrAFNB4o3DpHyMMY6s5ALLeHKscEWnqP8Ur6X4PvzZecCWU9BKAZAkAut"
+ "LPknAuxSCsUOvUfS1i87ex77Ot+w6POp34pEX+UWb+u5iFn2cQacDTHLV1LtE80L8jVLSbrbrlH43H0DjU5AkEA"
+ "gidhycxS86dxpEljnOMCw8CKoUBd5I880IUahEiUltk7OLJYS/Ts1wbn3kPOVX3wyJs8WBDtBkFrDHW2ezth2QJ"
+ "ADj3e1YhMVdjJW5jqwlD/VNddGjgzyunmiZg0uOXsHXbytYmsA545S8KRQFaJKFXYYFo2kOjqOiC1T2cAzMDjCQ"
+ "==\n-----END PRIVATE KEY-----\n";
private static final CredentialAccessBoundary CREDENTIAL_ACCESS_BOUNDARY =
CredentialAccessBoundary.newBuilder()
.addRule(
CredentialAccessBoundary.AccessBoundaryRule.newBuilder()
.setAvailableResource("//storage.googleapis.com/projects/_/buckets/bucket")
.addAvailablePermission("inRole:roles/storage.objectViewer")
.build())
.build();
static class MockStsTransportFactory implements HttpTransportFactory {
MockStsTransport transport = new MockStsTransport();
@Override
public HttpTransport create() {
return transport;
}
}
public static class StatefulMockStsTransportFactory implements HttpTransportFactory {
private static MockStsTransport transport = new MockStsTransport();
public StatefulMockStsTransportFactory() {}
@Override
public HttpTransport create() {
return transport;
}
}
@Test
void refreshAccessToken_reserialized_success() throws Exception {
StatefulMockStsTransportFactory.transport = new MockStsTransport();
StatefulMockStsTransportFactory transportFactory = new StatefulMockStsTransportFactory();
// Set token expiration to 1 hour (3_600_000 ms) in the future so that
// sourceCredential.refreshIfExpired() considers the source token valid and
// does not attempt an external network call to refresh the source credential.
long oneHourInMillis = TimeUnit.HOURS.toMillis(1);
AccessToken sourceAccessToken =
new AccessToken(
"sourceAccessToken", new Date(System.currentTimeMillis() + oneHourInMillis));
GoogleCredentials sourceCredentials =
((ServiceAccountCredentials) getServiceAccountSourceCredentials(/* canRefresh= */ true))
.toBuilder().setAccessToken(sourceAccessToken).build();
DownscopedCredentials downscopedCredentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setHttpTransportFactory(transportFactory)
.build();
// Verify deserialization succeeds and reconstructs the transient HTTP transport factory
// so that subsequent token refreshes do not throw a NullPointerException.
DownscopedCredentials deserialized = serializeAndDeserialize(downscopedCredentials);
assertNotNull(deserialized.getTransportFactory());
assertEquals(downscopedCredentials, deserialized);
assertEquals(downscopedCredentials.hashCode(), deserialized.hashCode());
// In-memory mock transport responds with a valid token exchange without network calls.
AccessToken accessToken = deserialized.refreshAccessToken();
assertEquals(
StatefulMockStsTransportFactory.transport.getAccessToken(), accessToken.getTokenValue());
}
@Test
void refreshAccessToken() throws IOException {
MockStsTransportFactory transportFactory = new MockStsTransportFactory();
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ true);
DownscopedCredentials downscopedCredentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setHttpTransportFactory(transportFactory)
.build();
AccessToken accessToken = downscopedCredentials.refreshAccessToken();
assertEquals(transportFactory.transport.getAccessToken(), accessToken.getTokenValue());
// Validate CAB specific params.
Map<String, String> query =
TestUtils.parseQuery(transportFactory.transport.getRequest().getContentAsString());
assertNotNull(query.get("options"));
assertEquals(CREDENTIAL_ACCESS_BOUNDARY.toJson(), query.get("options"));
assertEquals(
"urn:ietf:params:oauth:token-type:access_token", query.get("requested_token_type"));
// Verify domain.
String url = transportFactory.transport.getRequest().getUrl();
assertEquals(url, String.format(TOKEN_EXCHANGE_URL_FORMAT, GOOGLE_DEFAULT_UNIVERSE));
}
@Test
void refreshAccessToken_withCustomUniverseDomain() throws IOException {
MockStsTransportFactory transportFactory = new MockStsTransportFactory();
String universeDomain = "foobar";
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ true).toBuilder()
.setUniverseDomain(universeDomain)
.build();
DownscopedCredentials downscopedCredentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setHttpTransportFactory(transportFactory)
.setUniverseDomain(universeDomain)
.build();
AccessToken accessToken = downscopedCredentials.refreshAccessToken();
assertEquals(transportFactory.transport.getAccessToken(), accessToken.getTokenValue());
// Validate CAB specific params.
Map<String, String> query =
TestUtils.parseQuery(transportFactory.transport.getRequest().getContentAsString());
assertNotNull(query.get("options"));
assertEquals(CREDENTIAL_ACCESS_BOUNDARY.toJson(), query.get("options"));
assertEquals(
"urn:ietf:params:oauth:token-type:access_token", query.get("requested_token_type"));
// Verify domain.
String url = transportFactory.transport.getRequest().getUrl();
assertEquals(url, String.format(TOKEN_EXCHANGE_URL_FORMAT, universeDomain));
}
@Test
void refreshAccessToken_userCredentials_expectExpiresInCopied() throws IOException {
// STS only returns expires_in if the source access token belongs to a service account.
// For other source credential types, we can copy the source credentials expiration as
// the generated downscoped token will always have the same expiration time as the source
// credentials.
MockStsTransportFactory transportFactory = new MockStsTransportFactory();
transportFactory.transport.setReturnExpiresIn(false);
GoogleCredentials sourceCredentials = getUserSourceCredentials();
DownscopedCredentials downscopedCredentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setHttpTransportFactory(transportFactory)
.build();
AccessToken accessToken = downscopedCredentials.refreshAccessToken();
assertEquals(transportFactory.transport.getAccessToken(), accessToken.getTokenValue());
// Validate that the expires_in has been copied from the source credential.
assertEquals(
sourceCredentials.getAccessToken().getExpirationTime(), accessToken.getExpirationTime());
}
@Test
void refreshAccessToken_cantRefreshSourceCredentials_throws() throws IOException {
MockStsTransportFactory transportFactory = new MockStsTransportFactory();
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ false);
DownscopedCredentials downscopedCredentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setHttpTransportFactory(transportFactory)
.build();
IOException e = assertThrows(IOException.class, downscopedCredentials::refreshAccessToken);
assertEquals("Unable to refresh the provided source credential.", e.getMessage());
}
@Test
void builder_noSourceCredential_throws() {
DownscopedCredentials.Builder builder =
DownscopedCredentials.newBuilder()
.setHttpTransportFactory(OAuth2Utils.HTTP_TRANSPORT_FACTORY);
assertThrows(NullPointerException.class, builder::build);
}
@Test
void builder_noCredentialAccessBoundary_throws() throws IOException {
DownscopedCredentials.Builder builder =
DownscopedCredentials.newBuilder()
.setHttpTransportFactory(OAuth2Utils.HTTP_TRANSPORT_FACTORY)
.setSourceCredential(getServiceAccountSourceCredentials(/* canRefresh= */ true));
assertThrows(NullPointerException.class, builder::build);
}
@Test
void builder_noTransport_defaults() throws IOException {
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ true);
DownscopedCredentials credentials =
DownscopedCredentials.newBuilder()
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.build();
GoogleCredentials scopedSourceCredentials =
sourceCredentials.createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
assertEquals(scopedSourceCredentials, credentials.getSourceCredentials());
assertEquals(CREDENTIAL_ACCESS_BOUNDARY, credentials.getCredentialAccessBoundary());
assertEquals(OAuth2Utils.HTTP_TRANSPORT_FACTORY, credentials.getTransportFactory());
}
@Test
void builder_noUniverseDomain_defaults() throws IOException {
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ true);
DownscopedCredentials credentials =
DownscopedCredentials.newBuilder()
.setHttpTransportFactory(OAuth2Utils.HTTP_TRANSPORT_FACTORY)
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.build();
GoogleCredentials scopedSourceCredentials =
sourceCredentials.createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
assertEquals(OAuth2Utils.HTTP_TRANSPORT_FACTORY, credentials.getTransportFactory());
assertEquals(scopedSourceCredentials, credentials.getSourceCredentials());
assertEquals(CREDENTIAL_ACCESS_BOUNDARY, credentials.getCredentialAccessBoundary());
assertEquals(GOOGLE_DEFAULT_UNIVERSE, credentials.getUniverseDomain());
}
@Test
void builder_universeDomainMismatch_throws() throws IOException {
GoogleCredentials sourceCredentials =
getServiceAccountSourceCredentials(/* canRefresh= */ true);
DownscopedCredentials.Builder builder =
DownscopedCredentials.newBuilder()
.setHttpTransportFactory(OAuth2Utils.HTTP_TRANSPORT_FACTORY)
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY)
.setUniverseDomain("differentUniverseDomain");
IllegalArgumentException e = assertThrows(IllegalArgumentException.class, builder::build);
assertEquals(
"The downscoped credential's universe domain must be the same as the source credential.",
e.getMessage());
}
@Test
void builder_sourceUniverseDomainUnavailable_throws() {
GoogleCredentials sourceCredentials = new MockSourceCredentialWithoutUniverseDomain();
DownscopedCredentials.Builder builder =
DownscopedCredentials.newBuilder()
.setHttpTransportFactory(OAuth2Utils.HTTP_TRANSPORT_FACTORY)
.setSourceCredential(sourceCredentials)
.setCredentialAccessBoundary(CREDENTIAL_ACCESS_BOUNDARY);
IllegalStateException e = assertThrows(IllegalStateException.class, builder::build);
assertEquals(
"Error occurred when attempting to retrieve source credential universe domain.",
e.getMessage());
}
private static GoogleCredentials getServiceAccountSourceCredentials(boolean canRefresh)
throws IOException {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
String email = "service-account@google.com";
ServiceAccountCredentials sourceCredentials =
ServiceAccountCredentials.newBuilder()
.setClientEmail(email)
.setPrivateKey(OAuth2Utils.privateKeyFromPkcs8(SA_PRIVATE_KEY_PKCS8))
.setPrivateKeyId("privateKeyId")
.setProjectId("projectId")
.setHttpTransportFactory(transportFactory)
.build();
transportFactory.transport.addServiceAccount(email, "accessToken");
if (!canRefresh) {
transportFactory.transport.setError(new IOException());
}
return sourceCredentials.createScoped(OAuth2Utils.CLOUD_PLATFORM_SCOPE);
}
private static GoogleCredentials getUserSourceCredentials() {
MockTokenServerTransportFactory transportFactory = new MockTokenServerTransportFactory();
transportFactory.transport.addClient("clientId", "clientSecret");
transportFactory.transport.addRefreshToken("refreshToken", "accessToken");
AccessToken accessToken = new AccessToken("accessToken", new Date());
return UserCredentials.newBuilder()
.setClientId("clientId")
.setClientSecret("clientSecret")
.setRefreshToken("refreshToken")
.setAccessToken(accessToken)
.setHttpTransportFactory(transportFactory)
.build();
}
static class MockSourceCredentialWithoutUniverseDomain extends GoogleCredentials {
@Override
public String getUniverseDomain() throws IOException {
throw new IOException();
}
}
}