This repository has been archived by the owner on Nov 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 21
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 #4 from hotchkj/add-xmlkeymanager-tests
Fixes #3. Adds XmlKeyManager tests for S3 & KMS integrations so that …
- Loading branch information
Showing
12 changed files
with
230 additions
and
17 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
53 changes: 53 additions & 0 deletions
53
integrate/AspNetCore.DataProtection.Aws.IntegrationTests/EphemeralXmlRepository.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,53 @@ | ||
// 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 Microsoft.AspNet.DataProtection.Repositories; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Xml.Linq; | ||
|
||
namespace AspNetCore.DataProtection.Aws.IntegrationTests | ||
{ | ||
/// <summary> | ||
/// Borrowed straight from https://github.com/aspnet/DataProtection/blob/master/src/Microsoft.AspNetCore.DataProtection/Repositories/EphemeralXmlRepository.cs | ||
/// since Microsoft made this internal, which makes external testing that much harder | ||
/// </summary> | ||
internal class EphemeralXmlRepository : IXmlRepository | ||
{ | ||
private readonly List<XElement> _storedElements = new List<XElement>(); | ||
|
||
public virtual IReadOnlyCollection<XElement> GetAllElements() | ||
{ | ||
// force complete enumeration under lock for thread safety | ||
lock (_storedElements) | ||
{ | ||
return GetAllElementsCore().ToList().AsReadOnly(); | ||
} | ||
} | ||
|
||
private IEnumerable<XElement> GetAllElementsCore() | ||
{ | ||
// this method must be called under lock | ||
foreach (XElement element in _storedElements) | ||
{ | ||
yield return new XElement(element); // makes a deep copy so caller doesn't inadvertently modify it | ||
} | ||
} | ||
|
||
public virtual void StoreElement(XElement element, string friendlyName) | ||
{ | ||
if (element == null) | ||
{ | ||
throw new ArgumentNullException(nameof(element)); | ||
} | ||
|
||
XElement cloned = new XElement(element); // makes a deep copy so caller doesn't inadvertently modify it | ||
|
||
// under lock for thread safety | ||
lock (_storedElements) | ||
{ | ||
_storedElements.Add(cloned); | ||
} | ||
} | ||
} | ||
} |
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
63 changes: 63 additions & 0 deletions
63
integrate/AspNetCore.DataProtection.Aws.IntegrationTests/KmsManagerIntegrationTests.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,63 @@ | ||
// Copyright(c) 2016 Jeff Hotchkiss | ||
// Licensed under the MIT License. See License.md in the project root for license information. | ||
using Amazon; | ||
using Amazon.KeyManagementService; | ||
using AspNetCore.DataProtection.Aws.Kms; | ||
using Microsoft.AspNet.DataProtection.AuthenticatedEncryption; | ||
using Microsoft.AspNet.DataProtection.AuthenticatedEncryption.ConfigurationModel; | ||
using Microsoft.AspNet.DataProtection.KeyManagement; | ||
using Microsoft.AspNet.DataProtection.Repositories; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Linq; | ||
using Xunit; | ||
|
||
namespace AspNetCore.DataProtection.Aws.IntegrationTests | ||
{ | ||
public class KmsManagerIntegrationTests : IDisposable | ||
{ | ||
private readonly IAmazonKeyManagementService kmsClient; | ||
|
||
public KmsManagerIntegrationTests() | ||
{ | ||
// Expectation that local SDK has been configured correctly, whether via VS Tools or user config files | ||
kmsClient = new AmazonKeyManagementServiceClient(RegionEndpoint.EUWest1); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
kmsClient.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public void ExpectFullKeyManagerStoreRetrieveToSucceed() | ||
{ | ||
var config = new KmsXmlEncryptorConfig(KmsIntegrationTests.ApplicationName, KmsIntegrationTests.KmsTestingKey); | ||
|
||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddInstance(kmsClient); | ||
serviceCollection.AddInstance<IAuthenticatedEncryptorConfiguration>(new AuthenticatedEncryptorConfiguration(new AuthenticatedEncryptionOptions())); | ||
serviceCollection.AddDataProtection(); | ||
serviceCollection.ConfigureDataProtection(configure => | ||
{ | ||
configure.ProtectKeysWithAwsKms(config); | ||
}); | ||
serviceCollection.AddInstance<IXmlRepository>(new EphemeralXmlRepository()); | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
var keyManager = new XmlKeyManager(serviceProvider.GetRequiredService<IXmlRepository>(), | ||
serviceProvider.GetRequiredService<IAuthenticatedEncryptorConfiguration>(), | ||
serviceProvider); | ||
|
||
var activationDate = new DateTimeOffset(new DateTime(1980, 1, 1)); | ||
var expirationDate = new DateTimeOffset(new DateTime(1980, 6, 1)); | ||
keyManager.CreateNewKey(activationDate, expirationDate); | ||
|
||
var keys = keyManager.GetAllKeys(); | ||
|
||
Assert.Equal(1, keys.Count); | ||
Assert.Equal(activationDate, keys.Single().ActivationDate); | ||
Assert.Equal(expirationDate, keys.Single().ExpirationDate); | ||
} | ||
} | ||
} |
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
88 changes: 88 additions & 0 deletions
88
integrate/AspNetCore.DataProtection.Aws.IntegrationTests/S3ManagerIntegrationTests.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,88 @@ | ||
// Copyright(c) 2016 Jeff Hotchkiss | ||
// Licensed under the MIT License. See License.md in the project root for license information. | ||
using Amazon; | ||
using Amazon.S3; | ||
using Amazon.S3.Model; | ||
using AspNetCore.DataProtection.Aws.S3; | ||
using Microsoft.AspNet.DataProtection.AuthenticatedEncryption; | ||
using Microsoft.AspNet.DataProtection.AuthenticatedEncryption.ConfigurationModel; | ||
using Microsoft.AspNet.DataProtection.KeyManagement; | ||
using Microsoft.AspNet.DataProtection.Repositories; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Xunit; | ||
|
||
namespace AspNetCore.DataProtection.Aws.IntegrationTests | ||
{ | ||
public sealed class S3ManagerIntegrationTests : IDisposable | ||
{ | ||
private readonly IAmazonS3 s3client; | ||
|
||
public S3ManagerIntegrationTests() | ||
{ | ||
// Expectation that local SDK has been configured correctly, whether via VS Tools or user config files | ||
s3client = new AmazonS3Client(RegionEndpoint.EUWest1); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
s3client.Dispose(); | ||
} | ||
|
||
[Fact] | ||
public async Task ExpectFullKeyManagerStoreRetrieveToSucceed() | ||
{ | ||
var config = new S3XmlRepositoryConfig(S3IntegrationTests.BucketName); | ||
config.KeyPrefix = "RealXmlKeyManager/"; | ||
await ClearKeys(config.KeyPrefix); | ||
|
||
var serviceCollection = new ServiceCollection(); | ||
serviceCollection.AddInstance(s3client); | ||
serviceCollection.AddInstance<IAuthenticatedEncryptorConfiguration>(new AuthenticatedEncryptorConfiguration(new AuthenticatedEncryptionOptions())); | ||
serviceCollection.AddDataProtection(); | ||
serviceCollection.ConfigureDataProtection(configure => | ||
{ | ||
configure.PersistKeysToAwsS3(config); | ||
}); | ||
var serviceProvider = serviceCollection.BuildServiceProvider(); | ||
|
||
var keyManager = new XmlKeyManager(serviceProvider.GetRequiredService<IXmlRepository>(), | ||
serviceProvider.GetRequiredService<IAuthenticatedEncryptorConfiguration>(), | ||
serviceProvider); | ||
|
||
var activationDate = new DateTimeOffset(new DateTime(1980, 1, 1)); | ||
var expirationDate = new DateTimeOffset(new DateTime(1980, 6, 1)); | ||
keyManager.CreateNewKey(activationDate, expirationDate); | ||
|
||
var keys = keyManager.GetAllKeys(); | ||
|
||
Assert.Equal(1, keys.Count); | ||
Assert.Equal(activationDate, keys.Single().ActivationDate); | ||
Assert.Equal(expirationDate, keys.Single().ExpirationDate); | ||
} | ||
|
||
private async Task ClearKeys(string prefix) | ||
{ | ||
// XmlKeyManager uses a GUID for the naming so we cannot overwrite the same entry in the test | ||
// Thus we must first clear out any keys that old tests put in | ||
|
||
var listed = await s3client.ListObjectsV2Async(new ListObjectsV2Request | ||
{ | ||
BucketName = S3IntegrationTests.BucketName, | ||
Prefix = prefix | ||
}); | ||
|
||
// In sequence as we do not expect more than one or two of these assuming the tests work properly | ||
foreach (var s3Obj in listed.S3Objects) | ||
{ | ||
await s3client.DeleteObjectAsync(new DeleteObjectRequest | ||
{ | ||
BucketName = S3IntegrationTests.BucketName, | ||
Key = s3Obj.Key | ||
}); | ||
} | ||
} | ||
} | ||
} |
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
6 changes: 3 additions & 3 deletions
6
src/AspNetCore.DataProtection.Aws.S3/DataProtectionBuilderExtensions.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
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