Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DotNet 3 #176

Merged
merged 18 commits into from
Jun 2, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@
public class NugetInspectorArguments {
private static final Logger logger = LoggerFactory.getLogger(NugetInspectorArguments.class);

//At the time of righting, both inspectors (exe and dotnet) use the same arguments so a shared static method is provided.
//At the time of writing, both inspectors (exe and dotnet) use the same arguments so a shared static method is provided.
//If they diverge the options object protects the argument conversion so each inspector can convert as they see fit.
public static List<String> fromInspectorOptions(final NugetInspectorOptions nugetInspectorOptions, final File sourcePath, final File outputDirectory) throws IOException {
final List<String> options = new ArrayList<>(Arrays.asList(
public static List<String> fromInspectorOptions(NugetInspectorOptions nugetInspectorOptions, File sourcePath, File outputDirectory) throws IOException {
List<String> options = new ArrayList<>(Arrays.asList(
"--target_path=" + sourcePath.getCanonicalPath(),
"--output_directory=" + outputDirectory.getCanonicalPath(),
"--ignore_failure=" + nugetInspectorOptions.isIgnoreFailures()));
Expand All @@ -50,9 +50,9 @@ public static List<String> fromInspectorOptions(final NugetInspectorOptions nuge
nugetInspectorOptions.getIncludedModules()
.ifPresent(arg -> options.add("--included_modules=" + arg));

final List<String> nugetPackagesRepo = nugetInspectorOptions.getPackagesRepoUrl();
List<String> nugetPackagesRepo = nugetInspectorOptions.getPackagesRepoUrl();
if (nugetPackagesRepo != null && nugetPackagesRepo.size() > 0) {
final String packagesRepos = String.join(",", nugetPackagesRepo);
String packagesRepos = String.join(",", nugetPackagesRepo);
options.add("--packages_repo_url=" + packagesRepos);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.NugetInspectorLocator;
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.NugetLocatorOptions;
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.OnlineNugetInspectorLocator;
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.runtime.DotNetRuntimeFinder;
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.runtime.DotNetRuntimeManager;
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.runtime.DotNetRuntimeParser;
import com.synopsys.integration.detect.tool.signaturescanner.BlackDuckSignatureScanner;
import com.synopsys.integration.detect.tool.signaturescanner.BlackDuckSignatureScannerOptions;
import com.synopsys.integration.detect.workflow.ArtifactResolver;
Expand Down Expand Up @@ -139,7 +142,7 @@ public AirGapPathFinder airGapPathFinder() {

@Bean
public CodeLocationNameGenerator codeLocationNameService() {
final String codeLocationNameOverride = detectConfiguration.getValueOrEmpty(DetectProperties.Companion.getDETECT_CODE_LOCATION_NAME()).orElse(null);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not in love with removing all the finals, but since we don't have an official rule on it, it's fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My save actions are configured based on the Engineer Onboarding on Confluence, but if you guys have a different preference in Detect, I can update the settings for the project accordingly. I assumed the finals were just left over from when we still had that convention mandated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you could add them back for now that would be great.

String codeLocationNameOverride = detectConfiguration.getValueOrEmpty(DetectProperties.Companion.getDETECT_CODE_LOCATION_NAME()).orElse(null);
return new CodeLocationNameGenerator(codeLocationNameOverride);
}

Expand All @@ -155,7 +158,7 @@ public BdioCodeLocationCreator detectCodeLocationManager() {

@Bean
public AirGapInspectorPaths airGapManager() {
final AirGapOptions airGapOptions = detectConfigurationFactory.createAirGapOptions();
AirGapOptions airGapOptions = detectConfigurationFactory.createAirGapOptions();
return new AirGapInspectorPaths(airGapPathFinder(), airGapOptions);
}

Expand Down Expand Up @@ -197,28 +200,32 @@ public DetectExecutableResolver detectExecutableResolver() {
//#region Detectables
@Bean
public DockerInspectorResolver dockerInspectorResolver() {
final DockerInspectorInstaller dockerInspectorInstaller = new DockerInspectorInstaller(artifactResolver());
DockerInspectorInstaller dockerInspectorInstaller = new DockerInspectorInstaller(artifactResolver());
return new ArtifactoryDockerInspectorResolver(directoryManager, airGapManager(), fullFileFinder(), dockerInspectorInstaller, detectableOptionFactory.createDockerDetectableOptions());
}

@Bean()
public GradleInspectorResolver gradleInspectorResolver() {
final GradleInspectorInstaller gradleInspectorInstaller = new GradleInspectorInstaller(artifactResolver());
GradleInspectorInstaller gradleInspectorInstaller = new GradleInspectorInstaller(artifactResolver());
return new ArtifactoryGradleInspectorResolver(gradleInspectorInstaller, configuration, detectableOptionFactory.createGradleInspectorOptions().getGradleInspectorScriptOptions(), airGapManager(), directoryManager);
}

@Bean()
public NugetInspectorResolver nugetInspectorResolver() {
final NugetLocatorOptions installerOptions = detectableOptionFactory.createNugetInstallerOptions();
final NugetInspectorLocator locator;
final Optional<File> nugetAirGapPath = airGapManager().getNugetInspectorAirGapFile();
NugetLocatorOptions installerOptions = detectableOptionFactory.createNugetInstallerOptions();
NugetInspectorLocator locator;
Optional<File> nugetAirGapPath = airGapManager().getNugetInspectorAirGapFile();
if (nugetAirGapPath.isPresent()) {
locator = new AirgapNugetInspectorLocator(airGapManager());
} else {
final NugetInspectorInstaller installer = new NugetInspectorInstaller(artifactResolver());
NugetInspectorInstaller installer = new NugetInspectorInstaller(artifactResolver());
locator = new OnlineNugetInspectorLocator(installer, directoryManager, installerOptions.getNugetInspectorVersion().orElse(null));
}
return new LocatorNugetInspectorResolver(detectExecutableResolver(), executableRunner(), detectInfo, fullFileFinder(), installerOptions.getNugetInspectorName(), installerOptions.getPackagesRepoUrl(), locator);

ExecutableRunner executableRunner = executableRunner();
DotNetRuntimeFinder runtimeFinder = new DotNetRuntimeFinder(executableRunner, new File("."));
DotNetRuntimeManager dotNetRuntimeManager = new DotNetRuntimeManager(runtimeFinder, new DotNetRuntimeParser());
return new LocatorNugetInspectorResolver(detectExecutableResolver(), executableRunner, detectInfo, fullFileFinder(), installerOptions.getNugetInspectorName(), installerOptions.getPackagesRepoUrl(), locator, dotNetRuntimeManager);
}

@Bean()
Expand All @@ -245,7 +252,7 @@ public DetectDetectableFactory detectDetectableFactory() {

@Lazy
@Bean()
public BlackDuckSignatureScanner blackDuckSignatureScanner(final BlackDuckSignatureScannerOptions blackDuckSignatureScannerOptions, final ScanBatchRunner scanBatchRunner, final BlackDuckServerConfig blackDuckServerConfig) {
public BlackDuckSignatureScanner blackDuckSignatureScanner(BlackDuckSignatureScannerOptions blackDuckSignatureScannerOptions, ScanBatchRunner scanBatchRunner, BlackDuckServerConfig blackDuckServerConfig) {
return new BlackDuckSignatureScanner(directoryManager, fullFileFinder(), codeLocationNameManager(), blackDuckSignatureScannerOptions, eventSystem, scanBatchRunner, blackDuckServerConfig);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import com.synopsys.integration.rest.proxy.ProxyInfo;

public class ConnectionDetails {
private final ProxyInfo proxyInformation; // Not nuzll because of NO_PROXY_INFO value.
private final ProxyInfo proxyInformation; // Not null because of NO_PROXY_INFO value.
private final List<Pattern> ignoredProxyHostPatterns;
private final Long timeout;
private final Boolean alwaysTrust;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,26 +23,39 @@
package com.synopsys.integration.detect.tool.detector.inspectors.nuget;

import java.io.File;
import java.util.Optional;

import com.synopsys.integration.detect.workflow.airgap.AirGapInspectorPaths;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;

public class AirgapNugetInspectorLocator implements NugetInspectorLocator {
public static final String INSPECTOR_DIR_DOTNET3 = "nuget_dotnet3";
public static final String INSPECTOR_DIR_DOTNET = "nuget_dotnet";
public static final String INSPECTOR_DIR_CLASSIC = "nuget_classic";

private final AirGapInspectorPaths airGapInspectorPaths;

public AirgapNugetInspectorLocator(final AirGapInspectorPaths airGapInspectorPaths) {
public AirgapNugetInspectorLocator(AirGapInspectorPaths airGapInspectorPaths) {
this.airGapInspectorPaths = airGapInspectorPaths;
}

@Override
public File locateExeInspector() {
final Optional<File> nugetAirGapPath = airGapInspectorPaths.getNugetInspectorAirGapFile();
return new File(nugetAirGapPath.get(), "nuget_classic"); // TODO: Why is there no ifPresent() check?
public File locateDotnet3Inspector() throws DetectableException {
return locateInspector(INSPECTOR_DIR_DOTNET3);
}

@Override
public File locateDotnetInspector() throws DetectableException {
return locateInspector(INSPECTOR_DIR_DOTNET);
}

@Override
public File locateDotnetInspector() {
final Optional<File> nugetAirGapPath = airGapInspectorPaths.getNugetInspectorAirGapFile();
return new File(nugetAirGapPath.get(), "nuget_dotnet"); // TODO: Why is there no ifPresent() check?
public File locateExeInspector() throws DetectableException {
return locateInspector(INSPECTOR_DIR_CLASSIC);
}

private File locateInspector(String childName) throws DetectableException {
return airGapInspectorPaths.getNugetInspectorAirGapFile()
.map(nugetAirGapPath -> new File(nugetAirGapPath, childName))
.orElseThrow(() -> new DetectableException("Could not get the nuget air gap path"));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import java.io.File;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.synopsys.integration.detect.DetectInfo;
import com.synopsys.integration.detect.tool.detector.impl.DetectExecutableResolver;
import com.synopsys.integration.detect.tool.detector.inspectors.nuget.runtime.DotNetRuntimeManager;
import com.synopsys.integration.detect.type.OperatingSystemType;
import com.synopsys.integration.detectable.detectable.exception.DetectableException;
import com.synopsys.integration.detectable.detectable.executable.ExecutableRunner;
Expand All @@ -50,20 +52,23 @@ public class LocatorNugetInspectorResolver implements NugetInspectorResolver {
private final FileFinder fileFinder;
private final String nugetInspectorName;
private final List<String> packagesRepoUrl;
private final NugetInspectorLocator nugetInspectorLocator;
private final DotNetRuntimeManager dotNetRuntimeManager;

private boolean hasResolvedInspector;
private NugetInspector resolvedNugetInspector;
private final NugetInspectorLocator nugetInspectorLocator;

public LocatorNugetInspectorResolver(final DetectExecutableResolver executableResolver, final ExecutableRunner executableRunner, final DetectInfo detectInfo,
final FileFinder fileFinder, final String nugetInspectorName, final List<String> packagesRepoUrl, final NugetInspectorLocator nugetInspectorLocator) {
public LocatorNugetInspectorResolver(DetectExecutableResolver executableResolver, ExecutableRunner executableRunner, DetectInfo detectInfo,
FileFinder fileFinder, String nugetInspectorName, List<String> packagesRepoUrl, NugetInspectorLocator nugetInspectorLocator,
DotNetRuntimeManager dotNetRuntimeManager) {
this.executableResolver = executableResolver;
this.executableRunner = executableRunner;
this.detectInfo = detectInfo;
this.fileFinder = fileFinder;
this.nugetInspectorName = nugetInspectorName;
this.packagesRepoUrl = packagesRepoUrl;
this.nugetInspectorLocator = nugetInspectorLocator;
this.dotNetRuntimeManager = dotNetRuntimeManager;
}

@Override
Expand All @@ -75,14 +80,14 @@ public NugetInspector resolveNugetInspector() throws DetectableException {
}

return resolvedNugetInspector;
} catch (final Exception e) {
} catch (Exception e) {
throw new DetectableException(e);
}
}

private NugetInspector install() throws IntegrationException {
//dotnet
final File dotnetExecutable = executableResolver.resolveDotNet();
File dotnetExecutable = executableResolver.resolveDotNet();

boolean useDotnet = true;
if (shouldForceExeInspector(detectInfo)) {
Expand All @@ -97,61 +102,62 @@ private NugetInspector install() throws IntegrationException {
}

if (useDotnet) {
final File dotnetFolder = nugetInspectorLocator.locateDotnetInspector();
return findDotnetCoreInspector(dotnetFolder, dotnetExecutable);
File dotnetFolder;
if (dotNetRuntimeManager.isRuntimeAvailable(3, 1)) {
dotnetFolder = nugetInspectorLocator.locateDotnet3Inspector();
return findDotnetCoreInspector(dotnetFolder, dotnetExecutable, "NugetDotnet3Inspector.dll");
} else {
dotnetFolder = nugetInspectorLocator.locateDotnetInspector();
return findDotnetCoreInspector(dotnetFolder, dotnetExecutable, "BlackduckNugetInspector.dll");
}
} else {
final File classicFolder = nugetInspectorLocator.locateExeInspector();
File classicFolder = nugetInspectorLocator.locateExeInspector();
return findExeInspector(classicFolder);
}
}

private DotNetCoreNugetInspector findDotnetCoreInspector(final File nupkgFolder, final File dotnetExecutable) throws DetectableException {
//new inspector
final String dotnetInspectorName = "BlackduckNugetInspector.dll";
logger.debug("Searching for: " + dotnetInspectorName);
final File toolsFolder = new File(nupkgFolder, "tools");
final Optional<File> foundExe = fileFinder.findFiles(toolsFolder, dotnetInspectorName, 3).stream().findFirst();
if (foundExe.isPresent() && foundExe.get().exists()) {
final String inspectorExe = foundExe.get().getAbsolutePath();
logger.debug("Found nuget inspector: " + inspectorExe);
return new DotNetCoreNugetInspector(dotnetExecutable, inspectorExe, executableRunner);
} else {
throw new DetectableException("Unable to find nuget inspector, looking for " + dotnetInspectorName + " in " + toolsFolder.toString());
}
private NugetInspector findDotnetCoreInspector(File nupkgFolder, File dotnetExecutable, String dotnetInspectorName) throws DetectableException {
Function<String, NugetInspector> constructor = (String exePath) -> new DotNetCoreNugetInspector(dotnetExecutable, exePath, executableRunner);
return findInspector(nupkgFolder, dotnetInspectorName, constructor);
}

//original inspector
private NugetInspector findExeInspector(File nupkgFolder) throws DetectableException {
String exeName = nugetInspectorName + ".exe";
Function<String, NugetInspector> constructor = (String exePath) -> new ExeNugetInspector(executableRunner, exePath);
return findInspector(nupkgFolder, exeName, constructor);
}

private ExeNugetInspector findExeInspector(final File nupkgFolder) throws DetectableException {
//original inspector
final String exeName = nugetInspectorName + ".exe";
logger.debug("Searching for: " + exeName);
final File toolsFolder = new File(nupkgFolder, "tools");
private NugetInspector findInspector(File nupkgFolder, String inspectorName, Function<String, NugetInspector> inspectorInitializer) throws DetectableException {
logger.debug("Searching for: " + inspectorName);
File toolsFolder = new File(nupkgFolder, "tools");
logger.debug("Searching in: " + toolsFolder.getAbsolutePath());
final Optional<File> foundExe = fileFinder.findFiles(toolsFolder, exeName, 3).stream().findFirst();
if (foundExe.isPresent() && foundExe.get().exists()) {
final String inspectorExe = foundExe.get().getAbsolutePath();
logger.debug("Found nuget inspector: " + inspectorExe);
return new ExeNugetInspector(executableRunner, inspectorExe);
Optional<File> foundExecutable = fileFinder.findFiles(toolsFolder, inspectorName, 3).stream().findFirst();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Optional<File> foundExecutable = fileFinder.findFiles(toolsFolder, inspectorName, 3).stream().findFirst();
File foundExecutable = fileFinder.findFiles(toolsFolder, inspectorName, 3).stream().
.filter(File::exists)
.findFirst()
.orElseThrow(() -> new DetectableException(String.format("Unable to find nuget inspector, looking for %s in %s", inspectorName, toolsFolder.toString())));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we don't need the if/else if we do the filtering in the Stream.

if (foundExecutable.isPresent() && foundExecutable.get().exists()) {
String inspectorExecutable = foundExecutable.get().getAbsolutePath();
logger.debug("Found nuget inspector: {}", inspectorExecutable);
return inspectorInitializer.apply(inspectorExecutable);
} else {
throw new DetectableException("Unable to find nuget inspector named '" + exeName + "' in " + toolsFolder.getAbsolutePath());
throw new DetectableException(String.format("Unable to find nuget inspector, looking for %s in %s", inspectorName, toolsFolder.toString()));
}
}

private boolean isWindows(final DetectInfo detectInfo) {
private boolean isWindows(DetectInfo detectInfo) {
return detectInfo.getCurrentOs() == OperatingSystemType.WINDOWS;
}

private boolean isNotWindows(final DetectInfo detectInfo) {
private boolean isNotWindows(DetectInfo detectInfo) {
return !isWindows(detectInfo);
}

private boolean shouldForceExeInspector(final DetectInfo detectInfo) {
private boolean shouldForceExeInspector(DetectInfo detectInfo) {
if (isNotWindows(detectInfo)) {
return false;
}

//if customers have overridden the repo url's and include a v2 api, we must use the old nuget inspector (exe inspector) until 5.0.0 of detect.
//TODO: Remove in 7.0.0
for (final String source : packagesRepoUrl) {
for (String source : packagesRepoUrl) {
if (source.contains("v2")) {
logger.warn("You are using Version 2 of the Nuget Api. Please update to version 3. Support for 2 is deprecated.");
return true;
Expand Down