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

Refactored DI support #203

Merged
merged 1 commit into from
Mar 15, 2017
Merged

Refactored DI support #203

merged 1 commit into from
Mar 15, 2017

Conversation

ajaybhargavb
Copy link
Contributor

Issue - #134

  • Refactored builder extensions and service collection extensions
  • Refactored Settings/Configuration/Descriptor
  • Removed ConfigurationCommon/AuthenticatedEncryptorConfigurationExtensions
  • Added IAuthenticatedEncryptorFactory and implementations
  • Refactored IKey to have Descriptor instead of CreateEncryptorInstance()
  • Handled Repository/Encryptor special logic
  • Added samples
  • Updated tests

@rynowak @davidfowl @blowdart

@guardrex
Copy link

@ajaybhargavb Will these changes fix #154?

TL;DR ... The prob described in that issue was that DI wasn't working for IXmlDecryptor ...

public class CustomXmlDecryptor : IXmlDecryptor
{
    private byte[] _key;

    public CustomXmlDecryptor()
    {
    }

    public CustomXmlDecryptor(IOptions<CustomDataProtectionOptions> dataProtectionOptions)
    {
        _key = dataProtectionOptions.Value.Key;
    }

    public XElement Decrypt(XElement encryptedElement)
    {
        // Code here uses _key to decrypt
    }
}

_key wasn't getting a value, so I've had to use ...

public CustomXmlDecryptor(IServiceProvider services)
{
    var dataProtectionOptions = 
        services.GetRequiredService<IOptions<CustomDataProtectionOptions>>();
    _key = dataProtectionOptions.Value.Key;
}

... to make it work.

@ajaybhargavb
Copy link
Contributor Author

@guardrex, this change won't fix the issue that you referenced. It was never supported. IXmlDecryptor was not refactored as part of this change because it gets serialized and changing it would break existing applications.

@ajaybhargavb ajaybhargavb force-pushed the ajbaaska/refactor-dp-final branch from 4df6218 to 8ee57f7 Compare February 23, 2017 19:54
var serviceCollection = new ServiceCollection();
serviceCollection.AddLogging();
serviceCollection.AddDataProtection()
.PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp-keys"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this use some other path for the sake of the sample? Does this work on non-Windows?

var serviceCollection = new ServiceCollection();
serviceCollection.AddDataProtection()
// point at a specific folder and use DPAPI to encrypt keys
.PersistKeysToFileSystem(new DirectoryInfo(@"c:\temp-keys"))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

// one key in the key ring
services.GetDataProtector("Sample.KeyManager.v1").Protect("payload");
Console.WriteLine("Performed a protect operation.");
Thread.Sleep(2000);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for?

{
// get the path to %LOCALAPPDATA%\myapp-keys
var destFolder = Path.Combine(
Environment.GetEnvironmentVariable("LOCALAPPDATA"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens on non-Windows?

{
throw Error.Common_PropertyCannotBeNullOrEmpty(nameof(configuration.EncryptionAlgorithmType));
}
typeof(SymmetricAlgorithm).AssertIsAssignableFrom(configuration.EncryptionAlgorithmType);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Talked offline

@@ -118,7 +107,7 @@ internal static string GetDefaultProtectionDescriptorString()
using (var currentIdentity = WindowsIdentity.GetCurrent())
{
// use the SID to create an SDDL string
return Invariant($"SID={currentIdentity.User.Value}");
return String.Format(CultureInfo.InvariantCulture, "SID={0}", currentIdentity.User.Value);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string!

@@ -107,7 +87,11 @@ private static RegistryKey GetDefaultHklmStorageKey()
// Even though this is in HKLM, WAS ensures that applications hosted in IIS are properly isolated.
// See APP_POOL::EnsureSharedMachineKeyStorage in WAS source for more info.
// The version number will need to change if IIS hosts Core CLR directly.
var aspnetAutoGenKeysBaseKeyName = Invariant($@"SOFTWARE\Microsoft\ASP.NET\4.0.30319.0\AutoGenKeys\{WindowsIdentity.GetCurrent().User.Value}");
var aspnetAutoGenKeysBaseKeyName = String.Format(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

string! And elsewhere, too.

@@ -21,4 +21,8 @@
<PackageReference Include="xunit" Version="2.2.0-*" />
</ItemGroup>

<ItemGroup>
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does VS just put this here? If we remove it, will VS put it back? (If so, leave it.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, VS will just put this back.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah ok that's fine then.


private static IAuthenticatedEncryptor CreateEncryptorInstanceFromDescriptor(CngGcmAuthenticatedEncryptorDescriptor descriptor)
{
var key = new Key(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ick, can this use named params?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

IServiceCollection serviceCollection = new ServiceCollection();
RunTestWithRegValues(serviceCollection, new Dictionary<string, object>()
// Arrange
var registryEntries = new Dictionary<string, object>()
{
["unused"] = 42
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If it's unused, do we need to set any values in the registry at all?

@ajaybhargavb
Copy link
Contributor Author

🆙 📅

Copy link
Member

@Eilon Eilon left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😄

/// Settings for configuring authenticated encryption algorithms.
/// </summary>
public sealed class AuthenticatedEncryptionSettings : IInternalAuthenticatedEncryptionSettings
public class AuthenticatedEncryptorFactory : IAuthenticatedEncryptorFactory
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be sealed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes it should. Looks like I missed this one. Good catch 👍

- Refactored builder extensions and service collection extensions
- Refactored Settings/Configuration/Descriptor
- Removed ConfigurationCommon/AuthenticatedEncryptorConfigurationExtensions
- Added IAuthenticatedEncryptorFactory and implementations
- Refactored IKey to have Descriptor instead of CreateEncryptorInstance()
- Handled Repository/Encryptor special logic
- Added samples
- Updated tests
@ajaybhargavb ajaybhargavb force-pushed the ajbaaska/refactor-dp-final branch from 6db8037 to cde3b96 Compare March 15, 2017 03:29
@ajaybhargavb ajaybhargavb merged commit cde3b96 into dev Mar 15, 2017
@ajaybhargavb ajaybhargavb deleted the ajbaaska/refactor-dp-final branch March 15, 2017 03:31
@ajaybhargavb
Copy link
Contributor Author

Thanks @Eilon @rynowak @GrabYourPitchforks

ajaybhargavb added a commit to aspnet/Security that referenced this pull request Mar 15, 2017
@rynowak
Copy link
Member

rynowak commented Mar 15, 2017

🎉

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants