Cant connect to splunk using splunk-java sdk with any of TLS Versions

343 Views Asked by At

I have followed these examples to connect splunk using java-sdk. But I am unable to connect using any of TLS versions shown in this example. Disabling the security (one of the option provided to connect) is not a viable option in my case. I really appreciate help with this if someone succeeded using these options. Java versions I have tried so far are openjdk 8,11 &17.

 ServiceArgs loginArgs = new ServiceArgs();
    loginArgs.setUsername("username");
    loginArgs.setPassword("password");
    loginArgs.setScheme("https");
    loginArgs.setHost("network-splunk-hostname");
    loginArgs.setPort(443);

    // Create a Service instance and log in with the argument map
    //Service service = Service.connect(loginArgs);

    try {
        Service.setSslSecurityProtocol(SSLSecurityProtocol.SSLv3);
        Service serviceSSLv3 = Service.connect(loginArgs);
        serviceSSLv3.login();
        System.out.println("\t Success!");
    } catch (RuntimeException e) {
        System.out.println("\t Failure! ");
    }

    // TLSv1 is available by default in every modern version of Java
    System.out.println("Now trying to connect to Splunk using TLSv1");
    try {
        Service.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1);
        Service serviceTLSv1 = Service.connect(loginArgs);
        serviceTLSv1.login();
        System.out.println("\t Success!");
    } catch (RuntimeException e) {
        System.out.println("\t Failure! ");
    }


    // TLSv1.1 is available by default in Java 7 and up
    System.out.println("Now trying to connect to Splunk using TLSv1.1");
    try {
        Service.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_1);
        Service serviceTLSv1_1 = Service.connect(loginArgs);
        serviceTLSv1_1.login();
        System.out.println("\t Success!");
    } catch (RuntimeException e) {
        System.out.println("\t Failure! ");
    }

    // TLSv1.2 is available by default in Java 7 and up
    System.out.println("Now trying to connect to Splunk using TLSv1.2");
    try {
        Service.setSslSecurityProtocol(SSLSecurityProtocol.TLSv1_2);
        Service serviceTLSv1_2 = Service.connect(loginArgs);
        serviceTLSv1_2.login();
        System.out.println("\t Success!");
    } catch (RuntimeException e) {
        System.out.println("\t Failure! ");
    }

    // You can also specify your own SSLSocketFactory, in this case any version of SSL
    System.out.println("Now trying to connect to Splunk using a custom SSL only SSLSocketFactory");
    try {
        // Create an SSLSocketFactory configured to use SSL only
        SSLContext sslContext = SSLContext.getInstance("SSL");
        TrustManager[] byPassTrustManagers = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }

                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }
                }
        };
        sslContext.init(null, byPassTrustManagers, new SecureRandom());
        SSLSocketFactory SSLOnlySSLFactory = sslContext.getSocketFactory();
        Service.setSSLSocketFactory(SSLOnlySSLFactory);

        Service serviceCustomSSLFactory = Service.connect(loginArgs);
        serviceCustomSSLFactory.login();
        System.out.println("\t Success!");
    } catch (Exception e) {
        System.out.println("\t Failure!");
    }

    // You can also specify your own SSLSocketFactory, in this case any version of TLS
    System.out.println("Now trying to connect to Splunk using a custom TLS only SSLSocketFactory");
    try {
        // Create an SSLSocketFactory configured to use TLS only
        SSLContext sslContext = SSLContext.getInstance("TLS");
        TrustManager[] byPassTrustManagers = new TrustManager[]{
                new X509TrustManager() {
                    public X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }

                    public void checkClientTrusted(X509Certificate[] chain, String authType) {
                    }

                    public void checkServerTrusted(X509Certificate[] chain, String authType) {
                    }
                }
        };
        sslContext.init(null, byPassTrustManagers, new SecureRandom());
        SSLSocketFactory TLSOnlySSLFactory = sslContext.getSocketFactory();
        Service.setSSLSocketFactory(TLSOnlySSLFactory);

        Service serviceCustomSSLFactory = Service.connect(loginArgs);
        serviceCustomSSLFactory.login();
        System.out.println("\t Success!");
    } catch (Exception e) {
        System.out.println("\t Failure!");
    }

And the output is:


Failure!

Now trying to connect to Splunk using TLSv1 Failure!

Now trying to connect to Splunk using TLSv1.1 Failure!

Now trying to connect to Splunk using TLSv1.2 Failure!

Now trying to connect to Splunk using a custom SSL only SSLSocketFactory Failure!

Now trying to connect to Splunk using a custom TLS only SSLSocketFactory Failure!


0

There are 0 best solutions below