-
Notifications
You must be signed in to change notification settings - Fork 49
/
AuditTrailPlugin.java
191 lines (165 loc) · 6.84 KB
/
AuditTrailPlugin.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* The MIT License
*
* Copyright (c) 2004-2011, Sun Microsystems, Inc., Alan Harder
*
* 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 hudson.plugins.audit_trail;
import hudson.DescriptorExtensionList;
import hudson.Plugin;
import hudson.model.*;
import hudson.model.Descriptor.FormException;
import hudson.util.FormValidation;
import hudson.util.PluginServletFilter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import javax.servlet.ServletException;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
/**
* Keep audit trail of particular Jenkins operations, such as configuring jobs.
* @author Alan Harder
*/
public class AuditTrailPlugin extends Plugin {
private String pattern = ".*/(?:configSubmit|doDelete|postBuildResult|enable|disable|"
+ "cancelQueue|stop|toggleLogKeep|doWipeOutWorkspace|createItem|createView|toggleOffline|"
+ "cancelQuietDown|quietDown|restart|exit|safeExit)";
private boolean logBuildCause = true;
private List<AuditLogger> loggers = new ArrayList<AuditLogger>();
private transient boolean started;
private transient String log;
private transient int limit = 1, count = 1;
public String getPattern() { return pattern; }
public boolean getLogBuildCause() { return logBuildCause; }
public List<AuditLogger> getLoggers() { return loggers; }
@Override public void start() throws Exception {
// Set a default value; will be overridden by load() once customized:
load();
applySettings();
// Add Filter to watch all requests and log matching ones
PluginServletFilter.addFilter(new AuditTrailFilter(this));
}
@Override public void configure(StaplerRequest req, JSONObject formData)
throws IOException, ServletException, FormException {
pattern = formData.optString("pattern");
logBuildCause = formData.optBoolean("logBuildCause", true);
loggers = Descriptor.newInstancesFromHeteroList(
req, formData, "loggers", getLoggerDescriptors());
save();
applySettings();
}
public DescriptorExtensionList<AuditLogger, Descriptor<AuditLogger>> getLoggerDescriptors() {
return Jenkins.getInstance().getDescriptorList(AuditLogger.class);
}
private void applySettings() {
try {
AuditTrailFilter.setPattern(pattern);
}
catch (PatternSyntaxException ex) { ex.printStackTrace(); }
for (AuditLogger logger : loggers) {
logger.configure();
}
started = true;
}
/* package */ void onStarted(Run run) {
if (this.started) {
StringBuilder buf = new StringBuilder(100);
for (CauseAction action : run.getActions(CauseAction.class)) {
for (Cause cause : action.getCauses()) {
if (buf.length() > 0) buf.append(", ");
buf.append(cause.getShortDescription());
}
}
if (buf.length() == 0) buf.append("Started");
for (AuditLogger logger : loggers) {
logger.log(run.getParent().getUrl() + " #" + run.getNumber() + ' ' + buf.toString());
}
}
}
public void onFinalized(Run run) {
if (run instanceof AbstractBuild) {
onFinalized((AbstractBuild) run);
}
}
public void onFinalized(AbstractBuild build) {
if (this.started) {
StringBuilder causeBuilder = new StringBuilder(100);
for (CauseAction action : build.getActions(CauseAction.class)) {
for (Cause cause : action.getCauses()) {
if (causeBuilder.length() > 0) causeBuilder.append(", ");
causeBuilder.append(cause.getShortDescription());
}
}
if (causeBuilder.length() == 0) causeBuilder.append("Started");
for (AuditLogger logger : loggers) {
String message = build.getFullDisplayName() +
" " + causeBuilder.toString() +
" on node " + (build.getBuiltOn() == null ? "#unknown#" : build.getBuiltOn().getDisplayName()) +
" started at " + build.getTimestampString2() +
" completed in " + build.getDuration() + "ms" +
" completed: " + build.getResult();
logger.log(message);
}
}
}
/* package */ void onRequest(String uri, String extra, String username) {
if (this.started) {
for (AuditLogger logger : loggers) {
logger.log(uri + extra + " by " + username);
}
}
}
/**
* Backward compatibility
*/
private Object readResolve() {
if (log != null) {
if (loggers == null) {
loggers = new ArrayList<AuditLogger>();
}
LogFileAuditLogger logger = new LogFileAuditLogger(log, limit, count);
if (!loggers.contains(logger))
loggers.add(logger);
log = null;
}
return this;
}
/**
* Validate regular expression syntax.
*/
public FormValidation doRegexCheck(@QueryParameter final String value)
throws IOException, ServletException {
// No permission needed for simple syntax check
try {
Pattern.compile(value);
return FormValidation.ok();
}
catch (Exception ex) {
return FormValidation.errorWithMarkup("Invalid <a href=\""
+ "http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html"
+ "\">regular expression</a> (" + ex.getMessage() + ")");
}
}
}