Search in sources :

Example 1 with SSLContextSettings

use of org.infinispan.cli.impl.SSLContextSettings in project infinispan by infinispan.

the class CLI method exec.

@Override
public CommandResult exec(ContextAwareCommandInvocation invocation) {
    if (help) {
        invocation.println(invocation.getHelpInfo());
        return CommandResult.SUCCESS;
    }
    if (version) {
        invocation.printf("%s CLI %s\n", org.infinispan.commons.util.Version.getBrandName(), org.infinispan.commons.util.Version.getBrandVersion());
        invocation.printf("Copyright (C) Red Hat Inc. and/or its affiliates and other contributors\n");
        invocation.printf("License Apache License, v. 2.0. http://www.apache.org/licenses/LICENSE-2.0\n");
        return CommandResult.SUCCESS;
    }
    context = invocation.getContext();
    if (propertyMap != null) {
        propertyMap.forEach(context.getProperties()::putIfAbsent);
    }
    if (properties != null) {
        try (Reader r = Files.newBufferedReader(Paths.get(properties))) {
            Properties loaded = new Properties();
            loaded.load(r);
            loaded.forEach(context.getProperties()::putIfAbsent);
        } catch (IOException e) {
            throw new IllegalArgumentException(e);
        }
    }
    String sslTrustStore = truststore != null ? truststore.getAbsolutePath() : context.getProperty(Context.Property.TRUSTSTORE);
    if (sslTrustStore != null) {
        String sslTrustStorePassword = truststorePassword != null ? truststorePassword : context.getProperty(Context.Property.TRUSTSTORE_PASSWORD);
        try (FileInputStream f = new FileInputStream(sslTrustStore)) {
            KeyStore keyStore = KeyStoreUtil.loadKeyStore(ProviderUtil.INSTALLED_PROVIDERS, null, f, sslTrustStore, sslTrustStorePassword != null ? sslTrustStorePassword.toCharArray() : null);
            TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
            trustManagerFactory.init(keyStore);
            HostnameVerifier verifier = hostnameVerifier != null ? new RegexHostnameVerifier(hostnameVerifier) : null;
            SSLContextSettings sslContext = SSLContextSettings.getInstance("TLS", null, trustManagerFactory.getTrustManagers(), null, verifier);
            context.setSslContext(sslContext);
        } catch (Exception e) {
            invocation.getShell().writeln(MSG.keyStoreError(sslTrustStore, e));
            return CommandResult.FAILURE;
        }
    } else if (trustAll || Boolean.parseBoolean(context.getProperty(Context.Property.TRUSTALL))) {
        SSLContextSettings sslContext = SSLContextSettings.getInstance("TLS", null, new TrustManager[] { new ZeroSecurityTrustManager() }, null, new ZeroSecurityHostnameVerifier());
        context.setSslContext(sslContext);
    }
    String connectionString = connect != null ? connect : context.getProperty(Context.Property.AUTOCONNECT_URL);
    if (connectionString != null) {
        context.connect(null, connectionString);
    }
    if (file != null) {
        return batch(file.getAbsolutePath(), invocation.getShell());
    } else {
        if (context.getProperty(Context.Property.AUTOEXEC) != null) {
            batch(context.getProperty(Context.Property.AUTOEXEC), invocation.getShell());
        }
        return interactive(invocation.getShell());
    }
}
Also used : Reader(java.io.Reader) RegexHostnameVerifier(org.infinispan.cli.connection.RegexHostnameVerifier) IOException(java.io.IOException) ZeroSecurityTrustManager(org.infinispan.cli.util.ZeroSecurityTrustManager) Properties(java.util.Properties) KeyStore(java.security.KeyStore) FileInputStream(java.io.FileInputStream) CommandRegistryException(org.aesh.command.registry.CommandRegistryException) IOException(java.io.IOException) HostnameVerifier(javax.net.ssl.HostnameVerifier) RegexHostnameVerifier(org.infinispan.cli.connection.RegexHostnameVerifier) ZeroSecurityHostnameVerifier(org.infinispan.cli.util.ZeroSecurityHostnameVerifier) TrustManager(javax.net.ssl.TrustManager) ZeroSecurityTrustManager(org.infinispan.cli.util.ZeroSecurityTrustManager) SSLContextSettings(org.infinispan.cli.impl.SSLContextSettings) ZeroSecurityHostnameVerifier(org.infinispan.cli.util.ZeroSecurityHostnameVerifier) TrustManagerFactory(javax.net.ssl.TrustManagerFactory)

Aggregations

FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1 Reader (java.io.Reader)1 KeyStore (java.security.KeyStore)1 Properties (java.util.Properties)1 HostnameVerifier (javax.net.ssl.HostnameVerifier)1 TrustManager (javax.net.ssl.TrustManager)1 TrustManagerFactory (javax.net.ssl.TrustManagerFactory)1 CommandRegistryException (org.aesh.command.registry.CommandRegistryException)1 RegexHostnameVerifier (org.infinispan.cli.connection.RegexHostnameVerifier)1 SSLContextSettings (org.infinispan.cli.impl.SSLContextSettings)1 ZeroSecurityHostnameVerifier (org.infinispan.cli.util.ZeroSecurityHostnameVerifier)1 ZeroSecurityTrustManager (org.infinispan.cli.util.ZeroSecurityTrustManager)1