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

Closes gh-22187
pull/23755/head
Andy Wilkinson 4 years ago
parent a0afb73923
commit 3f1f830efe

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -73,6 +73,7 @@ public class ManagementContextAutoConfiguration {
@Override @Override
public void afterSingletonsInstantiated() { public void afterSingletonsInstantiated() {
verifySslConfiguration(); verifySslConfiguration();
verifyAddressConfiguration();
if (this.environment instanceof ConfigurableEnvironment) { if (this.environment instanceof ConfigurableEnvironment) {
addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.environment); addLocalManagementPortPropertyAlias((ConfigurableEnvironment) this.environment);
} }
@ -84,6 +85,12 @@ public class ManagementContextAutoConfiguration {
+ "server is not listening on a separate port"); + "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 * Add an alias for 'local.management.port' that actually resolves using
* 'local.server.port'. * 'local.server.port'.

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.endpoint.web.WebEndpointAutoConfiguration;
import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration; import org.springframework.boot.actuate.autoconfigure.web.servlet.ServletManagementContextAutoConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigurations; 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.autoconfigure.web.servlet.ServletWebServerFactoryAutoConfiguration;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner; import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.boot.test.system.CapturedOutput; import org.springframework.boot.test.system.CapturedOutput;
@ -54,6 +55,19 @@ class ManagementContextAutoConfigurationTests {
.run((context) -> assertThat(output).satisfies(numberOfOccurrences("Tomcat started on port", 2))); .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) { private <T extends CharSequence> Consumer<T> numberOfOccurrences(String substring, int expectedCount) {
return (charSequence) -> { return (charSequence) -> {
int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring); int count = StringUtils.countOccurrencesOf(charSequence.toString(), substring);

Loading…
Cancel
Save