From c2d17d3a98ef43c9da47ab41ca9116acf774042a Mon Sep 17 00:00:00 2001 From: Shaopeng Li Date: Thu, 30 Nov 2023 01:39:44 -0800 Subject: [PATCH] Fall back to DefaultTelemetryConnectionString --- src/BinSkim.Driver/AnalyzeOptions.cs | 5 + .../MultithreadedAnalyzeCommand.cs | 8 +- src/BinSkim.Sdk/BinSkim.Sdk.csproj | 9 ++ src/BinSkim.Sdk/BinaryAnalyzerContext.cs | 6 + .../EnvironmentResources.Designer.cs | 72 ++++++++++ src/BinSkim.Sdk/EnvironmentResources.resx | 123 ++++++++++++++++++ src/BinSkim.Sdk/Telemetry.cs | 7 + src/BinaryParsers/BinaryParsersProperties.cs | 5 + 8 files changed, 234 insertions(+), 1 deletion(-) create mode 100644 src/BinSkim.Sdk/EnvironmentResources.Designer.cs create mode 100644 src/BinSkim.Sdk/EnvironmentResources.resx diff --git a/src/BinSkim.Driver/AnalyzeOptions.cs b/src/BinSkim.Driver/AnalyzeOptions.cs index c1e1a01d..f19b3f29 100644 --- a/src/BinSkim.Driver/AnalyzeOptions.cs +++ b/src/BinSkim.Driver/AnalyzeOptions.cs @@ -53,6 +53,11 @@ public class AnalyzeOptions : AnalyzeOptionsBase HelpText = "If enabled, BinSkim won't break if we have a 'PdbLoadingException'.")] public bool? IgnorePdbLoadError { get; set; } + [Option( + "disable-telemetry", + HelpText = "If enabled, BinSkim will disable telemetry.")] + public bool? DisableTelemetry { get; set; } + [Option( 's', "statistics", diff --git a/src/BinSkim.Driver/MultithreadedAnalyzeCommand.cs b/src/BinSkim.Driver/MultithreadedAnalyzeCommand.cs index 3a685923..725bfeb9 100644 --- a/src/BinSkim.Driver/MultithreadedAnalyzeCommand.cs +++ b/src/BinSkim.Driver/MultithreadedAnalyzeCommand.cs @@ -70,6 +70,7 @@ public override BinaryAnalyzerContext InitializeGlobalContextFromOptions(Analyze // Update context object based on command-line parameters. context.SymbolPath = options.SymbolsPath ?? context.SymbolPath; context.IgnorePdbLoadError = options.IgnorePdbLoadError != null ? options.IgnorePdbLoadError.Value : context.IgnorePdbLoadError; + context.DisableTelemetry = options.DisableTelemetry != null ? options.DisableTelemetry.Value : context.DisableTelemetry; context.LocalSymbolDirectories = options.LocalSymbolDirectories ?? context.LocalSymbolDirectories; context.TracePdbLoads = options.Trace.Contains(nameof(Traces.PdbLoad)); @@ -172,6 +173,11 @@ public override int Run(AnalyzeOptions analyzeOptions) { Stopwatch stopwatch = null; + if (analyzeOptions.DisableTelemetry == true) + { + this.Telemetry = null; + } + if (analyzeOptions.Trace.Where(s => s == nameof(DefaultTraces.ScanTime)).Any()) { stopwatch = Stopwatch.StartNew(); @@ -243,6 +249,6 @@ public override int Run(AnalyzeOptions analyzeOptions) internal Sarif.SarifVersion UnitTestOutputVersion { get; set; } - private Sdk.Telemetry Telemetry { get; } + private Sdk.Telemetry Telemetry { get; set; } } } diff --git a/src/BinSkim.Sdk/BinSkim.Sdk.csproj b/src/BinSkim.Sdk/BinSkim.Sdk.csproj index b593b9f3..e6e7f07f 100644 --- a/src/BinSkim.Sdk/BinSkim.Sdk.csproj +++ b/src/BinSkim.Sdk/BinSkim.Sdk.csproj @@ -20,6 +20,11 @@ + + True + True + EnvironmentResources.resx + True True @@ -28,6 +33,10 @@ + + ResXFileCodeGenerator + EnvironmentResources.Designer.cs + ResXFileCodeGenerator SdkResources.Designer.cs diff --git a/src/BinSkim.Sdk/BinaryAnalyzerContext.cs b/src/BinSkim.Sdk/BinaryAnalyzerContext.cs index 027af485..8a8daaf2 100644 --- a/src/BinSkim.Sdk/BinaryAnalyzerContext.cs +++ b/src/BinSkim.Sdk/BinaryAnalyzerContext.cs @@ -82,6 +82,12 @@ public bool IgnorePdbLoadError set => this.Policy.SetProperty(BinaryParsersProperties.IgnorePdbLoadError, value); } + public bool DisableTelemetry + { + get => this.Policy?.GetProperty(BinaryParsersProperties.DisableTelemetry) == true; + set => this.Policy.SetProperty(BinaryParsersProperties.DisableTelemetry, value); + } + public bool IncludeWixBinaries { get => this.Policy?.GetProperty(BinaryParsersProperties.IncludeWixBinaries) == true; diff --git a/src/BinSkim.Sdk/EnvironmentResources.Designer.cs b/src/BinSkim.Sdk/EnvironmentResources.Designer.cs new file mode 100644 index 00000000..cde750f6 --- /dev/null +++ b/src/BinSkim.Sdk/EnvironmentResources.Designer.cs @@ -0,0 +1,72 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by a tool. +// Runtime Version:4.0.30319.42000 +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + +namespace Microsoft.CodeAnalysis.IL.Sdk { + using System; + + + /// + /// A strongly-typed resource class, for looking up localized strings, etc. + /// + // This class was auto-generated by the StronglyTypedResourceBuilder + // class via a tool like ResGen or Visual Studio. + // To add or remove a member, edit your .ResX file then rerun ResGen + // with the /str option, or rebuild your VS project. + [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")] + [global::System.Diagnostics.DebuggerNonUserCodeAttribute()] + [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] + internal class EnvironmentResources { + + private static global::System.Resources.ResourceManager resourceMan; + + private static global::System.Globalization.CultureInfo resourceCulture; + + [global::System.Diagnostics.CodeAnalysis.SuppressMessageAttribute("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode")] + internal EnvironmentResources() { + } + + /// + /// Returns the cached ResourceManager instance used by this class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Resources.ResourceManager ResourceManager { + get { + if (object.ReferenceEquals(resourceMan, null)) { + global::System.Resources.ResourceManager temp = new global::System.Resources.ResourceManager("Microsoft.CodeAnalysis.IL.Sdk.EnvironmentResources", typeof(EnvironmentResources).Assembly); + resourceMan = temp; + } + return resourceMan; + } + } + + /// + /// Overrides the current thread's CurrentUICulture property for all + /// resource lookups using this strongly typed resource class. + /// + [global::System.ComponentModel.EditorBrowsableAttribute(global::System.ComponentModel.EditorBrowsableState.Advanced)] + internal static global::System.Globalization.CultureInfo Culture { + get { + return resourceCulture; + } + set { + resourceCulture = value; + } + } + + /// + /// Looks up a localized string similar to . + /// + internal static string DefaultTelemetryConnectionString { + get { + return ResourceManager.GetString("DefaultTelemetryConnectionString", resourceCulture); + } + } + } +} diff --git a/src/BinSkim.Sdk/EnvironmentResources.resx b/src/BinSkim.Sdk/EnvironmentResources.resx new file mode 100644 index 00000000..3f29582a --- /dev/null +++ b/src/BinSkim.Sdk/EnvironmentResources.resx @@ -0,0 +1,123 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + + + \ No newline at end of file diff --git a/src/BinSkim.Sdk/Telemetry.cs b/src/BinSkim.Sdk/Telemetry.cs index 82e487a9..cdbd0ba2 100644 --- a/src/BinSkim.Sdk/Telemetry.cs +++ b/src/BinSkim.Sdk/Telemetry.cs @@ -124,6 +124,13 @@ private static void ConfigureTelemetryContext(TelemetryContext context) return "InstrumentationKey=" + appInsightsKey; } + // Fall back to DefaultTelemetryConnectionString. + string defaultTelemetryConnectionString = EnvironmentResources.DefaultTelemetryConnectionString; + if (!string.IsNullOrWhiteSpace(defaultTelemetryConnectionString)) + { + return defaultTelemetryConnectionString; + } + return null; } diff --git a/src/BinaryParsers/BinaryParsersProperties.cs b/src/BinaryParsers/BinaryParsersProperties.cs index 6afd2b43..e993b9a5 100644 --- a/src/BinaryParsers/BinaryParsersProperties.cs +++ b/src/BinaryParsers/BinaryParsersProperties.cs @@ -34,6 +34,11 @@ public IEnumerable GetOptions() "BinaryParsers", nameof(IgnorePdbLoadError), defaultValue: () => false, "Set this value to 'true' to don't break if we have a 'PdbLoadingException'."); + public static PerLanguageOption DisableTelemetry { get; } = + new PerLanguageOption( + "BinaryParsers", nameof(DisableTelemetry), defaultValue: () => false, + "Set this value to 'true' to disable telemetry."); + public static PerLanguageOption IncludeWixBinaries { get; } = new PerLanguageOption( "BinaryParsers", nameof(IncludeWixBinaries), defaultValue: () => false,