Skip to content

Commit cf95ba0

Browse files
committed
add a reproducer test and fix the issue by an alias accessor pair: getKeys() and setKeys(List<String>).
1 parent c97bfac commit cf95ba0

File tree

2 files changed

+83
-19
lines changed

2 files changed

+83
-19
lines changed

management/src/main/java/io/micronaut/management/endpoint/env/EnvironmentEndpoint.java

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -78,25 +78,43 @@ public EnvironmentEndpoint(Environment environment,
7878
}
7979

8080
/**
81-
* Gets the keys to be displayed by the environment endpoint.
82-
* Defaults to ["activeEnvironments", "packages", "propertySources"] if not configured.
83-
* Configurable via {@code endpoints.env.active-keys}.
84-
*
85-
* @return The list of active sections.
86-
*/
87-
public List<String> getActiveKeys() {
88-
return activeKeys;
89-
}
90-
91-
/**
92-
* Sets the sections to be displayed by the environment endpoint.
93-
* Example: {@code endpoints.env.active-keys=activeEnvironments,packages}
94-
*
95-
* @param activeKeys The list of sections. If an empty list is provided, no sections will be displayed.
96-
*/
97-
public void setActiveKeys(List<String> activeKeys) {
98-
this.activeKeys = activeKeys;
99-
}
81+
* Gets the keys to be displayed by the environment endpoint.
82+
* Defaults to ["activeEnvironments", "packages", "propertySources"] if not configured.
83+
* Configurable via {@code endpoints.env.active-keys} or {@code endpoints.env.keys}.
84+
*
85+
* @return The list of active sections.
86+
*/
87+
public List<String> getActiveKeys() {
88+
return activeKeys;
89+
}
90+
91+
/**
92+
* Alias accessor for configuration binding from {@code endpoints.env.keys}.
93+
*
94+
* @return The list of active sections.
95+
*/
96+
public List<String> getKeys() {
97+
return getActiveKeys();
98+
}
99+
100+
/**
101+
* Sets the sections to be displayed by the environment endpoint.
102+
* Example: {@code endpoints.env.active-keys=activeEnvironments,packages}
103+
*
104+
* @param activeKeys The list of sections. If an empty list is provided, no sections will be displayed.
105+
*/
106+
public void setActiveKeys(List<String> activeKeys) {
107+
this.activeKeys = activeKeys;
108+
}
109+
110+
/**
111+
* Alias mutator for configuration binding from {@code endpoints.env.keys}.
112+
*
113+
* @param keys The list of sections. If an empty list is provided, no sections will be displayed.
114+
*/
115+
public void setKeys(List<String> keys) {
116+
setActiveKeys(keys);
117+
}
100118

101119
/**
102120
* @return The environment information as a map with the following keys: activeEnvironments, packages and
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.micronaut.management.env
2+
3+
import com.fasterxml.jackson.databind.ObjectMapper
4+
import io.micronaut.context.ApplicationContext
5+
import io.micronaut.runtime.server.EmbeddedServer
6+
import io.micronaut.http.client.HttpClient
7+
import org.junit.jupiter.api.Assertions.assertEquals
8+
import org.junit.jupiter.api.Assertions.assertFalse
9+
import org.junit.jupiter.api.Assertions.assertTrue
10+
import org.junit.jupiter.api.Test
11+
12+
class EnvironmentEndpointKeysFilterTest {
13+
14+
@Test
15+
fun `env endpoint respects endpoints_env_keys filter`() {
16+
val props = mapOf(
17+
// isolate config for this spec
18+
"spec.name" to "EnvironmentEndpointKeysFilterTest",
19+
// ensure server starts on random port
20+
"micronaut.server.port" to -1,
21+
// enable env endpoint and make it non-sensitive so it's accessible in test
22+
"endpoints.all.enabled" to true,
23+
"endpoints.all.sensitive" to false,
24+
"endpoints.env.enabled" to true,
25+
"endpoints.env.sensitive" to false,
26+
// proposed configuration: expose only activeEnvironments
27+
"endpoints.env.keys[0]" to "activeEnvironments"
28+
)
29+
30+
val server = ApplicationContext.run(EmbeddedServer::class.java, props)
31+
val client = HttpClient.create(server.url)
32+
try {
33+
val json = client.toBlocking().retrieve("/env")
34+
val mapper = server.applicationContext.getBean(ObjectMapper::class.java)
35+
val node = mapper.readTree(json)
36+
37+
assertTrue(node.has("activeEnvironments"), "Should contain 'activeEnvironments'")
38+
assertFalse(node.has("packages"), "Should not contain 'packages' when filtered")
39+
assertFalse(node.has("propertySources"), "Should not contain 'propertySources' when filtered")
40+
assertEquals(1, node.size(), "Only 'activeEnvironments' should be present when filtered via endpoints.env.keys")
41+
} finally {
42+
client.close()
43+
server.close()
44+
}
45+
}
46+
}

0 commit comments

Comments
 (0)