Skip to content
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

Identity cache logger updates #3404

Merged
merged 73 commits into from
Jul 27, 2022
Merged
Show file tree
Hide file tree
Changes from 66 commits
Commits
Show all changes
73 commits
Select commit Hold shift + click to select a range
ca35676
Adding new identityLogger prototype
Mar 24, 2022
f35b470
typo
Mar 24, 2022
e82b691
Add I Log Scrubber
Mar 29, 2022
1d1cc4e
Introducing log scrubber and log adapters
Apr 12, 2022
7b2e924
Adding Logger package for prototype
Apr 21, 2022
eaa880a
Update identity logger package
May 26, 2022
24f7603
Refactoring to minimize use of identity logger package for computabil…
Jun 2, 2022
35d1e2e
Fixing errors
Jun 3, 2022
31e1bca
Fix build errors
Jun 3, 2022
a085cea
Removing extra nuget reference
Jun 3, 2022
9a9014a
Adding additional testing
Jun 6, 2022
221c46b
removing cs files from compile remove
Jun 6, 2022
c0d54ff
Test Update
Jun 6, 2022
72f94fe
Fix build errors
Jun 6, 2022
a6d3072
Fixing errors
Jun 6, 2022
48ef332
Test Fixes
Jun 7, 2022
f1284eb
Merge remote-tracking branch 'origin/master' into trwalke/IdentityLog…
Jun 7, 2022
acc962b
Fixing errors
Jun 7, 2022
3547409
Fix
Jun 7, 2022
7ef8608
fix error
Jun 7, 2022
e12a30a
Update build agent
Jun 7, 2022
8051921
Fixing build issues
Jun 8, 2022
3b0af88
Test Fixes
Jun 8, 2022
f1482f8
Test updates
Jun 8, 2022
82491f8
Test Updates
Jun 9, 2022
35b45d0
Revert "Test Updates"
Jun 9, 2022
1e23714
NetStandard Updates
Jun 9, 2022
44d4de9
null logger test
Jun 9, 2022
e7f8c74
Hiding from mobile platforms
Jun 9, 2022
9d931b6
Fixing build error
Jun 9, 2022
c3817f1
Removing mobile platforms from new logger
Jun 9, 2022
55f3ed7
update
Jun 9, 2022
1097f99
Hiding more stuff on mobile
Jun 9, 2022
10180b3
refactoring
Jun 9, 2022
c37cb65
Project Updates
Jun 9, 2022
17baf13
ui test update
Jun 9, 2022
4a1946a
Update Test-Cloud
Jun 10, 2022
3c47f7c
typo
Jun 10, 2022
d5fd348
typo
Jun 10, 2022
2000a89
Adding Logging
Jun 13, 2022
48fa122
Change NetStandard preprocessor statements
Jun 13, 2022
8c4cb94
Revert "update"
Jun 14, 2022
ee7ed7e
Revert "Hiding more stuff on mobile"
Jun 14, 2022
ea68e94
Adding logger package to andorid
Jun 14, 2022
114edec
Revert "Hiding from mobile platforms"
Jun 14, 2022
50ef289
Refactoring
Jun 14, 2022
8f46fd9
Suppressing obsolete error
Jun 14, 2022
bcac880
Resolving obsolete errors
Jun 14, 2022
60b697a
Revert "Suppressing obsolete error"
Jun 14, 2022
5318ad6
reverting suppressions
Jun 14, 2022
a2518e3
Adding test
Jun 15, 2022
1a2cf7e
Merge remote-tracking branch 'origin/master' into trwalke/IdentityLog…
Jun 16, 2022
d4ccd46
Refactoring
Jun 16, 2022
77d28f7
Removing MSAl Cache Logger
Jun 16, 2022
feb8321
Fixing automation
Jun 17, 2022
22c9c57
Merge branch 'master' into trwalke/IdentityLoggerUpdates
trwalke Jun 17, 2022
22cf87d
Merge remote-tracking branch 'origin/master' into trwalke/IdentityLog…
Jun 17, 2022
a82b9bc
Revert "Fixing automation"
Jun 17, 2022
45ce9e4
Refactoring
Jun 17, 2022
ec2f953
Revert "Revert "Fixing automation""
Jun 17, 2022
9048533
Revert "Removing MSAl Cache Logger"
Jun 20, 2022
7c8f0bc
Merge remote-tracking branch 'origin/master' into trwalke/IdentityLog…
Jun 20, 2022
2908953
Fixing constructors
Jun 20, 2022
60b56f2
Merge branch 'master' into trwalke/IdentityCacheLoggerUpdates
Jun 21, 2022
e8abb8e
Merge branch 'master' into trwalke/IdentityCacheLoggerUpdates
trwalke Jun 27, 2022
d841ae8
Refactoring to remove cache logger and move logic into loggerAdapter.
Jul 15, 2022
1a3621d
refactoring, adding test
Jul 19, 2022
545ef5c
Fix
Jul 19, 2022
00c1483
Fix error
Jul 20, 2022
d1d26c0
test fix
Jul 20, 2022
c71a766
Refactoring
Jul 25, 2022
17f83b1
Merge branch 'main' into trwalke/IdentityCacheLoggerUpdates
trwalke Jul 26, 2022
736c25a
Merge branch 'main' into trwalke/IdentityCacheLoggerUpdates
trwalke Jul 27, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ private async Task RefreshCacheForReadOperationsAsync()
suggestedCacheExpiry: null,
correlationId: _requestParams.RequestContext.CorrelationId,
requestScopes: _requestParams.Scope,
requestTenantId: _requestParams.AuthorityManager.OriginalAuthority.TenantId);
requestTenantId: _requestParams.AuthorityManager.OriginalAuthority.TenantId,
msalIdentityLogger: _requestParams.RequestContext.Logger.MsalIdentityLogger,
trwalke marked this conversation as resolved.
Show resolved Hide resolved
piiLoggingEnabled: _requestParams.RequestContext.Logger.PiiLoggingEnabled);

stopwatch.Start();
await TokenCacheInternal.OnBeforeAccessAsync(args).ConfigureAwait(false);
Expand All @@ -151,7 +153,9 @@ private async Task RefreshCacheForReadOperationsAsync()
suggestedCacheExpiry: null,
correlationId: _requestParams.RequestContext.CorrelationId,
requestScopes: _requestParams.Scope,
requestTenantId: _requestParams.AuthorityManager.OriginalAuthority.TenantId);
requestTenantId: _requestParams.AuthorityManager.OriginalAuthority.TenantId,
msalIdentityLogger: _requestParams.RequestContext.Logger.MsalIdentityLogger,
piiLoggingEnabled: _requestParams.RequestContext.Logger.PiiLoggingEnabled);

await TokenCacheInternal.OnAfterAccessAsync(args).ConfigureAwait(false);
RequestContext.ApiEvent.DurationInCacheInMs += stopwatch.ElapsedMilliseconds;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.Runtime.CompilerServices;
using Microsoft.Identity.Client.Internal.Logger;
using Microsoft.IdentityModel.Abstractions;

namespace Microsoft.Identity.Client.Core
{
Expand All @@ -12,6 +13,7 @@ internal interface ILoggerAdapter
bool IsDefaultPlatformLoggingEnabled { get; }
string ClientName { get; }
string ClientVersion { get; }
IIdentityLogger MsalIdentityLogger { get; }
trwalke marked this conversation as resolved.
Show resolved Hide resolved

/// <summary>
/// For expensive logging messages (e.g. when the log message evaluates a variable),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,12 @@ namespace Microsoft.Identity.Client.Internal.Logger
#if !XAMARINMAC20
internal class IdentityLoggerAdapter : ILoggerAdapter
{
private readonly IIdentityLogger _identityLogger;
private string _clientInfo;
private string _correlationId;

public bool PiiLoggingEnabled { get; }
public bool IsDefaultPlatformLoggingEnabled { get; } = false;
public string ClientName { get; }
public string ClientVersion { get; }
public IIdentityLogger MsalIdentityLogger { get; }

internal IdentityLoggerAdapter(
IIdentityLogger identityLogger,
Expand All @@ -31,12 +29,10 @@ internal IdentityLoggerAdapter(
{
ClientName = clientName;
ClientVersion = clientVersion;
_identityLogger = identityLogger;
MsalIdentityLogger = new MsalIdentityLogger(identityLogger, correlationId, clientName, clientVersion, enablePiiLogging);
_correlationId = correlationId.Equals(Guid.Empty)
? string.Empty
: " - " + correlationId;

_clientInfo = LoggerHelper.GetClientInfo(clientName, clientVersion);

PiiLoggingEnabled = enablePiiLogging;
}
Expand All @@ -57,17 +53,19 @@ public void Log(LogLevel logLevel, string messageWithPii, string messageScrubbed
{
if (IsLoggingEnabled(logLevel))
{
string messageToLog = LoggerHelper.GetMessageToLog(messageWithPii, messageScrubbed, PiiLoggingEnabled);

LogEntry entry = new LogEntry();
entry.EventLogLevel = LoggerHelper.GetEventLogLevel(logLevel);
entry.CorrelationId = _correlationId;
entry.Message = LoggerHelper.FormatLogMessage(messageWithPii, messageScrubbed, PiiLoggingEnabled, _correlationId, _clientInfo);
_identityLogger.Log(entry);
entry.Message = messageToLog;
MsalIdentityLogger.Log(entry);
}
}

public bool IsLoggingEnabled(LogLevel logLevel)
{
return _identityLogger.IsEnabled(LoggerHelper.GetEventLogLevel(logLevel));
return MsalIdentityLogger.IsEnabled(LoggerHelper.GetEventLogLevel(logLevel));
}

public DurationLogHelper LogBlockDuration(string measuredBlockName, LogLevel logLevel = LogLevel.Verbose)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@
using System.Diagnostics.Tracing;
using System.Runtime.CompilerServices;
using Microsoft.Identity.Client.Core;
using Microsoft.IdentityModel.Abstractions;

namespace Microsoft.Identity.Client.Internal.Logger
{
internal class LegacyIdentityLoggerAdapter : ILoggerAdapter
{
LogLevel _minLogLevel = LogLevel.Always;
LogCallback _logCallback;
private string _clientInfo;
private string _correlationId;

public bool PiiLoggingEnabled { get; }
Expand All @@ -21,9 +20,11 @@ internal class LegacyIdentityLoggerAdapter : ILoggerAdapter
public string ClientName { get; }
public string ClientVersion { get; }

public IIdentityLogger MsalIdentityLogger { get; }

public bool IsLoggingEnabled(LogLevel logLevel)
{
return _logCallback != null && logLevel <= _minLogLevel;
return MsalIdentityLogger.IsEnabled(LoggerHelper.GetEventLogLevel(logLevel));
}

internal LegacyIdentityLoggerAdapter(
Expand All @@ -41,20 +42,25 @@ internal LegacyIdentityLoggerAdapter(
? string.Empty
: " - " + correlationId;

_clientInfo = LoggerHelper.GetClientInfo(clientName, clientVersion);

PiiLoggingEnabled = enablePiiLogging;
_logCallback = loggingCallback;
_minLogLevel = logLevel;
IsDefaultPlatformLoggingEnabled = isDefaultPlatformLoggingEnabled;
MsalIdentityLogger = new MsalLegacyIdentityLogger(loggingCallback, correlationId, clientName, clientVersion, enablePiiLogging, logLevel);
}

public void Log(LogLevel logLevel, string messageWithPii, string messageScrubbed)
{
if (IsLoggingEnabled(logLevel))
{
string message = LoggerHelper.FormatLogMessage(messageWithPii, messageScrubbed, PiiLoggingEnabled, _correlationId, _clientInfo);
_logCallback.Invoke(logLevel, message, !string.IsNullOrEmpty(messageWithPii) ? true : false);
string messageToLog = LoggerHelper.GetMessageToLog(messageWithPii, messageScrubbed, PiiLoggingEnabled);

LogEntry entry = new LogEntry();
entry.EventLogLevel = LoggerHelper.GetEventLogLevel(logLevel);
entry.CorrelationId = _correlationId;
entry.Message = messageToLog;
MsalIdentityLogger.Log(entry);
//string message = LoggerHelper.FormatLogMessage(messageToLog, PiiLoggingEnabled, _correlationId, _clientInfo);
trwalke marked this conversation as resolved.
Show resolved Hide resolved
//_logCallback.Invoke(logLevel, message, !string.IsNullOrEmpty(messageWithPii) ? true : false);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,25 +78,25 @@ public static ILoggerAdapter CreateLogger(
return "Unknown SKU";
});

public static string FormatLogMessage(string messageWithPii, string messageScrubbed, bool piiEnabled, string correlationId, string clientInformation)
public static string FormatLogMessage(string message, bool piiEnabled, string correlationId, string clientInformation)
{
bool messageWithPiiExists = !string.IsNullOrWhiteSpace(messageWithPii);
// If we have a message with PII, and PII logging is enabled, use the PII message, else use the scrubbed message.
bool isLoggingPii = messageWithPiiExists && piiEnabled;
string messageToLog = isLoggingPii ? messageWithPii : messageScrubbed;
//bool messageWithPiiExists = !string.IsNullOrWhiteSpace(messageWithPii);
trwalke marked this conversation as resolved.
Show resolved Hide resolved
//// If we have a message with PII, and PII logging is enabled, use the PII message, else use the scrubbed message.
//bool isLoggingPii = messageWithPiiExists && piiEnabled;
//string messageToLog = isLoggingPii ? messageWithPii : messageScrubbed;

return string.Format(
CultureInfo.InvariantCulture,
"{0} MSAL {1} {2} {3} {4} [{5}{6}]{7} {8}",
isLoggingPii,
piiEnabled,
s_msalVersionLazy.Value,
s_skuLazy.Value,
s_runtimeVersionLazy.Value,
s_osLazy.Value,
DateTime.UtcNow.ToString("u"),
correlationId,
clientInformation,
messageToLog);
message);
}

internal static string GetPiiScrubbedExceptionDetails(Exception ex)
Expand Down Expand Up @@ -154,5 +154,13 @@ public static EventLogLevel GetEventLogLevel(LogLevel logLevel)

return (EventLogLevel)((int)logLevel + 2);
}

public static string GetMessageToLog(string messageWithPii, string messageScrubbed, bool piiLoggingEnabled)
{
bool messageWithPiiExists = !string.IsNullOrWhiteSpace(messageWithPii);
// If we have a message with PII, and PII logging is enabled, use the PII message, else use the scrubbed message.
bool isLoggingPii = messageWithPiiExists && piiLoggingEnabled;
return isLoggingPii ? messageWithPii : messageScrubbed;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using Microsoft.IdentityModel.Abstractions;

namespace Microsoft.Identity.Client.Internal.Logger
{
#if !XAMARINMAC20
//This class is used to wrap the functionality of the configured IIdentityLogger to add additional MSAL client information when logging messages.
internal class MsalIdentityLogger : IIdentityLogger
trwalke marked this conversation as resolved.
Show resolved Hide resolved
{
private readonly IIdentityLogger _identityLogger;
private readonly string _correlationId;
private readonly string _clientInformation;
private readonly bool _piiLoggingEnabled;

internal MsalIdentityLogger(IIdentityLogger identityLogger, Guid correlationId, string clientName, string clientVersion, bool enablePiiLogging)
{
_identityLogger = identityLogger;
_correlationId = correlationId.Equals(Guid.Empty)
? string.Empty
: " - " + correlationId;
_clientInformation = LoggerHelper.GetClientInfo(clientName, clientVersion);
_piiLoggingEnabled = enablePiiLogging;
}

public bool IsEnabled(EventLogLevel eventLevel)
{
return _identityLogger.IsEnabled(eventLevel);
}

public void Log(LogEntry entry)
{
entry.Message = LoggerHelper.FormatLogMessage(
entry.Message,
_piiLoggingEnabled,
string.IsNullOrEmpty(entry.CorrelationId) ? entry.CorrelationId : _correlationId,
trwalke marked this conversation as resolved.
Show resolved Hide resolved
_clientInformation);

_identityLogger.Log(entry);
}
}
#endif
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.IdentityModel.Abstractions;

namespace Microsoft.Identity.Client.Internal.Logger
{
internal class MsalLegacyIdentityLogger : IIdentityLogger
trwalke marked this conversation as resolved.
Show resolved Hide resolved
{
private LogCallback _logCallback;
private readonly string _correlationId;
private readonly string _clientInformation;
private readonly bool _piiLoggingEnabled;
private readonly LogLevel _minLogLevel;

public MsalLegacyIdentityLogger(
LogCallback logCallback,
Guid correlationId,
string clientName,
string clientVersion,
bool enablePiiLogging,
LogLevel minLogLevel)
{
_correlationId = correlationId.Equals(Guid.Empty)
? string.Empty
: " - " + correlationId;
_clientInformation = LoggerHelper.GetClientInfo(clientName, clientVersion);
_piiLoggingEnabled = enablePiiLogging;
_logCallback = logCallback;
_minLogLevel = minLogLevel;
}

public bool IsEnabled(EventLogLevel eventLevel)
{
return _logCallback != null && GetLogLevel(eventLevel) <= _minLogLevel;
}

public void Log(LogEntry entry)
{
string formattedMessage = LoggerHelper.FormatLogMessage(
entry.Message,
_piiLoggingEnabled,
string.IsNullOrEmpty(entry.CorrelationId) ? entry.CorrelationId : _correlationId,
_clientInformation);

_logCallback.Invoke(GetLogLevel(entry.EventLogLevel), formattedMessage, _piiLoggingEnabled);
}

private LogLevel GetLogLevel(EventLogLevel eventLogLevel)
{
//MSAL does not have a critical log level so it is combined with the error level
if (eventLogLevel == EventLogLevel.LogAlways)
{
return LogLevel.Always;
}
return (LogLevel)((int)eventLogLevel - 2);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ async Task<Tuple<MsalAccessTokenCacheItem, MsalIdTokenCacheItem, Account>> IToke
cancellationToken: requestParams.RequestContext.UserCancellationToken,
correlationId: requestParams.RequestContext.CorrelationId,
requestScopes: requestParams.Scope,
requestTenantId: requestParams.AuthorityManager.OriginalAuthority.TenantId);
requestTenantId: requestParams.AuthorityManager.OriginalAuthority.TenantId,
msalIdentityLogger: requestParams.RequestContext.Logger.MsalIdentityLogger,
piiLoggingEnabled: requestParams.RequestContext.Logger.PiiLoggingEnabled);

Stopwatch sw = Stopwatch.StartNew();

Expand Down Expand Up @@ -237,8 +239,9 @@ async Task<Tuple<MsalAccessTokenCacheItem, MsalIdTokenCacheItem, Account>> IToke
cancellationToken: requestParams.RequestContext.UserCancellationToken,
correlationId: requestParams.RequestContext.CorrelationId,
requestScopes: requestParams.Scope,
requestTenantId: requestParams.AuthorityManager.OriginalAuthority.TenantId);

requestTenantId: requestParams.AuthorityManager.OriginalAuthority.TenantId,
msalIdentityLogger: requestParams.RequestContext.Logger.MsalIdentityLogger,
piiLoggingEnabled: requestParams.RequestContext.Logger.PiiLoggingEnabled);

Stopwatch sw = Stopwatch.StartNew();
await tokenCacheInternal.OnAfterAccessAsync(args).ConfigureAwait(false);
Expand Down Expand Up @@ -728,7 +731,9 @@ internal async Task ExpireAllAccessTokensForTestAsync()
cancellationToken: default,
correlationId: default,
requestScopes: null,
requestTenantId: null);
requestTenantId: null,
msalIdentityLogger: null,
piiLoggingEnabled: false);

await tokenCacheInternal.OnAfterAccessAsync(args).ConfigureAwait(false);
}
Expand Down Expand Up @@ -1147,7 +1152,9 @@ async Task ITokenCacheInternal.RemoveAccountAsync(IAccount account, Authenticati
cancellationToken: requestParameters.RequestContext.UserCancellationToken,
correlationId: requestParameters.RequestContext.CorrelationId,
requestScopes: requestParameters.Scope,
requestTenantId: requestParameters.AuthorityManager.OriginalAuthority.TenantId);
requestTenantId: requestParameters.AuthorityManager.OriginalAuthority.TenantId,
msalIdentityLogger: requestParameters.RequestContext.Logger.MsalIdentityLogger,
piiLoggingEnabled: requestParameters.RequestContext.Logger.PiiLoggingEnabled);


await tokenCacheInternal.OnBeforeAccessAsync(args).ConfigureAwait(false);
Expand Down Expand Up @@ -1181,7 +1188,9 @@ async Task ITokenCacheInternal.RemoveAccountAsync(IAccount account, Authenticati
cancellationToken: requestParameters.RequestContext.UserCancellationToken,
correlationId: requestParameters.RequestContext.CorrelationId,
requestScopes: requestParameters.Scope,
requestTenantId: requestParameters.AuthorityManager.OriginalAuthority.TenantId);
requestTenantId: requestParameters.AuthorityManager.OriginalAuthority.TenantId,
msalIdentityLogger: requestParameters.RequestContext.Logger.MsalIdentityLogger,
piiLoggingEnabled: requestParameters.RequestContext.Logger.PiiLoggingEnabled);


await tokenCacheInternal.OnAfterAccessAsync(args).ConfigureAwait(false);
Expand Down
Loading