@@ -109,6 +109,68 @@ public class ComputeEngineCredentials extends GoogleCredentials
109
109
static final int MAX_COMPUTE_PING_TRIES = 3 ;
110
110
static final int COMPUTE_PING_CONNECTION_TIMEOUT_MS = 500 ;
111
111
112
+ /**
113
+ * Experimental Feature.
114
+ *
115
+ * <p>{@link GoogleAuthTransport} specifies how to authenticate to Google APIs.
116
+ *
117
+ * <p>Behavior of setting {@link GoogleAuthTransport} / {@link BindingEnforcement}:
118
+ *
119
+ * <p>MTLS-bound token where binding enforcement depends on IAM policy: MTLS / {}, {} /
120
+ * IAM_POLICY, MTLS / IAM_POLICY
121
+ *
122
+ * <p>MTLS-bound token where bindings are always enforced: {} / ON, MTLS / ON
123
+ *
124
+ * <p>DirectPath bound token: ALTS / {}
125
+ */
126
+ public enum GoogleAuthTransport {
127
+ // Authenticating to Google APIs via DirectPath
128
+ ALTS ("alts" ),
129
+ // Authenticating to Google APIs via GFE
130
+ MTLS ("mtls" );
131
+
132
+ private final String label ;
133
+
134
+ private GoogleAuthTransport (String label ) {
135
+ this .label = label ;
136
+ }
137
+
138
+ public String getLabel () {
139
+ return label ;
140
+ }
141
+ }
142
+
143
+ /**
144
+ * Experimental Feature.
145
+ *
146
+ * <p>{@link BindingEnforcement} specifies how binding info in tokens will be enforced.
147
+ *
148
+ * <p>Behavior of setting {@link GoogleAuthTransport} / {@link BindingEnforcement}:
149
+ *
150
+ * <p>MTLS-bound token where binding enforcement depends on IAM policy: MTLS / {}, {} /
151
+ * IAM_POLICY, MTLS / IAM_POLICY
152
+ *
153
+ * <p>MTLS-bound token where bindings are always enforced: {} / ON, MTLS / ON
154
+ *
155
+ * <p>DirectPath bound token: ALTS / {}
156
+ */
157
+ public enum BindingEnforcement {
158
+ // Binding enforcement will always happen, irrespective of the IAM policy.
159
+ ON ("on" ),
160
+ // Binding enforcement will depend on IAM policy.
161
+ IAM_POLICY ("iam-policy" );
162
+
163
+ private final String label ;
164
+
165
+ private BindingEnforcement (String label ) {
166
+ this .label = label ;
167
+ }
168
+
169
+ public String getLabel () {
170
+ return label ;
171
+ }
172
+ }
173
+
112
174
private static final String METADATA_FLAVOR = "Metadata-Flavor" ;
113
175
private static final String GOOGLE = "Google" ;
114
176
private static final String WINDOWS = "windows" ;
@@ -122,6 +184,9 @@ public class ComputeEngineCredentials extends GoogleCredentials
122
184
123
185
private final Collection <String > scopes ;
124
186
187
+ private final GoogleAuthTransport transport ;
188
+ private final BindingEnforcement bindingEnforcement ;
189
+
125
190
private transient HttpTransportFactory transportFactory ;
126
191
private transient String serviceAccountEmail ;
127
192
@@ -152,6 +217,8 @@ private ComputeEngineCredentials(ComputeEngineCredentials.Builder builder) {
152
217
scopeList .removeAll (Arrays .asList ("" , null ));
153
218
this .scopes = ImmutableSet .<String >copyOf (scopeList );
154
219
}
220
+ this .transport = builder .getGoogleAuthTransport ();
221
+ this .bindingEnforcement = builder .getBindingEnforcement ();
155
222
}
156
223
157
224
@ Override
@@ -191,7 +258,10 @@ public final Collection<String> getScopes() {
191
258
}
192
259
193
260
/**
194
- * If scopes is specified, add "?scopes=comma-separated-list-of-scopes" to the token url.
261
+ * If scopes is specified, add "?scopes=comma-separated-list-of-scopes" to the token url. If
262
+ * transport is specified, add "?transport=xyz" to the token url; xyz is one of "alts" or "mtls".
263
+ * If bindingEnforcement is specified, add "?binding-enforcement=xyz" to the token url; xyz is one
264
+ * of "iam-policy" or "on".
195
265
*
196
266
* @return token url with the given scopes
197
267
*/
@@ -200,6 +270,12 @@ String createTokenUrlWithScopes() {
200
270
if (!scopes .isEmpty ()) {
201
271
tokenUrl .set ("scopes" , Joiner .on (',' ).join (scopes ));
202
272
}
273
+ if (transport != null ) {
274
+ tokenUrl .set ("transport" , transport .getLabel ());
275
+ }
276
+ if (bindingEnforcement != null ) {
277
+ tokenUrl .set ("binding-enforcement" , bindingEnforcement .getLabel ());
278
+ }
203
279
return tokenUrl .toString ();
204
280
}
205
281
@@ -643,6 +719,9 @@ public static class Builder extends GoogleCredentials.Builder {
643
719
private Collection <String > scopes ;
644
720
private Collection <String > defaultScopes ;
645
721
722
+ private GoogleAuthTransport transport ;
723
+ private BindingEnforcement bindingEnforcement ;
724
+
646
725
protected Builder () {
647
726
setRefreshMargin (COMPUTE_REFRESH_MARGIN );
648
727
setExpirationMargin (COMPUTE_EXPIRATION_MARGIN );
@@ -684,6 +763,28 @@ public Builder setQuotaProjectId(String quotaProjectId) {
684
763
return this ;
685
764
}
686
765
766
+ /**
767
+ * Set the {@code GoogleAuthTransport} type.
768
+ *
769
+ * @param transport the transport type over which to authenticate to Google APIs
770
+ */
771
+ @ CanIgnoreReturnValue
772
+ public Builder setGoogleAuthTransport (GoogleAuthTransport transport ) {
773
+ this .transport = transport ;
774
+ return this ;
775
+ }
776
+
777
+ /**
778
+ * Set the {@code BindingEnforcement} type.
779
+ *
780
+ * @param bindingEnforcement the token binding enforcement policy.
781
+ */
782
+ @ CanIgnoreReturnValue
783
+ public Builder setBindingEnforcement (BindingEnforcement bindingEnforcement ) {
784
+ this .bindingEnforcement = bindingEnforcement ;
785
+ return this ;
786
+ }
787
+
687
788
public HttpTransportFactory getHttpTransportFactory () {
688
789
return transportFactory ;
689
790
}
@@ -696,6 +797,24 @@ public Collection<String> getDefaultScopes() {
696
797
return defaultScopes ;
697
798
}
698
799
800
+ /**
801
+ * Get the {@code GoogleAuthTransport} type.
802
+ *
803
+ * @return the transport type over which to authenticate to Google APIs
804
+ */
805
+ public GoogleAuthTransport getGoogleAuthTransport () {
806
+ return transport ;
807
+ }
808
+
809
+ /**
810
+ * Get the {@code BindingEnforcement} type.
811
+ *
812
+ * @return the token binding enforcement policy.
813
+ */
814
+ public BindingEnforcement getBindingEnforcement () {
815
+ return bindingEnforcement ;
816
+ }
817
+
699
818
@ Override
700
819
public ComputeEngineCredentials build () {
701
820
return new ComputeEngineCredentials (this );
0 commit comments