示例#1
0
 public OnGceFactAttribute()
 {
     // Ugly, but this is for tests.
     if (!ComputeCredential.IsRunningOnComputeEngine().GetAwaiter().GetResult())
     {
         Skip = "Not running on GCE";
     }
 }
示例#2
0
        /// <summary>
        /// Will use compute credentials if running on compute engine, otherwise will use environment variables
        /// </summary>
        /// <returns></returns>
        public static async Task <BigtableCredentials> UseApplicationDefaultCredentialsAsync()
        {
            if (await ComputeCredential.IsRunningOnComputeEngine())
            {
                // Hookup .pem file
                SetDefaultSslKeyFilePath();

                // Get credential
                var credentials = new ComputeCredential(new ComputeCredential.Initializer());

                // Return results
                return(new BigtableCredentials(credentials));
            }

            // Use environment
            return(await UseEnvironmentAsync());
        }
        public static async Task <string> DetermineRuntime()
        {
            // Check for environment variables that indicate a specific runtime.
            foreach (var indicator in environmentIndicators)
            {
                if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable(indicator.Key)))
                {
                    return(indicator.Value);
                }
            }

            // Check metadata server, which indicates GCE.
            if (await ComputeCredential.IsRunningOnComputeEngine())
            {
                return("Compute Engine");
            }
            else
            {
                // Metadata server not available, so it's probably not nunning
                // on Google Cloud.
                return(null);
            }
        }
 public void IsRunningOnComputeEngine_ResultIsCached()
 {
     // Two subsequent invocations should return the same task.
     Assert.Same(ComputeCredential.IsRunningOnComputeEngine(),
                 ComputeCredential.IsRunningOnComputeEngine());
 }
示例#5
0
        /// <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));
        }
示例#6
0
 static string ShowRunningOnComputeEngine() =>
 ComputeCredential.IsRunningOnComputeEngine().Result.ToString();