-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return null key if access is denied (#13317)
* Return null key if access is denied Fixes #11574. IKeyEncryptionResolver.Resolve is designed to delegate access verification to the resolver, so that any access issues are thrown downstream at the call site instead of upstream when trying to get the resolver/client. * Resolve offline and PR feedback
- Loading branch information
Showing
2 changed files
with
78 additions
and
7 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
68 changes: 68 additions & 0 deletions
68
sdk/keyvault/Azure.Security.KeyVault.Keys/tests/KeyResolverMockTests.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,68 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Net; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Core.Testing; | ||
using Azure.Security.KeyVault.Keys.Cryptography; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Security.KeyVault.Keys.Tests | ||
{ | ||
public class KeyResolverMockTests : ClientTestBase | ||
{ | ||
public KeyResolverMockTests(bool isAsync) : base(isAsync) | ||
{ | ||
} | ||
|
||
[Test] | ||
public async Task ShouldNotRequireGetPermissionForKey() | ||
{ | ||
// Test for https://github.com/Azure/azure-sdk-for-net/issues/11574 | ||
MockTransport transport = new MockTransport(request => new MockResponse((int)HttpStatusCode.Forbidden, nameof(HttpStatusCode.Forbidden))); | ||
|
||
KeyResolver resolver = GetResolver(transport); | ||
CryptographyClient client = await resolver.ResolveAsync(new Uri("https://mock.vault.azure.net/keys/mock-key")); | ||
|
||
RequestFailedException ex = Assert.ThrowsAsync<RequestFailedException>(() => client.UnwrapKeyAsync(KeyWrapAlgorithm.A256KW, new byte[] { 0, 1, 2, 3 })); | ||
Assert.AreEqual((int)HttpStatusCode.Forbidden, ex.Status); | ||
} | ||
|
||
[Test] | ||
public void ShouldRequireGetPermissionForSecret() | ||
{ | ||
// Test for https://github.com/Azure/azure-sdk-for-net/issues/11574 | ||
MockTransport transport = new MockTransport(request => new MockResponse((int)HttpStatusCode.Forbidden, nameof(HttpStatusCode.Forbidden))); | ||
|
||
KeyResolver resolver = GetResolver(transport); | ||
|
||
RequestFailedException ex = Assert.ThrowsAsync<RequestFailedException>(() => resolver.ResolveAsync(new Uri("https://mock.vault.azure.net/secrets/mock-secret"))); | ||
Assert.AreEqual((int)HttpStatusCode.Forbidden, ex.Status); | ||
} | ||
|
||
protected KeyResolver GetResolver(MockTransport transport) | ||
{ | ||
Assert.NotNull(transport); | ||
|
||
CryptographyClientOptions options = new CryptographyClientOptions | ||
{ | ||
Transport = transport, | ||
}; | ||
|
||
return InstrumentClient( | ||
new KeyResolver(new NullTokenCredential(), options)); | ||
} | ||
|
||
private class NullTokenCredential : TokenCredential | ||
{ | ||
public override AccessToken GetToken(TokenRequestContext requestContext, CancellationToken cancellationToken) => | ||
new AccessToken("invalid", DateTimeOffset.Now.AddHours(1)); | ||
|
||
public override ValueTask<AccessToken> GetTokenAsync(TokenRequestContext requestContext, CancellationToken cancellationToken) => | ||
new ValueTask<AccessToken>(GetToken(requestContext, cancellationToken)); | ||
} | ||
} | ||
} |