33
33
34
34
import static com .google .common .base .Preconditions .checkNotNull ;
35
35
36
+ import com .google .api .client .http .HttpHeaders ;
36
37
import com .google .api .client .json .GenericJson ;
37
38
import com .google .api .client .json .JsonObjectParser ;
38
39
import com .google .auth .RequestMetadataCallback ;
@@ -90,6 +91,7 @@ abstract static class CredentialSource implements java.io.Serializable {
90
91
private final CredentialSource credentialSource ;
91
92
private final Collection <String > scopes ;
92
93
private final ServiceAccountImpersonationOptions serviceAccountImpersonationOptions ;
94
+ private ExternalAccountMetricsHandler metricsHandler ;
93
95
94
96
@ Nullable private final String tokenInfoUrl ;
95
97
@ Nullable private final String serviceAccountImpersonationUrl ;
@@ -224,6 +226,8 @@ protected ExternalAccountCredentials(
224
226
validateServiceAccountImpersonationInfoUrl (serviceAccountImpersonationUrl );
225
227
}
226
228
229
+ this .metricsHandler = new ExternalAccountMetricsHandler (this );
230
+
227
231
this .impersonatedCredentials = buildImpersonatedCredentials ();
228
232
}
229
233
@@ -274,6 +278,11 @@ protected ExternalAccountCredentials(ExternalAccountCredentials.Builder builder)
274
278
validateServiceAccountImpersonationInfoUrl (serviceAccountImpersonationUrl );
275
279
}
276
280
281
+ this .metricsHandler =
282
+ builder .metricsHandler == null
283
+ ? new ExternalAccountMetricsHandler (this )
284
+ : builder .metricsHandler ;
285
+
277
286
this .impersonatedCredentials = buildImpersonatedCredentials ();
278
287
}
279
288
@@ -505,6 +514,12 @@ protected AccessToken exchangeExternalCredentialForAccessToken(
505
514
requestHandler .setInternalOptions (options .toString ());
506
515
}
507
516
517
+ // Set BYOID Metrics header.
518
+ HttpHeaders additionalHeaders = new HttpHeaders ();
519
+ additionalHeaders .set (
520
+ MetricsUtils .API_CLIENT_HEADER , this .metricsHandler .getExternalAccountMetricsHeader ());
521
+ requestHandler .setHeaders (additionalHeaders );
522
+
508
523
if (stsTokenExchangeRequest .getInternalOptions () != null ) {
509
524
// Overwrite internal options. Let subclass handle setting options.
510
525
requestHandler .setInternalOptions (stsTokenExchangeRequest .getInternalOptions ());
@@ -589,6 +604,10 @@ public ServiceAccountImpersonationOptions getServiceAccountImpersonationOptions(
589
604
return serviceAccountImpersonationOptions ;
590
605
}
591
606
607
+ String getCredentialSourceType () {
608
+ return "unknown" ;
609
+ }
610
+
592
611
EnvironmentProvider getEnvironmentProvider () {
593
612
return environmentProvider ;
594
613
}
@@ -663,8 +682,11 @@ static final class ServiceAccountImpersonationOptions implements java.io.Seriali
663
682
664
683
private final int lifetime ;
665
684
685
+ final boolean customTokenLifetimeRequested ;
686
+
666
687
ServiceAccountImpersonationOptions (Map <String , Object > optionsMap ) {
667
- if (!optionsMap .containsKey (TOKEN_LIFETIME_SECONDS_KEY )) {
688
+ customTokenLifetimeRequested = optionsMap .containsKey (TOKEN_LIFETIME_SECONDS_KEY );
689
+ if (!customTokenLifetimeRequested ) {
668
690
lifetime = DEFAULT_TOKEN_LIFETIME_SECONDS ;
669
691
return ;
670
692
}
@@ -714,6 +736,7 @@ public abstract static class Builder extends GoogleCredentials.Builder {
714
736
@ Nullable protected String workforcePoolUserProject ;
715
737
@ Nullable protected ServiceAccountImpersonationOptions serviceAccountImpersonationOptions ;
716
738
@ Nullable protected String universeDomain ;
739
+ @ Nullable protected ExternalAccountMetricsHandler metricsHandler ;
717
740
718
741
protected Builder () {}
719
742
@@ -733,6 +756,7 @@ protected Builder(ExternalAccountCredentials credentials) {
733
756
this .workforcePoolUserProject = credentials .workforcePoolUserProject ;
734
757
this .serviceAccountImpersonationOptions = credentials .serviceAccountImpersonationOptions ;
735
758
this .universeDomain = credentials .universeDomain ;
759
+ this .metricsHandler = credentials .metricsHandler ;
736
760
}
737
761
738
762
/**
0 commit comments