Skip to content

Commit

Permalink
Merge pull request #19 from young-s-park/get-executable-fix
Browse files Browse the repository at this point in the history
A fix for locating ScanCentral executable in Path
  • Loading branch information
akaryakina authored Jun 23, 2020
2 parents c9922d2 + 6b2f9cb commit d7c8a74
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* (c) Copyright 2019 Micro Focus or one of its affiliates.
* (c) Copyright 2020 Micro Focus or one of its affiliates.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -24,10 +24,10 @@
import hudson.remoting.VirtualChannel;

public class FindExecutableRemoteService implements FilePath.FileCallable<String> {
private String filename;
private final String filename;
private String home;
private String path;
private FilePath workspace;
private final FilePath workspace;

/**
* Searches the PATH on the remote machine
Expand Down Expand Up @@ -62,6 +62,10 @@ public String invoke(File file, VirtualChannel channel) throws IOException {
return s;
}
}
// if no path is passed in, get the system path
if (path == null) {
path = System.getenv("PATH");
}
File f = PathUtils.locateFileInPath(filename, path);
if (f != null) {
return f.getPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.kohsuke.stapler.*;

import javax.annotation.Nonnull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -49,7 +50,13 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
PrintStream log = taskListener.getLogger();
log.println("Fortify Jenkins plugin v " + VERSION);
log.println("Launching Fortify scancentral arguments command");
String cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
String cloudscanExec;
try {
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
} catch (FileNotFoundException ex) {
log.println("WARNING: Cannot find scancentral executable");
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener);
}

EnvVars vars = run.getEnvironment(taskListener);
ArrayList<String> args = new ArrayList<String>(2);
Expand All @@ -70,11 +77,11 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
Launcher.ProcStarter ps = launcher.decorateByEnv(vars).launch().pwd(filePath).cmds(args).envs(vars)
.stdout(taskListener.getLogger()).stderr(taskListener.getLogger());
int exitcode = ps.join();
log.println("Fortify cloudscan arguments command completed with exit code: " + exitcode);
log.println("Fortify scancentral arguments command completed with exit code: " + exitcode);

if (exitcode != 0) {
run.setResult(Result.FAILURE);
throw new AbortException("Fortify cloudscan arguments command execution failed.");
throw new AbortException("Fortify scancentral arguments command execution failed.");
}
}

Expand Down Expand Up @@ -112,7 +119,7 @@ protected Execution(CloudScanArguments csArguments, StepContext context) {

@Override
protected Void run() throws Exception {
getContext().get(TaskListener.class).getLogger().println("Running CloudScan arguments step");
getContext().get(TaskListener.class).getLogger().println("Running ScanCentral arguments step");
if (!getContext().get(FilePath.class).exists()) {
getContext().get(FilePath.class).mkdirs();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -116,7 +117,13 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
log.println("Fortify Jenkins plugin v " + VERSION);
log.println("Performing Fortify remote scan");
String projectRoot = filePath.getRemote() + File.separator + ".fortify";
String cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
String cloudscanExec;
try {
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
} catch (FileNotFoundException ex) {
log.println("WARNING: Cannot find scancentral executable");
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener);
}

EnvVars vars = run.getEnvironment(taskListener);
ArrayList<String> args = new ArrayList<String>(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import javax.annotation.Nonnull;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
Expand Down Expand Up @@ -180,7 +181,13 @@ public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull
log.println("Fortify Jenkins plugin v " + VERSION);
log.println("Performing Fortify remote analysis");
String projectRoot = filePath.getRemote() + File.separator + ".fortify";
String cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
String cloudscanExec;
try {
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
} catch (FileNotFoundException ex) {
log.println("WARNING: Cannot find scancentral executable");
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener);
}

EnvVars vars = run.getEnvironment(taskListener);
ArrayList<String> args = new ArrayList<String>(2);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import hudson.model.TaskListener;
import org.kohsuke.stapler.DataBoundSetter;

import java.io.FileNotFoundException;
import java.io.IOException;

public abstract class FortifyCloudScanStep extends FortifyStep {
Expand All @@ -26,22 +27,14 @@ public String getResolvedScanArgs(TaskListener listener) {

protected String getCloudScanExecutable(Run<?, ?> build, FilePath workspace, Launcher launcher,
TaskListener listener) throws InterruptedException, IOException {
listener.getLogger().println("Checking for cloudscan executable");
return getExecutable("cloudscan" + (launcher.isUnix() ? "" : ".bat"), true, build, workspace, launcher,
listener, null);
}

/* Look for scancentral executable in Jenkins environment, if not found, get the old cloudscan executable. It's considered not found
* if the getExecutable() returns just the filename rather than the full path.*/
protected String getScancentralExecutable(Run<?, ?> build, FilePath workspace, Launcher launcher,
TaskListener listener) throws InterruptedException, IOException {
String filename = "scancentral" + (launcher.isUnix() ? "" : ".bat");
String msg = "Checking for cloudscan executable";
String exec = getExecutable(filename, true, build, workspace, launcher,
listener, msg);
if (exec.equals(filename)) {
return getCloudScanExecutable(build, workspace, launcher, listener);
} else {
return exec;
}
return getExecutable("scancentral" + (launcher.isUnix() ? "" : ".bat"), true, build, workspace, launcher,
listener, null);
}
}
14 changes: 5 additions & 9 deletions src/main/java/com/fortify/plugin/jenkins/steps/FortifyStep.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* (c) Copyright 2019 Micro Focus or one of its affiliates.
* (c) Copyright 2020 Micro Focus or one of its affiliates.
*
* Licensed under the MIT License (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,6 +15,7 @@
*******************************************************************************/
package com.fortify.plugin.jenkins.steps;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;
Expand Down Expand Up @@ -62,7 +63,7 @@ protected void setLastBuild(Run<?, ?> lastBuild) {
* @param launcher
* @param listener
* @param msg
* @return found executable or filename if not found
* @return found executable
* @throws InterruptedException
* @throws IOException
*/
Expand All @@ -71,6 +72,7 @@ protected String getExecutable(String filename, boolean checkFortifyHome, Run<?,
EnvVars env = build.getEnvironment(listener);
String fortifyHome = null;
String path = null;
// check env variables defined in Jenkins master
for (Map.Entry<String, String> entry : env.entrySet()) {
String key = entry.getKey();
if ("FORTIFY_HOME".equals(key)) {
Expand All @@ -83,13 +85,7 @@ protected String getExecutable(String filename, boolean checkFortifyHome, Run<?,
}
String s = workspace.act(new FindExecutableRemoteService(filename, fortifyHome, path, workspace));
if (s == null) {
listener.getLogger().printf("WARNING: %s executable not found in the Jenkins environment.%n", filename);
if (msg != null) {
listener.getLogger().println(msg);
} else {
listener.getLogger().printf("Checking system PATH for %s.%n", filename);
}
return filename;
throw new FileNotFoundException("ERROR: executable not found: " + filename);
} else {
listener.getLogger().printf("Found executable: %s%n", s);
return s;
Expand Down

0 comments on commit d7c8a74

Please sign in to comment.