Skip to content

Commit e1a216e

Browse files
committed
Merge branch '1.5.x'
2 parents ed9f111 + af61278 commit e1a216e

File tree

7 files changed

+144
-2
lines changed

7 files changed

+144
-2
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryEndpointHandlerMapping.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.actuate.endpoint.Endpoint;
2828
import org.springframework.boot.actuate.endpoint.mvc.AbstractEndpointHandlerMapping;
2929
import org.springframework.boot.actuate.endpoint.mvc.HalJsonMvcEndpoint;
30+
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
3031
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
3132
import org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint;
3233
import org.springframework.web.cors.CorsConfiguration;
@@ -54,10 +55,20 @@ class CloudFoundryEndpointHandlerMapping
5455
protected void postProcessEndpoints(Set<NamedMvcEndpoint> endpoints) {
5556
super.postProcessEndpoints(endpoints);
5657
Iterator<NamedMvcEndpoint> iterator = endpoints.iterator();
58+
HealthMvcEndpoint healthMvcEndpoint = null;
5759
while (iterator.hasNext()) {
58-
if (iterator.next() instanceof HalJsonMvcEndpoint) {
60+
NamedMvcEndpoint endpoint = iterator.next();
61+
if (endpoint instanceof HalJsonMvcEndpoint) {
5962
iterator.remove();
6063
}
64+
else if (endpoint instanceof HealthMvcEndpoint) {
65+
iterator.remove();
66+
healthMvcEndpoint = (HealthMvcEndpoint) endpoint;
67+
}
68+
}
69+
if (healthMvcEndpoint != null) {
70+
endpoints.add(
71+
new CloudFoundryHealthMvcEndpoint(healthMvcEndpoint.getDelegate()));
6172
}
6273
}
6374

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2012-2016 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.cloudfoundry;
18+
19+
import java.security.Principal;
20+
21+
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
22+
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
23+
24+
/**
25+
* Extension of {@link HealthMvcEndpoint} for Cloud Foundry. Since security for Cloud
26+
* Foundry actuators is already handled by the {@link CloudFoundrySecurityInterceptor},
27+
* this endpoint skips the additional security checks done by the regular
28+
* {@link HealthMvcEndpoint}.
29+
*
30+
* @author Madhura Bhave
31+
*/
32+
class CloudFoundryHealthMvcEndpoint extends HealthMvcEndpoint {
33+
34+
CloudFoundryHealthMvcEndpoint(HealthEndpoint delegate) {
35+
super(delegate);
36+
}
37+
38+
@Override
39+
protected boolean exposeHealthDetails(Principal principal) {
40+
return true;
41+
}
42+
43+
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityInterceptor.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424

2525
import org.springframework.boot.actuate.cloudfoundry.CloudFoundryAuthorizationException.Reason;
2626
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
27+
import org.springframework.http.MediaType;
2728
import org.springframework.util.StringUtils;
2829
import org.springframework.web.cors.CorsUtils;
2930
import org.springframework.web.method.HandlerMethod;
@@ -74,6 +75,9 @@ public boolean preHandle(HttpServletRequest request, HttpServletResponse respons
7475
}
7576
catch (CloudFoundryAuthorizationException ex) {
7677
this.logger.error(ex);
78+
response.setContentType(MediaType.APPLICATION_JSON.toString());
79+
response.getWriter()
80+
.write("{\"security_error\":\"" + ex.getMessage() + "\"}");
7781
response.setStatus(ex.getStatusCode().value());
7882
return false;
7983
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ private boolean isCacheStale(long accessTime) {
178178
return (accessTime - this.lastAccess) >= getDelegate().getTimeToLive();
179179
}
180180

181-
private boolean exposeHealthDetails(Principal principal) {
181+
protected boolean exposeHealthDetails(Principal principal) {
182182
return isSecure(principal) || isUnrestricted();
183183
}
184184

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryEndpointHandlerMappingTests.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,15 @@
2222
import org.mockito.Mockito;
2323

2424
import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
25+
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
2526
import org.springframework.boot.actuate.endpoint.mvc.AbstractEndpointHandlerMappingTests;
2627
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
2728
import org.springframework.boot.actuate.endpoint.mvc.HalJsonMvcEndpoint;
29+
import org.springframework.boot.actuate.endpoint.mvc.HealthMvcEndpoint;
2830
import org.springframework.boot.actuate.endpoint.mvc.ManagementServletContext;
2931
import org.springframework.boot.actuate.endpoint.mvc.NamedMvcEndpoint;
32+
import org.springframework.boot.actuate.health.HealthIndicator;
33+
import org.springframework.boot.actuate.health.OrderedHealthAggregator;
3034
import org.springframework.context.support.StaticApplicationContext;
3135
import org.springframework.mock.web.MockHttpServletRequest;
3236
import org.springframework.web.method.HandlerMethod;
@@ -88,6 +92,23 @@ public void registersCloudFoundryDiscoveryEndpoint() throws Exception {
8892
.isInstanceOf(CloudFoundryDiscoveryMvcEndpoint.class);
8993
}
9094

95+
@Test
96+
public void registersCloudFoundryHealthEndpoint() throws Exception {
97+
StaticApplicationContext context = new StaticApplicationContext();
98+
HealthEndpoint delegate = new HealthEndpoint(new OrderedHealthAggregator(),
99+
Collections.<String, HealthIndicator>emptyMap());
100+
CloudFoundryEndpointHandlerMapping handlerMapping = new CloudFoundryEndpointHandlerMapping(
101+
Collections.singleton(new TestHealthMvcEndpoint(delegate)), null, null);
102+
handlerMapping.setPrefix("/test");
103+
handlerMapping.setApplicationContext(context);
104+
handlerMapping.afterPropertiesSet();
105+
HandlerExecutionChain handler = handlerMapping
106+
.getHandler(new MockHttpServletRequest("GET", "/test/health"));
107+
HandlerMethod handlerMethod = (HandlerMethod) handler.getHandler();
108+
Object handlerMethodBean = handlerMethod.getBean();
109+
assertThat(handlerMethodBean).isInstanceOf(CloudFoundryHealthMvcEndpoint.class);
110+
}
111+
91112
private static class TestEndpoint extends AbstractEndpoint<Object> {
92113

93114
TestEndpoint(String id) {
@@ -124,4 +145,12 @@ public String getContextPath() {
124145

125146
}
126147

148+
private static class TestHealthMvcEndpoint extends HealthMvcEndpoint {
149+
150+
TestHealthMvcEndpoint(HealthEndpoint delegate) {
151+
super(delegate);
152+
}
153+
154+
}
155+
127156
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/*
2+
* Copyright 2012-2016 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.cloudfoundry;
18+
19+
import org.junit.Test;
20+
21+
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
22+
import org.springframework.boot.actuate.health.Health;
23+
import org.springframework.boot.actuate.health.Status;
24+
25+
import static org.assertj.core.api.Assertions.assertThat;
26+
import static org.mockito.BDDMockito.given;
27+
import static org.mockito.Mockito.mock;
28+
29+
/**
30+
* Tests for {@link CloudFoundryHealthMvcEndpoint}.
31+
*
32+
* @author Madhura Bhave
33+
*/
34+
public class CloudFoundryHealthMvcEndpointTests {
35+
36+
@Test
37+
public void cloudFoundryHealthEndpointShouldAlwaysReturnAllHealthDetails()
38+
throws Exception {
39+
HealthEndpoint endpoint = mock(HealthEndpoint.class);
40+
given(endpoint.isEnabled()).willReturn(true);
41+
CloudFoundryHealthMvcEndpoint mvc = new CloudFoundryHealthMvcEndpoint(endpoint);
42+
given(endpoint.invoke())
43+
.willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
44+
given(endpoint.isSensitive()).willReturn(false);
45+
Object result = mvc.invoke(null);
46+
assertThat(result instanceof Health).isTrue();
47+
assertThat(((Health) result).getStatus() == Status.UP).isTrue();
48+
assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar");
49+
}
50+
51+
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityInterceptorTests.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.boot.actuate.endpoint.mvc.EndpointMvcAdapter;
2929
import org.springframework.http.HttpHeaders;
3030
import org.springframework.http.HttpStatus;
31+
import org.springframework.http.MediaType;
3132
import org.springframework.mock.web.MockHttpServletRequest;
3233
import org.springframework.mock.web.MockHttpServletResponse;
3334
import org.springframework.util.Base64Utils;
@@ -87,6 +88,9 @@ public void preHandleWhenTokenIsMissingShouldReturnFalse() throws Exception {
8788
assertThat(preHandle).isFalse();
8889
assertThat(this.response.getStatus())
8990
.isEqualTo(Reason.MISSING_AUTHORIZATION.getStatus().value());
91+
assertThat(this.response.getContentAsString()).contains("security_error");
92+
assertThat(this.response.getContentType())
93+
.isEqualTo(MediaType.APPLICATION_JSON.toString());
9094
}
9195

9296
@Test

0 commit comments

Comments
 (0)