Skip to content

Commit

Permalink
Fix the issue that string is returned instead of object. One more mes…
Browse files Browse the repository at this point in the history
…sage is added to accommodate this situation. (#415)
  • Loading branch information
vidai-msft authored Apr 11, 2024
1 parent 1c5829b commit cf91f5b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 12 deletions.
16 changes: 10 additions & 6 deletions src/Common/AzurePSCmdlet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private IOutputSanitizer OutputSanitizer
{
get
{
if (AzureSession.Instance != null && AzureSession.Instance.TryGetComponent<IOutputSanitizer>(nameof(IOutputSanitizer), out var outputSanitizer))
if (AzureSession.Instance.TryGetComponent<IOutputSanitizer>(nameof(IOutputSanitizer), out var outputSanitizer))
return outputSanitizer;

return null;
Expand Down Expand Up @@ -407,11 +407,15 @@ private void WriteSecretsWarningMessage()
if (_qosEvent?.SanitizerInfo != null)
{
var sanitizerInfo = _qosEvent.SanitizerInfo;
if (sanitizerInfo.ShowSecretsWarning)
if (sanitizerInfo.ShowSecretsWarning && sanitizerInfo.SecretsDetected)
{
if (sanitizerInfo.DetectedProperties?.Count > 0)
if (sanitizerInfo.DetectedProperties.Count == 0)
{
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessage, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties)));
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithoutProperty, MyInvocation.InvocationName));
}
else
{
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithProperty, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties)));
}
}
}
Expand Down Expand Up @@ -544,7 +548,7 @@ protected void WriteSurvey()

private void SanitizeOutput(object sendToPipeline)
{
if (OutputSanitizer != null && OutputSanitizer.RequireSecretsDetection)
if (OutputSanitizer?.RequireSecretsDetection == true)
{
OutputSanitizer.Sanitize(sendToPipeline, out var telemetry);
_qosEvent?.SanitizerInfo.Combine(telemetry);
Expand Down Expand Up @@ -767,7 +771,7 @@ protected virtual void InitializeQosEvent()
}
}

_qosEvent.SanitizerInfo = new SanitizerTelemetry();
_qosEvent.SanitizerInfo = new SanitizerTelemetry(OutputSanitizer?.RequireSecretsDetection == true);
}

private void RecordDebugMessages()
Expand Down
7 changes: 4 additions & 3 deletions src/Common/MetricHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -475,11 +475,12 @@ private void PopulateSanitizerPropertiesFromQos(AzurePSQoSEvent qos, IDictionary
eventProperties["secrets-warning"] = qos.SanitizerInfo.ShowSecretsWarning.ToString();
if (qos.SanitizerInfo.ShowSecretsWarning)
{
bool secretsDetected = qos.SanitizerInfo.DetectedProperties.Count > 0;
bool secretsDetected = qos.SanitizerInfo.SecretsDetected;
eventProperties["secrets-detected"] = secretsDetected.ToString();
if (secretsDetected)
{
eventProperties.Add("secrets-detected-properties", string.Join(";", qos.SanitizerInfo.DetectedProperties));
var detectedProperties = qos.SanitizerInfo.DetectedProperties.Count == 0 ? "[None]" : string.Join(";", qos.SanitizerInfo.DetectedProperties);
eventProperties.Add("secrets-detected-properties", detectedProperties);
}
if (qos.SanitizerInfo.HasErrorInDetection && qos.SanitizerInfo.DetectionError != null)
{
Expand Down Expand Up @@ -706,7 +707,7 @@ public override string ToString()

sb.Append($"; IsSuccess: {IsSuccess}; Duration: {Duration}");

if (SanitizerInfo?.ShowSecretsWarning == true)
if (SanitizerInfo.ShowSecretsWarning)
{
sb.Append($"; SanitizeDuration: {SanitizerInfo.SanitizeDuration}");
}
Expand Down
13 changes: 11 additions & 2 deletions src/Common/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/Common/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -1760,7 +1760,10 @@ Note : Go to {0} for steps to suppress this breaking change warning, and other i
<data name="SurveyPreface" xml:space="preserve">
<value>[Survey] Help us improve Azure PowerShell by sharing your experience. This survey should take about 5 minutes. Run 'Open-AzSurveyLink' to open in browser. Learn more at {0}</value>
</data>
<data name="DisplaySecretsWarningMessage" xml:space="preserve">
<data name="DisplaySecretsWarningMessageWithProperty" xml:space="preserve">
<value>The output of cmdlet {0} may compromise security by showing the following secrets: {1}. Learn more at https://go.microsoft.com/fwlink/?linkid=2258844</value>
</data>
<data name="DisplaySecretsWarningMessageWithoutProperty" xml:space="preserve">
<value>The output of cmdlet {0} may compromise security by showing secrets. Learn more at https://go.microsoft.com/fwlink/?linkid=2258844</value>
</data>
</root>
8 changes: 8 additions & 0 deletions src/Common/Sanitizer/SanitizerTelemetry.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ public class SanitizerTelemetry
{
public bool ShowSecretsWarning { get; set; } = false;

public bool SecretsDetected { get; set; } = false;

public HashSet<string> DetectedProperties { get; set; } = new HashSet<string>();

public bool HasErrorInDetection { get; set; } = false;
Expand All @@ -29,11 +31,17 @@ public class SanitizerTelemetry

public TimeSpan SanitizeDuration { get; set; }

public SanitizerTelemetry(bool showSecretsWarning)
{
ShowSecretsWarning = showSecretsWarning;
}

public void Combine(SanitizerTelemetry telemetry)
{
if (telemetry != null)
{
ShowSecretsWarning = ShowSecretsWarning || telemetry.ShowSecretsWarning;
SecretsDetected = SecretsDetected || telemetry.SecretsDetected;
DetectedProperties.UnionWith(telemetry.DetectedProperties);
HasErrorInDetection = HasErrorInDetection || telemetry.HasErrorInDetection;
DetectionError = DetectionError ?? telemetry.DetectionError;
Expand Down

0 comments on commit cf91f5b

Please sign in to comment.