]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-15192 Disable ip addr validation for sonar.cluster.es.hosts
authorJacek <jacek.poreda@sonarsource.com>
Tue, 20 Jul 2021 18:32:17 +0000 (13:32 -0500)
committersonartech <sonartech@sonarsource.com>
Wed, 21 Jul 2021 20:03:01 +0000 (20:03 +0000)
* provide new propert sonar.cluster.es.discovery.seed.hosts to allow dynamic node discovery in k8s

server/sonar-main/src/main/java/org/sonar/application/config/ClusterSettings.java
server/sonar-main/src/main/java/org/sonar/application/es/EsSettings.java
server/sonar-main/src/test/java/org/sonar/application/config/ClusterSettingsTest.java
server/sonar-process/src/main/java/org/sonar/process/ProcessProperties.java

index b6415d42483a13446fb60d95b48b1738e2c9dd9e..140a0f6944a20d590501425cde3ceb1ccc17dc53 100644 (file)
@@ -96,7 +96,7 @@ public class ClusterSettings implements Consumer<Props> {
   private void checkForApplicationNode(Props props) {
     ensureNotH2(props);
     requireValue(props, AUTH_JWT_SECRET);
-    Set<AddressAndPort> hzNodes = parseHosts(CLUSTER_HZ_HOSTS, requireValue(props, CLUSTER_HZ_HOSTS));
+    Set<AddressAndPort> hzNodes = parseAndCheckHosts(CLUSTER_HZ_HOSTS, requireValue(props, CLUSTER_HZ_HOSTS));
     ensureNotLoopbackAddresses(CLUSTER_HZ_HOSTS, hzNodes);
     checkClusterNodeHost(props);
     checkClusterSearchHosts(props);
@@ -117,28 +117,32 @@ public class ClusterSettings implements Consumer<Props> {
   }
 
   private void checkClusterSearchHosts(Props props) {
-    Set<AddressAndPort> searchHosts = parseHosts(CLUSTER_SEARCH_HOSTS, requireValue(props, CLUSTER_SEARCH_HOSTS));
+    Set<AddressAndPort> searchHosts = parseAndCheckHosts(CLUSTER_SEARCH_HOSTS, requireValue(props, CLUSTER_SEARCH_HOSTS));
     ensureNotLoopbackAddresses(CLUSTER_SEARCH_HOSTS, searchHosts);
   }
 
   private void checkClusterEsHosts(Props props) {
-    Set<AddressAndPort> esHosts = parseHosts(CLUSTER_ES_HOSTS, requireValue(props, CLUSTER_ES_HOSTS));
+    Set<AddressAndPort> esHosts = parseHosts(requireValue(props, CLUSTER_ES_HOSTS));
     ensureNotLoopbackAddresses(CLUSTER_ES_HOSTS, esHosts);
     ensureEitherPortsAreProvidedOrOnlyHosts(CLUSTER_ES_HOSTS, esHosts);
   }
 
-  private Set<AddressAndPort> parseHosts(Property property, String value) {
-    Set<AddressAndPort> res = stream(value.split(","))
+  private static Set<AddressAndPort> parseHosts(String value) {
+    return stream(value.split(","))
       .filter(Objects::nonNull)
       .map(String::trim)
       .map(ClusterSettings::parseHost)
       .collect(toSet());
+  }
+
+  private Set<AddressAndPort> parseAndCheckHosts(Property property, String value) {
+    Set<AddressAndPort> res = parseHosts( value);
     checkValidHosts(property, res);
     return res;
   }
 
   private void checkValidHosts(Property property, Set<AddressAndPort> addressAndPorts) {
-    List<String> invalidHosts = addressAndPorts.stream()
+   List<String> invalidHosts = addressAndPorts.stream()
       .map(AddressAndPort::getHost)
       .filter(t -> !network.toInetAddress(t).isPresent())
       .sorted()
index 79072191cce30df44d14de163545c283769887e0..562fba9d87bc02b623747e4a9b8d91451ffb3f25 100644 (file)
@@ -36,6 +36,7 @@ import org.sonar.process.System2;
 
 import static java.lang.String.valueOf;
 import static org.sonar.process.ProcessProperties.Property.CLUSTER_ENABLED;
+import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_DISCOVERY_SEED_HOSTS;
 import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_HOSTS;
 import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_KEYSTORE;
 import static org.sonar.process.ProcessProperties.Property.CLUSTER_ES_TRUSTSTORE;
@@ -211,8 +212,9 @@ public class EsSettings {
       builder.put(ES_NETWORK_HOST_KEY, nodeTransportHost);
 
       String hosts = props.value(CLUSTER_ES_HOSTS.getKey(), loopbackAddress.getHostAddress());
+      String discoveryHosts = props.value(CLUSTER_ES_DISCOVERY_SEED_HOSTS.getKey(), hosts);
       LOGGER.info("Elasticsearch cluster enabled. Connect to hosts [{}]", hosts);
-      builder.put("discovery.seed_hosts", hosts);
+      builder.put("discovery.seed_hosts", discoveryHosts);
       builder.put("cluster.initial_master_nodes", hosts);
     }
 
index 7a10706dbd34f32bfcf6f0ee9c90abf5b4fc6b35..789c8664da2835fa93aecea0eeee88bcaa5f8709 100644 (file)
@@ -379,7 +379,7 @@ public class ClusterSettingsTest {
   }
 
   @Test
-  public void validate_host_resolved_in_any_cluster_property_of_SEARCH_node() {
+  public void validate_host_resolved_in_node_search_host_property_of_SEARCH_node() {
     TestAppSettings settings = new TestAppSettings(ImmutableMap.<String, String>builder()
       .put(CLUSTER_ENABLED.getKey(), "true")
       .put(CLUSTER_NODE_TYPE.getKey(), "search")
@@ -387,8 +387,6 @@ public class ClusterSettingsTest {
       .put(CLUSTER_NODE_SEARCH_HOST.getKey(), "search_host")
       .put(CLUSTER_NODE_ES_HOST.getKey(), "search_host").build());
 
-    verifyHostIsChecked(settings, ImmutableList.of("remote_search_host_1"), "Address in property sonar.cluster.es.hosts is not a valid address: remote_search_host_1");
-    verifyHostIsChecked(settings, ImmutableList.of("remote_search_host_2"), "Address in property sonar.cluster.es.hosts is not a valid address: remote_search_host_2");
     verifyHostIsChecked(settings, ImmutableList.of("search_host"), "Address in property sonar.cluster.node.search.host is not a valid address: search_host");
   }
 
index a7da1d7672dd3eeae979e4a770153b001c759b0e..9217274a281004e4b2eee552f7fc639fc881f265 100644 (file)
@@ -132,6 +132,7 @@ public class ProcessProperties {
 
     // search node only settings
     CLUSTER_ES_HOSTS("sonar.cluster.es.hosts"),
+    CLUSTER_ES_DISCOVERY_SEED_HOSTS("sonar.cluster.es.discovery.seed.hosts"),
     CLUSTER_NODE_SEARCH_HOST("sonar.cluster.node.search.host"),
     CLUSTER_NODE_SEARCH_PORT("sonar.cluster.node.search.port"),
     CLUSTER_NODE_ES_HOST("sonar.cluster.node.es.host"),