33
33
import org .springframework .boot .actuate .endpoint .EndpointId ;
34
34
import org .springframework .boot .actuate .endpoint .annotation .Endpoint ;
35
35
import org .springframework .boot .actuate .endpoint .annotation .ReadOperation ;
36
+ import org .springframework .boot .actuate .endpoint .annotation .WriteOperation ;
36
37
import org .springframework .boot .actuate .endpoint .web .EndpointMapping ;
37
38
import org .springframework .boot .actuate .endpoint .web .ExposableWebEndpoint ;
38
39
import org .springframework .boot .actuate .endpoint .web .WebOperation ;
49
50
import org .springframework .boot .test .context .runner .WebApplicationContextRunner ;
50
51
import org .springframework .context .ApplicationContext ;
51
52
import org .springframework .http .HttpMethod ;
53
+ import org .springframework .http .MediaType ;
52
54
import org .springframework .mock .web .MockHttpServletRequest ;
53
55
import org .springframework .security .config .BeanIds ;
54
56
import org .springframework .security .web .FilterChainProxy ;
55
57
import org .springframework .security .web .SecurityFilterChain ;
56
58
import org .springframework .test .util .ReflectionTestUtils ;
59
+ import org .springframework .test .web .servlet .MockMvc ;
57
60
import org .springframework .test .web .servlet .assertj .MockMvcTester ;
61
+ import org .springframework .test .web .servlet .setup .MockMvcBuilders ;
58
62
import org .springframework .web .client .RestTemplate ;
59
63
import org .springframework .web .cors .CorsConfiguration ;
60
64
import org .springframework .web .filter .CompositeFilter ;
61
65
62
66
import static org .assertj .core .api .Assertions .assertThat ;
67
+ import static org .springframework .security .test .web .servlet .request .SecurityMockMvcRequestPostProcessors .csrf ;
68
+ import static org .springframework .security .test .web .servlet .setup .SecurityMockMvcConfigurers .springSecurity ;
69
+ import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .post ;
70
+ import static org .springframework .test .web .servlet .result .MockMvcResultMatchers .status ;
63
71
64
72
/**
65
73
* Tests for {@link CloudFoundryActuatorAutoConfiguration}.
@@ -170,7 +178,7 @@ void cloudFoundryPlatformActiveAndCloudControllerUrlNotPresent() {
170
178
}
171
179
172
180
@ Test
173
- void cloudFoundryPathsIgnoredBySpringSecurity () {
181
+ void cloudFoundryPathsPermittedBySpringSecurity () {
174
182
this .contextRunner .withBean (TestEndpoint .class , TestEndpoint ::new )
175
183
.withPropertyValues ("VCAP_APPLICATION:---" , "vcap.application.application_id:my-app-id" )
176
184
.run ((context ) -> {
@@ -189,6 +197,19 @@ void cloudFoundryPathsIgnoredBySpringSecurity() {
189
197
});
190
198
}
191
199
200
+ @ Test
201
+ void cloudFoundryPathsPermittedWithCsrfBySpringSecurity () {
202
+ this .contextRunner .withBean (TestEndpoint .class , TestEndpoint ::new )
203
+ .withPropertyValues ("VCAP_APPLICATION:---" , "vcap.application.application_id:my-app-id" )
204
+ .run ((context ) -> {
205
+ MockMvc mvc = MockMvcBuilders .webAppContextSetup (context ).apply (springSecurity ()).build ();
206
+ mvc .perform (post (BASE_PATH + "/test?name=test" ).contentType (MediaType .APPLICATION_JSON )
207
+ .with (csrf ().useInvalidToken ())).andExpect (status ().isServiceUnavailable ());
208
+ // If CSRF fails we'll get a 403, if it works we get service unavailable
209
+ // because of "Cloud controller URL is not available"
210
+ });
211
+ }
212
+
192
213
private SecurityFilterChain getSecurityFilterChain (AssertableWebApplicationContext context ) {
193
214
Filter springSecurityFilterChain = context .getBean (BeanIds .SPRING_SECURITY_FILTER_CHAIN , Filter .class );
194
215
FilterChainProxy filterChainProxy = getFilterChainProxy (springSecurityFilterChain );
@@ -258,7 +279,7 @@ void endpointPathCustomizationIsNotApplied() {
258
279
.findFirst ()
259
280
.get ();
260
281
Collection <WebOperation > operations = endpoint .getOperations ();
261
- assertThat (operations ).hasSize (1 );
282
+ assertThat (operations ).hasSize (2 );
262
283
assertThat (operations .iterator ().next ().getRequestPredicate ().getPath ()).isEqualTo ("test" );
263
284
});
264
285
}
@@ -307,6 +328,10 @@ String hello() {
307
328
return "hello world" ;
308
329
}
309
330
331
+ @ WriteOperation
332
+ void update (String name ) {
333
+ }
334
+
310
335
}
311
336
312
337
}
0 commit comments