Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import java.util.Map;
import java.util.function.Predicate;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Stream;

import com.fasterxml.jackson.annotation.JsonInclude;

import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
import org.springframework.boot.actuate.endpoint.OperationResponseBody;
import org.springframework.boot.actuate.endpoint.SanitizableData;
import org.springframework.boot.actuate.endpoint.Sanitizer;
Expand Down Expand Up @@ -87,11 +89,21 @@ public EnvironmentDescriptor environment(@Nullable String pattern) {

EnvironmentDescriptor getEnvironmentDescriptor(String pattern, boolean showUnsanitized) {
if (StringUtils.hasText(pattern)) {
return getEnvironmentDescriptor(Pattern.compile(pattern).asPredicate(), showUnsanitized);
return getEnvironmentDescriptor(parsePattern(pattern).asPredicate(), showUnsanitized);
}
return getEnvironmentDescriptor((name) -> true, showUnsanitized);
}

private Pattern parsePattern(String pattern) {
try {
return Pattern.compile(pattern);
}
catch (PatternSyntaxException ex) {
throw new InvalidEndpointRequestException("Failed to parse regular expression: " + pattern,
"Invalid regular expression", ex);
}
}

private EnvironmentDescriptor getEnvironmentDescriptor(Predicate<String> propertyNamePredicate,
boolean showUnsanitized) {
List<PropertySourceDescriptor> propertySources = new ArrayList<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.regex.PatternSyntaxException;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import org.springframework.boot.actuate.endpoint.InvalidEndpointRequestException;
import org.springframework.boot.actuate.endpoint.Show;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentDescriptor;
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentEntryDescriptor;
Expand All @@ -48,6 +50,7 @@
import org.springframework.mock.env.MockPropertySource;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

/**
* Tests for {@link EnvironmentEndpoint}.
Expand Down Expand Up @@ -116,6 +119,15 @@ void responseWhenShowWhenAuthorized() {
});
}

@Test
void responseWhenPatternIsInvalidThrowsInvalidEndpointRequestException() {
ConfigurableEnvironment environment = emptyEnvironment();
EnvironmentEndpoint endpoint = new EnvironmentEndpoint(environment, Collections.emptyList(), Show.ALWAYS);
assertThatExceptionOfType(InvalidEndpointRequestException.class).isThrownBy(() -> endpoint.environment("["))
.withMessageContaining("Failed to parse regular expression: [")
.withCauseInstanceOf(PatternSyntaxException.class);
}

@Test
void compositeSourceIsHandledCorrectly() {
ConfigurableEnvironment environment = emptyEnvironment();
Expand Down
Loading