Skip to content

Commit

Permalink
Merge pull request #36 from Traceableai/test-suite-jenkins
Browse files Browse the repository at this point in the history
ENG-41076 : suite fields added and updated corresponding .sh file
  • Loading branch information
dhruv-singhal-github authored Feb 29, 2024
2 parents fcc5413 + 265287c commit f1696d0
Show file tree
Hide file tree
Showing 14 changed files with 544 additions and 194 deletions.
30 changes: 30 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Use the official Jenkins image from Docker Hub
FROM jenkins/jenkins:lts

# Switch to root to install Docker CLI
USER root

# Install Docker CLI to allow Jenkins to run Docker commands
RUN apt-get update && apt-get install -y apt-transport-https \
ca-certificates curl gnupg2 \
software-properties-common
RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add -
RUN add-apt-repository \
"deb [arch=amd64] https://download.docker.com/linux/debian \
$(lsb_release -cs) stable"
RUN apt-get update && apt-get install -y docker-ce-cli

# Switch back to the Jenkins user
USER jenkins

# Skip the initial setup wizard
ENV JAVA_OPTS -Djenkins.install.runSetupWizard=false

# Expose the default Jenkins port
EXPOSE 8080

# Expose the port for attached slave agents
EXPOSE 50000

# Start the Jenkins server
CMD ["jenkins.sh"]
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.28</version>
<scope>compile</scope>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.aspose</groupId>
Expand All @@ -100,7 +100,7 @@
<repository>
<id>snapshots</id>
<name>repo</name>
<url>http://repository.aspose.com/repo/</url>
<url>https://repository.aspose.com/repo/</url>
</repository>
</repositories>
<pluginRepositories>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package io.jenkins.plugins.traceable.ast;



import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import hudson.EnvVars;
Expand All @@ -11,13 +13,15 @@
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import io.jenkins.plugins.traceable.ast.scan.helper.Assets;
import io.jenkins.plugins.traceable.ast.scan.helper.TrafficType;
import jenkins.tasks.SimpleBuildStep;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Scanner;
Expand All @@ -30,8 +34,7 @@ public class TraceableASTInitAndRunStepBuilder extends Builder implements Simple
private String scanName;
private String testEnvironment;
private static String clientToken;
private String policyName;
private String scanEvalCriteria;
private String attackPolicy;
private String openApiSpecIds;
private String openApiSpecFiles;
private String postmanCollection;
Expand All @@ -48,13 +51,41 @@ public class TraceableASTInitAndRunStepBuilder extends Builder implements Simple
private String scanTimeout;
private static String scanId;
private static Boolean scanEnded;
private String referenceEnv;
private String maxRetries;
private static String traceableRootCaFileName;
private static String traceableCliCertFileName;
private static String traceableCliKeyFileName;
private String workspacePathString;

private String suiteName;

private String includeEndpointLabels;

private String includeEndpointIds;

private String includeServiceIds;


private String hookName;

private Assets assets;

private TrafficType trafficType;

private Boolean includeAllEndPoints;

private Boolean xastLive;

private Boolean xastReplay;

public Assets getAssets(){
return assets;
}

public TrafficType getTrafficType() {
return trafficType;
}

public String getScanName() { return scanName; }
public String getTestEnvironment() { return testEnvironment; }
public static String getClientToken() { return clientToken; }
Expand All @@ -68,13 +99,12 @@ public class TraceableASTInitAndRunStepBuilder extends Builder implements Simple
public String getScanTimeout() { return scanTimeout; }
public static String getScanId() { return scanId; }
public static Boolean getScanEnded() { return scanEnded; }
public String getReferenceEnv() { return referenceEnv; }

public String getMaxRetries() { return maxRetries; }
public static String getTraceableRootCaFileName() { return traceableRootCaFileName; }
public static String getTraceableCliCertFileName() { return traceableCliCertFileName; }
public static String getTraceableCliKeyFileName() { return traceableCliKeyFileName; }

public String getScanEvalCriteria() { return scanEvalCriteria; }

public String getPostmanEnvironment() { return postmanEnvironment; }

Expand All @@ -84,7 +114,7 @@ public class TraceableASTInitAndRunStepBuilder extends Builder implements Simple

public String getOpenApiSpecIds() { return openApiSpecIds; }

public String getPolicyName() { return policyName; }
public String getAttackPolicy() { return attackPolicy; }

public String getCliSource() {
return cliSource;
Expand All @@ -94,9 +124,25 @@ public String getCliField() {
return cliField;
}

@DataBoundConstructor
public TraceableASTInitAndRunStepBuilder() {
traceableCliBinaryLocation = null;
public String getSuiteName() {
return suiteName;
}

public String getIncludeEndpointLabels() {
return includeEndpointLabels;
}

public String getIncludeEndpointIds() {
return includeEndpointIds;
}

public String getIncludeServiceIds() {
return includeServiceIds;
}


public String getHookName() {
return hookName;
}


Expand Down Expand Up @@ -143,9 +189,6 @@ public static void setTraceableCliBinaryLocation(String traceableCliBinaryLocati
@DataBoundSetter
public void setScanTimeout(String scanTimeout) { this.scanTimeout = scanTimeout; }

@DataBoundSetter
public void setReferenceEnv(String referenceEnv) { this.referenceEnv = referenceEnv;}

@DataBoundSetter
public void setMaxRetries(String maxRetries) { this.maxRetries = maxRetries; }

Expand All @@ -164,36 +207,109 @@ public static void setTraceableCliKeyFileName(String traceableCliKeyFileName) {
TraceableASTInitAndRunStepBuilder.traceableCliKeyFileName = traceableCliKeyFileName;
}

@DataBoundSetter
public void setScanEvalCriteria(String scanEvalCriteria) {
this.scanEvalCriteria = scanEvalCriteria;
}

@DataBoundSetter
public void setPostmanEnvironment(String postmanEnvironment) {
this.postmanEnvironment = postmanEnvironment;
if(trafficType != TrafficType.DAST_POSTMAN_COLLECTION) {
this.postmanEnvironment = null;
}
}

@DataBoundSetter
public void setPostmanCollection(String postmanCollection) {
this.postmanCollection = postmanCollection;
if(trafficType != TrafficType.DAST_POSTMAN_COLLECTION) {
this.postmanCollection = null;
}
}

@DataBoundSetter
public void setOpenApiSpecIds(String openApiSpecIds) {
this.openApiSpecIds = openApiSpecIds;
if(trafficType != TrafficType.DAST_OPEN_API_SPECS) {
this.openApiSpecIds = null;
}
}

@DataBoundSetter
public void setOpenApiSpecFiles(String openApiSpecFiles) {
this.openApiSpecFiles = openApiSpecFiles;
if(trafficType != TrafficType.DAST_OPEN_API_SPECS) {
this.openApiSpecFiles = null;
}
}

@DataBoundSetter
public void setPolicyName(String policyName) {
this.policyName = policyName;
public void setAttackPolicy(String attackPolicy) {
this.attackPolicy = attackPolicy;
}

@DataBoundSetter
public void setSuiteName(String suiteName) {
this.suiteName = suiteName;
}

@DataBoundSetter
public void setIncludeEndpointLabels(String includeEndpointLabels) {
this.includeEndpointLabels = includeEndpointLabels;
if(assets != Assets.EndpointLabels) {
this.includeEndpointLabels = null;
}
}

@DataBoundSetter
public void setIncludeEndpointIds(String includeEndpointIds) {
this.includeEndpointIds = includeEndpointIds;
if(assets != Assets.EndpointIds) {
this.includeEndpointIds = null;
}
}

@DataBoundSetter
public void setIncludeServiceIds(String includeServiceIds) {
this.includeServiceIds = includeServiceIds;
if(assets != Assets.ServiceIds) {
this.includeServiceIds = null;
}
}

@DataBoundSetter
public void setHookName(String hookName) {
this.hookName = hookName;
}

@DataBoundSetter
public void setAssets(Assets assets) {
this.assets = assets;
if(assets != Assets.AllEndpoints) {
this.includeAllEndPoints = false;
}
}

@DataBoundSetter
public void setTrafficType(TrafficType trafficType) {
this.trafficType = trafficType;
switch (trafficType) {
case XAST_LIVE:
this.xastLive = true;
this.xastReplay = false;
break;
case XAST_REPLAY:
this.xastLive = false;
this.xastReplay = true;
break;
default:
this.xastLive = false;
this.xastReplay = false;
}
}

@DataBoundConstructor
public TraceableASTInitAndRunStepBuilder() {
traceableCliBinaryLocation = null;
this.includeAllEndPoints = true;
this.xastLive = true;
}

@Override
public void perform(Run<?, ?> run, FilePath workspace, EnvVars env, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
Expand Down Expand Up @@ -236,47 +352,39 @@ private void downloadTraceableCliBinary(TaskListener listener) {

// Run the scan.
private void runAndInitScan( TaskListener listener, Run<?, ?> run ){
String configFile= "scan:\n plugins:\n disabled: true\n custom:\n disabled: false\n " + scanEvalCriteria.replaceAll("\n","\n ");
Path configPath = null;
try {

// Creating an instance of file
configPath = Paths.get(workspacePathString , "/config.yaml");
byte[] arr = configFile.getBytes();

// Write the string to file
java.nio.file.Files.write(configPath, arr);
} catch (IOException e) {
log.error("Error writing to config.yaml the config: {}", configFile);
throw new RuntimeException(e);
}

String replay = String.valueOf(xastReplay != null && xastReplay);
String allEndPoint = String.valueOf(includeAllEndPoints != null && includeAllEndPoints);
String scriptPath = "shell_scripts/run_and_init_ast_scan.sh";
String[] args =
new String[] {
traceableCliBinaryLocation,
traceableRootCaFileName,
traceableCliCertFileName,
traceableCliKeyFileName,
scanName,
testEnvironment,
clientToken,
policyName,
attackPolicy,
pluginsToInclude,
includeUrlRegex,
excludeUrlRegex,
targetUrl,
traceableServer,
idleTimeout,
scanTimeout,
run.getId(),
run.getUrl(),
referenceEnv,
maxRetries,
openApiSpecIds,
openApiSpecFiles,
postmanCollection,
postmanEnvironment,
traceableRootCaFileName,
traceableCliCertFileName,
traceableCliKeyFileName,
configPath.toString()
suiteName,
includeServiceIds,
includeEndpointIds,
includeEndpointLabels,
hookName,
allEndPoint,
replay
};
runScript(scriptPath, args, listener, "runAndInitScan");
}
Expand Down Expand Up @@ -304,6 +412,7 @@ private void runScript(String scriptPath, String[] args, TaskListener listener,
x.close();
String execScript = new StringBuffer().append("/bin/bash ").append(tempFile.getAbsolutePath()).toString();
for(int i=0;i<args.length;i++) {
if(!StringUtils.isEmpty(args[i])) args[i] = args[i].replace(" ","");
if(args[i]!=null && !args[i].equals(""))
execScript = new StringBuffer().append(execScript).append(" ").append(args[i]).toString();
else execScript = new StringBuffer().append(execScript).append(" ''").toString();
Expand Down Expand Up @@ -348,6 +457,7 @@ private void logOutput(InputStream inputStream, String prefix, TaskListener list
.start();
}


@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Builder> {

Expand Down
Loading

0 comments on commit f1696d0

Please sign in to comment.