Repository navigation
Use DAB's client ID for SqlClient Entra ID authentication #3927
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
Draft
Cheena Malhotra (cheenamalhotra)
wants to merge
1
commit into
main
Choose a base branch
from
dev/cheena/entra-client-identity
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.
+121
−0
Draft
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Reflection; | ||
| using Azure.DataApiBuilder.Core.AuthenticationHelpers; | ||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| namespace Cli.Tests; | ||
|
|
||
| [TestClass] | ||
| [DoNotParallelize] | ||
| public sealed class SqlClientAuthenticationTests | ||
| { | ||
| private const string CLIENT_ID = "11111111-1111-1111-1111-111111111111"; | ||
| private readonly string? _originalClientId = | ||
| Environment.GetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_CLIENT_ID_ENV_VAR); | ||
| private readonly Dictionary<SqlAuthenticationMethod, SqlAuthenticationProvider?> _originalProviders = | ||
| Enum.GetValues<SqlAuthenticationMethod>().ToDictionary(method => method, SqlAuthenticationProvider.GetProvider); | ||
|
|
||
| [TestCleanup] | ||
| public void Cleanup() | ||
| { | ||
| Environment.SetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_CLIENT_ID_ENV_VAR, _originalClientId); | ||
| foreach ((SqlAuthenticationMethod method, SqlAuthenticationProvider? provider) in _originalProviders) | ||
| { | ||
| if (provider is not null) | ||
| { | ||
| Assert.IsTrue(SqlAuthenticationProvider.SetProvider(method, provider)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public void RegisterProviders_ConfiguredClientId_RegistersSharedProviderForEverySupportedMethod() | ||
| { | ||
| Environment.SetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_CLIENT_ID_ENV_VAR, CLIENT_ID); | ||
|
|
||
| SqlClientAuthentication.RegisterProviders(); | ||
|
|
||
| SqlAuthenticationProvider provider = SqlAuthenticationProvider.GetProvider(SqlAuthenticationMethod.ActiveDirectoryInteractive); | ||
| Assert.IsInstanceOfType<ActiveDirectoryAuthenticationProvider>(provider); | ||
| // SqlClient does not expose the constructor's client ID through its public API. | ||
| FieldInfo? clientIdField = typeof(ActiveDirectoryAuthenticationProvider) | ||
| .GetField("_applicationClientId", BindingFlags.Instance | BindingFlags.NonPublic); | ||
| Assert.IsNotNull(clientIdField); | ||
| Assert.AreEqual(CLIENT_ID, clientIdField.GetValue(provider)); | ||
|
|
||
| foreach (SqlAuthenticationMethod method in Enum.GetValues<SqlAuthenticationMethod>()) | ||
| { | ||
| if (provider.IsSupported(method)) | ||
| { | ||
| Assert.AreSame(provider, SqlAuthenticationProvider.GetProvider(method), method.ToString()); | ||
| } | ||
| else | ||
| { | ||
| Assert.AreSame(_originalProviders[method], SqlAuthenticationProvider.GetProvider(method), method.ToString()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| [TestMethod] | ||
| [DataRow(null)] | ||
| [DataRow("")] | ||
| [DataRow(" \t")] | ||
| public void RegisterProviders_NoClientId_PreservesExistingProviders(string? clientId) | ||
| { | ||
| Environment.SetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_CLIENT_ID_ENV_VAR, clientId); | ||
|
|
||
| SqlClientAuthentication.RegisterProviders(); | ||
|
|
||
| foreach ((SqlAuthenticationMethod method, SqlAuthenticationProvider? provider) in _originalProviders) | ||
| { | ||
| Assert.AreSame(provider, SqlAuthenticationProvider.GetProvider(method), method.ToString()); | ||
| } | ||
| } | ||
| } |
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
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,35 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Azure.DataApiBuilder.Config.ObjectModel; | ||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| namespace Azure.DataApiBuilder.Core.AuthenticationHelpers; | ||
|
|
||
| /// <summary> | ||
| /// Configures SqlClient authentication with the application identity used for OBO. | ||
| /// </summary> | ||
| internal static class SqlClientAuthentication | ||
| { | ||
| /// <summary> | ||
| /// Registers a shared provider for every authentication method it supports. | ||
| /// Leaves SqlClient's existing providers unchanged when no application client ID is configured. | ||
| /// </summary> | ||
| internal static void RegisterProviders() | ||
| { | ||
| string? clientId = Environment.GetEnvironmentVariable(UserDelegatedAuthOptions.DAB_OBO_CLIENT_ID_ENV_VAR); | ||
| if (string.IsNullOrWhiteSpace(clientId)) | ||
| { | ||
| return; | ||
| } | ||
|
|
||
| ActiveDirectoryAuthenticationProvider provider = new(clientId); | ||
| foreach (SqlAuthenticationMethod method in Enum.GetValues<SqlAuthenticationMethod>()) | ||
| { | ||
| if (provider.IsSupported(method) && !SqlAuthenticationProvider.SetProvider(method, provider)) | ||
| { | ||
| throw new InvalidOperationException($"Failed to register the SqlClient authentication provider for {method}."); | ||
| } | ||
| } | ||
| } | ||
| } | ||
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.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is an important fallback, if OBO is not enabled/setup, this clientId will be null. Just want to make sure, in that situation, the existing default app client id will still be valid for EntraAuth modes, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
when SqlClient drops support for non-WAM broker enabled workflows by removing localhost reply URLs, would this fallback scenario continue to work when OBO is not enabled?