Search in sources :

Example 1 with SavingX509TrustManager

use of com.sequenceiq.cloudbreak.client.CertificateTrustManager.SavingX509TrustManager in project cloudbreak by hortonworks.

the class TlsSetupService method setupTls.

public void setupTls(Stack stack, InstanceMetaData gwInstance) throws CloudbreakException {
    try {
        SavingX509TrustManager x509TrustManager = new SavingX509TrustManager();
        TrustManager[] trustManagers = { x509TrustManager };
        SSLContext sslContext = SslConfigurator.newInstance().createSSLContext();
        sslContext.init(null, trustManagers, new SecureRandom());
        Client client = RestClientUtil.createClient(sslContext, false, null);
        Integer gatewayPort = stack.getGatewayPort();
        String ip = gatewayConfigService.getGatewayIp(stack, gwInstance);
        LOGGER.info("Trying to fetch the server's certificate: {}:{}", ip, gatewayPort);
        nginxPollerService.pollWithTimeoutSingleFailure(nginxCertListenerTask, new NginxPollerObject(stack, client, ip, gatewayPort, x509TrustManager), POLLING_INTERVAL, MAX_ATTEMPTS_FOR_HOSTS);
        WebTarget nginxTarget = client.target(String.format("https://%s:%d", ip, gatewayPort));
        nginxTarget.path("/").request().get();
        X509Certificate[] chain = x509TrustManager.getChain();
        String serverCert = PkiUtil.convert(chain[0]);
        InstanceMetaData metaData = instanceMetaDataRepository.findOne(gwInstance.getId());
        metaData.setServerCert(BaseEncoding.base64().encode(serverCert.getBytes()));
        instanceMetaDataRepository.save(metaData);
    } catch (Exception e) {
        throw new CloudbreakException("Failed to retrieve the server's certificate", e);
    }
}
Also used : SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) TrustManager(javax.net.ssl.TrustManager) SavingX509TrustManager(com.sequenceiq.cloudbreak.client.CertificateTrustManager.SavingX509TrustManager) InstanceMetaData(com.sequenceiq.cloudbreak.domain.InstanceMetaData) SavingX509TrustManager(com.sequenceiq.cloudbreak.client.CertificateTrustManager.SavingX509TrustManager) CloudbreakException(com.sequenceiq.cloudbreak.service.CloudbreakException) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Aggregations

SavingX509TrustManager (com.sequenceiq.cloudbreak.client.CertificateTrustManager.SavingX509TrustManager)1 InstanceMetaData (com.sequenceiq.cloudbreak.domain.InstanceMetaData)1 CloudbreakException (com.sequenceiq.cloudbreak.service.CloudbreakException)1 SecureRandom (java.security.SecureRandom)1 X509Certificate (java.security.cert.X509Certificate)1 SSLContext (javax.net.ssl.SSLContext)1 TrustManager (javax.net.ssl.TrustManager)1 Client (javax.ws.rs.client.Client)1 WebTarget (javax.ws.rs.client.WebTarget)1