18
18
19
19
import java .util .Arrays ;
20
20
import java .util .Collections ;
21
+ import java .util .Set ;
21
22
22
23
import javax .servlet .http .HttpServletRequest ;
23
24
34
35
import org .springframework .mock .env .MockEnvironment ;
35
36
import org .springframework .mock .web .MockHttpServletRequest ;
36
37
import org .springframework .mock .web .MockServletContext ;
38
+ import org .springframework .security .core .Authentication ;
39
+ import org .springframework .security .core .authority .SimpleGrantedAuthority ;
37
40
38
41
import static org .assertj .core .api .Assertions .assertThat ;
39
42
import static org .mockito .BDDMockito .given ;
43
+ import static org .mockito .Mockito .doReturn ;
40
44
import static org .mockito .Mockito .mock ;
41
45
42
46
/**
@@ -52,7 +56,7 @@ public class HealthMvcEndpointTests {
52
56
53
57
private static final PropertySource <?> SECURITY_ROLES = new MapPropertySource ("test" ,
54
58
Collections .<String , Object >singletonMap ("management.security.roles" ,
55
- "HERO, USER " ));
59
+ "HERO" ));
56
60
57
61
private HttpServletRequest request = new MockHttpServletRequest ();
58
62
@@ -62,13 +66,11 @@ public class HealthMvcEndpointTests {
62
66
63
67
private MockEnvironment environment ;
64
68
65
- private HttpServletRequest user = createAuthenticationToken ( "ROLE_USER " );
69
+ private HttpServletRequest defaultUser = createAuthenticationRequest ( "ROLE_ACTUATOR " );
66
70
67
- private HttpServletRequest actuator = createAuthenticationToken ( "ROLE_ACTUATOR " );
71
+ private HttpServletRequest hero = createAuthenticationRequest ( "HERO " );
68
72
69
- private HttpServletRequest hero = createAuthenticationToken ("ROLE_HERO" );
70
-
71
- private HttpServletRequest createAuthenticationToken (String role ) {
73
+ private HttpServletRequest createAuthenticationRequest (String role ) {
72
74
MockServletContext servletContext = new MockServletContext ();
73
75
servletContext .declareRoles (role );
74
76
return new MockHttpServletRequest (servletContext );
@@ -86,7 +88,7 @@ public void init() {
86
88
@ Test
87
89
public void up () {
88
90
given (this .endpoint .invoke ()).willReturn (new Health .Builder ().up ().build ());
89
- Object result = this .mvc .invoke (this .request );
91
+ Object result = this .mvc .invoke (this .request , null );
90
92
assertThat (result instanceof Health ).isTrue ();
91
93
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
92
94
}
@@ -95,7 +97,7 @@ public void up() {
95
97
@ Test
96
98
public void down () {
97
99
given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
98
- Object result = this .mvc .invoke (this .request );
100
+ Object result = this .mvc .invoke (this .request , null );
99
101
assertThat (result instanceof ResponseEntity ).isTrue ();
100
102
ResponseEntity <Health > response = (ResponseEntity <Health >) result ;
101
103
assertThat (response .getBody ().getStatus () == Status .DOWN ).isTrue ();
@@ -109,7 +111,7 @@ public void customMapping() {
109
111
.willReturn (new Health .Builder ().status ("OK" ).build ());
110
112
this .mvc .setStatusMapping (
111
113
Collections .singletonMap ("OK" , HttpStatus .INTERNAL_SERVER_ERROR ));
112
- Object result = this .mvc .invoke (this .request );
114
+ Object result = this .mvc .invoke (this .request , null );
113
115
assertThat (result instanceof ResponseEntity ).isTrue ();
114
116
ResponseEntity <Health > response = (ResponseEntity <Health >) result ;
115
117
assertThat (response .getBody ().getStatus ().equals (new Status ("OK" ))).isTrue ();
@@ -123,7 +125,7 @@ public void customMappingWithRelaxedName() {
123
125
.willReturn (new Health .Builder ().outOfService ().build ());
124
126
this .mvc .setStatusMapping (Collections .singletonMap ("out-of-service" ,
125
127
HttpStatus .INTERNAL_SERVER_ERROR ));
126
- Object result = this .mvc .invoke (this .request );
128
+ Object result = this .mvc .invoke (this .request , null );
127
129
assertThat (result instanceof ResponseEntity ).isTrue ();
128
130
ResponseEntity <Health > response = (ResponseEntity <Health >) result ;
129
131
assertThat (response .getBody ().getStatus ().equals (Status .OUT_OF_SERVICE )).isTrue ();
@@ -134,7 +136,7 @@ public void customMappingWithRelaxedName() {
134
136
public void presenceOfRightRoleShouldExposeDetails () {
135
137
given (this .endpoint .invoke ())
136
138
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
137
- Object result = this .mvc .invoke (this .actuator );
139
+ Object result = this .mvc .invoke (this .defaultUser , null );
138
140
assertThat (result instanceof Health ).isTrue ();
139
141
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
140
142
assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
@@ -145,7 +147,7 @@ public void managementSecurityDisabledShouldExposeDetails() throws Exception {
145
147
this .mvc = new HealthMvcEndpoint (this .endpoint , false );
146
148
given (this .endpoint .invoke ())
147
149
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
148
- Object result = this .mvc .invoke (this .user );
150
+ Object result = this .mvc .invoke (this .defaultUser , null );
149
151
assertThat (result instanceof Health ).isTrue ();
150
152
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
151
153
assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
@@ -155,18 +157,32 @@ public void managementSecurityDisabledShouldExposeDetails() throws Exception {
155
157
public void rightRoleNotPresentShouldNotExposeDetails () {
156
158
given (this .endpoint .invoke ())
157
159
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
158
- Object result = this .mvc .invoke (this .user );
160
+ Object result = this .mvc .invoke (this .hero , null );
159
161
assertThat (result instanceof Health ).isTrue ();
160
162
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
161
163
assertThat (((Health ) result ).getDetails ().get ("foo" )).isNull ();
162
164
}
163
165
166
+ @ Test
167
+ public void rightAuthorityPresentShouldExposeDetails () throws Exception {
168
+ this .environment .getPropertySources ().addLast (SECURITY_ROLES );
169
+ Authentication principal = mock (Authentication .class );
170
+ Set <SimpleGrantedAuthority > authorities = Collections .singleton (new SimpleGrantedAuthority ("HERO" ));
171
+ doReturn (authorities ).when (principal ).getAuthorities ();
172
+ given (this .endpoint .invoke ())
173
+ .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
174
+ Object result = this .mvc .invoke (this .defaultUser , principal );
175
+ assertThat (result instanceof Health ).isTrue ();
176
+ assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
177
+ assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
178
+ }
179
+
164
180
@ Test
165
181
public void customRolePresentShouldExposeDetails () {
166
182
this .environment .getPropertySources ().addLast (SECURITY_ROLES );
167
183
given (this .endpoint .invoke ())
168
184
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
169
- Object result = this .mvc .invoke (this .hero );
185
+ Object result = this .mvc .invoke (this .hero , null );
170
186
assertThat (result instanceof Health ).isTrue ();
171
187
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
172
188
assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
@@ -177,38 +193,51 @@ public void customRoleShouldNotExposeDetailsForDefaultRole() {
177
193
this .environment .getPropertySources ().addLast (SECURITY_ROLES );
178
194
given (this .endpoint .invoke ())
179
195
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
180
- Object result = this .mvc .invoke (this .actuator );
196
+ Object result = this .mvc .invoke (this .defaultUser , null );
181
197
assertThat (result instanceof Health ).isTrue ();
182
198
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
183
199
assertThat (((Health ) result ).getDetails ().get ("foo" )).isNull ();
184
200
}
185
201
186
202
@ Test
187
- public void customRoleFromListShouldNotExposeDetailsForDefaultRole () {
203
+ public void customRoleFromListShouldExposeDetails () {
188
204
// gh-8314
189
205
this .mvc = new HealthMvcEndpoint (this .endpoint , true ,
190
206
Arrays .asList ("HERO" , "USER" ));
191
207
given (this .endpoint .invoke ())
192
208
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
193
- Object result = this .mvc .invoke (this .hero );
209
+ Object result = this .mvc .invoke (this .hero , null );
194
210
assertThat (result instanceof Health ).isTrue ();
195
211
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
196
212
assertThat (((Health ) result ).getDetails ().get ("foo" )).isEqualTo ("bar" );
197
213
}
198
214
215
+ @ Test
216
+ public void customRoleFromListShouldNotExposeDetailsForDefaultRole () {
217
+ // gh-8314
218
+ this .mvc = new HealthMvcEndpoint (this .endpoint , true ,
219
+ Arrays .asList ("HERO" , "USER" ));
220
+ given (this .endpoint .invoke ())
221
+ .willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
222
+ Object result = this .mvc .invoke (this .defaultUser , null );
223
+ assertThat (result instanceof Health ).isTrue ();
224
+ assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
225
+ assertThat (((Health ) result ).getDetails ().get ("foo" )).isNull ();
226
+ }
227
+
199
228
@ Test
200
229
public void healthIsCached () {
201
230
given (this .endpoint .getTimeToLive ()).willReturn (10000L );
202
231
given (this .endpoint .invoke ())
203
232
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
204
- Object result = this .mvc .invoke (this .actuator );
233
+ Object result = this .mvc .invoke (this .defaultUser , null );
205
234
assertThat (result instanceof Health ).isTrue ();
206
235
Health health = (Health ) result ;
207
236
assertThat (health .getStatus () == Status .UP ).isTrue ();
208
237
assertThat (health .getDetails ()).hasSize (1 );
209
238
assertThat (health .getDetails ().get ("foo" )).isEqualTo ("bar" );
210
239
given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
211
- result = this .mvc .invoke (this .request ); // insecure now
240
+ result = this .mvc .invoke (this .request , null ); // insecure now
212
241
assertThat (result instanceof Health ).isTrue ();
213
242
health = (Health ) result ;
214
243
// so the result is cached
@@ -222,11 +251,11 @@ public void noCachingWhenTimeToLiveIsZero() {
222
251
given (this .endpoint .getTimeToLive ()).willReturn (0L );
223
252
given (this .endpoint .invoke ())
224
253
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
225
- Object result = this .mvc .invoke (this .request );
254
+ Object result = this .mvc .invoke (this .request , null );
226
255
assertThat (result instanceof Health ).isTrue ();
227
256
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
228
257
given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
229
- result = this .mvc .invoke (this .request );
258
+ result = this .mvc .invoke (this .request , null );
230
259
@ SuppressWarnings ("unchecked" )
231
260
Health health = ((ResponseEntity <Health >) result ).getBody ();
232
261
assertThat (health .getStatus () == Status .DOWN ).isTrue ();
@@ -237,12 +266,12 @@ public void newValueIsReturnedOnceTtlExpires() throws InterruptedException {
237
266
given (this .endpoint .getTimeToLive ()).willReturn (50L );
238
267
given (this .endpoint .invoke ())
239
268
.willReturn (new Health .Builder ().up ().withDetail ("foo" , "bar" ).build ());
240
- Object result = this .mvc .invoke (this .request );
269
+ Object result = this .mvc .invoke (this .request , null );
241
270
assertThat (result instanceof Health ).isTrue ();
242
271
assertThat (((Health ) result ).getStatus () == Status .UP ).isTrue ();
243
272
Thread .sleep (100 );
244
273
given (this .endpoint .invoke ()).willReturn (new Health .Builder ().down ().build ());
245
- result = this .mvc .invoke (this .request );
274
+ result = this .mvc .invoke (this .request , null );
246
275
@ SuppressWarnings ("unchecked" )
247
276
Health health = ((ResponseEntity <Health >) result ).getBody ();
248
277
assertThat (health .getStatus () == Status .DOWN ).isTrue ();
0 commit comments