Skip to content

Commit

Permalink
Some changes for default handling #2594
Browse files Browse the repository at this point in the history
- web api default reference id not set for api uploads
- wrote unit test to ensure default ref id and default
  project id are used, but when dedicated values are set
  the defaults are not used
  • Loading branch information
de-jcup committed Oct 13, 2023
1 parent 3f28d04 commit 38f7fdb
Show file tree
Hide file tree
Showing 2 changed files with 292 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ public void prepare(SystemTestRuntimeContext context) {
initializeAlteredConfiguration(context);

prepareLocal(context);
prepareRemote(context);

prepareTests(context);
}
Expand Down Expand Up @@ -127,7 +128,10 @@ private void handleWebScan(TestDefinition test, SystemTestRuntimeContext context
}
SecHubWebScanConfiguration webScan = webScanOpt.get();
LOG.warn("Web scan found, but no special preparation done for url: {}", webScan.getUrl());

if (webScan.getApi().isEmpty()) {
return;
}
handleUsedDataConfigurationObjects(webScan.getApi().get(), test, context);
}

private void handleInfraScan(TestDefinition test, SystemTestRuntimeContext context, RunSecHubJobDefinition runSecHubJob) {
Expand Down Expand Up @@ -256,6 +260,14 @@ private void prepareLocal(SystemTestRuntimeContext context) {

}

private void prepareRemote(SystemTestRuntimeContext context) {
if (context.isLocalRun()) {
LOG.debug("Skip remote preparation - run is not remote");
return;
}
/* currently no special remote preparation at all */
}

private void addFallbackDefaultProfileToExecutorsWithoutProfile(SystemTestRuntimeContext context) {
SecHubConfigurationDefinition sechubConfig = context.getLocalSecHubConfigurationOrFail();
List<SecHubExecutorConfigDefinition> executors = sechubConfig.getExecutors();
Expand Down Expand Up @@ -320,7 +332,6 @@ private void createFallbackNamesForExecutorsWithoutName(SystemTestRuntimeContext
executor.setName(newName);
}
}

}

private void createFallbacksForPDSSolutions(SystemTestRuntimeContext context) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,33 @@

import java.io.IOException;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.Optional;
import java.util.Set;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.EnumSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.mercedesbenz.sechub.commons.model.JSONConverter;
import com.mercedesbenz.sechub.commons.model.ScanType;
import com.mercedesbenz.sechub.commons.model.SecHubCodeScanConfiguration;
import com.mercedesbenz.sechub.commons.model.SecHubDataConfigurationUsageByName;
import com.mercedesbenz.sechub.commons.model.SecHubInfrastructureScanConfiguration;
import com.mercedesbenz.sechub.commons.model.SecHubLicenseScanConfiguration;
import com.mercedesbenz.sechub.commons.model.SecHubSecretScanConfiguration;
import com.mercedesbenz.sechub.commons.model.SecHubWebScanApiConfiguration;
import com.mercedesbenz.sechub.commons.model.SecHubWebScanConfiguration;
import com.mercedesbenz.sechub.systemtest.config.DefaultFallback;
import com.mercedesbenz.sechub.systemtest.config.LocalSetupDefinition;
import com.mercedesbenz.sechub.systemtest.config.RemoteSetupDefinition;
import com.mercedesbenz.sechub.systemtest.config.RunSecHubJobDefinition;
import com.mercedesbenz.sechub.systemtest.config.SystemTestConfiguration;
import com.mercedesbenz.sechub.systemtest.config.TestDefinition;
import com.mercedesbenz.sechub.systemtest.config.UploadDefinition;
import com.mercedesbenz.sechub.systemtest.runtime.init.SystemTestRuntimePreparator;
import com.mercedesbenz.sechub.systemtest.runtime.variable.EnvironmentProvider;
import com.mercedesbenz.sechub.test.TestUtil;
Expand All @@ -22,6 +44,8 @@ class SystemTestRuntimePreparatorTest {
private EnvironmentProvider environmentProvider;
private SystemTestRuntimeContext context;

private static final Logger LOG = LoggerFactory.getLogger(SystemTestRuntimePreparatorTest.class);

@BeforeEach
void beforeEach() throws IOException {
Path aTemporaryFolder = TestUtil.createTempDirectoryInBuildFolder("systemtest_prep_testfolder");
Expand Down Expand Up @@ -60,4 +84,259 @@ void variable_handling_in_a_remote_config_even_comments_can_have_variables() {

}

@ParameterizedTest
@EnumSource(PreparationTestData.class)
void default_preparation_as_expected(PreparationTestData data) {
/* prepare */
SystemTestConfiguration originConfiguration = new SystemTestConfiguration();
if (data.scope.local) {
LocalSetupDefinition localSetup = new LocalSetupDefinition();
originConfiguration.getSetup().setLocal(Optional.of(localSetup));
} else {
RemoteSetupDefinition remoteSetup = new RemoteSetupDefinition();
originConfiguration.getSetup().setRemote(Optional.of(remoteSetup));
}

RunSecHubJobDefinition secHubJob = new RunSecHubJobDefinition();
prepareUpload(data, secHubJob);
prepareScanConfigurationsForScanTypes(data, secHubJob);
prepareProject(data, secHubJob);

TestDefinition testdefinition = new TestDefinition();
testdefinition.getExecute().setRunSecHubJob(Optional.of(secHubJob));

originConfiguration.getTests().add(testdefinition);

context.originConfiguration = originConfiguration;

// setup context - local/remote run. important for preparator!
context.localRun = data.scope.local;

LOG.debug("originConfiguration for {} - local run: {} \n{}", data.name(), context.isLocalRun(), JSONConverter.get().toJSON(originConfiguration, true));

/* execute */
preparatorToTest.prepare(context);

/* test */
SystemTestConfiguration preparedConfig = context.getConfiguration();
LOG.debug("preparedConfig for {}:\n{}", data.name(), JSONConverter.get().toJSON(preparedConfig, true));

TestDefinition test1 = preparedConfig.getTests().iterator().next();
RunSecHubJobDefinition job = test1.getExecute().getRunSecHubJob().get();

String expectedReferenceId = data.scope.refId;
if (expectedReferenceId == null) {
/* not defined means the default fallback must be injected by preparator */
expectedReferenceId = DefaultFallback.FALLBACK_UPLOAD_REF_ID.getValue();
}
assertUploadRefUsesReferenceId(job, expectedReferenceId);
assertJobScansUseReferenceId(job, expectedReferenceId);

String expectedProjectName = data.scope.getDefinedProjectName();
if (expectedProjectName == null) {
/* not defined means the default fallback must be injected by preparator */
/* we use the ref id here also for defined project names */
expectedProjectName = DefaultFallback.FALLBACK_PROJECT_NAME.getValue();
}
assertProjectName(job, expectedProjectName);

}

private void prepareProject(PreparationTestData data, RunSecHubJobDefinition secHubJob) {
if (!data.scope.hasDedicatedProjectName()) {
return;
}
secHubJob.setProject(data.scope.getDefinedProjectName());

}

private void assertProjectName(RunSecHubJobDefinition job, String expectedProjectName) {
assertEquals(expectedProjectName, job.getProject());

if (context.isLocalRun()) {

}
}

private void assertUploadRefUsesReferenceId(RunSecHubJobDefinition job, String expectedReferenceId) {
UploadDefinition upload1 = job.getUploads().iterator().next();
if (upload1.getReferenceId().isEmpty()) {
fail("upload reference id not set!");
}
String refId = upload1.getReferenceId().get();
assertEquals(expectedReferenceId, refId);

}

private void assertJobScansUseReferenceId(RunSecHubJobDefinition job, String expectedReferenceId) {
if (job.getCodeScan().isPresent()) {
assertContainsReference(job.getCodeScan().get().getNamesOfUsedDataConfigurationObjects(), expectedReferenceId);
}
if (job.getSecretScan().isPresent()) {
assertContainsReference(job.getSecretScan().get().getNamesOfUsedDataConfigurationObjects(), expectedReferenceId);
}
if (job.getLicenseScan().isPresent()) {
assertContainsReference(job.getLicenseScan().get().getNamesOfUsedDataConfigurationObjects(), expectedReferenceId);
}
if (job.getWebScan().isPresent() && job.getWebScan().get().getApi().isPresent()) {
assertContainsReference(job.getWebScan().get().getApi().get().getNamesOfUsedDataConfigurationObjects(), expectedReferenceId);
}
}

private void assertContainsReference(Set<String> list, String exptectedReferenceId) {
if (list.contains(exptectedReferenceId)) {
return;
}
assertEquals(Arrays.asList(exptectedReferenceId), list, "Reference id is not contained as expected");

}

private void prepareScanConfigurationsForScanTypes(PreparationTestData data, RunSecHubJobDefinition secHubJob) {
if (data.scope.isTypeContained(ScanType.CODE_SCAN)) {
SecHubCodeScanConfiguration codeScan = new SecHubCodeScanConfiguration();
prepareRefidIfDefined(data, codeScan);
secHubJob.setCodeScan(Optional.of(codeScan));
}
if (data.scope.isTypeContained(ScanType.SECRET_SCAN)) {
SecHubSecretScanConfiguration secretScan = new SecHubSecretScanConfiguration();
prepareRefidIfDefined(data, secretScan);
secHubJob.setSecretScan(Optional.of(secretScan));
}
if (data.scope.isTypeContained(ScanType.LICENSE_SCAN)) {
SecHubLicenseScanConfiguration licenseScan = new SecHubLicenseScanConfiguration();
prepareRefidIfDefined(data, licenseScan);
secHubJob.setLicenseScan(Optional.of(licenseScan));
}
if (data.scope.isTypeContained(ScanType.WEB_SCAN)) {
SecHubWebScanConfiguration webScan = new SecHubWebScanConfiguration();
SecHubWebScanApiConfiguration apiConfig = new SecHubWebScanApiConfiguration();
webScan.setApi(Optional.of(apiConfig));
prepareRefidIfDefined(data, apiConfig);
secHubJob.setWebScan(Optional.of(webScan));
}
if (data.scope.isTypeContained(ScanType.INFRA_SCAN)) {
SecHubInfrastructureScanConfiguration infraScan = new SecHubInfrastructureScanConfiguration();
/* we cannot set a reference for infra scans */
secHubJob.setInfraScan(Optional.of(infraScan));
}
}

private void prepareRefidIfDefined(PreparationTestData data, SecHubDataConfigurationUsageByName target) {
if (data.scope.refId == null) {
return;
}
target.getNamesOfUsedDataConfigurationObjects().add(data.scope.refId);
}

private void prepareUpload(PreparationTestData data, RunSecHubJobDefinition secHubJob) {
UploadDefinition upload1 = new UploadDefinition();
if (data.scope.sourceUpload) {
upload1.setSourceFolder(Optional.of("src-folder1"));
}
if (data.scope.binaryUpload) {
upload1.setBinariesFolder(Optional.of("bin-folder1"));
}
if (data.scope.refId != null) {
upload1.setReferenceId(Optional.of(data.scope.refId));
}
secHubJob.getUploads().add(upload1);
}

private record TestScope(boolean local, String refId, boolean sourceUpload, boolean binaryUpload, ScanType... types) {

private boolean isTypeContained(ScanType typeToSearch) {
for (ScanType currentType : types) {
if (currentType.equals(typeToSearch)) {
return true;
}
}
return false;
}

private boolean hasDedicatedProjectName() {
// we just reuse refid for simplicity
return refId != null;
}

private String getDefinedProjectName() {
// we just reuse refid for simplicity
return refId;
}
}

private static final boolean LOCAL = false;
private static final boolean REMOTE = false;

private static final boolean SRC_UPLOAD = true;
private static final boolean NO_SRC_UPLOAD = false;

private static final boolean BIN_UPLOAD = true;
private static final boolean NO_BIN_UPLOAD = false;

private static final String DEFINED_REF_ID = "defined-ref-id";

private enum PreparationTestData {

/*
* next data represents upload + scan have NO dedicated reference id, means
* default reference id will be set
*/
LOCAL_BINYARY_UPLOAD__CODE_SCAN(new TestScope(LOCAL, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.CODE_SCAN)),
LOCAL_BINYARY_UPLOAD__LICENSE_SCAN(new TestScope(LOCAL, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.LICENSE_SCAN)),
LOCAL_BINYARY_UPLOAD__WEB_SCAN(new TestScope(LOCAL, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.WEB_SCAN)),
LOCAL_BINYARY_UPLOAD__INFRA_SCAN(new TestScope(LOCAL, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.INFRA_SCAN)),

LOCAL_SOURCE_UPLOAD__CODE_SCAN(new TestScope(LOCAL, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.CODE_SCAN)),
LOCAL_SOURCE_UPLOAD__LICENSE_SCAN(new TestScope(LOCAL, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.LICENSE_SCAN)),
LOCAL_SOURCE_UPLOAD__WEB_SCAN(new TestScope(LOCAL, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.WEB_SCAN)),
LOCAL_SOURCE_UPLOAD__INFRA_SCAN(new TestScope(LOCAL, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.INFRA_SCAN)),
LOCAL_SOURCE_UPLOAD__SECRET_SCAN(new TestScope(LOCAL, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.SECRET_SCAN)),

REMOTE_BINYARY_UPLOAD__CODE_SCAN(new TestScope(REMOTE, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.CODE_SCAN)),
REMOTE_BINYARY_UPLOAD__LICENSE_SCAN(new TestScope(REMOTE, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.LICENSE_SCAN)),
REMOTE_BINYARY_UPLOAD__WEB_SCAN(new TestScope(REMOTE, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.WEB_SCAN)),
REMOTE_BINYARY_UPLOAD__INFRA_SCAN(new TestScope(REMOTE, null, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.INFRA_SCAN)),

REMOTE_SOURCE_UPLOAD__CODE_SCAN(new TestScope(REMOTE, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.CODE_SCAN)),
REMOTE_SOURCE_UPLOAD__LICENSE_SCAN(new TestScope(REMOTE, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.LICENSE_SCAN)),
REMOTE_SOURCE_UPLOAD__WEB_SCAN(new TestScope(REMOTE, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.WEB_SCAN)),
REMOTE_SOURCE_UPLOAD__INFRA_SCAN(new TestScope(REMOTE, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.INFRA_SCAN)),
REMOTE_SOURCE_UPLOAD__SECRET_SCAN(new TestScope(REMOTE, null, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.SECRET_SCAN)),

/*
* next data represents upload + scan have a dedicated reference id, which is
* not changed
*/
DEFINED_REF_ID_LOCAL_BINYARY_UPLOAD__CODE_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.CODE_SCAN)),
DEFINED_REF_ID_LOCAL_BINYARY_UPLOAD__LICENSE_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.LICENSE_SCAN)),
DEFINED_REF_ID_LOCAL_BINYARY_UPLOAD__WEB_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.WEB_SCAN)),
DEFINED_REF_ID_LOCAL_BINYARY_UPLOAD__INFRA_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.INFRA_SCAN)),

DEFINED_REF_ID_LOCAL_SOURCE_UPLOAD__CODE_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.CODE_SCAN)),
DEFINED_REF_ID_LOCAL_SOURCE_UPLOAD__LICENSE_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.LICENSE_SCAN)),
DEFINED_REF_ID_LOCAL_SOURCE_UPLOAD__WEB_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.WEB_SCAN)),
DEFINED_REF_ID_LOCAL_SOURCE_UPLOAD__INFRA_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.INFRA_SCAN)),
DEFINED_REF_ID_LOCAL_SOURCE_UPLOAD__SECRET_SCAN(new TestScope(LOCAL, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.SECRET_SCAN)),

DEFINED_REF_ID_REMOTE_BINYARY_UPLOAD__CODE_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.CODE_SCAN)),
DEFINED_REF_ID_REMOTE_BINYARY_UPLOAD__LICENSE_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.LICENSE_SCAN)),
DEFINED_REF_ID_REMOTE_BINYARY_UPLOAD__WEB_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.WEB_SCAN)),
DEFINED_REF_ID_REMOTE_BINYARY_UPLOAD__INFRA_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, NO_SRC_UPLOAD, BIN_UPLOAD, ScanType.INFRA_SCAN)),

DEFINED_REF_ID_REMOTE_SOURCE_UPLOAD__CODE_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.CODE_SCAN)),
DEFINED_REF_ID_REMOTE_SOURCE_UPLOAD__LICENSE_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.LICENSE_SCAN)),
DEFINED_REF_ID_REMOTE_SOURCE_UPLOAD__WEB_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.WEB_SCAN)),
DEFINED_REF_ID_REMOTE_SOURCE_UPLOAD__INFRA_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.INFRA_SCAN)),
DEFINED_REF_ID_REMOTE_SOURCE_UPLOAD__SECRET_SCAN(new TestScope(REMOTE, DEFINED_REF_ID, SRC_UPLOAD, NO_BIN_UPLOAD, ScanType.SECRET_SCAN)),

;

private TestScope scope;

private PreparationTestData(TestScope scope) {
this.scope = scope;
}

}

}

0 comments on commit 38f7fdb

Please sign in to comment.