From b57dedb74af145b01a74c2d06ea64f38fe93ba96 Mon Sep 17 00:00:00 2001 From: Raiyan Date: Wed, 27 Sep 2017 23:08:35 +0530 Subject: [PATCH] - Eureka client auto configures the secure port when secure port is enabled - The eureka instance auto registration also changes the secure port when the server port is random (0) Fixes gh-2303 --- .../eureka/CloudEurekaInstanceConfig.java | 1 + .../eureka/EurekaClientAutoConfiguration.java | 9 +++++++ .../EurekaAutoServiceRegistration.java | 13 +++++++--- .../serviceregistry/EurekaRegistration.java | 8 +++++++ .../EurekaClientAutoConfigurationTests.java | 24 +++++++++++++++++++ 5 files changed, 52 insertions(+), 3 deletions(-) diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaInstanceConfig.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaInstanceConfig.java index 0156ac8c..6ea3e889 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaInstanceConfig.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/CloudEurekaInstanceConfig.java @@ -24,5 +24,6 @@ import com.netflix.appinfo.InstanceInfo; */ public interface CloudEurekaInstanceConfig extends EurekaInstanceConfig { void setNonSecurePort(int port); + void setSecurePort(int securePort); InstanceInfo.InstanceStatus getInitialStatus(); } diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java index a49ead99..30f00ab8 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfiguration.java @@ -126,13 +126,21 @@ public class EurekaClientAutoConfiguration { String hostname = eurekaPropertyResolver.getProperty("hostname"); boolean preferIpAddress = Boolean.parseBoolean(eurekaPropertyResolver.getProperty("preferIpAddress")); + boolean isSecurePortEnabled = Boolean.parseBoolean(eurekaPropertyResolver.getProperty("securePortEnabled")); int nonSecurePort = Integer.valueOf(propertyResolver.getProperty("server.port", propertyResolver.getProperty("port", "8080"))); + int managementPort = Integer.valueOf(propertyResolver.getProperty("management.port", String.valueOf(nonSecurePort))); String managementContextPath = propertyResolver.getProperty("management.contextPath", propertyResolver.getProperty("server.contextPath", "/")); EurekaInstanceConfigBean instance = new EurekaInstanceConfigBean(inetUtils); instance.setNonSecurePort(nonSecurePort); instance.setInstanceId(getDefaultInstanceId(propertyResolver)); instance.setPreferIpAddress(preferIpAddress); + + if(isSecurePortEnabled) { + int securePort = Integer.valueOf(propertyResolver.getProperty("server.port", propertyResolver.getProperty("port", "8080"))); + instance.setSecurePort(securePort); + } + if (managementPort != nonSecurePort && managementPort != 0) { if (StringUtils.hasText(hostname)) { instance.setHostname(hostname); @@ -148,6 +156,7 @@ public class EurekaClientAutoConfiguration { if (StringUtils.hasText(healthCheckUrlPath)) { instance.setHealthCheckUrlPath(healthCheckUrlPath); } + String scheme = instance.getSecurePortEnabled() ? "https" : "http"; URL base = new URL(scheme, instance.getHostname(), managementPort, managementContextPath); instance.setStatusPageUrl(new URL(base, StringUtils.trimLeadingCharacter(instance.getStatusPageUrlPath(), '/')).toString()); diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java index 548e9aac..26ac1c4b 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaAutoServiceRegistration.java @@ -36,6 +36,7 @@ import org.springframework.core.Ordered; * @author Spencer Gibb * @author Jon Schneider * @author Jakub Narloch + * @author raiyan */ public class EurekaAutoServiceRegistration implements AutoServiceRegistration, SmartLifecycle, Ordered { @@ -61,9 +62,15 @@ public class EurekaAutoServiceRegistration implements AutoServiceRegistration, S @Override public void start() { - // only set the port if the nonSecurePort is 0 and this.port != 0 - if (this.port.get() != 0 && this.registration.getNonSecurePort() == 0) { - this.registration.setNonSecurePort(this.port.get()); + // only set the port if the nonSecurePort or securePort is 0 and this.port != 0 + if (this.port.get() != 0) { + if (this.registration.getNonSecurePort() == 0) { + this.registration.setNonSecurePort(this.port.get()); + } + + if (this.registration.getSecurePort() == 0 && this.registration.isSecure()) { + this.registration.setSecurePort(this.port.get()); + } } // only initialize if nonSecurePort is greater than 0 and it isn't already running diff --git a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaRegistration.java b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaRegistration.java index 7f2adc55..dd9f991a 100644 --- a/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaRegistration.java +++ b/spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/serviceregistry/EurekaRegistration.java @@ -193,6 +193,14 @@ public class EurekaRegistration implements Registration, Closeable { return this.instanceConfig.getNonSecurePort(); } + public void setSecurePort(int port) { + this.instanceConfig.setSecurePort(port); + } + + public int getSecurePort() { + return this.instanceConfig.getSecurePort(); + } + @Override public void close() throws IOException { this.eurekaClient.shutdown(); diff --git a/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java index cddd720e..3de661c7 100644 --- a/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java +++ b/spring-cloud-netflix-eureka-client/src/test/java/org/springframework/cloud/netflix/eureka/EurekaClientAutoConfigurationTests.java @@ -91,6 +91,23 @@ public class EurekaClientAutoConfigurationTests { this.context.getBeanDefinition("eurekaClient").getFactoryMethodName()); } + @Test + public void securePortPeriods() { + testSecurePort("server.port"); + } + + @Test + public void securePortUnderscores() { + testSecurePort("SERVER_PORT"); + } + + @Test + public void securePort() { + testSecurePort("PORT"); + assertEquals("eurekaClient", + this.context.getBeanDefinition("eurekaClient").getFactoryMethodName()); + } + @Test public void managementPort() { EnvironmentTestUtils.addEnvironment(this.context, "server.port=8989", @@ -331,6 +348,13 @@ public class EurekaClientAutoConfigurationTests { assertEquals(8888, getInstanceConfig().getNonSecurePort()); } + private void testSecurePort(String propName) { + EnvironmentTestUtils.addEnvironment(this.context, "eureka.instance.securePortEnabled=true"); + addEnvironment(this.context, propName + ":8443"); + setupContext(); + assertEquals(8443, getInstanceConfig().getSecurePort()); + } + private EurekaInstanceConfigBean getInstanceConfig() { return this.context.getBean(EurekaInstanceConfigBean.class); }