Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow scripted changes to logged data #71

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,6 @@
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.39</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
Expand All @@ -157,7 +156,8 @@
<version>2.1.5</version>
<scope>test</scope>
</dependency>
<dependency>

<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>java-hamcrest</artifactId>
<version>2.0.0.0</version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@
package jenkins.plugins.logstash;

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.nio.charset.Charset;

import org.kohsuke.stapler.DataBoundConstructor;

Expand All @@ -35,8 +39,14 @@
import hudson.model.BuildListener;
import hudson.tasks.BuildWrapper;
import hudson.tasks.BuildWrapperDescriptor;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;

import javax.annotation.CheckForNull;

import org.kohsuke.stapler.DataBoundSetter;

/**
* Logstash note on each output line.
*
* This BuildWrapper is not used anymore.
* We just keep it to be able to convert projects that have the BuildWrapper configured at startup or when posting the xml via the rest api
Expand All @@ -48,13 +58,21 @@
public class LogstashBuildWrapper extends BuildWrapper
{

@CheckForNull

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LogstashBuildWrapper is the wrong place. This class is deprecated and only here for backwards compatibility.
You have to put this in the LogstashJobProperty.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, thanks, I'll investigate what is this LogstashJobProperty. Please check out other comments as I'm more concerned about decisions around script handling.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LogstashJobProperty is the place were the script needs to be defined.
LogstashConsoleLogFilter is the place were the LogstashWriter is created when enabled globally, set via JobProperty or for the pipeline step.

private SecureGroovyScript secureGroovyScript;

/**
* Create a new {@link LogstashBuildWrapper}.
*/
@DataBoundConstructor
public LogstashBuildWrapper()
{}

@DataBoundSetter
public void setSecureGroovyScript(@CheckForNull SecureGroovyScript script) {
this.secureGroovyScript = script != null ? script.configuringWithNonKeyItem() : null;
}

/**
* {@inheritDoc}
*/
Expand All @@ -73,6 +91,22 @@ public DescriptorImpl getDescriptor()
return (DescriptorImpl)super.getDescriptor();
}

@CheckForNull
public SecureGroovyScript getSecureGroovyScript() {
return secureGroovyScript;
}

// Method to encapsulate calls for unit-testing
LogstashWriter getLogStashWriter(AbstractBuild<?, ?> build, OutputStream errorStream) {
Charset charset = build.getCharset();
LogstashScriptProcessor processor = null;
if (secureGroovyScript != null) {
processor = new LogstashScriptProcessor(secureGroovyScript, new OutputStreamWriter(errorStream, charset));
}

return new LogstashWriter(build, errorStream, null, charset, processor);
}

/**
* Registers {@link LogstashBuildWrapper} as a {@link BuildWrapper}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,41 @@

import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.annotation.CheckForNull;

import hudson.Extension;
import hudson.console.ConsoleLogFilter;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Run;

import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;

@Extension(ordinal = 1000)
public class LogstashConsoleLogFilter extends ConsoleLogFilter implements Serializable
{

private static Logger LOGGER = Logger.getLogger(LogstashConsoleLogFilter.class.getName());

@CheckForNull
private SecureGroovyScript secureGroovyScript = null;
private transient Run<?, ?> run;
public LogstashConsoleLogFilter() {};

public LogstashConsoleLogFilter(Run<?, ?> run)
{
this(run, null);
}

public LogstashConsoleLogFilter(Run<?, ?> run, SecureGroovyScript script)
{
this.run = run;
this.secureGroovyScript = script;
}
private static final long serialVersionUID = 1L;

Expand All @@ -41,7 +54,7 @@ public OutputStream decorateLogger(Run build, OutputStream logger) throws IOExce
{
if (isLogstashEnabled(build))
{
LogstashWriter logstash = getLogStashWriter(build, logger);
LogstashWriter logstash = getLogStashWriter(build, logger, secureGroovyScript);
return new LogstashOutputStream(logger, logstash);
}
else
Expand All @@ -51,7 +64,7 @@ public OutputStream decorateLogger(Run build, OutputStream logger) throws IOExce
}
if (run != null)
{
LogstashWriter logstash = getLogStashWriter(run, logger);
LogstashWriter logstash = getLogStashWriter(run, logger, secureGroovyScript);
return new LogstashOutputStream(logger, logstash);
}
else
Expand All @@ -60,9 +73,13 @@ public OutputStream decorateLogger(Run build, OutputStream logger) throws IOExce
}
}

LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream)
LogstashWriter getLogStashWriter(Run<?, ?> build, OutputStream errorStream, SecureGroovyScript script)
{
return new LogstashWriter(build, errorStream, null, build.getCharset());
LogstashScriptProcessor processor = null;
if (secureGroovyScript != null) {
processor = new LogstashScriptProcessor(secureGroovyScript, new OutputStreamWriter(errorStream, build.getCharset()));
}
return new LogstashWriter(build, errorStream, null, build.getCharset(), processor);
}

private boolean isLogstashEnabled(Run<?, ?> build)
Expand All @@ -76,8 +93,10 @@ private boolean isLogstashEnabled(Run<?, ?> build)
if (build.getParent() instanceof AbstractProject)
{
AbstractProject<?, ?> project = (AbstractProject<?, ?>)build.getParent();
if (project.getProperty(LogstashJobProperty.class) != null)
LogstashJobProperty jobProperty = project.getProperty(LogstashJobProperty.class);
if (jobProperty != null)
{
this.secureGroovyScript = jobProperty.getSecureGroovyScript();
return true;
}
}
Expand Down
17 changes: 17 additions & 0 deletions src/main/java/jenkins/plugins/logstash/LogstashJobProperty.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package jenkins.plugins.logstash;

import javax.annotation.CheckForNull;

import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.StaplerRequest;

import hudson.Extension;
Expand All @@ -9,18 +12,32 @@
import hudson.model.JobProperty;
import hudson.model.JobPropertyDescriptor;
import net.sf.json.JSONObject;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;

/**
* This JobProperty is a marker to decide if logs should be sent to an indexer.
*
*/
public class LogstashJobProperty extends JobProperty<Job<?, ?>>
{
@CheckForNull
private SecureGroovyScript secureGroovyScript = null;

@DataBoundConstructor
public LogstashJobProperty()
{}

@DataBoundSetter
public void setSecureGroovyScript(@CheckForNull SecureGroovyScript script)
{
this.secureGroovyScript = script != null ? script.configuringWithNonKeyItem() : null;
}

public SecureGroovyScript getSecureGroovyScript()
{
return this.secureGroovyScript;
}

@Extension
public static class DescriptorImpl extends JobPropertyDescriptor
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void flush() throws IOException {
*/
@Override
public void close() throws IOException {
logstash.close();
delegate.close();
super.close();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* The MIT License
*
* Copyright 2017 Red Hat inc, and individual contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.plugins.logstash;

import net.sf.json.JSONObject;

/**
* Interface describing processors of persisted payload.
*
* @author Aleksandar Kostadinov
* @since 1.4.0
*/
public interface LogstashPayloadProcessor {
/**
* Modifies a JSON payload compatible with the Logstash schema.
*
* @param payload the JSON payload that has been constructed so far.
* @return The formatted JSON object, can be null to ignore this payload.
*/
JSONObject process(JSONObject payload) throws Exception;

/**
* Finalizes any operations, for example returns cashed lines at end of build.
*
* @return A formatted JSON object, can be null when it has nothing.
*/
JSONObject finish() throws Exception;
}
117 changes: 117 additions & 0 deletions src/main/java/jenkins/plugins/logstash/LogstashScriptProcessor.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* The MIT License
*
* Copyright 2017 Red Hat inc. and individual contributors
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package jenkins.plugins.logstash;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.LinkedHashMap;

import javax.annotation.Nonnull;

import groovy.lang.Binding;

import net.sf.json.JSONObject;

import jenkins.model.Jenkins;
import org.jenkinsci.plugins.scriptsecurity.sandbox.groovy.SecureGroovyScript;
import org.jenkinsci.plugins.scriptsecurity.sandbox.whitelists.Whitelisted;

/**
* This class is handling custom groovy script processing of JSON payload.
* Each call to process executes the script provided in job configuration.
* Script is executed under the same binding each time so that it has ability
* to persist data during build execution if desired by script author.
* When build is finished, script will receive null as the payload and can
* return any cached but non-sent data back for persisting.
* The return value of script is the payload to be persisted unless null.
*
* @author Aleksandar Kostadinov
* @since 1.4.0
*/
public class LogstashScriptProcessor implements LogstashPayloadProcessor{
@Nonnull
private final SecureGroovyScript script;

@Nonnull
private final OutputStreamWriter consoleWriter;

/** Groovy binding for script execution */
@Nonnull
private final Binding binding;

/** Classloader for script execution */
@Nonnull
private final ClassLoader classLoader;

public LogstashScriptProcessor(SecureGroovyScript script, OutputStreamWriter consoleWriter) {
this.script = script;
this.consoleWriter = consoleWriter;

// TODO: should we put variables in the binding like manager, job, etc.?
binding = new Binding();
binding.setVariable("console", new BuildConsoleWrapper());

// if users need access to types defined by any plugin we may decide to
// switch to Jenkins.getInstance().getPluginManager().uberClassLoader
classLoader = LogstashScriptProcessor.class.getClassLoader();
}

/**
* Helper method to allow logging to build console.
*/
private void buildLogPrintln(Object o) throws IOException {
consoleWriter.write(o.toString() + "\n");
consoleWriter.flush();
}

/*
* good examples in:
* https://github.com/jenkinsci/envinject-plugin/blob/master/src/main/java/org/jenkinsci/plugins/envinject/service/EnvInjectEnvVars.java
* https://github.com/jenkinsci/groovy-postbuild-plugin/pull/11/files
*/
@Override
public JSONObject process(JSONObject payload) throws Exception {
binding.setVariable("payload", payload);
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In #40 we spoke about possibly duplicating the payload so original one is intact. For small payload objects this might be acceptable but if we apply same to post-build action, then the object might be quiet large. Also dup operation may not be so quick. And I don't see some immediate benefit of doing this additional processing. So leaving as is unless there are objections.

script.evaluate(classLoader, binding);
return (JSONObject) binding.getVariable("payload");
}

@Override
public JSONObject finish() throws Exception {
buildLogPrintln("Tearing down Script Log Processor..");
return process(new JSONObject());
}

/**
* Helper to allow access from sandboxed script to output messages to console.
*/
private class BuildConsoleWrapper {
@Whitelisted
public void println(Object o) throws IOException {
buildLogPrintln(o);
}
}
}
Loading