Skip to content

Commit

Permalink
Merge pull request #17 from PierreBtz/pbeitz/JENKINS-57232
Browse files Browse the repository at this point in the history
[JENKINS-57232] Add JCASC support
  • Loading branch information
PierreBtz authored Nov 30, 2019
2 parents 9f0a2cc + ab1980f commit f9c4678
Show file tree
Hide file tree
Showing 11 changed files with 327 additions and 113 deletions.
29 changes: 26 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>3.43</version>
<version>3.50</version>
<relativePath />
</parent>

Expand All @@ -14,7 +14,7 @@
<name>Audit Trail</name>
<version>2.7-SNAPSHOT</version>
<properties>
<jenkins.version>2.60.3</jenkins.version>
<jenkins.version>2.138.1</jenkins.version>
<java.level>8</java.level>
</properties>

Expand All @@ -39,6 +39,17 @@
<version>3.0.2</version>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.jenkins</groupId>
<artifactId>configuration-as-code</artifactId>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -55,7 +66,6 @@
<plugin>
<groupId>org.jenkins-ci.tools</groupId>
<artifactId>maven-hpi-plugin</artifactId>
<version>2.5</version>
<configuration>
<loggers>
<hudson.plugins.audit_trail>FINE</hudson.plugins.audit_trail>
Expand All @@ -78,4 +88,17 @@
<url>https://repo.jenkins-ci.org/public/</url>
</pluginRepository>
</pluginRepositories>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>io.jenkins.tools.bom</groupId>
<artifactId>bom-2.138.x</artifactId>
<version>3</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>

</project>
11 changes: 9 additions & 2 deletions src/main/java/hudson/plugins/audit_trail/AuditLogger.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package hudson.plugins.audit_trail;

import hudson.DescriptorExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Descriptor;
import jenkins.model.Jenkins;

import java.io.IOException;

/**
* @author <a href="mailto:[email protected]">Nicolas De Loof</a>
* @author Pierre Beitz
*/
public abstract class AuditLogger implements Describable<AuditLogger>, ExtensionPoint {

public abstract void configure();

public abstract void log(String event);

public Descriptor getDescriptor() {
public Descriptor<AuditLogger> getDescriptor() {
return Jenkins.getInstance().getDescriptorOrDie(getClass());
}

Expand All @@ -31,4 +32,10 @@ public void cleanUp() throws SecurityException {
// default does nothing
}

/**
* Returns all the registered {@link AuditLogger} descriptors.
*/
public static DescriptorExtensionList<AuditLogger, Descriptor<AuditLogger>> all() {
return Jenkins.getInstance().getDescriptorList(AuditLogger.class);
}
}
46 changes: 43 additions & 3 deletions src/main/java/hudson/plugins/audit_trail/AuditTrailFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,14 @@
*/
package hudson.plugins.audit_trail;

import com.google.inject.Injector;
import hudson.Extension;
import hudson.init.Initializer;
import hudson.model.User;
import hudson.util.PluginServletFilter;
import jenkins.model.Jenkins;

import javax.inject.Inject;
import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
Expand All @@ -34,21 +39,33 @@
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

import static hudson.init.InitMilestone.PLUGINS_PREPARED;

/**
* Servlet filter to watch requests and log those we are interested in.
* @author Alan Harder
* @author Pierre Beitz
*/
@Extension
public class AuditTrailFilter implements Filter {

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

private static Pattern uriPattern = null;

private final AuditTrailPlugin plugin;
@Inject
private AuditTrailPlugin configuration;

/**
* @deprecated as of 2.6
**/
@Deprecated
public AuditTrailFilter(AuditTrailPlugin plugin) {
this.plugin = plugin;
this.configuration = plugin;
}

public AuditTrailFilter() {
// used by the injector
}

public void init(FilterConfig fc) {
Expand Down Expand Up @@ -85,7 +102,7 @@ public void doFilter(ServletRequest request, ServletResponse res, FilterChain ch
if(LOGGER.isLoggable(Level.FINE))
LOGGER.log(Level.FINE, "Audit request {0} by user {1}", new Object[]{uri, username});

plugin.onRequest(uri, extra, username);
onRequest(uri, extra, username);
} else {
LOGGER.log(Level.FINEST, "Skip audit for request {0}", uri);
}
Expand All @@ -94,4 +111,27 @@ public void doFilter(ServletRequest request, ServletResponse res, FilterChain ch

public void destroy() {
}

// the default milestone doesn't seem right, as the injector is not available yet (at least with the JenkinsRule)
@Initializer(after = PLUGINS_PREPARED)
public static void init() throws ServletException {
Injector injector = Jenkins.getInstance().getInjector();
if (injector == null) {
return;
}
PluginServletFilter.addFilter(injector.getInstance(AuditTrailFilter.class));
}

private void onRequest(String uri, String extra, String username) {
if (configuration != null) {
if (configuration.isStarted()) {
for (AuditLogger logger : configuration.getLoggers()) {
logger.log(uri + extra + " by " + username);
}
} else {
LOGGER.warning("Plugin configuration not properly injected, please report an issue to the Audit Trail Plugin");
}
}

}
}
Loading

0 comments on commit f9c4678

Please sign in to comment.