Skip to content

Commit 3894229

Browse files
committed
Fix status handling of /application/env/{propertyName}
This commit makes sure to return a 404 status if the env endpoint is invoked with a property that does not exist in the environment. Closes gh-10179
1 parent c2c6f49 commit 3894229

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/env/EnvironmentEndpointAutoConfiguration.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnEnabledEndpoint;
2020
import org.springframework.boot.actuate.env.EnvironmentEndpoint;
21+
import org.springframework.boot.actuate.env.EnvironmentWebEndpointExtension;
2122
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
23+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
2325
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2426
import org.springframework.context.annotation.Bean;
@@ -55,4 +57,13 @@ public EnvironmentEndpoint environmentEndpoint(Environment environment) {
5557
return endpoint;
5658
}
5759

60+
@Bean
61+
@ConditionalOnMissingBean
62+
@ConditionalOnEnabledEndpoint
63+
@ConditionalOnBean(EnvironmentEndpoint.class)
64+
public EnvironmentWebEndpointExtension environmentWebEndpointExtension(
65+
EnvironmentEndpoint environmentEndpoint) {
66+
return new EnvironmentWebEndpointExtension(environmentEndpoint);
67+
}
68+
5869
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2012-2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.actuate.env;
18+
19+
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
20+
import org.springframework.boot.actuate.endpoint.annotation.Selector;
21+
import org.springframework.boot.actuate.endpoint.web.WebEndpointResponse;
22+
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpointExtension;
23+
import org.springframework.boot.actuate.env.EnvironmentEndpoint.EnvironmentEntryDescriptor;
24+
25+
/**
26+
* {@link WebEndpointExtension} for the {@link EnvironmentEndpoint}.
27+
*
28+
* @author Stephane Nicoll
29+
* @since 2.0.0
30+
*/
31+
@WebEndpointExtension(endpoint = EnvironmentEndpoint.class)
32+
public class EnvironmentWebEndpointExtension {
33+
34+
private final EnvironmentEndpoint delegate;
35+
36+
public EnvironmentWebEndpointExtension(EnvironmentEndpoint delegate) {
37+
this.delegate = delegate;
38+
}
39+
40+
@ReadOperation
41+
public WebEndpointResponse<EnvironmentEntryDescriptor> environmentEntry(
42+
@Selector String toMatch) {
43+
EnvironmentEntryDescriptor descriptor = this.delegate.environmentEntry(toMatch);
44+
int status = descriptor.getProperty() != null ? WebEndpointResponse.STATUS_OK
45+
: WebEndpointResponse.STATUS_NOT_FOUND;
46+
return new WebEndpointResponse<>(descriptor, status);
47+
}
48+
49+
}

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/env/EnvironmentEndpointWebIntegrationTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,17 @@ public void nestedPathWithSensitivePlaceholderShouldSanitize() throws Exception
9696
.jsonPath(forPropertyEntry("placeholder")).isEqualTo("******");
9797
}
9898

99+
@Test
100+
public void nestedPathForUnknownKeyShouldReturn404AndBody() throws Exception {
101+
client.get().uri("/application/env/this.does.not.exist").exchange().expectStatus()
102+
.isNotFound()
103+
.expectBody()
104+
.jsonPath("property").doesNotExist()
105+
.jsonPath("propertySources[?(@.name=='test')]").exists()
106+
.jsonPath("propertySources[?(@.name=='systemProperties')]").exists()
107+
.jsonPath("propertySources[?(@.name=='systemEnvironment')]").exists();
108+
}
109+
99110
@Test
100111
public void nestedPathMatchedByRegexWhenPlaceholderCannotBeResolvedShouldReturnUnresolvedProperty()
101112
throws Exception {
@@ -140,6 +151,12 @@ public EnvironmentEndpoint endpoint(Environment environment) {
140151
return new EnvironmentEndpoint(environment);
141152
}
142153

154+
@Bean
155+
public EnvironmentWebEndpointExtension webEndpointExtension(
156+
EnvironmentEndpoint endpoint) {
157+
return new EnvironmentWebEndpointExtension(endpoint);
158+
}
159+
143160
}
144161

145162
}

0 commit comments

Comments
 (0)