-
Notifications
You must be signed in to change notification settings - Fork 109
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
support custom log lines processing via script #40
Changes from all commits
f662b45
d4f0396
9c57498
f69b2de
747a0bc
321d77a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,6 +43,8 @@ | |
import org.kohsuke.stapler.QueryParameter; | ||
import org.kohsuke.stapler.StaplerRequest; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
/** | ||
* POJO for storing global configurations shared between components. | ||
* | ||
|
@@ -65,6 +67,9 @@ public static Descriptor getLogstashDescriptor() { | |
public static final class Descriptor extends ToolDescriptor<LogstashInstallation> { | ||
public IndexerType type; | ||
public SyslogFormat syslogFormat; | ||
@SuppressFBWarnings( | ||
value="UUF_UNUSED_PUBLIC_OR_PROTECTED_FIELD", | ||
justification="TODO: do we need this?") | ||
public SyslogProtocol syslogProtocol; | ||
public String host; | ||
public Integer port = -1; | ||
|
@@ -85,6 +90,9 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc | |
} | ||
|
||
@Override | ||
@SuppressFBWarnings( | ||
value="NP_PARAMETER_MUST_BE_NONNULL_BUT_MARKED_AS_NULLABLE", | ||
justification="TODO: investigate") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can't we just leave those cases unsuppressed? or is the build setup to fail in that case? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. build fails There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok, I think we should disable this on maven level then -- just make it emit warnings for now let me know if you need help with this There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. project automated CI will will show a failure. Is there a way to make it not fail but just warn? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this should be set somewhere in maven and be overridable -- let me check There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will look into that once I catch up with the holiday pile up of tasks. |
||
public ToolInstallation newInstance(StaplerRequest req, JSONObject formData) throws FormException { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Req is marked as nullable in the interface, so apparently we should handle this gracefully. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just suppressed the warning, I don't think I touched anything about this. Good to go after all suppression later and have them fixed. |
||
req.bindJSON(this, formData.getJSONObject("logstash")); | ||
save(); | ||
|
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; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* 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.OutputStream; | ||
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; | ||
|
||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
|
||
/** | ||
* 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 OutputStream consoleOut; | ||
|
||
/** Groovy binding for script execution */ | ||
@Nonnull | ||
private final Binding binding; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Seems unsafe to keep this as a field. Initialize in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is it unsafe? The main idea is to allow executing the script always with the same binding. This would allow script to create properties for persisting data between executions. The way I understand your suggestion would not allow this. Or did I misunderstand? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, if we generate Binding in |
||
|
||
/** Classloader for script execution */ | ||
@Nonnull | ||
private final ClassLoader classLoader; | ||
|
||
public LogstashScriptProcessor(SecureGroovyScript script, OutputStream consoleOut) { | ||
this.script = script; | ||
this.consoleOut = consoleOut; | ||
|
||
// TODO: should we put variables in the binding like manager, job, etc.? | ||
binding = new Binding(); | ||
binding.setVariable("console", new BuildConsoleWrapper()); | ||
|
||
// not sure what the diff is compared to getClass().getClassLoader(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sounds like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it does, but then the scripts will be non portable -- you can have them working in one Jenkins instance but not another. I'd suggest to do the more restrictive thing for now. It's easier to extend the possibilities later than restrict them There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is certainly a risk in any case.
Sandboxed scripts could only use whitelisted methods anyway. |
||
final Jenkins jenkins = Jenkins.getInstance(); | ||
classLoader = jenkins.getPluginManager().uberClassLoader; | ||
} | ||
|
||
/** | ||
* Helper method to allow logging to build console. | ||
*/ | ||
@SuppressFBWarnings( | ||
value="DM_DEFAULT_ENCODING", | ||
justification="TODO: not sure how to fix this") | ||
private void buildLogPrintln(Object o) throws IOException { | ||
consoleOut.write(o.toString().getBytes()); | ||
consoleOut.write("\n".getBytes()); | ||
consoleOut.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); | ||
script.evaluate(classLoader, binding); | ||
return (JSONObject) binding.getVariable("payload"); | ||
} | ||
|
||
@Override | ||
public JSONObject finish() throws Exception { | ||
buildLogPrintln("Tearing down Script Log Processor.."); | ||
return process(null); | ||
} | ||
|
||
/** | ||
* 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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you decide to delete it, make it transient first.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would prefer if this findbug issues are fixed in another pull request as this one is large enough.
I will look at the date formatter issues while waiting for script security plugin though as they appear to be more critical.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
And thank you for the quick turnaround!