Skip to content

Commit 8201c8d

Browse files
committed
Merge pull request #5074 from vpavic:multiple-management-roles
* pr/5074: Polish contribution Support configuration of multiple management roles
2 parents e9a226c + b02aba4 commit 8201c8d

File tree

7 files changed

+74
-13
lines changed

7 files changed

+74
-13
lines changed

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/CrshAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ public SpringAuthenticationProperties springAuthenticationProperties() {
196196
// overridden by ConfigurationProperties.
197197
SpringAuthenticationProperties authenticationProperties = new SpringAuthenticationProperties();
198198
if (this.management != null) {
199-
authenticationProperties.setRoles(
200-
new String[] { this.management.getSecurity().getRole() });
199+
List<String> roles = this.management.getSecurity().getRoles();
200+
authenticationProperties.setRoles(roles.toArray(new String[roles.size()]));
201201
}
202202
return authenticationProperties;
203203
}

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementServerProperties.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.springframework.boot.actuate.autoconfigure;
1818

1919
import java.net.InetAddress;
20+
import java.util.Collections;
21+
import java.util.List;
2022

2123
import javax.validation.constraints.NotNull;
2224

@@ -33,6 +35,7 @@
3335
*
3436
* @author Dave Syer
3537
* @author Stephane Nicoll
38+
* @author Vedran Pavic
3639
* @see ServerProperties
3740
*/
3841
@ConfigurationProperties(prefix = "management", ignoreUnknownFields = true)
@@ -160,9 +163,9 @@ public static class Security {
160163
private boolean enabled = true;
161164

162165
/**
163-
* Role required to access the management endpoint.
166+
* Comma-separated list of roles that can access the management endpoint.
164167
*/
165-
private String role = "ADMIN";
168+
private List<String> roles = Collections.singletonList("ADMIN");
166169

167170
/**
168171
* Session creating policy to use (always, never, if_required, stateless).
@@ -177,12 +180,17 @@ public void setSessions(SessionCreationPolicy sessions) {
177180
this.sessions = sessions;
178181
}
179182

183+
public void setRoles(List<String> roles) {
184+
this.roles = roles;
185+
}
186+
187+
@Deprecated
180188
public void setRole(String role) {
181-
this.role = role;
189+
this.roles = Collections.singletonList(role);
182190
}
183191

184-
public String getRole() {
185-
return this.role;
192+
public List<String> getRoles() {
193+
return this.roles;
186194
}
187195

188196
public boolean isEnabled() {

spring-boot-actuator/src/main/java/org/springframework/boot/actuate/autoconfigure/ManagementWebSecurityAutoConfiguration.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public ManagementSecurityPropertiesConfiguration(
124124
public void init() {
125125
if (this.management != null && this.security != null) {
126126
this.security.getUser().getRole()
127-
.add(this.management.getSecurity().getRole());
127+
.addAll(this.management.getSecurity().getRoles());
128128
}
129129
}
130130

@@ -296,8 +296,9 @@ private void configurePermittedRequests(
296296
// Permit access to the non-sensitive endpoints
297297
requests.requestMatchers(new LazyEndpointPathRequestMatcher(
298298
this.contextResolver, EndpointPaths.NON_SENSITIVE)).permitAll();
299-
// Restrict the rest to the configured role
300-
requests.anyRequest().hasRole(this.management.getSecurity().getRole());
299+
// Restrict the rest to the configured roles
300+
List<String> roles = this.management.getSecurity().getRoles();
301+
requests.anyRequest().hasAnyRole(roles.toArray(new String[roles.size()]));
301302
}
302303

303304
}

spring-boot-actuator/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,15 @@
163163
"description": "Enable git info.",
164164
"defaultValue": true
165165
},
166+
{
167+
"name": "management.security.role",
168+
"type": "java.lang.String",
169+
"description": "Roles required to access the management endpoint.",
170+
"defaultValue": "ADMIN",
171+
"deprecation": {
172+
"replacement": "management.security.roles"
173+
}
174+
},
166175
{
167176
"name": "spring.git.properties",
168177
"type": "java.lang.String",

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/ManagementServerPropertiesAutoConfigurationTests.java

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure;
1818

19+
import org.junit.After;
1920
import org.junit.Test;
2021

22+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
23+
import org.springframework.boot.test.util.EnvironmentTestUtils;
24+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
25+
import org.springframework.context.annotation.Configuration;
26+
2127
import static org.assertj.core.api.Assertions.assertThat;
2228

2329
/**
@@ -28,6 +34,15 @@
2834
*/
2935
public class ManagementServerPropertiesAutoConfigurationTests {
3036

37+
private AnnotationConfigApplicationContext context;
38+
39+
@After
40+
public void close() {
41+
if (this.context != null) {
42+
this.context.close();
43+
}
44+
}
45+
3146
@Test
3247
public void defaultManagementServerProperties() {
3348
ManagementServerProperties properties = new ManagementServerProperties();
@@ -58,4 +73,32 @@ public void slashOfContextPathIsDefaultValue() {
5873
assertThat(properties.getContextPath()).isEqualTo("");
5974
}
6075

76+
@Test
77+
@Deprecated
78+
public void managementRoleSetRolesProperly() {
79+
ManagementServerProperties properties = load("management.security.role=FOO");
80+
assertThat(properties.getSecurity().getRoles()).containsOnly("FOO");
81+
}
82+
83+
@Test
84+
public void managementRolesSetMultipleRoles() {
85+
ManagementServerProperties properties = load("management.security.roles=FOO,BAR,BIZ");
86+
assertThat(properties.getSecurity().getRoles()).containsOnly("FOO", "BAR", "BIZ");
87+
}
88+
89+
public ManagementServerProperties load(String... environment) {
90+
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
91+
EnvironmentTestUtils.addEnvironment(ctx, environment);
92+
ctx.register(TestConfiguration.class);
93+
ctx.refresh();
94+
this.context = ctx;
95+
return this.context.getBean(ManagementServerProperties.class);
96+
}
97+
98+
@Configuration
99+
@EnableConfigurationProperties(ManagementServerProperties.class)
100+
static class TestConfiguration {
101+
102+
}
103+
61104
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ content into your application; rather pick only the properties that you need.
996996
management.context-path= # Management endpoint context-path. For instance `/actuator`
997997
management.port= # Management endpoint HTTP port. Use the same port as the application by default.
998998
management.security.enabled=true # Enable security.
999-
management.security.role=ADMIN # Role required to access the management endpoint.
999+
management.security.roles=ADMIN # Comma-separated list of roles that can access the management endpoint.
10001000
management.security.sessions=stateless # Session creating policy to use (always, never, if_required, stateless).
10011001
10021002
# HEALTH INDICATORS (previously health.*)

spring-boot-docs/src/main/asciidoc/production-ready-features.adoc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -520,14 +520,14 @@ TIP: Generated passwords are logged as the application starts. Search for '`Usin
520520
security password`'.
521521

522522
You can use Spring properties to change the username and password and to change the
523-
security role required to access the endpoints. For example, you might set the following
523+
security role(s) required to access the endpoints. For example, you might set the following
524524
in your `application.properties`:
525525

526526
[source,properties,indent=0]
527527
----
528528
security.user.name=admin
529529
security.user.password=secret
530-
management.security.role=SUPERUSER
530+
management.security.roles=SUPERUSER
531531
----
532532

533533
TIP: If you don't use Spring Security and your HTTP endpoints are exposed publicly,

0 commit comments

Comments
 (0)