You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* Add StackExchange.Utils.Configuration
This commit adds a helper library that performs substitution and prefixing for `IConfiguration`-based configuration sources. It allows a value in the configuration tree to reference other values within the configuration system using a placeholder syntax `${key}` or `${nested:key}`. In addition, prefixing allows a "namespace" prefix to be applied to subset of configuration, making it possible to segment configuration into logical areas.
This is particularly useful for storing secrets in a different, secure location but in a way that it makes it easy to compose configuration like connection strings with dealing with it inside the application. E.g. consider the following JSON:
**appsettings.json**
```json
{
"ConnectionStrings": {
"Database": "Server=srv01;Database=db01;User ID=${secrets:Database:UserId};Password=${secrets:Database:Password}"
}
}
```
**secrets.json**
```json
{
"Database": {
"UserId": "User123",
"Password": "Password123!"
}
}
```
This instructs the substitution provider to lookup the keys `secrets:Database:UserId` and `secrets:Database:Password` from configuration again and replaces them in the value returned for `ConnectionStrings:Database`.
To support this a `ConfigurationBuilder` is configured as follows:
```c#
var configuration = new ConfigurationBuilder()
.WithPrefix(
"secrets",
// everything in this configuration builder will be prefixed with "secrets:"
c => c.AddJsonFile("secrets.json")
)
.WithSubstitution(
// values in this configuration builder will have substitutions
// replaced prior to being returned to the caller
c => c.AddJsonFile("appsettings.json")
)
.Build();
```
Here we're loading a JSON file called `secrets.json` (it could equally be any source supported by the `IConfiguration` system) and prefixing it with `secrets:`. Then, we load `appsettings.json` with support for substitutions. If a caller asks the `IConfiguration` that is produced for a connection string:
```c#
var connectionString = configuration.GetConnectionString("Database");
```
That value will be returned as `Server=srv01;Database=db01;User ID=User123;Password=Password123!`.
- Build scripts updated to build all projects
- Builds scripts updated to support Linux/MacOS
- `.gitignore` tweaks for Rider
- Move to `LangVersion = latest`
- `version.json` in each packages directories
- Use `Microsoft.NETFramework.ReferenceAssemblies` for net462 builds on Linux
* Typos in doc
* Re-enable Kestrel tests that were conditionally compiled out and fix the build breaks
MacOS doesn't support a PKCS12 certificate without a password so `certificate.pfx` has been updated to add a password (`password`). Also MacOS does not support ALPN so HTTP/2 over TLS is not currently available - this commit skips those tests on MacOS.
* Clarifying comment
* Fix up build scripts to use `Build.csproj` instead of individual project files
* Argument validation + tests
* Support `Set` in the prefix and substitution providers, add tests
`StackExchange.Utils.Configuration` is a helper library that performs substitution and prefixing for `IConfiguration`-based configuration sources. It allows a value in the configuration tree to reference other values within the configuration system using a placeholder syntax `${key}` or `${nested:key}`. In addition, prefixing allows a "namespace" prefix to be applied to a subset of configuration, making it possible to segment configuration into logical areas.
63
+
64
+
This is particularly useful for storing secrets in a different, secure location but in a way that it makes it easy to compose configuration values like connection strings without dealing with it inside the application. E.g. consider the following files:
This instructs the substitution provider to lookup the keys `secrets:Database:UserId` and `secrets:Database:Password` from the configuration system and replaces them in the value returned for `ConnectionStrings:Database`.
86
+
87
+
To support this a `ConfigurationBuilder` is configured as follows:
88
+
89
+
```c#
90
+
varconfiguration=newConfigurationBuilder()
91
+
.WithPrefix(
92
+
"secrets",
93
+
// everything in this configuration builder will be prefixed with "secrets:"
94
+
c=>c.AddJsonFile("secrets.json")
95
+
)
96
+
.WithSubstitution(
97
+
// values in this configuration builder will have substitutions
98
+
// replaced prior to being returned to the caller
99
+
c=>c.AddJsonFile("appsettings.json")
100
+
)
101
+
.Build();
102
+
```
103
+
104
+
Here we're loading a JSON file called `secrets.json` (it could equally be any source supported by the `IConfiguration` system - ideally something secure like Azure KeyVault or Hashicorp's Vault) and prefixing it with `secrets:`. Then, we load `appsettings.json` with support for substitutions. If a caller asks the `IConfiguration` that is produced for a connection string:
This will do exactly the same as if the value was substituted within the configuration system itself - the returned value will be `Server=srv01;Database=db01;User ID=User123;Password=Password123!`.
129
+
130
+
#### Notes
131
+
If a value has substitution placeholders that could not be replaced they are left intact - only placeholder keys that can be located in the configuration system are replaced.
132
+
133
+
### License
62
134
StackExchange.Utils is licensed under the [MIT license](https://github.com/StackExchange/StackExchange.Utils/blob/master/LICENSE.txt).
0 commit comments