Skip to content

Commit f3a161a

Browse files
committedNov 28, 2024·
Enable relaxed matching of enabled and access properties
The lowercase form of the endpoint ID needs to be used so that relaxed matching of properties, as provided by ConfigurationPropertySources, works as intended. Without this change the id of the endpoint in a property had to be an exact match of the endpoint's ID. Closes gh-43302
1 parent b5feada commit f3a161a

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed
 

‎spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/PropertiesEndpointAccessResolver.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ private static Access determineDefaultAccess(PropertyResolver properties) {
8080
@Override
8181
public Access accessFor(EndpointId endpointId, Access defaultAccess) {
8282
return this.accessCache.computeIfAbsent(endpointId,
83-
(key) -> resolveAccess(endpointId, defaultAccess).cap(this.maxPermittedAccess));
83+
(key) -> resolveAccess(endpointId.toLowerCaseString(), defaultAccess).cap(this.maxPermittedAccess));
8484
}
8585

86-
private Access resolveAccess(EndpointId endpointId, Access defaultAccess) {
86+
private Access resolveAccess(String endpointId, Access defaultAccess) {
8787
String accessKey = "management.endpoint.%s.access".formatted(endpointId);
8888
String enabledKey = "management.endpoint.%s.enabled".formatted(endpointId);
8989
Access access = this.properties.getProperty(accessKey, Access.class);

‎spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/endpoint/PropertiesEndpointAccessResolverTests.java

+19
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
import org.springframework.boot.actuate.endpoint.Access;
2222
import org.springframework.boot.actuate.endpoint.EndpointId;
23+
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
2324
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
2425
import org.springframework.mock.env.MockEnvironment;
2526

@@ -35,6 +36,10 @@ class PropertiesEndpointAccessResolverTests {
3536

3637
private final MockEnvironment environment = new MockEnvironment();
3738

39+
PropertiesEndpointAccessResolverTests() {
40+
ConfigurationPropertySources.attach(this.environment);
41+
}
42+
3843
@Test
3944
void whenNoPropertiesAreConfiguredThenAccessForReturnsEndpointsDefaultAccess() {
4045
assertThat(accessResolver().accessFor(EndpointId.of("test"), Access.READ_ONLY)).isEqualTo(Access.READ_ONLY);
@@ -52,6 +57,13 @@ void whenAccessForEndpointIsConfiguredThenAccessForReturnsIt() {
5257
assertThat(accessResolver().accessFor(EndpointId.of("test"), Access.READ_ONLY)).isEqualTo(Access.UNRESTRICTED);
5358
}
5459

60+
@Test
61+
void whenAccessForEndpointWithCamelCaseIdIsConfiguredThenAccessForReturnsIt() {
62+
this.environment.withProperty("management.endpoint.alpha-bravo.access", Access.UNRESTRICTED.name());
63+
assertThat(accessResolver().accessFor(EndpointId.of("alphaBravo"), Access.READ_ONLY))
64+
.isEqualTo(Access.UNRESTRICTED);
65+
}
66+
5567
@Test
5668
void whenAccessForEndpointAndDefaultAccessForAllEndpointsAreConfiguredAccessForReturnsAccessForEndpoint() {
5769
this.environment.withProperty("management.endpoint.test.access", Access.NONE.name())
@@ -83,6 +95,13 @@ void whenEndpointIsEnabledAccessForReturnsUnrestricted() {
8395
assertThat(accessResolver().accessFor(EndpointId.of("test"), Access.READ_ONLY)).isEqualTo(Access.UNRESTRICTED);
8496
}
8597

98+
@Test
99+
void whenEndpointWithCamelCaseIdIsEnabledAccessForReturnsUnrestricted() {
100+
this.environment.withProperty("management.endpoint.alpha-bravo.enabled", "true");
101+
assertThat(accessResolver().accessFor(EndpointId.of("alphaBravo"), Access.READ_ONLY))
102+
.isEqualTo(Access.UNRESTRICTED);
103+
}
104+
86105
@Test
87106
void whenEnabledByDefaultAndDefaultAccessAreBothConfiguredResolverCreationThrows() {
88107
this.environment.withProperty("management.endpoints.enabled-by-default", "true")

0 commit comments

Comments
 (0)
Please sign in to comment.