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

Distributed scan run #62

Merged
merged 8 commits into from
Aug 12, 2024
Merged
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
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
package io.jenkins.plugins.traceable.ast;

import com.google.common.base.Charsets;
import com.google.common.io.CharStreams;
import com.google.common.io.Files;
import hudson.EnvVars;
import hudson.Extension;
import hudson.FilePath;
Expand All @@ -12,11 +9,9 @@
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import java.io.*;
import java.util.Scanner;
import java.util.UUID;
import io.jenkins.plugins.traceable.ast.scan.utils.RunScript;
import java.io.IOException;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;

Expand Down Expand Up @@ -58,15 +53,15 @@ public static void setScanId(String scanId) {
@Override
public void perform(Run<?, ?> run, FilePath workspace, EnvVars env, Launcher launcher, TaskListener listener)
throws InterruptedException, IOException {
runScan(listener, run);
runScan(run, workspace, listener);
if (scanId != null) {
abortScan(listener);
abortScan(workspace, listener);
}
TraceableASTInitStepBuilder.setScanEnded(true);
}

// Run the scan.
private void runScan(TaskListener listener, Run<?, ?> run) {
private void runScan(Run<?, ?> run, FilePath workspace, TaskListener listener) {
String scriptPath = "shell_scripts/run_ast_scan.sh";
String[] args = new String[] {
TraceableASTInitStepBuilder.getTraceableCliBinaryLocation(),
Expand All @@ -77,75 +72,33 @@ private void runScan(TaskListener listener, Run<?, ?> run) {
TraceableASTInitStepBuilder.getTraceableCliCertFileName(),
TraceableASTInitStepBuilder.getTraceableCliKeyFileName()
};
runScript(scriptPath, args, listener, "runScan");
runScript(workspace, listener, scriptPath, args, "runScan");
}

// Stop the scan with the given scan ID.
private void abortScan(TaskListener listener) {
private void abortScan(FilePath workspace, TaskListener listener) {
String scriptPath = "shell_scripts/stop_ast_scan.sh";
String[] args = new String[] {TraceableASTInitStepBuilder.getTraceableCliBinaryLocation(), scanId};
runScript(scriptPath, args, listener, "abortScan");
runScript(workspace, listener, scriptPath, args, "abortScan");
}

private void runScript(String scriptPath, String[] args, TaskListener listener, String caller) {
private void runScript(FilePath workspace, TaskListener listener, String scriptPath, String[] args, String caller) {
try {
// Read the bundled script as string
String bundledScript = CharStreams.toString(
new InputStreamReader(getClass().getResourceAsStream(scriptPath), Charsets.UTF_8));
// Create a temp file with uuid appended to the name just to be safe
File tempFile = File.createTempFile("script_" + UUID.randomUUID().toString(), ".sh");
// Write the string to temp file
BufferedWriter x = Files.newWriter(tempFile, Charsets.UTF_8);
x.write(bundledScript);
x.close();
StringBuilder execScript = new StringBuilder("/bin/bash " + tempFile.getAbsolutePath());
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.append(" ").append(args[i]);
else execScript.append(" ''");
}
ProcessBuilder processBuilder = new ProcessBuilder(execScript.toString());
Process pb = processBuilder.start();
logOutput(pb.getInputStream(), "", listener, caller);
logOutput(pb.getErrorStream(), "Error: ", listener, caller);
pb.waitFor();
boolean deleted_temp = tempFile.delete();
if (!deleted_temp) {
throw new FileNotFoundException("Temp file not found");
}

String returnValue = workspace.act(new RunScript(listener, scriptPath, args, caller));

if (caller.equals("runScan")) {
TraceableASTRunStepBuilder.setScanId(returnValue);
}
} catch (Exception e) {
e.printStackTrace();
}
}

private void logOutput(InputStream inputStream, String prefix, TaskListener listener, String caller) {
new Thread(() -> {
Scanner scanner = new Scanner(inputStream, "UTF-8");
while (scanner.hasNextLine()) {
synchronized (this) {
String line = scanner.nextLine();

// Extract the scan ID from the cli output of scan init command.
if (prefix.equals("") && line.contains("Running scan with ID")) {
String[] tokens = line.split(" ");
setScanId(tokens[tokens.length - 1].substring(0, 36));
}
if (!caller.equals("abortScan")) {
listener.getLogger().println(prefix + line);
}
}
}
scanner.close();
})
.start();
}

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

private String STEP_NAME = "Traceable AST - Run";
private final String STEP_NAME = "Traceable AST - Run";

@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ public String invoke(File f, VirtualChannel channel) throws IOException, Interru
copyScript();
runScript();
deleteScript();

return RunScript.scanId;
}

Expand Down
Loading