Skip to content

Commit

Permalink
Update logging for Runtime Broker (#4318)
Browse files Browse the repository at this point in the history
Update WamAdapters.cs
  • Loading branch information
gladjohn authored Aug 30, 2023
1 parent 3e13383 commit 0ea350f
Showing 1 changed file with 22 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ private static MsalException CreateExceptionFromWamError(
long errorCode = authResult.Error.ErrorCode;
string errorMessage;

logger.Info("[WamBroker] Processing WAM exception");
logger.Verbose(() => $"[WamBroker] TelemetryData: {authResult.TelemetryData}");
logger.Info("[RuntimeBroker] Processing WAM exception");
logger.Verbose(() => $"[RuntimeBroker] TelemetryData: {authResult.TelemetryData}");

switch ((ResponseStatus)authResult.Error.Status)
{
case ResponseStatus.UserCanceled:
logger.Error($"[WamBroker] {MsalError.AuthenticationCanceledError} {MsalErrorMessage.AuthenticationCanceled}");
logger.Error($"[RuntimeBroker] {MsalError.AuthenticationCanceledError} {MsalErrorMessage.AuthenticationCanceled}");
var clientEx = new MsalClientException(MsalError.AuthenticationCanceledError, MsalErrorMessage.AuthenticationCanceled);
return clientEx;
case ResponseStatus.InteractionRequired:
Expand All @@ -79,7 +79,7 @@ private static MsalException CreateExceptionFromWamError(
$" Error Code: {errorCode} \n" +
$" Error Message: {authResult.Error.Context} \n" +
$" Internal Error Code: {internalErrorCode} \n";
logger.Error($"[WamBroker] {MsalError.FailedToAcquireTokenSilentlyFromBroker} {errorMessage}");
logger.Error($"[RuntimeBroker] {MsalError.FailedToAcquireTokenSilentlyFromBroker} {errorMessage}");
var ex = new MsalUiRequiredException(MsalError.FailedToAcquireTokenSilentlyFromBroker, errorMessage);
return ex;

Expand All @@ -94,7 +94,7 @@ private static MsalException CreateExceptionFromWamError(
$" Possible causes: \n" +
$"- Invalid redirect uri - ensure you have configured the following url in the application registration in Azure Portal: " +
$"{GetExpectedRedirectUri(authenticationRequestParameters.AppConfig.ClientId)} \n";
logger.Error($"[WamBroker] WAM_provider_error_{errorCode} {errorMessage}");
logger.Error($"[RuntimeBroker] WAM_provider_error_{errorCode} {errorMessage}");
serviceException = new MsalServiceException($"WAM_provider_error_{errorCode}", errorMessage);
serviceException.IsRetryable = false;
return serviceException;
Expand All @@ -110,14 +110,14 @@ private static MsalException CreateExceptionFromWamError(
$" Internal Error Code: {internalErrorCode} \n" +
$" Possible cause: no Internet connection ";

logger.Error($"[WamBroker] WAM_network_error_{errorCode} {errorMessage}");
logger.Error($"[RuntimeBroker] WAM_network_error_{errorCode} {errorMessage}");
serviceException = new MsalServiceException(errorCode.ToString(), errorMessage);
serviceException.IsRetryable = true;
return serviceException;

default:
errorMessage = $"Unknown {authResult.Error} (error code {errorCode}) (internal error code {internalErrorCode})";
logger.Verbose(() => $"[WamBroker] {MsalError.UnknownBrokerError} {errorMessage}");
logger.Verbose(() => $"[RuntimeBroker] {MsalError.UnknownBrokerError} {errorMessage}");
var ex2 = new MsalServiceException(MsalError.UnknownBrokerError, errorMessage);
return ex2;
}
Expand All @@ -144,12 +144,12 @@ public static NativeInterop.AuthParameters GetCommonAuthParameters(
if (!ScopeHelper.HasNonMsalScopes(authenticationRequestParameters.Scope))
{
authParams.RequestedScopes = ScopeHelper.GetMsalRuntimeScopes();
logger.Verbose(() => "[WamBroker] No scopes were passed in the request. Adding default scopes.");
logger.Verbose(() => "[RuntimeBroker] No scopes were passed in the request. Adding default scopes.");
}
else
{
authParams.RequestedScopes = string.Join(" ", authenticationRequestParameters.Scope);
logger.Verbose(() => "[WamBroker] Scopes were passed in the request.");
logger.Verbose(() => "[RuntimeBroker] Scopes were passed in the request.");
}

//WAM redirect URi does not need to be configured by the user
Expand Down Expand Up @@ -193,7 +193,7 @@ public static NativeInterop.AuthParameters GetCommonAuthParameters(

AddPopParams(authenticationRequestParameters, authParams);

logger.Verbose(() => "[WamBroker] Acquired Common Auth Parameters.");
logger.Verbose(() => "[RuntimeBroker] Acquired Common Auth Parameters.");

return authParams;
}
Expand Down Expand Up @@ -221,7 +221,7 @@ private static void AddPopParams(AuthenticationRequestParameters authenticationR
// if PopAuthenticationConfiguration is set, proof of possession will be performed via the runtime broker
if (authenticationRequestParameters.PopAuthenticationConfiguration != null)
{
authenticationRequestParameters.RequestContext.Logger.Info("[WamBroker] Proof-of-Possession is configured. Using Proof-of-Possession with broker request. ");
authenticationRequestParameters.RequestContext.Logger.Info("[RuntimeBroker] Proof-of-Possession is configured. Using Proof-of-Possession with broker request. ");
authParams.PopParams.HttpMethod = authenticationRequestParameters.PopAuthenticationConfiguration.HttpMethod?.Method;
authParams.PopParams.UriHost = authenticationRequestParameters.PopAuthenticationConfiguration.HttpHost;
authParams.PopParams.UriPath = authenticationRequestParameters.PopAuthenticationConfiguration.HttpPath;
Expand All @@ -247,12 +247,12 @@ private static void SetMSALIdentityProvider(

if (tenantObjectId.Equals(Constants.MsaTenantId, StringComparison.OrdinalIgnoreCase))
{
logger.Verbose(() => $"[WamBroker] MSALRuntime Identity provider set to MSA.");
logger.Verbose(() => $"[RuntimeBroker] MSALRuntime Identity provider set to MSA.");
authParams.Properties[MsalIdentityProvider] = IdentityProviderTypeMSA;
}
else
{
logger.Verbose(() => "[WamBroker] MSALRuntime Identity provider set to AAD");
logger.Verbose(() => "[RuntimeBroker] MSALRuntime Identity provider set to AAD");
authParams.Properties[MsalIdentityProvider] = IdentityProviderTypeAAD;
}
}
Expand All @@ -269,11 +269,11 @@ public static MsalTokenResponse HandleResponse(
if (TokenReceivedFromWam(authResult, logger))
{
msalTokenResponse = ParseRuntimeResponse(authResult, authenticationRequestParameters, logger);
logger.Verbose(() => "[WamBroker] Successfully retrieved token.");
logger.Verbose(() => "[RuntimeBroker] Successfully retrieved token.");
return msalTokenResponse;
}

logger.Info($"[WamBroker] {errorMessage} {authResult.Error}");
logger.Info($"[RuntimeBroker] {errorMessage} {authResult.Error}");
MsalException ex = CreateExceptionFromWamError(authResult, authenticationRequestParameters, logger);
ex = DecorateExceptionWithRuntimeErrorProperties(ex, authResult.Error);
throw ex;
Expand All @@ -297,7 +297,7 @@ private static bool TokenReceivedFromWam(
if (authResult.Error != null
&& (ResponseStatus)authResult.Error.Status == ResponseStatus.UserSwitch)
{
logger.Info("[WamBroker] WAM response status account switch. Treating as success");
logger.Info("[RuntimeBroker] WAM response status account switch. Treating as success");
return true;
}

Expand All @@ -323,7 +323,7 @@ private static MsalTokenResponse ParseRuntimeResponse(

if (string.IsNullOrWhiteSpace(correlationId))
{
logger.Warning("[WamBroker] No correlation ID in response");
logger.Warning("[RuntimeBroker] No correlation ID in response");
correlationId = null;
}

Expand Down Expand Up @@ -356,13 +356,13 @@ private static MsalTokenResponse ParseRuntimeResponse(
TokenSource = TokenSource.Broker
};

logger.Info("[WamBroker] WAM response status success");
logger.Info("[RuntimeBroker] WAM response status success");

return msalTokenResponse;
}
catch (MsalRuntimeException ex)
{
logger.ErrorPii($"[WamBroker] Could not acquire token using WAM. {ex.Message}", string.Empty);
logger.ErrorPii($"[RuntimeBroker] Could not acquire token using WAM. {ex.Message}", string.Empty);
throw new MsalServiceException("wam_failed", $"Could not acquire token using WAM. {ex.Message}");
}

Expand Down Expand Up @@ -439,7 +439,7 @@ private static void ToLogMessage(
{
// Create PII enabled string builder
var builder = new StringBuilder(
Environment.NewLine + "=== [WamBroker] Converting WAM Account to MSAL Account ===" +
Environment.NewLine + "=== [RuntimeBroker] Converting WAM Account to MSAL Account ===" +
Environment.NewLine);

builder.AppendLine($"wamAccount.AccountId: {wamAccount.AccountId}.");
Expand All @@ -450,7 +450,7 @@ private static void ToLogMessage(

// Create non PII enabled string builder
builder = new StringBuilder(
Environment.NewLine + "=== [WamBroker] Converting WAM Account to MSAL Account ===" +
Environment.NewLine + "=== [RuntimeBroker] Converting WAM Account to MSAL Account ===" +
Environment.NewLine);

builder.AppendLine($"wamAccount.AccountId: {string.IsNullOrEmpty(wamAccount.AccountId)}.");
Expand All @@ -461,7 +461,7 @@ private static void ToLogMessage(
logger.InfoPii(messageWithPii, builder.ToString());
}

logger.Error($"[WamBroker] WAM Account properties are missing. Cannot convert to MSAL Accounts.");
logger.Error($"[RuntimeBroker] WAM Account properties are missing. Cannot convert to MSAL Accounts.");
}
}
}

1 comment on commit 0ea350f

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark 'TokenCacheTestsWithCache'.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 1.30.

Benchmark suite Current: 0ea350f Previous: c6f4a21 Ratio
Microsoft.Identity.Test.Performance.TokenCacheTests.RemoveAccountAsync_TestAsync(CacheSize: (10000, 10)) 256787.73958333334 ns (± 96080.76654513062) 188063.05208333334 ns (± 77057.40313481545) 1.37

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.