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

fix: OSS Index Analyzer SocketTimeoutException exception handling based on warn only parameter #5845

Merged
merged 2 commits into from
Jul 29, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java.net.SocketTimeoutException;

import javax.annotation.Nullable;
import org.apache.commons.lang3.StringUtils;
import org.sonatype.goodies.packageurl.InvalidException;
Expand Down Expand Up @@ -154,6 +156,14 @@ protected void analyzeDependency(final Dependency dependency, final Engine engin
LOG.debug("Error requesting component reports, disabling the analyzer", ex);
throw new AnalysisException("Failed to request component-reports", ex);
}
} catch (SocketTimeoutException e) {
final boolean warnOnly = getSettings().getBoolean(Settings.KEYS.ANALYZER_OSSINDEX_WARN_ONLY_ON_REMOTE_ERRORS, false);
aikebah marked this conversation as resolved.
Show resolved Hide resolved
if (warnOnly) {
LOG.warn("OSS Index socket timeout, disabling the analyzer", e);
} else {
LOG.debug("OSS Index socket timeout", e);
throw new AnalysisException("Failed to establish socket to OSS Index", e);
}
} catch (Exception e) {
LOG.debug("Error requesting component reports", e);
throw new AnalysisException("Failed to request component-reports", e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
import java.util.concurrent.Executors;
import java.util.concurrent.Future;

import java.net.SocketTimeoutException;

import org.junit.Assert;
import org.junit.Test;
import org.owasp.dependencycheck.BaseTest;
Expand Down Expand Up @@ -150,6 +152,68 @@ public void should_analyzeDependency_only_warn_when_transport_error_from_sonatyp
}
}


@Test
public void should_analyzeDependency_only_warn_when_socket_error_from_sonatype() throws Exception {
// Given
OssIndexAnalyzer analyzer = new OssIndexAnalyzerThrowingSocketTimeout();

getSettings().setBoolean(Settings.KEYS.ANALYZER_OSSINDEX_WARN_ONLY_ON_REMOTE_ERRORS, true);
analyzer.initialize(getSettings());

Identifier identifier = new PurlIdentifier("maven", "test", "test", "1.0",
Confidence.HIGHEST);

Dependency dependency = new Dependency();
dependency.addSoftwareIdentifier(identifier);
Settings settings = getSettings();
Engine engine = new Engine(settings);
engine.setDependencies(Collections.singletonList(dependency));

// When
try {
analyzer.analyzeDependency(dependency, engine);
} catch (AnalysisException e) {
Assert.fail("Analysis exception thrown upon remote error although only a warning should have been logged");
} finally {
analyzer.close();
engine.close();
}
}


@Test
public void should_analyzeDependency_fail_when_socket_error_from_sonatype() throws Exception {
// Given
OssIndexAnalyzer analyzer = new OssIndexAnalyzerThrowingSocketTimeout();

getSettings().setBoolean(Settings.KEYS.ANALYZER_OSSINDEX_WARN_ONLY_ON_REMOTE_ERRORS, false);
analyzer.initialize(getSettings());

Identifier identifier = new PurlIdentifier("maven", "test", "test", "1.0",
Confidence.HIGHEST);

Dependency dependency = new Dependency();
dependency.addSoftwareIdentifier(identifier);
Settings settings = getSettings();
Engine engine = new Engine(settings);
engine.setDependencies(Collections.singletonList(dependency));

// When
AnalysisException output = new AnalysisException();
try {
analyzer.analyzeDependency(dependency, engine);
} catch (AnalysisException e) {
output = e;
}

// Then
assertEquals("Failed to establish socket to OSS Index", output.getMessage());
analyzer.close();
}



static final class OssIndexAnalyzerThrowing403 extends OssIndexAnalyzer {
@Override
OssindexClient newOssIndexClient() {
Expand Down Expand Up @@ -198,5 +262,30 @@ public ComponentReport requestComponentReport(PackageUrl coordinates) throws Exc
public void close() throws Exception {

}
}
}

static final class OssIndexAnalyzerThrowingSocketTimeout extends OssIndexAnalyzer {
@Override
OssindexClient newOssIndexClient() {
return new OssIndexClientSocketTimeoutException();
}
}

private static final class OssIndexClientSocketTimeoutException implements OssindexClient {

@Override
public Map<PackageUrl, ComponentReport> requestComponentReports(List<PackageUrl> coordinates) throws Exception {
throw new SocketTimeoutException("Read timed out");
}

@Override
public ComponentReport requestComponentReport(PackageUrl coordinates) throws Exception {
throw new SocketTimeoutException("Read timed out");
}

@Override
public void close() throws Exception {

}
}
}
Loading