Skip to content

Commit

Permalink
Keep code location creation code only where its needed and some maint…
Browse files Browse the repository at this point in the history
…ainability refactoring (#1359)
  • Loading branch information
andrian-sevastyanov authored Feb 13, 2025
1 parent 431b059 commit 13600dc
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -570,9 +570,7 @@ public ScanCreationResponse uploadBdioHeaderToInitiateScassScan(BlackDuckRunData

Response response = blackDuckApiClient.execute(buildBlackDuckResponseRequest);
String contentString = response.getContentString();
ScanCreationResponse scanCreationResponse = gson.fromJson(contentString, ScanCreationResponse.class);

return scanCreationResponse;
return gson.fromJson(contentString, ScanCreationResponse.class);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public void runBdbaScan(NameVersion projectNameVersion, BlackDuckRunData blackDu
}
}

private DefaultUploadStatus uploadBdbaFile(String scanId, BlackDuckRunData blackDuckRunData, File bdbaFile) throws IOException, OperationException, IntegrationException {
private DefaultUploadStatus uploadBdbaFile(String scanId, BlackDuckRunData blackDuckRunData, File bdbaFile) throws IOException, IntegrationException {
String storageServiceEndpoint = String.join("", STORAGE_BDBA_ENDPOINT, scanId);
logger.debug("Uploading BDBA scan artifact to storage endpoint: {}", storageServiceEndpoint);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -141,21 +141,13 @@ public void runOnline(
});

stepHelper.runToolIfIncluded(DetectTool.BINARY_SCAN, "Binary Scanner", () -> {
AbstractBinaryScanStepRunner binaryScanStepRunner = CommonScanStepRunner.areScassScansPossible(blackDuckRunData.getBlackDuckServerVersion()) ?
new ScassOrBdbaBinaryScanStepRunner(operationRunner) :
new PreScassBinaryScanStepRunner(operationRunner);
invokeBinaryScanningWorkflow(DetectTool.BINARY_SCAN, binaryScanStepRunner, dockerTargetData, projectNameVersion, blackDuckRunData, binaryTargets, scanIdsToWaitFor, codeLocationAccumulator, mustWaitAtBomSummaryLevel);
invokeBinaryScanningWorkflow(DetectTool.BINARY_SCAN, dockerTargetData, projectNameVersion, blackDuckRunData, binaryTargets, scanIdsToWaitFor, codeLocationAccumulator, mustWaitAtBomSummaryLevel);
});

stepHelper.runToolIfIncluded(
DetectTool.CONTAINER_SCAN,
"Container Scanner",
() -> {
AbstractContainerScanStepRunner containerScanStepRunner = CommonScanStepRunner.areScassScansPossible(blackDuckRunData.getBlackDuckServerVersion()) ?
new ScassOrBdbaContainerScanStepRunner(operationRunner, projectNameVersion, blackDuckRunData, gson) :
new PreScassContainerScanStepRunner(operationRunner, projectNameVersion, blackDuckRunData, gson);
invokeContainerScanningWorkflow(containerScanStepRunner, scanIdsToWaitFor, codeLocationAccumulator);
}
() -> invokeContainerScanningWorkflow(scanIdsToWaitFor, codeLocationAccumulator, blackDuckRunData, projectNameVersion)
);

stepHelper.runToolIfIncludedWithCallbacks(
Expand Down Expand Up @@ -221,7 +213,6 @@ public void runOnline(

private void invokeBinaryScanningWorkflow(
DetectTool detectTool,
AbstractBinaryScanStepRunner binaryScanStepRunner,
DockerTargetData dockerTargetData,
NameVersion projectNameVersion,
BlackDuckRunData blackDuckRunData,
Expand All @@ -233,6 +224,10 @@ private void invokeBinaryScanningWorkflow(
throws IntegrationException, OperationException {
logger.debug("Invoking intelligent persistent binary scan.");

AbstractBinaryScanStepRunner binaryScanStepRunner = CommonScanStepRunner.areScassScansPossible(blackDuckRunData.getBlackDuckServerVersion()) ?
new ScassOrBdbaBinaryScanStepRunner(operationRunner) :
new PreScassBinaryScanStepRunner(operationRunner);

Optional<UUID> scanId = binaryScanStepRunner.invokeBinaryScanningWorkflow(dockerTargetData, projectNameVersion,
blackDuckRunData, binaryTargets);

Expand All @@ -249,12 +244,20 @@ private void invokeBinaryScanningWorkflow(
}

private void invokeContainerScanningWorkflow(
AbstractContainerScanStepRunner containerScanStepRunner,
Set<String> scanIdsToWaitFor,
CodeLocationAccumulator codeLocationAccumulator
) {
CodeLocationAccumulator codeLocationAccumulator,
BlackDuckRunData blackDuckRunData,
NameVersion projectNameVersion
) throws IntegrationException, OperationException{
logger.debug("Invoking intelligent persistent container scan.");

AbstractContainerScanStepRunner containerScanStepRunner;
if (CommonScanStepRunner.areScassScansPossible(blackDuckRunData.getBlackDuckServerVersion())) {
containerScanStepRunner = new ScassOrBdbaContainerScanStepRunner(operationRunner, projectNameVersion, blackDuckRunData, gson);
} else {
containerScanStepRunner = new PreScassContainerScanStepRunner(operationRunner, projectNameVersion, blackDuckRunData, gson);
}

Optional<UUID> scanId = containerScanStepRunner.invokeContainerScanningWorkflow();
scanId.ifPresent(uuid -> scanIdsToWaitFor.add(uuid.toString()));
Set<String> containerScanCodeLocations = new HashSet<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
import com.blackduck.integration.detect.lifecycle.run.data.BlackDuckRunData;
import com.blackduck.integration.detect.lifecycle.run.operation.OperationRunner;
import com.blackduck.integration.detect.lifecycle.run.step.CommonScanStepRunner;
import com.blackduck.integration.detect.workflow.codelocation.CodeLocationNameManager;
import com.blackduck.integration.exception.IntegrationException;
import com.blackduck.integration.exception.IntegrationTimeoutException;
import com.blackduck.integration.util.NameVersion;
Expand All @@ -23,17 +22,16 @@
public abstract class AbstractContainerScanStepRunner {

protected final OperationRunner operationRunner;
protected final String scanType = CommonScanStepRunner.CONTAINER;
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
protected final NameVersion projectNameVersion;
protected final BlackDuckRunData blackDuckRunData;
protected final File containerRunDirectory;
protected final File containerImage;
protected final Gson gson;
protected String codeLocationName;
private static final BlackDuckVersion MIN_BLACK_DUCK_VERSION = new BlackDuckVersion(2023, 10, 0);
protected static final String SCAN_TYPE = CommonScanStepRunner.CONTAINER;
private static final BlackDuckVersion MIN_BLACK_DUCK_VERSION = new BlackDuckVersion(2023, 10, 0);

public AbstractContainerScanStepRunner(OperationRunner operationRunner, NameVersion projectNameVersion, BlackDuckRunData blackDuckRunData, Gson gson)
protected AbstractContainerScanStepRunner(OperationRunner operationRunner, NameVersion projectNameVersion, BlackDuckRunData blackDuckRunData, Gson gson)
throws IntegrationException, OperationException {
this.operationRunner = operationRunner;
this.projectNameVersion = projectNameVersion;
Expand Down Expand Up @@ -80,16 +78,11 @@ public Optional<UUID> invokeContainerScanningWorkflow() {
}
}

protected abstract UUID performBlackduckInteractions() throws IOException, IntegrationException, OperationException;
public abstract String getCodeLocationName();

public String getCodeLocationName() {
if (codeLocationName == null) {
codeLocationName = createContainerScanCodeLocationName();
}
return codeLocationName;
}
protected abstract UUID performBlackduckInteractions() throws IOException, IntegrationException, OperationException;

private boolean isContainerImageResolved() {
protected boolean isContainerImageResolved() {
return containerImage != null && containerImage.exists();
}

Expand All @@ -101,13 +94,4 @@ private boolean isBlackDuckVersionValid() {
Optional<BlackDuckVersion> blackDuckVersion = blackDuckRunData.getBlackDuckServerVersion();
return blackDuckVersion.isPresent() && blackDuckVersion.get().isAtLeast(MIN_BLACK_DUCK_VERSION);
}

private String createContainerScanCodeLocationName() {
if (!isContainerImageResolved()) {
return null;
}

CodeLocationNameManager codeLocationNameManager = operationRunner.getCodeLocationNameManager();
return codeLocationNameManager.createContainerScanCodeLocationName(containerImage, projectNameVersion.getName(), projectNameVersion.getVersion());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.blackduck.integration.detect.lifecycle.run.step.CommonScanStepRunner;
import com.blackduck.integration.detect.lifecycle.run.step.utility.UploaderHelper;
import com.blackduck.integration.detect.util.bdio.protobuf.DetectProtobufBdioHeaderUtil;
import com.blackduck.integration.detect.workflow.codelocation.CodeLocationNameManager;
import com.blackduck.integration.exception.IntegrationException;
import com.blackduck.integration.rest.response.Response;
import com.blackduck.integration.sca.upload.client.uploaders.ContainerUploader;
Expand All @@ -25,6 +26,7 @@ public class PreScassContainerScanStepRunner extends AbstractContainerScanStepRu
private final String projectGroupName;
private final Long containerImageSizeInBytes;
private UploaderFactory uploadFactory;
private String codeLocationName;
private static final BlackDuckVersion MIN_MULTIPART_UPLOAD_VERSION = new BlackDuckVersion(2024, 10, 0);
private static final String STORAGE_CONTAINERS_ENDPOINT = "/api/storage/containers/";
private static final String STORAGE_IMAGE_CONTENT_TYPE = "application/vnd.blackducksoftware.container-scan-data-1+octet-stream";
Expand All @@ -36,6 +38,14 @@ public PreScassContainerScanStepRunner(OperationRunner operationRunner, NameVers
containerImageSizeInBytes = containerImage != null && containerImage.exists() ? containerImage.length() : 0;
}

@Override
public String getCodeLocationName() {
if (codeLocationName == null) {
codeLocationName = createContainerScanCodeLocationName();
}
return codeLocationName;
}

@Override
protected UUID performBlackduckInteractions() throws IOException, IntegrationException, OperationException {
UUID scanId = initiateScan();
Expand All @@ -51,10 +61,19 @@ protected UUID performBlackduckInteractions() throws IOException, IntegrationExc
return scanId;
}

private String createContainerScanCodeLocationName() {
if (!isContainerImageResolved()) {
return null;
}

CodeLocationNameManager codeLocationNameManager = operationRunner.getCodeLocationNameManager();
return codeLocationNameManager.createContainerScanCodeLocationName(containerImage, projectNameVersion.getName(), projectNameVersion.getVersion());
}

private UUID initiateScan() throws IOException, IntegrationException, OperationException {
DetectProtobufBdioHeaderUtil detectProtobufBdioHeaderUtil = new DetectProtobufBdioHeaderUtil(
UUID.randomUUID().toString(),
scanType,
SCAN_TYPE,
projectNameVersion,
projectGroupName,
getCodeLocationName(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import com.google.gson.Gson;

public class ScassOrBdbaContainerScanStepRunner extends AbstractContainerScanStepRunner {

CommonScanStepRunner commonScanStepRunner;

private String codeLocationName;
private CommonScanStepRunner commonScanStepRunner;

public ScassOrBdbaContainerScanStepRunner(OperationRunner operationRunner, NameVersion projectNameVersion, BlackDuckRunData blackDuckRunData, Gson gson) throws IntegrationException, OperationException {
super(operationRunner, projectNameVersion, blackDuckRunData, gson);
Expand All @@ -29,10 +30,15 @@ protected UUID performBlackduckInteractions() throws IntegrationException, Opera
Optional.of(containerImage),
operationRunner,
gson,
scanType);
SCAN_TYPE);

codeLocationName = scanResult.getCodeLocationName();

return scanResult.getScanId();
}

@Override
public String getCodeLocationName() {
return codeLocationName;
}
}

0 comments on commit 13600dc

Please sign in to comment.