Unverified Commit ba87fef3 by Ryan Baxter Committed by GitHub

Set secure health check url. Fixes #2673 (#2710)

parent a9aca1b8
......@@ -142,6 +142,7 @@ public class EurekaClientAutoConfiguration {
instance.setNonSecurePort(serverPort);
instance.setInstanceId(getDefaultInstanceId(environmentPropertyResolver));
instance.setPreferIpAddress(preferIpAddress);
instance.setSecurePortEnabled(isSecurePortEnabled);
if (StringUtils.hasText(ipAddress)) {
instance.setIpAddress(ipAddress);
}
......@@ -169,6 +170,9 @@ public class EurekaClientAutoConfiguration {
if(metadata != null) {
instance.setStatusPageUrl(metadata.getStatusPageUrl());
instance.setHealthCheckUrl(metadata.getHealthCheckUrl());
if(instance.isSecurePortEnabled()) {
instance.setSecureHealthCheckUrl(metadata.getSecureHealthCheckUrl());
}
Map<String, String> metadataMap = instance.getMetadataMap();
if (metadataMap.get("management.port") == null) {
metadataMap.put("management.port", String.valueOf(metadata.getManagementPort()));
......
......@@ -24,11 +24,16 @@ public class DefaultManagementMetadataProvider implements ManagementMetadataProv
return null;
}
String healthCheckUrl = getHealthCheckUrl(instance, serverPort, serverContextPath,
managementContextPath, managementPort);
managementContextPath, managementPort, false);
String statusPageUrl = getStatusPageUrl(instance, serverPort, serverContextPath,
managementContextPath, managementPort);
return new ManagementMetadata(healthCheckUrl, statusPageUrl, managementPort == null ? serverPort : managementPort);
ManagementMetadata metadata = new ManagementMetadata(healthCheckUrl, statusPageUrl, managementPort == null ? serverPort : managementPort);
if(instance.isSecurePortEnabled()) {
metadata.setSecureHealthCheckUrl(getHealthCheckUrl(instance, serverPort, serverContextPath,
managementContextPath, managementPort, true));
}
return metadata;
}
private boolean isRandom(Integer port) {
......@@ -36,10 +41,10 @@ public class DefaultManagementMetadataProvider implements ManagementMetadataProv
}
private String getHealthCheckUrl(EurekaInstanceConfigBean instance, int serverPort, String serverContextPath,
String managementContextPath, Integer managementPort) {
String managementContextPath, Integer managementPort, boolean isSecure) {
String healthCheckUrlPath = instance.getHealthCheckUrlPath();
String healthCheckUrl = getUrl(instance, serverPort, serverContextPath, managementContextPath,
managementPort, healthCheckUrlPath);
managementPort, healthCheckUrlPath, isSecure);
log.debug("Constructed eureka meta-data healthcheckUrl: " + healthCheckUrl);
return healthCheckUrl;
}
......@@ -48,19 +53,19 @@ public class DefaultManagementMetadataProvider implements ManagementMetadataProv
String managementContextPath, Integer managementPort) {
String statusPageUrlPath = instance.getStatusPageUrlPath();
String statusPageUrl = getUrl(instance, serverPort, serverContextPath, managementContextPath,
managementPort, statusPageUrlPath);
managementPort, statusPageUrlPath, false);
log.debug("Constructed eureka meta-data statusPageUrl: " + statusPageUrl);
return statusPageUrl;
}
private String getUrl(EurekaInstanceConfigBean instance, int serverPort,
String serverContextPath, String managementContextPath,
Integer managementPort, String urlPath) {
Integer managementPort, String urlPath, boolean isSecure) {
managementContextPath = refineManagementContextPath(serverContextPath, managementContextPath, managementPort);
if (managementPort == null) {
managementPort = serverPort;
}
String scheme = instance.getSecurePortEnabled() ? "https" : "http";
String scheme = isSecure ? "https" : "http";
return constructValidUrl(scheme, instance.getHostname(), managementPort, managementContextPath, urlPath);
}
......
......@@ -7,11 +7,13 @@ public class ManagementMetadata {
private final String healthCheckUrl;
private final String statusPageUrl;
private final Integer managementPort;
private String secureHealthCheckUrl;
public ManagementMetadata(String healthCheckUrl, String statusPageUrl, Integer managementPort) {
this.healthCheckUrl = healthCheckUrl;
this.statusPageUrl = statusPageUrl;
this.managementPort = managementPort;
this.secureHealthCheckUrl = null;
}
public String getHealthCheckUrl() {
......@@ -26,6 +28,14 @@ public class ManagementMetadata {
return managementPort;
}
public String getSecureHealthCheckUrl() {
return secureHealthCheckUrl;
}
public void setSecureHealthCheckUrl(String secureHealthCheckUrl) {
this.secureHealthCheckUrl = secureHealthCheckUrl;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
......
......@@ -5,6 +5,7 @@ import org.junit.Test;
import org.springframework.cloud.netflix.eureka.EurekaInstanceConfigBean;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
......@@ -18,6 +19,7 @@ public class DefaultManagementMetadataProviderTest {
when(INSTANCE.getHostname()).thenReturn("host");
when(INSTANCE.getHealthCheckUrlPath()).thenReturn("health");
when(INSTANCE.getStatusPageUrlPath()).thenReturn("info");
when(INSTANCE.isSecurePortEnabled()).thenReturn(false);
}
@Test
......@@ -51,6 +53,7 @@ public class DefaultManagementMetadataProviderTest {
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:7777/health");
assertThat(actual.getSecureHealthCheckUrl()).isNullOrEmpty();
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:7777/info");
assertThat(actual.getManagementPort()).isEqualTo(7777);
}
......@@ -64,6 +67,7 @@ public class DefaultManagementMetadataProviderTest {
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:8888/health");
assertThat(actual.getSecureHealthCheckUrl()).isNullOrEmpty();
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:8888/info");
assertThat(actual.getManagementPort()).isEqualTo(8888);
}
......@@ -77,6 +81,7 @@ public class DefaultManagementMetadataProviderTest {
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:8888/health");
assertThat(actual.getSecureHealthCheckUrl()).isNullOrEmpty();
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:8888/info");
assertThat(actual.getManagementPort()).isEqualTo(8888);
}
......@@ -90,6 +95,7 @@ public class DefaultManagementMetadataProviderTest {
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:8888/Management/health");
assertThat(actual.getSecureHealthCheckUrl()).isNullOrEmpty();
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:8888/Management/info");
assertThat(actual.getManagementPort()).isEqualTo(8888);
}
......@@ -103,6 +109,7 @@ public class DefaultManagementMetadataProviderTest {
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:7777/Management/health");
assertThat(actual.getSecureHealthCheckUrl()).isNullOrEmpty();
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:7777/Management/info");
assertThat(actual.getManagementPort()).isEqualTo(7777);
}
......@@ -116,6 +123,7 @@ public class DefaultManagementMetadataProviderTest {
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:7777/Server/health");
assertThat(actual.getSecureHealthCheckUrl()).isNullOrEmpty();
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:7777/Server/info");
assertThat(actual.getManagementPort()).isEqualTo(7777);
}
......@@ -129,6 +137,23 @@ public class DefaultManagementMetadataProviderTest {
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:8888/Management/health");
assertThat(actual.getSecureHealthCheckUrl()).isNullOrEmpty();
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:8888/Management/info");
assertThat(actual.getManagementPort()).isEqualTo(8888);
}
@Test
public void setSecureHealthCheckUrl() throws Exception {
int serverPort = 7777;
String serverContextPath = "/";
String managementContextPath = "/Management";
Integer managementPort = 8888;
doReturn(true).when(INSTANCE).isSecurePortEnabled();
ManagementMetadata actual = provider.get(INSTANCE, serverPort, serverContextPath, managementContextPath, managementPort);
assertThat(actual.getHealthCheckUrl()).isEqualTo("http://host:8888/Management/health");
assertThat(actual.getSecureHealthCheckUrl()).isEqualTo("https://host:8888/Management/health");
assertThat(actual.getStatusPageUrl()).isEqualTo("http://host:8888/Management/info");
assertThat(actual.getManagementPort()).isEqualTo(8888);
......
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