DockerSecrets.Configuration
1.3.1
dotnet add package DockerSecrets.Configuration --version 1.3.1
NuGet\Install-Package DockerSecrets.Configuration -Version 1.3.1
<PackageReference Include="DockerSecrets.Configuration" Version="1.3.1" />
<PackageVersion Include="DockerSecrets.Configuration" Version="1.3.1" />
<PackageReference Include="DockerSecrets.Configuration" />
paket add DockerSecrets.Configuration --version 1.3.1
#r "nuget: DockerSecrets.Configuration, 1.3.1"
#:package DockerSecrets.Configuration@1.3.1
#addin nuget:?package=DockerSecrets.Configuration&version=1.3.1
#tool nuget:?package=DockerSecrets.Configuration&version=1.3.1
DockerSecrets.Configuration
Overview
DockerSecrets.Configuration is a lightweight NuGet package that seamlessly integrates Docker secrets into your .NET configuration system. This provider reads secrets from a mounted Docker secrets directory (defaulting to /run/secrets), processes secret file names based on configurable namespaces and custom delimiters, and loads them as key-value pairs into your application configuration.
The package supports filtering secrets by multiple namespaces and can optionally include secrets without a namespace, giving you complete control over how secrets are imported into your application.
Table of Contents
Features
- Docker Secrets Integration: Load secrets directly from a mounted directory into your configuration.
- Namespace Filtering: Filter secrets using one or multiple namespaces.
- Customizable Delimiters: Configure namespace and key delimiters for flexible file naming conventions.
- Empty Namespace Inclusion: Optionally include secrets that do not have a namespace.
Installation
Install the package via the .NET CLI:
dotnet add package DockerSecrets.Configuration
Or via the Package Manager Console:
Install-Package DockerSecrets.Configuration
For more details, visit the NuGet package page.
Usage
Basic Example
Add the Docker secrets configuration provider to your configuration builder. For instance, in your Program.cs or Startup.cs:
using DockerSecrets.Configuration;
using Microsoft.Extensions.Configuration;
using System.IO;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddDockerSecrets(); // Uses default parameters: secretsPath = "/run/secrets"
// Build the configuration
var configuration = builder.Build();
// Access a secret value (assuming a file named "ApplicationSettings__EncryptionKey")
var encryptionKey = configuration["ApplicationSettings:EncryptionKey"];
Using Multiple Namespaces
You can filter secrets by specific namespaces and control whether to include secrets without a namespace:
using DockerSecrets.Configuration;
using Microsoft.Extensions.Configuration;
using System.IO;
var builder = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddDockerSecrets(
secretsPath: "/run/secrets",
expectedNamespaces: new[] { "Test", "Production" },
namespaceDelimiter: ".",
keyDelimiter: "__",
includeEmptyNamespace: false);
var configuration = builder.Build();
In this example, only secrets with file names beginning with Test. or Production. will be loaded, and secrets with no namespace will be excluded.
API Documentation
AddDockerSecrets Extension Method
public static IConfigurationBuilder AddDockerSecrets(
this IConfigurationBuilder builder,
string secretsPath = "/run/secrets",
IEnumerable<string> expectedNamespaces = null,
string namespaceDelimiter = ".",
string keyDelimiter = "__",
bool includeEmptyNamespace = false)
Parameters:
- builder: The configuration builder to which the provider is added.
- secretsPath: The directory path where Docker secrets are mounted.
- expectedNamespaces: A collection of namespaces to filter secrets. If
nullor empty, secrets without a namespace are automatically included. - namespaceDelimiter: The delimiter that separates the namespace from the key in a secret file name.
- keyDelimiter: The delimiter used to transform the secret file name into a configuration key.
- includeEmptyNamespace: Indicates whether to include secrets without a namespace. Defaults to
falseunless no namespaces are provided.
DockerSecretsConfigurationSource
This class represents the configuration source for Docker secrets.
Properties:
- SecretsPath: The directory where Docker secrets are mounted (default:
/run/secrets). - ExpectedNamespaces: A collection of namespaces used to filter which secrets to load.
- NamespaceDelimiter: The delimiter that separates the namespace from the key in the secret file name.
- KeyDelimiter: The delimiter used in the secret file name to construct the configuration key.
- IncludeEmptyNamespace: Indicates whether secrets without a namespace should be included.
Method:
- Build(IConfigurationBuilder builder): Builds the
DockerSecretsConfigurationProviderinstance.
DockerSecretsConfigurationProvider
This provider reads Docker secrets from the specified directory, converts secret file names into configuration keys using the provided delimiters, and loads the secrets into the configuration system.
Key Responsibilities:
- Reads all files from the configured secrets directory.
- Parses file names to extract the namespace and key parts.
- Replaces custom key delimiters with the standard configuration key delimiter (
:). - Loads secrets based on the filtering rules defined in the configuration source.
Contributing
Contributions are welcome! To contribute:
- Fork the repository.
- Create a new branch for your feature or bugfix.
- Write tests for your changes.
- Submit a pull request with detailed information about your changes.
If you have any issues, suggestions, or improvements, please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Additional Resources
- NuGet Package
- GitHub Repository (Replace with your actual repository URL)
Happy coding! Enjoy secure and manageable configuration with DockerSecrets.Configuration.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 is compatible. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- Microsoft.Extensions.Configuration (>= 9.0.3)
-
.NETStandard 2.1
- Microsoft.Extensions.Configuration (>= 9.0.3)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.