@@ -191,6 +191,8 @@ public String getLabel() {
191191 private transient HttpTransportFactory transportFactory ;
192192 private transient String serviceAccountEmail ;
193193
194+ private final boolean shouldCheckMDSOnInitialization ;
195+
194196 private String universeDomainFromMetadata = null ;
195197
196198 /**
@@ -221,14 +223,17 @@ private ComputeEngineCredentials(ComputeEngineCredentials.Builder builder) {
221223 this .transport = builder .getGoogleAuthTransport ();
222224 this .bindingEnforcement = builder .getBindingEnforcement ();
223225 this .name = GoogleCredentialsInfo .COMPUTE_ENGINE_CREDENTIALS .getCredentialName ();
226+ this .shouldCheckMDSOnInitialization = builder .shouldCheckMDSOnInitialization ;
224227 try {
225- if (builder .shouldCheckMDSOnInitialization
226- && shouldGetDefaultServiceAccountDuringInitialization ()) {
228+ if (shouldCheckMDSOnInitialization
229+ && !Boolean .parseBoolean (System .getenv (DefaultCredentialsProvider .NO_GCE_CHECK_ENV_VAR ))
230+ && checkStaticGceDetection (new DefaultCredentialsProvider ())) {
227231 this .serviceAccountEmail = getDefaultServiceAccount ();
228232 this .principal = this .serviceAccountEmail ;
229233 }
230234 } catch (IOException e ) {
231- // No-op as MDS is not available yet and should not fail initialization
235+ // This should never happen since we do a passive check for MDS. However, if this does fail,
236+ // we mo-op as MDS is not available yet and should not fail initialization
232237 }
233238 }
234239
@@ -751,17 +756,6 @@ public byte[] sign(byte[] toSign) {
751756 }
752757 }
753758
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-
765759 private String getDefaultServiceAccount () throws IOException {
766760 HttpResponse response =
767761 getMetadataResponse (getServiceAccountsUrl (), RequestType .UNTRACKED , false );
@@ -814,6 +808,7 @@ protected Builder(ComputeEngineCredentials credentials) {
814808 super (credentials );
815809 this .transportFactory = credentials .transportFactory ;
816810 this .scopes = credentials .scopes ;
811+ this .shouldCheckMDSOnInitialization = credentials .shouldCheckMDSOnInitialization ;
817812 }
818813
819814 @ CanIgnoreReturnValue
0 commit comments