Merge branch '2.1.x'
Closes gh-17079
This commit is contained in:
@@ -33,10 +33,9 @@ public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
|
||||
@Bean
|
||||
public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
|
||||
return new InMemoryUserDetailsManager(
|
||||
User.withDefaultPasswordEncoder().username("user").password("password")
|
||||
.authorities("ROLE_USER").build(),
|
||||
User.withDefaultPasswordEncoder().username("beans").password("beans")
|
||||
.authorities("ROLE_BEANS").build(),
|
||||
User.withDefaultPasswordEncoder().username("user").password("password").authorities("ROLE_USER")
|
||||
.build(),
|
||||
User.withDefaultPasswordEncoder().username("beans").password("beans").authorities("ROLE_BEANS").build(),
|
||||
User.withDefaultPasswordEncoder().username("admin").password("admin")
|
||||
.authorities("ROLE_ACTUATOR", "ROLE_USER").build());
|
||||
}
|
||||
|
||||
@@ -52,36 +52,31 @@ class CorsSampleActuatorApplicationTests {
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
RestTemplateBuilder builder = new RestTemplateBuilder();
|
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(
|
||||
this.applicationContext.getEnvironment(), "http");
|
||||
LocalHostUriTemplateHandler handler = new LocalHostUriTemplateHandler(this.applicationContext.getEnvironment(),
|
||||
"http");
|
||||
builder = builder.uriTemplateHandler(handler);
|
||||
this.testRestTemplate = new TestRestTemplate(builder);
|
||||
}
|
||||
|
||||
@Test
|
||||
void endpointShouldReturnUnauthorized() {
|
||||
ResponseEntity<?> entity = this.testRestTemplate.getForEntity("/actuator/env",
|
||||
Map.class);
|
||||
ResponseEntity<?> entity = this.testRestTemplate.getForEntity("/actuator/env", Map.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void preflightRequestToEndpointShouldReturnOk() throws Exception {
|
||||
RequestEntity<?> healthRequest = RequestEntity.options(new URI("/actuator/env"))
|
||||
.header("Origin", "http://localhost:8080")
|
||||
.header("Access-Control-Request-Method", "GET").build();
|
||||
ResponseEntity<?> exchange = this.testRestTemplate.exchange(healthRequest,
|
||||
Map.class);
|
||||
.header("Origin", "http://localhost:8080").header("Access-Control-Request-Method", "GET").build();
|
||||
ResponseEntity<?> exchange = this.testRestTemplate.exchange(healthRequest, Map.class);
|
||||
assertThat(exchange.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
void preflightRequestWhenCorsConfigInvalidShouldReturnForbidden() throws Exception {
|
||||
RequestEntity<?> entity = RequestEntity.options(new URI("/actuator/env"))
|
||||
.header("Origin", "http://localhost:9095")
|
||||
.header("Access-Control-Request-Method", "GET").build();
|
||||
ResponseEntity<byte[]> exchange = this.testRestTemplate.exchange(entity,
|
||||
byte[].class);
|
||||
.header("Origin", "http://localhost:9095").header("Access-Control-Request-Method", "GET").build();
|
||||
ResponseEntity<byte[]> exchange = this.testRestTemplate.exchange(entity, byte[].class);
|
||||
assertThat(exchange.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
|
||||
@@ -35,8 +35,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
* @author Madhura Bhave
|
||||
*/
|
||||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
|
||||
properties = { "management.server.port=0",
|
||||
"management.server.servlet.context-path=/management" })
|
||||
properties = { "management.server.port=0", "management.server.servlet.context-path=/management" })
|
||||
class ManagementPortAndPathSampleActuatorApplicationTests {
|
||||
|
||||
@LocalServerPort
|
||||
@@ -55,24 +54,22 @@ class ManagementPortAndPathSampleActuatorApplicationTests {
|
||||
|
||||
@Test
|
||||
void actuatorPathOnMainPortShouldNotMatch() {
|
||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||
"http://localhost:" + this.port + "/actuator/health", String.class);
|
||||
ResponseEntity<String> entity = new TestRestTemplate()
|
||||
.getForEntity("http://localhost:" + this.port + "/actuator/health", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testSecureActuator() {
|
||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||
"http://localhost:" + this.managementPort + "/management/actuator/env",
|
||||
String.class);
|
||||
ResponseEntity<String> entity = new TestRestTemplate()
|
||||
.getForEntity("http://localhost:" + this.managementPort + "/management/actuator/env", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInsecureActuator() {
|
||||
ResponseEntity<String> entity = new TestRestTemplate().getForEntity(
|
||||
"http://localhost:" + this.managementPort + "/management/actuator/health",
|
||||
String.class);
|
||||
ResponseEntity<String> entity = new TestRestTemplate()
|
||||
.getForEntity("http://localhost:" + this.managementPort + "/management/actuator/health", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
|
||||
}
|
||||
@@ -80,8 +77,7 @@ class ManagementPortAndPathSampleActuatorApplicationTests {
|
||||
@Test
|
||||
void testMissing() {
|
||||
ResponseEntity<String> entity = new TestRestTemplate("admin", "admin")
|
||||
.getForEntity("http://localhost:" + this.managementPort
|
||||
+ "/management/actuator/missing", String.class);
|
||||
.getForEntity("http://localhost:" + this.managementPort + "/management/actuator/missing", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND);
|
||||
assertThat(entity.getBody()).contains("\"status\":404");
|
||||
}
|
||||
|
||||
@@ -58,30 +58,26 @@ class SampleActuatorCustomSecurityApplicationTests {
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Object> body = entity.getBody();
|
||||
assertThat((String) body.get("message"))
|
||||
.contains("Expected exception in controller");
|
||||
assertThat((String) body.get("message")).contains("Expected exception in controller");
|
||||
}
|
||||
|
||||
@Test
|
||||
void testInsecureStaticResources() {
|
||||
ResponseEntity<String> entity = restTemplate()
|
||||
.getForEntity("/css/bootstrap.min.css", String.class);
|
||||
ResponseEntity<String> entity = restTemplate().getForEntity("/css/bootstrap.min.css", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getBody()).contains("body");
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorInsecureEndpoint() {
|
||||
ResponseEntity<String> entity = restTemplate().getForEntity("/actuator/health",
|
||||
String.class);
|
||||
ResponseEntity<String> entity = restTemplate().getForEntity("/actuator/health", String.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getBody()).contains("\"status\":\"UP\"");
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorLinksIsSecure() {
|
||||
ResponseEntity<Object> entity = restTemplate().getForEntity("/actuator",
|
||||
Object.class);
|
||||
ResponseEntity<Object> entity = restTemplate().getForEntity("/actuator", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
entity = adminRestTemplate().getForEntity("/actuator", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
@@ -89,43 +85,40 @@ class SampleActuatorCustomSecurityApplicationTests {
|
||||
|
||||
@Test
|
||||
void actuatorSecureEndpointWithAnonymous() {
|
||||
ResponseEntity<Object> entity = restTemplate().getForEntity("/actuator/env",
|
||||
Object.class);
|
||||
ResponseEntity<Object> entity = restTemplate().getForEntity("/actuator/env", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorSecureEndpointWithUnauthorizedUser() {
|
||||
ResponseEntity<Object> entity = userRestTemplate().getForEntity("/actuator/env",
|
||||
Object.class);
|
||||
ResponseEntity<Object> entity = userRestTemplate().getForEntity("/actuator/env", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorSecureEndpointWithAuthorizedUser() {
|
||||
ResponseEntity<Object> entity = adminRestTemplate().getForEntity("/actuator/env",
|
||||
Object.class);
|
||||
ResponseEntity<Object> entity = adminRestTemplate().getForEntity("/actuator/env", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorCustomMvcSecureEndpointWithAnonymous() {
|
||||
ResponseEntity<String> entity = restTemplate()
|
||||
.getForEntity("/actuator/example/echo?text={t}", String.class, "test");
|
||||
ResponseEntity<String> entity = restTemplate().getForEntity("/actuator/example/echo?text={t}", String.class,
|
||||
"test");
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.UNAUTHORIZED);
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorCustomMvcSecureEndpointWithUnauthorizedUser() {
|
||||
ResponseEntity<String> entity = userRestTemplate()
|
||||
.getForEntity("/actuator/example/echo?text={t}", String.class, "test");
|
||||
ResponseEntity<String> entity = userRestTemplate().getForEntity("/actuator/example/echo?text={t}", String.class,
|
||||
"test");
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.FORBIDDEN);
|
||||
}
|
||||
|
||||
@Test
|
||||
void actuatorCustomMvcSecureEndpointWithAuthorizedUser() {
|
||||
ResponseEntity<String> entity = adminRestTemplate()
|
||||
.getForEntity("/actuator/example/echo?text={t}", String.class, "test");
|
||||
ResponseEntity<String> entity = adminRestTemplate().getForEntity("/actuator/example/echo?text={t}",
|
||||
String.class, "test");
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(entity.getBody()).isEqualTo("test");
|
||||
assertThat(entity.getHeaders().getFirst("echo")).isEqualTo("test");
|
||||
@@ -133,15 +126,13 @@ class SampleActuatorCustomSecurityApplicationTests {
|
||||
|
||||
@Test
|
||||
void actuatorExcludedFromEndpointRequestMatcher() {
|
||||
ResponseEntity<Object> entity = userRestTemplate()
|
||||
.getForEntity("/actuator/mappings", Object.class);
|
||||
ResponseEntity<Object> entity = userRestTemplate().getForEntity("/actuator/mappings", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
}
|
||||
|
||||
@Test
|
||||
void mvcMatchersCanBeUsedToSecureActuators() {
|
||||
ResponseEntity<Object> entity = beansRestTemplate()
|
||||
.getForEntity("/actuator/beans", Object.class);
|
||||
ResponseEntity<Object> entity = beansRestTemplate().getForEntity("/actuator/beans", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
entity = beansRestTemplate().getForEntity("/actuator/beans/", Object.class);
|
||||
assertThat(entity.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
@@ -164,8 +155,7 @@ class SampleActuatorCustomSecurityApplicationTests {
|
||||
}
|
||||
|
||||
private TestRestTemplate configure(TestRestTemplate restTemplate) {
|
||||
restTemplate
|
||||
.setUriTemplateHandler(new LocalHostUriTemplateHandler(this.environment));
|
||||
restTemplate.setUriTemplateHandler(new LocalHostUriTemplateHandler(this.environment));
|
||||
return restTemplate;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user