Skip to content

Commit 1dce65e

Browse files
authored
API BREAKING CHANGE: Remove back-compat interface shims (#952)
1 parent c695cca commit 1dce65e

File tree

13 files changed

+44
-136
lines changed

13 files changed

+44
-136
lines changed

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

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

81-
var configuration2 = configuration as IConfiguration2;
82-
if (configuration2 is not null)
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)
8385
{
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-
}
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+
}
10198

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-
}
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)");
107103
}
108104

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

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -144,22 +144,8 @@ async Task Scan(string path)
144144
licenseInformationRetrieved = true;
145145

146146
List<string> apiResponses;
147-
var licenseInformationFetcher2 = licenseInformationFetcher as ILicenseInformationFetcher2;
148-
var licenseInformationTimeoutInSecondsConfigSetting = GetLicenseInformationTimeoutInSecondsSetting(configuration);
149147

150-
if (licenseInformationFetcher2 is null && (bool)!licenseInformationTimeoutInSecondsConfigSetting?.IsDefaultSource)
151-
{
152-
log.Warning("Timeout value is specified, but ILicenseInformationFetcher2 is not implemented for the licenseInformationFetcher");
153-
}
154-
155-
if (licenseInformationFetcher2 is null || licenseInformationTimeoutInSecondsConfigSetting is null)
156-
{
157-
apiResponses = await licenseInformationFetcher.FetchLicenseInformationAsync(listOfComponentsForApi);
158-
}
159-
else
160-
{
161-
apiResponses = await licenseInformationFetcher2.FetchLicenseInformationAsync(listOfComponentsForApi, licenseInformationTimeoutInSecondsConfigSetting.Value);
162-
}
148+
apiResponses = await licenseInformationFetcher.FetchLicenseInformationAsync(listOfComponentsForApi, configuration.LicenseInformationTimeoutInSeconds.Value);
163149

164150
foreach (var response in apiResponses)
165151
{
@@ -233,15 +219,4 @@ async Task Scan(string path)
233219
}
234220

235221
protected abstract IEnumerable<ScannedComponent> FilterScannedComponents(ScanResult result);
236-
237-
private ConfigurationSetting<int>? GetLicenseInformationTimeoutInSecondsSetting(IConfiguration configuration)
238-
{
239-
var configuration2 = configuration as IConfiguration2;
240-
if (configuration2 is not null)
241-
{
242-
return configuration2.LicenseInformationTimeoutInSeconds;
243-
}
244-
245-
return null;
246-
}
247222
}

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,12 @@ public interface ILicenseInformationFetcher
1818
List<string> ConvertComponentsToListForApi(IEnumerable<ScannedComponent> scannedComponents);
1919

2020
/// <summary>
21-
/// Calls the ClearlyDefined API to get the license information for the list of components. Uses a default timeout specified in implementation
21+
/// Calls the ClearlyDefined API to get the license information for the list of components.
2222
/// </summary>
2323
/// <param name="listOfComponentsForApi"> A list of strings formatted into a list of strings that can be used to call the batch ClearlyDefined API.</param>
24+
/// <param name="timeoutInSeconds">Timeout in seconds to use when making web requests. Caller owns sanitizing this value</param>
2425
/// <returns></returns>
25-
Task<List<string>> FetchLicenseInformationAsync(List<string> listOfComponentsForApi);
26+
Task<List<string>> FetchLicenseInformationAsync(List<string> listOfComponentsForApi, int timeoutInSeconds);
2627

2728
/// <summary>
2829
/// Gets the dictionary of licenses that were fetched from the ClearlyDefined API.

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

Lines changed: 0 additions & 18 deletions
This file was deleted.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,5 @@ namespace Microsoft.Sbom.Api.Executors;
88

99
public interface ILicenseInformationService
1010
{
11-
public Task<List<string>> FetchLicenseInformationFromAPI(List<string> listOfComponentsForApi);
11+
public Task<List<string>> FetchLicenseInformationFromAPI(List<string> listOfComponentsForApi, int timeoutInSeconds);
1212
}

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

Lines changed: 0 additions & 12 deletions
This file was deleted.

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

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
namespace Microsoft.Sbom.Api.Executors;
1616

17-
public class LicenseInformationFetcher : ILicenseInformationFetcher2
17+
public class LicenseInformationFetcher : ILicenseInformationFetcher
1818
{
1919
private readonly ILogger log;
2020
private readonly IRecorder recorder;
@@ -82,23 +82,9 @@ public List<string> ConvertComponentsToListForApi(IEnumerable<ScannedComponent>
8282
return listOfComponentsForApi;
8383
}
8484

85-
public async Task<List<string>> FetchLicenseInformationAsync(List<string> listOfComponentsForApi)
86-
{
87-
return await licenseInformationService.FetchLicenseInformationFromAPI(listOfComponentsForApi);
88-
}
89-
9085
public async Task<List<string>> FetchLicenseInformationAsync(List<string> listOfComponentsForApi, int timeoutInSeconds)
9186
{
92-
var licenseInformationService2 = licenseInformationService as ILicenseInformationService2;
93-
if (licenseInformationService2 is null)
94-
{
95-
log.Warning("Timeout is specified in License Fetcher, but licenseInformationService does not implement ILicenseInformationService2");
96-
return await licenseInformationService.FetchLicenseInformationFromAPI(listOfComponentsForApi);
97-
}
98-
else
99-
{
100-
return await licenseInformationService2.FetchLicenseInformationFromAPI(listOfComponentsForApi, timeoutInSeconds);
101-
}
87+
return await licenseInformationService.FetchLicenseInformationFromAPI(listOfComponentsForApi, timeoutInSeconds);
10288
}
10389

10490
// Will attempt to extract license information from a clearlyDefined batch API response. Will always return a dictionary which may be empty depending on the response.

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
namespace Microsoft.Sbom.Api.Executors;
1717

18-
public class LicenseInformationService : ILicenseInformationService2
18+
public class LicenseInformationService : ILicenseInformationService
1919
{
2020
private readonly ILogger log;
2121
private readonly IRecorder recorder;
@@ -28,11 +28,6 @@ public LicenseInformationService(ILogger log, IRecorder recorder, HttpClient htt
2828
this.httpClient = httpClient ?? throw new ArgumentNullException(nameof(httpClient));
2929
}
3030

31-
public async Task<List<string>> FetchLicenseInformationFromAPI(List<string> listOfComponentsForApi)
32-
{
33-
return await FetchLicenseInformationFromAPI(listOfComponentsForApi, Common.Constants.MaxLicenseFetchTimeoutInSeconds);
34-
}
35-
3631
public async Task<List<string>> FetchLicenseInformationFromAPI(List<string> listOfComponentsForApi, int timeoutInSeconds)
3732
{
3833
var batchSize = 500;
@@ -42,10 +37,7 @@ public async Task<List<string>> FetchLicenseInformationFromAPI(List<string> list
4237
var uri = new Uri("https://api.clearlydefined.io/definitions?expand=-files");
4338

4439
httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
45-
if (timeoutInSeconds > 0)
46-
{
47-
httpClient.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
48-
} // The else cases should be sanitized in Config Sanitizer, and even if not, it'll just use httpClient's default timeout
40+
httpClient.Timeout = TimeSpan.FromSeconds(timeoutInSeconds);
4941

5042
for (var i = 0; i < listOfComponentsForApi.Count; i += batchSize)
5143
{

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

Lines changed: 3 additions & 3 deletions
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 : IConfiguration2
19+
public class Configuration : IConfiguration
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 : IConfiguration2
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(); // IConfiguration2
50+
private static readonly AsyncLocal<ConfigurationSetting<int>> licenseInformationTimeout = new();
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();
@@ -311,7 +311,7 @@ public ConfigurationSetting<bool> FetchLicenseInformation
311311
set => fetchLicenseInformation.Value = value;
312312
}
313313

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

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,4 +212,10 @@ public interface IConfiguration
212212
/// The compliance standard to validate against.
213213
/// </summary>
214214
ConfigurationSetting<string> ComplianceStandard { get; set; }
215+
216+
/// Specifies the timeout in seconds for fetching the license information. Defaults to <see cref="Constants.DefaultLicenseFetchTimeoutInSeconds"/>.
217+
/// Has no effect if FetchLicenseInformation (li) argument is false or not provided. Negative values are set to the default and values exceeding the
218+
/// maximum are truncated to <see cref="Constants.MaxLicenseFetchTimeoutInSeconds"/>
219+
/// </summary>
220+
ConfigurationSetting<int> LicenseInformationTimeoutInSeconds { get; set; }
215221
}

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

Lines changed: 0 additions & 19 deletions
This file was deleted.

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

Lines changed: 2 additions & 2 deletions
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 : IConfiguration2
16+
public class InputConfiguration : IConfiguration
1717
{
1818
/// <inheritdoc cref="IConfiguration.BuildDropPath" />
1919
[DirectoryExists]
@@ -141,7 +141,7 @@ public class InputConfiguration : IConfiguration2
141141
[DefaultValue(false)]
142142
public ConfigurationSetting<bool> FetchLicenseInformation { get; set; }
143143

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

test/Microsoft.Sbom.Api.Tests/VersionSpecificPins/Version_4_0/InterfaceConcretionTests.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,14 @@ private class PinnedILicenseInformationFetcher : ILicenseInformationFetcher
7575
public void AppendLicensesToDictionary(Dictionary<string, string> partialLicenseDictionary) => throw new NotImplementedException();
7676
public Dictionary<string, string> ConvertClearlyDefinedApiResponseToList(string httpResponseContent) => throw new NotImplementedException();
7777
public List<string> ConvertComponentsToListForApi(IEnumerable<ScannedComponent> scannedComponents) => throw new NotImplementedException();
78-
public Task<List<string>> FetchLicenseInformationAsync(List<string> listOfComponentsForApi) => throw new NotImplementedException();
78+
public Task<List<string>> FetchLicenseInformationAsync(List<string> listOfComponentsForApi, int timeoutInSeconds) => throw new NotImplementedException();
7979
public string GetFromLicenseDictionary(string key) => throw new NotImplementedException();
8080
public ConcurrentDictionary<string, string> GetLicenseDictionary() => throw new NotImplementedException();
8181
}
8282

83-
private class PinnedILicenstInformationSErvice : ILicenseInformationService
83+
private class PinnedILicenstInformationService : ILicenseInformationService
8484
{
85-
public Task<List<string>> FetchLicenseInformationFromAPI(List<string> listOfComponentsForApi) => throw new NotImplementedException();
85+
public Task<List<string>> FetchLicenseInformationFromAPI(List<string> listOfComponentsForApi, int timeoutInSeconds) => throw new NotImplementedException();
8686
}
8787

8888
private class Pinned_SBOMReaderForExternalDocumentReference : ISbomReaderForExternalDocumentReference
@@ -330,6 +330,7 @@ private class PinnedIConfiguration : IConfiguration
330330
public ConfigurationSetting<string> SbomPath { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
331331
public ConfigurationSetting<string> SbomDir { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
332332
public ConfigurationSetting<string> ComplianceStandard { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
333+
public ConfigurationSetting<int> LicenseInformationTimeoutInSeconds { get => throw new NotImplementedException(); set => throw new NotImplementedException(); }
333334
}
334335

335336
private class PinnedISettingSourceable : ISettingSourceable

0 commit comments

Comments
 (0)