Skip to content

Commit b5a6eeb

Browse files
committed
Added encryption of environment specific appsettings. Upgraded target framework.
1 parent 6fc959a commit b5a6eeb

12 files changed

+69
-20
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,3 +518,5 @@ MigrationBackup/
518518
# Ionide (cross platform F# VS Code tools) working folder
519519

520520
# End of https://www.toptal.com/developers/gitignore/api/visualstudio,csharp
521+
522+
!cert.pfx

ConfigCrypter.Console/ConfigCrypter.Console.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
5-
<TargetFramework>netcoreapp3.0</TargetFramework>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
66
<PackAsTool>true</PackAsTool>
77
<ToolCommandName>config-crypter</ToolCommandName>
88
<PackageOutputPath>./nupkg</PackageOutputPath>
@@ -16,6 +16,7 @@
1616
<RepositoryUrl>https://github.com/devattic/ConfigCrypter</RepositoryUrl>
1717
<Company>DevAttic</Company>
1818
<PackageTags>config appsettings encryption tool netcore</PackageTags>
19+
<Version>1.1.0</Version>
1920
</PropertyGroup>
2021

2122
<ItemGroup>

ConfigCrypter/ConfigCrypter.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,15 @@
1414
<RepositoryUrl>https://github.com/devattic/ConfigCrypter</RepositoryUrl>
1515
<PackageTags>netcore aspnetcore netstandard config appsettings encryption decryption</PackageTags>
1616
<PackageLicenseExpression>MIT</PackageLicenseExpression>
17+
<AssemblyVersion>1.1.0.0</AssemblyVersion>
18+
<Version>1.1.0</Version>
1719
</PropertyGroup>
1820

1921
<ItemGroup>
2022
<PackageReference Include="CommandLineParser" Version="2.8.0" />
2123
<PackageReference Include="Microsoft.Extensions.Configuration.Abstractions" Version="3.0.0" />
2224
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="3.0.0" />
25+
<PackageReference Include="Microsoft.Extensions.Hosting.Abstractions" Version="3.0.0" />
2326
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
2427
</ItemGroup>
2528

ConfigCrypter/ConfigProviders/Json/EncryptedJsonConfigProvider.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ public override void Load()
2929
{
3030
foreach (var key in _jsonConfigSource.KeysToDecrypt)
3131
{
32-
var encryptedValue = Data[key];
33-
Data[key] = crypter.DecryptString(encryptedValue);
32+
if (Data.TryGetValue(key, out var encryptedValue))
33+
{
34+
Data[key] = crypter.DecryptString(encryptedValue);
35+
}
3436
}
3537
}
3638
}

ConfigCrypter/Extensions/ConfigurationBuilderExtensions.cs

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
using System;
2-
using DevAttic.ConfigCrypter.CertificateLoaders;
1+
using DevAttic.ConfigCrypter.CertificateLoaders;
32
using DevAttic.ConfigCrypter.ConfigProviders.Json;
43
using Microsoft.Extensions.Configuration;
4+
using Microsoft.Extensions.Hosting;
5+
using System;
56

67
namespace DevAttic.ConfigCrypter.Extensions
78
{
@@ -23,11 +24,34 @@ public static IConfigurationBuilder AddEncryptedAppSettings(
2324
}
2425

2526
var configSource = new EncryptedJsonConfigSource { Path = "appsettings.json" };
26-
configAction(configSource);
27+
configAction?.Invoke(configSource);
2728

28-
InitializeCertificateLoader(configSource);
29+
return AddEncryptedJsonConfig(builder, configSource);
30+
}
31+
32+
/// <summary>
33+
/// Adds a provider to decrypt keys in the appsettings.json and the corresponding environment appsettings files.
34+
/// </summary>
35+
/// <param name="builder">A ConfigurationBuilder instance.</param>
36+
/// <param name="configAction">An action used to configure the configuration source.</param>
37+
/// <param name="hostEnvironment">The current host environment. Used to add environment specific appsettings files. (appsettings.Development.json, appsettings.Production.json)</param>
38+
/// <returns>The current ConfigurationBuilder instance.</returns>
39+
public static IConfigurationBuilder AddEncryptedAppSettings(
40+
this IConfigurationBuilder builder, IHostEnvironment hostEnvironment, Action<EncryptedJsonConfigSource> configAction)
41+
{
42+
if (builder is null)
43+
{
44+
throw new ArgumentNullException(nameof(builder));
45+
}
46+
47+
var appSettingSource = new EncryptedJsonConfigSource { Path = "appsettings.json" };
48+
var environmentSource = new EncryptedJsonConfigSource { Path = $"appsettings.{hostEnvironment.EnvironmentName}.json", Optional = true };
49+
configAction?.Invoke(appSettingSource);
50+
configAction?.Invoke(environmentSource);
51+
52+
AddEncryptedJsonConfig(builder, appSettingSource);
53+
AddEncryptedJsonConfig(builder, environmentSource);
2954

30-
builder.Add(configSource);
3155
return builder;
3256
}
3357

@@ -47,14 +71,28 @@ public static IConfigurationBuilder AddEncryptedJsonConfig(
4771
}
4872

4973
var configSource = new EncryptedJsonConfigSource();
50-
configAction(configSource);
74+
configAction?.Invoke(configSource);
5175

5276
InitializeCertificateLoader(configSource);
5377

5478
builder.Add(configSource);
5579
return builder;
5680
}
5781

82+
/// <summary>
83+
/// Adds a provider to decrypt keys in the given json config file by using the passed EncryptedJsonConfigSource.
84+
/// </summary>
85+
/// <param name="builder">A ConfigurationBuilder instance.</param>
86+
/// <param name="configSource">The fully configured config source.</param>
87+
/// <returns>The current ConfigurationBuilder instance.</returns>
88+
public static IConfigurationBuilder AddEncryptedJsonConfig(this IConfigurationBuilder builder, EncryptedJsonConfigSource configSource)
89+
{
90+
InitializeCertificateLoader(configSource);
91+
builder.Add(configSource);
92+
93+
return builder;
94+
}
95+
5896
private static void InitializeCertificateLoader(EncryptedJsonConfigSource config)
5997
{
6098
if (!string.IsNullOrEmpty(config.CertificatePath))

Example.WebApp/Example.WebApp.csproj

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,6 @@
88
<None Remove="test-certificate.pfx" />
99
</ItemGroup>
1010

11-
<ItemGroup>
12-
<Content Include="test-certificate.pfx">
13-
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
14-
</Content>
15-
</ItemGroup>
16-
1711
<ItemGroup>
1812
<ProjectReference Include="..\ConfigCrypter\ConfigCrypter.csproj" />
1913
</ItemGroup>

Example.WebApp/Program.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System.Collections.Generic;
22
using DevAttic.ConfigCrypter.Extensions;
33
using Microsoft.AspNetCore.Hosting;
4+
using Microsoft.Extensions.Configuration;
45
using Microsoft.Extensions.Hosting;
56

67
namespace Example.WebApp
@@ -18,11 +19,11 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
1819
{
1920
webBuilder.UseStartup<Startup>();
2021
})
21-
.ConfigureAppConfiguration(cfg =>
22+
.ConfigureAppConfiguration((hostingContext, cfg) =>
2223
{
23-
cfg.AddEncryptedAppSettings(crypter =>
24+
cfg.AddEncryptedAppSettings(hostingContext.HostingEnvironment, crypter =>
2425
{
25-
crypter.CertificatePath = "test-certificate.pfx";
26+
crypter.CertificatePath = "cert.pfx";
2627
crypter.KeysToDecrypt = new List<string> { "Nested:KeyToEncrypt" };
2728
});
2829
});

Example.WebApp/appsettings.Development.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,8 @@
55
"Microsoft": "Warning",
66
"Microsoft.Hosting.Lifetime": "Information"
77
}
8+
},
9+
"Nested": {
10+
"KeyToEncrypt": "jQ42kYnVFiu2Fpod4jnaHWfDjhFbxqucGLlUi5HqSnNAVDhwwFLAwvoBQVLfwpN1TC8WBwbWynN2Ej7EO2uRA+cNZUCGxFj+LY+YGJnwT8u1a59gG4QDKgUHp53KtU/UoTWNxPiXWCCRoumvZt1vkPVzO9qYlhRCR6UAPMHQc6lu8UdakW8uCU1sitvtLOdiVGvIk3yLOFg3iJ4XfGn+f8Myvu773v9jLM6541x5Jsyv7mFyNklVBnihwsyBg3WzpjYicms2ioxWBsU+nPGZbX8fyzLTBVvEa1WxyAg8M2IbetdREEl5zwwnI0Ak1MR/lhXN9s9eks1paYloKh9lxg=="
811
}
9-
}
12+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"Nested": {
3+
"KeyToEncrypt": "lFd454PZda3cx/d5YUEKw/dt4yy0rN08niRqCIAmVU8vmPufuhkrTF1K4eyZGQAo1H8UsiQxQO+7CvOSEgODznm6hcO4TofOlyMbiBR/1xswZ0QVFtOpN6JWOtdFJcu/ROrV+T0jl/dcfB5JRLFSJvdsJRMPpwOCrkPeKJ9sQftoOt6V3M258lV5bItdwdYfqCUPS7lq1VPeT4i4HM9ZxqlNU3BxAaAm4YkyXIVpTvMwrvUU2QO7/jbASpWdRxBa1l9FHamhYbMcePinjj3v16MjptYyHl84Wc6XZBBXFm11QHTEAOcFmGEsoxrbDEB6ipGsC+xUoy6IwLtmOxpDTw=="
4+
}
5+
}

Example.WebApp/appsettings.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@
88
},
99
"AllowedHosts": "*",
1010
"Nested": {
11-
"KeyToEncrypt": "CL5DfNyy8utV5RIFJ90RqTqKu12VoOl4TahozXwBD/0hSF2sJPOx4w9MQotnzV0e5uMI7sFxPzkZxOOaHNb0KaU670Hh0E4jwHjG3e6veF1rC+J7QCCKpS3ywccyCppJjvKZcvIRF1bJDNMEO55MrtSUvdsSg51y4EzopWtkHTyCnRHshbDoaSjdJ7Spfe6oBQ+I/3GUnWdPRdB37D3cR3m803FuKV69ac5xQbEy8hPyYYka0wl6AAbnvrJGWCtoW4pgA7sCS1D2u8KMN6BUPq7K1hsS6GcqJORcZDurmEdge7Ik8G7QMJpasGn3qQI3HlconXVb2XvouIKnsR11VQ=="
11+
"KeyToEncrypt": "XPTvTMWCczyrVC3ZcyH3ZC91ueBI1a2FFjrs8pwDC47HRriqdV4tUthERw2WEji3XGQ5rJAy2J0fJz9GRUdxZ8TvSp2LqZi50tns7YkL1HLNVLG+h0BBOzOV9zW4EXZKyNgsjLCsnEQplqpe9dTKcRZLg+ATT17A3C92GP9TO4eoyAPQu5iBt/VVckjM/Cd727yfSBoMlgTx4GeUI+je+aWDSqCdC9m+Cn7wROaK3hp1CVtOsnOqAzwQUKychewMw9VNbzHxBgNaSQ3IqeVFwtL1G2vvE9/+MudXqhTUSRO2Nv7bcva9xPzybdKPUy4u7OIRM8F+WRGieP01nYU79A=="
1212
}
1313
}

Example.WebApp/cert.pfx

2.57 KB
Binary file not shown.

cert.pfx

2.57 KB
Binary file not shown.

0 commit comments

Comments
 (0)