Skip to content

Commit 0aa3b00

Browse files
author
Dave Syer
committed
Ensure shutdown endpoint is disabled by default
Fixes spring-projectsgh-377
1 parent c5d8150 commit 0aa3b00

File tree

4 files changed

+76
-1
lines changed

4 files changed

+76
-1
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/EndpointMvcAdapter.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
package org.springframework.boot.actuate.endpoint.mvc;
1818

19+
import java.util.Collections;
20+
import java.util.Map;
21+
1922
import org.springframework.boot.actuate.endpoint.Endpoint;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.ResponseEntity;
2025
import org.springframework.util.Assert;
2126
import org.springframework.web.bind.annotation.RequestMapping;
2227
import org.springframework.web.bind.annotation.RequestMethod;
@@ -43,9 +48,18 @@ public EndpointMvcAdapter(Endpoint<?> delegate) {
4348
@RequestMapping(method = RequestMethod.GET)
4449
@ResponseBody
4550
public Object invoke() {
51+
if (!this.delegate.isEnabled()) {
52+
// Shouldn't happen
53+
return new ResponseEntity<Map<String, String>>(Collections.singletonMap(
54+
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
55+
}
4656
return this.delegate.invoke();
4757
}
4858

59+
public Endpoint<?> getDelegate() {
60+
return this.delegate;
61+
}
62+
4963
@Override
5064
public String getPath() {
5165
return "/" + this.delegate.getId();

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/MvcEndpoints.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public void afterPropertiesSet() throws Exception {
6060
Collection<Endpoint> delegates = BeanFactoryUtils.beansOfTypeIncludingAncestors(
6161
this.applicationContext, Endpoint.class).values();
6262
for (Endpoint<?> endpoint : delegates) {
63-
if (isGenericEndpoint(endpoint.getClass())) {
63+
if (isGenericEndpoint(endpoint.getClass()) && endpoint.isEnabled()) {
6464
this.endpoints.add(new EndpointMvcAdapter(endpoint));
6565
}
6666
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/mvc/ShutdownMvcEndpoint.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,12 @@
1616

1717
package org.springframework.boot.actuate.endpoint.mvc;
1818

19+
import java.util.Collections;
20+
import java.util.Map;
21+
1922
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
23+
import org.springframework.http.HttpStatus;
24+
import org.springframework.http.ResponseEntity;
2025
import org.springframework.web.bind.annotation.RequestMapping;
2126
import org.springframework.web.bind.annotation.RequestMethod;
2227
import org.springframework.web.bind.annotation.ResponseBody;
@@ -36,6 +41,10 @@ public ShutdownMvcEndpoint(ShutdownEndpoint delegate) {
3641
@ResponseBody
3742
@Override
3843
public Object invoke() {
44+
if (!getDelegate().isEnabled()) {
45+
return new ResponseEntity<Map<String, String>>(Collections.singletonMap(
46+
"message", "This endpoint is disabled"), HttpStatus.NOT_FOUND);
47+
}
3948
return super.invoke();
4049
}
4150
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2012-2013 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.endpoint.mvc;
18+
19+
import java.util.Map;
20+
21+
import org.junit.Before;
22+
import org.junit.Test;
23+
import org.springframework.boot.actuate.endpoint.ShutdownEndpoint;
24+
import org.springframework.http.HttpStatus;
25+
import org.springframework.http.ResponseEntity;
26+
27+
import static org.junit.Assert.assertEquals;
28+
import static org.mockito.Mockito.mock;
29+
import static org.mockito.Mockito.when;
30+
31+
/**
32+
* @author Dave Syer
33+
*/
34+
public class ShutdownMvcEndpointTests {
35+
36+
private ShutdownEndpoint endpoint = mock(ShutdownEndpoint.class);
37+
private ShutdownMvcEndpoint mvc = new ShutdownMvcEndpoint(this.endpoint);
38+
39+
@Before
40+
public void init() {
41+
when(this.endpoint.isEnabled()).thenReturn(false);
42+
}
43+
44+
@Test
45+
public void disabled() {
46+
@SuppressWarnings("unchecked")
47+
ResponseEntity<Map<String, String>> response = (ResponseEntity<Map<String, String>>) this.mvc
48+
.invoke();
49+
assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode());
50+
}
51+
52+
}

0 commit comments

Comments
 (0)