Example usage for org.apache.commons.net.util KeyManagerUtils createClientKeyManager

List of usage examples for org.apache.commons.net.util KeyManagerUtils createClientKeyManager

Introduction

In this page you can find the example usage for org.apache.commons.net.util KeyManagerUtils createClientKeyManager.

Prototype

public static KeyManager createClientKeyManager(File storePath, String storePass)
        throws IOException, GeneralSecurityException 

Source Link

Document

Create a client key manager which returns a particular key.

Usage

From source file:net.di2e.ecdr.security.ssl.client.cxf.CxfSSLClientConfigurationImpl.java

@Override
public void configurationUpdateCallback(Map<String, String> updatedConfiguration) {
    if (updatedConfiguration != null) {
        String keystore = updatedConfiguration.get(ConfigurationManager.KEY_STORE);
        String keystorePassword = updatedConfiguration.get(ConfigurationManager.KEY_STORE_PASSWORD);

        KeyManager[] keyManagers = null;
        if (StringUtils.isNotBlank(keystore) && keystorePassword != null) {
            try {
                KeyManager manager = KeyManagerUtils.createClientKeyManager(new File(keystore),
                        keystorePassword);
                keyManagers = new KeyManager[1];
                keyManagers[0] = manager;

            } catch (IOException | GeneralSecurityException ex) {
                LOGGER.debug("Could not access keystore {}, using default java keystore.", keystore);
            }//from  w  w  w .  ja  v a2  s  .  c o  m
        }

        String trustStoreLocation = updatedConfiguration.get(ConfigurationManager.TRUST_STORE);
        String trustStorePassword = updatedConfiguration.get(ConfigurationManager.TRUST_STORE_PASSWORD);
        TrustManager[] trustManagers = null;
        if (StringUtils.isNotBlank(trustStoreLocation) && trustStorePassword != null) {
            try (FileInputStream fis = new FileInputStream(trustStoreLocation)) {
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                try {
                    trustStore.load(fis,
                            StringUtils.isNotEmpty(trustStorePassword) ? trustStorePassword.toCharArray()
                                    : null);
                    trustManagers = new TrustManager[1];
                    trustManagers[0] = TrustManagerUtils.getDefaultTrustManager(trustStore);
                } catch (IOException ioe) {
                    LOGGER.debug("Could not load truststore {}, using default java truststore");
                }
            } catch (IOException | GeneralSecurityException ex) {
                LOGGER.debug("Could not access truststore {}, using default java truststore.",
                        trustStoreLocation);
            }
        }
        synchronized (tlsClientParameters) {
            LOGGER.debug(
                    "Setting the CXF KeyManager and TrustManager based on the Platform Global Configuration values");
            tlsClientParameters.setKeyManagers(keyManagers);
            tlsClientParameters.setTrustManagers(trustManagers);
        }
    }
}

From source file:ddf.test.itests.catalog.TestFtp.java

private FTPSClient createSecureClient(boolean setKeystore) throws Exception {
    FTPSClient ftps = new FTPSClient();

    if (setKeystore) {
        KeyManager keyManager = KeyManagerUtils.createClientKeyManager(
                new File(System.getProperty("javax.net.ssl.keyStore")),
                System.getProperty("javax.net.ssl.keyStorePassword"));
        ftps.setKeyManager(keyManager);/*from w  w  w.  jav  a2 s.  c o m*/
    }

    int attempts = 0;
    while (true) {
        try {
            ftps.connect(FTP_SERVER, Integer.parseInt(FTP_PORT.getPort()));
            break;
        } catch (SocketException e) {
            // a socket exception can be thrown if the ftp server is still in the process of coming up
            // or down
            Thread.sleep(1000);
            if (attempts++ > 30) {
                throw e;
            }
        }
    }

    showServerReply(ftps);
    int connectionReply = ftps.getReplyCode();
    if (!FTPReply.isPositiveCompletion(connectionReply)) {
        fail("FTP server refused connection: " + connectionReply);
    }

    boolean success = ftps.login(USERNAME, PASSWORD);
    showServerReply(ftps);
    if (!success) {
        fail("Could not log in to the FTP server.");
    }

    ftps.enterLocalPassiveMode();
    ftps.setControlKeepAliveTimeout(300);
    ftps.setFileType(FTP.BINARY_FILE_TYPE);

    return ftps;
}

From source file:net.di2e.ecdr.source.rest.AbstractCDRSource.java

protected TLSClientParameters getTlsClientParameters() {
    TLSClientParameters tlsClientParameters = new TLSClientParameters();
    tlsClientParameters.setDisableCNCheck(disableCNCheck);
    String keystore = System.getProperty(SSL_KEYSTORE_JAVA_PROPERTY);
    String keystorePassword = System.getProperty(SSL_KEYSTORE_PASSWORD_JAVA_PROPERTY);

    KeyManager[] keyManagers = null;
    if (StringUtils.isNotBlank(keystore) && keystorePassword != null) {
        try {//from   w ww. j  a  v a2 s  . c  o m
            KeyManager manager = KeyManagerUtils.createClientKeyManager(new File(keystore), keystorePassword);
            keyManagers = new KeyManager[1];
            keyManagers[0] = manager;

        } catch (IOException | GeneralSecurityException ex) {
            LOGGER.debug("Could not access keystore {}, using default java keystore.", keystore);
        }
    }

    LOGGER.debug(
            "Setting the CXF KeyManager and TrustManager based on the Platform Global Configuration values");
    tlsClientParameters.setKeyManagers(keyManagers);
    return tlsClientParameters;

}

From source file:org.apache.falcon.resource.channel.SecureHTTPChannel.java

@Override
protected Client getClient() throws Exception {
    Properties properties = StartupProperties.get();
    String keyStoreFile = properties.getProperty("keystore.file", "conf/prism.keystore");
    String password = properties.getProperty("keystore.password", "falcon-prism-passwd");
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(/*from w  w w  .  ja va 2 s  .  com*/
            new KeyManager[] { KeyManagerUtils.createClientKeyManager(new File(keyStoreFile), password) },
            new TrustManager[] { TrustManagerUtils.getValidateServerCertificateTrustManager() },
            new SecureRandom());
    DefaultClientConfig config = new DefaultClientConfig();
    config.getProperties().put(HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
            new HTTPSProperties(new AllowAllHostnameVerifier(), sslContext));
    LOG.info("Configuring client with " + new File(keyStoreFile).getAbsolutePath());
    return Client.create(config);
}

From source file:org.teiid.resource.adapter.ftp.FtpManagedConnectionFactory.java

public void setKeyPath(String keyPath) {
    this.keyPath = keyPath;
    if (this.keyPath != null && Files.exists(Paths.get(this.keyPath))) {
        if (this.keyPassword == null) {
            this.keyPassword = ""; //$NON-NLS-1$
        }/*from   w w  w . ja  v  a  2  s . com*/
        try {
            this.keyManager = KeyManagerUtils.createClientKeyManager(Paths.get(this.keyPath).toFile(),
                    this.keyPassword);
        } catch (IOException | GeneralSecurityException e) {
            throw new TeiidRuntimeException(UTIL.getString("ftp_ketstore_path", this.keyPath, e)); //$NON-NLS-1$
        }
    }
}