Csharpessentials gcpsecretmanager

From senrecep/CSharpEssentials · View interactive version

Csharpessentials gcpsecretmanager is an AI agent skill from the GitHub repository senrecep/CSharpEssentials. Use when loading Google Cloud Secret Manager secrets into IConfiguration at startup. Covers builder.Configuration.AddGcpSecretManager(o => o.AddProject(new ProjectSecretConfiguration { ProjectId, SecretIds, PrefixFilters, RawSecretIds, Region })) or the "GoogleSecretManager" appsettings section, wit

How to use this skill

Skill file

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"]!;

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

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

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.