Add MVC and JMX endpoints to retrieve audit events
Add MVC and JMX specific endpoints that allow audit events to be retrieved. See gh-6579
This commit is contained in:
committed by
Phillip Webb
parent
a6d18f714f
commit
2f1e4f0c02
@@ -20,6 +20,34 @@ include::{generated}/endpoints.adoc[]
|
||||
|
||||
|
||||
|
||||
=== /auditevents
|
||||
This endpoint provides information about audit events registered by the application.
|
||||
Audit events can be filtered using the `after`, `principal` and `type` parameters as
|
||||
defined by `AuditEventRepository`.
|
||||
|
||||
Example cURL request with `after` parameter:
|
||||
include::{generated}/auditevents/curl-request.adoc[]
|
||||
|
||||
Example HTTP request with `after` parameter:
|
||||
include::{generated}/auditevents/http-request.adoc[]
|
||||
|
||||
Example HTTP response:
|
||||
include::{generated}/auditevents/http-response.adoc[]
|
||||
|
||||
Example cURL request with `principal` and `after` parameters:
|
||||
include::{generated}/auditevents/filter-by-principal/curl-request.adoc[]
|
||||
|
||||
Example HTTP request with `principal` and `after` parameters:
|
||||
include::{generated}/auditevents/filter-by-principal/http-request.adoc[]
|
||||
|
||||
Example cURL request with `principal`, `after` and `type` parameters:
|
||||
include::{generated}/auditevents/filter-by-principal-and-type/curl-request.adoc[]
|
||||
|
||||
Example HTTP request with `principal`, `after` and `type` parameters:
|
||||
include::{generated}/auditevents/filter-by-principal-and-type/http-request.adoc[]
|
||||
|
||||
|
||||
|
||||
=== /logfile
|
||||
This endpoint (if available) contains the plain text logfile configured by the user
|
||||
using `logging.file` or `logging.path` (by default logs are only emitted on stdout
|
||||
|
||||
@@ -77,7 +77,8 @@ public class EndpointDocumentation {
|
||||
static final File LOG_FILE = new File("target/logs/spring.log");
|
||||
|
||||
private static final Set<String> SKIPPED = Collections.<String>unmodifiableSet(
|
||||
new HashSet<String>(Arrays.asList("/docs", "/logfile", "/heapdump")));
|
||||
new HashSet<String>(Arrays.asList("/docs", "/logfile", "/heapdump",
|
||||
"/auditevents")));
|
||||
|
||||
@Autowired
|
||||
private MvcEndpoints mvcEndpoints;
|
||||
@@ -127,6 +128,33 @@ public class EndpointDocumentation {
|
||||
.andExpect(status().isOk()).andDo(document("set-logger"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void auditEvents() throws Exception {
|
||||
this.mockMvc.perform(get("/auditevents")
|
||||
.param("after", "2016-11-01T10:00:00+0000")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk()).andDo(document("auditevents"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void auditEventsByPrincipal() throws Exception {
|
||||
this.mockMvc.perform(get("/auditevents").param("principal", "admin")
|
||||
.param("after", "2016-11-01T10:00:00+0000")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(document("auditevents/filter-by-principal"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void auditEventsByPrincipalAndType() throws Exception {
|
||||
this.mockMvc.perform(get("/auditevents").param("principal", "admin")
|
||||
.param("after", "2016-11-01T10:00:00+0000")
|
||||
.param("type", "AUTHENTICATION_SUCCESS")
|
||||
.accept(MediaType.APPLICATION_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andDo(document("auditevents/filter-by-principal-and-type"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void endpoints() throws Exception {
|
||||
final File docs = new File("src/main/asciidoc");
|
||||
|
||||
@@ -16,10 +16,18 @@
|
||||
|
||||
package org.springframework.boot.actuate.hypermedia;
|
||||
|
||||
import java.time.Instant;
|
||||
import java.util.Collections;
|
||||
import java.util.Date;
|
||||
|
||||
import groovy.text.GStringTemplateEngine;
|
||||
import groovy.text.TemplateEngine;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.CommandLineRunner;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.actuate.audit.AuditEvent;
|
||||
import org.springframework.boot.actuate.audit.AuditEventRepository;
|
||||
import org.springframework.boot.actuate.endpoint.EnvironmentEndpoint;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.flyway.FlywayAutoConfiguration;
|
||||
@@ -30,7 +38,10 @@ import org.springframework.context.annotation.Import;
|
||||
// Flyway must go first
|
||||
@SpringBootApplication
|
||||
@Import({ FlywayAutoConfiguration.class, LiquibaseAutoConfiguration.class })
|
||||
public class SpringBootHypermediaApplication {
|
||||
public class SpringBootHypermediaApplication implements CommandLineRunner {
|
||||
|
||||
@Autowired
|
||||
private AuditEventRepository auditEventRepository;
|
||||
|
||||
@Bean
|
||||
public TemplateEngine groovyTemplateEngine() {
|
||||
@@ -46,4 +57,14 @@ public class SpringBootHypermediaApplication {
|
||||
SpringApplication.run(SpringBootHypermediaApplication.class, args);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void run(String... args) throws Exception {
|
||||
this.auditEventRepository.add(new AuditEvent(Date.from(Instant.parse(
|
||||
"2016-11-01T11:00:00Z")), "user", "AUTHENTICATION_FAILURE",
|
||||
Collections.emptyMap()));
|
||||
this.auditEventRepository.add(new AuditEvent(Date.from(Instant.parse(
|
||||
"2016-11-01T12:00:00Z")), "admin", "AUTHENTICATION_SUCCESS",
|
||||
Collections.emptyMap()));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user