From 3f1f830efee820d04460ce6a5ca88d3f130f3b5d Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Mon, 19 Oct 2020 15:06:36 +0100 Subject: [PATCH] Fail if management.server.address is set but actuator is on the same port Closes gh-22187 --- .../ManagementContextAutoConfiguration.java | 9 ++++++++- .../ManagementContextAutoConfigurationTests.java | 16 +++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java index e563fc9367d..300c8a120d6 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfiguration.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -73,6 +73,7 @@ public class ManagementContextAutoConfiguration { @Override public void afterSingletonsInstantiated() { verifySslConfiguration(); + verifyAddressConfiguration(); if (this.environment instanceof ConfigurableEnvironment) { addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.environment); } @@ -84,6 +85,12 @@ public class ManagementContextAutoConfiguration { + "server is not listening on a separate port"); } + private void verifyAddressConfiguration() { + Object address = this.environment.getProperty("management.server.address"); + Assert.state(address == null, "Management-specific server address cannot be configured as the management " + + "server is not listening on a separate port"); + } + /** * Add an alias for 'local.management.port' that actually resolves using * 'local.server.port'. diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java index 681216d1ca4..29298c1956f 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/web/server/ManagementContextAutoConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 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. @@ -24,6 +24,7 @@ import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfi import org.springframework.boot.actuate.autoconfigure.endpoint.web.WebEndpointAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration; import org.springframework.boot.autoconfigure.AutoConfigurations; +import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration; import org.springframework.boot.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration; import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.system.CapturedOutput; @@ -54,6 +55,19 @@ class ManagementContextAutoConfigurationTests { .run((context) -> assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2))); } + @Test + void givenSamePortManagementServerWhenManagementServerAddressIsConfiguredThenContextRefreshFails() { + WebApplicationContextRunner contextRunner = new WebApplicationContextRunner( + AnnotationConfigServletWebServerApplicationContext::new) + .withConfiguration(AutoConfigurations.of(ManagementContextAutoConfiguration.class, + ServletWebServerFactoryAutoConfiguration.class, + ServletManagementContextAutoConfiguration.class, WebEndpointAutoConfiguration.class, + EndpointAutoConfiguration.class, DispatcherServletAutoConfiguration.class)); + contextRunner.withPropertyValues("server.port=0", "management.server.address=127.0.0.1") + .run((context) -> assertThat(context).getFailure() + .hasMessageStartingWith("Management-specific server address cannot be configured")); + } + private Consumer numberOfOccurrences(String substring, int expectedCount) { return (charSequence) -> { int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring);