Skip to content

Commit e6c5fe7

Browse files
authored
Merge pull request #215 from splunk/verify-cluster-master-hosts
Feature added to include cluster master hosts for HostName verification
2 parents cc0066c + 15d6b1d commit e6c5fe7

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

splunk/src/main/java/com/splunk/HttpService.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,8 @@
2323
import java.io.OutputStreamWriter;
2424
import java.net.*;
2525
import java.security.cert.X509Certificate;
26-
import java.util.HashMap;
27-
import java.util.List;
28-
import java.util.Map;
26+
import java.util.*;
2927
import java.util.Map.Entry;
30-
import java.util.Objects;
3128

3229
/**
3330
* The {@code HttpService} class represents a generic HTTP service at a given
@@ -50,13 +47,11 @@ public class HttpService {
5047
private static SSLSocketFactory sslSocketFactory = createSSLFactory();
5148
private static String HTTPS_SCHEME = "https";
5249
private static String HTTP_SCHEME = "http";
53-
private static String HOSTNAME = "localhost";
54-
private static String HOSTIP = "127.0.0.1";
55-
private static String HOSTIPv6 = "::1";
50+
private static List<String> VALID_HOSTS = new ArrayList<String>(Arrays.asList("localhost", "127.0.0.1", "::1"));
5651

5752
private static final HostnameVerifier HOSTNAME_VERIFIER = new HostnameVerifier() {
5853
public boolean verify(String s, SSLSession sslSession) {
59-
if (s.equals(HOSTNAME) || s.equals(HOSTIP) || s.equals(HOSTIPv6)) {
54+
if(VALID_HOSTS.contains(s)){
6055
return true;
6156
} else {
6257
HostnameVerifier hv = HttpsURLConnection.getDefaultHostnameVerifier();
@@ -239,6 +234,14 @@ public static void setSslSecurityProtocol(SSLSecurityProtocol securityProtocol)
239234
}
240235
}
241236

237+
/**
238+
* Adds list of Cluster Master Hosts to the list of Valid Hosts for Hostname verification.
239+
* @param searchHeadService Splunk SearchHead Service instance
240+
*/
241+
public static void addClusterMasterURIsToHosts(Service searchHeadService){
242+
VALID_HOSTS.addAll(searchHeadService.getClusterMasters());
243+
}
244+
242245
/**
243246
* Returns the URL prefix of this service, consisting of
244247
* {@code scheme://host[:port]}.

splunk/src/main/java/com/splunk/Service.java

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
import java.io.IOException;
2020
import java.io.InputStream;
2121
import java.io.UnsupportedEncodingException;
22-
import java.net.Socket;
23-
import java.net.URLEncoder;
24-
import java.net.URLStreamHandler;
22+
import java.net.*;
23+
import java.util.ArrayList;
24+
import java.util.List;
2525
import java.util.Map;
2626

2727
/**
@@ -594,6 +594,35 @@ public ServiceInfo getInfo() {
594594
return new ServiceInfo(this);
595595
}
596596

597+
/**
598+
* Returns list of all applicable Cluster Master Hosts for the SearchHead Service.
599+
*
600+
* @return List of Cluster Master Host(s).
601+
*/
602+
public List<String> getClusterMasters(){
603+
Entity caps = new Entity(this, "cluster/config");
604+
List<String> hosts = new ArrayList<String>();
605+
try {
606+
String clusterMasterURIs = caps.getString("master_uri");
607+
URL clusterMasterUrl;
608+
//for multi-cluster environment, there might be more than cluster master for the searchHead
609+
if(clusterMasterURIs.contains(",")){
610+
String[] masterURIs = clusterMasterURIs.split(",");
611+
for(String uri : masterURIs){
612+
clusterMasterUrl = new URL(uri);
613+
hosts.add(clusterMasterUrl.getHost());
614+
}
615+
}else {
616+
clusterMasterUrl = new URL(clusterMasterURIs);
617+
hosts.add(clusterMasterUrl.getHost());
618+
}
619+
return hosts;
620+
} catch (MalformedURLException e) {
621+
e.printStackTrace();
622+
}
623+
return hosts;
624+
}
625+
597626
/**
598627
* Returns a collection of configured inputs.
599628
*

0 commit comments

Comments
 (0)