Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

<ItemGroup>
<PackageReference Include="Azure.Core" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" VersionOverride="8.0.11"/>
<PackageReference Include="Azure.Storage.Blobs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" VersionOverride="8.0.1" />
<PackageReference Include="Moq" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

<ItemGroup>
<PackageReference Include="Azure.Core" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" />
<PackageReference Include="Microsoft.AspNetCore.DataProtection" VersionOverride="8.0.11"/>
<PackageReference Include="Azure.Security.KeyVault.Keys" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFrameworks>$(RequiredTargetFrameworks)</TargetFrameworks>
Expand All @@ -9,10 +9,8 @@
<PackageReference Include="NUnit" />
<PackageReference Include="NUnit3TestAdapter" />
<PackageReference Include="Microsoft.NET.Test.Sdk" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" VersionOverride="8.0.1" />
<PackageReference Include="Moq" />

<PackageReference Include="Microsoft.AspNetCore.DataProtection.AzureKeyVault" VersionOverride="3.1.7" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@
using Azure.Identity;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
using Microsoft.Azure.KeyVault;
using Azure.Security.KeyVault.Keys;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using NUnit.Framework;
using Azure.Core.TestFramework;

namespace Azure.Extensions.AspNetCore.DataProtection.Keys.Tests
{
Expand All @@ -19,7 +20,7 @@ public class AzureDataProtectionBuilderExtensionsTests
public void ProtectKeysWithAzureKeyVault_UsesAzureKeyVaultXmlEncryptor()
{
// Arrange
var client = new KeyVaultClient((_, _, _) => Task.FromResult(string.Empty));
var client = new KeyClient(new Uri("http://www.example.com/dummyKey"),new MockCredential());
var serviceCollection = new ServiceCollection();
var builder = serviceCollection.AddDataProtection();

Expand All @@ -36,7 +37,7 @@ public void ProtectKeysWithAzureKeyVault_UsesAzureKeyVaultXmlEncryptor()
public void ProtectKeysWithAzureKeyVault_WithServiceProviderFunc_UsesAzureKeyVaultXmlEncryptor()
{
// Arrange
var client = new KeyVaultClient((_, _, _) => Task.FromResult(string.Empty));
var client = new KeyClient(new Uri("http://www.example.com/dummyKey"), new MockCredential());
var serviceCollection = new ServiceCollection();
var builder = serviceCollection.AddDataProtection();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
using System.Threading.Tasks;
using System.Xml.Linq;
using Azure.Core.TestFramework;
using Azure.Extensions.AspNetCore.DataProtection.Keys.Tests;
using Azure.Identity;
using Azure.Security.KeyVault.Keys;
using Microsoft.AspNetCore.DataProtection;
using Microsoft.AspNetCore.DataProtection.KeyManagement;
Expand Down Expand Up @@ -56,7 +54,7 @@ public async Task ProtectsKeysWithKeyVaultKey()
}

[Test]
public async Task CanUprotectExistingKeys()
public async Task CanDecryptEncryptedKeys()
{
var client = new KeyClient(new Uri(TestEnvironment.KeyVaultUrl), TestEnvironment.Credential);
var key = await client.CreateKeyAsync("TestEncryptionKey2", KeyType.Rsa);
Expand All @@ -65,32 +63,23 @@ public async Task CanUprotectExistingKeys()

var testKeyRepository = new TestKeyRepository();

AzureDataProtectionBuilderExtensions.ProtectKeysWithAzureKeyVault(
serviceCollection.AddDataProtection(),
key.Value.Id.AbsoluteUri,
TestEnvironment.ClientId,
TestEnvironment.ClientSecret);
// Configure data protection to use TokenCredential
serviceCollection.AddDataProtection()
.ProtectKeysWithAzureKeyVault(key.Value.Id, TestEnvironment.Credential);

serviceCollection.Configure<KeyManagementOptions>(options =>
{
options.XmlRepository = testKeyRepository;
});

var servicesOld = serviceCollection.BuildServiceProvider();

var serviceCollectionNew = new ServiceCollection();
serviceCollectionNew.AddDataProtection().ProtectKeysWithAzureKeyVault(key.Value.Id, TestEnvironment.Credential);
serviceCollectionNew.Configure<KeyManagementOptions>(options =>
{
options.XmlRepository = testKeyRepository;
});
var services = serviceCollection.BuildServiceProvider();

var dataProtector = servicesOld.GetService<IDataProtectionProvider>().CreateProtector("Fancy purpose");
// Encrypt data
var dataProtector = services.GetService<IDataProtectionProvider>().CreateProtector("Fancy purpose");
var protectedText = dataProtector.Protect("Hello world!");

var newServices = serviceCollectionNew.BuildServiceProvider();
var newDataProtectionProvider = newServices.GetService<IDataProtectionProvider>().CreateProtector("Fancy purpose");
var unprotectedText = newDataProtectionProvider.Unprotect(protectedText);
// Decrypt data
var unprotectedText = dataProtector.Unprotect(protectedText);

Assert.AreEqual("Hello world!", unprotectedText);

Expand Down