Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
23c5b65
Improve detect.policy.check.fail.on.names improvement: part 1
shantyk May 12, 2025
e5f8f42
Modify checkPolicyBySeverity() so the output remains as is
shantyk Jul 10, 2025
bd38827
Improve detect.policy.check.fail.on.names: part 2
shantyk Jul 10, 2025
01ac56c
Refactor to minimize repeated code
shantyk Jul 10, 2025
f69abb9
clean up separate severity exit status, keep as is
shantyk Jul 10, 2025
762c0d5
Clean up TODO comments
shantyk Jul 11, 2025
8f1b4dd
Refactoring
shantyk Jul 11, 2025
e0c831d
Fix issue with ExitCodeType enum
shantyk Jul 11, 2025
392a02b
Rename parameters for readability
shantyk Jul 11, 2025
617c619
Remove test case attempts in favour of manual test documented on conf…
shantyk Jul 11, 2025
e03eb4e
Address review comments: refactor to return ExitCodeRequest instead o…
shantyk Jul 14, 2025
9b0f7fd
Address review comments around spacing and wildcart imports
shantyk Jul 14, 2025
0a2e091
Update default winning exit code request
shantyk Jul 14, 2025
8969a0b
Replace instances of exitCodePublisher.publishExitCode() that had a r…
shantyk Jul 15, 2025
b72374f
Fix test cases: remove duplicate priority exit code type
shantyk Jul 15, 2025
3c69f44
Update getWinningExitCodeRequest()
shantyk Jul 15, 2025
007bf6a
Merge branch 'master' into dev/shanty/IDETECT-3941_policy_violation_l…
shantyk Jul 15, 2025
c04f25d
Update failing test
shantyk Jul 15, 2025
680056b
Create ExitCodeRequestWithCustomDescription class
shantyk Jul 15, 2025
c8e7ccd
Remove debug comment
shantyk Jul 15, 2025
17594e0
Respect existing behaviour for detect.stateless.policy.check.fail.on.…
shantyk Jul 15, 2025
d915bf0
Add release note
shantyk Jul 15, 2025
58ac3ef
Update release note wording
shantyk Jul 15, 2025
a3cea77
Refactor ExitCodeRequest
shantyk Jul 15, 2025
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
2 changes: 2 additions & 0 deletions documentation/src/main/markdown/currentreleasenotes.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

* A new property, [detect.stateless.policy.check.fail.on.severities](properties/basic-properties.html#ariaid-title34) has been added, which will trigger [detect_product_short] to fail the scan and notify the user if a policy violation matches the configured value. This property overrides the default "Blocker" and "Critical" severity settings that cause [detect_product_short] scans to exit. This property applies to both [Rapid](runningdetect/rapidscan.md) and [Stateless](runningdetect/statelessscan.md) scans. Intelligent persistent scans, (when scan mode is not set to RAPID, STATELESS, or [--detect.blackduck.scan.mode](properties/all-properties.html#ariaid-title5) is explicitly set to INTELLIGENT and scan data is persisted), should continue using the [detect.policy.check.fail.on.severities](properties/basic-properties.html#ariaid-title34), property.

* [detect_product_short] will now print the names of fatal policy violations at the [detect_product_short] status stage if [detect.policy.check.fail.on.names](properties/configuration/project.md#fail-on-policy-names-with-violations) is configured.

* To provide greater control over Cargo dependencies reported in the BOM, a new property, `detect.cargo.dependency.types.excluded` has been added to allow exclusion of specific Cargo dependency types (`DEV`, `BUILD`) from scans. The default behavior (`NONE`) will include all dependency types.

* Node Package Manager (npm) scans now report optional dependencies. The `detect.npm.dependency.types.excluded` property has been extended to exclude optional dependencies if OPTIONAL is specified in the list of arguments.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
import com.blackduck.integration.detect.lifecycle.shutdown.ShutdownDecider;
import com.blackduck.integration.detect.lifecycle.shutdown.ShutdownDecision;
import com.blackduck.integration.detect.lifecycle.shutdown.ShutdownManager;
import com.blackduck.integration.detect.lifecycle.shutdown.ExitCodeRequest;
import com.blackduck.integration.detect.tool.cache.InstalledToolData;
import com.blackduck.integration.detect.tool.cache.InstalledToolManager;
import com.blackduck.integration.detect.workflow.DetectRunId;
Expand Down Expand Up @@ -183,10 +184,10 @@ public void run(ApplicationArguments applicationArguments) {
// system must now know or be able to compute the winning exit
// code. We'll pass this to FormattedOutput.createFormattedOutput
// via Application.createStatusOutputFile.
ExitCodeType exitCodeType = exitCodeManager.getWinningExitCode();
ExitCodeRequest exitCodeRequest = exitCodeManager.getWinningExitCodeRequest();
logger.info("");
detectBootResult.getDirectoryManager()
.ifPresent(directoryManager -> createStatusOutputFile(formattedOutputManager, detectInfo, directoryManager, exitCodeType, autonomousManagerOptional));
.ifPresent(directoryManager -> createStatusOutputFile(formattedOutputManager, detectInfo, directoryManager, exitCodeRequest, autonomousManagerOptional));

//Create installed tool data file.
detectBootResult.getDirectoryManager().ifPresent(directoryManager -> createOrUpdateInstalledToolsFile(installedToolManager, directoryManager.getPermanentDirectory()));
Expand Down Expand Up @@ -253,14 +254,14 @@ private void runApplication(EventSystem eventSystem, ExitCodeManager exitCodeMan
}
}

private void createStatusOutputFile(FormattedOutputManager formattedOutputManager, DetectInfo detectInfo, DirectoryManager directoryManager, ExitCodeType exitCodeType, Optional<AutonomousManager> autonomousManagerOptional) {
private void createStatusOutputFile(FormattedOutputManager formattedOutputManager, DetectInfo detectInfo, DirectoryManager directoryManager, ExitCodeRequest exitCodeRequest, Optional<AutonomousManager> autonomousManagerOptional) {
logger.info("");
try {
File statusFile = new File(directoryManager.getStatusOutputDirectory(), STATUS_JSON_FILE_NAME);
logger.info("Creating status file: {}", statusFile);

Gson formattedGson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
String json = formattedGson.toJson(formattedOutputManager.createFormattedOutput(detectInfo, exitCodeType, autonomousManagerOptional));
String json = formattedGson.toJson(formattedOutputManager.createFormattedOutput(detectInfo, exitCodeRequest, autonomousManagerOptional));
FileUtils.writeStringToFile(statusFile, json, Charset.defaultCharset());

if (directoryManager.getJsonStatusOutputDirectory() != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.Optional;

import com.blackduck.integration.detect.lifecycle.shutdown.ExitCodePublisher;
import org.apache.commons.lang3.time.DurationFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -34,31 +35,33 @@ public ExitResult exit(ExitOptions exitOptions, Optional<AutonomousManager> auto

//Generally, when requesting a failure status, an exit code is also requested, but if it is not, we default to an unknown error.
if (statusManager.hasAnyFailure()) {
eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(ExitCodeType.FAILURE_UNKNOWN_ERROR, "A failure status was requested by one or more of Detect's tools."));
ExitCodePublisher publisher = new ExitCodePublisher(eventSystem);
publisher.publishExitCode(ExitCodeType.FAILURE_UNKNOWN_ERROR);
}

//Find the final (as requested) exit code
ExitCodeType finalExitCode = exitCodeManager.getWinningExitCode();
ExitCodeRequest finalExitCodeRequest = exitCodeManager.getWinningExitCodeRequest();
ExitCodeType finalExitCodeType = finalExitCodeRequest.getExitCodeType();

//Print detect's status
statusManager.logDetectResults(new Slf4jIntLogger(logger), finalExitCode, autonomousManagerOptional);
statusManager.logDetectResults(new Slf4jIntLogger(logger), finalExitCodeRequest, autonomousManagerOptional);

//Print duration of run
long endTime = System.currentTimeMillis();
String duration = DurationFormatUtils.formatPeriod(startTime, endTime, "HH'h' mm'm' ss's' SSS'ms'");
logger.info("Detect duration: {}", duration);

//Exit with formal exit code
if (finalExitCode != ExitCodeType.SUCCESS && forceSuccessExit) {
logger.warn("Forcing success: Exiting with exit code 0. Ignored exit code was {}.", finalExitCode.getExitCode());
} else if (finalExitCode != ExitCodeType.SUCCESS) {
logger.error("Exiting with code {} - {}", finalExitCode.getExitCode(), finalExitCode);
if (finalExitCodeType != ExitCodeType.SUCCESS && forceSuccessExit) {
logger.warn("Forcing success: Exiting with exit code 0. Ignored exit code was {}.", finalExitCodeType.getExitCode());
} else if (finalExitCodeType != ExitCodeType.SUCCESS) {
logger.error("Exiting with code {} - {}", finalExitCodeType.getExitCode(), finalExitCodeType);
}

if (!shouldExit) {
logger.info("Would normally exit({}) but it is overridden.", finalExitCode.getExitCode());
logger.info("Would normally exit({}) but it is overridden.", finalExitCodeType.getExitCode());
}

return new ExitResult(finalExitCode, forceSuccessExit, shouldExit);
return new ExitResult(finalExitCodeType, forceSuccessExit, shouldExit);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@ public void run(BootSingletons bootSingletons) {
logger.debug("Integrated Matching Correlation ID: {}", bootSingletons.getDetectRunId().getCorrelationId());
String correlationId = getCorrelationId(operationRunner.getDetectConfigurationFactory(), bootSingletons);
if (!universalToolsResult.getDetectCodeLocations().isEmpty()
|| (productRunData.shouldUseBlackDuckProduct() && !productRunData.getBlackDuckRunData().isOnline() && forceBdio && !universalToolsResult.didAnyFail() && exitCodeManager.getWinningExitCode().isSuccess())) {
|| (productRunData.shouldUseBlackDuckProduct()
&& !productRunData.getBlackDuckRunData().isOnline()
&& forceBdio && !universalToolsResult.didAnyFail()
&& exitCodeManager.getWinningExitCodeRequest().getExitCodeType().isSuccess())) {
bdio = stepRunner.generateBdio(correlationId, universalToolsResult, nameVersion);
} else {
bdio = BdioResult.none();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1355,25 +1355,25 @@ public Optional<File> collectBinaryTargets(Set<String> targets) throws Operation
public void publishBinaryFailure(String message) {
logger.error("Binary scan failure: {}", message);
statusEventPublisher.publishStatusSummary(Status.forTool(DetectTool.BINARY_SCAN, StatusType.FAILURE));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR, "BINARY_SCAN");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR);
}

public void publishContainerTimeout(Exception e) {
logger.error("Container scan timeout: {}", e.getMessage());
statusEventPublisher.publishStatusSummary(Status.forTool(DetectTool.CONTAINER_SCAN, StatusType.FAILURE));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_TIMEOUT, "CONTAINER_SCAN");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_TIMEOUT);
}

public void publishContainerFailure(Exception e) {
logger.error("Container scan failure: {}", e.getMessage());
statusEventPublisher.publishStatusSummary(Status.forTool(DetectTool.CONTAINER_SCAN, StatusType.FAILURE));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR, "CONTAINER_SCAN");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR);
}

public void publishSignatureFailure(String message) {
logger.error("Signature scan failure: {}", message);
statusEventPublisher.publishStatusSummary(Status.forTool(DetectTool.SIGNATURE_SCAN, StatusType.FAILURE));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR, "SIGNATURE_SCAN");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR);
}

public void publishBinarySuccess() {
Expand All @@ -1387,7 +1387,7 @@ public void publishContainerSuccess() {
public void publishImpactFailure(Exception e) {
logger.error("Impact analysis failure: {}", e.getMessage());
statusEventPublisher.publishStatusSummary(Status.forTool(DetectTool.IMPACT_ANALYSIS, StatusType.FAILURE));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR, "IMPACT_ANALYSIS");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BLACKDUCK_FEATURE_ERROR);
}

public void publishImpactSuccess() {
Expand Down Expand Up @@ -1554,7 +1554,8 @@ public String findLicenseUrl(BlackDuckRunData blackDuckRunData, String licenseNa
}

public void publishDetectorFailure() {
eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(ExitCodeType.FAILURE_DETECTOR, "A detector failed."));
ExitCodePublisher publisher = new ExitCodePublisher(eventSystem);
publisher.publishExitCode(ExitCodeType.FAILURE_DETECTOR);
}

public Optional<File> findRapidScanConfig() throws OperationException {
Expand Down Expand Up @@ -1654,7 +1655,7 @@ private void checkBomStatusAndHandleFailure(BomStatusScanView bomStatusScanView)
if (bomStatusScanView.getStatus() == BomStatusScanStatusType.FAILURE) {
String message = "Black Duck failed to prepare BOM for the scan";
logger.error("BOM Scan Status: {} - {}.", BomStatusScanStatusType.FAILURE, message);
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BOM_PREPARATION, message);
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_BOM_PREPARATION);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.blackduck.integration.detect.configuration.enumeration.ExitCodeType;
import com.blackduck.integration.detect.workflow.event.Event;
import com.blackduck.integration.detect.workflow.event.EventSystem;
import org.jetbrains.annotations.TestOnly;

public class ExitCodeManager {
private final List<ExitCodeRequest> exitCodeRequests = new ArrayList<>();
Expand All @@ -24,15 +25,21 @@ public void requestExitCode(ExitCodeType exitCodeType) {
exitCodeRequests.add(new ExitCodeRequest(exitCodeType));
}

@TestOnly
public void addExitCodeRequest(ExitCodeRequest request) {
exitCodeRequests.add(request);
}

public ExitCodeType getWinningExitCode() {
ExitCodeType winningExitCodeType = ExitCodeType.SUCCESS;
public ExitCodeRequest getWinningExitCodeRequest() {
ExitCodeRequest championExitCodeRequest = new ExitCodeRequest(ExitCodeType.SUCCESS);

for (ExitCodeRequest exitCodeRequest : exitCodeRequests) {
winningExitCodeType = ExitCodeType.getWinningExitCodeType(winningExitCodeType, exitCodeRequest.getExitCodeType());
ExitCodeType thisRoundsWinner = ExitCodeType.getWinningExitCodeType(championExitCodeRequest.getExitCodeType(), exitCodeRequest.getExitCodeType());

if (thisRoundsWinner != championExitCodeRequest.getExitCodeType()) {
championExitCodeRequest = exitCodeRequest;
}
}
return winningExitCodeType;
return championExitCodeRequest;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ public ExitCodePublisher(EventSystem eventSystem) {
this.eventSystem = eventSystem;
}

public void publishExitCode(ExitCodeRequest exitCodeRequest) {
eventSystem.publishEvent(Event.ExitCode, exitCodeRequest);
public void publishExitCode(ExitCodeType exitCodeType) {
eventSystem.publishEvent(Event.ExitCode, new ExitCodeRequest(exitCodeType));
}

public void publishExitCode(ExitCodeType exitCodeType, String reason) {
publishExitCode(new ExitCodeRequest(exitCodeType, reason));
public void publishExitCode(ExitCodeRequestWithCustomDescription exitCodeRequest) {
eventSystem.publishEvent(Event.ExitCode, exitCodeRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,16 @@

public class ExitCodeRequest {
private final ExitCodeType exitCodeType;
private final String reason;

public ExitCodeRequest(ExitCodeType exitCodeType, String reason) {
this.exitCodeType = exitCodeType;
this.reason = reason;
}

public ExitCodeRequest(ExitCodeType exitCodeType) {
this(exitCodeType, null);
this.exitCodeType = exitCodeType;
}

public ExitCodeType getExitCodeType() {
return exitCodeType;
}

public String getReason() {
return reason;
return exitCodeType.getDescription();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package com.blackduck.integration.detect.lifecycle.shutdown;

import com.blackduck.integration.detect.configuration.enumeration.ExitCodeType;

public class ExitCodeRequestWithCustomDescription extends ExitCodeRequest {

private String customDescription;
public ExitCodeRequestWithCustomDescription(ExitCodeType exitCodeType, String reason) {
super(exitCodeType);
this.customDescription = reason;
}

@Override
public String getReason() {
return customDescription;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public DetectableToolResult extract() { //TODO: Move docker/bazel out of detecta
logger.error(String.format("Was not extractable: %s", extractable.toDescription()));
statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", Arrays.asList(extractable.toDescription())));
statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_GENERAL_ERROR, extractable.toDescription());
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_GENERAL_ERROR);
return DetectableToolResult.failed(extractable);
}

Expand All @@ -126,7 +126,7 @@ public DetectableToolResult extract() { //TODO: Move docker/bazel out of detecta
List<String> errorMessages = collectErrorMessages(extraction);
statusEventPublisher.publishIssue(new DetectIssue(DetectIssueType.DETECTABLE_TOOL, "Detectable Tool Issue", errorMessages));
statusEventPublisher.publishStatusSummary(new Status(name, StatusType.FAILURE));
exitCodePublisher.publishExitCode(new ExitCodeRequest(ExitCodeType.FAILURE_GENERAL_ERROR, extraction.getDescription()));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_GENERAL_ERROR);
return DetectableToolResult.failed();
} else {
logger.debug("Extraction success.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public DetectorToolResult performDetectors(
if (!findResultOptional.isPresent()) {
logger.error("The source directory could not be searched for detectors - detector tool failed.");
logger.error("Please ensure the provided source path is a directory and detect has access.");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_CONFIGURATION, "Detector tool failed to run on the configured source path.");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_CONFIGURATION);
return new DetectorToolResult();
}

Expand All @@ -121,7 +121,7 @@ public DetectorToolResult performDetectors(
private void checkAndHandleOutOfMemoryIssue(List<DetectorDirectoryReport> reports) {
if (detectorIssuePublisher.hasOutOfMemoryIssue(reports)) {
logger.error("Detected an issue. " + DetectorStatusCode.EXECUTABLE_TERMINATED_LIKELY_OUT_OF_MEMORY.getDescription());
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_OUT_OF_MEMORY, "Executable terminated likely due to out of memory.");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_OUT_OF_MEMORY);
}
}

Expand Down Expand Up @@ -247,7 +247,7 @@ private void publishStatusEvents(Map<DetectorType, StatusType> statusMap) {
statusMap.forEach((detectorType, statusType) ->
statusEventPublisher.publishStatusSummary(new DetectorStatus(detectorType, statusType)));
if (statusMap.containsValue(StatusType.FAILURE)) {
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_DETECTOR, "One or more detectors were not successful.");
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_DETECTOR);
}
}

Expand Down Expand Up @@ -280,7 +280,7 @@ private boolean checkAccuracyMet(List<DetectorDirectoryReport> reports, ExcludeI
DetectableDefinition extractedDetectable = extracted.getExtractedDetectable().getDetectable();
if (extractedDetectable.getAccuracyType() != DetectableAccuracyType.HIGH) {
accuracyMet.set(false);
exitCodePublisher.publishExitCode(new ExitCodeRequest(ExitCodeType.FAILURE_ACCURACY_NOT_MET));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_ACCURACY_NOT_MET);
List<String> messages = new ArrayList<>();

messages.add("Accuracy Not Met: " + extracted.getRule().getDetectorType());
Expand All @@ -301,7 +301,7 @@ private void publishMissingDetectorEvents(List<DetectorType> requiredDetectors,
if (!missingDetectors.isEmpty()) {
String missingDetectorDisplay = missingDetectors.stream().map(Enum::toString).collect(Collectors.joining(","));
logger.error("One or more required detector types were not found: {}", missingDetectorDisplay);
exitCodePublisher.publishExitCode(new ExitCodeRequest(ExitCodeType.FAILURE_DETECTOR_REQUIRED));
exitCodePublisher.publishExitCode(ExitCodeType.FAILURE_DETECTOR_REQUIRED);
}
}
}
Loading