Skip to content

Commit c6c54df

Browse files
authored
Add interface pin (not yet IConfiguration2) (#919)
1 parent cb45054 commit c6c54df

File tree

7 files changed

+444
-35
lines changed

7 files changed

+444
-35
lines changed

src/Microsoft.Sbom.Api/Config/ConfigSanitizer.cs

+24-20
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,32 @@ public IConfiguration SanitizeConfig(IConfiguration configuration)
7878
// Set default package supplier if not provided in configuration.
7979
configuration.PackageSupplier = GetPackageSupplierFromAssembly(configuration, logger);
8080

81-
// Prevent null value for LicenseInformationTimeoutInSeconds.
82-
// Values of (0, Constants.MaxLicenseFetchTimeoutInSeconds] are allowed. Negative values are replaced with the default, and
83-
// the higher values are truncated to the maximum of Common.Constants.MaxLicenseFetchTimeoutInSeconds
84-
if (configuration.LicenseInformationTimeoutInSeconds is null)
81+
var configuration2 = configuration as IConfiguration2;
82+
if (configuration2 is not null)
8583
{
86-
configuration.LicenseInformationTimeoutInSeconds = new(Common.Constants.DefaultLicenseFetchTimeoutInSeconds, SettingSource.Default);
87-
}
88-
else if (configuration.LicenseInformationTimeoutInSeconds.Value <= 0)
89-
{
90-
logger.Warning($"Negative and Zero Values not allowed for timeout. Using the default {Common.Constants.DefaultLicenseFetchTimeoutInSeconds} seconds instead.");
91-
configuration.LicenseInformationTimeoutInSeconds.Value = Common.Constants.DefaultLicenseFetchTimeoutInSeconds;
92-
}
93-
else if (configuration.LicenseInformationTimeoutInSeconds.Value > Common.Constants.MaxLicenseFetchTimeoutInSeconds)
94-
{
95-
logger.Warning($"Specified timeout exceeds maximum allowed. Truncating the timeout to {Common.Constants.MaxLicenseFetchTimeoutInSeconds} seconds.");
96-
configuration.LicenseInformationTimeoutInSeconds.Value = Common.Constants.MaxLicenseFetchTimeoutInSeconds;
97-
}
84+
// Prevent null value for LicenseInformationTimeoutInSeconds.
85+
// Values of (0, Constants.MaxLicenseFetchTimeoutInSeconds] are allowed. Negative values are replaced with the default, and
86+
// the higher values are truncated to the maximum of Common.Constants.MaxLicenseFetchTimeoutInSeconds
87+
if (configuration2.LicenseInformationTimeoutInSeconds is null)
88+
{
89+
configuration2.LicenseInformationTimeoutInSeconds = new(Common.Constants.DefaultLicenseFetchTimeoutInSeconds, SettingSource.Default);
90+
}
91+
else if (configuration2.LicenseInformationTimeoutInSeconds.Value <= 0)
92+
{
93+
logger.Warning($"Negative and Zero Values not allowed for timeout. Using the default {Common.Constants.DefaultLicenseFetchTimeoutInSeconds} seconds instead.");
94+
configuration2.LicenseInformationTimeoutInSeconds.Value = Common.Constants.DefaultLicenseFetchTimeoutInSeconds;
95+
}
96+
else if (configuration2.LicenseInformationTimeoutInSeconds.Value > Common.Constants.MaxLicenseFetchTimeoutInSeconds)
97+
{
98+
logger.Warning($"Specified timeout exceeds maximum allowed. Truncating the timeout to {Common.Constants.MaxLicenseFetchTimeoutInSeconds} seconds.");
99+
configuration2.LicenseInformationTimeoutInSeconds.Value = Common.Constants.MaxLicenseFetchTimeoutInSeconds;
100+
}
98101

99-
// Check if arg -lto is specified but -li is not
100-
if (configuration.FetchLicenseInformation?.Value != true && !configuration.LicenseInformationTimeoutInSeconds.IsDefaultSource)
101-
{
102-
logger.Warning("A license fetching timeout is specified (argument -lto), but this has no effect when FetchLicenseInfo is unspecified or false (argument -li)");
102+
// Check if arg -lto is specified but -li is not
103+
if (configuration.FetchLicenseInformation?.Value != true && !configuration2.LicenseInformationTimeoutInSeconds.IsDefaultSource)
104+
{
105+
logger.Warning("A license fetching timeout is specified (argument -lto), but this has no effect when FetchLicenseInfo is unspecified or false (argument -li)");
106+
}
103107
}
104108

105109
// Replace backslashes in directory paths with the OS-sepcific directory separator character.

src/Microsoft.Sbom.Api/Executors/ComponentDetectionBaseWalker.cs

+16-3
Original file line numberDiff line numberDiff line change
@@ -139,18 +139,20 @@ async Task Scan(string path)
139139

140140
List<string> apiResponses;
141141
var licenseInformationFetcher2 = licenseInformationFetcher as ILicenseInformationFetcher2;
142-
if (licenseInformationFetcher2 is null && (bool)!configuration.LicenseInformationTimeoutInSeconds?.IsDefaultSource)
142+
var licenseInformationTimeoutInSecondsConfigSetting = GetLicenseInformationTimeoutInSecondsSetting(configuration);
143+
144+
if (licenseInformationFetcher2 is null && (bool)!licenseInformationTimeoutInSecondsConfigSetting?.IsDefaultSource)
143145
{
144146
log.Warning("Timeout value is specified, but ILicenseInformationFetcher2 is not implemented for the licenseInformationFetcher");
145147
}
146148

147-
if (licenseInformationFetcher2 is null || configuration.LicenseInformationTimeoutInSeconds is null)
149+
if (licenseInformationFetcher2 is null || licenseInformationTimeoutInSecondsConfigSetting is null)
148150
{
149151
apiResponses = await licenseInformationFetcher.FetchLicenseInformationAsync(listOfComponentsForApi);
150152
}
151153
else
152154
{
153-
apiResponses = await licenseInformationFetcher2.FetchLicenseInformationAsync(listOfComponentsForApi, configuration.LicenseInformationTimeoutInSeconds.Value);
155+
apiResponses = await licenseInformationFetcher2.FetchLicenseInformationAsync(listOfComponentsForApi, licenseInformationTimeoutInSecondsConfigSetting.Value);
154156
}
155157

156158
foreach (var response in apiResponses)
@@ -225,4 +227,15 @@ async Task Scan(string path)
225227
}
226228

227229
protected abstract IEnumerable<ScannedComponent> FilterScannedComponents(ScanResult result);
230+
231+
private ConfigurationSetting<int>? GetLicenseInformationTimeoutInSecondsSetting(IConfiguration configuration)
232+
{
233+
var configuration2 = configuration as IConfiguration2;
234+
if (configuration2 is not null)
235+
{
236+
return configuration2.LicenseInformationTimeoutInSeconds;
237+
}
238+
239+
return null;
240+
}
228241
}

src/Microsoft.Sbom.Common/Config/Configuration.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ namespace Microsoft.Sbom.Common.Config;
1616

1717
[SuppressMessage("StyleCop.CSharp.NamingRules", "SA1311:Static readonly fields should begin with upper-case letter", Justification = "Private fields with the same name as public properties.")]
1818
[SuppressMessage("Naming", "CA1724:Type names should not match namespaces", Justification = "This is the configuration class")]
19-
public class Configuration : IConfiguration
19+
public class Configuration : IConfiguration2
2020
{
2121
private static readonly AsyncLocal<ConfigurationSetting<string>> buildDropPath = new();
2222
private static readonly AsyncLocal<ConfigurationSetting<string>> buildComponentPath = new();
@@ -47,7 +47,7 @@ public class Configuration : IConfiguration
4747
private static readonly AsyncLocal<ConfigurationSetting<string>> generationTimestamp = new();
4848
private static readonly AsyncLocal<ConfigurationSetting<bool>> followSymlinks = new();
4949
private static readonly AsyncLocal<ConfigurationSetting<bool>> fetchLicenseInformation = new();
50-
private static readonly AsyncLocal<ConfigurationSetting<int>> licenseInformationTimeout = new();
50+
private static readonly AsyncLocal<ConfigurationSetting<int>> licenseInformationTimeout = new(); // IConfiguration2
5151
private static readonly AsyncLocal<ConfigurationSetting<bool>> enablePackageMetadataParsing = new();
5252
private static readonly AsyncLocal<ConfigurationSetting<bool>> deleteManifestDirIfPresent = new();
5353
private static readonly AsyncLocal<ConfigurationSetting<bool>> failIfNoPackages = new();
@@ -310,7 +310,7 @@ public ConfigurationSetting<bool> FetchLicenseInformation
310310
set => fetchLicenseInformation.Value = value;
311311
}
312312

313-
/// <inheritdoc cref="IConfiguration.LicenseInformationTimeoutInSeconds" />
313+
/// <inheritdoc cref="IConfiguration2.LicenseInformationTimeoutInSeconds" />
314314
[DefaultValue(Constants.DefaultLicenseFetchTimeoutInSeconds)]
315315
public ConfigurationSetting<int> LicenseInformationTimeoutInSeconds
316316
{

src/Microsoft.Sbom.Common/Config/IConfiguration.cs

-7
Original file line numberDiff line numberDiff line change
@@ -193,13 +193,6 @@ public interface IConfiguration
193193
/// </summary>
194194
ConfigurationSetting<bool> FetchLicenseInformation { get; set; }
195195

196-
/// <summary>
197-
/// Specifies the timeout in seconds for fetching the license information. Defaults to <see cref="Constants.DefaultLicenseFetchTimeoutInSeconds"/>.
198-
/// Has no effect if FetchLicenseInformation (li) argument is false or not provided. Negative values are set to the default and values exceeding the
199-
/// maximum are truncated to <see cref="Constants.MaxLicenseFetchTimeoutInSeconds"/>
200-
/// </summary>
201-
ConfigurationSetting<int> LicenseInformationTimeoutInSeconds { get; set; }
202-
203196
/// <summary>
204197
/// If set to true, we will attempt to locate and parse package metadata files for additional information to include in the SBOM such as .nuspec/.pom files in the local package cache.
205198
/// </summary>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft. All rights reserved.
2+
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
3+
4+
namespace Microsoft.Sbom.Common.Config;
5+
6+
/// <summary>
7+
/// This holds the configuration for the ManifestTool. The values in this
8+
/// file are populated from the command line or config file. Some values
9+
/// are set by default.
10+
/// </summary>
11+
public interface IConfiguration2 : IConfiguration
12+
{
13+
/// <summary>
14+
/// Specifies the timeout in seconds for fetching the license information. Defaults to <see cref="Constants.DefaultLicenseFetchTimeoutInSeconds"/>.
15+
/// Has no effect if FetchLicenseInformation (li) argument is false or not provided. Negative values are set to the default and values exceeding the
16+
/// maximum are truncated to <see cref="Constants.MaxLicenseFetchTimeoutInSeconds"/>
17+
/// </summary>
18+
ConfigurationSetting<int> LicenseInformationTimeoutInSeconds { get; set; }
19+
}

src/Microsoft.Sbom.Common/Config/InputConfiguration.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
namespace Microsoft.Sbom.Common.Config;
1414

1515
[System.Diagnostics.CodeAnalysis.SuppressMessage("StyleCop.CSharp.NamingRules", "SA1311:Static readonly fields should begin with upper-case letter", Justification = "Private fields with the same name as public properties.")]
16-
public class InputConfiguration : IConfiguration
16+
public class InputConfiguration : IConfiguration2
1717
{
1818
/// <inheritdoc cref="IConfiguration.BuildDropPath" />
1919
[DirectoryExists]
@@ -141,7 +141,7 @@ public class InputConfiguration : IConfiguration
141141
[DefaultValue(false)]
142142
public ConfigurationSetting<bool> FetchLicenseInformation { get; set; }
143143

144-
/// <inheritdoc cref="IConfiguration.LicenseInformationTimeoutInSeconds" />
144+
/// <inheritdoc cref="IConfiguration2.LicenseInformationTimeoutInSeconds" />
145145
[DefaultValue(Constants.DefaultLicenseFetchTimeoutInSeconds)]
146146
public ConfigurationSetting<int> LicenseInformationTimeoutInSeconds { get; set; }
147147

0 commit comments

Comments
 (0)