@@ -109,6 +109,68 @@ public class ComputeEngineCredentials extends GoogleCredentials
109109 static final int MAX_COMPUTE_PING_TRIES = 3 ;
110110 static final int COMPUTE_PING_CONNECTION_TIMEOUT_MS = 500 ;
111111
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+
112174 private static final String METADATA_FLAVOR = "Metadata-Flavor" ;
113175 private static final String GOOGLE = "Google" ;
114176 private static final String WINDOWS = "windows" ;
@@ -122,6 +184,9 @@ public class ComputeEngineCredentials extends GoogleCredentials
122184
123185 private final Collection <String > scopes ;
124186
187+ private final GoogleAuthTransport transport ;
188+ private final BindingEnforcement bindingEnforcement ;
189+
125190 private transient HttpTransportFactory transportFactory ;
126191 private transient String serviceAccountEmail ;
127192
@@ -152,6 +217,8 @@ private ComputeEngineCredentials(ComputeEngineCredentials.Builder builder) {
152217 scopeList .removeAll (Arrays .asList ("" , null ));
153218 this .scopes = ImmutableSet .<String >copyOf (scopeList );
154219 }
220+ this .transport = builder .getGoogleAuthTransport ();
221+ this .bindingEnforcement = builder .getBindingEnforcement ();
155222 }
156223
157224 @ Override
@@ -191,7 +258,10 @@ public final Collection<String> getScopes() {
191258 }
192259
193260 /**
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".
195265 *
196266 * @return token url with the given scopes
197267 */
@@ -200,6 +270,12 @@ String createTokenUrlWithScopes() {
200270 if (!scopes .isEmpty ()) {
201271 tokenUrl .set ("scopes" , Joiner .on (',' ).join (scopes ));
202272 }
273+ if (transport != null ) {
274+ tokenUrl .set ("transport" , transport .getLabel ());
275+ }
276+ if (bindingEnforcement != null ) {
277+ tokenUrl .set ("binding-enforcement" , bindingEnforcement .getLabel ());
278+ }
203279 return tokenUrl .toString ();
204280 }
205281
@@ -643,6 +719,9 @@ public static class Builder extends GoogleCredentials.Builder {
643719 private Collection <String > scopes ;
644720 private Collection <String > defaultScopes ;
645721
722+ private GoogleAuthTransport transport ;
723+ private BindingEnforcement bindingEnforcement ;
724+
646725 protected Builder () {
647726 setRefreshMargin (COMPUTE_REFRESH_MARGIN );
648727 setExpirationMargin (COMPUTE_EXPIRATION_MARGIN );
@@ -684,6 +763,28 @@ public Builder setQuotaProjectId(String quotaProjectId) {
684763 return this ;
685764 }
686765
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+
687788 public HttpTransportFactory getHttpTransportFactory () {
688789 return transportFactory ;
689790 }
@@ -696,6 +797,24 @@ public Collection<String> getDefaultScopes() {
696797 return defaultScopes ;
697798 }
698799
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+
699818 @ Override
700819 public ComputeEngineCredentials build () {
701820 return new ComputeEngineCredentials (this );
0 commit comments