diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java index 259e9451be7..9fa578385eb 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/nginx/NginxCollectImpl.java @@ -78,7 +78,7 @@ public class NginxCollectImpl extends AbstractCollect { @Override public void preCheck(Metrics metrics) throws IllegalArgumentException { final NginxProtocol nginxProtocol; - if (metrics == null || (nginxProtocol = metrics.getNginx()) == null || nginxProtocol.isInValid()) { + if (metrics == null || (nginxProtocol = metrics.getNginx()) == null || nginxProtocol.isInvalid()) { throw new IllegalArgumentException("Nginx collect must has nginx params"); } } @@ -141,7 +141,7 @@ private HttpUriRequest createHttpRequest(NginxProtocol nginxProtocol) { RequestBuilder requestBuilder = RequestBuilder.get(); String portWithUri = nginxProtocol.getPort() + CollectUtil.replaceUriSpecialChar(nginxProtocol.getUrl()); String host = nginxProtocol.getHost(); - + if (IpDomainUtil.isHasSchema(host)) { requestBuilder.setUri(host + ":" + portWithUri); } else { diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/RegistryImpl.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/RegistryImpl.java index 39de5ae4be7..44ad1196e53 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/RegistryImpl.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/RegistryImpl.java @@ -51,7 +51,9 @@ public class RegistryImpl extends AbstractCollect { @Override public void preCheck(Metrics metrics) throws IllegalArgumentException { + RegistryProtocol registryProtocol = metrics.getRegistry(); + if (Objects.isNull(registryProtocol) || registryProtocol.isInvalid()){ throw new IllegalArgumentException("registry collect must have a valid registry protocol param! "); } diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/discovery/impl/NacosDiscoveryClient.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/discovery/impl/NacosDiscoveryClient.java index 983fe8e364e..906de99803f 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/discovery/impl/NacosDiscoveryClient.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/registry/discovery/impl/NacosDiscoveryClient.java @@ -53,10 +53,17 @@ public ConnectConfig buildConnectConfig(RegistryProtocol registryProtocol) { @Override public void initClient(ConnectConfig connectConfig) { try { + localConnectConfig = connectConfig; namingService = NamingFactory.createNamingService(connectConfig.getHost() + ":" + connectConfig.getPort()); + + // Perform a synchronous probe to verify connectivity eagerly, + // because NamingFactory.createNamingService() establishes the TCP + // connection in a background thread and getServerStatus() returns + // "UP" by default before that thread finishes. + namingService.getServicesOfServer(0, 1); } catch (NacosException exception) { - throw new RuntimeException("Failed to init namingService"); + throw new RuntimeException("Failed to connect to Nacos server: " + exception.getErrMsg()); } } diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/sd/NacosSdCollectImpl.java b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/sd/NacosSdCollectImpl.java index dd6dad2a15c..c553a092654 100644 --- a/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/sd/NacosSdCollectImpl.java +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/main/java/org/apache/hertzbeat/collector/collect/sd/NacosSdCollectImpl.java @@ -39,12 +39,12 @@ */ @Slf4j public class NacosSdCollectImpl extends AbstractCollect { - + /** * Client management to interact with discovery services */ private final DiscoveryClientManagement discoveryClientManagement = new DiscoveryClientManagement(); - + @Override public void preCheck(Metrics metrics) throws IllegalArgumentException { // Validate the required configuration is present @@ -55,7 +55,7 @@ public void preCheck(Metrics metrics) throws IllegalArgumentException { throw new IllegalArgumentException("Nacos service discovery monitoring, the config is invalid"); } } - + @Override public void collect(CollectRep.MetricsData.Builder builder, Metrics metrics) { // Create Registry protocol from NacosSd protocol @@ -74,13 +74,13 @@ public void collect(CollectRep.MetricsData.Builder builder, Metrics metrics) { builder.setMsg("Failed to get Nacos discovery client"); return; } - + // Get all services registered in Nacos List services = discoveryClient.getServices(); if (CollectionUtils.isEmpty(services)) { return; } - + // Populate the response data with service information services.forEach(service -> { CollectRep.ValueRow.Builder valueRowBuilder = CollectRep.ValueRow.newBuilder(); @@ -106,7 +106,7 @@ public void collect(CollectRep.MetricsData.Builder builder, Metrics metrics) { } } } - + @Override public String supportProtocol() { return DispatchConstants.PROTOCOL_NACOS_SD; diff --git a/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/registry/discovery/impl/NacosDiscoveryClientTest.java b/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/registry/discovery/impl/NacosDiscoveryClientTest.java new file mode 100644 index 00000000000..71978cdac8e --- /dev/null +++ b/hertzbeat-collector/hertzbeat-collector-basic/src/test/java/org/apache/hertzbeat/collector/collect/registry/discovery/impl/NacosDiscoveryClientTest.java @@ -0,0 +1,292 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +package org.apache.hertzbeat.collector.collect.registry.discovery.impl; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyInt; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.doThrow; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import com.alibaba.nacos.api.exception.NacosException; +import com.alibaba.nacos.api.naming.NamingFactory; +import com.alibaba.nacos.api.naming.NamingService; +import com.alibaba.nacos.api.naming.pojo.Instance; +import com.alibaba.nacos.api.naming.pojo.ListView; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.hertzbeat.collector.collect.registry.constant.DiscoveryClientHealthStatus; +import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ConnectConfig; +import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServerInfo; +import org.apache.hertzbeat.collector.collect.registry.discovery.entity.ServiceInstance; +import org.apache.hertzbeat.common.entity.job.protocol.RegistryProtocol; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.mockito.junit.jupiter.MockitoExtension; +import org.springframework.test.util.ReflectionTestUtils; + +/** + * Test case for {@link NacosDiscoveryClient} + */ +@ExtendWith(MockitoExtension.class) +class NacosDiscoveryClientTest { + + private NacosDiscoveryClient nacosDiscoveryClient; + + @Mock + private NamingService namingService; + + private static final String HOST = "127.0.0.1"; + private static final int PORT = 8848; + + @BeforeEach + void setUp() { + nacosDiscoveryClient = new NacosDiscoveryClient(); + } + + @Test + void testBuildConnectConfig() { + RegistryProtocol protocol = RegistryProtocol.builder() + .host(HOST) + .port(String.valueOf(PORT)) + .build(); + + ConnectConfig config = nacosDiscoveryClient.buildConnectConfig(protocol); + + assertNotNull(config); + assertEquals(HOST, config.getHost()); + assertEquals(PORT, config.getPort()); + } + + @Test + void testInitClientSuccess() throws NacosException { + ConnectConfig config = ConnectConfig.builder().host(HOST).port(PORT).build(); + + try (MockedStatic mockedFactory = Mockito.mockStatic(NamingFactory.class)) { + mockedFactory.when(() -> NamingFactory.createNamingService(HOST + ":" + PORT)) + .thenReturn(namingService); + ListView emptyView = new ListView<>(); + emptyView.setData(Collections.emptyList()); + when(namingService.getServicesOfServer(0, 1)).thenReturn(emptyView); + + nacosDiscoveryClient.initClient(config); + + mockedFactory.verify(() -> NamingFactory.createNamingService(HOST + ":" + PORT)); + verify(namingService).getServicesOfServer(0, 1); + } + } + + @Test + void testInitClientFailedOnCreate() throws NacosException { + ConnectConfig config = ConnectConfig.builder().host(HOST).port(PORT).build(); + + try (MockedStatic mockedFactory = Mockito.mockStatic(NamingFactory.class)) { + mockedFactory.when(() -> NamingFactory.createNamingService(anyString())) + .thenThrow(new NacosException(500, "connection refused")); + + assertThrows(RuntimeException.class, () -> nacosDiscoveryClient.initClient(config)); + } + } + + @Test + void testInitClientFailedOnProbe() throws NacosException { + ConnectConfig config = ConnectConfig.builder().host(HOST).port(PORT).build(); + + try (MockedStatic mockedFactory = Mockito.mockStatic(NamingFactory.class)) { + mockedFactory.when(() -> NamingFactory.createNamingService(anyString())) + .thenReturn(namingService); + when(namingService.getServicesOfServer(0, 1)) + .thenThrow(new NacosException(500, "host unreachable")); + + RuntimeException ex = assertThrows(RuntimeException.class, () -> nacosDiscoveryClient.initClient(config)); + assertTrue(ex.getMessage().contains("Failed to connect")); + } + } + + @Test + void testGetServerInfoWithNullNamingService() { + assertThrows(NullPointerException.class, () -> nacosDiscoveryClient.getServerInfo()); + } + + @Test + void testGetServerInfoWhenHealthy() { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.UP); + + ServerInfo serverInfo = nacosDiscoveryClient.getServerInfo(); + + assertNotNull(serverInfo); + assertEquals(HOST, serverInfo.getAddress()); + assertEquals(String.valueOf(PORT), serverInfo.getPort()); + } + + @Test + void testGetServerInfoWhenNotHealthy() { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.DOWN); + + assertThrows(RuntimeException.class, () -> nacosDiscoveryClient.getServerInfo()); + } + + @Test + void testGetServicesWithNullNamingService() { + List result = nacosDiscoveryClient.getServices(); + + assertNotNull(result); + assertTrue(result.isEmpty()); + } + + @Test + void testGetServicesWhenNotHealthy() { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.DOWN); + + List result = nacosDiscoveryClient.getServices(); + + assertNotNull(result); + assertTrue(result.isEmpty()); + } + + @Test + void testGetServicesSuccess() throws NacosException { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.UP); + + ListView serviceNames = new ListView<>(); + serviceNames.setData(Collections.singletonList("test-service")); + when(namingService.getServicesOfServer(0, 9999)).thenReturn(serviceNames); + + Instance instance = new Instance(); + instance.setInstanceId("inst-1"); + instance.setServiceName("test-service"); + instance.setIp("192.168.1.1"); + instance.setPort(9090); + instance.setWeight(1.0); + Map metadata = new HashMap<>(); + metadata.put("version", "1.0"); + instance.setMetadata(metadata); + instance.setHealthy(true); + + when(namingService.getAllInstances("test-service")).thenReturn(Collections.singletonList(instance)); + + List result = nacosDiscoveryClient.getServices(); + + assertNotNull(result); + assertEquals(1, result.size()); + ServiceInstance serviceInstance = result.get(0); + assertEquals("inst-1", serviceInstance.getServiceId()); + assertEquals("test-service", serviceInstance.getServiceName()); + assertEquals("192.168.1.1", serviceInstance.getAddress()); + assertEquals(9090, serviceInstance.getPort()); + assertEquals(DiscoveryClientHealthStatus.UP, serviceInstance.getHealthStatus()); + } + + @Test + void testGetServicesWithUnhealthyInstance() throws NacosException { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.UP); + + ListView serviceNames = new ListView<>(); + serviceNames.setData(Collections.singletonList("test-service")); + when(namingService.getServicesOfServer(0, 9999)).thenReturn(serviceNames); + + Instance instance = new Instance(); + instance.setInstanceId("inst-down"); + instance.setServiceName("test-service"); + instance.setIp("10.0.0.1"); + instance.setPort(8080); + instance.setWeight(1.0); + instance.setHealthy(false); + + when(namingService.getAllInstances("test-service")).thenReturn(Collections.singletonList(instance)); + + List result = nacosDiscoveryClient.getServices(); + + assertEquals(1, result.size()); + assertEquals(DiscoveryClientHealthStatus.DOWN, result.get(0).getHealthStatus()); + } + + @Test + void testGetServicesThrowsNacosException() throws NacosException { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.UP); + when(namingService.getServicesOfServer(anyInt(), anyInt())) + .thenThrow(new NacosException(500, "server error")); + + assertThrows(RuntimeException.class, () -> nacosDiscoveryClient.getServices()); + } + + @Test + void testHealthCheckReturnsTrue() { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.UP); + + assertTrue(nacosDiscoveryClient.healthCheck()); + } + + @Test + void testHealthCheckReturnsFalse() { + injectNamingServiceAndConfig(); + when(namingService.getServerStatus()).thenReturn(DiscoveryClientHealthStatus.DOWN); + + assertFalse(nacosDiscoveryClient.healthCheck()); + } + + @Test + void testCloseWithNullNamingService() { + nacosDiscoveryClient.close(); + } + + @Test + void testCloseSuccess() throws NacosException { + injectNamingServiceAndConfig(); + + nacosDiscoveryClient.close(); + + verify(namingService).shutDown(); + } + + @Test + void testCloseThrowsNacosException() throws NacosException { + injectNamingServiceAndConfig(); + doThrow(new NacosException(500, "shutdown error")).when(namingService).shutDown(); + + nacosDiscoveryClient.close(); + + verify(namingService).shutDown(); + } + + private void injectNamingServiceAndConfig() { + ReflectionTestUtils.setField(nacosDiscoveryClient, "namingService", namingService); + ConnectConfig config = ConnectConfig.builder().host(HOST).port(PORT).build(); + ReflectionTestUtils.setField(nacosDiscoveryClient, "localConnectConfig", config); + } +} diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java index 154669560fa..8caa8dcd1cb 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java @@ -77,4 +77,11 @@ public class SshTunnel implements CommonRequestProtocol, Protocol { * share connection session */ private String shareConnection = "true"; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsProtocol.java index 9982d25da3a..43c3102c36f 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsProtocol.java @@ -39,6 +39,7 @@ public class DnsProtocol implements CommonRequestProtocol, Protocol { private String queryClass; private String recordType; + @Override public boolean isInvalid() { return StringUtils.isAnyBlank(dnsServerIP, port, address, timeout, tcp, queryClass, recordType); } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java index 9597ac6f910..b2d00590edc 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/DnsSdProtocol.java @@ -30,7 +30,7 @@ @AllArgsConstructor @NoArgsConstructor public class DnsSdProtocol implements Protocol { - + private String host; private String port; @@ -38,4 +38,11 @@ public class DnsSdProtocol implements Protocol { private String recordType; private String recordName; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/EurekaSdProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/EurekaSdProtocol.java index c522d919d8e..0c8cf93f761 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/EurekaSdProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/EurekaSdProtocol.java @@ -32,4 +32,11 @@ public class EurekaSdProtocol implements Protocol{ private String url; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/FtpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/FtpProtocol.java index f095116c750..a3d2b525ab6 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/FtpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/FtpProtocol.java @@ -65,4 +65,11 @@ public class FtpProtocol implements CommonRequestProtocol, Protocol { * */ private String ssl = "false"; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpProtocol.java index 1b48e4871a9..61b36f0e7a7 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpProtocol.java @@ -133,4 +133,11 @@ public static class Authorization { */ private String digestAuthPassword; } + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java index 67d50fa6e57..a90751df819 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/HttpSdProtocol.java @@ -30,6 +30,13 @@ @AllArgsConstructor @NoArgsConstructor public class HttpSdProtocol implements Protocol { - + private String url; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java index 33761967445..4133eb74803 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IcmpProtocol.java @@ -44,4 +44,11 @@ public class IcmpProtocol implements CommonRequestProtocol, Protocol { @Override public void setPort(String port) { } + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ImapProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ImapProtocol.java index fe8bec07bbf..35e4a328544 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ImapProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ImapProtocol.java @@ -64,4 +64,10 @@ public class ImapProtocol implements CommonRequestProtocol, Protocol { */ private String folderName; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java index b2dca81df57..103626f541d 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/IpmiProtocol.java @@ -69,4 +69,11 @@ public class IpmiProtocol implements CommonRequestProtocol, Protocol { static class Field { } + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JdbcProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JdbcProtocol.java index 7f378a5f70e..0454ffcbfce 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JdbcProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JdbcProtocol.java @@ -79,4 +79,11 @@ public class JdbcProtocol implements CommonRequestProtocol, Protocol { * ssh tunnel */ private SshTunnel sshTunnel; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java index b08ea6e4e43..192d65e6c62 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/JmxProtocol.java @@ -65,4 +65,10 @@ public class JmxProtocol implements CommonRequestProtocol, Protocol { */ private String objectName; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/KafkaProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/KafkaProtocol.java index c2bc9a80606..f1a30655779 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/KafkaProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/KafkaProtocol.java @@ -55,4 +55,11 @@ public class KafkaProtocol implements CommonRequestProtocol, Protocol { * Monitor internal topic */ private String monitorInternalTopic = "false"; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java index cba7e578510..6384600b94d 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MemcachedProtocol.java @@ -41,5 +41,11 @@ public class MemcachedProtocol implements CommonRequestProtocol, Protocol { */ private String port; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java index c019873b3c7..b08840dce3f 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ModbusProtocol.java @@ -52,4 +52,11 @@ public class ModbusProtocol implements Protocol { private String timeout; private List registerAddresses; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MongodbProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MongodbProtocol.java index 4ab005b1c1c..0f58343bfb3 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MongodbProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MongodbProtocol.java @@ -77,4 +77,11 @@ public class MongodbProtocol implements CommonRequestProtocol, Protocol { */ private String timeout; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } + } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MqttProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MqttProtocol.java index d0d1fa7dd9b..e80f6604445 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MqttProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/MqttProtocol.java @@ -109,4 +109,11 @@ public boolean hasAuth() { return StringUtils.isNotBlank(this.username) && StringUtils.isNotBlank(this.password); } + @Override + public boolean isInvalid() { + + // todo: add + return true; + } + } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NebulaGraphProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NebulaGraphProtocol.java index d67ed5c8ada..48d999984a0 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NebulaGraphProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NebulaGraphProtocol.java @@ -57,4 +57,11 @@ public class NebulaGraphProtocol implements CommonRequestProtocol, Protocol { */ private String timeout; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } + } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NginxProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NginxProtocol.java index 95e655c87b8..62ca0e38c5a 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NginxProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NginxProtocol.java @@ -60,7 +60,8 @@ public class NginxProtocol implements CommonRequestProtocol, Protocol { * Validates the relevant parameters * @return is invalid true or false */ - public boolean isInValid() { + @Override + public boolean isInvalid() { return StringUtils.isAnyBlank(host, port, timeout); } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NgqlProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NgqlProtocol.java index 6af38d125d2..a5b775aac00 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NgqlProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NgqlProtocol.java @@ -71,4 +71,11 @@ public class NgqlProtocol implements CommonRequestProtocol, Protocol { * how to parse data */ private String parseType; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NtpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NtpProtocol.java index d7228020513..ee9178b02b4 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NtpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/NtpProtocol.java @@ -44,4 +44,11 @@ public class NtpProtocol implements CommonRequestProtocol, Protocol { * TIME OUT PERIOD */ private String timeout; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java index 5b240774136..bbcc01b1c98 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PlcProtocol.java @@ -52,4 +52,11 @@ public class PlcProtocol implements Protocol { private List registerAddresses; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } + } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Pop3Protocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Pop3Protocol.java index baa762db623..4adca6b648f 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Pop3Protocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Pop3Protocol.java @@ -61,6 +61,7 @@ public class Pop3Protocol implements CommonRequestProtocol, Protocol { */ private String authorize; + @Override public boolean isInvalid() { return StringUtils.isAllBlank(host, port, timeout, ssl, email, authorize); } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java index 2744efbfaca..3411a2b56fb 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PrometheusProtocol.java @@ -104,4 +104,11 @@ public static class Authorization { */ private String digestAuthPassword; } + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java index fa51b9cc3ab..7d41fe91345 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/Protocol.java @@ -21,4 +21,11 @@ * Base class for all protocols */ public interface Protocol { + + /** + * Check Protocol params vaild. + * @return True or False. + */ + boolean isInvalid(); + } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java index e7998179a44..5cd8a46dc4a 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/PushProtocol.java @@ -36,4 +36,11 @@ public class PushProtocol implements CommonRequestProtocol, Protocol { private String port; private String uri = "/api/push"; private List fields; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java index 3155d2fa7a1..1d1764936a5 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedfishProtocol.java @@ -62,4 +62,11 @@ public class RedfishProtocol implements CommonRequestProtocol, Protocol { private String schema; private List jsonPath; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedisProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedisProtocol.java index 393e43f91ad..b0c240fcecc 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedisProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RedisProtocol.java @@ -67,4 +67,11 @@ public class RedisProtocol implements CommonRequestProtocol, Protocol { */ private SshTunnel sshTunnel; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } + } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java index ce459071ce4..a33f5cfd754 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocol.java @@ -25,6 +25,9 @@ import lombok.NoArgsConstructor; import org.apache.commons.lang3.StringUtils; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validPort; +import static org.apache.hertzbeat.common.util.IpDomainUtil.validateIpDomain; + /** * Registry protocol */ @@ -37,7 +40,10 @@ public class RegistryProtocol implements CommonRequestProtocol, Protocol { private String port; private String discoveryClientTypeName; + @Override public boolean isInvalid() { - return StringUtils.isAnyBlank(host, String.valueOf(port), discoveryClientTypeName); + + return validateIpDomain(host) && validPort(port) + && StringUtils.isAnyBlank(host, String.valueOf(port), discoveryClientTypeName); } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RocketmqProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RocketmqProtocol.java index e7bfe12b511..3688e23a548 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RocketmqProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/RocketmqProtocol.java @@ -65,4 +65,11 @@ public void setHost(String host) { public void setPort(String port) { this.namesrvPort = port; } + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java index cebe9e8a11f..bfcd18d0abd 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/S7Protocol.java @@ -56,4 +56,11 @@ public class S7Protocol implements Protocol { private String timeout; private List registerAddresses; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ScriptProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ScriptProtocol.java index eeae02df8d8..9876a751de5 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ScriptProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ScriptProtocol.java @@ -69,4 +69,11 @@ public void setHost(String host) { public void setPort(String port) { this.scriptPath = port; } + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SmtpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SmtpProtocol.java index aa42bfa0bbf..fa8d8a120b1 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SmtpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SmtpProtocol.java @@ -54,4 +54,11 @@ public class SmtpProtocol implements CommonRequestProtocol, Protocol { * Sent command */ private String cmd; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SnmpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SnmpProtocol.java index 109c1acb4f9..229cde701ec 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SnmpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SnmpProtocol.java @@ -91,4 +91,11 @@ public class SnmpProtocol implements CommonRequestProtocol, Protocol { * v3 requires */ private String privPasswordEncryption; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SshProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SshProtocol.java index b9c708a4861..88e5f8b78d5 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SshProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/SshProtocol.java @@ -65,7 +65,7 @@ public class SshProtocol implements CommonRequestProtocol, Protocol { * private key passphrase (optional) */ private String privateKeyPassphrase; - + /** * reuse connection session */ @@ -110,4 +110,11 @@ public class SshProtocol implements CommonRequestProtocol, Protocol { * Proxy private key (optional) */ private String proxyPrivateKey; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java index c10b12e91c0..a485c87b02d 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/TelnetProtocol.java @@ -51,4 +51,11 @@ public class TelnetProtocol implements CommonRequestProtocol, Protocol { */ private String cmd; + @Override + public boolean isInvalid() { + + // todo: add + return true; + } + } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java index 221eccc4f63..f062b39fb19 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/UdpProtocol.java @@ -34,7 +34,7 @@ public class UdpProtocol implements CommonRequestProtocol, Protocol { * IP ADDRESS OR DOMAIN NAME OF THE PEER HOST */ private String host; - + /** * Port number */ @@ -44,9 +44,16 @@ public class UdpProtocol implements CommonRequestProtocol, Protocol { * TIME OUT PERIOD */ private String timeout; - + /** * Custom protocol packet hexString Hexadecimal character string */ private String content; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java index 84687cd2027..c4f5045fc1f 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/WebsocketProtocol.java @@ -39,9 +39,16 @@ public class WebsocketProtocol implements CommonRequestProtocol, Protocol { * Port number */ private String port; - + /** * The path to the websocket endpoint */ private String path; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ZookeeperSdProtocol.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ZookeeperSdProtocol.java index 830398d517e..7b53f354b54 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ZookeeperSdProtocol.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/entity/job/protocol/ZookeeperSdProtocol.java @@ -30,8 +30,15 @@ @AllArgsConstructor @NoArgsConstructor public class ZookeeperSdProtocol implements Protocol{ - + private String url; private String pathPrefix; + + @Override + public boolean isInvalid() { + + // todo: add + return true; + } } diff --git a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java index 3c72baa41ab..83eabdf2e4f 100644 --- a/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java +++ b/hertzbeat-common-core/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java @@ -149,4 +149,22 @@ public static String getCurrentHostName() { } } + /** + * check port is valid. + * @return true if valid + */ + public static boolean validPort(String portStr) { + + if (portStr == null || portStr.trim().isEmpty()) { + return false; + } + + try { + int port = Integer.parseInt(portStr); + return port >= 0 && port <= 65535; + } catch (NumberFormatException e) { + return false; + } + } + } diff --git a/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocolTest.java b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocolTest.java new file mode 100644 index 00000000000..f11e2ba9118 --- /dev/null +++ b/hertzbeat-common-core/src/test/java/org/apache/hertzbeat/common/entity/job/protocol/RegistryProtocolTest.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + + +package org.apache.hertzbeat.common.entity.job.protocol; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +class RegistryProtocolTest { + + @Test + void isInvalid() { + + RegistryProtocol protocol1 = new RegistryProtocol(); + protocol1.setPort("8080"); + protocol1.setHost("127.0.0.1"); + assertTrue(protocol1.isInvalid()); + + RegistryProtocol protocol2 = new RegistryProtocol(); + protocol2.setPort("8080"); + protocol2.setHost("www.baidu.com"); + assertTrue(protocol2.isInvalid()); + + RegistryProtocol protocol3 = new RegistryProtocol(); + protocol3.setPort("8080"); + protocol3.setHost("www.baidu.com."); + assertFalse(protocol3.isInvalid()); + + RegistryProtocol protocol4 = new RegistryProtocol(); + protocol3.setPort("80800"); + protocol3.setHost("10.45.56.344"); + assertFalse(protocol4.isInvalid()); + } +} diff --git a/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java b/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java index 154669560fa..7eb9bb2c790 100644 --- a/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java +++ b/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/entity/job/SshTunnel.java @@ -77,4 +77,11 @@ public class SshTunnel implements CommonRequestProtocol, Protocol { * share connection session */ private String shareConnection = "true"; + + @Override + public boolean isInvalid() { + + // todo add + return false; + } } diff --git a/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java b/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java index e882cbef59f..a2a7ae09629 100644 --- a/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java +++ b/hertzbeat-common-spring/src/main/java/org/apache/hertzbeat/common/util/IpDomainUtil.java @@ -150,4 +150,20 @@ public static String getCurrentHostName() { } } + /** + * check port is valid. + * @return true if valid + */ + public static boolean validPort(String portStr) { + if (portStr == null || portStr.trim().isEmpty()) { + return false; + } + try { + int port = Integer.parseInt(portStr); + return port >= 0 && port <= 65535; + } catch (NumberFormatException e) { + return false; + } + } + }