Skip to content

Commit

Permalink
RDMP-16 Add Max Message Length Check to Logging Notifications (#1595)
Browse files Browse the repository at this point in the history
* add message length check

* fix tabbing

* fix codescan alert

* update max length and add env config

* attempt to allow RDMP_

* fix typo

* add codeql update

* Update ToLoggingDatabaseDataLoadEventListener.cs

Adjust string trimming logic, handle ultra-short limits, only parse the environment variable once instead of per logging, avoid double-lookup in OnProgress handler

---------

Co-authored-by: James A Sutherland <[email protected]>
  • Loading branch information
JFriel and jas88 authored Aug 28, 2023
1 parent e0a5af6 commit ea0bc63
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,34 +59,51 @@ public virtual void StartLogging()
_logManager.CreateDataLoadInfo(_loggingTask, _hostingApplication.ToString(), _runDescription, "", false);
}

private const string RDMPLoggingStringLengthLimit = "RDMP_LOGGING_STRING_LENGTH_LIMIT";
private static readonly int StrStringLengthLimit;

static ToLoggingDatabaseDataLoadEventListener()
{
StrStringLengthLimit = int.TryParse(Environment.GetEnvironmentVariable(RDMPLoggingStringLengthLimit),out var limit) ? limit : int.MaxValue;
}

private static string EnsureMessageAValidLength(string message)
{
return StrStringLengthLimit < 4 ? "" :
message.Length > StrStringLengthLimit ? message[..(StrStringLengthLimit - 3)] + "..." : message;
}
public virtual void OnNotify(object sender, NotifyEventArgs e)
{
if (DataLoadInfo == null)
StartLogging();
if (StrStringLengthLimit < 4) // Logging suppressed
return;

switch (e.ProgressEventType)
{
case ProgressEventType.Trace:
case ProgressEventType.Debug:
break;
case ProgressEventType.Information:
DataLoadInfo.LogProgress(Logging.DataLoadInfo.ProgressEventType.OnInformation, sender.ToString(),
e.Message);
DataLoadInfo?.LogProgress(Logging.DataLoadInfo.ProgressEventType.OnInformation, sender.ToString(),
EnsureMessageAValidLength(e.Message));
break;
case ProgressEventType.Warning:
var msg = e.Message + (e.Exception == null
? ""
: Environment.NewLine + ExceptionHelper.ExceptionToListOfInnerMessages(e.Exception, true));
DataLoadInfo.LogProgress(Logging.DataLoadInfo.ProgressEventType.OnWarning, sender.ToString(), msg);
msg = EnsureMessageAValidLength(msg);
DataLoadInfo?.LogProgress(Logging.DataLoadInfo.ProgressEventType.OnWarning, sender.ToString(), msg);
break;
case ProgressEventType.Error:
var err = e.Message + (e.Exception == null
? ""
: Environment.NewLine + ExceptionHelper.ExceptionToListOfInnerMessages(e.Exception, true));
DataLoadInfo.LogFatalError(sender.ToString(), err);
err = EnsureMessageAValidLength(err);
DataLoadInfo?.LogFatalError(sender.ToString(), err);
break;
default:
throw new ArgumentOutOfRangeException();
throw new ArgumentOutOfRangeException(nameof(e));
}
}

Expand All @@ -97,18 +114,16 @@ public virtual void OnProgress(object sender, ProgressEventArgs e)

Debug.Assert(DataLoadInfo != null, "DataLoadInfo != null");

if (e.Progress.UnitOfMeasurement == ProgressType.Records)
if (e.Progress.UnitOfMeasurement != ProgressType.Records) return;

if (!TableLoads.TryGetValue(e.TaskDescription,out var t))
{
//if(!tableLoads.Any(tbl=>tbl.))
if (!TableLoads.ContainsKey(e.TaskDescription))
{
var t = DataLoadInfo.CreateTableLoadInfo("", e.TaskDescription,
new[] { new DataSource(sender.ToString()) }, e.Progress.KnownTargetValue);
TableLoads.Add(e.TaskDescription, t);
}

TableLoads[e.TaskDescription].Inserts = e.Progress.Value;
t = DataLoadInfo.CreateTableLoadInfo("", e.TaskDescription,
new[] { new DataSource(sender.ToString()) }, e.Progress.KnownTargetValue);
TableLoads.Add(e.TaskDescription, t);
}

t.Inserts = e.Progress.Value;
}

public virtual void FinalizeTableLoadInfos()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ public static void FindProblems(List<string> csFilesFound)
"Settings.Designer.cs",
"PlatformDatabaseCreationOptions.cs",
"PackOptions.cs",
"PasswordEncryptionKeyLocation.cs"
"PasswordEncryptionKeyLocation.cs",
"ToLoggingDatabaseDataLoadEventListener.cs"
}); //allowed because it's default arguments for CLI

prohibitedStrings.Add("TEST_");
Expand Down

0 comments on commit ea0bc63

Please sign in to comment.