Skip to content

Commit cf91f5b

Browse files
authored
Fix the issue that string is returned instead of object. One more message is added to accommodate this situation. (#415)
1 parent 1c5829b commit cf91f5b

File tree

5 files changed

+37
-12
lines changed

5 files changed

+37
-12
lines changed

src/Common/AzurePSCmdlet.cs

+10-6
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ private IOutputSanitizer OutputSanitizer
171171
{
172172
get
173173
{
174-
if (AzureSession.Instance != null && AzureSession.Instance.TryGetComponent<IOutputSanitizer>(nameof(IOutputSanitizer), out var outputSanitizer))
174+
if (AzureSession.Instance.TryGetComponent<IOutputSanitizer>(nameof(IOutputSanitizer), out var outputSanitizer))
175175
return outputSanitizer;
176176

177177
return null;
@@ -407,11 +407,15 @@ private void WriteSecretsWarningMessage()
407407
if (_qosEvent?.SanitizerInfo != null)
408408
{
409409
var sanitizerInfo = _qosEvent.SanitizerInfo;
410-
if (sanitizerInfo.ShowSecretsWarning)
410+
if (sanitizerInfo.ShowSecretsWarning && sanitizerInfo.SecretsDetected)
411411
{
412-
if (sanitizerInfo.DetectedProperties?.Count > 0)
412+
if (sanitizerInfo.DetectedProperties.Count == 0)
413413
{
414-
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessage, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties)));
414+
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithoutProperty, MyInvocation.InvocationName));
415+
}
416+
else
417+
{
418+
WriteWarning(string.Format(Resources.DisplaySecretsWarningMessageWithProperty, MyInvocation.InvocationName, string.Join(", ", sanitizerInfo.DetectedProperties)));
415419
}
416420
}
417421
}
@@ -544,7 +548,7 @@ protected void WriteSurvey()
544548

545549
private void SanitizeOutput(object sendToPipeline)
546550
{
547-
if (OutputSanitizer != null && OutputSanitizer.RequireSecretsDetection)
551+
if (OutputSanitizer?.RequireSecretsDetection == true)
548552
{
549553
OutputSanitizer.Sanitize(sendToPipeline, out var telemetry);
550554
_qosEvent?.SanitizerInfo.Combine(telemetry);
@@ -767,7 +771,7 @@ protected virtual void InitializeQosEvent()
767771
}
768772
}
769773

770-
_qosEvent.SanitizerInfo = new SanitizerTelemetry();
774+
_qosEvent.SanitizerInfo = new SanitizerTelemetry(OutputSanitizer?.RequireSecretsDetection == true);
771775
}
772776

773777
private void RecordDebugMessages()

src/Common/MetricHelper.cs

+4-3
Original file line numberDiff line numberDiff line change
@@ -475,11 +475,12 @@ private void PopulateSanitizerPropertiesFromQos(AzurePSQoSEvent qos, IDictionary
475475
eventProperties["secrets-warning"] = qos.SanitizerInfo.ShowSecretsWarning.ToString();
476476
if (qos.SanitizerInfo.ShowSecretsWarning)
477477
{
478-
bool secretsDetected = qos.SanitizerInfo.DetectedProperties.Count > 0;
478+
bool secretsDetected = qos.SanitizerInfo.SecretsDetected;
479479
eventProperties["secrets-detected"] = secretsDetected.ToString();
480480
if (secretsDetected)
481481
{
482-
eventProperties.Add("secrets-detected-properties", string.Join(";", qos.SanitizerInfo.DetectedProperties));
482+
var detectedProperties = qos.SanitizerInfo.DetectedProperties.Count == 0 ? "[None]" : string.Join(";", qos.SanitizerInfo.DetectedProperties);
483+
eventProperties.Add("secrets-detected-properties", detectedProperties);
483484
}
484485
if (qos.SanitizerInfo.HasErrorInDetection && qos.SanitizerInfo.DetectionError != null)
485486
{
@@ -706,7 +707,7 @@ public override string ToString()
706707

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

709-
if (SanitizerInfo?.ShowSecretsWarning == true)
710+
if (SanitizerInfo.ShowSecretsWarning)
710711
{
711712
sb.Append($"; SanitizeDuration: {SanitizerInfo.SanitizeDuration}");
712713
}

src/Common/Properties/Resources.Designer.cs

+11-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Common/Properties/Resources.resx

+4-1
Original file line numberDiff line numberDiff line change
@@ -1760,7 +1760,10 @@ Note : Go to {0} for steps to suppress this breaking change warning, and other i
17601760
<data name="SurveyPreface" xml:space="preserve">
17611761
<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>
17621762
</data>
1763-
<data name="DisplaySecretsWarningMessage" xml:space="preserve">
1763+
<data name="DisplaySecretsWarningMessageWithProperty" xml:space="preserve">
17641764
<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>
17651765
</data>
1766+
<data name="DisplaySecretsWarningMessageWithoutProperty" xml:space="preserve">
1767+
<value>The output of cmdlet {0} may compromise security by showing secrets. Learn more at https://go.microsoft.com/fwlink/?linkid=2258844</value>
1768+
</data>
17661769
</root>

src/Common/Sanitizer/SanitizerTelemetry.cs

+8
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ public class SanitizerTelemetry
2121
{
2222
public bool ShowSecretsWarning { get; set; } = false;
2323

24+
public bool SecretsDetected { get; set; } = false;
25+
2426
public HashSet<string> DetectedProperties { get; set; } = new HashSet<string>();
2527

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

3032
public TimeSpan SanitizeDuration { get; set; }
3133

34+
public SanitizerTelemetry(bool showSecretsWarning)
35+
{
36+
ShowSecretsWarning = showSecretsWarning;
37+
}
38+
3239
public void Combine(SanitizerTelemetry telemetry)
3340
{
3441
if (telemetry != null)
3542
{
3643
ShowSecretsWarning = ShowSecretsWarning || telemetry.ShowSecretsWarning;
44+
SecretsDetected = SecretsDetected || telemetry.SecretsDetected;
3745
DetectedProperties.UnionWith(telemetry.DetectedProperties);
3846
HasErrorInDetection = HasErrorInDetection || telemetry.HasErrorInDetection;
3947
DetectionError = DetectionError ?? telemetry.DetectionError;

0 commit comments

Comments
 (0)