Repository navigation
Enable secretless OBO authentication in DAB using Workload Identity Federation #3880
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Mohit G (gmohit21)
wants to merge
2
commits into
main
Choose a base branch
from
dev/gmohit/obo-workload-identity-federation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,402
−163
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,184 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Net; | ||
| using Azure.Core; | ||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||
| using Azure.DataApiBuilder.Service.Exceptions; | ||
| using Azure.Identity; | ||
| using Microsoft.Extensions.Logging; | ||
| using Microsoft.Identity.Client; | ||
|
|
||
| namespace Azure.DataApiBuilder.Core.AuthenticationHelpers | ||
| { | ||
| /// <summary> | ||
| /// Validates and creates the application credential used for user-delegated authentication. | ||
| /// </summary> | ||
| public static class OboCredentialFactory | ||
| { | ||
| public const string MISSING_CREDENTIALS_ERROR_MESSAGE = | ||
| "User-delegated authentication requires DAB_OBO_CLIENT_ID, DAB_OBO_TENANT_ID, and exactly one of DAB_OBO_CLIENT_SECRET or DAB_OBO_MANAGED_IDENTITY_CLIENT_ID."; | ||
|
|
||
| private const string CONFLICTING_CREDENTIALS_ERROR_MESSAGE = | ||
| "Configure exactly one OBO credential method. DAB_OBO_CLIENT_SECRET and DAB_OBO_MANAGED_IDENTITY_CLIENT_ID cannot both be set."; | ||
|
|
||
| private const string TOKEN_EXCHANGE_SCOPE = "api://AzureADTokenExchange/.default"; | ||
|
|
||
| /// <summary> | ||
| /// Validates the environment variables for the selected OBO application credential. | ||
| /// Callers are responsible for checking whether user-delegated authentication is enabled. | ||
| /// </summary> | ||
| public static void ValidateEnvironmentVariables() | ||
| { | ||
| ValidateEnvironmentVariables(Environment.GetEnvironmentVariable); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Creates a confidential client whose credential is independent of the incoming user assertion. | ||
| /// The returned client should be reused for subsequent OBO requests. | ||
| /// </summary> | ||
| public static IConfidentialClientApplication CreateClient(ILogger logger) | ||
| { | ||
| return CreateClient(logger, Environment.GetEnvironmentVariable); | ||
| } | ||
|
|
||
| internal static void ValidateEnvironmentVariables(Func<string, string?> environmentVariableReader) | ||
| { | ||
| _ = ReadOptions(environmentVariableReader); | ||
| } | ||
|
|
||
| internal static IConfidentialClientApplication CreateClient( | ||
| ILogger logger, | ||
| Func<string, string?> environmentVariableReader, | ||
| Func<string, TokenCredential>? managedIdentityCredentialFactory = null, | ||
| IMsalHttpClientFactory? httpClientFactory = null) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(logger); | ||
| OboCredentialOptions options = ReadOptions(environmentVariableReader); | ||
|
|
||
| ConfidentialClientApplicationBuilder builder = ConfidentialClientApplicationBuilder | ||
| .Create(options.ClientId) | ||
| .WithAuthority(AzureCloudInstance.AzurePublic, options.TenantId); | ||
|
|
||
| if (options.ClientSecret is not null) | ||
| { | ||
| builder.WithClientSecret(options.ClientSecret); | ||
| } | ||
| else | ||
| { | ||
| TokenCredential credential = managedIdentityCredentialFactory is null | ||
| ? new ManagedIdentityCredential(ManagedIdentityId.FromUserAssignedClientId(options.ManagedIdentityClientId!)) | ||
| : managedIdentityCredentialFactory(options.ManagedIdentityClientId!); | ||
|
|
||
| // Reuse the credential, not a token snapshot: Azure Identity handles assertion refresh. | ||
| builder.WithClientAssertion((AssertionRequestOptions requestOptions) => | ||
| GetManagedIdentityAssertionAsync(credential, logger, requestOptions.CancellationToken)); | ||
| } | ||
|
|
||
| if (httpClientFactory is not null) | ||
| { | ||
| builder.WithHttpClientFactory(httpClientFactory); | ||
| } | ||
|
|
||
| return builder.Build(); | ||
| } | ||
|
|
||
| private static async Task<string> GetManagedIdentityAssertionAsync( | ||
| TokenCredential credential, | ||
| ILogger logger, | ||
| CancellationToken cancellationToken) | ||
| { | ||
| try | ||
| { | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| AccessToken assertion = await credential.GetTokenAsync( | ||
| new TokenRequestContext([TOKEN_EXCHANGE_SCOPE]), | ||
| cancellationToken).ConfigureAwait(false); | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
|
|
||
| if (string.IsNullOrWhiteSpace(assertion.Token) || assertion.ExpiresOn <= DateTimeOffset.UtcNow) | ||
| { | ||
| throw CreateAuthenticationFailure(logger); | ||
| } | ||
|
|
||
| return assertion.Token; | ||
| } | ||
| catch (AuthenticationFailedException) | ||
| { | ||
| // CredentialUnavailableException derives from AuthenticationFailedException. | ||
| cancellationToken.ThrowIfCancellationRequested(); | ||
| throw CreateAuthenticationFailure(logger); | ||
| } | ||
| } | ||
|
|
||
| private static DataApiBuilderException CreateAuthenticationFailure(ILogger logger) | ||
| { | ||
| // SDK exceptions can contain credential responses. Do not log or retain their payloads. | ||
| logger.LogError( | ||
| "{EventType}: Unable to acquire the managed identity client assertion for user-delegated authentication.", | ||
| "OboClientAssertionAcquisitionFailed"); | ||
|
|
||
| return new DataApiBuilderException( | ||
| message: DataApiBuilderException.OBO_TOKEN_ACQUISITION_FAILED, | ||
| statusCode: HttpStatusCode.Unauthorized, | ||
| subStatusCode: DataApiBuilderException.SubStatusCodes.OboAuthenticationFailure); | ||
| } | ||
|
|
||
| private static OboCredentialOptions ReadOptions(Func<string, string?> environmentVariableReader) | ||
| { | ||
| ArgumentNullException.ThrowIfNull(environmentVariableReader); | ||
|
|
||
| string? clientId = environmentVariableReader(UserDelegatedAuthOptions.DAB_OBO_CLIENT_ID_ENV_VAR); | ||
| string? tenantId = environmentVariableReader(UserDelegatedAuthOptions.DAB_OBO_TENANT_ID_ENV_VAR); | ||
| string? clientSecret = environmentVariableReader(UserDelegatedAuthOptions.DAB_OBO_CLIENT_SECRET_ENV_VAR); | ||
| string? managedIdentityClientId = environmentVariableReader(UserDelegatedAuthOptions.DAB_OBO_MANAGED_IDENTITY_CLIENT_ID_ENV_VAR); | ||
| bool hasClientSecret = !string.IsNullOrWhiteSpace(clientSecret); | ||
| bool hasManagedIdentity = !string.IsNullOrWhiteSpace(managedIdentityClientId); | ||
|
|
||
| if (hasClientSecret && hasManagedIdentity) | ||
| { | ||
| throw CreateConfigurationFailure(CONFLICTING_CREDENTIALS_ERROR_MESSAGE); | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(clientId) || string.IsNullOrWhiteSpace(tenantId) | ||
| || (!hasClientSecret && !hasManagedIdentity)) | ||
| { | ||
| throw CreateConfigurationFailure(MISSING_CREDENTIALS_ERROR_MESSAGE); | ||
| } | ||
|
|
||
| return new OboCredentialOptions( | ||
| clientId, | ||
| tenantId, | ||
| hasClientSecret ? clientSecret : null, | ||
| hasManagedIdentity ? managedIdentityClientId : null); | ||
| } | ||
|
|
||
| private static DataApiBuilderException CreateConfigurationFailure(string message) | ||
| { | ||
| return new DataApiBuilderException( | ||
| message: message, | ||
| statusCode: HttpStatusCode.ServiceUnavailable, | ||
| subStatusCode: DataApiBuilderException.SubStatusCodes.ConfigValidationError); | ||
| } | ||
|
|
||
| private sealed class OboCredentialOptions | ||
| { | ||
| public string ClientId { get; } | ||
| public string TenantId { get; } | ||
| public string? ClientSecret { get; } | ||
| public string? ManagedIdentityClientId { get; } | ||
|
|
||
| public OboCredentialOptions( | ||
| string clientId, | ||
| string tenantId, | ||
| string? clientSecret, | ||
| string? managedIdentityClientId) | ||
| { | ||
| ClientId = clientId; | ||
| TenantId = tenantId; | ||
| ClientSecret = clientSecret; | ||
| ManagedIdentityClientId = managedIdentityClientId; | ||
| } | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or 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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.