CSharpEssentials.GcpSecretManager
Configuration provider that loads Google Cloud Secret Manager secrets into IConfiguration once, at startup.
Installation
dotnet add package CSharpEssentials.GcpSecretManager
Namespace
using CSharpEssentials.GcpSecretManager; // ProjectSecretConfiguration
using CSharpEssentials.GcpSecretManager.Configuration; // SecretManagerConfigurationOptions
using CSharpEssentials.GcpSecretManager.Extensions; // AddGcpSecretManager
Register in code
builder.Configuration.AddGcpSecretManager(options =>
{
options.AddProject(new ProjectSecretConfiguration
{
ProjectId = "my-gcp-project",
PrefixFilters = ["MyApp__"], // load secrets whose ID starts with a prefix
SecretIds = ["stripe-api-key"], // and/or these exact IDs
RawSecretIds = ["stripe-api-key"], // never parse these as JSON
Region = null // null = global endpoint
});
options.CredentialsPath = null; // null = Application Default Credentials
options.BatchSize = 10; // secrets loaded in parallel
options.PageSize = 300; // secrets listed per page
});
string connectionString = builder.Configuration["MyApp:ConnectionStrings:Default"]!;
- With no
PrefixFiltersand noSecretIds, every secret of the project is loaded. AddProjectreturns the options, so calls chain. Anullproject throwsArgumentNullException; no project at all throwsArgumentException.SecretManagerConfigurationOptionsandProjectSecretConfigurationaresealed records.BatchSize<= 0 orPageSize< 0 throwsArgumentOutOfRangeException.AddGcpSecretManagerextendsIConfigurationManager(builder.Configuration), notIConfigurationBuilder.
Register from appsettings.json
Without a delegate, AddGcpSecretManager() reads the GoogleSecretManager section and throws InvalidOperationException if the section, its projects or a ProjectId is missing:
{
"GoogleSecretManager": {
"Projects": [
{ "ProjectId": "my-project", "PrefixFilters": ["Prod__"] }
]
}
}
builder.Configuration.AddGcpSecretManager();
With a delegate, set LoadFromAppSettings = true (and optionally ConfigurationSectionName) to combine both.
Key mapping and JSON secrets
- Every
__in the secret id becomes:, soMyApp__ConnectionStrings__Defaultmaps toMyApp:ConnectionStrings:Default. Keys are case-insensitive; on a duplicate key the first project or secret wins. ImplementISecretManagerConfigurationLoaderand setoptions.Loaderfor another mapping or selection. - A secret whose value is a JSON object or array is also flattened into child keys (
Db={"Host":"x"}addsDb:Host). Secrets listed inRawSecretIdsor matchingRawSecretPrefixesare kept as the raw string only.
Logging and retries
Configuration providers are built before dependency injection, so pass a logger factory explicitly. Without one, nothing is logged.
using ILoggerFactory loggerFactory = LoggerFactory.Create(b => b.AddConsole());
builder.Configuration.AddGcpSecretManager(options =>
{
options.LoggerFactory = loggerFactory;
options.AddProject(new ProjectSecretConfiguration { ProjectId = "my-gcp-project" });
});
Listing and secret access are retried through CSharpEssentials.Resilience on ResourceExhausted and Unavailable only (3 retries, exponential backoff from 2 s). Other failures are logged at Error; a listing failure skips the project, an access failure skips the secret, and loading continues.
Guidelines
- Call
AddGcpSecretManager()after the JSON file sources so secrets override local values. - Name secrets with
__separators so they bind to options sections. - Narrow loading with
PrefixFiltersorSecretIds; loading a whole project is slow and over-privileged. - Grant the service account
Secret Manager Secret Accessoronly. - Use Application Default Credentials in GCP (
gcloud auth application-default loginlocally) instead of a credentials file.