Disable the java info contributor by default

Closes gh-28310

Co-authored-by Phillip Webb <pwebb@vmware.com>
This commit is contained in:
Andy Wilkinson 2021-10-19 15:07:14 +01:00
parent f2b3f1f41f
commit f98c1e7231
8 changed files with 128 additions and 31 deletions

View File

@ -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 <prefix>.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"));

View File

@ -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.<name>.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.<name>.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.<name>.enabled} has not been set.
* @return the fallback behavior
*/
InfoContributorFallback fallback() default InfoContributorFallback.USE_DEFAULTS_PROPERTY;
}

View File

@ -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();

View File

@ -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;
}

View File

@ -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);
}
}

View File

@ -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",

View File

@ -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<String, Object> content = invokeContributor(context.getBean(JavaInfoContributor.class));
assertThat(content).containsKey("java");

View File

@ -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.<id>.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.<id>.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`.