Skip to content

Commit b6b2fd4

Browse files
vpavicsnicoll
authored andcommitted
Make Audit events Web endpoint after parameter required
Closes gh-10322
1 parent dd27682 commit b6b2fd4

File tree

4 files changed

+84
-0
lines changed

4 files changed

+84
-0
lines changed

spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/audit/AuditEventsEndpointAutoConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import org.springframework.boot.actuate.audit.AuditEventRepository;
2020
import org.springframework.boot.actuate.audit.AuditEventsEndpoint;
2121
import org.springframework.boot.actuate.audit.AuditEventsJmxEndpointExtension;
22+
import org.springframework.boot.actuate.audit.AuditEventsWebEndpointExtension;
2223
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
2324
import org.springframework.boot.actuate.logging.LoggersEndpoint;
2425
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
@@ -33,6 +34,7 @@
3334
*
3435
* @author Phillip Webb
3536
* @author Andy Wilkinson
37+
* @author Vedran Pavic
3638
* @since 2.0.0
3739
*/
3840
@Configuration
@@ -57,4 +59,13 @@ public AuditEventsJmxEndpointExtension auditEventsJmxEndpointExtension(
5759
return new AuditEventsJmxEndpointExtension(auditEventsEndpoint);
5860
}
5961

62+
@Bean
63+
@ConditionalOnMissingBean
64+
@ConditionalOnEnabledEndpoint
65+
@ConditionalOnBean(AuditEventsEndpoint.class)
66+
public AuditEventsWebEndpointExtension auditEventsWebEndpointExtension(
67+
AuditEventsEndpoint auditEventsEndpoint) {
68+
return new AuditEventsWebEndpointExtension(auditEventsEndpoint);
69+
}
70+
6071
}

spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/audit/AuditEventsEndpointAutoConfigurationTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.boot.actuate.audit.AuditEventsEndpoint;
2222
import org.springframework.boot.actuate.audit.AuditEventsJmxEndpointExtension;
23+
import org.springframework.boot.actuate.audit.AuditEventsWebEndpointExtension;
2324
import org.springframework.boot.autoconfigure.AutoConfigurations;
2425
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
2526

@@ -30,6 +31,7 @@
3031
*
3132
* @author Andy Wilkinson
3233
* @author Phillip Webb
34+
* @author Vedran Pavic
3335
*/
3436
public class AuditEventsEndpointAutoConfigurationTests {
3537

@@ -49,6 +51,12 @@ public void runShouldHaveJmxExtensionBean() {
4951
.hasSingleBean(AuditEventsJmxEndpointExtension.class));
5052
}
5153

54+
@Test
55+
public void runShouldHaveWebExtensionBean() {
56+
this.contextRunner.run((context) -> assertThat(context)
57+
.hasSingleBean(AuditEventsWebEndpointExtension.class));
58+
}
59+
5260
@Test
5361
public void runWhenEnabledPropertyIsFalseShouldNotHaveEndpointOrExtensionBean()
5462
throws Exception {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.audit;
18+
19+
import java.util.Date;
20+
21+
import org.springframework.boot.actuate.audit.AuditEventsEndpoint.AuditEventsDescriptor;
22+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
23+
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
24+
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension;
25+
import org.springframework.http.HttpStatus;
26+
27+
/**
28+
* {@link WebEndpointExtension} for the {@link AuditEventsEndpoint}.
29+
*
30+
* @author Vedran Pavic
31+
* @since 2.0.0
32+
*/
33+
@WebEndpointExtension(endpoint = AuditEventsEndpoint.class)
34+
public class AuditEventsWebEndpointExtension {
35+
36+
private final AuditEventsEndpoint delegate;
37+
38+
public AuditEventsWebEndpointExtension(AuditEventsEndpoint delegate) {
39+
this.delegate = delegate;
40+
}
41+
42+
@ReadOperation
43+
public WebEndpointResponse<AuditEventsDescriptor> eventsWithPrincipalDateAfterAndType(
44+
String principal, Date after, String type) {
45+
if (after == null) {
46+
return new WebEndpointResponse<>(HttpStatus.BAD_REQUEST.value());
47+
}
48+
AuditEventsDescriptor auditEvents = this.delegate
49+
.eventsWithPrincipalDateAfterAndType(principal, after, type);
50+
return new WebEndpointResponse<>(auditEvents);
51+
}
52+
53+
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/audit/AuditEventsEndpointWebIntegrationTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ public class AuditEventsEndpointWebIntegrationTests {
4141

4242
private static WebTestClient client;
4343

44+
@Test
45+
public void eventsWithoutParams() throws Exception {
46+
client.get().uri((builder) -> builder.path("/application/auditevents").build())
47+
.exchange().expectStatus().isBadRequest();
48+
}
49+
4450
@Test
4551
public void eventsWithDateAfter() throws Exception {
4652
client.get()
@@ -92,6 +98,12 @@ public AuditEventsEndpoint auditEventsEndpoint() {
9298
return new AuditEventsEndpoint(auditEventsRepository());
9399
}
94100

101+
@Bean
102+
public AuditEventsWebEndpointExtension auditEventsWebEndpointExtension(
103+
AuditEventsEndpoint auditEventsEndpoint) {
104+
return new AuditEventsWebEndpointExtension(auditEventsEndpoint);
105+
}
106+
95107
private AuditEvent createEvent(String instant, String principal, String type) {
96108
return new AuditEvent(Date.from(Instant.parse(instant)), principal, type,
97109
Collections.<String, Object>emptyMap());

0 commit comments

Comments
 (0)