Skip to content
This repository was archived by the owner on Oct 17, 2018. It is now read-only.

Commit 5d1a523

Browse files
author
Nate McMaster
committed
Make ILoggerFactory an optional service on any DI-injected services
1 parent abf05e2 commit 5d1a523

33 files changed

+238
-236
lines changed

samples/AzureBlob/AzureBlob.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Import Project="..\..\build\dependencies.props" />
44

55
<PropertyGroup>
66
<TargetFramework>netcoreapp2.0</TargetFramework>
7+
<OutputType>exe</OutputType>
78
</PropertyGroup>
89

910
<ItemGroup>

samples/AzureBlob/Program.cs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using Microsoft.Extensions.DependencyInjection;
77
using Microsoft.Extensions.Logging;
88
using Microsoft.WindowsAzure.Storage;
9+
using LogLevel = Microsoft.Extensions.Logging.LogLevel;
910

1011
namespace AzureBlob
1112
{
@@ -24,21 +25,19 @@ public static void Main(string[] args)
2425
container.CreateIfNotExistsAsync().GetAwaiter().GetResult();
2526

2627
// Configure
27-
28-
var serviceCollection = new ServiceCollection();
29-
serviceCollection.AddLogging();
30-
serviceCollection.AddDataProtection()
31-
.PersistKeysToAzureBlobStorage(container, "keys.xml");
32-
33-
var services = serviceCollection.BuildServiceProvider();
34-
var loggerFactory = services.GetService<LoggerFactory>();
35-
loggerFactory.AddConsole();
36-
37-
// Run a sample payload
38-
39-
var protector = services.GetDataProtector("sample-purpose");
40-
var protectedData = protector.Protect("Hello world!");
41-
Console.WriteLine(protectedData);
28+
using (var services = new ServiceCollection()
29+
.AddLogging(o => o.AddConsole().SetMinimumLevel(LogLevel.Debug))
30+
.AddDataProtection()
31+
.PersistKeysToAzureBlobStorage(container, "keys.xml")
32+
.Services
33+
.BuildServiceProvider())
34+
{
35+
// Run a sample payload
36+
37+
var protector = services.GetDataProtector("sample-purpose");
38+
var protectedData = protector.Protect("Hello world!");
39+
Console.WriteLine(protectedData);
40+
}
4241
}
4342
}
4443
}

samples/AzureBlob/Properties/launchSettings.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

samples/CustomEncryptorSample/CustomEncryptorSample.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Import Project="..\..\build\dependencies.props" />
44

55
<PropertyGroup>
66
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
7+
<OutputType>exe</OutputType>
78
</PropertyGroup>
89

910
<ItemGroup>

samples/CustomEncryptorSample/Program.cs

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,23 @@ public class Program
1414
public static void Main(string[] args)
1515
{
1616
var keysFolder = Path.Combine(Directory.GetCurrentDirectory(), "temp-keys");
17-
var serviceCollection = new ServiceCollection();
18-
serviceCollection.AddLogging();
19-
serviceCollection.AddDataProtection()
17+
using (var services = new ServiceCollection()
18+
.AddLogging(o => o.AddConsole().SetMinimumLevel(LogLevel.Debug))
19+
.AddDataProtection()
2020
.PersistKeysToFileSystem(new DirectoryInfo(keysFolder))
21-
.UseXmlEncryptor(s => new CustomXmlEncryptor(s));
21+
.UseXmlEncryptor(s => new CustomXmlEncryptor(s))
22+
.Services.BuildServiceProvider())
23+
{
24+
var protector = services.GetDataProtector("SamplePurpose");
2225

23-
var services = serviceCollection.BuildServiceProvider();
24-
var loggerFactory = services.GetRequiredService<LoggerFactory>();
25-
loggerFactory.AddConsole();
26+
// protect the payload
27+
var protectedPayload = protector.Protect("Hello World!");
28+
Console.WriteLine($"Protect returned: {protectedPayload}");
2629

27-
var protector = services.GetDataProtector("SamplePurpose");
28-
29-
// protect the payload
30-
var protectedPayload = protector.Protect("Hello World!");
31-
Console.WriteLine($"Protect returned: {protectedPayload}");
32-
33-
// unprotect the payload
34-
var unprotectedPayload = protector.Unprotect(protectedPayload);
35-
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
30+
// unprotect the payload
31+
var unprotectedPayload = protector.Unprotect(protectedPayload);
32+
Console.WriteLine($"Unprotect returned: {unprotectedPayload}");
33+
}
3634
}
3735
}
3836
}

samples/CustomEncryptorSample/Properties/launchSettings.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

samples/KeyManagementSample/KeyManagementSample.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Import Project="..\..\build\dependencies.props" />
44

55
<PropertyGroup>
66
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
7+
<OutputType>exe</OutputType>
78
</PropertyGroup>
89

910
<ItemGroup>

samples/KeyManagementSample/Program.cs

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -16,48 +16,50 @@ public static void Main(string[] args)
1616
{
1717
var keysFolder = Path.Combine(Directory.GetCurrentDirectory(), "temp-keys");
1818
var serviceCollection = new ServiceCollection();
19-
var builder = serviceCollection.AddDataProtection()
19+
var builder = serviceCollection
20+
.AddDataProtection()
2021
// point at a specific folder and use DPAPI to encrypt keys
2122
.PersistKeysToFileSystem(new DirectoryInfo(keysFolder));
2223
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
2324
{
2425
builder.ProtectKeysWithDpapi();
2526
}
2627

27-
var services = serviceCollection.BuildServiceProvider();
28+
using (var services = serviceCollection.BuildServiceProvider())
29+
{
30+
// perform a protect operation to force the system to put at least
31+
// one key in the key ring
32+
services.GetDataProtector("Sample.KeyManager.v1").Protect("payload");
33+
Console.WriteLine("Performed a protect operation.");
2834

29-
// perform a protect operation to force the system to put at least
30-
// one key in the key ring
31-
services.GetDataProtector("Sample.KeyManager.v1").Protect("payload");
32-
Console.WriteLine("Performed a protect operation.");
35+
// get a reference to the key manager
36+
var keyManager = services.GetService<IKeyManager>();
3337

34-
// get a reference to the key manager
35-
var keyManager = services.GetService<IKeyManager>();
38+
// list all keys in the key ring
39+
var allKeys = keyManager.GetAllKeys();
40+
Console.WriteLine($"The key ring contains {allKeys.Count} key(s).");
41+
foreach (var key in allKeys)
42+
{
43+
Console.WriteLine($"Key {key.KeyId:B}: Created = {key.CreationDate:u}, IsRevoked = {key.IsRevoked}");
44+
}
3645

37-
// list all keys in the key ring
38-
var allKeys = keyManager.GetAllKeys();
39-
Console.WriteLine($"The key ring contains {allKeys.Count} key(s).");
40-
foreach (var key in allKeys)
41-
{
42-
Console.WriteLine($"Key {key.KeyId:B}: Created = {key.CreationDate:u}, IsRevoked = {key.IsRevoked}");
43-
}
46+
// revoke all keys in the key ring
47+
keyManager.RevokeAllKeys(DateTimeOffset.Now, reason: "Revocation reason here.");
48+
Console.WriteLine("Revoked all existing keys.");
4449

45-
// revoke all keys in the key ring
46-
keyManager.RevokeAllKeys(DateTimeOffset.Now, reason: "Revocation reason here.");
47-
Console.WriteLine("Revoked all existing keys.");
50+
// add a new key to the key ring with immediate activation and a 1-month expiration
51+
keyManager.CreateNewKey(
52+
activationDate: DateTimeOffset.Now,
53+
expirationDate: DateTimeOffset.Now.AddMonths(1));
54+
Console.WriteLine("Added a new key.");
4855

49-
// add a new key to the key ring with immediate activation and a 1-month expiration
50-
keyManager.CreateNewKey(
51-
activationDate: DateTimeOffset.Now,
52-
expirationDate: DateTimeOffset.Now.AddMonths(1));
53-
Console.WriteLine("Added a new key.");
54-
55-
// list all keys in the key ring
56-
allKeys = keyManager.GetAllKeys();
57-
Console.WriteLine($"The key ring contains {allKeys.Count} key(s).");
58-
foreach (var key in allKeys)
59-
{
60-
Console.WriteLine($"Key {key.KeyId:B}: Created = {key.CreationDate:u}, IsRevoked = {key.IsRevoked}");
56+
// list all keys in the key ring
57+
allKeys = keyManager.GetAllKeys();
58+
Console.WriteLine($"The key ring contains {allKeys.Count} key(s).");
59+
foreach (var key in allKeys)
60+
{
61+
Console.WriteLine($"Key {key.KeyId:B}: Created = {key.CreationDate:u}, IsRevoked = {key.IsRevoked}");
62+
}
6163
}
6264
}
6365
}

samples/KeyManagementSample/Properties/launchSettings.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

samples/Redis/Program.cs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,19 +17,18 @@ public static void Main(string[] args)
1717
var redis = ConnectionMultiplexer.Connect("localhost:6379");
1818

1919
// Configure
20-
var serviceCollection = new ServiceCollection();
21-
serviceCollection.AddLogging();
22-
serviceCollection.AddDataProtection()
23-
.PersistKeysToRedis(redis, "DataProtection-Keys");
24-
25-
var services = serviceCollection.BuildServiceProvider();
26-
var loggerFactory = services.GetService<LoggerFactory>();
27-
loggerFactory.AddConsole();
28-
29-
// Run a sample payload
30-
var protector = services.GetDataProtector("sample-purpose");
31-
var protectedData = protector.Protect("Hello world!");
32-
Console.WriteLine(protectedData);
20+
using (var services = new ServiceCollection()
21+
.AddLogging(o => o.AddConsole().SetMinimumLevel(LogLevel.Debug))
22+
.AddDataProtection()
23+
.PersistKeysToRedis(redis, "DataProtection-Keys")
24+
.Services
25+
.BuildServiceProvider())
26+
{
27+
// Run a sample payload
28+
var protector = services.GetDataProtector("sample-purpose");
29+
var protectedData = protector.Protect("Hello world!");
30+
Console.WriteLine(protectedData);
31+
}
3332
}
3433
}
3534
}

samples/Redis/Properties/launchSettings.json

Lines changed: 0 additions & 22 deletions
This file was deleted.

samples/Redis/Redis.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
<Project Sdk="Microsoft.NET.Sdk.Web">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<Import Project="..\..\build\dependencies.props" />
44

55
<PropertyGroup>
66
<TargetFrameworks>net461;netcoreapp2.0</TargetFrameworks>
7+
<OutputType>exe</OutputType>
78
</PropertyGroup>
89

910
<ItemGroup>

src/Microsoft.AspNetCore.DataProtection/AuthenticatedEncryption/ConfigurationModel/AuthenticatedEncryptorConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Security.Cryptography;
66
using Microsoft.AspNetCore.Cryptography;
7+
using Microsoft.Extensions.Logging.Abstractions;
78

89
namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel
910
{
@@ -42,7 +43,7 @@ IAuthenticatedEncryptorDescriptor IInternalAlgorithmConfiguration.CreateDescript
4243

4344
void IInternalAlgorithmConfiguration.Validate()
4445
{
45-
var factory = new AuthenticatedEncryptorFactory(DataProtectionProviderFactory.GetDefaultLoggerFactory());
46+
var factory = new AuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
4647
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
4748
var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this);
4849
try

src/Microsoft.AspNetCore.DataProtection/AuthenticatedEncryption/ConfigurationModel/CngCbcAuthenticatedEncryptorConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.Cryptography;
5+
using Microsoft.Extensions.Logging.Abstractions;
56

67
namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel
78
{
@@ -88,7 +89,7 @@ IAuthenticatedEncryptorDescriptor IInternalAlgorithmConfiguration.CreateDescript
8889
/// </summary>
8990
void IInternalAlgorithmConfiguration.Validate()
9091
{
91-
var factory = new CngCbcAuthenticatedEncryptorFactory(DataProtectionProviderFactory.GetDefaultLoggerFactory());
92+
var factory = new CngCbcAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
9293
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
9394
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
9495
{

src/Microsoft.AspNetCore.DataProtection/AuthenticatedEncryption/ConfigurationModel/CngGcmAuthenticatedEncryptorConfiguration.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using Microsoft.AspNetCore.Cryptography;
5+
using Microsoft.Extensions.Logging.Abstractions;
56

67
namespace Microsoft.AspNetCore.DataProtection.AuthenticatedEncryption.ConfigurationModel
78
{
@@ -64,7 +65,7 @@ IAuthenticatedEncryptorDescriptor IInternalAlgorithmConfiguration.CreateDescript
6465
/// </summary>
6566
void IInternalAlgorithmConfiguration.Validate()
6667
{
67-
var factory = new CngGcmAuthenticatedEncryptorFactory(DataProtectionProviderFactory.GetDefaultLoggerFactory());
68+
var factory = new CngGcmAuthenticatedEncryptorFactory(NullLoggerFactory.Instance);
6869
// Run a sample payload through an encrypt -> decrypt operation to make sure data round-trips properly.
6970
using (var encryptor = factory.CreateAuthenticatedEncryptorInstance(Secret.Random(512 / 8), this))
7071
{

0 commit comments

Comments
 (0)