Skip to content

Commit

Permalink
Implementation changes for #1167
Browse files Browse the repository at this point in the history
- to avoid implementing always same builder logic for every PDS
  adapter a refactoring was done:

  - PDSAdapterConfigData is contained
  by every PDSAdapterConfig variant. PDS relevant config data is stored
  there now.

  - PDSAdapterConfiguratoris also contained now in every pds adapter
    config variant and is used for configuration by common
    PDSAdapterConfigurationStrategy

  - The data and the configuration is implemented in one class:
    PDSAdapterDataConfigurator
 - documented PDS adapter configuration in architecture concepts
  • Loading branch information
de-jcup committed May 21, 2022
1 parent c0f208d commit ef49e51
Show file tree
Hide file tree
Showing 26 changed files with 579 additions and 516 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ public int getAdapterVersion() {

@Override
protected String execute(PDSAdapterConfig config, AdapterRuntimeContext runtimeContext) throws AdapterException {
String mockingDisabled = config.getJobParameters().get(JOB_PARAMETER_KEY__PDS_MOCKING_DISABLED);
PDSAdapterConfigData data = config.getPDSAdapterConfigData();
String mockingDisabled = data.getJobParameters().get(JOB_PARAMETER_KEY__PDS_MOCKING_DISABLED);

boolean useMock = !Boolean.parseBoolean(mockingDisabled);

LOG.info("execution starting, using mocked adapter={}", useMock);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,8 @@
// SPDX-License-Identifier: MIT
package com.mercedesbenz.sechub.adapter.pds;

import java.util.Map;
import java.util.UUID;

import com.mercedesbenz.sechub.adapter.AdapterConfig;

public interface PDSAdapterConfig extends AdapterConfig {

Map<String, String> getJobParameters();

UUID getSecHubJobUUID();

String getPdsProductIdentifier();
public interface PDSAdapterConfig extends AdapterConfig, PDSAdapterConfigDataProvider {

}
Original file line number Diff line number Diff line change
@@ -1,32 +1,7 @@
package com.mercedesbenz.sechub.adapter.pds;

import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

import com.mercedesbenz.sechub.adapter.AdapterConfigBuilder;
import com.mercedesbenz.sechub.commons.model.SecHubConfigurationModel;

public interface PDSAdapterConfigBuilder extends AdapterConfigBuilder {

PDSAdapterConfigBuilder setBinariesTarFileInputStream(InputStream binariesTarFileInputStream);

PDSAdapterConfigBuilder setSourceCodeZipFileInputStream(InputStream sourceCodeZipFileInputStream);

PDSAdapterConfigBuilder setSecHubJobUUID(UUID sechubJobUUID);

PDSAdapterConfigBuilder setSecHubConfigModel(SecHubConfigurationModel model);

PDSAdapterConfigBuilder setSourceZipFileChecksum(String sourceZipFileChecksum);

PDSAdapterConfigBuilder setPDSProductIdentifier(String productIdentifier);

/**
* Set job parameters - mandatory
*
* @param jobParameters a map with key values
* @return builder
*/
PDSAdapterConfigBuilder setJobParameters(Map<String, String> jobParameters);
public interface PDSAdapterConfigBuilder extends AdapterConfigBuilder, PDSAdapterConfiguratorProvider {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.mercedesbenz.sechub.adapter.pds;

import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

import com.mercedesbenz.sechub.commons.model.ScanType;
import com.mercedesbenz.sechub.commons.model.SecHubConfigurationModel;

public interface PDSAdapterConfigData {

/**
* @return an unmodifiable map with job parameters
*/
Map<String, String> getJobParameters();

UUID getSecHubJobUUID();

String getPdsProductIdentifier();

InputStream getSourceCodeZipFileInputStreamOrNull();

String getSourceCodeZipFileChecksumOrNull();

InputStream getBinaryTarFileInputStreamOrNull();

boolean isReusingSecHubStorage();

boolean isSourceCodeZipFileRequired();

boolean isBinaryTarFileRequired();

SecHubConfigurationModel getSecHubConfigurationModel();

ScanType getScanType();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mercedesbenz.sechub.adapter.pds;

public interface PDSAdapterConfigDataProvider {

PDSAdapterConfigData getPDSAdapterConfigData();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package com.mercedesbenz.sechub.adapter.pds;

import java.io.InputStream;
import java.util.Map;
import java.util.UUID;

import com.mercedesbenz.sechub.commons.model.ScanType;
import com.mercedesbenz.sechub.commons.model.SecHubConfigurationModel;

public interface PDSAdapterConfigurator {

void setPdsProductIdentifier(String pdsProductIdentifier);

void setSourceCodeZipFileInputStreamOrNull(InputStream sourceCodeZipFileInputStreamOrNull);

void setSourceCodeZipFileChecksumOrNull(String sourceCodeZipFileChecksumOrNull);

void setBinaryTarFileInputStreamOrNull(InputStream binaryTarFileInputStreamOrNull);

void setSecHubJobUUID(UUID secHubJobUUID);

/**
* Set job parameters - mandatory
*
* @param jobParameters a map with key values
* @return builder
*/
void setJobParameters(Map<String, String> jobParameters);

void setSecHubConfigurationModel(SecHubConfigurationModel secHubConfigurationModel);

void setReusingSecHubStorage(boolean reusingSecHubStorage);

void setSourceCodeZipFileRequired(boolean sourceCodeZipFileRequired);

void setBinaryTarFileRequired(boolean binaryTarFileRequired);

void setScanType(ScanType scanType);

/**
* Will be automatically called by {@link #configure()}. and validates defined
* parts only
*/
void validateNonCalculatedParts();

/**
* Will be automatically called by {@link #configure()}. If the calculated parts
* needs a validation as well, this must be done also here. The
* {@link #validateNonCalculatedParts()} method does only check the non
* calculated parts.
*/
void calculate();

/**
* First {@link #validateNonCalculatedParts()} will be called by this method.
* Then the {@link #calculate()} method is called. The default implementation
* does this already and should not be changed.
*/
public default void configure() {
validateNonCalculatedParts();
calculate();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.mercedesbenz.sechub.adapter.pds;

public interface PDSAdapterConfiguratorProvider {

PDSAdapterConfigurator getPDSAdapterConfigurator();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package com.mercedesbenz.sechub.adapter.pds;

import java.io.InputStream;
import java.util.Collections;
import java.util.Map;
import java.util.UUID;

import com.mercedesbenz.sechub.commons.model.ScanType;
import com.mercedesbenz.sechub.commons.model.SecHubConfigurationModel;
import com.mercedesbenz.sechub.commons.model.SecHubConfigurationModelReducedCloningSupport;
import com.mercedesbenz.sechub.commons.pds.PDSDefaultParameterKeyConstants;

public class PDSAdapterDataConfigurator implements PDSAdapterConfigData, PDSAdapterConfigurator {
private static final String EMPTY_TARGET_TYPE = "";

private String pdsProductIdentifier;
private InputStream sourceCodeZipFileInputStreamOrNull;
private String sourceCodeZipFileChecksumOrNull;
private InputStream binaryTarFileInputStreamOrNull;
private UUID secHubJobUUID;
private Map<String, String> jobParameters;
private SecHubConfigurationModel secHubConfigurationModel;
private boolean reusingSecHubStorage;

private boolean sourceCodeZipFileRequired;
private boolean binaryTarFileRequired;
private String targetType = EMPTY_TARGET_TYPE;
private ScanType scanType;

public void setTargetType(String targetType) {
if (targetType == null) {
this.targetType = EMPTY_TARGET_TYPE;
} else {
this.targetType = targetType;
}
}

@Override
public void setScanType(ScanType scanType) {
this.scanType = scanType;
}

@Override
public ScanType getScanType() {
return scanType;
}

@Override
public Map<String, String> getJobParameters() {
return Collections.unmodifiableMap(jobParameters);
}

public void addJobParameter(String key, String value) {
jobParameters.put(key, value);
}

@Override
public UUID getSecHubJobUUID() {
return secHubJobUUID;
}

@Override
public String getPdsProductIdentifier() {
return pdsProductIdentifier;
}

@Override
public InputStream getSourceCodeZipFileInputStreamOrNull() {
return sourceCodeZipFileInputStreamOrNull;
}

@Override
public String getSourceCodeZipFileChecksumOrNull() {
return sourceCodeZipFileChecksumOrNull;
}

@Override
public InputStream getBinaryTarFileInputStreamOrNull() {
return binaryTarFileInputStreamOrNull;
}

@Override
public boolean isReusingSecHubStorage() {
return reusingSecHubStorage;
}

@Override
public boolean isSourceCodeZipFileRequired() {
return sourceCodeZipFileRequired;
}

@Override
public boolean isBinaryTarFileRequired() {
return binaryTarFileRequired;
}

@Override
public SecHubConfigurationModel getSecHubConfigurationModel() {
return secHubConfigurationModel;
}

@Override
public void setPdsProductIdentifier(String pdsProductIdentifier) {
this.pdsProductIdentifier = pdsProductIdentifier;
}

@Override
public void setSourceCodeZipFileInputStreamOrNull(InputStream sourceCodeZipFileInputStreamOrNull) {
this.sourceCodeZipFileInputStreamOrNull = sourceCodeZipFileInputStreamOrNull;
}

@Override
public void setSourceCodeZipFileChecksumOrNull(String sourceCodeZipFileChecksumOrNull) {
this.sourceCodeZipFileChecksumOrNull = sourceCodeZipFileChecksumOrNull;
}

@Override
public void setBinaryTarFileInputStreamOrNull(InputStream binaryTarFileInputStreamOrNull) {
this.binaryTarFileInputStreamOrNull = binaryTarFileInputStreamOrNull;
}

@Override
public void setSecHubJobUUID(UUID secHubJobUUID) {
this.secHubJobUUID = secHubJobUUID;
}

@Override
public void setJobParameters(Map<String, String> jobParameters) {
this.jobParameters = jobParameters;
}

@Override
public void setSecHubConfigurationModel(SecHubConfigurationModel secHubConfigurationModel) {
this.secHubConfigurationModel = secHubConfigurationModel;
}

public void setReusingSecHubStorage(boolean reusingSecHubStorage) {
this.reusingSecHubStorage = reusingSecHubStorage;
}

public void setSourceCodeZipFileRequired(boolean sourceCodeZipFileRequired) {
this.sourceCodeZipFileRequired = sourceCodeZipFileRequired;
}

public void setBinaryTarFileRequired(boolean binaryTarFileRequired) {
this.binaryTarFileRequired = binaryTarFileRequired;
}

@Override
public void validateNonCalculatedParts() {
if (pdsProductIdentifier == null) {
throw new IllegalStateException("pds product identifier not set!");
}
if (jobParameters == null) {
throw new IllegalStateException("job parameters not set!");
}
if (secHubJobUUID == null) {
throw new IllegalStateException("sechubJobUUID not set!");
}
if (scanType == null) {
throw new IllegalStateException("scanType not set!");
}
if (targetType == null) {
/*
* remark: the target type is only set by network specific scans like infra
* scan, or web scan, but not for code or license scans!
*/
throw new IllegalStateException("targetType may notbe null!");
}
}

@Override
public void calculate() {
if (secHubConfigurationModel != null) {
String reducedConfigJSON = SecHubConfigurationModelReducedCloningSupport.DEFAULT.createReducedScanConfigurationCloneJSON(secHubConfigurationModel,
scanType);
jobParameters.put(PDSDefaultParameterKeyConstants.PARAM_KEY_PDS_SCAN_CONFIGURATION, reducedConfigJSON);
}
jobParameters.put(PDSDefaultParameterKeyConstants.PARAM_KEY_PDS_SCAN_TARGET_TYPE, targetType);
}

}
Loading

0 comments on commit ef49e51

Please sign in to comment.