-
Notifications
You must be signed in to change notification settings - Fork 325
Improved OBO logging and added OBO observability to SESSION_CONTEXT #3192
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
anushakolan
wants to merge
11
commits into
main
Choose a base branch
from
dev/anushakolan/obo-correlation
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.
+456
−18
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
8dc80df
Improved OBO logging and added OBO observability to SESSION_CONTEXT
anushakolan 31dba47
Merge remote-tracking branch 'origin/main' into dev/anushakolan/obo-c…
anushakolan aceafbd
Update src/Service.Tests/UnitTests/SqlQueryExecutorUnitTests.cs
anushakolan 80c1e43
Merge branch 'main' into dev/anushakolan/obo-correlation
Aniruddh25 530f644
Merge branch 'main' into dev/anushakolan/obo-correlation
Aniruddh25 e243ca3
Merge branch 'main' into dev/anushakolan/obo-correlation
Aniruddh25 fa929c2
Merge branch 'main' into dev/anushakolan/obo-correlation
anushakolan a5bea96
Merge branch 'main' into dev/anushakolan/obo-correlation
anushakolan 649fcc9
Addressed comments and added more testing.
anushakolan c615dc2
Merge branch 'main' into dev/anushakolan/obo-correlation
anushakolan 0afa20c
Merge branch 'main' into dev/anushakolan/obo-correlation
Aniruddh25 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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright (c) Microsoft Corporation. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using System.Diagnostics; | ||
| using System.Net; | ||
| using System.Security.Claims; | ||
| using System.Security.Cryptography; | ||
|
|
@@ -66,21 +67,30 @@ public OboSqlTokenProvider( | |
| { | ||
| if (principal is null) | ||
| { | ||
| _logger.LogWarning("Cannot acquire OBO token: ClaimsPrincipal is null."); | ||
| _logger.LogWarning( | ||
| "{EventType}: Cannot acquire OBO token - ClaimsPrincipal is null (traceId: {TraceId}).", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use an |
||
| "OboValidationFailed", | ||
| Activity.Current?.TraceId.ToString() ?? "none"); | ||
| return null; | ||
| } | ||
|
|
||
| if (string.IsNullOrWhiteSpace(incomingJwtAssertion)) | ||
| { | ||
| _logger.LogWarning("Cannot acquire OBO token: Incoming JWT assertion is null or empty."); | ||
| _logger.LogWarning( | ||
| "{EventType}: Cannot acquire OBO token - Incoming JWT assertion is null or empty (traceId: {TraceId}).", | ||
| "OboValidationFailed", | ||
| Activity.Current?.TraceId.ToString() ?? "none"); | ||
| return null; | ||
| } | ||
|
|
||
| // Extract identity claims | ||
| string? subjectId = ExtractSubjectId(principal); | ||
| if (string.IsNullOrWhiteSpace(subjectId)) | ||
| { | ||
| _logger.LogWarning("Cannot acquire OBO token: Neither 'oid' nor 'sub' claim found in token."); | ||
| _logger.LogWarning( | ||
| "{EventType}: Cannot acquire OBO token - Neither 'oid' nor 'sub' claim found in token (traceId: {TraceId}).", | ||
| "OboValidationFailed", | ||
| Activity.Current?.TraceId.ToString() ?? "none"); | ||
| throw new DataApiBuilderException( | ||
| message: DataApiBuilderException.OBO_IDENTITY_CLAIMS_MISSING, | ||
| statusCode: HttpStatusCode.Unauthorized, | ||
|
|
@@ -90,7 +100,10 @@ public OboSqlTokenProvider( | |
| string? tenantId = principal.FindFirst("tid")?.Value; | ||
| if (string.IsNullOrWhiteSpace(tenantId)) | ||
| { | ||
| _logger.LogWarning("Cannot acquire OBO token: 'tid' (tenant id) claim not found or empty in token."); | ||
| _logger.LogWarning( | ||
| "{EventType}: Cannot acquire OBO token - 'tid' (tenant id) claim not found or empty in token (traceId: {TraceId}).", | ||
| "OboValidationFailed", | ||
| Activity.Current?.TraceId.ToString() ?? "none"); | ||
| throw new DataApiBuilderException( | ||
| message: DataApiBuilderException.OBO_TENANT_CLAIM_MISSING, | ||
| statusCode: HttpStatusCode.Unauthorized, | ||
|
|
@@ -115,9 +128,11 @@ public OboSqlTokenProvider( | |
| { | ||
| wasCacheMiss = true; | ||
| _logger.LogInformation( | ||
| "OBO token cache MISS for subject {SubjectId} (tenant: {TenantId}). Acquiring new token from Azure AD.", | ||
| "{EventType}: OBO token cache MISS for subject {SubjectId} (tenant: {TenantId}, traceId: {TraceId}). Acquiring new token from Azure AD.", | ||
| "OboTokenCacheMiss", | ||
| subjectId, | ||
| tenantId); | ||
| tenantId, | ||
| Activity.Current?.TraceId.ToString() ?? "none"); | ||
|
|
||
| AuthenticationResult result = await _msalClient.AcquireTokenOnBehalfOfAsync( | ||
| scopes, | ||
|
|
@@ -144,8 +159,10 @@ public OboSqlTokenProvider( | |
| ctx.Options.SetSkipDistributedCache(true, true); | ||
|
|
||
| _logger.LogInformation( | ||
| "OBO token ACQUIRED for subject {SubjectId}. Expires: {ExpiresOn}, Cache TTL: {CacheDuration}.", | ||
| "{EventType}: OBO token ACQUIRED for subject {SubjectId} (traceId: {TraceId}). Expires: {ExpiresOn}, Cache TTL: {CacheDuration}.", | ||
| "OboTokenAcquired", | ||
| subjectId, | ||
| Activity.Current?.TraceId.ToString() ?? "none", | ||
| result.ExpiresOn, | ||
| cacheDuration); | ||
|
|
||
|
|
@@ -155,7 +172,11 @@ public OboSqlTokenProvider( | |
|
|
||
| if (!string.IsNullOrEmpty(accessToken) && !wasCacheMiss) | ||
| { | ||
| _logger.LogInformation("OBO token cache HIT for subject {SubjectId}.", subjectId); | ||
| _logger.LogInformation( | ||
| "{EventType}: OBO token cache HIT for subject {SubjectId} (traceId: {TraceId}).", | ||
| "OboTokenCacheHit", | ||
| subjectId, | ||
| Activity.Current?.TraceId.ToString() ?? "none"); | ||
| } | ||
|
|
||
| return accessToken; | ||
|
|
@@ -164,8 +185,10 @@ public OboSqlTokenProvider( | |
| { | ||
| _logger.LogError( | ||
| ex, | ||
| "Failed to acquire OBO token for subject {SubjectId}. Error: {ErrorCode} - {Message}", | ||
| "{EventType}: Failed to acquire OBO token for subject {SubjectId} (traceId: {TraceId}). Error: {ErrorCode} - {Message}", | ||
| "OboTokenAcquisitionFailed", | ||
| subjectId, | ||
| Activity.Current?.TraceId.ToString() ?? "none", | ||
| ex.ErrorCode, | ||
| ex.Message); | ||
| throw new DataApiBuilderException( | ||
|
|
||
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.
Because of our retry policy in query executor, a transient fault may end up trying to re-add params that already exist in this dictionary, which would error out. This is an already existing issue however, and I dont think Ive seen this show up yet, but figured it is worth flagging, and i think can be fixed by changing from .add to using an indexer; parameters[traceIdParamName]=foo