-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Key Vault] Add CAE support & update System.ClientModel dependency ve…
…rsion (#46013) * Add flag and enable CAE to AuthorizeRequestInternal * Enable CAE for AuthorizeRequestOnChallenge * Add flag in SecretClientOption and SecretClient * Revert "Add flag in SecretClientOption and SecretClient" This reverts commit 02a805d. * Enable CAE by default * Removing unused parameter * Remove saving the claims in the cache * Update Changelog * Update changelogs * Simplify error checking logic Addressing comment in PR * Add test for base64 claims * Override Process function to handle the first CAE Challenge after a scope challenge * Add tests * Separate credential and client transports and assert for a 401. * Nest rety inside challenge if block * Add test for claims in token * Fix CI by removing extra test case parameter * Nit changes to tests * Simplify tests * removing unnecessary mock responses * Refactor tests to test CAE in all projects * Make tests non parallelizable * Add setup method to CAE tests * Test for tokens obtained from cae challenges * Fix test / CI * Update dependency for System.ClientModel * Apply suggestions
- Loading branch information
1 parent
991f2e6
commit bb158f4
Showing
13 changed files
with
795 additions
and
3 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
109 changes: 109 additions & 0 deletions
109
sdk/keyvault/Azure.Security.KeyVault.Administration/tests/ContinuousAccessEvaluationTests.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,109 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core.TestFramework; | ||
using Azure.Security.KeyVault.Tests; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Security.KeyVault.Administration.Tests | ||
{ | ||
[NonParallelizable] | ||
internal class ContinuousAccessEvaluationTests : ContinuousAccessEvaluationTestsBase | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
ChallengeBasedAuthenticationPolicy.ClearCache(); | ||
} | ||
|
||
[Test] | ||
[TestCase(@"Bearer realm="""", authorization_uri=""https://login.microsoftonline.com/common/oauth2/authorize"", error=""insufficient_claims"", claims=""eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwidmFsdWUiOiIxNzI2MDc3NTk1In0sInhtc19jYWVlcnJvciI6eyJ2YWx1ZSI6IjEwMDEyIn19fQ==""", """{"access_token":{"nbf":{"essential":true,"value":"1726077595"},"xms_caeerror":{"value":"10012"}}}""")] | ||
public async Task VerifyCaeClaims(string challenge, string expectedClaims) | ||
{ | ||
int callCount = 0; | ||
|
||
MockResponse response = new MockResponse(200); | ||
|
||
MockTransport transport = GetMockTransportWithCaeChallenges(numberOfCaeChallenges: 1, final200response: response); | ||
|
||
var credential = new TokenCredentialStub((r, c) => | ||
{ | ||
if (callCount == 0) | ||
{ | ||
// The first challenge should not have any claims. | ||
Assert.IsNull(r.Claims); | ||
} | ||
else if (callCount == 1) | ||
{ | ||
Assert.AreEqual(expectedClaims, r.Claims); | ||
} | ||
else | ||
{ | ||
Assert.Fail("unexpected token request"); | ||
} | ||
Interlocked.Increment(ref callCount); | ||
Assert.AreEqual(true, r.IsCaeEnabled); | ||
|
||
return new(callCount.ToString(), DateTimeOffset.Now.AddHours(2)); | ||
}, true); | ||
|
||
KeyVaultBackupClient client = new( | ||
VaultUri, | ||
credential, | ||
new KeyVaultAdministrationClientOptions() | ||
{ | ||
Transport = transport, | ||
}); | ||
|
||
try | ||
{ | ||
KeyVaultBackupOperation operation = await client.StartBackupAsync(VaultUri); | ||
} | ||
catch (RequestFailedException ex) | ||
{ | ||
Assert.AreEqual(200, ex.Status); | ||
return; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.Fail($"Expected RequestFailedException, but got {ex.GetType()}"); | ||
return; | ||
} | ||
} | ||
|
||
[Test] | ||
public void ThrowsWithTwoConsecutiveCaeChallenges() | ||
{ | ||
MockTransport keyVaultTransport = GetMockTransportWithCaeChallenges(numberOfCaeChallenges: 2); | ||
|
||
MockTransport credentialTransport = GetMockCredentialTransport(2); | ||
|
||
KeyVaultBackupClient client = new( | ||
VaultUri, | ||
new MockCredential(credentialTransport), | ||
new KeyVaultAdministrationClientOptions() | ||
{ | ||
Transport = keyVaultTransport, | ||
}); | ||
|
||
try | ||
{ | ||
var operation = client.StartBackup(VaultUri); | ||
} | ||
catch (RequestFailedException ex) | ||
{ | ||
Assert.AreEqual(401, ex.Status); | ||
return; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.Fail($"Expected RequestFailedException, but got {ex.GetType()}"); | ||
return; | ||
} | ||
Assert.Fail("Expected RequestFailedException, but no exception was thrown."); | ||
} | ||
} | ||
} |
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
106 changes: 106 additions & 0 deletions
106
sdk/keyvault/Azure.Security.KeyVault.Certificates/tests/ContinuousAccessEvaluationTests.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,106 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core.TestFramework; | ||
using Azure.Security.KeyVault.Tests; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Security.KeyVault.Certificates.Tests | ||
{ | ||
[NonParallelizable] | ||
internal class ContinuousAccessEvaluationTests : ContinuousAccessEvaluationTestsBase | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
ChallengeBasedAuthenticationPolicy.ClearCache(); | ||
} | ||
|
||
[Test] | ||
[TestCase(@"Bearer realm="""", authorization_uri=""https://login.microsoftonline.com/common/oauth2/authorize"", error=""insufficient_claims"", claims=""eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwidmFsdWUiOiIxNzI2MDc3NTk1In0sInhtc19jYWVlcnJvciI6eyJ2YWx1ZSI6IjEwMDEyIn19fQ==""", """{"access_token":{"nbf":{"essential":true,"value":"1726077595"},"xms_caeerror":{"value":"10012"}}}""")] | ||
public async Task VerifyCaeClaims(string challenge, string expectedClaims) | ||
{ | ||
int callCount = 0; | ||
|
||
MockResponse responseWithSecret = new MockResponse(200) | ||
.WithContent(@"{ | ||
""id"": ""https://foo.vault.azure.net/certificates/1/foo"", | ||
""cer"": ""Zm9v"", | ||
""attributes"": { | ||
}, | ||
""pending"": { | ||
""id"": ""foo"" | ||
} | ||
}"); | ||
|
||
MockTransport transport = GetMockTransportWithCaeChallenges(numberOfCaeChallenges: 1, final200response: responseWithSecret); | ||
|
||
var credential = new TokenCredentialStub((r, c) => | ||
{ | ||
if (callCount == 0) | ||
{ | ||
// The first challenge should not have any claims. | ||
Assert.IsNull(r.Claims); | ||
} | ||
else if (callCount == 1) | ||
{ | ||
Assert.AreEqual(expectedClaims, r.Claims); | ||
} | ||
else | ||
{ | ||
Assert.Fail("unexpected token request"); | ||
} | ||
Interlocked.Increment(ref callCount); | ||
Assert.AreEqual(true, r.IsCaeEnabled); | ||
|
||
return new(callCount.ToString(), DateTimeOffset.Now.AddHours(2)); | ||
}, true); | ||
|
||
CertificateClient client = new( | ||
VaultUri, | ||
credential, | ||
new CertificateClientOptions() | ||
{ | ||
Transport = transport, | ||
}); | ||
|
||
Response<KeyVaultCertificateWithPolicy> response = await client.GetCertificateAsync("certificate"); | ||
Assert.AreEqual(200, response.GetRawResponse().Status); | ||
} | ||
|
||
[Test] | ||
public void ThrowsWithTwoConsecutiveCaeChallenges() | ||
{ | ||
MockTransport keyVaultTransport = GetMockTransportWithCaeChallenges(numberOfCaeChallenges: 2); | ||
|
||
MockTransport credentialTransport = GetMockCredentialTransport(2); | ||
|
||
CertificateClient client = new( | ||
VaultUri, | ||
new MockCredential(credentialTransport), | ||
new CertificateClientOptions() | ||
{ | ||
Transport = keyVaultTransport, | ||
}); | ||
|
||
try | ||
{ | ||
client.GetCertificate("certificate"); | ||
} | ||
catch (RequestFailedException ex) | ||
{ | ||
Assert.AreEqual(401, ex.Status); | ||
return; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.Fail($"Expected RequestFailedException, but got {ex.GetType()}"); | ||
return; | ||
} | ||
Assert.Fail("Expected RequestFailedException, but no exception was thrown."); | ||
} | ||
} | ||
} |
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
120 changes: 120 additions & 0 deletions
120
sdk/keyvault/Azure.Security.KeyVault.Keys/tests/ContinuousAccessEvaluationTests.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,120 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
using System; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
using Azure.Core.TestFramework; | ||
using Azure.Security.KeyVault.Tests; | ||
using NUnit.Framework; | ||
|
||
namespace Azure.Security.KeyVault.Keys.Tests | ||
{ | ||
[NonParallelizable] | ||
internal class ContinuousAccessEvaluationTests : ContinuousAccessEvaluationTestsBase | ||
{ | ||
[SetUp] | ||
public void Setup() | ||
{ | ||
ChallengeBasedAuthenticationPolicy.ClearCache(); | ||
} | ||
|
||
[Test] | ||
[TestCase(@"Bearer realm="""", authorization_uri=""https://login.microsoftonline.com/common/oauth2/authorize"", error=""insufficient_claims"", claims=""eyJhY2Nlc3NfdG9rZW4iOnsibmJmIjp7ImVzc2VudGlhbCI6dHJ1ZSwidmFsdWUiOiIxNzI2MDc3NTk1In0sInhtc19jYWVlcnJvciI6eyJ2YWx1ZSI6IjEwMDEyIn19fQ==""", """{"access_token":{"nbf":{"essential":true,"value":"1726077595"},"xms_caeerror":{"value":"10012"}}}""")] | ||
public async Task VerifyCaeClaims(string challenge, string expectedClaims) | ||
{ | ||
int callCount = 0; | ||
|
||
MockResponse responseWithKey = new MockResponse(200) | ||
.WithContent(@"{ | ||
""key"": { | ||
""kid"": ""https://heathskeyvault.vault.azure.net/keys/625710934/ef3685592e1c4e839206aaa10f0f058e"", | ||
""kty"": ""RSA"", | ||
""key_ops"": [ | ||
""encrypt"", | ||
""decrypt"", | ||
""sign"", | ||
""verify"", | ||
""wrapKey"", | ||
""unwrapKey"" | ||
], | ||
""n"": ""foo"", | ||
""e"": ""AQAB"" | ||
}, | ||
""attributes"": { | ||
""enabled"": true, | ||
""created"": 1613807137, | ||
""updated"": 1613807137, | ||
""recoveryLevel"": ""Recoverable\u002BPurgeable"", | ||
""recoverableDays"": 90 | ||
} | ||
}"); | ||
|
||
MockTransport transport = GetMockTransportWithCaeChallenges(numberOfCaeChallenges: 1, final200response: responseWithKey); | ||
|
||
var credential = new TokenCredentialStub((r, c) => | ||
{ | ||
if (callCount == 0) | ||
{ | ||
// The first challenge should not have any claims. | ||
Assert.IsNull(r.Claims); | ||
} | ||
else if (callCount == 1) | ||
{ | ||
Assert.AreEqual(expectedClaims, r.Claims); | ||
} | ||
else | ||
{ | ||
Assert.Fail("unexpected token request"); | ||
} | ||
Interlocked.Increment(ref callCount); | ||
Assert.AreEqual(true, r.IsCaeEnabled); | ||
|
||
return new(callCount.ToString(), DateTimeOffset.Now.AddHours(2)); | ||
}, true); | ||
|
||
KeyClient client = new( | ||
VaultUri, | ||
credential, | ||
new KeyClientOptions() | ||
{ | ||
Transport = transport, | ||
}); | ||
|
||
Response<KeyVaultKey> response = await client.GetKeyAsync("key"); | ||
Assert.AreEqual(200, response.GetRawResponse().Status); | ||
} | ||
|
||
[Test] | ||
public void ThrowsWithTwoConsecutiveCaeChallenges() | ||
{ | ||
MockTransport keyVaultTransport = GetMockTransportWithCaeChallenges(numberOfCaeChallenges: 2); | ||
|
||
MockTransport credentialTransport = GetMockCredentialTransport(2); | ||
|
||
KeyClient client = new( | ||
VaultUri, | ||
new MockCredential(credentialTransport), | ||
new KeyClientOptions() | ||
{ | ||
Transport = keyVaultTransport, | ||
}); | ||
|
||
try | ||
{ | ||
client.GetKey("key"); | ||
} | ||
catch (RequestFailedException ex) | ||
{ | ||
Assert.AreEqual(401, ex.Status); | ||
return; | ||
} | ||
catch (Exception ex) | ||
{ | ||
Assert.Fail($"Expected RequestFailedException, but got {ex.GetType()}"); | ||
return; | ||
} | ||
Assert.Fail("Expected RequestFailedException, but no exception was thrown."); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.