Skip to content

Commit f98c1e7

Browse files
committed
Disable the java info contributor by default
Closes gh-28310 Co-authored-by Phillip Webb <[email protected]>
1 parent f2b3f1f commit f98c1e7

File tree

8 files changed

+126
-29
lines changed

8 files changed

+126
-29
lines changed

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

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -54,7 +54,7 @@ public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeM
5454
if (outcome != null) {
5555
return outcome;
5656
}
57-
return getDefaultEndpointsOutcome(context);
57+
return getDefaultOutcome(context, annotationAttributes);
5858
}
5959

6060
protected ConditionOutcome getEndpointOutcome(ConditionContext context, String endpointName) {
@@ -68,6 +68,27 @@ protected ConditionOutcome getEndpointOutcome(ConditionContext context, String e
6868
return null;
6969
}
7070

71+
/**
72+
* Return the default outcome that should be used if not property is set. By default
73+
* this method will use the {@code <prefix>.defaults.enabled} property, matching if it
74+
* is {@code true} or if it is not configured.
75+
* @param context the condition context
76+
* @param annotationAttributes the annotation attributes
77+
* @return the default outcome
78+
* @since 2.6.0
79+
*/
80+
protected ConditionOutcome getDefaultOutcome(ConditionContext context, AnnotationAttributes annotationAttributes) {
81+
return getDefaultEndpointsOutcome(context);
82+
}
83+
84+
/**
85+
* Return the default outcome that should be used.
86+
* @param context the condition context
87+
* @return the default outcome
88+
* @deprecated since 2.6.0 for removal in 2.8.0 in favor of
89+
* {@link #getDefaultOutcome(ConditionContext, AnnotationAttributes)}
90+
*/
91+
@Deprecated
7192
protected ConditionOutcome getDefaultEndpointsOutcome(ConditionContext context) {
7293
boolean match = Boolean
7394
.parseBoolean(context.getEnvironment().getProperty(this.prefix + "defaults.enabled", "true"));

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/ConditionalOnEnabledInfoContributor.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,11 +25,9 @@
2525
import org.springframework.context.annotation.Conditional;
2626

2727
/**
28-
* {@link Conditional @Conditional} that checks whether or not a default info contributor
29-
* is enabled. Matches if the value of the {@code management.info.<name>.enabled} property
30-
* is {@code true}. Otherwise, matches if the value of the
31-
* {@code management.info.defaults.enabled} property is {@code true} or if it is not
32-
* configured.
28+
* {@link Conditional @Conditional} that checks whether or not an info contributor is
29+
* enabled. Matches if the value of the {@code management.info.<name>.enabled} property is
30+
* {@code true}. Otherwise, use the specific {@link #fallback() fallback} method.
3331
*
3432
* @author Stephane Nicoll
3533
* @since 2.0.0
@@ -46,4 +44,10 @@
4644
*/
4745
String value();
4846

47+
/**
48+
* Fallback behavior when {@code management.info.<name>.enabled} has not been set.
49+
* @return the fallback behavior
50+
*/
51+
InfoContributorFallback fallback() default InfoContributorFallback.USE_DEFAULTS_PROPERTY;
52+
4953
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public InfoContributor buildInfoContributor(BuildProperties buildProperties) {
8080
}
8181

8282
@Bean
83-
@ConditionalOnEnabledInfoContributor("java")
83+
@ConditionalOnEnabledInfoContributor(value = "java", fallback = InfoContributorFallback.DISABLE)
8484
@Order(DEFAULT_ORDER)
8585
public JavaInfoContributor javaInfoContributor() {
8686
return new JavaInfoContributor();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* Copyright 2012-2021 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+
* https://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.autoconfigure.info;
18+
19+
import org.springframework.boot.actuate.autoconfigure.OnEndpointElementCondition;
20+
21+
/**
22+
* Controls the fallback behavior when the primary property that controls whether an info
23+
* contributor is enabled is not set.
24+
*
25+
* @author Andy Wilkinson
26+
* @since 2.6.0
27+
* @see OnEndpointElementCondition
28+
*/
29+
public enum InfoContributorFallback {
30+
31+
/**
32+
* Fall back to the {@code management.info.defaults.enabled} property, matching if it
33+
* is {@code true} or if it is not configured.
34+
*/
35+
USE_DEFAULTS_PROPERTY,
36+
37+
/**
38+
* Do not fall back, thereby disabling the info contributor.
39+
*/
40+
DISABLE;
41+
42+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/OnEnabledInfoContributorCondition.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,7 +17,11 @@
1717
package org.springframework.boot.actuate.autoconfigure.info;
1818

1919
import org.springframework.boot.actuate.autoconfigure.OnEndpointElementCondition;
20+
import org.springframework.boot.autoconfigure.condition.ConditionMessage;
21+
import org.springframework.boot.autoconfigure.condition.ConditionOutcome;
2022
import org.springframework.context.annotation.Condition;
23+
import org.springframework.context.annotation.ConditionContext;
24+
import org.springframework.core.annotation.AnnotationAttributes;
2125

2226
/**
2327
* {@link Condition} that checks if an info indicator is enabled.
@@ -30,4 +34,14 @@ class OnEnabledInfoContributorCondition extends OnEndpointElementCondition {
3034
super("management.info.", ConditionalOnEnabledInfoContributor.class);
3135
}
3236

37+
@Override
38+
protected ConditionOutcome getDefaultOutcome(ConditionContext context, AnnotationAttributes annotationAttributes) {
39+
InfoContributorFallback fallback = annotationAttributes.getEnum("fallback");
40+
if (fallback == InfoContributorFallback.DISABLE) {
41+
return new ConditionOutcome(false, ConditionMessage.forCondition(ConditionalOnEnabledInfoContributor.class)
42+
.because("management.info." + annotationAttributes.getString("value") + ".enabled is not true"));
43+
}
44+
return super.getDefaultOutcome(context, annotationAttributes);
45+
}
46+
3347
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,12 @@
273273
"name": "management.info.git.mode",
274274
"defaultValue": "simple"
275275
},
276+
{
277+
"name": "management.info.java.enabled",
278+
"type": "java.lang.Boolean",
279+
"description": "Whether to enable Java info.",
280+
"defaultValue": false
281+
},
276282
{
277283
"name": "management.metrics.binders.files.enabled",
278284
"type": "java.lang.Boolean",

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,11 @@ void disableEnvContributor() {
5454
.run((context) -> assertThat(context).doesNotHaveBean(EnvironmentInfoContributor.class));
5555
}
5656

57-
@Test
58-
void disableJavaContributor() {
59-
this.contextRunner.withPropertyValues("management.info.java.enabled=false")
60-
.run((context) -> assertThat(context).doesNotHaveBean(JavaInfoContributor.class));
61-
}
62-
6357
@Test
6458
void defaultInfoContributorsEnabled() {
6559
this.contextRunner.run((context) -> {
66-
assertThat(context).hasSingleBean(EnvironmentInfoContributor.class)
67-
.hasSingleBean(JavaInfoContributor.class);
68-
assertThat(context.getBeansOfType(InfoContributor.class)).hasSize(2);
60+
assertThat(context).hasSingleBean(EnvironmentInfoContributor.class);
61+
assertThat(context.getBeansOfType(InfoContributor.class)).hasSize(1);
6962
});
7063
}
7164

@@ -146,7 +139,7 @@ void customBuildInfoContributor() {
146139

147140
@Test
148141
void javaInfoContributor() {
149-
this.contextRunner.run((context) -> {
142+
this.contextRunner.withPropertyValues("management.info.java.enabled=true").run((context) -> {
150143
assertThat(context).hasSingleBean(JavaInfoContributor.class);
151144
Map<String, Object> content = invokeContributor(context.getBean(JavaInfoContributor.class));
152145
assertThat(content).containsKey("java");

spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,24 +1145,41 @@ Spring Boot includes a number of auto-configured `InfoContributor` beans, and yo
11451145
==== Auto-configured InfoContributors
11461146
When appropriate, Spring auto-configures the following `InfoContributor` beans:
11471147

1148-
[cols="1,4"]
1148+
[cols="1,4,8,4"]
11491149
|===
1150-
| Name | Description
1150+
| ID | Name | Description | Prequisites
1151+
1152+
| `build`
1153+
| {spring-boot-actuator-module-code}/info/BuildInfoContributor.java[`BuildInfoContributor`]
1154+
| Exposes build information.
1155+
| A `META-INF/build-info.properties` resource.
11511156

1157+
| `env`
11521158
| {spring-boot-actuator-module-code}/info/EnvironmentInfoContributor.java[`EnvironmentInfoContributor`]
1153-
| Exposes any key from the `Environment` under the `info` key.
1159+
| Exposes any property from the `Environment` whose name starts with `info.`.
1160+
| None.
11541161

1162+
| `git`
11551163
| {spring-boot-actuator-module-code}/info/GitInfoContributor.java[`GitInfoContributor`]
1156-
| Exposes git information if a `git.properties` file is available.
1157-
1158-
| {spring-boot-actuator-module-code}/info/BuildInfoContributor.java[`BuildInfoContributor`]
1159-
| Exposes build information if a `META-INF/build-info.properties` file is available.
1164+
| Exposes git information.
1165+
| A `git.properties` resource.
11601166

1167+
| `java`
11611168
| {spring-boot-actuator-module-code}/info/JavaInfoContributor.java[`JavaInfoContributor`]
1162-
| Exposes Java runtime information under the `java` key.
1169+
| Exposes Java runtime information.
1170+
| None.
1171+
11631172
|===
11641173

1165-
TIP: You can disable them all by setting the configprop:management.info.defaults.enabled[] property.
1174+
Whether or not an individual contributor is enabled is controlled by its `management.info.<id>.enabled` property.
1175+
Different contributors have different defaults for this property, depending on their prerequisites and the nature of the information that they expose.
1176+
1177+
With no prequisites to indicate that it should be enabled, the `java` contributor is disabled by default.
1178+
You can enable it by setting the configprop:management.info.java.enabled[] property to `true`.
1179+
1180+
The `env`, `git`, and `build` info contributors are enabled by default.
1181+
Each can be disabled by setting its `management.info.<id>.enabled` property to `false`.
1182+
Alternatively, to disable every contributor that is usually enabled by default, set the configprop:management.info.defaults.enabled[] property to `false`.
11661183

11671184

11681185

0 commit comments

Comments
 (0)