Skip to content

Commit

Permalink
Add JDK's choice
Browse files Browse the repository at this point in the history
  • Loading branch information
lroucoux committed May 13, 2015
1 parent 101485e commit 67d1a3b
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 13 deletions.
76 changes: 71 additions & 5 deletions src/main/java/fr/novia/zaproxyplugin/ZAProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
import hudson.model.BuildListener;
import hudson.model.EnvironmentSpecific;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Computer;
import hudson.model.Descriptor;
import hudson.model.JDK;
import hudson.model.Node;
import hudson.remoting.VirtualChannel;
import hudson.slaves.NodeSpecific;
Expand All @@ -44,6 +47,7 @@
import hudson.tools.ToolInstallation;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
Expand Down Expand Up @@ -71,10 +75,21 @@
import java.net.Proxy;
import java.net.Socket;
import java.net.SocketTimeoutException;
import java.net.URI;
import java.net.URL;
import java.nio.file.FileSystem;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.WatchKey;
import java.nio.file.WatchService;
import java.nio.file.WatchEvent.Kind;
import java.nio.file.WatchEvent.Modifier;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -169,14 +184,17 @@ public class ZAProxy extends AbstractDescribableImpl<ZAProxy> implements Seriali
/** List of all ZAP command lines specified by the user */
private final List<ZAPcmdLine> cmdLinesZAP;

private final String jdk;

// Fields in fr/novia/zaproxyplugin/ZAProxy/config.jelly must match the parameter names in the "DataBoundConstructor"
@DataBoundConstructor
public ZAProxy(boolean autoInstall, String toolUsed, String zapHome, int timeoutInSec,
String filenameLoadSession, String targetURL, boolean spiderURL, boolean scanURL,
boolean saveReports, List<String> chosenFormats, String filenameReports,
boolean saveSession, String filenameSaveSession,
String zapDefaultDir, String chosenPolicy,
List<ZAPcmdLine> cmdLinesZAP) {
List<ZAPcmdLine> cmdLinesZAP,
String jdk) {

this.autoInstall = autoInstall;
this.toolUsed = toolUsed;
Expand All @@ -194,6 +212,8 @@ public ZAProxy(boolean autoInstall, String toolUsed, String zapHome, int timeout
this.zapDefaultDir = zapDefaultDir;
this.chosenPolicy = chosenPolicy;
this.cmdLinesZAP = cmdLinesZAP != null ? new ArrayList<ZAPcmdLine>(cmdLinesZAP) : Collections.<ZAPcmdLine>emptyList();

this.jdk = jdk;
System.out.println(this.toString());
}

Expand All @@ -219,6 +239,8 @@ public String toString() {
s += "zapProxyHost ["+zapProxyHost+"]\n";
s += "zapProxyPort ["+zapProxyPort+"]\n";

s+= "jdk ["+jdk+"]";

return s;
}

Expand Down Expand Up @@ -304,6 +326,17 @@ public void setZapProxyPort(int zapProxyPort) {
public List<ZAPcmdLine> getCmdLinesZAP() {
return cmdLinesZAP;
}

/**
* Gets the JDK that this Sonar builder is configured with, or null.
*/
public JDK getJDK() {
return Jenkins.getInstance().getJDK(jdk);
}

public String getJdk() {
return jdk;
}

/**
* Get the ZAP_HOME setup by Custom Tools Plugin or already present on the build's machine.
Expand Down Expand Up @@ -494,6 +527,10 @@ public void startZAP(AbstractBuild<?, ?> build, BuildListener listener, Launcher
envVars.put(e.getKey(),e.getValue());

FilePath workDir = new FilePath(ws.getChannel(), zapProgram);
listener.getLogger().println("ws.getChannel() = " + ws.getChannel());

// Java
computeJdkToUse(build, listener, envVars);

// Launch ZAP process on remote machine (on master if no remote machine)
launcher.launch().cmds(cmd).envs(envVars).stdout(listener).pwd(workDir).start();
Expand All @@ -503,9 +540,35 @@ public void startZAP(AbstractBuild<?, ?> build, BuildListener listener, Launcher

} catch (IOException e) {
e.printStackTrace();
listener.error(e.toString());
}
}

private void computeJdkToUse(AbstractBuild<?, ?> build,
BuildListener listener, EnvVars env) throws IOException,
InterruptedException {
JDK jdkToUse = getJdkToUse(build.getProject());
if (jdkToUse != null) {
Computer computer = Computer.currentComputer();
// just in case we are not in a build
if (computer != null) {
jdkToUse = jdkToUse.forNode(computer.getNode(), listener);
}
jdkToUse.buildEnvVars(env);
}
}

/**
* @return JDK to be used with this project.
*/
private JDK getJdkToUse(AbstractProject<?, ?> project) {
JDK jdkToUse = getJDK();
if (jdkToUse == null) {
jdkToUse = project.getJDK();
}
return jdkToUse;
}

/**
* Add list of command line in the list in param
* @param l the list to attach ZAP command line
Expand Down Expand Up @@ -571,6 +634,7 @@ private void waitForSuccessfulConnectionToZap(int timeout, BuildListener listene
socket.close();
} catch (IOException e) {
e.printStackTrace();
listener.error(e.toString());
}
}
}
Expand Down Expand Up @@ -1053,11 +1117,12 @@ public PolicyFileCallable(String zapDefaultDir) {
}

public File[] invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {

File zapDir = new File(zapDefaultDir, NAME_POLICIES_DIR_ZAP);
File[] listFiles = {};

if(zapDir.exists()) {
Path pathPolicyDir = Paths.get(zapDefaultDir, NAME_POLICIES_DIR_ZAP);

if(Files.isDirectory(pathPolicyDir)) {
File zapPolicyDir = new File(zapDefaultDir, NAME_POLICIES_DIR_ZAP);
// create new filename filter (get only file with FILE_POLICY_EXTENSION extension)
FilenameFilter policyFilter = new FilenameFilter() {

Expand All @@ -1080,7 +1145,7 @@ public boolean accept(File dir, String name) {
};

// returns pathnames for files and directory
listFiles = zapDir.listFiles(policyFilter);
listFiles = zapPolicyDir.listFiles(policyFilter);
}
return listFiles;
}
Expand Down Expand Up @@ -1115,6 +1180,7 @@ public Void invoke(File f, VirtualChannel channel) throws IOException,
zaproxy.waitForSuccessfulConnectionToZap(zaproxy.timeoutInSec, listener);
} catch (Exception e) {
e.printStackTrace();
listener.error(e.toString());
}
return null;
}
Expand Down
65 changes: 57 additions & 8 deletions src/main/java/fr/novia/zaproxyplugin/ZAProxyBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,28 +24,33 @@

package fr.novia.zaproxyplugin;

import hudson.Extension;
import hudson.FilePath;
import hudson.FilePath.FileCallable;
import hudson.remoting.VirtualChannel;
import org.jenkinsci.remoting.RoleChecker;

import java.io.File;

import hudson.model.Node;
import hudson.slaves.SlaveComputer;
import hudson.Extension;
import hudson.Launcher;
import hudson.Launcher.LocalLauncher;
import hudson.Launcher.RemoteLauncher;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Node;
import hudson.remoting.VirtualChannel;
import hudson.slaves.SlaveComputer;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import net.sf.json.JSONObject;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

/**
* /!\
* Au jour du 27/03/2015
Expand Down Expand Up @@ -148,6 +153,7 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen

boolean res;
try {
//copyFile(build.getWorkspace(), listener);
res = build.getWorkspace().act(new ZAProxyCallable(this.zaproxy, listener));
} catch (Exception e) {
e.printStackTrace();
Expand All @@ -157,6 +163,49 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
return res;
}

private void copyFile(FilePath workspace, BuildListener listener) throws IOException, InterruptedException {
//if(zaproxy.getScanURL() && !zaproxy.pathToLocalPolicy.isEmpty() && zaproxy.pathToLocalPolicy != null)
// TODO a recup via un champ
// File fileToCopy = new File(zaproxy.pathToLocalPolicy);
File fileToCopy = new File("C:\\Users\\ludovic.roucoux\\OWASP ZAP\\policies\\OnlySQLInjection.policy");

String stringForLogger = "Copy [" + fileToCopy.getAbsolutePath() + "] to ";

String data = FileUtils.readFileToString(fileToCopy, (String)null);

stringForLogger = workspace.act(new CopyFileCallable(data, zaproxy.getZapDefaultDir(),
fileToCopy.getName(), stringForLogger));
listener.getLogger().println(stringForLogger);
}

private static class CopyFileCallable implements FileCallable<String> {
private static final long serialVersionUID = -3375349701206827354L;
private String data;
private String zapDefaultDir;
private String copyFilename;
private String stringForLogger;

public CopyFileCallable(String data, String zapDefaultDir,
String copyFilename, String stringForLogger) {
this.data = data;
this.zapDefaultDir = zapDefaultDir;
this.copyFilename = copyFilename;
this.stringForLogger = stringForLogger;
}

public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
File fileCopiedDir = new File(zapDefaultDir, ZAProxy.NAME_POLICIES_DIR_ZAP);
File fileCopied = new File(fileCopiedDir, copyFilename);

FileUtils.writeStringToFile(fileCopied, data);
stringForLogger += "[" + fileCopied.getAbsolutePath() + "]";
return stringForLogger;
}

@Override
public void checkRoles(RoleChecker checker) throws SecurityException {}

This comment has been minimized.

Copy link
@jglick

jglick Aug 3, 2015

Member

Probably wrong. See jenkinsci/remoting#56.

}



/**
Expand Down
11 changes: 11 additions & 0 deletions src/main/resources/fr/novia/zaproxyplugin/ZAProxy/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,17 @@ SOFTWARE.
See global.jelly for a general discussion about jelly script.
-->

<!-- JDK -->
<j:set var="jdks" value="${app.JDKs}" />
<f:entry title="JDK" field="jdk">
<select class="setting-input validated" name="jdk" checkUrl="'${rootURL}/defaultJDKCheck?value='+this.value">
<option>${%InheritFromJob}</option>
<j:forEach var="inst" items="${jdks}">
<f:option selected="${inst.name==instance.JDK.name}" value="${inst.name}">${inst.name}</f:option>
</j:forEach>
</select>
</f:entry>

<f:entry title="Timeout for ZAProxy initialization" field="timeoutInSec"
description="Enter a value in seconde">
<f:number default="60" clazz="required positive-number" />
Expand Down

0 comments on commit 67d1a3b

Please sign in to comment.