-
Notifications
You must be signed in to change notification settings - Fork 335
Add application identity to USERAGENT payload (V2) #4632
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
cheenamalhotra
wants to merge
12
commits into
main
Choose a base branch
from
dev/cheena/refactored-pancake
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.
Open
Changes from 1 commit
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
60037bc
Add SqlClient agent identifier to USERAGENT payload
cheenamalhotra c9f0cec
Address PR review feedback
cheenamalhotra 171d890
Make SqlClientAgent CLS-compliant and cover config failures
cheenamalhotra 1d296c9
Lock SqlClientAgent underlying type and drop stale attribute
cheenamalhotra 0c2bfcc
Inline the agent registration sample
cheenamalhotra a1ae554
Tests | Stop CancelAndDisposePreparedCommand deadlocking on the catalog
cheenamalhotra 9c7f4b0
Report agent registration outcome and stop using the catalog in cance…
cheenamalhotra 2f82e5e
Report application identity in USERAGENT payload V2
cheenamalhotra 1510d6f
Preserve application identity across clones, add sqlpackage
cheenamalhotra d849cf4
Reject identity changes after login, propagate to discovery login
cheenamalhotra 095d84e
Document identity exception and trust boundary
cheenamalhotra e8a5509
Leave localized resx files to the OneLocBuild pipeline
cheenamalhotra 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
Some comments aren't visible on the classic Files Changed page.
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,13 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.Data.SqlClient; | ||
|
|
||
| internal static class MiddlewareRegistration | ||
| { | ||
| // Register once during application startup, before opening any connections. | ||
| internal static void Register() | ||
| { | ||
| SqlConnection.RegisterSqlClientAgent(SqlClientAgent.EntityFramework); | ||
| } | ||
| } |
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
230 changes: 230 additions & 0 deletions
230
src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/SqlClientAgent.cs
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,230 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
| // See the LICENSE file in the project root for more information. | ||
|
|
||
| using System; | ||
| using System.Configuration; | ||
| using System.Threading; | ||
| using Microsoft.Data.Common; | ||
| using Microsoft.Data.SqlClient.Internal; | ||
|
|
||
| #nullable enable | ||
|
|
||
| namespace Microsoft.Data.SqlClient; | ||
|
|
||
| /// <summary> | ||
| /// Identifies known middleware agents that use Microsoft.Data.SqlClient. | ||
| /// </summary> | ||
| [CLSCompliant(false)] | ||
| public enum SqlClientAgent : ushort | ||
| { | ||
| /// <summary>The Microsoft Entity Framework Core SQL Server provider.</summary> | ||
| EntityFramework = 1, | ||
|
|
||
| /// <summary>Microsoft Semantic Kernel.</summary> | ||
| SemanticKernel = 2, | ||
|
|
||
| /// <summary>Microsoft SQL Server Management Studio.</summary> | ||
| ManagementStudio = 3, | ||
|
|
||
| /// <summary>Microsoft SQL Server Management Objects.</summary> | ||
| SqlManagementObjects = 4, | ||
|
|
||
| /// <summary>Microsoft SQL Server Data-Tier Application Framework.</summary> | ||
|
cheenamalhotra marked this conversation as resolved.
Outdated
|
||
| DataTierApplicationFramework = 5, | ||
|
|
||
| /// <summary>Microsoft SQL Tools Service.</summary> | ||
| SqlToolsService = 6, | ||
|
|
||
| /// <summary>Microsoft ASP.NET Core distributed SQL Server cache.</summary> | ||
| AspNetCoreDistributedSqlServerCache = 7, | ||
|
|
||
| /// <summary>Microsoft Entity Framework 6 SQL Server provider.</summary> | ||
| EntityFramework6 = 8, | ||
|
|
||
| /// <summary>Microsoft Azure Functions SQL extension.</summary> | ||
| AzureFunctionsSqlExtension = 9, | ||
|
|
||
| /// <summary>Microsoft Orleans ADO.NET providers.</summary> | ||
|
cheenamalhotra marked this conversation as resolved.
Outdated
|
||
| OrleansAdoNet = 10, | ||
|
|
||
| /// <summary>Microsoft Durable Task SQL Server provider.</summary> | ||
| DurableTaskSqlServer = 11 | ||
|
cheenamalhotra marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// <para> | ||
| /// Holds the process-wide agent registration reported in the USERAGENT | ||
| /// login feature extension. | ||
| /// </para> | ||
| /// <para> | ||
| /// An agent may be registered at most once per process, either from the | ||
| /// application configuration file or programmatically via | ||
| /// <see cref="SqlConnection.RegisterSqlClientAgent"/>. The configuration | ||
| /// file wins, because it is read during static construction, before any | ||
| /// application code can call | ||
| /// <see cref="SqlConnection.RegisterSqlClientAgent"/>. | ||
| /// </para> | ||
| /// </summary> | ||
| internal static class SqlClientAgentRegistration | ||
| { | ||
| // The registered agent identifier, or 0 when no agent is registered. | ||
| // | ||
| // Written at most once, either by static construction from the application | ||
| // configuration file, or by the first Register() call. | ||
| private static int s_agentId = LoadFromAppConfig(); | ||
|
|
||
| /// <summary> | ||
| /// The registered agent, or null when no agent has been registered. | ||
| /// </summary> | ||
| internal static SqlClientAgent? Agent | ||
| { | ||
| get | ||
| { | ||
| int id = Volatile.Read(ref s_agentId); | ||
| return id == 0 ? null : (SqlClientAgent)id; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Register the given agent for the lifetime of the process. | ||
| /// </summary> | ||
| /// <param name="id">The agent to register.</param> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// <paramref name="id"/> is not a valid agent identifier. | ||
| /// </exception> | ||
| /// <exception cref="InvalidOperationException"> | ||
| /// An agent has already been registered. | ||
| /// </exception> | ||
| internal static void Register(SqlClientAgent id) | ||
| { | ||
| Validate(id); | ||
| if (Interlocked.CompareExchange(ref s_agentId, (ushort)id, 0) != 0) | ||
| { | ||
| throw SQL.SqlClientAgentAlreadyRegistered(); | ||
|
mdaigle marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <para> | ||
| /// Convert a configured agent identifier to a | ||
| /// <see cref="SqlClientAgent"/>. | ||
| /// </para> | ||
| /// <para> | ||
| /// Both the name of a known agent (case-insensitive) and its numeric | ||
| /// identifier are accepted. A numeric identifier that is not yet | ||
| /// defined in <see cref="SqlClientAgent"/> is accepted, so an agent | ||
| /// assigned an identifier after this driver shipped can still be | ||
| /// configured. | ||
| /// </para> | ||
| /// </summary> | ||
| /// <param name="value">The agent identifier to convert.</param> | ||
| /// <returns>The agent the identifier names.</returns> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// <paramref name="value"/> is not a valid agent identifier. | ||
| /// </exception> | ||
| internal static SqlClientAgent Parse(string value) | ||
| { | ||
| // Enum.TryParse accepts comma-separated lists and combines them, so | ||
| // "EntityFramework,SemanticKernel" would silently yield an unrelated | ||
| // agent. Only a single name or number is valid here. | ||
| if (value is null | ||
| || value.IndexOf(',') >= 0 | ||
| || !Enum.TryParse(value, ignoreCase: true, out SqlClientAgent id) | ||
| || (ushort)id == 0) | ||
| { | ||
| throw SQL.InvalidSqlClientAgent(value ?? string.Empty, nameof(value)); | ||
| } | ||
|
|
||
| return id; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Throw if the given agent is not a valid agent identifier. | ||
| /// </summary> | ||
| /// <param name="id">The agent to validate.</param> | ||
| /// <exception cref="ArgumentOutOfRangeException"> | ||
| /// <paramref name="id"/> is not a valid agent identifier. | ||
| /// </exception> | ||
| private static void Validate(SqlClientAgent id) | ||
| { | ||
| if ((ushort)id == 0) | ||
| { | ||
| throw SQL.InvalidSqlClientAgent(id.ToString(), nameof(id)); | ||
| } | ||
|
cheenamalhotra marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| /// <summary> | ||
| /// <para> | ||
| /// Read the agent registered in the application configuration file. | ||
| /// </para> | ||
| /// <para>All known exceptions are consumed.</para> | ||
| /// </summary> | ||
| /// <returns> | ||
| /// The configured agent identifier, or 0 when no valid agent is | ||
| /// configured. | ||
| /// </returns> | ||
| private static int LoadFromAppConfig() | ||
| { | ||
| // This runs during static initialization on the login path, so any | ||
| // escaping exception would surface as a TypeInitializationException on | ||
| // every connection attempt. Telemetry must never break connections. | ||
| try | ||
| { | ||
| object section = ConfigurationManager.GetSection(SqlClientAgentConfigurationSection.Name); | ||
| if (section is null) | ||
| { | ||
| return 0; | ||
| } | ||
|
|
||
| if (section is SqlClientAgentConfigurationSection configurationSection) | ||
| { | ||
| return (ushort)Parse(configurationSection.Id); | ||
| } | ||
|
|
||
| SqlClientEventSource.Log.TryTraceEvent( | ||
| "SqlClientAgentRegistration: The SqlClientAgent configuration section has an unexpected type; the agent was not registered."); | ||
| } | ||
| catch (Exception e) when (ADP.IsCatchableExceptionType(e)) | ||
| { | ||
| SqlClientEventSource.Log.TryTraceEvent( | ||
| "SqlClientAgentRegistration: Unable to load the SqlClientAgent configuration; the agent was not registered: {0}", | ||
| e); | ||
| } | ||
|
|
||
| return 0; | ||
| } | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// <para> | ||
| /// The <c>SqlClientAgent</c> application configuration file section, used | ||
| /// to register an agent without changing application code. | ||
| /// </para> | ||
| /// <para> | ||
| /// <code> | ||
| /// <configSections> | ||
| /// <section name="SqlClientAgent" | ||
| /// type="Microsoft.Data.SqlClient.SqlClientAgentConfigurationSection, Microsoft.Data.SqlClient" /> | ||
| /// </configSections> | ||
| /// <SqlClientAgent id="EntityFramework" /> | ||
| /// </code> | ||
| /// </para> | ||
| /// </summary> | ||
| internal sealed class SqlClientAgentConfigurationSection : ConfigurationSection | ||
| { | ||
| /// <summary> | ||
| /// The name of this configuration section. | ||
| /// </summary> | ||
| internal const string Name = "SqlClientAgent"; | ||
|
|
||
| /// <summary> | ||
| /// The name or numeric identifier of the agent to register. | ||
| /// </summary> | ||
| [ConfigurationProperty("id", IsRequired = true)] | ||
| public string Id | ||
| { | ||
| get => this["id"] as string ?? string.Empty; | ||
| set => this["id"] = value; | ||
| } | ||
| } | ||
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
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.
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 property won't throw out-of-range.