-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathIHostBuilderExtensions.cs
58 lines (54 loc) · 3.31 KB
/
IHostBuilderExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
using Arcus.Security.Core;
using Microsoft.Extensions.Configuration;
using System;
using Microsoft.Extensions.DependencyInjection;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.Hosting
{
/// <summary>
/// Extensions on the <see cref="IHostBuilder"/> to configure the <see cref="ISecretProvider"/> as in a more consumer-friendly manner.
/// </summary>
// ReSharper disable once InconsistentNaming
public static class IHostBuilderExtensions
{
/// <summary>
/// Configure an <see cref="ISecretProvider"/> in the application with a given set of stores configured in the given <paramref name="configureSecretStores"/>.
/// </summary>
/// <param name="hostBuilder">The builder to append the secret store configuration to.</param>
/// <param name="configureSecretStores">The customization of the different target secret store sources to include in the final <see cref="ISecretProvider"/>.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="hostBuilder"/> or <paramref name="configureSecretStores"/> is <c>null</c>.</exception>
public static IHostBuilder ConfigureSecretStore(this IHostBuilder hostBuilder, Action<IConfiguration, SecretStoreBuilder> configureSecretStores)
{
if (hostBuilder is null)
{
throw new ArgumentNullException(nameof(hostBuilder), "Requires a host builder to add the secret store");
}
if (configureSecretStores is null)
{
throw new ArgumentNullException(nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
}
return ConfigureSecretStore(hostBuilder, (context, config, secretStores) => configureSecretStores(config, secretStores));
}
/// <summary>
/// Configure an <see cref="ISecretProvider"/> in the application with a given set of stores configured in the given <paramref name="configureSecretStores"/>.
/// </summary>
/// <param name="hostBuilder">The builder to append the secret store configuration to.</param>
/// <param name="configureSecretStores">The customization of the different target secret store sources to include in the final <see cref="ISecretProvider"/>.</param>
/// <exception cref="ArgumentNullException">Thrown when the <paramref name="hostBuilder"/> or <paramref name="configureSecretStores"/> is <c>null</c>.</exception>
public static IHostBuilder ConfigureSecretStore(this IHostBuilder hostBuilder, Action<HostBuilderContext, IConfiguration, SecretStoreBuilder> configureSecretStores)
{
if (hostBuilder is null)
{
throw new ArgumentNullException(nameof(hostBuilder), "Requires a host builder to add the secret store");
}
if (configureSecretStores is null)
{
throw new ArgumentNullException(nameof(configureSecretStores), "Requires a function to register the secret providers in the secret store");
}
return hostBuilder.ConfigureServices((context, services) =>
{
services.AddSecretStore(stores => configureSecretStores(context, context.Configuration, stores));
});
}
}
}