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 gh-14773
This commit is contained in:
Phillip Webb 2018-10-13 23:20:16 -07:00
parent 674a909bab
commit df5dfbf4be
4 changed files with 34 additions and 1 deletions

View File

@ -21,11 +21,13 @@ import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Locale;
import java.util.Set;
import java.util.stream.Collectors;
import org.springframework.boot.actuate.endpoint.EndpointFilter;
import org.springframework.boot.actuate.endpoint.EndpointId;
import org.springframework.boot.actuate.endpoint.ExposableEndpoint;
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
@ -74,10 +76,19 @@ public class ExposeExcludePropertyEndpointFilter<E extends ExposableEndpoint<?>>
}
private Set<String> bind(Binder binder, String name) {
return asSet(binder.bind(name, Bindable.listOf(String.class))
return asSet(binder.bind(name, Bindable.listOf(String.class)).map(this::cleanup)
.orElseGet(ArrayList::new));
}
private List<String> cleanup(List<String> values) {
return values.stream().map(this::cleanup).collect(Collectors.toList());
}
private String cleanup(String value) {
return "*".equals(value) ? "*"
: EndpointId.fromPropertyValue(value).toLowerCaseString();
}
private Set<String> asSet(Collection<String> items) {
if (items == null) {
return Collections.emptySet();

View File

@ -147,6 +147,12 @@ public class ExposeExcludePropertyEndpointFilterTests {
assertThat(match(EndpointId.of("buz"))).isFalse();
}
@Test
public void matchWhenMixedCaseShouldMatch() {
setupFilter("foo-bar", "");
assertThat(match(EndpointId.of("fooBar"))).isTrue();
}
private void setupFilter(String include, String exclude) {
MockEnvironment environment = new MockEnvironment();
environment.setProperty("foo.include", include);

View File

@ -87,4 +87,14 @@ public final class EndpointId {
return new EndpointId(value);
}
/**
* Factory method to create a new {@link EndpointId} from a property value. Is more
* lenient that {@link #of(String)} to allow for common "relaxed" property variants.
* @param value the property value to convert
* @return an {@link EndpointId} instance
*/
public static EndpointId fromPropertyValue(String value) {
return new EndpointId(value.replace("-", ""));
}
}

View File

@ -93,4 +93,10 @@ public class EndpointIdTests {
assertThat(EndpointId.of("fooBar").toString()).isEqualTo("fooBar");
}
@Test
public void fromPropertyValueStripsDashes() {
EndpointId fromPropertyValue = EndpointId.fromPropertyValue("foo-bar");
assertThat(fromPropertyValue).isEqualTo(EndpointId.of("fooBar"));
}
}