-
-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #227 from Aguafrommars/user/ole
Keys rotation refactoring
- Loading branch information
Showing
69 changed files
with
3,219 additions
and
164 deletions.
There are no files selected for viewing
This file contains 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 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
64 changes: 64 additions & 0 deletions
64
src/Aguacongas.TheIdServer/Extensions/IdentityServerBuilderExtensions.cs
This file contains 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,64 @@ | ||
using Aguacongas.IdentityServer.Admin.Configuration; | ||
using Aguacongas.IdentityServer.EntityFramework.Store; | ||
using Aguacongas.IdentityServer.KeysRotation; | ||
using Aguacongas.IdentityServer.KeysRotation.Extentions; | ||
using Aguacongas.TheIdServer.Models; | ||
using Microsoft.Extensions.Configuration; | ||
using StackExchange.Redis; | ||
using System; | ||
using System.IO; | ||
using System.Security.Cryptography.X509Certificates; | ||
|
||
namespace Microsoft.Extensions.DependencyInjection | ||
{ | ||
public static class IdentityServerBuilderExtensions | ||
{ | ||
public static IIdentityServerBuilder ConfigureKeysRotation(this IIdentityServerBuilder identityServerBuilder, IConfiguration configuration) | ||
{ | ||
var builder = identityServerBuilder.AddKeysRotation(options => configuration.GetSection(nameof(KeyRotationOptions))?.Bind(options)) | ||
.AddRsaEncryptorConfiguration(options => configuration.GetSection(nameof(RsaEncryptorConfiguration))?.Bind(options)); | ||
var dataProtectionsOptions = configuration.Get<DataProtectionOptions>(); | ||
switch (dataProtectionsOptions.StorageKind) | ||
{ | ||
case StorageKind.AzureStorage: | ||
builder.PersistKeysToAzureBlobStorage(new Uri(dataProtectionsOptions.StorageConnectionString)); | ||
break; | ||
case StorageKind.EntityFramework: | ||
builder.PersistKeysToDbContext<OperationalDbContext>(); | ||
break; | ||
case StorageKind.FileSytem: | ||
builder.PersistKeysToFileSystem(new DirectoryInfo(dataProtectionsOptions.StorageConnectionString)); | ||
break; | ||
case StorageKind.Redis: | ||
var redis = ConnectionMultiplexer.Connect(dataProtectionsOptions.StorageConnectionString); | ||
if (string.IsNullOrEmpty(dataProtectionsOptions.RedisKey)) | ||
{ | ||
builder.PersistKeysToStackExchangeRedis(redis); | ||
break; | ||
} | ||
builder.PersistKeysToStackExchangeRedis(redis, dataProtectionsOptions.RedisKey); | ||
break; | ||
} | ||
var protectOptions = dataProtectionsOptions.KeyProtectionOptions; | ||
if (protectOptions != null) | ||
{ | ||
switch (protectOptions.KeyProtectionKind) | ||
{ | ||
case KeyProtectionKind.AzureKeyVault: | ||
builder.ProtectKeysWithAzureKeyVault(protectOptions.AzureKeyVaultKeyId, protectOptions.AzureKeyVaultClientId, protectOptions.AzureKeyVaultClientSecret); | ||
break; | ||
case KeyProtectionKind.X509: | ||
if (!string.IsNullOrEmpty(protectOptions.X509CertificatePath)) | ||
{ | ||
var certificate = SigningKeysLoader.LoadFromFile(protectOptions.X509CertificatePath, protectOptions.X509CertificatePassword, X509KeyStorageFlags.Exportable | X509KeyStorageFlags.UserKeySet); | ||
builder.ProtectKeysWithCertificate(certificate); | ||
break; | ||
} | ||
builder.ProtectKeysWithCertificate(protectOptions.X509CertificateThumbprint); | ||
break; | ||
} | ||
} | ||
return identityServerBuilder; | ||
} | ||
} | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,7 +1,12 @@ | ||
{ | ||
"IdentityServer": { | ||
"Key": { | ||
"Type": "Development" | ||
} | ||
"Type": "KeysRotation", | ||
"StorageKind": "EntityFramework", | ||
"KeyRotationOptions": { | ||
"NewKeyLifetime": "14.00:00:00", | ||
"KeyPropagationWindow": "30.00:00:00" | ||
} | ||
} | ||
} | ||
} |
This file contains 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 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 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 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
48 changes: 48 additions & 0 deletions
48
...yServer/Aguacongas.IdentityServer.KeysRotation/AzureKeyVault/AzureKeyVaultXmlDecryptor.cs
This file contains 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,48 @@ | ||
// Copyright (c) .NET Foundation. All rights reserved. | ||
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. | ||
|
||
using System; | ||
using System.IO; | ||
using System.Threading.Tasks; | ||
using System.Xml.Linq; | ||
using Microsoft.AspNetCore.DataProtection.XmlEncryption; | ||
using Microsoft.Extensions.DependencyInjection; | ||
|
||
namespace Aguacongas.IdentityServer.KeysRotation.AzureKeyVault | ||
{ | ||
internal class AzureKeyVaultXmlDecryptor : IXmlDecryptor | ||
{ | ||
private readonly IKeyVaultWrappingClient _client; | ||
|
||
public AzureKeyVaultXmlDecryptor(IServiceProvider serviceProvider) | ||
{ | ||
_client = serviceProvider.GetService<IKeyVaultWrappingClient>(); | ||
} | ||
|
||
public XElement Decrypt(XElement encryptedElement) | ||
{ | ||
return DecryptAsync(encryptedElement).GetAwaiter().GetResult(); | ||
} | ||
|
||
private async Task<XElement> DecryptAsync(XElement encryptedElement) | ||
{ | ||
var kid = (string)encryptedElement.Element("kid"); | ||
var symmetricKey = Convert.FromBase64String((string)encryptedElement.Element("key")); | ||
var symmetricIV = Convert.FromBase64String((string)encryptedElement.Element("iv")); | ||
|
||
var encryptedValue = Convert.FromBase64String((string)encryptedElement.Element("value")); | ||
|
||
var result = await _client.UnwrapKeyAsync(kid, AzureKeyVaultXmlEncryptor.DefaultKeyEncryption, symmetricKey).ConfigureAwait(false); | ||
|
||
byte[] decryptedValue; | ||
using (var symmetricAlgorithm = AzureKeyVaultXmlEncryptor.DefaultSymmetricAlgorithmFactory()) | ||
{ | ||
using var decryptor = symmetricAlgorithm.CreateDecryptor(result.Result, symmetricIV); | ||
decryptedValue = decryptor.TransformFinalBlock(encryptedValue, 0, encryptedValue.Length); | ||
} | ||
|
||
using var memoryStream = new MemoryStream(decryptedValue); | ||
return XElement.Load(memoryStream); | ||
} | ||
} | ||
} |
Oops, something went wrong.