Skip to content

Commit

Permalink
fixes GH-49
Browse files Browse the repository at this point in the history
The Plugin Can Not Find FORTIFY_HOME & PATH. The issue was relevant only for pipelines where EnvVars can be overwritten and need to be pulled out of step's context rather than the build
  • Loading branch information
akaryakina committed Aug 11, 2022
1 parent 427d655 commit 3b3becf
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 150 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*******************************************************************************
* (c) Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.fortify.plugin.jenkins.steps;

import com.google.common.collect.ImmutableSet;
Expand All @@ -13,7 +28,6 @@
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.*;

import javax.annotation.Nonnull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -45,24 +59,23 @@ public StepExecution start(StepContext context) throws Exception {
}

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener taskListener) throws InterruptedException, IOException {
public void perform(Run<?, ?> run, FilePath filePath, EnvVars vars, Launcher launcher, TaskListener taskListener) throws InterruptedException, IOException {
setLastBuild(run);
PrintStream log = taskListener.getLogger();
log.println("Fortify Jenkins plugin v " + VERSION);
log.println("Launching Fortify scancentral arguments command");
String cloudscanExec;
try {
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener, vars);
} catch (FileNotFoundException ex) {
log.println("WARNING: Cannot find scancentral executable");
try {
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener);
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener, vars);
} catch (FileNotFoundException exception) {
throw new RuntimeException("Cannot find cloudscan executable");
}
}

EnvVars vars = run.getEnvironment(taskListener);
ArrayList<String> args = new ArrayList<String>(2);
args.add(cloudscanExec);
args.add("arguments");
Expand Down Expand Up @@ -123,14 +136,13 @@ protected Execution(CloudScanArguments csArguments, StepContext context) {

@Override
protected Void run() throws Exception {
getContext().get(TaskListener.class).getLogger().println("Running ScanCentral arguments step");
if (!getContext().get(FilePath.class).exists()) {
getContext().get(FilePath.class).mkdirs();
StepContext context = getContext();
context.get(TaskListener.class).getLogger().println("Running ScanCentral arguments step");
if (!context.get(FilePath.class).exists()) {
context.get(FilePath.class).mkdirs();
}

csArguments.perform(getContext().get(Run.class), getContext().get(FilePath.class), getContext().get(Launcher.class),
getContext().get(TaskListener.class));

csArguments.perform(context.get(Run.class), context.get(FilePath.class), context.get(EnvVars.class),
context.get(Launcher.class), context.get(TaskListener.class));
return null;
}

Expand Down
40 changes: 26 additions & 14 deletions src/main/java/com/fortify/plugin/jenkins/steps/CloudScanMbs.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*******************************************************************************
* (c) Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.fortify.plugin.jenkins.steps;

import com.fortify.plugin.jenkins.FortifyPlugin;
Expand All @@ -17,7 +32,6 @@
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.*;

import javax.annotation.Nonnull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -110,37 +124,36 @@ public StepExecution start(StepContext context) throws Exception {
}

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener taskListener) throws InterruptedException, IOException {
public void perform(Run<?, ?> run, FilePath filePath, EnvVars vars, Launcher launcher, TaskListener taskListener) throws InterruptedException, IOException {
setLastBuild(run);
PrintStream log = taskListener.getLogger();
log.println("Fortify Jenkins plugin v " + VERSION);
log.println("Performing Fortify remote scan");
String projectRoot = filePath.child(".fortify").getRemote();
String cloudscanExec;
try {
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener, vars);
} catch (FileNotFoundException ex) {
log.println("WARNING: Cannot find scancentral executable");
try {
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener);
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener, vars);
} catch (FileNotFoundException exception) {
throw new RuntimeException("Cannot find cloudscan executable");
}
}

EnvVars vars = run.getEnvironment(taskListener);
ArrayList<String> args = new ArrayList<String>(2);
args.add(cloudscanExec);

/*
if SSC is configured, use SSC's configuration to find the Controller
*/
if (FortifyPlugin.DESCRIPTOR.getUrl() != null) {
if (StringUtils.isNotBlank(FortifyPlugin.DESCRIPTOR.getUrl())) {
args.add("-sscurl");
args.add(FortifyPlugin.DESCRIPTOR.getUrl());
args.add("-ssctoken");
args.add(FortifyPlugin.DESCRIPTOR.getToken());
} else if (FortifyPlugin.DESCRIPTOR.getCtrlUrl() != null) {
} else if (StringUtils.isNotBlank(FortifyPlugin.DESCRIPTOR.getCtrlUrl())) {
args.add("-url");
args.add(FortifyPlugin.DESCRIPTOR.getCtrlUrl());
} else {
Expand Down Expand Up @@ -251,14 +264,13 @@ protected Execution(CloudScanMbs csMbs, StepContext context) {

@Override
protected Void run() throws Exception {
getContext().get(TaskListener.class).getLogger().println("Running Fortify remote scan step");
if (!getContext().get(FilePath.class).exists()) {
getContext().get(FilePath.class).mkdirs();
StepContext context = getContext();
context.get(TaskListener.class).getLogger().println("Running Fortify remote scan step");
if (!context.get(FilePath.class).exists()) {
context.get(FilePath.class).mkdirs();
}

csMbs.perform(getContext().get(Run.class), getContext().get(FilePath.class), getContext().get(Launcher.class),
getContext().get(TaskListener.class));

csMbs.perform(context.get(Run.class), context.get(FilePath.class), context.get(EnvVars.class),
context.get(Launcher.class), context.get(TaskListener.class));
return null;
}

Expand Down
42 changes: 27 additions & 15 deletions src/main/java/com/fortify/plugin/jenkins/steps/CloudScanStart.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,18 @@
/*******************************************************************************
* (c) Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.fortify.plugin.jenkins.steps;

import com.fortify.plugin.jenkins.FortifyPlugin;
Expand All @@ -17,7 +32,6 @@
import org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution;
import org.kohsuke.stapler.*;

import javax.annotation.Nonnull;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
Expand Down Expand Up @@ -194,37 +208,36 @@ public StepExecution start(StepContext context) throws Exception {
}

@Override
public void perform(@Nonnull Run<?, ?> run, @Nonnull FilePath filePath, @Nonnull Launcher launcher, @Nonnull TaskListener taskListener) throws InterruptedException, IOException {
public void perform(Run<?, ?> run, FilePath filePath, EnvVars vars, Launcher launcher, TaskListener taskListener) throws InterruptedException, IOException {
setLastBuild(run);
PrintStream log = taskListener.getLogger();
log.println("Fortify Jenkins plugin v " + VERSION);
log.println("Performing Fortify remote analysis");
String projectRoot = filePath.child(".fortify").getRemote();
String cloudscanExec;
try {
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener);
cloudscanExec = getScancentralExecutable(run, filePath, launcher, taskListener, vars);
} catch (FileNotFoundException ex) {
log.println("WARNING: Cannot find scancentral executable");
try {
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener);
cloudscanExec = getCloudScanExecutable(run, filePath, launcher, taskListener, vars);
} catch (FileNotFoundException exception) {
throw new RuntimeException("Cannot find cloudscan executable");
}
}

EnvVars vars = run.getEnvironment(taskListener);
ArrayList<String> args = new ArrayList<String>(2);
args.add(cloudscanExec);

/*
if SSC is configured, use SSC's configuration to find the Controller
*/
if (FortifyPlugin.DESCRIPTOR.getUrl() != null) {
if (StringUtils.isNotBlank(FortifyPlugin.DESCRIPTOR.getUrl())) {
args.add("-sscurl");
args.add(FortifyPlugin.DESCRIPTOR.getUrl());
args.add("-ssctoken");
args.add(FortifyPlugin.DESCRIPTOR.getToken());
} else if (FortifyPlugin.DESCRIPTOR.getCtrlUrl() != null) {
} else if (StringUtils.isNotBlank(FortifyPlugin.DESCRIPTOR.getCtrlUrl())) {
args.add("-url");
args.add(FortifyPlugin.DESCRIPTOR.getCtrlUrl());
} else {
Expand Down Expand Up @@ -334,7 +347,7 @@ public String getDisplayName() {

@Override
public Set<? extends Class<?>> getRequiredContext() {
return ImmutableSet.of(Run.class, FilePath.class, Launcher.class, TaskListener.class);
return ImmutableSet.of(Run.class, FilePath.class, EnvVars.class, Launcher.class, TaskListener.class);
}

public void doRefreshProjects(StaplerRequest req, StaplerResponse rsp, @QueryParameter String value)
Expand Down Expand Up @@ -371,14 +384,13 @@ protected Execution(CloudScanStart csStart, StepContext context) {

@Override
protected Void run() throws Exception {
getContext().get(TaskListener.class).getLogger().println("Running Fortify remote analysis step");
if (!getContext().get(FilePath.class).exists()) {
getContext().get(FilePath.class).mkdirs();
StepContext context = getContext();
context.get(TaskListener.class).getLogger().println("Running Fortify remote analysis step");
if (!context.get(FilePath.class).exists()) {
context.get(FilePath.class).mkdirs();
}

csStart.perform(getContext().get(Run.class), getContext().get(FilePath.class), getContext().get(Launcher.class),
getContext().get(TaskListener.class));

csStart.perform(context.get(Run.class), context.get(FilePath.class), context.get(EnvVars.class),
context.get(Launcher.class), context.get(TaskListener.class));
return null;
}

Expand Down
19 changes: 8 additions & 11 deletions src/main/java/com/fortify/plugin/jenkins/steps/FortifyClean.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,19 +56,16 @@ public StepExecution start(StepContext context) throws Exception {
}

@Override
public void perform(Run<?, ?> build, FilePath workspace, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
public void perform(Run<?, ?> build, FilePath workspace, EnvVars vars, Launcher launcher, TaskListener listener) throws InterruptedException, IOException {
setLastBuild(build);
PrintStream log = listener.getLogger();
log.println("Fortify Jenkins plugin v " + VERSION);
log.println("Launching Fortify SCA clean command");
String projectRoot = workspace.child(".fortify").getRemote();
String sourceanalyzer = null;

if (sourceanalyzer == null) {
sourceanalyzer = getSourceAnalyzerExecutable(build, workspace, launcher, listener);
sourceanalyzer = getSourceAnalyzerExecutable(build, workspace, launcher, listener, vars);
}

EnvVars vars = build.getEnvironment(listener);
ArrayList<String> args = new ArrayList<String>(2);
args.add(sourceanalyzer);
args.add("-Dcom.fortify.sca.ProjectRoot=" + projectRoot);
Expand Down Expand Up @@ -144,13 +141,13 @@ protected Execution(FortifyClean fc, StepContext context) {

@Override
protected Void run() throws Exception {
getContext().get(TaskListener.class).getLogger().println("Running FortifyClean step");
if (!getContext().get(FilePath.class).exists()) {
getContext().get(FilePath.class).mkdirs();
StepContext context = getContext();
context.get(TaskListener.class).getLogger().println("Running FortifyClean step");
if (!context.get(FilePath.class).exists()) {
context.get(FilePath.class).mkdirs();
}
fc.perform(getContext().get(Run.class), getContext().get(FilePath.class), getContext().get(Launcher.class),
getContext().get(TaskListener.class));

fc.perform(context.get(Run.class), context.get(FilePath.class), context.get(EnvVars.class),
context.get(Launcher.class), context.get(TaskListener.class));
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
/*******************************************************************************
* (c) Copyright 2022 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.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/MIT
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*******************************************************************************/
package com.fortify.plugin.jenkins.steps;

import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
Expand All @@ -25,15 +41,15 @@ public String getResolvedScanArgs(TaskListener listener) {
}

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

protected String getScancentralExecutable(Run<?, ?> build, FilePath workspace, Launcher launcher,
TaskListener listener) throws InterruptedException, IOException {
TaskListener listener, EnvVars vars) throws InterruptedException, IOException {
return getExecutable("scancentral" + (launcher.isUnix() ? "" : ".bat"), build, workspace,
listener, "FORTIFY_HOME");
listener, "FORTIFY_HOME", vars);
}
}
Loading

0 comments on commit 3b3becf

Please sign in to comment.