| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | using System; |
| | using System.IO; |
| | using System.Threading; |
| | using System.Threading.Tasks; |
| |
|
| | using Google.Apis.Logging; |
| |
|
| | namespace Google.Apis.Auth.OAuth2 |
| | { |
| |
|
| | |
| | |
| | |
| | |
| | |
| | internal class DefaultCredentialProvider |
| | { |
| | private static readonly ILogger Logger = ApplicationContext.Logger.ForType<DefaultCredentialProvider>(); |
| |
|
| | |
| | |
| | |
| | public const string CredentialEnvironmentVariable = "GOOGLE_APPLICATION_CREDENTIALS"; |
| |
|
| | |
| | private const string WellKnownCredentialsFile = "application_default_credentials.json"; |
| |
|
| | |
| | private const string AppdataEnvironmentVariable = "APPDATA"; |
| |
|
| | |
| | private const string HomeEnvironmentVariable = "HOME"; |
| |
|
| | |
| | private const string CloudSDKConfigDirectoryWindows = "gcloud"; |
| |
|
| | |
| | private const string HelpPermalink = |
| | "https://cloud.google.com/docs/authentication/external/set-up-adc"; |
| |
|
| | |
| | private static readonly string CloudSDKConfigDirectoryUnix = Path.Combine(".config", "gcloud"); |
| |
|
| | |
| | private readonly Lazy<Task<GoogleCredential>> cachedCredentialTask; |
| |
|
| | |
| | public DefaultCredentialProvider() |
| | { |
| | cachedCredentialTask = new Lazy<Task<GoogleCredential>>(CreateDefaultCredentialAsync); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | public Task<GoogleCredential> GetDefaultCredentialAsync() => cachedCredentialTask.Value; |
| |
|
| | |
| | private async Task<GoogleCredential> CreateDefaultCredentialAsync() |
| | { |
| | GoogleCredential credential = |
| | |
| | await GetAdcFromEnvironmentVariableAsync().ConfigureAwait(false) |
| | |
| | ?? await GetAdcFromWellKnownFileAsync().ConfigureAwait(false) |
| | |
| | ?? await GetAdcFromComputeAsync().ConfigureAwait(false) |
| | |
| | ?? throw new InvalidOperationException($"Your default credentials were not found. To set up Application Default Credentials, see {HelpPermalink}."); |
| |
|
| | return credential.CreateWithEnvironmentQuotaProject(); |
| |
|
| | async Task<GoogleCredential> GetAdcFromEnvironmentVariableAsync() |
| | { |
| | string credentialPath = GetEnvironmentVariable(CredentialEnvironmentVariable); |
| | if (!string.IsNullOrWhiteSpace(credentialPath)) |
| | { |
| | try |
| | { |
| | return await CreateDefaultCredentialFromFileAsync(credentialPath, default).ConfigureAwait(false); |
| | } |
| | catch (Exception e) |
| | { |
| | |
| | |
| | throw new InvalidOperationException( |
| | $"Error reading credential file from location {credentialPath}: {e.Message}{Environment.NewLine}" + |
| | $"Please check the value of the Environment Variable {CredentialEnvironmentVariable}.", e); |
| | } |
| | } |
| | return null; |
| | } |
| |
|
| | async Task<GoogleCredential> GetAdcFromWellKnownFileAsync() |
| | { |
| | string credentialPath = GetWellKnownCredentialFilePath(); |
| | if (!string.IsNullOrWhiteSpace(credentialPath)) |
| | { |
| | try |
| | { |
| | return await CreateDefaultCredentialFromFileAsync(credentialPath, default).ConfigureAwait(false); |
| | } |
| | catch (Exception e) when (e is FileNotFoundException || e is DirectoryNotFoundException) |
| | { |
| | |
| | Logger.Debug($"Well-known credential file {credentialPath} not found."); |
| | } |
| | catch (Exception e) |
| | { |
| | throw new InvalidOperationException( |
| | $"Error reading credential file from location {credentialPath}: {e.Message}{Environment.NewLine}" + |
| | "Please rerun 'gcloud auth login' to regenerate credentials file.", e); |
| | } |
| | } |
| | return null; |
| | } |
| |
|
| | async Task<GoogleCredential> GetAdcFromComputeAsync() |
| | { |
| | 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()); |
| | } |
| | return null; |
| | } |
| | } |
| |
|
| | private async Task<GoogleCredential> CreateDefaultCredentialFromFileAsync(string credentialPath, CancellationToken cancellationToken) |
| | { |
| | Logger.Debug("Loading Credential from file {0}", credentialPath); |
| |
|
| | |
| | using Stream stream = GetStream(credentialPath); |
| | var credential = await CredentialFactory.FromStreamAsync<IGoogleCredential>(stream, cancellationToken).ConfigureAwait(false); |
| | return credential.ToGoogleCredential(); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | private string GetWellKnownCredentialFilePath() |
| | { |
| | var appData = GetEnvironmentVariable(AppdataEnvironmentVariable); |
| | if (appData != null) |
| | { |
| | return Path.Combine(appData, CloudSDKConfigDirectoryWindows, WellKnownCredentialsFile); |
| | } |
| | var unixHome = GetEnvironmentVariable(HomeEnvironmentVariable); |
| | if (unixHome != null) |
| | { |
| | return Path.Combine(unixHome, CloudSDKConfigDirectoryUnix, WellKnownCredentialsFile); |
| | } |
| | return Path.Combine(CloudSDKConfigDirectoryWindows, WellKnownCredentialsFile); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | protected virtual string GetEnvironmentVariable(string variableName) |
| | { |
| | return Environment.GetEnvironmentVariable(variableName); |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | protected virtual Stream GetStream(string filePath) |
| | { |
| | return new FileStream(filePath, FileMode.Open, FileAccess.Read); |
| | } |
| | } |
| | } |
| |
|