Repository navigation
Expand file tree
/
Copy pathConfigurationBuilderExtensions.cs
More file actions
119 lines (111 loc) · 6.17 KB
/
Copy pathConfigurationBuilderExtensions.cs
File metadata and controls
119 lines (111 loc) · 6.17 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
using System;
using System.IO;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.FileProviders;
namespace SecureConfig.Decrypt
{
/// <summary>
/// Extension methods for adding <see cref="EncryptedJsonConfigurationProvider"/>.
/// </summary>
public static class ConfigurationBuilderExtensions
{
/// <summary>
/// Adds the encrypted JSON configuration provider at <paramref name="path"/> to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="key">The encryption key</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEncryptedJsonFile(this IConfigurationBuilder builder, string path, byte[] key)
{
return AddEncryptedJsonFile(builder, provider: null, path: path, optional: false, reloadOnChange: false, key: key);
}
/// <summary>
/// Adds the encrypted JSON configuration provider at <paramref name="path"/> to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional">Whether the file is optional.</param>
/// <param name="key">The encryption key</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEncryptedJsonFile(this IConfigurationBuilder builder, string path, bool optional, byte[] key)
{
return AddEncryptedJsonFile(builder, provider: null, path: path, optional: optional, reloadOnChange: false, key: key);
}
/// <summary>
/// Adds the encrypted JSON configuration provider at <paramref name="path"/> to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional">Whether the file is optional.</param>
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
/// <param name="key">The encryption key</param>
/// <returns>The <see cref="AddEncryptedJsonFile"/>.</returns>
public static IConfigurationBuilder AddJsonFile(this IConfigurationBuilder builder, string path, bool optional, bool reloadOnChange, byte[] key)
{
return AddEncryptedJsonFile(builder, provider: null, path: path, optional: optional, reloadOnChange: reloadOnChange, key: key);
}
/// <summary>
/// Adds an encrypted JSON configuration source to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="provider">The <see cref="IFileProvider"/> to use to access the file.</param>
/// <param name="path">Path relative to the base path stored in
/// <see cref="IConfigurationBuilder.Properties"/> of <paramref name="builder"/>.</param>
/// <param name="optional">Whether the file is optional.</param>
/// <param name="reloadOnChange">Whether the configuration should be reloaded if the file changes.</param>
/// <param name="key">The encryption key</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEncryptedJsonFile(this IConfigurationBuilder builder, IFileProvider provider, string path, bool optional, bool reloadOnChange, byte[] key)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
if (string.IsNullOrEmpty(path))
{
throw new ArgumentException("File path must be a non-empty string", nameof(path));
}
return builder.AddEncryptedJsonFile(s =>
{
s.FileProvider = provider;
s.Path = path;
s.Optional = optional;
s.Key = key;
s.ReloadOnChange = reloadOnChange;
s.ResolveFileProvider();
});
}
/// <summary>
/// Adds an encrypted JSON configuration source to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="configureSource">Configures the source.</param>
/// <param name="key">The encryption key</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEncryptedJsonFile(this IConfigurationBuilder builder, Action<EncryptedJsonConfigurationSource> configureSource)
=> builder.Add(configureSource);
/// <summary>
/// Adds a JSON configuration source to <paramref name="builder"/>.
/// </summary>
/// <param name="builder">The <see cref="IConfigurationBuilder"/> to add to.</param>
/// <param name="stream">The <see cref="Stream"/> to read the json configuration data from.</param>
/// <param name="key">The encryption key</param>
/// <returns>The <see cref="IConfigurationBuilder"/>.</returns>
public static IConfigurationBuilder AddEncryptedJsonStream(this IConfigurationBuilder builder, Stream stream, byte[] key)
{
if (builder == null)
{
throw new ArgumentNullException(nameof(builder));
}
return builder.Add<EncryptedJsonStreamConfigurationSource>(s =>
{
s.Stream = stream;
s.Key = key;
});
}
}
}