Search in sources :

Example 1 with SniSslEngineFactory

use of com.datastax.oss.driver.internal.core.ssl.SniSslEngineFactory in project java-driver by datastax.

the class CloudConfigFactory method createCloudConfig.

/**
 * Creates a {@link CloudConfig} with information fetched from the specified {@link InputStream}.
 *
 * <p>The stream must contain a valid secure connect bundle archive in ZIP format. Note that the
 * stream will be closed after a call to that method and cannot be used anymore.
 *
 * @param cloudConfig the stream to read the Cloud configuration from; cannot be null.
 * @throws IOException If the Cloud configuration cannot be read.
 * @throws GeneralSecurityException If the Cloud SSL context cannot be created.
 */
@NonNull
public CloudConfig createCloudConfig(@NonNull InputStream cloudConfig) throws IOException, GeneralSecurityException {
    Objects.requireNonNull(cloudConfig, "cloudConfig cannot be null");
    JsonNode configJson = null;
    ByteArrayOutputStream keyStoreOutputStream = null;
    ByteArrayOutputStream trustStoreOutputStream = null;
    ObjectMapper mapper = new ObjectMapper().configure(JsonParser.Feature.AUTO_CLOSE_SOURCE, false);
    try (ZipInputStream zipInputStream = new ZipInputStream(cloudConfig)) {
        ZipEntry entry;
        while ((entry = zipInputStream.getNextEntry()) != null) {
            String fileName = entry.getName();
            switch(fileName) {
                case "config.json":
                    configJson = mapper.readTree(zipInputStream);
                    break;
                case "identity.jks":
                    keyStoreOutputStream = new ByteArrayOutputStream();
                    ByteStreams.copy(zipInputStream, keyStoreOutputStream);
                    break;
                case "trustStore.jks":
                    trustStoreOutputStream = new ByteArrayOutputStream();
                    ByteStreams.copy(zipInputStream, trustStoreOutputStream);
                    break;
            }
        }
    }
    if (configJson == null) {
        throw new IllegalStateException("Invalid bundle: missing file config.json");
    }
    if (keyStoreOutputStream == null) {
        throw new IllegalStateException("Invalid bundle: missing file identity.jks");
    }
    if (trustStoreOutputStream == null) {
        throw new IllegalStateException("Invalid bundle: missing file trustStore.jks");
    }
    char[] keyStorePassword = getKeyStorePassword(configJson);
    char[] trustStorePassword = getTrustStorePassword(configJson);
    ByteArrayInputStream keyStoreInputStream = new ByteArrayInputStream(keyStoreOutputStream.toByteArray());
    ByteArrayInputStream trustStoreInputStream = new ByteArrayInputStream(trustStoreOutputStream.toByteArray());
    SSLContext sslContext = createSslContext(keyStoreInputStream, keyStorePassword, trustStoreInputStream, trustStorePassword);
    URL metadataServiceUrl = getMetadataServiceUrl(configJson);
    JsonNode proxyMetadataJson;
    try (BufferedReader proxyMetadata = fetchProxyMetadata(metadataServiceUrl, sslContext)) {
        proxyMetadataJson = mapper.readTree(proxyMetadata);
    }
    InetSocketAddress sniProxyAddress = getSniProxyAddress(proxyMetadataJson);
    List<EndPoint> endPoints = getEndPoints(proxyMetadataJson, sniProxyAddress);
    String localDatacenter = getLocalDatacenter(proxyMetadataJson);
    SniSslEngineFactory sslEngineFactory = new SniSslEngineFactory(sslContext);
    validateIfBundleContainsUsernamePassword(configJson);
    return new CloudConfig(sniProxyAddress, endPoints, localDatacenter, sslEngineFactory);
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ZipEntry(java.util.zip.ZipEntry) JsonNode(com.fasterxml.jackson.databind.JsonNode) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SSLContext(javax.net.ssl.SSLContext) SniEndPoint(com.datastax.oss.driver.internal.core.metadata.SniEndPoint) EndPoint(com.datastax.oss.driver.api.core.metadata.EndPoint) URL(java.net.URL) ZipInputStream(java.util.zip.ZipInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) SniSslEngineFactory(com.datastax.oss.driver.internal.core.ssl.SniSslEngineFactory) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) NonNull(edu.umd.cs.findbugs.annotations.NonNull)

Aggregations

EndPoint (com.datastax.oss.driver.api.core.metadata.EndPoint)1 SniEndPoint (com.datastax.oss.driver.internal.core.metadata.SniEndPoint)1 SniSslEngineFactory (com.datastax.oss.driver.internal.core.ssl.SniSslEngineFactory)1 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 NonNull (edu.umd.cs.findbugs.annotations.NonNull)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 InetSocketAddress (java.net.InetSocketAddress)1 URL (java.net.URL)1 ZipEntry (java.util.zip.ZipEntry)1 ZipInputStream (java.util.zip.ZipInputStream)1 SSLContext (javax.net.ssl.SSLContext)1