Skip to content

Commit

Permalink
Moved the common validation method to the base scanner class
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalhcl-5960 committed Dec 6, 2024
1 parent 193d928 commit 66a62fb
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ private Map<String, String> getScanProperties(Run<?,?> build, TaskListener liste
Map<String, String> properties = m_scanner.getProperties(resolver);
properties.put(CoreConstants.SCANNER_TYPE, m_scanner.getType());
properties.put(CoreConstants.APP_ID, m_application);
properties.put(CoreConstants.SCAN_NAME, (resolver == null ? m_name : Util.replaceMacro(m_name, resolver)) + "_" + SystemUtil.getTimeStamp()); //$NON-NLS-1$
properties.put(CoreConstants.SCAN_NAME, resolver == null ? m_name : Util.replaceMacro(m_name, resolver) + "_" + SystemUtil.getTimeStamp()); //$NON-NLS-1$
properties.put(CoreConstants.EMAIL_NOTIFICATION, Boolean.toString(m_emailNotification));
properties.put(CoreConstants.PERSONAL_SCAN, Boolean.toString(m_personalScan));
properties.put("FullyAutomatic", Boolean.toString(!m_intervention));
Expand Down Expand Up @@ -308,22 +308,6 @@ private void shouldFailBuild(IResultsProvider provider,Run<?,?> build) throws Ab
throw new AbortException(Messages.error_checking_results(provider.getStatus()));
}
}

private void validateGeneralSettings(boolean isAppScan360, Map<String, String> properties, IProgress progress) throws IOException {
if (isAppScan360) {
if (m_intervention) {
progress.setStatus(new Message(Message.WARNING, Messages.warning_allow_intervention_AppScan360()));
}
} else if (m_authProvider.getacceptInvalidCerts()) {
progress.setStatus(new Message(Message.WARNING, Messages.warning_asoc_certificates()));
}

if (properties.containsKey(CoreConstants.OPEN_SOURCE_ONLY)) {
progress.setStatus(new Message(Message.WARNING, Messages.warning_sca()));
m_scanner = ScannerFactory.getScanner(Scanner.SOFTWARE_COMPOSITION_ANALYZER, properties.get(CoreConstants.TARGET));
properties.put(CoreConstants.SCANNER_TYPE, CoreConstants.SOFTWARE_COMPOSITION_ANALYZER);
}
}

private void perform(Run<?,?> build, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
m_authProvider = new JenkinsAuthenticationProvider(m_credentials, build.getParent().getParent());
Expand All @@ -332,8 +316,13 @@ private void perform(Run<?,?> build, Launcher launcher, TaskListener listener) t
Map<String, String> properties = getScanProperties(build,listener);
boolean isAppScan360 = ((JenkinsAuthenticationProvider) m_authProvider).isAppScan360();

m_scanner.validateSettings((JenkinsAuthenticationProvider) m_authProvider,properties, progress);
validateGeneralSettings(isAppScan360,properties,progress);
m_scanner.validateSettings((JenkinsAuthenticationProvider) m_authProvider,properties, progress, isAppScan360);

if (properties.containsKey(CoreConstants.OPEN_SOURCE_ONLY)) {
progress.setStatus(new Message(Message.WARNING, Messages.warning_sca()));
m_scanner = ScannerFactory.getScanner(Scanner.SOFTWARE_COMPOSITION_ANALYZER, properties.get(CoreConstants.TARGET));
properties.put(CoreConstants.SCANNER_TYPE, CoreConstants.SOFTWARE_COMPOSITION_ANALYZER);
}


final IScan scan = ScanFactory.createScan(properties, progress, m_authProvider);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@
import java.util.Map;

import com.hcl.appscan.sdk.CoreConstants;
import com.hcl.appscan.sdk.app.CloudApplicationProvider;
import com.hcl.appscan.sdk.logging.IProgress;
import com.hcl.appscan.sdk.scan.CloudScanServiceProvider;
import com.hcl.appscan.sdk.scan.IScanServiceProvider;
import org.apache.wink.json4j.JSONArray;
import org.apache.wink.json4j.JSONException;
import org.apache.wink.json4j.JSONObject;
Expand Down Expand Up @@ -250,17 +248,17 @@ public String upgradeLoginScenario(){
}
}

public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress) throws IOException {
public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress, boolean isAppScan360) throws IOException {
if(!ServiceUtil.hasDastEntitlement(authProvider)) {
throw new AbortException(Messages.error_active_subscription_validation(getType()));
}
if(getRescanDast()) {
if(getRescanDast()) {
if(!properties.containsKey(CoreConstants.SCAN_ID)) {
throw new AbortException(Messages.error_empty_scan_id());
} else if (m_incrementalScan && !properties.containsKey("IncrementalBaseJobId")) {
throw new AbortException(Messages.error_empty_execution_id());
}
}
}
if (authProvider.isAppScan360()) {
if (properties.containsKey(Scanner.PRESENCE_ID)) {
throw new AbortException(Messages.error_presence_AppScan360());
Expand All @@ -273,8 +271,16 @@ public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<Str
if (!getRescanDast() && !authProvider.isAppScan360() && !properties.containsKey(Scanner.PRESENCE_ID) && !ServiceUtil.isValidUrl(properties.get(TARGET), authProvider, authProvider.getProxy())) {
throw new AbortException(Messages.error_url_validation(properties.get(TARGET)));
}
validations(authProvider, properties, progress);
}
validateGeneralSettings(authProvider, properties, progress, isAppScan360);
if(properties.containsKey(CoreConstants.SCAN_ID)) {
try {
JSONObject scanDetails = ServiceUtil.getScanDetails(DYNAMIC_ANALYZER, properties.get(CoreConstants.SCAN_ID), authProvider);
scanIdValidation(scanDetails, properties);
} catch (JSONException e) {
//Ignore and move on.
}
}
}

@Override
public Map<String, String> getProperties(VariableResolver<String> resolver) throws AbortException {
Expand Down Expand Up @@ -364,9 +370,8 @@ public String getDisplayName() {

public ListBoxModel doFillExecutionIdItems(@RelativePath("..") @QueryParameter String credentials, @AncestorInPath ItemGroup<?> context, @QueryParameter String scanId) throws JSONException {
IAuthenticationProvider authProvider = new JenkinsAuthenticationProvider(credentials, context);
JSONArray executionDetails = new CloudScanServiceProvider(null, authProvider).getBaseScanDetails(scanId, authProvider);
JSONArray executionDetails = new CloudScanServiceProvider(authProvider).getBaseScanDetails(scanId, authProvider);
ListBoxModel model = new ListBoxModel();

if(executionDetails != null) {
for(int i = 0; i < executionDetails.length(); i++) {
JSONObject value = executionDetails.getJSONObject(i);
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/com/hcl/appscan/jenkins/plugin/scanners/Scanner.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public String getTarget() {

public abstract Map<String, String> getProperties(VariableResolver<String> resolver) throws AbortException;

public abstract void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress) throws IOException;
public abstract void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress, boolean isAppScan360) throws IOException;

public abstract String getType();

Expand All @@ -66,21 +66,23 @@ protected String resolvePath(String path, VariableResolver<String> resolver) {

return path;
}
protected void validations(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress) throws IOException {
protected void validateGeneralSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress, boolean isAppScan360) throws IOException {
if (isAppScan360) {
if (!properties.get("FullyAutomatic").equals("true")) {
progress.setStatus(new Message(Message.WARNING, Messages.warning_allow_intervention_AppScan360()));
}
} else if (authProvider.getacceptInvalidCerts()) {
progress.setStatus(new Message(Message.WARNING, Messages.warning_asoc_certificates()));
}

if(properties.containsKey(CoreConstants.SCAN_ID)) {
if(properties.get(CoreConstants.PERSONAL_SCAN).equals("true")) {
progress.setStatus(new Message(Message.WARNING, Messages.warning_personal_scan_rescan()));
}
try {
scanIdValidation(authProvider, properties,progress);
} catch (JSONException e) {
//Ignore and move on.
}
}
}

protected void scanIdValidation(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress) throws JSONException, IOException {
JSONObject scanDetails = ServiceUtil.getScanDetails(properties.get(CoreConstants.SCANNER_TYPE), properties.get(CoreConstants.SCAN_ID), authProvider);
protected void scanIdValidation(JSONObject scanDetails, Map<String, String> properties) throws JSONException, IOException {
if(scanDetails == null) {
throw new AbortException(Messages.error_invalid_scan_id());
} else {
Expand All @@ -89,8 +91,6 @@ protected void scanIdValidation(JenkinsAuthenticationProvider authProvider, Map<
throw new AbortException(Messages.error_scan_id_validation_status(status));
} else if (!scanDetails.get("RescanAllowed").equals(true) && scanDetails.get("ParsedFromUploadedFile").equals(true)) {
throw new AbortException(Messages.error_scan_id_validation_rescan_allowed());
} else if (properties.get(CoreConstants.SCANNER_TYPE).equals(Scanner.STATIC_ANALYZER) && scanDetails.containsKey("GitRepoPlatform") && scanDetails.get("GitRepoPlatform")!=null) {
throw new AbortException(Messages.error_invalid_scan_id_git_repo());
} else if (!scanDetails.get(CoreConstants.APP_ID).equals(properties.get(CoreConstants.APP_ID))) {
throw new AbortException(Messages.error_invalid_scan_id_application());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,14 +74,22 @@ public String getScanId() {
return m_scanId;
}

public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress) throws IOException {
public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress, boolean isAppScan360) throws IOException {
if(!ServiceUtil.hasScaEntitlement(authProvider)) {
throw new AbortException(Messages.error_active_subscription_validation(getType()));
}
if (authProvider.isAppScan360()) {
throw new AbortException(Messages.error_sca_AppScan360());
}
validations(authProvider, properties, progress);
validateGeneralSettings(authProvider, properties, progress, isAppScan360);
if(properties.containsKey(CoreConstants.SCAN_ID)) {
try {
JSONObject scanDetails = ServiceUtil.getScanDetails(SOFTWARE_COMPOSITION_ANALYZER, properties.get(CoreConstants.SCAN_ID), authProvider);
scanIdValidation(scanDetails, properties);
} catch (JSONException e) {
//Ignore and move on.
}
}
}

public Map<String, String> getProperties(VariableResolver<String> resolver) throws AbortException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ public boolean isScanMethod(String scanMethod) {
return m_scanMethod.equals(scanMethod);
}

public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress) throws IOException {
public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<String, String> properties, IProgress progress, boolean isAppScan360) throws IOException {
if(!ServiceUtil.hasSastEntitlement(authProvider)) {
throw new AbortException(Messages.error_active_subscription_validation(getType()));
}
Expand Down Expand Up @@ -242,7 +242,18 @@ public void validateSettings(JenkinsAuthenticationProvider authProvider, Map<Str
if (properties.containsKey(CoreConstants.INCLUDE_SCA) && properties.containsKey(CoreConstants.UPLOAD_DIRECT) && !properties.get(TARGET).endsWith(".irx")) {
throw new AbortException(Messages.error_invalid_format_include_sca());
}
validations(authProvider,properties,progress);
validateGeneralSettings(authProvider, properties, progress, isAppScan360);
if(properties.containsKey(CoreConstants.SCAN_ID)) {
try {
JSONObject scanDetails = ServiceUtil.getScanDetails(STATIC_ANALYZER, properties.get(CoreConstants.SCAN_ID), authProvider);
if(scanDetails!=null && scanDetails.containsKey("GitRepoPlatform") && scanDetails.get("GitRepoPlatform")!=null) {
throw new AbortException(Messages.error_invalid_scan_id_git_repo());
}
scanIdValidation(scanDetails, properties);
} catch (JSONException e) {
//Ignore and move on.
}
}
}

@Override
Expand Down

0 comments on commit 66a62fb

Please sign in to comment.