39
39
import com .google .common .collect .ListMultimap ;
40
40
import com .google .common .collect .Multimaps ;
41
41
import io .grpc .Attributes ;
42
- import io .grpc .CallCredentials ;
43
42
import io .grpc .CallCredentials .MetadataApplier ;
43
+ import io .grpc .CallCredentials2 ;
44
44
import io .grpc .Metadata ;
45
45
import io .grpc .MethodDescriptor ;
46
46
import io .grpc .SecurityLevel ;
@@ -105,11 +105,8 @@ public class GoogleAuthLibraryCallCredentialsTest {
105
105
.build ();
106
106
private URI expectedUri = URI .create ("https://testauthority/a.service" );
107
107
108
- private final String authority = "testauthority" ;
109
- private final Attributes attrs = Attributes .newBuilder ()
110
- .set (CallCredentials .ATTR_AUTHORITY , authority )
111
- .set (CallCredentials .ATTR_SECURITY_LEVEL , SecurityLevel .PRIVACY_AND_INTEGRITY )
112
- .build ();
108
+ private static final String AUTHORITY = "testauthority" ;
109
+ private static final SecurityLevel SECURITY_LEVEL = SecurityLevel .PRIVACY_AND_INTEGRITY ;
113
110
114
111
private ArrayList <Runnable > pendingRunnables = new ArrayList <>();
115
112
@@ -155,7 +152,7 @@ public void copyCredentialsToHeaders() throws Exception {
155
152
156
153
GoogleAuthLibraryCallCredentials callCredentials =
157
154
new GoogleAuthLibraryCallCredentials (credentials );
158
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
155
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
159
156
160
157
verify (credentials ).getRequestMetadata (eq (expectedUri ));
161
158
verify (applier ).apply (headersCaptor .capture ());
@@ -177,7 +174,7 @@ public void invalidBase64() throws Exception {
177
174
178
175
GoogleAuthLibraryCallCredentials callCredentials =
179
176
new GoogleAuthLibraryCallCredentials (credentials );
180
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
177
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
181
178
182
179
verify (credentials ).getRequestMetadata (eq (expectedUri ));
183
180
verify (applier ).fail (statusCaptor .capture ());
@@ -193,7 +190,7 @@ public void credentialsFailsWithIoException() throws Exception {
193
190
194
191
GoogleAuthLibraryCallCredentials callCredentials =
195
192
new GoogleAuthLibraryCallCredentials (credentials );
196
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
193
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
197
194
198
195
verify (credentials ).getRequestMetadata (eq (expectedUri ));
199
196
verify (applier ).fail (statusCaptor .capture ());
@@ -209,7 +206,7 @@ public void credentialsFailsWithRuntimeException() throws Exception {
209
206
210
207
GoogleAuthLibraryCallCredentials callCredentials =
211
208
new GoogleAuthLibraryCallCredentials (credentials );
212
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
209
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
213
210
214
211
verify (credentials ).getRequestMetadata (eq (expectedUri ));
215
212
verify (applier ).fail (statusCaptor .capture ());
@@ -229,7 +226,7 @@ public void credentialsReturnNullMetadata() throws Exception {
229
226
GoogleAuthLibraryCallCredentials callCredentials =
230
227
new GoogleAuthLibraryCallCredentials (credentials );
231
228
for (int i = 0 ; i < 3 ; i ++) {
232
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
229
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
233
230
}
234
231
235
232
verify (credentials , times (3 )).getRequestMetadata (eq (expectedUri ));
@@ -255,14 +252,11 @@ public AccessToken refreshAccessToken() throws IOException {
255
252
return token ;
256
253
}
257
254
};
258
- // Security level should not impact non-GoogleCredentials
259
- Attributes securityNone = attrs .toBuilder ()
260
- .set (CallCredentials .ATTR_SECURITY_LEVEL , SecurityLevel .NONE )
261
- .build ();
262
255
263
256
GoogleAuthLibraryCallCredentials callCredentials =
264
257
new GoogleAuthLibraryCallCredentials (credentials );
265
- callCredentials .applyRequestMetadata (method , securityNone , executor , applier );
258
+ callCredentials .applyRequestMetadata (
259
+ new RequestInfoImpl (SecurityLevel .NONE ), executor , applier );
266
260
assertEquals (1 , runPendingRunnables ());
267
261
268
262
verify (applier ).apply (headersCaptor .capture ());
@@ -276,13 +270,11 @@ public AccessToken refreshAccessToken() throws IOException {
276
270
public void googleCredential_privacyAndIntegrityAllowed () {
277
271
final AccessToken token = new AccessToken ("allyourbase" , new Date (Long .MAX_VALUE ));
278
272
final Credentials credentials = GoogleCredentials .create (token );
279
- Attributes privacy = attrs .toBuilder ()
280
- .set (CallCredentials .ATTR_SECURITY_LEVEL , SecurityLevel .PRIVACY_AND_INTEGRITY )
281
- .build ();
282
273
283
274
GoogleAuthLibraryCallCredentials callCredentials =
284
275
new GoogleAuthLibraryCallCredentials (credentials );
285
- callCredentials .applyRequestMetadata (method , privacy , executor , applier );
276
+ callCredentials .applyRequestMetadata (
277
+ new RequestInfoImpl (SecurityLevel .PRIVACY_AND_INTEGRITY ), executor , applier );
286
278
runPendingRunnables ();
287
279
288
280
verify (applier ).apply (headersCaptor .capture ());
@@ -297,33 +289,11 @@ public void googleCredential_integrityDenied() {
297
289
final AccessToken token = new AccessToken ("allyourbase" , new Date (Long .MAX_VALUE ));
298
290
final Credentials credentials = GoogleCredentials .create (token );
299
291
// Anything less than PRIVACY_AND_INTEGRITY should fail
300
- Attributes integrity = attrs .toBuilder ()
301
- .set (CallCredentials .ATTR_SECURITY_LEVEL , SecurityLevel .INTEGRITY )
302
- .build ();
303
-
304
- GoogleAuthLibraryCallCredentials callCredentials =
305
- new GoogleAuthLibraryCallCredentials (credentials );
306
- callCredentials .applyRequestMetadata (method , integrity , executor , applier );
307
- runPendingRunnables ();
308
-
309
- verify (applier ).fail (statusCaptor .capture ());
310
- Status status = statusCaptor .getValue ();
311
- assertEquals (Status .Code .UNAUTHENTICATED , status .getCode ());
312
- }
313
-
314
- @ Test
315
- public void googleCredential_nullSecurityDenied () {
316
- final AccessToken token = new AccessToken ("allyourbase" , new Date (Long .MAX_VALUE ));
317
- final Credentials credentials = GoogleCredentials .create (token );
318
- // Null should not (for the moment) crash in horrible ways. In the future this could be changed,
319
- // since it technically isn't allowed per the API.
320
- Attributes integrity = attrs .toBuilder ()
321
- .set (CallCredentials .ATTR_SECURITY_LEVEL , null )
322
- .build ();
323
292
324
293
GoogleAuthLibraryCallCredentials callCredentials =
325
294
new GoogleAuthLibraryCallCredentials (credentials );
326
- callCredentials .applyRequestMetadata (method , integrity , executor , applier );
295
+ callCredentials .applyRequestMetadata (
296
+ new RequestInfoImpl (SecurityLevel .INTEGRITY ), executor , applier );
327
297
runPendingRunnables ();
328
298
329
299
verify (applier ).fail (statusCaptor .capture ());
@@ -335,20 +305,12 @@ public void googleCredential_nullSecurityDenied() {
335
305
public void serviceUri () throws Exception {
336
306
GoogleAuthLibraryCallCredentials callCredentials =
337
307
new GoogleAuthLibraryCallCredentials (credentials );
338
- callCredentials .applyRequestMetadata (method ,
339
- Attributes .newBuilder ()
340
- .setAll (attrs )
341
- .set (CallCredentials .ATTR_AUTHORITY , "example.com:443" )
342
- .build (),
343
- executor , applier );
308
+ callCredentials .applyRequestMetadata (
309
+ new RequestInfoImpl ("example.com:443" ), executor , applier );
344
310
verify (credentials ).getRequestMetadata (eq (new URI ("https://example.com/a.service" )));
345
311
346
- callCredentials .applyRequestMetadata (method ,
347
- Attributes .newBuilder ()
348
- .setAll (attrs )
349
- .set (CallCredentials .ATTR_AUTHORITY , "example.com:123" )
350
- .build (),
351
- executor , applier );
312
+ callCredentials .applyRequestMetadata (
313
+ new RequestInfoImpl ("example.com:123" ), executor , applier );
352
314
verify (credentials ).getRequestMetadata (eq (new URI ("https://example.com:123/a.service" )));
353
315
}
354
316
@@ -366,7 +328,7 @@ public AccessToken refreshAccessToken() {
366
328
367
329
GoogleAuthLibraryCallCredentials callCredentials =
368
330
new GoogleAuthLibraryCallCredentials (credentials );
369
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
331
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
370
332
assertEquals (0 , runPendingRunnables ());
371
333
372
334
verify (applier ).apply (headersCaptor .capture ());
@@ -393,7 +355,7 @@ public AccessToken refreshAccessToken() {
393
355
394
356
GoogleAuthLibraryCallCredentials callCredentials =
395
357
new GoogleAuthLibraryCallCredentials (credentials );
396
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
358
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
397
359
assertEquals (1 , runPendingRunnables ());
398
360
399
361
verify (applier ).apply (headersCaptor .capture ());
@@ -412,7 +374,7 @@ public void oauthClassesNotInClassPath() throws Exception {
412
374
assertNull (GoogleAuthLibraryCallCredentials .createJwtHelperOrNull (null ));
413
375
GoogleAuthLibraryCallCredentials callCredentials =
414
376
new GoogleAuthLibraryCallCredentials (credentials , null );
415
- callCredentials .applyRequestMetadata (method , attrs , executor , applier );
377
+ callCredentials .applyRequestMetadata (new RequestInfoImpl () , executor , applier );
416
378
417
379
verify (credentials ).getRequestMetadata (eq (expectedUri ));
418
380
verify (applier ).apply (headersCaptor .capture ());
@@ -430,4 +392,46 @@ private int runPendingRunnables() {
430
392
}
431
393
return savedPendingRunnables .size ();
432
394
}
395
+
396
+ private final class RequestInfoImpl extends CallCredentials2 .RequestInfo {
397
+ final String authority ;
398
+ final SecurityLevel securityLevel ;
399
+
400
+ RequestInfoImpl () {
401
+ this (AUTHORITY , SECURITY_LEVEL );
402
+ }
403
+
404
+ RequestInfoImpl (SecurityLevel securityLevel ) {
405
+ this (AUTHORITY , securityLevel );
406
+ }
407
+
408
+ RequestInfoImpl (String authority ) {
409
+ this (authority , SECURITY_LEVEL );
410
+ }
411
+
412
+ RequestInfoImpl (String authority , SecurityLevel securityLevel ) {
413
+ this .authority = authority ;
414
+ this .securityLevel = securityLevel ;
415
+ }
416
+
417
+ @ Override
418
+ public MethodDescriptor <?, ?> getMethodDescriptor () {
419
+ return method ;
420
+ }
421
+
422
+ @ Override
423
+ public SecurityLevel getSecurityLevel () {
424
+ return securityLevel ;
425
+ }
426
+
427
+ @ Override
428
+ public String getAuthority () {
429
+ return authority ;
430
+ }
431
+
432
+ @ Override
433
+ public Attributes getTransportAttrs () {
434
+ return Attributes .EMPTY ;
435
+ }
436
+ }
433
437
}
0 commit comments