public void IsRunningOnComputeEngine_ResultIsCached()
 {
     // Two subsequent invocations should return the same task.
     Assert.AreSame(ComputeCredential.IsRunningOnComputeEngine(),
                    ComputeCredential.IsRunningOnComputeEngine());
 }
        /// <summary>Creates a new default credential.</summary>
        private async Task <GoogleCredential> CreateDefaultCredentialAsync()
        {
            // 1. First try the environment variable.
            string credentialPath = GetEnvironmentVariable(CredentialEnvironmentVariable);

            if (!String.IsNullOrWhiteSpace(credentialPath))
            {
                try
                {
                    return(CreateDefaultCredentialFromFile(credentialPath));
                }
                catch (Exception e)
                {
                    // Catching generic exception type because any corrupted file could manifest in different ways
                    // including but not limited to the System, System.IO or from the Newtonsoft.Json namespace.
                    throw new InvalidOperationException(
                              String.Format("Error reading credential file from location {0}: {1}"
                                            + "\nPlease check the value of the Environment Variable {2}",
                                            credentialPath,
                                            e.Message,
                                            CredentialEnvironmentVariable), e);
                }
            }

            // 2. Then try the well known file.
            credentialPath = GetWellKnownCredentialFilePath();
            if (!String.IsNullOrWhiteSpace(credentialPath))
            {
                try
                {
                    return(CreateDefaultCredentialFromFile(credentialPath));
                }
                catch (FileNotFoundException)
                {
                    // File is not present, eat the exception and move on to the next check.
                    Logger.Debug("Well-known credential file {0} not found.", credentialPath);
                }
                catch (DirectoryNotFoundException)
                {
                    // Directory not present, eat the exception and move on to the next check.
                    Logger.Debug("Well-known credential file {0} not found.", credentialPath);
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException(
                              String.Format("Error reading credential file from location {0}: {1}"
                                            + "\nPlease rerun 'gcloud auth login' to regenerate credentials file.",
                                            credentialPath,
                                            e.Message), e);
                }
            }

            // 3. Then try the compute engine.
            Logger.Debug("Checking whether the application is running on ComputeEngine.");
            if (await ComputeCredential.IsRunningOnComputeEngine().ConfigureAwait(false))
            {
                Logger.Debug("ComputeEngine check passed. Using ComputeEngine Credentials.");
                return(new GoogleCredential(new ComputeCredential()));
            }

            // If everything we tried has failed, throw an exception.
            throw new InvalidOperationException(
                      String.Format("The Application Default Credentials are not available. They are available if running"
                                    + " in Google Compute Engine. Otherwise, the environment variable {0} must be defined"
                                    + " pointing to a file defining the credentials. See {1} for more information.",
                                    CredentialEnvironmentVariable,
                                    HelpPermalink));
        }
 public void IsRunningOnComputeEngine()
 {
     // It should be safe to assume that this test is not running on GCE.
     Assert.IsFalse(ComputeCredential.IsRunningOnComputeEngine().Result);
 }