|
| 1 | +/* |
| 2 | + * Copyright 2012-2018 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 | +package org.springframework.boot.actuate.autoconfigure.security.servlet; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Base64; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.junit.Test; |
| 23 | + |
| 24 | +import org.springframework.boot.actuate.endpoint.EndpointId; |
| 25 | +import org.springframework.boot.actuate.endpoint.ExposableEndpoint; |
| 26 | +import org.springframework.boot.actuate.endpoint.Operation; |
| 27 | +import org.springframework.boot.actuate.endpoint.annotation.Endpoint; |
| 28 | +import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; |
| 29 | +import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoint; |
| 30 | +import org.springframework.boot.actuate.endpoint.web.PathMappedEndpoints; |
| 31 | +import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext; |
| 32 | +import org.springframework.boot.test.context.runner.WebApplicationContextRunner; |
| 33 | +import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext; |
| 34 | +import org.springframework.context.annotation.Bean; |
| 35 | +import org.springframework.context.annotation.Configuration; |
| 36 | +import org.springframework.security.config.annotation.web.builders.HttpSecurity; |
| 37 | +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; |
| 38 | +import org.springframework.test.web.reactive.server.WebTestClient; |
| 39 | + |
| 40 | +import static org.mockito.BDDMockito.given; |
| 41 | +import static org.mockito.Mockito.mock; |
| 42 | + |
| 43 | +/** |
| 44 | + * Abstract base class for {@link EndpointRequest} tests. |
| 45 | + * |
| 46 | + * @author Madhura Bhave |
| 47 | + */ |
| 48 | +public abstract class AbstractEndpointRequestIntegrationTests { |
| 49 | + |
| 50 | + protected abstract WebApplicationContextRunner getContextRunner(); |
| 51 | + |
| 52 | + @Test |
| 53 | + public void toEndpointShouldMatch() { |
| 54 | + getContextRunner().run((context) -> { |
| 55 | + WebTestClient webTestClient = getWebTestClient(context); |
| 56 | + webTestClient.get().uri("/actuator/e1").exchange().expectStatus().isOk(); |
| 57 | + }); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void toAllEndpointsShouldMatch() { |
| 62 | + getContextRunner().withPropertyValues("spring.security.user.password=password") |
| 63 | + .run((context) -> { |
| 64 | + WebTestClient webTestClient = getWebTestClient(context); |
| 65 | + webTestClient.get().uri("/actuator/e2").exchange().expectStatus() |
| 66 | + .isUnauthorized(); |
| 67 | + webTestClient.get().uri("/actuator/e2") |
| 68 | + .header("Authorization", getBasicAuth()).exchange() |
| 69 | + .expectStatus().isOk(); |
| 70 | + }); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void toLinksShouldMatch() { |
| 75 | + getContextRunner().run((context) -> { |
| 76 | + WebTestClient webTestClient = getWebTestClient(context); |
| 77 | + webTestClient.get().uri("/actuator").exchange().expectStatus().isOk(); |
| 78 | + webTestClient.get().uri("/actuator/").exchange().expectStatus().isOk(); |
| 79 | + }); |
| 80 | + } |
| 81 | + |
| 82 | + protected WebTestClient getWebTestClient(AssertableWebApplicationContext context) { |
| 83 | + int port = context |
| 84 | + .getSourceApplicationContext( |
| 85 | + AnnotationConfigServletWebServerApplicationContext.class) |
| 86 | + .getWebServer().getPort(); |
| 87 | + return WebTestClient.bindToServer().baseUrl("http://localhost:" + port).build(); |
| 88 | + } |
| 89 | + |
| 90 | + String getBasicAuth() { |
| 91 | + return "Basic " + Base64.getEncoder().encodeToString("user:password".getBytes()); |
| 92 | + } |
| 93 | + |
| 94 | + static class BaseConfiguration { |
| 95 | + |
| 96 | + @Bean |
| 97 | + public TestEndpoint1 endpoint1() { |
| 98 | + return new TestEndpoint1(); |
| 99 | + } |
| 100 | + |
| 101 | + @Bean |
| 102 | + public TestEndpoint2 endpoint2() { |
| 103 | + return new TestEndpoint2(); |
| 104 | + } |
| 105 | + |
| 106 | + @Bean |
| 107 | + public TestEndpoint3 endpoint3() { |
| 108 | + return new TestEndpoint3(); |
| 109 | + } |
| 110 | + |
| 111 | + @Bean |
| 112 | + public PathMappedEndpoints pathMappedEndpoints() { |
| 113 | + List<ExposableEndpoint<?>> endpoints = new ArrayList<>(); |
| 114 | + endpoints.add(mockEndpoint("e1")); |
| 115 | + endpoints.add(mockEndpoint("e2")); |
| 116 | + endpoints.add(mockEndpoint("e3")); |
| 117 | + return new PathMappedEndpoints("/actuator", () -> endpoints); |
| 118 | + } |
| 119 | + |
| 120 | + private TestPathMappedEndpoint mockEndpoint(String id) { |
| 121 | + TestPathMappedEndpoint endpoint = mock(TestPathMappedEndpoint.class); |
| 122 | + given(endpoint.getEndpointId()).willReturn(EndpointId.of(id)); |
| 123 | + given(endpoint.getRootPath()).willReturn(id); |
| 124 | + return endpoint; |
| 125 | + } |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + @Endpoint(id = "e1") |
| 130 | + static class TestEndpoint1 { |
| 131 | + |
| 132 | + @ReadOperation |
| 133 | + public Object getAll() { |
| 134 | + return "endpoint 1"; |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
| 139 | + @Endpoint(id = "e2") |
| 140 | + static class TestEndpoint2 { |
| 141 | + |
| 142 | + @ReadOperation |
| 143 | + public Object getAll() { |
| 144 | + return "endpoint 2"; |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + |
| 149 | + @Endpoint(id = "e3") |
| 150 | + static class TestEndpoint3 { |
| 151 | + |
| 152 | + @ReadOperation |
| 153 | + public Object getAll() { |
| 154 | + return null; |
| 155 | + } |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + interface TestPathMappedEndpoint |
| 160 | + extends ExposableEndpoint<Operation>, PathMappedEndpoint { |
| 161 | + |
| 162 | + } |
| 163 | + |
| 164 | + @Configuration |
| 165 | + static class SecurityConfiguration { |
| 166 | + |
| 167 | + @Bean |
| 168 | + public WebSecurityConfigurerAdapter webSecurityConfigurerAdapter() { |
| 169 | + return new WebSecurityConfigurerAdapter() { |
| 170 | + @Override |
| 171 | + protected void configure(HttpSecurity http) throws Exception { |
| 172 | + http.authorizeRequests().requestMatchers(EndpointRequest.toLinks()) |
| 173 | + .permitAll() |
| 174 | + .requestMatchers(EndpointRequest.to(TestEndpoint1.class)) |
| 175 | + .permitAll().requestMatchers(EndpointRequest.toAnyEndpoint()) |
| 176 | + .authenticated().anyRequest().hasRole("ADMIN").and() |
| 177 | + .httpBasic(); |
| 178 | + } |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + } |
| 183 | + |
| 184 | +} |
0 commit comments