Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Infer SCA Resolver Arguments from Task #37

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
<version>3.7.0.1746</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,10 @@ private void populateOSA_SCA_FieldsForCreate(Map<String, Object> context) {
context.put(GLOBAL_CXSCA_ACCOUNT_NAME,getAdminConfig(GLOBAL_CXSCA_ACCOUNT_NAME));
context.put(GLOBAL_CXSCA_USERNAME,getAdminConfig(GLOBAL_CXSCA_USERNAME));
context.put(GLOBAL_CXSCA_PWD,getAdminConfig(GLOBAL_CXSCA_PWD));


context.put(CXSCA_RESOLVER_ENABLED_GLOBAL,getAdminConfig(CXSCA_RESOLVER_ENABLED_GLOBAL));
context.put(CXSCA_RESOLVER_PATH_GLOBAL,getAdminConfig(CXSCA_RESOLVER_PATH_GLOBAL));
context.put(CXSCA_RESOLVER_ADD_PARAM_GLOBAL,getAdminConfig(CXSCA_RESOLVER_ADD_PARAM_GLOBAL));

context.put(GLOBAL_OSA_ARCHIVE_INCLUDE_PATTERNS, getAdminConfig(GLOBAL_OSA_ARCHIVE_INCLUDE_PATTERNS));
context.put(GLOBAL_OSA_INSTALL_BEFORE_SCAN, getAdminConfig(GLOBAL_OSA_INSTALL_BEFORE_SCAN));
Expand Down Expand Up @@ -550,9 +553,9 @@ private Map<String, String> generateCxOSAAndSCAFields(@NotNull final ActionParam

config.put(CXSCA_USERNAME,getAdminConfig(GLOBAL_CXSCA_USERNAME).trim());
config.put(CXSCA_PWD,getAdminConfig(GLOBAL_CXSCA_PWD).trim());
config.put(CXSCA_RESOLVER_ENABLED_GLOBAL,getDefaultString(params, CXSCA_RESOLVER_ENABLED_GLOBAL).trim());
config.put(CXSCA_RESOLVER_PATH_GLOBAL,getDefaultString(params, CXSCA_RESOLVER_PATH_GLOBAL).trim());
config.put(CXSCA_RESOLVER_ADD_PARAM_GLOBAL,getDefaultString(params, CXSCA_RESOLVER_ADD_PARAM_GLOBAL).trim());
config.put(CXSCA_RESOLVER_ENABLED_GLOBAL,getAdminConfig(CXSCA_RESOLVER_ENABLED_GLOBAL).trim());
config.put(CXSCA_RESOLVER_PATH_GLOBAL,getAdminConfig(CXSCA_RESOLVER_PATH_GLOBAL).trim());
config.put(CXSCA_RESOLVER_ADD_PARAM_GLOBAL,getAdminConfig(CXSCA_RESOLVER_ADD_PARAM_GLOBAL).trim());

}

Expand Down Expand Up @@ -664,12 +667,10 @@ public void validate(@NotNull final ActionParametersMap params, @NotNull final E
}
validateNotEmpty(params, errorCollection, PROJECT_NAME);
if(scaResolverEnabled && useGlobalSettings && enableDependancyScan){
validateNotEmpty(params, errorCollection, CXSCA_RESOLVER_PATH);
validateNotEmpty(params, errorCollection, CXSCA_RESOLVER_ADD_PARAM);
validateNotEmpty(params, errorCollection, CXSCA_RESOLVER_PATH);
}
if(scaResolverEnabledGlobal && !useGlobalSettings && enableDependancyScan){
validateNotEmpty(params, errorCollection, CXSCA_RESOLVER_PATH_GLOBAL);
validateNotEmpty(params, errorCollection, CXSCA_RESOLVER_ADD_PARAM_GLOBAL);
}
containsIllegals(params, errorCollection, PROJECT_NAME);
validateProjectNameLength(params, errorCollection, PROJECT_NAME);
Expand Down
31 changes: 30 additions & 1 deletion src/main/java/com/cx/plugin/configuration/CxGlobalConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ public class CxGlobalConfig extends GlobalAdminAction {
private String globalcxScaAccessControlServerUrl = DEFAULT_CXSCA_ACCESS_CONTROL_URL;
private String globalcxScaWebAppUrl = DEFAULT_CXSCA_WEB_APP_URL;
private String globalcxScaAccountName = "";

private String globalCxScaResolverEnabled;
private String globalCxScaResolverPath;
private String globalCxScaResolverAddParam;

private Map<String, String> globalDependencyScanTypeValues = ImmutableMap.of("OSA", "Use CxOSA dependency scanner", "AST_SCA", "Use CxSCA dependency scanner");

Expand Down Expand Up @@ -104,7 +108,11 @@ public String execute() {

globalcxScaUsername = adminConfig.getSystemProperty(GLOBAL_CXSCA_USERNAME);
globalcxScaPss = adminConfig.getSystemProperty(GLOBAL_CXSCA_PWD);


globalCxScaResolverEnabled = adminConfig.getSystemProperty(CXSCA_RESOLVER_ENABLED_GLOBAL);
globalCxScaResolverPath = adminConfig.getSystemProperty(CXSCA_RESOLVER_PATH_GLOBAL);
globalCxScaResolverAddParam = adminConfig.getSystemProperty(CXSCA_RESOLVER_ADD_PARAM_GLOBAL);

globalFolderExclusions = adminConfig.getSystemProperty(GLOBAL_FOLDER_EXCLUSION);
String filterProperty = adminConfig.getSystemProperty(GLOBAL_FILTER_PATTERN);
if (filterProperty != null) {
Expand Down Expand Up @@ -177,6 +185,10 @@ public String save() {
adminConfig.setSystemProperty(GLOBAL_CXSCA_USERNAME, globalcxScaUsername);
adminConfig.setSystemProperty(GLOBAL_CXSCA_PWD, encrypt(globalcxScaPss));

adminConfig.setSystemProperty(CXSCA_RESOLVER_ENABLED_GLOBAL, globalCxScaResolverEnabled);
adminConfig.setSystemProperty(CXSCA_RESOLVER_PATH_GLOBAL, globalCxScaResolverPath);
adminConfig.setSystemProperty(CXSCA_RESOLVER_ADD_PARAM_GLOBAL, globalCxScaResolverAddParam);

adminConfig.setSystemProperty(GLOBAL_FOLDER_EXCLUSION, globalFolderExclusions);
adminConfig.setSystemProperty(GLOBAL_FILTER_PATTERN, globalFilterPatterns);
adminConfig.setSystemProperty(GLOBAL_SCAN_TIMEOUT_IN_MINUTES, globalScanTimeoutInMinutes);
Expand Down Expand Up @@ -513,6 +525,23 @@ public void setGlobalcxScaAccountName(String globalcxScaAccountName) {
this.globalcxScaAccountName = globalcxScaAccountName;
}

public String getGlobalCxScaResolverEnabled() { return globalCxScaResolverEnabled; }

public void setGlobalCxScaResolverEnabled(String globalCxScaResolverEnabled) {
this.globalCxScaResolverEnabled = globalCxScaResolverEnabled;
}

public String getGlobalCxScaResolverPath() { return globalCxScaResolverPath; }

public void setGlobalCxScaResolverPath(String globalCxScaResolverPath) {
this.globalCxScaResolverPath = globalCxScaResolverPath;
}

public String getGlobalCxScaResolverAddParam() { return globalCxScaResolverAddParam; }

public void setGlobalCxScaResolverAddParam(String globalCxScaResolverAddParam) {
this.globalCxScaResolverAddParam = globalCxScaResolverAddParam;
}

public Map<String, String> getGlobalDependencyScanTypeValues() {
return globalDependencyScanTypeValues;
Expand Down
85 changes: 47 additions & 38 deletions src/main/java/com/cx/plugin/utils/CxConfigHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,11 +93,7 @@
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.*;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang3.SystemUtils;
Expand All @@ -113,6 +109,7 @@
import com.cx.restclient.dto.ProxyConfig;
import com.cx.restclient.dto.ScannerType;
import com.cx.restclient.exception.CxClientException;
import com.cx.restclient.sca.utils.CxSCAResolverUtils;

/**
* Created by Galn on 25/10/2017.
Expand All @@ -132,6 +129,8 @@ public class CxConfigHelper {
private boolean dependencyScanEnabled;
private ScannerType dependencyScanType;
private boolean effectiveIncrementalScan;
private static final String scaResolverResultPath = ".cxscaresolver" + File.separator + "sca";
private static final String scaResolverSastResultPath = ".cxscaresolver" + File.separator + "sast";

public boolean isEffectiveIncrementalScan() {
return effectiveIncrementalScan;
Expand Down Expand Up @@ -254,7 +253,11 @@ public CxScanConfig resolveConfigurationMap(ConfigurationMap configMap, File wor
scanConfig.setOsaFolderExclusions(configMap.get(DEPENDENCY_SCAN_FOLDER_EXCLUDE));
if(configMap.get(DEPENDENCY_SCAN_TYPE).equalsIgnoreCase(ScannerType.AST_SCA.toString())) {
scannerType = ScannerType.AST_SCA;
scanConfig.setAstScaConfig(getScaConfig(configMap, false));
try {
scanConfig.setAstScaConfig(getScaConfig(configMap, workDir, false));
} catch(ParseException e) {
throw new TaskException("Could not parse SCA additional arguments.", e);
}
}else {
scannerType = ScannerType.OSA;
scanConfig.setOsaArchiveIncludePatterns(configMap.get(OSA_ARCHIVE_INCLUDE_PATTERNS));
Expand All @@ -266,7 +269,11 @@ public CxScanConfig resolveConfigurationMap(ConfigurationMap configMap, File wor
scanConfig.setOsaFolderExclusions(getAdminConfig(GLOBAL_DEPENDENCY_SCAN_FOLDER_EXCLUDE));
if(getAdminConfig(GLOBAL_DEPENDENCY_SCAN_TYPE).equalsIgnoreCase(ScannerType.AST_SCA.toString())) {
scannerType = ScannerType.AST_SCA;
scanConfig.setAstScaConfig(getScaConfig(configMap, true));
try {
scanConfig.setAstScaConfig(getScaConfig(configMap, workDir, true));
} catch(ParseException e) {
throw new TaskException("Could not parse SCA additional arguments.", e);
}
}else {
scannerType = ScannerType.OSA;
scanConfig.setOsaArchiveIncludePatterns(getAdminConfig(GLOBAL_OSA_ARCHIVE_INCLUDE_PATTERNS));
Expand Down Expand Up @@ -416,7 +423,7 @@ private String getAdminConfig(String key) {
return StringUtils.defaultString(adminConfig.getSystemProperty(key));
}

private AstScaConfig getScaConfig(ConfigurationMap configMap, boolean fromGlobal) {
private AstScaConfig getScaConfig(ConfigurationMap configMap, File workDir, boolean fromGlobal) throws ParseException {
AstScaConfig result = new AstScaConfig();

if(fromGlobal) {
Expand All @@ -426,12 +433,10 @@ private AstScaConfig getScaConfig(ConfigurationMap configMap, boolean fromGlobal
result.setTenant(getAdminConfig(GLOBAL_CXSCA_ACCOUNT_NAME));
result.setUsername(getAdminConfig(GLOBAL_CXSCA_USERNAME));
result.setPassword(decrypt(getAdminConfig(GLOBAL_CXSCA_PWD)));
if(OPTION_TRUE.equalsIgnoreCase(configMap.get(CXSCA_RESOLVER_ENABLED_GLOBAL))) {
validateScaResolverParams(configMap.get(CXSCA_RESOLVER_ADD_PARAM_GLOBAL));
result.setPathToScaResolver(configMap.get(CXSCA_RESOLVER_PATH_GLOBAL));
result.setScaResolverAddParameters(configMap.get(CXSCA_RESOLVER_ADD_PARAM_GLOBAL));
if(OPTION_TRUE.equalsIgnoreCase(getAdminConfig(CXSCA_RESOLVER_ENABLED_GLOBAL))) {
result.setPathToScaResolver(getAdminConfig(CXSCA_RESOLVER_PATH_GLOBAL));
result.setScaResolverAddParameters(generateScaResolverParams(configMap, workDir, true));
result.setEnableScaResolver(true);

}


Expand All @@ -444,44 +449,48 @@ private AstScaConfig getScaConfig(ConfigurationMap configMap, boolean fromGlobal
result.setPassword(decrypt(configMap.get(CXSCA_PWD)));

if(OPTION_TRUE.equalsIgnoreCase(configMap.get(CXSCA_RESOLVER_ENABLED))) {
validateScaResolverParams(configMap.get(CXSCA_RESOLVER_ADD_PARAM));
result.setPathToScaResolver(configMap.get(CXSCA_RESOLVER_PATH));
result.setScaResolverAddParameters(configMap.get(CXSCA_RESOLVER_ADD_PARAM));
result.setScaResolverAddParameters(generateScaResolverParams(configMap, workDir, false));
result.setEnableScaResolver(true);

}


}

return result;
}


private static void validateScaResolverParams(String additionalParams) {

String[] arguments = additionalParams.split(" ");
private List<String> generateScaResolverParams(ConfigurationMap configMap, File workDir, boolean fromGlobal)
throws ParseException {
Map<String, String> params = new HashMap<>();

for (int i = 0; i < arguments.length ; i++) {
if(arguments[i].startsWith("-") && (i+1 != arguments.length && !arguments[i+1].startsWith("-")))
params.put(arguments[i], arguments[i+1]);
else
params.put(arguments[i], "");
/* Mandatory Parameters */
params.put("--resolver-result-path", workDir.getAbsolutePath() + File.separator + scaResolverResultPath);
params.put("--scan-path", workDir.getAbsolutePath());
params.put("--project-name", configMap.get(PROJECT_NAME).trim());
/* CxSAST Parameters */
params.put("--sast-result-path", workDir.getAbsolutePath() + File.separator + scaResolverSastResultPath);
params.put("--cxserver", fromGlobal ? getAdminConfig(GLOBAL_SERVER_URL) : configMap.get(SERVER_URL));
params.put("--cxuser", fromGlobal ? getAdminConfig(GLOBAL_USER_NAME) : configMap.get(USER_NAME));
params.put("--cxpassword", decrypt(fromGlobal ? getAdminConfig(GLOBAL_PWD) : configMap.get(PASSWORD)));
params.put("--cxprojectname", configMap.get(PROJECT_NAME).trim());
/* User additional arguments */
params.putAll(
CxSCAResolverUtils.parseArguments(
fromGlobal ? getAdminConfig(CXSCA_RESOLVER_ADD_PARAM_GLOBAL) : configMap.get(CXSCA_RESOLVER_ADD_PARAM)
)
);

List<String> resolved = new ArrayList<>();
for (Map.Entry<String, String> entry : params.entrySet()) {
if (entry.getValue() != null) {
resolved.add(entry.getKey());
resolved.add(entry.getValue());
} else {
resolved.add(entry.getKey());
}
}

String dirPath = params.get("-s");
if(StringUtils.isEmpty(dirPath))
throw new CxClientException("Source code path (-s <source code path>) is not provided.");

String projectName = params.get("-n");
if(StringUtils.isEmpty(projectName))
throw new CxClientException("Project name parameter (-n <project name>) must be provided to ScaResolver.");

String resultParam = params.get("-r");
if(StringUtils.isEmpty(resultParam))
throw new CxClientException("Result path parameter (-r <project name>) must be provided to ScaResolver.");

return resolved;
}

private static void fileExists(String file) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/resources/com/cx/plugin/cxGlobalConfig.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,12 @@
[@ww.password labelKey="cxScaGlobalPassword.label" name="globalcxScaPss" showPassword='true' /]
<button type="button" class="aui-button test-cxsca-connection" id="g_test-cxsca-connection" onclick="connectToScaServer()">Connect to Server</button>
<div id="gtestScaConnectionMessage" class="test-cxsca-connection-message"></div>
<br/>
[@ww.checkbox labelKey="cxScaResolverEnabled.label" name="globalCxScaResolverEnabled" id="globalCxScaResolverEnabled" toggle='true' /]
[@ui.bambooSection title='SCA Resolver' cssClass="cx" dependsOn="globalCxScaResolverEnabled" showOn="true"]
[@ww.textfield labelKey="cxScaResolverPath.label" name="globalCxScaResolverPath" id="globalCxScaResolverPath" descriptionKey="cxScaResolverPath.description" required='true'/]
[@ww.textarea labelKey="cxScaResolverAddParam.label" name="globalCxScaResolverAddParam" id="globalCxScaResolverAddParam" descriptionKey="cxScaResolverAddParam.description" rows="3" cssClass="long-field"/]
[/@ui.bambooSection]
[/@ui.bambooSection]
[/@ui.bambooSection]
[/@ui.bambooSection]
Expand Down
16 changes: 8 additions & 8 deletions src/main/resources/com/cx/plugin/editExampleTask.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@
[@ww.password labelKey="cxScaPassword.label" id="cxScaPassword" name="cxScaPassword" showPassword='true' required='true'/]
<button type="button" class="aui-button test-cxsca-connection" id="test-cxsca-connection">Connect to Server</button>
<div id="test-cxsca-connection-message" class="test-cxsca-connection-message"></div>

<br/>
[@ww.checkbox labelKey="cxScaResolverEnabled.label" name="cxScaResolverEnabled" id="cxScaResolverEnabled" toggle='true' /]
[@ui.bambooSection title='SCA Resolver' dependsOn='cxScaResolverEnabled' showOn='true' cssClass="cx"]
[@ww.textfield labelKey="cxScaResolverPath.label" name="cxScaResolverPath" id="cxScaResolverPath" descriptionKey="cxScaResolverPath.description" required='true'/]
[@ww.textarea labelKey="cxScaResolverAddParam.label" name="cxScaResolverAddParam" id="cxScaResolverAddParam" descriptionKey="cxScaResolverAddParam.description" required='true'/]
[@ww.textarea labelKey="cxScaResolverAddParam.label" name="cxScaResolverAddParam" id="cxScaResolverAddParam" descriptionKey="cxScaResolverAddParam.description" rows="3" cssClass="long-field"/]
[/@ui.bambooSection]
[/@ui.bambooSection]
[/@ui.bambooSection]
Expand Down Expand Up @@ -228,12 +228,12 @@

[@ww.label labelKey="cxScaUsername.label" name="globalcxScaUsername"/]
[@ww.label labelKey="cxScaPassword.label" type="password" /]

[@ww.checkbox labelKey="cxScaResolverEnabled.label" name="globalCxScaResolverEnabled" id="globalCxScaResolverEnabled" toggle='true' /]
[@ui.bambooSection title='SCA Resolver' dependsOn='globalCxScaResolverEnabled' showOn='true' cssClass="cx"]
[@ww.textfield labelKey="cxScaResolverPath.label" name="globalCxScaResolverPath" id="globalCxScaResolverPath" descriptionKey="cxScaResolverPath.description" required='true'/]
[@ww.textarea labelKey="cxScaResolverAddParam.label" name="globalCxScaResolverAddParam" id="globalCxScaResolverAddParam" descriptionKey="cxScaResolverAddParam.description" required='true'/]
[/@ui.bambooSection]
<br/>
[@ww.checkbox labelKey="cxScaResolverEnabled.label" name="globalCxScaResolverEnabled" id="globalCxScaResolverEnabled" toggle='true' disabled="true" /]
[@ui.bambooSection title='SCA Resolver' dependsOn='globalCxScaResolverEnabled' showOn='true' cssClass="cx"]
[@ww.label labelKey="cxScaResolverPath.label" name="globalCxScaResolverPath"/]
[@ww.label labelKey="cxScaResolverAddParam.label" name="globalCxScaResolverAddParam" rows="3" cssClass="long-field"/]
[/@ui.bambooSection]
[/@ui.bambooSection]
[/@ui.bambooSection]

Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/english.properties
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ cxScaResolverEnabled.description=Perform SCA scan using dependency resolution by
cxScaResolverPath.label=Path to SCA Resolver
cxScaResolverPath.description=Enter the absolute path where Sca Resolver tool is installed on the agent host.
cxScaResolverAddParam.label=SCA Resolver Additional
cxScaResolverAddParam.description=Provide command line parameters supported by the Sca Resolver tool. Mandatory parameters are -s,-n and -r.
cxScaResolverAddParam.description=Provide additional command line parameters supported by the Sca Resolver tool.
cxScaResolverManifest.label=Perform SCA scan by uploading manifests file(s)/source to SCA Service.
cxScaResolverManifest.description=Perform SCA scan by uploading manifests file(s)/source to SCA Service.
enableDependencyScan.label=Enable Dependency Scan
Expand Down