Custom Configuration provider for asp.net core

Hi,

it would be awesome if BW would provide a custom configuration provider for asp.net core that would retrieve secrets from BW Secrets manager.

I have implemented this feature in one of my project, but unfortunately this version is slow because it fetches every secrets on startup.

using Bitwarden.Sdk;
using Microsoft.Extensions.Configuration;

public class BitwardenConfig
{
    public string AccessToken { get; set; }
    public string OrganizationId { get; set; }
    public string ProjectId { get; set; }
}

public class BitwardenConfigurationProvider : ConfigurationProvider
{
    private readonly Guid _organizationId;
    private readonly Guid _projectId;
    private readonly BitwardenClient _client;

    public BitwardenConfigurationProvider(string apiKey, string organizationId, string projectId)
    {
        _organizationId = Guid.Parse(organizationId);
        _projectId = Guid.Parse(projectId);

        _client = new();
        _client.AccessTokenLogin(apiKey);
    }

    public override void Load()
    {
        var secrets = _client.Secrets.List(_organizationId);
        var secretValues = secrets.Data.Select(x => _client.Secrets.Get(x.Id)).Where(x => x.ProjectId == _projectId);

        Data = secretValues.ToDictionary(k => k.Key, v => v.Value);
    }
}

public class BitwardenConfigurationSource(BitwardenConfig config) : IConfigurationSource
{
    public IConfigurationProvider Build(IConfigurationBuilder builder)
    {
        return new BitwardenConfigurationProvider(config.AccessToken, config.OrganizationId, config.ProjectId);
    }
}

public static class BitwardenConfigurationExtensions
{

    public static ConfigurationManager AddBitwarden(this ConfigurationManager manager)
    {
        var section = manager.GetRequiredSection("Bitwarden").Get<BitwardenConfig>();

        ((IConfigurationBuilder)manager).Add(new BitwardenConfigurationSource(section));
        
        return manager;
    }
}

Then you can use it in your app builder:

// add config
builder.Configuration.AddBitwarden();

Also you will need to add a new Bitwarden section to your appsettings.json:

{
  "Bitwarden": {
  "AccessToken": "0.xxxxx",
  "OrganizationId": "xxx-xxx-",
  "ProjectId": "xxx-xxx-"
 }
}

I hope in the future there will be a batch download for the secrets and the ability to filter the secrets using project ID.