Skip to content

Commit c0a4970

Browse files
authored
Add tenant switching test to Key Vault (#35474)
* Add tenant switching test to Key Vault Relates to #35086 and depends on #35086 * Resolve PR feedback
1 parent b1ae27d commit c0a4970

File tree

5 files changed

+87
-2
lines changed

5 files changed

+87
-2
lines changed

sdk/keyvault/Azure.Security.KeyVault.Administration/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- When a Key Vault is moved to another tenant, the client is reauthenticated.
12+
1113
### Other Changes
1214

1315
## 4.3.0 (2023-03-14)

sdk/keyvault/Azure.Security.KeyVault.Certificates/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
### Bugs Fixed
1313

14+
- When a Key Vault is moved to another tenant, the client is reauthenticated.
15+
1416
### Other Changes
1517

1618
## 4.5.1 (2023-03-31)

sdk/keyvault/Azure.Security.KeyVault.Keys/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- When a Key Vault is moved to another tenant, the client is reauthenticated.
12+
1113
### Other Changes
1214

1315
## 4.5.0 (2023-03-14)

sdk/keyvault/Azure.Security.KeyVault.Secrets/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
### Bugs Fixed
1010

11+
- When a Key Vault is moved to another tenant, the client is reauthenticated.
12+
1113
### Other Changes
1214

1315
## 4.5.0 (2023-03-14)

sdk/keyvault/Azure.Security.KeyVault.Secrets/tests/ChallengeBasedAuthenticationPolicyTests.cs

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,29 +3,34 @@
33

44
using System;
55
using System.Diagnostics;
6-
using System.Diagnostics.Tracing;
76
using System.IO;
87
using System.Text;
98
using System.Text.Json;
109
using System.Text.RegularExpressions;
1110
using System.Threading;
1211
using System.Threading.Tasks;
1312
using Azure.Core;
14-
using Azure.Core.Diagnostics;
1513
using Azure.Core.Pipeline;
1614
using Azure.Core.TestFramework;
1715
using Azure.Security.KeyVault.Tests;
1816
using NUnit.Framework;
1917

2018
namespace Azure.Security.KeyVault.Secrets.Tests
2119
{
20+
[NonParallelizable]
2221
public class ChallengeBasedAuthenticationPolicyTests
2322
{
2423
private const string TenantId = "72f988bf-86f1-41af-91ab-2d7cd011db47";
2524
private const string VaultHost = "test.vault.azure.net";
2625

2726
private static Uri VaultUri => new Uri("https://" + VaultHost);
2827

28+
[SetUp]
29+
public void Setup()
30+
{
31+
ChallengeBasedAuthenticationPolicy.ClearCache();
32+
}
33+
2934
[Test]
3035
public async Task SingleRequest()
3136
{
@@ -122,6 +127,78 @@ public async Task TenantChangedRequest()
122127
}
123128
}
124129

130+
[Test]
131+
public async Task ReauthenticatesWhenTenantChanged()
132+
{
133+
MockTransport transport = new(new[]
134+
{
135+
// Initial tenant.
136+
new MockResponse(401)
137+
.WithHeader("WWW-Authenticate", @"Bearer authorization=""https://login.windows.net/de763a21-49f7-4b08-a8e1-52c8fbc103b4"", resource=""https://vault.azure.net"""),
138+
139+
new MockResponse(200)
140+
.WithJson("""
141+
{
142+
"token_type": "Bearer",
143+
"expires_in": 3599,
144+
"resource": "https://vault.azure.net",
145+
"access_token": "ZGU3NjNhMjEtNDlmNy00YjA4LWE4ZTEtNTJjOGZiYzEwM2I0"
146+
}
147+
"""),
148+
149+
new MockResponse(200)
150+
{
151+
ContentStream = new KeyVaultSecret("test-secret", "secret-value").ToStream(),
152+
},
153+
154+
// Moved tenants.
155+
new MockResponse(401)
156+
.WithHeader("WWW-Authenticate", @"Bearer authorization=""https://login.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47"", resource=""https://vault.azure.net""")
157+
.WithJson("""
158+
{
159+
"error": {
160+
"code": "Unauthorized",
161+
"message": "AKV10032: Invalid issuer. Expected one of https://sts.windows.net/72f988bf-86f1-41af-91ab-2d7cd011db47/, https://sts.windows.net/f8cdef31-a31e-4b4a-93e4-5f571e91255a/, https://sts.windows.net/e2d54eb5-3869-4f70-8578-dee5fc7331f4/, https://sts.windows.net/33e01921-4d64-4f8c-a055-5bdaffd5e33d/, https://sts.windows.net/975f013f-7f24-47e8-a7d3-abc4752bf346/, found https://sts.windows.net/96be4b7a-defb-4dc2-a31f-49ee6145d5ab/."
162+
}
163+
}
164+
"""),
165+
166+
new MockResponse(200)
167+
.WithJson("""
168+
{
169+
"token_type": "Bearer",
170+
"expires_in": 3599,
171+
"resource": "https://vault.azure.net",
172+
"access_token": "NzJmOTg4YmYtODZmMS00MWFmLTkxYWItMmQ3Y2QwMTFkYjQ3"
173+
}
174+
"""),
175+
176+
new MockResponse(200)
177+
{
178+
ContentStream = new KeyVaultSecret("test-secret", "secret-value").ToStream(),
179+
},
180+
});
181+
182+
SecretClientOptions options = new()
183+
{
184+
Transport = transport,
185+
};
186+
187+
SecretClient client = new(
188+
VaultUri,
189+
new MockCredential(transport),
190+
options);
191+
192+
Response<KeyVaultSecret> response = await client.GetSecretAsync("test-secret");
193+
Assert.AreEqual(200, response.GetRawResponse().Status);
194+
Assert.AreEqual("secret-value", response.Value.Value);
195+
196+
// Try it again now that the vault should have moved tenants.
197+
response = await client.GetSecretAsync("test-secret");
198+
Assert.AreEqual(200, response.GetRawResponse().Status);
199+
Assert.AreEqual("secret-value", response.Value.Value);
200+
}
201+
125202
private class MockTransportBuilder
126203
{
127204
private const string AuthorizationHeader = "Authorization";

0 commit comments

Comments
 (0)