Skip to content

Commit 5494f0c

Browse files
committed
chore: Fix MDS check
1 parent b3a2634 commit 5494f0c

File tree

4 files changed

+98
-79
lines changed

4 files changed

+98
-79
lines changed

oauth2_http/java/com/google/auth/oauth2/ComputeEngineCredentials.java

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,8 @@ private ComputeEngineCredentials(ComputeEngineCredentials.Builder builder) {
222222
this.bindingEnforcement = builder.getBindingEnforcement();
223223
this.name = GoogleCredentialsInfo.COMPUTE_ENGINE_CREDENTIALS.getCredentialName();
224224
try {
225-
// Only do a passive check for MDS (do not make an MDS ping check during initialization)
226-
if (checkStaticGceDetection(new DefaultCredentialsProvider())) {
225+
if (builder.shouldCheckMDSOnInitialization
226+
&& shouldGetDefaultServiceAccountDuringInitialization()) {
227227
this.serviceAccountEmail = getDefaultServiceAccount();
228228
this.principal = this.serviceAccountEmail;
229229
}
@@ -696,6 +696,10 @@ public static Builder newBuilder() {
696696
return new Builder();
697697
}
698698

699+
static Builder newTestBuilder() {
700+
return newBuilder().setShouldCheckMDSOnInitialization(false);
701+
}
702+
699703
/**
700704
* Returns the email address associated with the GCE default service account.
701705
*
@@ -747,6 +751,17 @@ public byte[] sign(byte[] toSign) {
747751
}
748752
}
749753

754+
/** This method is only intended to be called during initialization */
755+
private boolean shouldGetDefaultServiceAccountDuringInitialization() throws IOException {
756+
// Only check GCE is the user did not request a GCE skip via the Env Var. Otherwise, return
757+
// immediately
758+
if (!Boolean.parseBoolean(System.getenv(DefaultCredentialsProvider.NO_GCE_CHECK_ENV_VAR))) {
759+
// Only do a passive check for MDS (do not make an MDS ping check during initialization)
760+
return checkStaticGceDetection(new DefaultCredentialsProvider());
761+
}
762+
return false;
763+
}
764+
750765
private String getDefaultServiceAccount() throws IOException {
751766
HttpResponse response =
752767
getMetadataResponse(getServiceAccountsUrl(), RequestType.UNTRACKED, false);
@@ -788,6 +803,8 @@ public static class Builder extends GoogleCredentials.Builder {
788803
private GoogleAuthTransport transport;
789804
private BindingEnforcement bindingEnforcement;
790805

806+
private boolean shouldCheckMDSOnInitialization = true;
807+
791808
protected Builder() {
792809
setRefreshMargin(COMPUTE_REFRESH_MARGIN);
793810
setExpirationMargin(COMPUTE_EXPIRATION_MARGIN);
@@ -851,6 +868,11 @@ public Builder setBindingEnforcement(BindingEnforcement bindingEnforcement) {
851868
return this;
852869
}
853870

871+
Builder setShouldCheckMDSOnInitialization(boolean shouldCheckMDSOnInitialization) {
872+
this.shouldCheckMDSOnInitialization = shouldCheckMDSOnInitialization;
873+
return this;
874+
}
875+
854876
public HttpTransportFactory getHttpTransportFactory() {
855877
return transportFactory;
856878
}

0 commit comments

Comments
 (0)