Skip to content

Commit

Permalink
[JENKINS-45960] Supporting Groovy PostBuild plugin (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
v1v committed Aug 4, 2017
1 parent b6adad6 commit b967f78
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 30 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Expand Up @@ -181,6 +181,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jvnet.hudson.plugins</groupId>
<artifactId>groovy-postbuild</artifactId>
<version>2.2.2</version>
<scope>test</scope>
</dependency>

<!-- JENKINS-29427 -->
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand Down
@@ -1,9 +1,12 @@
package org.jenkins.ci.plugins.jenkinslint.check;

import hudson.model.AbstractProject;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.Project;
import hudson.tasks.Builder;
import jenkins.model.Jenkins;
import hudson.tasks.Publisher;
import hudson.util.DescribableList;
import org.jenkins.ci.plugins.jenkinslint.model.AbstractCheck;

import java.util.List;
Expand All @@ -22,33 +25,32 @@ public GroovySystemExitChecker(boolean enabled) {

public boolean executeCheck(Item item) {
boolean found = false;
if (Jenkins.getInstance().pluginManager.getPlugin("groovy") != null) {

if (item.getClass().getSimpleName().equals("MavenModuleSet")) {
try {
Object getPrebuilders = item.getClass().getMethod("getPrebuilders", null).invoke(item);
if (getPrebuilders instanceof List) {
found = isSystemExit((List) getPrebuilders);
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
if (item.getClass().getSimpleName().equals("MavenModuleSet")) {
try {
Object getPrebuilders = item.getClass().getMethod("getPrebuilders", null).invoke(item);
if (getPrebuilders instanceof List && isSystemExit((List) getPrebuilders)) {
found = true;
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
if (item instanceof Project) {
found = isSystemExit(((Project) item).getBuilders());
}
if (item.getClass().getSimpleName().equals("MatrixProject")) {
try {
Object getBuilders = item.getClass().getMethod("getBuilders", null).invoke(item);
if (getBuilders instanceof List) {
found = isSystemExit((List) getBuilders);
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
if (item instanceof Project) {
found = isSystemExit(((Project) item).getBuilders());
}
if (item.getClass().getSimpleName().equals("MatrixProject")) {
try {
Object getBuilders = item.getClass().getMethod("getBuilders", null).invoke(item);
if (getBuilders instanceof List && isSystemExit((List) getBuilders)) {
found = true;
}
}catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
} else {
LOG.log(Level.INFO, "Groovy is not installed");
}

if (isSystemExitInPublisher(((AbstractProject) item).getPublishersList())) {
found = true;
}
return found;
}
Expand All @@ -61,21 +63,48 @@ private boolean isSystemExit (List<Builder> builders) {
try {
Object scriptSource = builder.getClass().getMethod("getScriptSource",null).invoke(builder);
if (scriptSource.getClass().getName().endsWith("StringScriptSource")) {
Object command = scriptSource.getClass().getMethod("getCommand",null).invoke(scriptSource);
if (command instanceof String) {
// TODO: Parse to search for non comments, otherwise some false positives!
status = (command != null && ((String) command).toLowerCase().contains("system.exit"));
LOG.log(Level.FINE, "isSystemExit " + status);
if (containsSystemExit(scriptSource.getClass().getMethod("getCommand",null).invoke(scriptSource))) {
status = true;
}
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
status = false;
}
}
}
}
return status;
}


private boolean isSystemExitInPublisher (DescribableList<Publisher, Descriptor<Publisher>> publishersList) {
boolean status = false;
for (Publisher publisher : publishersList) {
if (publisher.getClass().getName().endsWith("GroovyPostbuildRecorder")) {
LOG.log(Level.FINEST, "GroovyPostbuildRecorder " + publisher);
try {
Object scriptSource = publisher.getClass().getMethod("getScript",null).invoke(publisher);
if (scriptSource.getClass().getName().endsWith("SecureGroovyScript")) {
if (containsSystemExit(scriptSource.getClass().getMethod("getScript",null).invoke(scriptSource))) {
status = true;
}
}
} catch (Exception e) {
LOG.log(Level.WARNING, "Exception " + e.getMessage(), e.getCause());
}
}
}
return status;
}


private boolean containsSystemExit (Object command) {
boolean status = false;
if (command instanceof String) {
// TODO: Parse to search for non comments, otherwise some false positives!
status = (command != null && ((String) command).toLowerCase().contains("system.exit"));
LOG.log(Level.FINE, "isSystemExit " + status);
}
return status;
}
}
Expand Up @@ -5,7 +5,9 @@
import hudson.model.FreeStyleProject;
import hudson.plugins.groovy.StringScriptSource;
import org.jenkins.ci.plugins.jenkinslint.AbstractTestCase;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.junit.Test;
import org.jvnet.hudson.plugins.groovypostbuild.GroovyPostbuildRecorder;
import org.jvnet.hudson.test.Issue;

import static org.junit.Assert.assertFalse;
Expand Down Expand Up @@ -94,4 +96,37 @@ public class GroovySystemExitCheckerTestCase extends AbstractTestCase {
project.getPrebuilders().add(new hudson.plugins.groovy.SystemGroovy(new StringScriptSource("System.exit(0)"),null,null));
assertTrue(checker.executeCheck(project));
}
@Test public void testJobWithPublisherGroovy() throws Exception {
FreeStyleProject project = j.createFreeStyleProject("WithoutSystem");
SecureGroovyScript without = new SecureGroovyScript("println 'hi'",false,null);
project.getPublishersList().add(new GroovyPostbuildRecorder(without, 0, false));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createFreeStyleProject("WithSystem");
SecureGroovyScript with = new SecureGroovyScript("System.exit(0)",false,null);
project.getPublishersList().add(new GroovyPostbuildRecorder(with, 0, false));
assertTrue(checker.executeCheck(project));
}
@Test public void testMatrixProjecWithPublisherGroovy() throws Exception {
MatrixProject project = j.createMatrixProject("WithoutSystem");
SecureGroovyScript without = new SecureGroovyScript("println 'hi'",false,null);
project.getPublishersList().add(new GroovyPostbuildRecorder(without, 0, false));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMatrixProject("WithSystem");
SecureGroovyScript with = new SecureGroovyScript("System.exit(0)",false,null);
project.getPublishersList().add(new GroovyPostbuildRecorder(with, 0, false));
assertTrue(checker.executeCheck(project));
}
@Test public void testMavenModuleWithPublisherGroovy() throws Exception {
MavenModuleSet project = j.createMavenProject("WithoutSystem");
SecureGroovyScript without = new SecureGroovyScript("println 'hi'",false,null);
project.getPublishersList().add(new GroovyPostbuildRecorder(without, 0, false));
assertFalse(checker.executeCheck(project));
project.delete();
project = j.createMavenProject("WithSystem");
SecureGroovyScript with = new SecureGroovyScript("System.exit(0)",false,null);
project.getPublishersList().add(new GroovyPostbuildRecorder(with, 0, false));
assertTrue(checker.executeCheck(project));
}
}

0 comments on commit b967f78

Please sign in to comment.