From e2cd99d40d0017ac83120a2a8cd803c368f67ae2 Mon Sep 17 00:00:00 2001 From: Jeremy Long Date: Thu, 3 Nov 2016 06:41:37 -0400 Subject: [PATCH] modified code for #606 --- .../analyzer/NexusAnalyzer.java | 19 +++++++++++++++++- .../data/nexus/NexusSearch.java | 20 +++---------------- .../data/nexus/NexusSearchTest.java | 3 ++- 3 files changed, 23 insertions(+), 19 deletions(-) diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/NexusAnalyzer.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/NexusAnalyzer.java index 4a89f7278..32f93c1aa 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/NexusAnalyzer.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/analyzer/NexusAnalyzer.java @@ -87,6 +87,7 @@ public class NexusAnalyzer extends AbstractFileTypeAnalyzer { */ private static final String SUPPORTED_EXTENSIONS = "jar"; + private boolean useProxy; /** * The Nexus Search to be set up for this analyzer. */ @@ -144,10 +145,11 @@ public class NexusAnalyzer extends AbstractFileTypeAnalyzer { LOGGER.debug("Initializing Nexus Analyzer"); LOGGER.debug("Nexus Analyzer enabled: {}", isEnabled()); if (isEnabled()) { + useProxy = useProxy(); final String searchUrl = Settings.getString(Settings.KEYS.ANALYZER_NEXUS_URL); LOGGER.debug("Nexus Analyzer URL: {}", searchUrl); try { - searcher = new NexusSearch(new URL(searchUrl)); + searcher = new NexusSearch(new URL(searchUrl), useProxy); if (!searcher.preflightRequest()) { setEnabled(false); throw new InitializationException("There was an issue getting Nexus status. Disabling analyzer."); @@ -263,4 +265,19 @@ public class NexusAnalyzer extends AbstractFileTypeAnalyzer { LOGGER.debug("Could not connect to nexus repository", ioe); } } + + /** + * Determine if a proxy should be used. + * + * @return {@code true} if a proxy should be used + */ + public static boolean useProxy() { + try { + return Settings.getString(Settings.KEYS.PROXY_SERVER) != null + && Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_USES_PROXY); + } catch (InvalidSettingException ise) { + LOGGER.warn("Failed to parse proxy settings.", ise); + return false; + } + } } diff --git a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nexus/NexusSearch.java b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nexus/NexusSearch.java index 3039f0ab3..71c52d466 100644 --- a/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nexus/NexusSearch.java +++ b/dependency-check-core/src/main/java/org/owasp/dependencycheck/data/nexus/NexusSearch.java @@ -60,28 +60,14 @@ public class NexusSearch { * @param rootURL the root URL of the repository on which searches should * execute. full URL's are calculated relative to this URL, so it should end * with a / + * @param useProxy flag indicating if the proxy settings should be used */ - public NexusSearch(URL rootURL) { + public NexusSearch(URL rootURL, boolean useProxy) { this.rootURL = rootURL; - useProxy = useProxy(); + this.useProxy = useProxy; LOGGER.debug("Using proxy: {}", useProxy); } - /** - * Determine if a proxy should be used. - * - * @return {@code true} if a proxy should be used - */ - private boolean useProxy() { - try { - return Settings.getString(Settings.KEYS.PROXY_SERVER) != null - && Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_USES_PROXY); - } catch (InvalidSettingException ise) { - LOGGER.warn("Failed to parse proxy settings.", ise); - return false; - } - } - /** * Searches the configured Nexus repository for the given sha1 hash. If the * artifact is found, a MavenArtifact is populated with the diff --git a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nexus/NexusSearchTest.java b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nexus/NexusSearchTest.java index 7a719ae0d..e565bf524 100644 --- a/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nexus/NexusSearchTest.java +++ b/dependency-check-core/src/test/java/org/owasp/dependencycheck/data/nexus/NexusSearchTest.java @@ -26,6 +26,7 @@ import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.owasp.dependencycheck.BaseTest; +import org.owasp.dependencycheck.analyzer.NexusAnalyzer; import org.owasp.dependencycheck.utils.Settings; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -39,7 +40,7 @@ public class NexusSearchTest extends BaseTest { public void setUp() throws Exception { String nexusUrl = Settings.getString(Settings.KEYS.ANALYZER_NEXUS_URL); LOGGER.debug(nexusUrl); - searcher = new NexusSearch(new URL(nexusUrl)); + searcher = new NexusSearch(new URL(nexusUrl), NexusAnalyzer.useProxy()); Assume.assumeTrue(searcher.preflightRequest()); }