Commit 3f1f830e authored by Andy Wilkinson's avatar Andy Wilkinson

Fail if management.server.address is set but actuator is on the same port

Closes gh-22187
parent a0afb739
/*
* 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'.
......
/*
* 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 <T extends CharSequence> Consumer<T> numberOfOccurrences(String substring, int expectedCount) {
return (charSequence) -> {
int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment