Skip to content

Commit edad8c4

Browse files
Merge pull request #1467 from blackducksoftware/dev/zahidblackduck/IDETECT-4728
Enhance error message for BOM_COMPARE mode failures in rapid scan
2 parents ee17731 + b739e07 commit edad8c4

File tree

2 files changed

+43
-30
lines changed

2 files changed

+43
-30
lines changed

src/main/java/com/blackduck/integration/detect/configuration/enumeration/ExitCodeType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public enum ExitCodeType {
1616
),
1717
FAILURE_BLACKDUCK_FEATURE_ERROR(
1818
11,
19-
"Detect encountered an error while attempting an operation on Black Duck. Ensure that your Black Duck version is compatible with this version of Detect, and that your Black Duck user account has the required roles."
19+
"Detect encountered an error while trying to perform an operation on Black Duck SCA. Ensure that your Black Duck SCA version is compatible with this version of Detect, your Black Duck user account has the required roles, and the project version exists in Black Duck when using BOM_COMPARE mode in a rapid scan."
2020
),
2121
FAILURE_MINIMUM_INTERVAL_NOT_MET(13, "Detect did not wait the minimum required scan interval."),
2222
FAILURE_IAC(

src/main/java/com/blackduck/integration/detect/lifecycle/run/operation/OperationRunner.java

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,10 @@
3232
import com.blackduck.integration.detect.configuration.enumeration.RapidCompareMode;
3333
import com.blackduck.integration.detect.lifecycle.run.step.CommonScanStepRunner;
3434
import com.blackduck.integration.detect.workflow.blackduck.report.ReportData;
35+
import com.blackduck.integration.rest.exception.IntegrationRestException;
3536
import org.apache.commons.codec.digest.DigestUtils;
3637
import org.apache.commons.lang3.StringUtils;
38+
import org.apache.http.HttpStatus;
3739
import org.apache.http.entity.ContentType;
3840
import org.jetbrains.annotations.Nullable;
3941
import org.slf4j.Logger;
@@ -69,7 +71,6 @@
6971
import com.blackduck.integration.componentlocator.beans.Component;
7072
import com.blackduck.integration.detect.configuration.DetectConfigurationFactory;
7173
import com.blackduck.integration.detect.configuration.DetectInfo;
72-
import com.blackduck.integration.detect.configuration.DetectProperties;
7374
import com.blackduck.integration.detect.configuration.DetectUserFriendlyException;
7475
import com.blackduck.integration.detect.configuration.DetectorToolOptions;
7576
import com.blackduck.integration.detect.configuration.connection.ConnectionFactory;
@@ -90,7 +91,6 @@
9091
import com.blackduck.integration.detect.lifecycle.run.step.utility.OperationAuditLog;
9192
import com.blackduck.integration.detect.lifecycle.run.step.utility.OperationWrapper;
9293
import com.blackduck.integration.detect.lifecycle.shutdown.ExitCodePublisher;
93-
import com.blackduck.integration.detect.lifecycle.shutdown.ExitCodeRequest;
9494
import com.blackduck.integration.detect.tool.DetectableTool;
9595
import com.blackduck.integration.detect.tool.DetectableToolResult;
9696
import com.blackduck.integration.detect.tool.binaryscanner.BinaryScanFindMultipleTargetsOperation;
@@ -183,7 +183,6 @@
183183
import com.blackduck.integration.detect.workflow.componentlocationanalysis.BdioToComponentListTransformer;
184184
import com.blackduck.integration.detect.workflow.componentlocationanalysis.GenerateComponentLocationAnalysisOperation;
185185
import com.blackduck.integration.detect.workflow.componentlocationanalysis.ScanResultToComponentListTransformer;
186-
import com.blackduck.integration.detect.workflow.event.Event;
187186
import com.blackduck.integration.detect.workflow.event.EventSystem;
188187
import com.blackduck.integration.detect.workflow.file.DirectoryManager;
189188
import com.blackduck.integration.detect.workflow.phonehome.PhoneHomeManager;
@@ -713,38 +712,55 @@ public List<DeveloperScansScanView> waitForRapidResults(BlackDuckRunData blackDu
713712
return auditLog.namedInternal("Rapid Wait", () -> {
714713
BlackDuckServicesFactory blackDuckServicesFactory = blackDuckRunData.getBlackDuckServicesFactory();
715714
int fibonacciSequenceIndex = getFibonacciSequenceIndex();
716-
return new RapidModeWaitOperation(blackDuckServicesFactory.getBlackDuckApiClient()).waitForScans(
717-
rapidScans,
718-
detectConfigurationFactory.findTimeoutInSeconds(),
719-
RapidModeWaitOperation.DEFAULT_WAIT_INTERVAL_IN_SECONDS,
720-
mode,
721-
calculateMaxWaitInSeconds(fibonacciSequenceIndex)
722-
);
715+
716+
try {
717+
return new RapidModeWaitOperation(blackDuckServicesFactory.getBlackDuckApiClient()).waitForScans(
718+
rapidScans,
719+
detectConfigurationFactory.findTimeoutInSeconds(),
720+
RapidModeWaitOperation.DEFAULT_WAIT_INTERVAL_IN_SECONDS,
721+
mode,
722+
calculateMaxWaitInSeconds(fibonacciSequenceIndex)
723+
);
724+
} catch (IntegrationRestException e) {
725+
throw handleRapidScanException(e);
726+
} catch (Exception e) {
727+
logger.error("Exception while waiting for rapid results: {}", e.getMessage(), e);
728+
throw new OperationException(e);
729+
}
723730
});
724731
}
725732

726-
public static boolean shouldSkipResolvedPolicies(DeveloperScansScanView resultView, RapidCompareMode rapidCompareMode) {
727-
if (resultView.getPolicyStatuses() == null || resultView.getPolicyStatuses().isEmpty()) {
728-
return false;
733+
private OperationException handleRapidScanException(IntegrationRestException e) {
734+
RapidCompareMode rapidCompareMode = detectConfigurationFactory.createRapidScanOptions().getCompareMode();
735+
736+
if (isBomCompareError(e, rapidCompareMode)) {
737+
String enhancedMessage = createBomCompareErrorMessage(e.getMessage());
738+
logger.error("Rapid scan failed. {}", enhancedMessage);
739+
return new OperationException(new IntegrationRestException(
740+
e.getHttpMethod(), e.getHttpUrl(), e.getHttpStatusCode(),
741+
e.getHttpStatusMessage(), e.getHttpResponseContent(), enhancedMessage));
729742
}
730-
return (RapidCompareMode.BOM_COMPARE.equals(rapidCompareMode) || RapidCompareMode.BOM_COMPARE_STRICT.equals(rapidCompareMode))
731-
&& resultView.getPolicyStatuses().stream().allMatch(POLICY_STATUS_RESOLVED::equalsIgnoreCase);
743+
744+
logger.error("Rapid scan failed. {}", e.getMessage());
745+
return new OperationException(e);
732746
}
733747

734-
public static List<DeveloperScansScanView> filterUnresolvedPolicyResults(List<DeveloperScansScanView> scanResults, RapidCompareMode rapidCompareMode) {
735-
return scanResults.stream()
736-
.filter(resultView -> !shouldSkipResolvedPolicies(resultView, rapidCompareMode))
737-
.collect(Collectors.toList());
748+
private boolean isBomCompareError(IntegrationRestException e, RapidCompareMode rapidCompareMode) {
749+
return HttpStatus.SC_BAD_REQUEST == e.getHttpStatusCode() &&
750+
(RapidCompareMode.BOM_COMPARE.equals(rapidCompareMode) ||
751+
RapidCompareMode.BOM_COMPARE_STRICT.equals(rapidCompareMode));
738752
}
739753

740-
public final RapidScanResultSummary logRapidReport(List<DeveloperScansScanView> scanResults, BlackduckScanMode mode) throws OperationException {
741-
RapidScanOptions rapidScanOptions = detectConfigurationFactory.createRapidScanOptions();
742-
List<PolicyRuleSeverityType> severitiesToFailPolicyCheck = rapidScanOptions.getSeveritiesToFailPolicyCheck();
743-
RapidCompareMode rapidCompareMode = rapidScanOptions.getCompareMode();
744-
List<DeveloperScansScanView> filteredResults = filterUnresolvedPolicyResults(scanResults, rapidCompareMode);
754+
private String createBomCompareErrorMessage(String originalMessage) {
755+
return originalMessage + " BOM_COMPARE mode requires the target project version to exist in Black Duck Hub. " +
756+
"Please ensure 'detect.project.version.name' matches an existing project version. " +
757+
"Consider running a full scan first if the version hasn't been uploaded yet.";
758+
}
745759

746-
return auditLog.namedInternal("Print Rapid Mode Results", () ->
747-
new RapidModeLogReportOperation(exitCodePublisher, rapidScanResultAggregator, mode).perform(filteredResults, severitiesToFailPolicyCheck));
760+
public final RapidScanResultSummary logRapidReport(List<DeveloperScansScanView> scanResults, BlackduckScanMode mode) throws OperationException {
761+
List<PolicyRuleSeverityType> severitiesToFailPolicyCheck = detectConfigurationFactory.createRapidScanOptions().getSeveritiesToFailPolicyCheck();
762+
return auditLog.namedInternal("Print Rapid Mode Results", () ->
763+
new RapidModeLogReportOperation(exitCodePublisher, rapidScanResultAggregator, mode).perform(scanResults, severitiesToFailPolicyCheck));
748764
}
749765

750766
public final File generateRapidJsonFile(NameVersion projectNameVersion, List<DeveloperScansScanView> scanResults) throws OperationException {
@@ -776,7 +792,6 @@ private void failComponentLocationAnalysisOperationTask(String reason) throws Op
776792
/**
777793
* Given a BDIO, creates a JSON file called {@value GenerateComponentLocationAnalysisOperation#DETECT_OUTPUT_FILE_NAME} containing
778794
* every detected component's {@link ExternalId} along with its declaration location when applicable.
779-
*
780795
* @param bdio
781796
* @throws OperationException
782797
*/
@@ -808,7 +823,6 @@ public void generateComponentLocationAnalysisIfEnabled(BdioResult bdio) throws O
808823
/**
809824
* Given a Rapid/Stateless Detector Scan result, creates a JSON file called {@value GenerateComponentLocationAnalysisOperation#DETECT_OUTPUT_FILE_NAME} containing
810825
* every reported component's {@link ExternalId} along with its declaration location and upgrade guidance information when applicable.
811-
*
812826
* @param rapidResults
813827
* @param bdio
814828
* @throws OperationException
@@ -859,7 +873,6 @@ private Set<String> getApplicableDetectorTypesAsStrings(Set<DetectorType> applic
859873
/**
860874
* Since component location analysis is not supported for online Intelligent scans in 8.11, an appropriate console
861875
* msg is logged and status=FAILURE is recorded in the status.json file
862-
*
863876
* @throws OperationException
864877
*/
865878
public void attemptToGenerateComponentLocationAnalysisIfEnabled() throws OperationException {

0 commit comments

Comments
 (0)