From 49d550e5b6c303b1ac8b154184a6240b876ad03c Mon Sep 17 00:00:00 2001 From: Timothy Trowbridge Date: Tue, 15 Apr 2025 15:10:36 +0200 Subject: [PATCH] Refactor Google Secrets integration to support options and improve configuration handling --- CHANGELOG.md | 4 ++++ .../ConfigurationBuilderExtensions.cs | 15 ------------ GoogleSecrets/GoogleSecretsOptions.cs | 2 +- GoogleSecrets/GoogleSecretsProvider.cs | 6 +++++ .../WebApplicationBuilderExtensions.cs | 23 +++++++++++-------- 5 files changed, 25 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 501514d..f901b7e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and adheres to a project-specific [Versioning](/README.md). ## [Unreleased] +### Added + +- Added new overload for `AddGoogleSecrets` to allow passing `GoogleSecretsOptions`. + ## [1.4.0] - 2025-03-18 ### Added diff --git a/GoogleSecrets/ConfigurationBuilderExtensions.cs b/GoogleSecrets/ConfigurationBuilderExtensions.cs index 5a78bfe..b4715cd 100644 --- a/GoogleSecrets/ConfigurationBuilderExtensions.cs +++ b/GoogleSecrets/ConfigurationBuilderExtensions.cs @@ -17,16 +17,6 @@ public static class ConfigurationBuilderExtensions /// options public static IConfigurationBuilder AddGoogleSecrets(this IConfigurationBuilder configuration) { - // Configure app configuration to add Google Secrets if environment variable is set - var googleSecretProject = Environment.GetEnvironmentVariable(EnvironmentVariableNames.GoogleSecretsProject); - if (!string.IsNullOrWhiteSpace(googleSecretProject)) - { - return AddGoogleSecrets(configuration, options => - { - options.ProjectName = googleSecretProject; - }); - } - return AddGoogleSecrets(configuration, _ => { }); } @@ -45,11 +35,6 @@ public static IConfigurationBuilder AddGoogleSecrets(this IConfigurationBuilder var googleSecretsOptions = new GoogleSecretsOptions(); options(googleSecretsOptions); - if (string.IsNullOrWhiteSpace(googleSecretsOptions.ProjectName)) - { - throw new ArgumentNullException(nameof(googleSecretsOptions.ProjectName)); - } - configuration.Add(new GoogleSecretsSource(googleSecretsOptions, configuration.Build())); return configuration; diff --git a/GoogleSecrets/GoogleSecretsOptions.cs b/GoogleSecrets/GoogleSecretsOptions.cs index fa03469..f0c80c3 100644 --- a/GoogleSecrets/GoogleSecretsOptions.cs +++ b/GoogleSecrets/GoogleSecretsOptions.cs @@ -41,7 +41,7 @@ public class GoogleSecretsOptions /// /// The name of the project or the numeric project id /// - public string ProjectName { get; set; } + public string ProjectName { get; set; } = Environment.GetEnvironmentVariable(EnvironmentVariableNames.GoogleSecretsProject) ?? string.Empty; /// /// Gets or sets the filter. diff --git a/GoogleSecrets/GoogleSecretsProvider.cs b/GoogleSecrets/GoogleSecretsProvider.cs index 19e1904..43de1a8 100644 --- a/GoogleSecrets/GoogleSecretsProvider.cs +++ b/GoogleSecrets/GoogleSecretsProvider.cs @@ -45,6 +45,12 @@ public override void Load() { try { + if (string.IsNullOrWhiteSpace(this.Source.ProjectName)) + { + this.logger.LogWarning("ProjectName is not set. Skipping Google Secrets Provider."); + return; + } + // Create client SecretManagerServiceClient secretManagerServiceClient = SecretManagerServiceClient.Create(); diff --git a/Neolution.Extensions.Configuration.GoogleSecrets.AspNetCore/WebApplicationBuilderExtensions.cs b/Neolution.Extensions.Configuration.GoogleSecrets.AspNetCore/WebApplicationBuilderExtensions.cs index 82d4dd7..a1574c6 100644 --- a/Neolution.Extensions.Configuration.GoogleSecrets.AspNetCore/WebApplicationBuilderExtensions.cs +++ b/Neolution.Extensions.Configuration.GoogleSecrets.AspNetCore/WebApplicationBuilderExtensions.cs @@ -17,15 +17,20 @@ public static void AddGoogleSecrets(this WebApplicationBuilder builder) { ArgumentNullException.ThrowIfNull(builder); - // Configure app configuration to add Google Secrets if environment variable is set - var googleSecretProject = Environment.GetEnvironmentVariable(EnvironmentVariableNames.GoogleSecretsProject); - if (!string.IsNullOrWhiteSpace(googleSecretProject)) - { - builder.Configuration.AddGoogleSecrets(options => - { - options.ProjectName = googleSecretProject; - }); - } + builder.Configuration.AddGoogleSecrets(_ => { }); + } + + /// + /// Adds the Google secrets to the . + /// + /// + /// + public static void AddGoogleSecrets(this WebApplicationBuilder builder, Action googleSecretsOptions) + { + ArgumentNullException.ThrowIfNull(builder); + ArgumentNullException.ThrowIfNull(googleSecretsOptions); + + builder.Configuration.AddGoogleSecrets(googleSecretsOptions); } } } \ No newline at end of file