Skip to content

Commit df5dfbf

Browse files
committed
Support mixed case endpoint includes/excludes
Update `ExposeExcludePropertyEndpointFilter` so that mixed case endpoint IDs are supported. Prior to this commit it was not easy for an endpoint to be missed by the filter due to the formatting of the property value. See spring-projectsgh-14773
1 parent 674a909 commit df5dfbf

File tree

4 files changed

+34
-1
lines changed

4 files changed

+34
-1
lines changed

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,13 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.HashSet;
24+
import java.util.List;
2425
import java.util.Locale;
2526
import java.util.Set;
2627
import java.util.stream.Collectors;
2728

2829
import org.springframework.boot.actuate.endpoint.EndpointFilter;
30+
import org.springframework.boot.actuate.endpoint.EndpointId;
2931
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
3032
import org.springframework.boot.context.properties.bind.Bindable;
3133
import org.springframework.boot.context.properties.bind.Binder;
@@ -74,10 +76,19 @@ public ExposeExcludePropertyEndpointFilter(Class<E> endpointType,
7476
}
7577

7678
private Set<String> bind(Binder binder, String name) {
77-
return asSet(binder.bind(name, Bindable.listOf(String.class))
79+
return asSet(binder.bind(name, Bindable.listOf(String.class)).map(this::cleanup)
7880
.orElseGet(ArrayList::new));
7981
}
8082

83+
private List<String> cleanup(List<String> values) {
84+
return values.stream().map(this::cleanup).collect(Collectors.toList());
85+
}
86+
87+
private String cleanup(String value) {
88+
return "*".equals(value) ? "*"
89+
: EndpointId.fromPropertyValue(value).toLowerCaseString();
90+
}
91+
8192
private Set<String> asSet(Collection<String> items) {
8293
if (items == null) {
8394
return Collections.emptySet();

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,12 @@ public void matchWhenExcludeIsAsteriskShouldMatchNone() {
147147
assertThat(match(EndpointId.of("buz"))).isFalse();
148148
}
149149

150+
@Test
151+
public void matchWhenMixedCaseShouldMatch() {
152+
setupFilter("foo-bar", "");
153+
assertThat(match(EndpointId.of("fooBar"))).isTrue();
154+
}
155+
150156
private void setupFilter(String include, String exclude) {
151157
MockEnvironment environment = new MockEnvironment();
152158
environment.setProperty("foo.include", include);

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/endpoint/EndpointId.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,14 @@ public static EndpointId of(String value) {
8787
return new EndpointId(value);
8888
}
8989

90+
/**
91+
* Factory method to create a new {@link EndpointId} from a property value. Is more
92+
* lenient that {@link #of(String)} to allow for common "relaxed" property variants.
93+
* @param value the property value to convert
94+
* @return an {@link EndpointId} instance
95+
*/
96+
public static EndpointId fromPropertyValue(String value) {
97+
return new EndpointId(value.replace("-", ""));
98+
}
99+
90100
}

spring-boot-project/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/EndpointIdTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,10 @@ public void toStringReturnsString() {
9393
assertThat(EndpointId.of("fooBar").toString()).isEqualTo("fooBar");
9494
}
9595

96+
@Test
97+
public void fromPropertyValueStripsDashes() {
98+
EndpointId fromPropertyValue = EndpointId.fromPropertyValue("foo-bar");
99+
assertThat(fromPropertyValue).isEqualTo(EndpointId.of("fooBar"));
100+
}
101+
96102
}

0 commit comments

Comments
 (0)