-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Support reading existing DataProtection KeyVault keys #14778
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
pakrym
merged 4 commits into
Azure:master
from
pakrym:pakrym/support-reading-existing-keys
Sep 2, 2020
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
5 changes: 4 additions & 1 deletion
5
sdk/extensions/Azure.Extensions.AspNetCore.DataProtection.Keys/CHANGELOG.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
...s/Azure.Extensions.AspNetCore.DataProtection.Keys/src/DecryptorTypeForwardingActivator.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System; | ||
| using System.Text.RegularExpressions; | ||
| using Microsoft.AspNetCore.DataProtection.Internal; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Extensions.Logging.Abstractions; | ||
|
|
||
| #pragma warning disable AZC0001 | ||
| namespace Microsoft.AspNetCore.DataProtection | ||
| #pragma warning restore AZC0001 | ||
| { | ||
| /// <summary> | ||
| /// This type is a copy of https://github.com/dotnet/aspnetcore/blob/e1bf38ccaf2a98f95e48bf22b8b76d996a0c33ea/src/DataProtection/DataProtection/test/TypeForwardingActivatorTests.cs | ||
| /// with addition of AzureKeyVaultXmlDecryptor forwarding logic. | ||
| /// </summary> | ||
| internal class DecryptorTypeForwardingActivator : IActivator | ||
| { | ||
| private readonly IServiceProvider _services; | ||
| private const string OldNamespace = "Microsoft.AspNet.DataProtection"; | ||
| private const string CurrentNamespace = "Microsoft.AspNetCore.DataProtection"; | ||
| private const string CurrentAzureNamespace = "Azure.Extensions.AspNetCore.DataProtection.Keys"; | ||
|
|
||
| private const string OldDecryptor = "Microsoft.AspNetCore.DataProtection.AzureKeyVault.AzureKeyVaultXmlDecryptor"; | ||
| private const string OldDecryptorAssembly = "Microsoft.AspNetCore.DataProtection.AzureKeyVault"; | ||
| private const string NewDecryptor = "Azure.Extensions.AspNetCore.DataProtection.Keys.AzureKeyVaultXmlDecryptor"; | ||
| private const string NewDecryptorAssembly = "Azure.Extensions.AspNetCore.DataProtection.Keys"; | ||
|
|
||
| private readonly ILogger _logger; | ||
| private static readonly Regex _versionPattern = new Regex(@",\s?Version=[0-9]+(\.[0-9]+){0,3}", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); | ||
| private static readonly Regex _tokenPattern = new Regex(@",\s?PublicKeyToken=[\w\d]+", RegexOptions.Compiled, TimeSpan.FromSeconds(2)); | ||
|
|
||
| public DecryptorTypeForwardingActivator(IServiceProvider services) | ||
| : this(services, NullLoggerFactory.Instance) | ||
| { | ||
| } | ||
|
|
||
| public DecryptorTypeForwardingActivator(IServiceProvider services, ILoggerFactory loggerFactory) | ||
| { | ||
| _services = services; | ||
| _logger = loggerFactory.CreateLogger(typeof(DecryptorTypeForwardingActivator)); | ||
| } | ||
|
|
||
| public object CreateInstance(Type expectedBaseType, string originalTypeName) | ||
| => CreateInstance(expectedBaseType, originalTypeName, out var _); | ||
|
|
||
| // for testing | ||
| internal object CreateInstance(Type expectedBaseType, string originalTypeName, out bool forwarded) | ||
| { | ||
| var forwardedTypeName = originalTypeName; | ||
| var candidate = false; | ||
| if (originalTypeName.Contains(OldNamespace)) | ||
| { | ||
| candidate = true; | ||
| forwardedTypeName = originalTypeName.Replace(OldNamespace, CurrentNamespace); | ||
| } | ||
|
|
||
| if (originalTypeName.Contains(OldDecryptor)) | ||
| { | ||
| candidate = true; | ||
| forwardedTypeName = originalTypeName | ||
| .Replace(OldDecryptor, NewDecryptor) | ||
| .Replace(OldDecryptorAssembly, NewDecryptorAssembly); | ||
| } | ||
|
|
||
| if ((candidate || forwardedTypeName.StartsWith(CurrentNamespace + ".", StringComparison.Ordinal)) || | ||
| forwardedTypeName.StartsWith(CurrentAzureNamespace + ".", StringComparison.Ordinal)) | ||
| { | ||
| candidate = true; | ||
| forwardedTypeName = RemoveVersionFromAssemblyName(forwardedTypeName); | ||
| forwardedTypeName = RemovePublicKeyTokenFromAssemblyName(forwardedTypeName); | ||
| } | ||
|
|
||
| if (candidate) | ||
| { | ||
| var type = Type.GetType(forwardedTypeName, false); | ||
| if (type != null) | ||
| { | ||
| _logger.LogDebug("Forwarded activator type request from {FromType} to {ToType}", | ||
| originalTypeName, | ||
| forwardedTypeName); | ||
| forwarded = true; | ||
| return CreateInstanceImpl(expectedBaseType, forwardedTypeName); | ||
| } | ||
| } | ||
|
|
||
| forwarded = false; | ||
| return CreateInstanceImpl(expectedBaseType, originalTypeName); | ||
| } | ||
|
|
||
| private static string RemovePublicKeyTokenFromAssemblyName(string forwardedTypeName) | ||
| => _tokenPattern.Replace(forwardedTypeName, ""); | ||
|
|
||
| internal static string RemoveVersionFromAssemblyName(string forwardedTypeName) | ||
| => _versionPattern.Replace(forwardedTypeName, ""); | ||
|
|
||
| private object CreateInstanceImpl(Type expectedBaseType, string implementationTypeName) | ||
| { | ||
| // Would the assignment even work? | ||
| var implementationType = Type.GetType(implementationTypeName, throwOnError: true); | ||
|
|
||
| if (!expectedBaseType.IsAssignableFrom(implementationType)) | ||
| { | ||
| // It might seem a bit strange to throw an InvalidCastException explicitly rather than | ||
| // to let the CLR generate one, but searching through NetFX there is indeed precedent | ||
| // for this pattern when the caller knows ahead of time the operation will fail. | ||
| throw new InvalidCastException($"The type '{implementationType.AssemblyQualifiedName}' is not assignable to '{expectedBaseType.AssemblyQualifiedName}'."); | ||
| } | ||
|
|
||
| // If no IServiceProvider was specified, prefer .ctor() [if it exists] | ||
| if (_services == null) | ||
| { | ||
| var ctorParameterless = implementationType.GetConstructor(Type.EmptyTypes); | ||
| if (ctorParameterless != null) | ||
| { | ||
| return Activator.CreateInstance(implementationType); | ||
| } | ||
| } | ||
|
|
||
| // If an IServiceProvider was specified or if .ctor() doesn't exist, prefer .ctor(IServiceProvider) [if it exists] | ||
| var ctorWhichTakesServiceProvider = implementationType.GetConstructor(new Type[] { typeof(IServiceProvider) }); | ||
| if (ctorWhichTakesServiceProvider != null) | ||
| { | ||
| return ctorWhichTakesServiceProvider.Invoke(new[] { _services }); | ||
| } | ||
|
|
||
| // Finally, prefer .ctor() as an ultimate fallback. | ||
| // This will throw if the ctor cannot be called. | ||
| return Activator.CreateInstance(implementationType); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.