|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using Azure.Identity; |
| 6 | + |
| 7 | +namespace Microsoft.Data.SqlClient.Extensions.Azure.Test; |
| 8 | + |
| 9 | +// Verify that we're running in an environment that supports Azure Pipelines |
| 10 | +// Workload Identity Federation authentication. |
| 11 | +public class WorkloadIdentityFederationTests |
| 12 | +{ |
| 13 | + [ConditionalFact(typeof(Config), nameof(Config.HasSystemAccessToken))] |
| 14 | + public async void GetCredential() |
| 15 | + { |
| 16 | + AzurePipelinesCredential credential = new( |
| 17 | + // The tenant ID if the managed identity associated to our workload |
| 18 | + // identity federation service connection. See: |
| 19 | + // |
| 20 | + // https://ms.portal.azure.com/#@microsoft.onmicrosoft.com/resource/subscriptions/654fffd0-d02d-4894-b1b7-e2dfbc44a665/resourceGroups/aad-testlab-dl797892652000/providers/Microsoft.ManagedIdentity/userAssignedIdentities/dotnetMSI/properties |
| 21 | + "72f988bf-86f1-41af-91ab-2d7cd011db47", |
| 22 | + // The client ID of the managed identity associated to our workload |
| 23 | + // identity federation service connection. See: |
| 24 | + // |
| 25 | + // https://ms.portal.azure.com/#@microsoft.onmicrosoft.com/resource/subscriptions/654fffd0-d02d-4894-b1b7-e2dfbc44a665/resourceGroups/aad-testlab-dl797892652000/providers/Microsoft.ManagedIdentity/userAssignedIdentities/dotnetMSI/overview |
| 26 | + "92a44a21-5265-4fdd-9537-45b1cf54aa2d", |
| 27 | + |
| 28 | + // The Azure Dev Ops service connection ID (resourceId found in the |
| 29 | + // URL) of our workload identity federation setup. See: |
| 30 | + // |
| 31 | + // https://sqlclientdrivers.visualstudio.com/public/_settings/adminservices?resourceId=ec9623b2-829c-497f-ae1f-7461766f9a9c |
| 32 | + "ec9623b2-829c-497f-ae1f-7461766f9a9c", |
| 33 | + Config.SystemAccessToken); |
| 34 | + |
| 35 | + // Acquire a token suitable for accessing Azure SQL databases. |
| 36 | + var token = await credential.GetTokenAsync( |
| 37 | + new(["https://database.windows.net/.default"]), |
| 38 | + CancellationToken.None); |
| 39 | + |
| 40 | + Assert.NotEmpty(token.Token); |
| 41 | + } |
| 42 | +} |
0 commit comments