Commit 4882544c authored by Stephane Nicoll's avatar Stephane Nicoll

Polish contribution

Closes gh-6540
parent dced154f
...@@ -18,7 +18,6 @@ package org.springframework.boot.actuate.endpoint.mvc; ...@@ -18,7 +18,6 @@ package org.springframework.boot.actuate.endpoint.mvc;
import java.security.Principal; import java.security.Principal;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
...@@ -190,10 +189,7 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint ...@@ -190,10 +189,7 @@ public class HealthMvcEndpoint extends AbstractEndpointMvcAdapter<HealthEndpoint
if (isSpringSecurityAuthentication(principal)) { if (isSpringSecurityAuthentication(principal)) {
Authentication authentication = (Authentication) principal; Authentication authentication = (Authentication) principal;
List<String> roles = Arrays.asList(StringUtils.trimArrayElements(StringUtils List<String> roles = Arrays.asList(StringUtils.trimArrayElements(StringUtils
.commaDelimitedListToStringArray(this.roleResolver.getProperty("roles")))); .commaDelimitedListToStringArray(this.roleResolver.getProperty("roles", "ROLE_ADMIN"))));
if (roles.isEmpty()) {
roles = Collections.singletonList("ROLE_ADMIN");
}
for (GrantedAuthority authority : authentication.getAuthorities()) { for (GrantedAuthority authority : authentication.getAuthorities()) {
String name = authority.getAuthority(); String name = authority.getAuthority();
for (String role : roles) { for (String role : roles) {
......
...@@ -60,13 +60,17 @@ public class HealthMvcEndpointTests { ...@@ -60,13 +60,17 @@ public class HealthMvcEndpointTests {
private MockEnvironment environment; private MockEnvironment environment;
private UsernamePasswordAuthenticationToken user = new UsernamePasswordAuthenticationToken( private UsernamePasswordAuthenticationToken user = createAuthenticationToken("ROLE_USER");
"user", "password",
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_USER"));
private UsernamePasswordAuthenticationToken admin = new UsernamePasswordAuthenticationToken( private UsernamePasswordAuthenticationToken admin = createAuthenticationToken("ROLE_ADMIN");
"user", "password",
AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_ADMIN")); private UsernamePasswordAuthenticationToken hero = createAuthenticationToken("ROLE_HERO");
private UsernamePasswordAuthenticationToken createAuthenticationToken(String authority) {
return new UsernamePasswordAuthenticationToken(
"user", "password",
AuthorityUtils.commaSeparatedStringToAuthorityList(authority));
}
@Before @Before
public void init() { public void init() {
...@@ -147,17 +151,26 @@ public class HealthMvcEndpointTests { ...@@ -147,17 +151,26 @@ public class HealthMvcEndpointTests {
@Test @Test
public void secureCustomRole() { public void secureCustomRole() {
this.mvc = new HealthMvcEndpoint(this.endpoint, false);
this.mvc.setEnvironment(this.environment);
this.environment.getPropertySources().addLast(SECURITY_ROLES); this.environment.getPropertySources().addLast(SECURITY_ROLES);
given(this.endpoint.invoke()) given(this.endpoint.invoke())
.willReturn(new Health.Builder().up().withDetail("foo", "bar").build()); .willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
Object result = this.mvc.invoke(this.user); Object result = this.mvc.invoke(this.hero);
assertThat(result instanceof Health).isTrue(); assertThat(result instanceof Health).isTrue();
assertThat(((Health) result).getStatus() == Status.UP).isTrue(); assertThat(((Health) result).getStatus() == Status.UP).isTrue();
assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar"); assertThat(((Health) result).getDetails().get("foo")).isEqualTo("bar");
} }
@Test
public void secureCustomRoleNoAccess() {
this.environment.getPropertySources().addLast(SECURITY_ROLES);
given(this.endpoint.invoke())
.willReturn(new Health.Builder().up().withDetail("foo", "bar").build());
Object result = this.mvc.invoke(this.admin);
assertThat(result instanceof Health).isTrue();
assertThat(((Health) result).getStatus() == Status.UP).isTrue();
assertThat(((Health) result).getDetails().get("foo")).isNull();
}
@Test @Test
public void healthIsCached() { public void healthIsCached() {
given(this.endpoint.getTimeToLive()).willReturn(10000L); given(this.endpoint.getTimeToLive()).willReturn(10000L);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment