diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfiguration.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfiguration.java index 9df978a816f..6026654ecdc 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfiguration.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfiguration.java @@ -84,9 +84,11 @@ public class CloudFoundryActuatorAutoConfiguration { private CloudFoundrySecurityService getCloudFoundrySecurityService( RestTemplateBuilder restTemplateBuilder, Environment environment) { String cloudControllerUrl = environment.getProperty("vcap.application.cf_api"); + boolean skipSslValidation = Boolean.parseBoolean( + environment.getProperty("management.cloudfoundry.skipSslValidation")); return cloudControllerUrl == null ? null - : new CloudFoundrySecurityService(restTemplateBuilder, - cloudControllerUrl); + : new CloudFoundrySecurityService(restTemplateBuilder, cloudControllerUrl, + skipSslValidation); } private CorsConfiguration getCorsConfiguration() { diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityService.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityService.java index ce6126ba883..723b53a7e95 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityService.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityService.java @@ -46,11 +46,14 @@ class CloudFoundrySecurityService { private String uaaUrl; CloudFoundrySecurityService(RestTemplateBuilder restTemplateBuilder, - String cloudControllerUrl) { + String cloudControllerUrl, boolean skipSslValidation) { Assert.notNull(restTemplateBuilder, "RestTemplateBuilder must not be null"); Assert.notNull(cloudControllerUrl, "CloudControllerUrl must not be null"); - this.restTemplate = restTemplateBuilder - .requestFactory(SkipSslVerificationHttpRequestFactory.class).build(); + if (skipSslValidation) { + restTemplateBuilder = restTemplateBuilder + .requestFactory(SkipSslVerificationHttpRequestFactory.class); + } + this.restTemplate = restTemplateBuilder.build(); this.cloudControllerUrl = cloudControllerUrl; } diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfigurationTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfigurationTests.java index 5de230a3533..374bdb977b3 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfigurationTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundryActuatorAutoConfigurationTests.java @@ -42,6 +42,7 @@ import org.springframework.mock.web.MockServletContext; import org.springframework.security.config.annotation.web.builders.WebSecurity.IgnoredRequestConfigurer; import org.springframework.security.web.util.matcher.RequestMatcher; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.web.client.RestTemplate; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; import org.springframework.web.cors.CorsConfiguration; @@ -117,6 +118,22 @@ public class CloudFoundryActuatorAutoConfigurationTests { assertThat(cloudControllerUrl).isEqualTo("http://my-cloud-controller.com"); } + @Test + public void skipSslValidation() throws Exception { + EnvironmentTestUtils.addEnvironment(this.context, + "management.cloudfoundry.skipSslValidation:true"); + this.context.refresh(); + CloudFoundryEndpointHandlerMapping handlerMapping = getHandlerMapping(); + Object interceptor = ReflectionTestUtils.getField(handlerMapping, + "securityInterceptor"); + Object interceptorSecurityService = ReflectionTestUtils.getField(interceptor, + "cloudFoundrySecurityService"); + RestTemplate restTemplate = (RestTemplate) ReflectionTestUtils + .getField(interceptorSecurityService, "restTemplate"); + assertThat(restTemplate.getRequestFactory()) + .isInstanceOf(SkipSslVerificationHttpRequestFactory.class); + } + @Test public void cloudFoundryPlatformActiveAndCloudControllerUrlNotPresent() throws Exception { diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityServiceTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityServiceTests.java index 585b69a759c..0f80c90fef2 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityServiceTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityServiceTests.java @@ -28,7 +28,9 @@ import org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer import org.springframework.boot.web.client.RestTemplateBuilder; import org.springframework.http.HttpStatus; import org.springframework.http.MediaType; +import org.springframework.test.util.ReflectionTestUtils; import org.springframework.test.web.client.MockRestServiceServer; +import org.springframework.web.client.RestTemplate; import static org.assertj.core.api.Assertions.assertThat; import static org.springframework.test.web.client.match.MockRestRequestMatchers.header; @@ -63,10 +65,33 @@ public class CloudFoundrySecurityServiceTests { public void setup() throws Exception { MockServerRestTemplateCustomizer mockServerCustomizer = new MockServerRestTemplateCustomizer(); RestTemplateBuilder builder = new RestTemplateBuilder(mockServerCustomizer); - this.securityService = new CloudFoundrySecurityService(builder, CLOUD_CONTROLLER); + this.securityService = new CloudFoundrySecurityService(builder, CLOUD_CONTROLLER, + false); this.server = mockServerCustomizer.getServer(); } + @Test + public void skipSslValidationWhenTrue() throws Exception { + RestTemplateBuilder builder = new RestTemplateBuilder(); + this.securityService = new CloudFoundrySecurityService(builder, CLOUD_CONTROLLER, + true); + RestTemplate restTemplate = (RestTemplate) ReflectionTestUtils + .getField(this.securityService, "restTemplate"); + assertThat(restTemplate.getRequestFactory()) + .isInstanceOf(SkipSslVerificationHttpRequestFactory.class); + } + + @Test + public void doNotskipSslValidationWhenFalse() throws Exception { + RestTemplateBuilder builder = new RestTemplateBuilder(); + this.securityService = new CloudFoundrySecurityService(builder, CLOUD_CONTROLLER, + false); + RestTemplate restTemplate = (RestTemplate) ReflectionTestUtils + .getField(this.securityService, "restTemplate"); + assertThat(restTemplate.getRequestFactory()) + .isNotInstanceOf(SkipSslVerificationHttpRequestFactory.class); + } + @Test public void getAccessLevelWhenSpaceDeveloperShouldReturnFull() throws Exception { String responseBody = "{\"read_sensitive_data\": true,\"read_basic_data\": true}";