-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update distributed tests to use AAD auth (#9207)
* Update distributed tests to use AAD auth
- Loading branch information
1 parent
c78ee8c
commit 86f0ca5
Showing
11 changed files
with
116 additions
and
94 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
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
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
63 changes: 0 additions & 63 deletions
63
test/DistributedTests/DistributedTests.Common/SecretConfiguration.cs
This file was deleted.
Oops, something went wrong.
69 changes: 69 additions & 0 deletions
69
test/DistributedTests/DistributedTests.Common/TokenCredentialHelper.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,69 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Diagnostics; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Azure.Core; | ||
using Azure.Data.Tables; | ||
using Azure.Identity; | ||
using Azure.Storage.Queues; | ||
|
||
namespace DistributedTests.Common; | ||
|
||
public static class TokenCredentialHelper | ||
{ | ||
private static string EmulatorConnectionString = "UseDevelopmentStorage=true"; | ||
|
||
public static TableServiceClient CreateTableServiceClient(this Uri azureTableUri) | ||
{ | ||
if (azureTableUri.IsLoopback) | ||
{ | ||
// Assume it's the emulator/azurite | ||
return new TableServiceClient(EmulatorConnectionString); | ||
} | ||
return new TableServiceClient(azureTableUri, GetTokenCredential()); | ||
} | ||
|
||
public static QueueServiceClient CreateQueueServiceClient(this Uri azureQueueUri) | ||
{ | ||
if (azureQueueUri.IsLoopback) | ||
{ | ||
// Assume it's the emulator/azurite | ||
return new QueueServiceClient(EmulatorConnectionString); | ||
} | ||
return new QueueServiceClient(azureQueueUri, GetTokenCredential()); | ||
} | ||
|
||
public static TokenCredential GetTokenCredential() | ||
{ | ||
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID"); | ||
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID"); | ||
if (tenantId != null && clientId != null) | ||
{ | ||
// Uses Federated Id Creds, from here: | ||
// https://review.learn.microsoft.com/en-us/identity/microsoft-identity-platform/federated-identity-credentials?branch=main&tabs=dotnet#azure-sdk-for-net | ||
return new ClientAssertionCredential( | ||
tenantId, // Tenant ID for destination resource | ||
clientId, // Client ID of the app we're federating to | ||
() => GetManagedIdentityToken(null, "api://AzureADTokenExchange")) // null here for default MSI | ||
; | ||
} | ||
else | ||
{ | ||
return new DefaultAzureCredential(); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Gets a token for the user-assigned Managed Identity. | ||
/// </summary> | ||
/// <param name="msiClientId">Client ID for the Managed Identity.</param> | ||
/// <param name="audience">Target audience. For public clouds should be api://AzureADTokenExchange.</param> | ||
/// <returns>If successful, returns an access token.</returns> | ||
public static string GetManagedIdentityToken(string msiClientId, string audience) | ||
{ | ||
var miCredential = new ManagedIdentityCredential(msiClientId); | ||
return miCredential.GetToken(new TokenRequestContext(new[] { $"{audience}/.default" })).Token; | ||
} | ||
} |
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.