From 43d655bedb7e6eb91914b85a244081af35fa2119 Mon Sep 17 00:00:00 2001 From: Auri Munoz Date: Mon, 21 Oct 2024 11:57:06 +0200 Subject: [PATCH] Fail service registration if address is not provided --- .../smallrye/stork/api/ServiceRegistrar.java | 7 +++++ .../consul/ConsulServiceRegistrar.java | 1 + .../consul/ConsulServiceRegistrationTest.java | 24 +++++++++++++++++ .../eureka/EurekaServiceRegistrar.java | 2 ++ .../eureka/EurekaRegistrationTest.java | 24 +++++++++++++++++ .../StaticListServiceRegistrar.java | 1 + .../StaticServiceRegistrationTest.java | 26 +++++++++++++++++++ 7 files changed, 85 insertions(+) diff --git a/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java b/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java index b8d95547..04d8889d 100644 --- a/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java +++ b/api/src/main/java/io/smallrye/stork/api/ServiceRegistrar.java @@ -5,9 +5,16 @@ public interface ServiceRegistrar & MetadataKey> { default Uni registerServiceInstance(String serviceName, String ipAddress, int defaultPort) { + checkAddressNotNull(ipAddress); return registerServiceInstance(serviceName, Metadata.empty(), ipAddress, defaultPort); } + default void checkAddressNotNull(String ipAddress) { + if (ipAddress == null || ipAddress.isEmpty() || ipAddress.isBlank()) { + throw new IllegalArgumentException("Parameter ipAddress should be provided."); + } + } + Uni registerServiceInstance(String serviceName, Metadata metadata, String ipAddress, int defaultPort); diff --git a/service-registration/consul/src/main/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrar.java b/service-registration/consul/src/main/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrar.java index 0d56009a..5a1e8e33 100644 --- a/service-registration/consul/src/main/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrar.java +++ b/service-registration/consul/src/main/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrar.java @@ -37,6 +37,7 @@ public ConsulServiceRegistrar(ConsulRegistrarConfiguration config, String servic @Override public Uni registerServiceInstance(String serviceName, Metadata metadata, String ipAddress, int defaultPort) { + checkAddressNotNull(ipAddress); String consulId = metadata.getMetadata().get(ConsulMetadataKey.META_CONSUL_SERVICE_ID).toString(); diff --git a/service-registration/consul/src/test/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrationTest.java b/service-registration/consul/src/test/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrationTest.java index 4719c949..0ac686e9 100644 --- a/service-registration/consul/src/test/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrationTest.java +++ b/service-registration/consul/src/test/java/io/smallrye/stork/serviceregistration/consul/ConsulServiceRegistrationTest.java @@ -3,6 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import java.time.Duration; import java.util.Map; @@ -86,4 +88,26 @@ void shouldRegisterServiceInstancesInConsul() throws InterruptedException { assertThat(subscriber.awaitItem().getItem()).isNotNull(); } + + @Test + void shouldFailIfNoIpAddressProvided() throws InterruptedException { + String serviceName = "my-service"; + TestConfigProvider.addServiceConfig(serviceName, null, null, "consul", + null, Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort), "refresh-period", "5"), + Map.of("consul-host", "localhost", "consul-port", String.valueOf(consulPort))); + Stork stork = StorkTestUtils.getNewStorkInstance(); + + ServiceRegistrar consulRegistrar = stork.getService(serviceName).getServiceRegistrar(); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + consulRegistrar.registerServiceInstance(serviceName, Metadata.of(ConsulMetadataKey.class) + .with(ConsulMetadataKey.META_CONSUL_SERVICE_ID, serviceName), null, 8406); + }); + + String expectedMessage = "Parameter ipAddress should be provided."; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + + } } diff --git a/service-registration/eureka/src/main/java/io/smallrye/stork/serviceregistration/eureka/EurekaServiceRegistrar.java b/service-registration/eureka/src/main/java/io/smallrye/stork/serviceregistration/eureka/EurekaServiceRegistrar.java index eb487e2f..5338b4c4 100644 --- a/service-registration/eureka/src/main/java/io/smallrye/stork/serviceregistration/eureka/EurekaServiceRegistrar.java +++ b/service-registration/eureka/src/main/java/io/smallrye/stork/serviceregistration/eureka/EurekaServiceRegistrar.java @@ -41,6 +41,8 @@ public EurekaServiceRegistrar(EurekaRegistrarConfiguration config, String servic public Uni registerServiceInstance(String serviceName, Metadata metadata, String ipAddress, int defaultPort) { + checkAddressNotNull(ipAddress); + return registerApplicationInstance(client, serviceName, metadata.getMetadata().get(EurekaMetadataKey.META_EUREKA_SERVICE_ID).toString(), ipAddress, null, defaultPort, null, -1, "UP", ""); diff --git a/service-registration/eureka/src/test/java/io/smallrye/stork/serviceregistration/eureka/EurekaRegistrationTest.java b/service-registration/eureka/src/test/java/io/smallrye/stork/serviceregistration/eureka/EurekaRegistrationTest.java index 43a86bb9..1e06425a 100644 --- a/service-registration/eureka/src/test/java/io/smallrye/stork/serviceregistration/eureka/EurekaRegistrationTest.java +++ b/service-registration/eureka/src/test/java/io/smallrye/stork/serviceregistration/eureka/EurekaRegistrationTest.java @@ -3,6 +3,8 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.fail; import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; import java.time.Duration; import java.util.Map; @@ -104,4 +106,26 @@ public void testRegistrationServiceInstances(TestInfo info) { } + @Test + void shouldFailIfNoIpAddressProvided() throws InterruptedException { + String serviceName = "my-service"; + TestConfigProvider.addServiceConfig(serviceName, null, null, "eureka", null, null, + Map.of("eureka-host", eureka.getHost(), "eureka-port", String.valueOf(port))); + + Stork stork = StorkTestUtils.getNewStorkInstance(); + + ServiceRegistrar eurekaServiceRegistrar = stork.getService(serviceName).getServiceRegistrar(); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + eurekaServiceRegistrar.registerServiceInstance(serviceName, Metadata.of(EurekaMetadataKey.class) + .with(EurekaMetadataKey.META_EUREKA_SERVICE_ID, serviceName), null, 8406); + }); + + String expectedMessage = "Parameter ipAddress should be provided."; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + + } + } diff --git a/service-registration/static-list/src/main/java/io/smallrye/stork/serviceregistration/staticlist/StaticListServiceRegistrar.java b/service-registration/static-list/src/main/java/io/smallrye/stork/serviceregistration/staticlist/StaticListServiceRegistrar.java index 03ea9cbd..53e5b942 100644 --- a/service-registration/static-list/src/main/java/io/smallrye/stork/serviceregistration/staticlist/StaticListServiceRegistrar.java +++ b/service-registration/static-list/src/main/java/io/smallrye/stork/serviceregistration/staticlist/StaticListServiceRegistrar.java @@ -24,6 +24,7 @@ public StaticListServiceRegistrar(StaticRegistrarConfiguration config, String se public Uni registerServiceInstance(String serviceName, Metadata metadata, String ipAddress, int defaultPort) { + checkAddressNotNull(ipAddress); HostAndPort hostAndPortToAdd = StorkAddressUtils.parseToHostAndPort(ipAddress, defaultPort, "service '" + serviceName + "'"); String hostAndPortToAddString = StorkAddressUtils.parseToString(hostAndPortToAdd); diff --git a/service-registration/static-list/src/test/java/io/smallrye/stork/serviceregistration/staticlist/StaticServiceRegistrationTest.java b/service-registration/static-list/src/test/java/io/smallrye/stork/serviceregistration/staticlist/StaticServiceRegistrationTest.java index 0788be8a..60d44d81 100644 --- a/service-registration/static-list/src/test/java/io/smallrye/stork/serviceregistration/staticlist/StaticServiceRegistrationTest.java +++ b/service-registration/static-list/src/test/java/io/smallrye/stork/serviceregistration/staticlist/StaticServiceRegistrationTest.java @@ -1,6 +1,8 @@ package io.smallrye.stork.serviceregistration.staticlist; import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.List; import java.util.Map; @@ -89,4 +91,28 @@ void shouldRegisterServiceInstancesWithSchemeAndPath() { assertThat(addresses).hasSize(1); assertThat(addresses.get(0)).isEqualTo("localhost:8081/hello"); } + + @Test + void shouldFailIfAddresseNull() { + TestConfigProvider.addServiceConfig("first-service", null, null, "static", + null, null, null); + + stork = StorkTestUtils.getNewStorkInstance(); + + String serviceName = "first-service"; + + ServiceRegistrar staticRegistrar = stork.getService(serviceName).getServiceRegistrar(); + + staticRegistrar.registerServiceInstance(serviceName, "http://localhost:8081/hello", 8080); + + Exception exception = assertThrows(IllegalArgumentException.class, () -> { + staticRegistrar.registerServiceInstance(serviceName, null, 8080); + }); + + String expectedMessage = "Parameter ipAddress should be provided."; + String actualMessage = exception.getMessage(); + + assertTrue(actualMessage.contains(expectedMessage)); + + } }