Skip to content

Commit 9f512a0

Browse files
authored
Refactor Google Secrets integration to support options and improve configuration handling (#23)
This pull request introduces several significant changes to the `GoogleSecrets` configuration functionality. The most important updates include the addition of a new overload for `AddGoogleSecrets`, the removal of redundant code, and enhancements to the handling of `GoogleSecretsOptions`. ### Key Changes: #### New Features: * Added a new overload for `AddGoogleSecrets` to allow passing `GoogleSecretsOptions`. #### Code Simplification: * Removed the redundant environment variable check for `GoogleSecretsProject` in `ConfigurationBuilderExtensions`. [[1]](diffhunk://#diff-3a42ad5a1b6e9fd57bf3513b6cdee8a15582556ed87bd3a456c3cff0da036c27L20-L29) [[2]](diffhunk://#diff-4c1edd3a47fe0ee6052f244806defa0d9acf7a1e57660749e2a7de329850795eL20-R33) * Simplified the `AddGoogleSecrets` method by removing the check for `ProjectName` in `GoogleSecretsOptions`. #### Enhancements: * Set the `ProjectName` property in `GoogleSecretsOptions` to default to the environment variable `GoogleSecretsProject` if available. * Added a warning log and early return in `GoogleSecretsProvider` if `ProjectName` is not set.
1 parent c587a9e commit 9f512a0

5 files changed

Lines changed: 25 additions & 25 deletions

File tree

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and adheres to a project-specific [Versioning](/README.md).
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- Added new overload for `AddGoogleSecrets` to allow passing `GoogleSecretsOptions`.
13+
1014
## [1.4.0] - 2025-03-18
1115

1216
### Added

GoogleSecrets/ConfigurationBuilderExtensions.cs

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,6 @@ public static class ConfigurationBuilderExtensions
1717
/// <exception cref="System.ArgumentNullException">options</exception>
1818
public static IConfigurationBuilder AddGoogleSecrets(this IConfigurationBuilder configuration)
1919
{
20-
// Configure app configuration to add Google Secrets if environment variable is set
21-
var googleSecretProject = Environment.GetEnvironmentVariable(EnvironmentVariableNames.GoogleSecretsProject);
22-
if (!string.IsNullOrWhiteSpace(googleSecretProject))
23-
{
24-
return AddGoogleSecrets(configuration, options =>
25-
{
26-
options.ProjectName = googleSecretProject;
27-
});
28-
}
29-
3020
return AddGoogleSecrets(configuration, _ => { });
3121
}
3222

@@ -45,11 +35,6 @@ public static IConfigurationBuilder AddGoogleSecrets(this IConfigurationBuilder
4535
var googleSecretsOptions = new GoogleSecretsOptions();
4636
options(googleSecretsOptions);
4737

48-
if (string.IsNullOrWhiteSpace(googleSecretsOptions.ProjectName))
49-
{
50-
throw new ArgumentNullException(nameof(googleSecretsOptions.ProjectName));
51-
}
52-
5338
configuration.Add(new GoogleSecretsSource(googleSecretsOptions, configuration.Build()));
5439

5540
return configuration;

GoogleSecrets/GoogleSecretsOptions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class GoogleSecretsOptions
4141
/// <value>
4242
/// The name of the project or the numeric project id
4343
/// </value>
44-
public string ProjectName { get; set; }
44+
public string ProjectName { get; set; } = Environment.GetEnvironmentVariable(EnvironmentVariableNames.GoogleSecretsProject) ?? string.Empty;
4545

4646
/// <summary>
4747
/// Gets or sets the filter.

GoogleSecrets/GoogleSecretsProvider.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public override void Load()
4545
{
4646
try
4747
{
48+
if (string.IsNullOrWhiteSpace(this.Source.ProjectName))
49+
{
50+
this.logger.LogWarning("ProjectName is not set. Skipping Google Secrets Provider.");
51+
return;
52+
}
53+
4854
// Create client
4955
SecretManagerServiceClient secretManagerServiceClient = SecretManagerServiceClient.Create();
5056

Neolution.Extensions.Configuration.GoogleSecrets.AspNetCore/WebApplicationBuilderExtensions.cs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,20 @@ public static void AddGoogleSecrets(this WebApplicationBuilder builder)
1717
{
1818
ArgumentNullException.ThrowIfNull(builder);
1919

20-
// Configure app configuration to add Google Secrets if environment variable is set
21-
var googleSecretProject = Environment.GetEnvironmentVariable(EnvironmentVariableNames.GoogleSecretsProject);
22-
if (!string.IsNullOrWhiteSpace(googleSecretProject))
23-
{
24-
builder.Configuration.AddGoogleSecrets(options =>
25-
{
26-
options.ProjectName = googleSecretProject;
27-
});
28-
}
20+
builder.Configuration.AddGoogleSecrets(_ => { });
21+
}
22+
23+
/// <summary>
24+
/// Adds the Google secrets to the <see cref="WebApplicationBuilder"/>.
25+
/// </summary>
26+
/// <param name="builder"></param>
27+
/// <param name="googleSecretsOptions"></param>
28+
public static void AddGoogleSecrets(this WebApplicationBuilder builder, Action<GoogleSecretsOptions> googleSecretsOptions)
29+
{
30+
ArgumentNullException.ThrowIfNull(builder);
31+
ArgumentNullException.ThrowIfNull(googleSecretsOptions);
32+
33+
builder.Configuration.AddGoogleSecrets(googleSecretsOptions);
2934
}
3035
}
3136
}

0 commit comments

Comments
 (0)