-
Notifications
You must be signed in to change notification settings - Fork 0
/
TraceableASTRunStepBuilder.java
159 lines (138 loc) · 5.72 KB
/
TraceableASTRunStepBuilder.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
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;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import java.io.*;
import java.util.Scanner;
import java.util.UUID;
import jenkins.tasks.SimpleBuildStep;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
public class TraceableASTRunStepBuilder extends Builder implements SimpleBuildStep {
private String idleTimeout;
private String maxRetries;
private static String scanId;
public String getIdleTimeout() {
return idleTimeout;
}
public String getMaxRetries() {
return maxRetries;
}
public static String getScanId() {
return scanId;
}
@DataBoundConstructor
public TraceableASTRunStepBuilder() {}
@DataBoundSetter
public void setIdleTimeout(String idleTimeout) {
this.idleTimeout = idleTimeout;
}
@DataBoundSetter
public void setMaxRetries(String maxRetries) {
this.maxRetries = maxRetries;
}
public static void setScanId(String scanId) {
TraceableASTRunStepBuilder.scanId = scanId;
}
@Override
public void perform(Run<?, ?> run, FilePath workspace, EnvVars env, Launcher launcher, TaskListener listener)
throws InterruptedException, IOException {
runScan(listener, run);
if (scanId != null) {
abortScan(listener);
}
TraceableASTInitStepBuilder.setScanEnded(true);
}
// Run the scan.
private void runScan(TaskListener listener, Run<?, ?> run) {
String scriptPath = "shell_scripts/run_ast_scan.sh";
String[] args = new String[] {
TraceableASTInitStepBuilder.getTraceableCliBinaryLocation(),
TraceableASTInitStepBuilder.getClientToken(),
idleTimeout,
maxRetries,
TraceableASTInitStepBuilder.getTraceableRootCaFileName(),
TraceableASTInitStepBuilder.getTraceableCliCertFileName(),
TraceableASTInitStepBuilder.getTraceableCliKeyFileName()
};
runScript(scriptPath, args, listener, "runScan");
}
// Stop the scan with the given scan ID.
private void abortScan(TaskListener listener) {
String scriptPath = "shell_scripts/stop_ast_scan.sh";
String[] args = new String[] {TraceableASTInitStepBuilder.getTraceableCliBinaryLocation(), scanId};
runScript(scriptPath, args, listener, "abortScan");
}
private void runScript(String scriptPath, String[] args, TaskListener listener, 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(" ''");
}
Process pb = Runtime.getRuntime().exec(execScript.toString());
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");
}
} 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";
@Override
public boolean isApplicable(Class<? extends AbstractProject> aClass) {
return true;
}
@Override
public String getDisplayName() {
return STEP_NAME;
}
}
}