From f98c1e7231fc36ac4b9c843f2bc2629f4d531f6a Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 19 Oct 2021 15:07:14 +0100 Subject: [PATCH] Disable the java info contributor by default Closes gh-28310 Co-authored-by Phillip Webb --- .../OnEndpointElementCondition.java | 25 ++++++++++- .../ConditionalOnEnabledInfoContributor.java | 16 ++++--- .../InfoContributorAutoConfiguration.java | 2 +- .../info/InfoContributorFallback.java | 42 +++++++++++++++++++ .../OnEnabledInfoContributorCondition.java | 16 ++++++- ...itional-spring-configuration-metadata.json | 6 +++ ...InfoContributorAutoConfigurationTests.java | 13 ++---- .../src/docs/asciidoc/actuator/endpoints.adoc | 39 ++++++++++++----- 8 files changed, 128 insertions(+), 31 deletions(-) create mode 100644 spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorFallback.java diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/OnEndpointElementCondition.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/OnEndpointElementCondition.java index e1634475bbc..ad9d1edc1df 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/OnEndpointElementCondition.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/OnEndpointElementCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -54,7 +54,7 @@ public abstract class OnEndpointElementCondition extends SpringBootCondition { if (outcome != null) { return outcome; } - return getDefaultEndpointsOutcome(context); + return getDefaultOutcome(context, annotationAttributes); } protected ConditionOutcome getEndpointOutcome(ConditionContext context, String endpointName) { @@ -68,6 +68,27 @@ public abstract class OnEndpointElementCondition extends SpringBootCondition { return null; } + /** + * Return the default outcome that should be used if not property is set. By default + * this method will use the {@code .defaults.enabled} property, matching if it + * is {@code true} or if it is not configured. + * @param context the condition context + * @param annotationAttributes the annotation attributes + * @return the default outcome + * @since 2.6.0 + */ + protected ConditionOutcome getDefaultOutcome(ConditionContext context, AnnotationAttributes annotationAttributes) { + return getDefaultEndpointsOutcome(context); + } + + /** + * Return the default outcome that should be used. + * @param context the condition context + * @return the default outcome + * @deprecated since 2.6.0 for removal in 2.8.0 in favor of + * {@link #getDefaultOutcome(ConditionContext, AnnotationAttributes)} + */ + @Deprecated protected ConditionOutcome getDefaultEndpointsOutcome(ConditionContext context) { boolean match = Boolean .parseBoolean(context.getEnvironment().getProperty(this.prefix + "defaults.enabled", "true")); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/ConditionalOnEnabledInfoContributor.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/ConditionalOnEnabledInfoContributor.java index 3d43e3a47a4..bc0ddc8a79c 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/ConditionalOnEnabledInfoContributor.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/ConditionalOnEnabledInfoContributor.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -25,11 +25,9 @@ import java.lang.annotation.Target; import org.springframework.context.annotation.Conditional; /** - * {@link Conditional @Conditional} that checks whether or not a default info contributor - * is enabled. Matches if the value of the {@code management.info..enabled} property - * is {@code true}. Otherwise, matches if the value of the - * {@code management.info.defaults.enabled} property is {@code true} or if it is not - * configured. + * {@link Conditional @Conditional} that checks whether or not an info contributor is + * enabled. Matches if the value of the {@code management.info..enabled} property is + * {@code true}. Otherwise, use the specific {@link #fallback() fallback} method. * * @author Stephane Nicoll * @since 2.0.0 @@ -46,4 +44,10 @@ public @interface ConditionalOnEnabledInfoContributor { */ String value(); + /** + * Fallback behavior when {@code management.info..enabled} has not been set. + * @return the fallback behavior + */ + InfoContributorFallback fallback() default InfoContributorFallback.USE_DEFAULTS_PROPERTY; + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java index cde1f0cf847..1b3ff48d43e 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfiguration.java @@ -80,7 +80,7 @@ public class InfoContributorAutoConfiguration { } @Bean - @ConditionalOnEnabledInfoContributor("java") + @ConditionalOnEnabledInfoContributor(value = "java", fallback = InfoContributorFallback.DISABLE) @Order(DEFAULT_ORDER) public JavaInfoContributor javaInfoContributor() { return new JavaInfoContributor(); diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorFallback.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorFallback.java new file mode 100644 index 00000000000..0f78db5cbe4 --- /dev/null +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorFallback.java @@ -0,0 +1,42 @@ +/* + * Copyright 2012-2021 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.boot.actuate.autoconfigure.info; + +import org.springframework.boot.actuate.autoconfigure.OnEndpointElementCondition; + +/** + * Controls the fallback behavior when the primary property that controls whether an info + * contributor is enabled is not set. + * + * @author Andy Wilkinson + * @since 2.6.0 + * @see OnEndpointElementCondition + */ +public enum InfoContributorFallback { + + /** + * Fall back to the {@code management.info.defaults.enabled} property, matching if it + * is {@code true} or if it is not configured. + */ + USE_DEFAULTS_PROPERTY, + + /** + * Do not fall back, thereby disabling the info contributor. + */ + DISABLE; + +} diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/OnEnabledInfoContributorCondition.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/OnEnabledInfoContributorCondition.java index 38928c71ecb..a36a2f9d656 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/OnEnabledInfoContributorCondition.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/info/OnEnabledInfoContributorCondition.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,7 +17,11 @@ package org.springframework.boot.actuate.autoconfigure.info; import org.springframework.boot.actuate.autoconfigure.OnEndpointElementCondition; +import org.springframework.boot.autoconfigure.condition.ConditionMessage; +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; import org.springframework.context.annotation.Condition; +import org.springframework.context.annotation.ConditionContext; +import org.springframework.core.annotation.AnnotationAttributes; /** * {@link Condition} that checks if an info indicator is enabled. @@ -30,4 +34,14 @@ class OnEnabledInfoContributorCondition extends OnEndpointElementCondition { super("management.info.", ConditionalOnEnabledInfoContributor.class); } + @Override + protected ConditionOutcome getDefaultOutcome(ConditionContext context, AnnotationAttributes annotationAttributes) { + InfoContributorFallback fallback = annotationAttributes.getEnum("fallback"); + if (fallback == InfoContributorFallback.DISABLE) { + return new ConditionOutcome(false, ConditionMessage.forCondition(ConditionalOnEnabledInfoContributor.class) + .because("management.info." + annotationAttributes.getString("value") + ".enabled is not true")); + } + return super.getDefaultOutcome(context, annotationAttributes); + } + } diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json index c101f7c12c8..05e054d821a 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/resources/META-INF/additional-spring-configuration-metadata.json @@ -273,6 +273,12 @@ "name": "management.info.git.mode", "defaultValue": "simple" }, + { + "name": "management.info.java.enabled", + "type": "java.lang.Boolean", + "description": "Whether to enable Java info.", + "defaultValue": false + }, { "name": "management.metrics.binders.files.enabled", "type": "java.lang.Boolean", diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java index e4001ed7fba..6c9eaf92abb 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/info/InfoContributorAutoConfigurationTests.java @@ -54,18 +54,11 @@ class InfoContributorAutoConfigurationTests { .run((context) -> assertThat(context).doesNotHaveBean(EnvironmentInfoContributor.class)); } - @Test - void disableJavaContributor() { - this.contextRunner.withPropertyValues("management.info.java.enabled=false") - .run((context) -> assertThat(context).doesNotHaveBean(JavaInfoContributor.class)); - } - @Test void defaultInfoContributorsEnabled() { this.contextRunner.run((context) -> { - assertThat(context).hasSingleBean(EnvironmentInfoContributor.class) - .hasSingleBean(JavaInfoContributor.class); - assertThat(context.getBeansOfType(InfoContributor.class)).hasSize(2); + assertThat(context).hasSingleBean(EnvironmentInfoContributor.class); + assertThat(context.getBeansOfType(InfoContributor.class)).hasSize(1); }); } @@ -146,7 +139,7 @@ class InfoContributorAutoConfigurationTests { @Test void javaInfoContributor() { - this.contextRunner.run((context) -> { + this.contextRunner.withPropertyValues("management.info.java.enabled=true").run((context) -> { assertThat(context).hasSingleBean(JavaInfoContributor.class); Map content = invokeContributor(context.getBean(JavaInfoContributor.class)); assertThat(content).containsKey("java"); diff --git a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc index b1935605067..f0408129cf9 100644 --- a/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc +++ b/spring-boot-project/spring-boot-docs/src/docs/asciidoc/actuator/endpoints.adoc @@ -1145,24 +1145,41 @@ Spring Boot includes a number of auto-configured `InfoContributor` beans, and yo ==== Auto-configured InfoContributors When appropriate, Spring auto-configures the following `InfoContributor` beans: -[cols="1,4"] +[cols="1,4,8,4"] |=== -| Name | Description - -| {spring-boot-actuator-module-code}/info/EnvironmentInfoContributor.java[`EnvironmentInfoContributor`] -| Exposes any key from the `Environment` under the `info` key. - -| {spring-boot-actuator-module-code}/info/GitInfoContributor.java[`GitInfoContributor`] -| Exposes git information if a `git.properties` file is available. +| ID | Name | Description | Prequisites +| `build` | {spring-boot-actuator-module-code}/info/BuildInfoContributor.java[`BuildInfoContributor`] -| Exposes build information if a `META-INF/build-info.properties` file is available. +| Exposes build information. +| A `META-INF/build-info.properties` resource. +| `env` +| {spring-boot-actuator-module-code}/info/EnvironmentInfoContributor.java[`EnvironmentInfoContributor`] +| Exposes any property from the `Environment` whose name starts with `info.`. +| None. + +| `git` +| {spring-boot-actuator-module-code}/info/GitInfoContributor.java[`GitInfoContributor`] +| Exposes git information. +| A `git.properties` resource. + +| `java` | {spring-boot-actuator-module-code}/info/JavaInfoContributor.java[`JavaInfoContributor`] -| Exposes Java runtime information under the `java` key. +| Exposes Java runtime information. +| None. + |=== -TIP: You can disable them all by setting the configprop:management.info.defaults.enabled[] property. +Whether or not an individual contributor is enabled is controlled by its `management.info..enabled` property. +Different contributors have different defaults for this property, depending on their prerequisites and the nature of the information that they expose. + +With no prequisites to indicate that it should be enabled, the `java` contributor is disabled by default. +You can enable it by setting the configprop:management.info.java.enabled[] property to `true`. + +The `env`, `git`, and `build` info contributors are enabled by default. +Each can be disabled by setting its `management.info..enabled` property to `false`. +Alternatively, to disable every contributor that is usually enabled by default, set the configprop:management.info.defaults.enabled[] property to `false`.