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
This commit is contained in:
Andy Wilkinson
2024-11-28 14:07:27 +00:00
parent b5feadab13
commit f3a161afdd
2 changed files with 21 additions and 2 deletions

View File

@@ -80,10 +80,10 @@ public class PropertiesEndpointAccessResolver implements EndpointAccessResolver
@Override
public Access accessFor(EndpointId endpointId, Access defaultAccess) {
return this.accessCache.computeIfAbsent(endpointId,
(key) -> resolveAccess(endpointId, defaultAccess).cap(this.maxPermittedAccess));
(key) -> resolveAccess(endpointId.toLowerCaseString(), defaultAccess).cap(this.maxPermittedAccess));
}
private Access resolveAccess(EndpointId endpointId, Access defaultAccess) {
private Access resolveAccess(String endpointId, Access defaultAccess) {
String accessKey = "management.endpoint.%s.access".formatted(endpointId);
String enabledKey = "management.endpoint.%s.enabled".formatted(endpointId);
Access access = this.properties.getProperty(accessKey, Access.class);

View File

@@ -20,6 +20,7 @@ import org.junit.jupiter.api.Test;
import org.springframework.boot.actuate.endpoint.Access;
import org.springframework.boot.actuate.endpoint.EndpointId;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.context.properties.source.MutuallyExclusiveConfigurationPropertiesException;
import org.springframework.mock.env.MockEnvironment;
@@ -35,6 +36,10 @@ class PropertiesEndpointAccessResolverTests {
private final MockEnvironment environment = new MockEnvironment();
PropertiesEndpointAccessResolverTests() {
ConfigurationPropertySources.attach(this.environment);
}
@Test
void whenNoPropertiesAreConfiguredThenAccessForReturnsEndpointsDefaultAccess() {
assertThat(accessResolver().accessFor(EndpointId.of("test"), Access.READ_ONLY)).isEqualTo(Access.READ_ONLY);
@@ -52,6 +57,13 @@ class PropertiesEndpointAccessResolverTests {
assertThat(accessResolver().accessFor(EndpointId.of("test"), Access.READ_ONLY)).isEqualTo(Access.UNRESTRICTED);
}
@Test
void whenAccessForEndpointWithCamelCaseIdIsConfiguredThenAccessForReturnsIt() {
this.environment.withProperty("management.endpoint.alpha-bravo.access", Access.UNRESTRICTED.name());
assertThat(accessResolver().accessFor(EndpointId.of("alphaBravo"), Access.READ_ONLY))
.isEqualTo(Access.UNRESTRICTED);
}
@Test
void whenAccessForEndpointAndDefaultAccessForAllEndpointsAreConfiguredAccessForReturnsAccessForEndpoint() {
this.environment.withProperty("management.endpoint.test.access", Access.NONE.name())
@@ -83,6 +95,13 @@ class PropertiesEndpointAccessResolverTests {
assertThat(accessResolver().accessFor(EndpointId.of("test"), Access.READ_ONLY)).isEqualTo(Access.UNRESTRICTED);
}
@Test
void whenEndpointWithCamelCaseIdIsEnabledAccessForReturnsUnrestricted() {
this.environment.withProperty("management.endpoint.alpha-bravo.enabled", "true");
assertThat(accessResolver().accessFor(EndpointId.of("alphaBravo"), Access.READ_ONLY))
.isEqualTo(Access.UNRESTRICTED);
}
@Test
void whenEnabledByDefaultAndDefaultAccessAreBothConfiguredResolverCreationThrows() {
this.environment.withProperty("management.endpoints.enabled-by-default", "true")