Search in sources :

Example 1 with HttpAuthTypes

use of org.janusgraph.diskstorage.es.rest.util.HttpAuthTypes in project janusgraph by JanusGraph.

the class RestClientSetup method getHttpClientConfigCallback.

/**
 * <p>
 * Returns the callback for customizing {@link CloseableHttpAsyncClient} or null if no
 * customization is needed.
 * </p>
 * <p>
 * See {@link RestClientBuilder#setHttpClientConfigCallback(HttpClientConfigCallback)} for more details.
 * </p>
 *
 * @param config
 *            ES index configuration
 * @return callback or null if the client customization is not needed
 */
protected HttpClientConfigCallback getHttpClientConfigCallback(Configuration config) {
    final List<HttpClientConfigCallback> callbackList = new LinkedList<>();
    final HttpAuthTypes authType = ConfigOption.getEnumValue(config.get(ElasticSearchIndex.ES_HTTP_AUTH_TYPE), HttpAuthTypes.class);
    log.debug("Configuring HTTP(S) authentication type {}", authType);
    switch(authType) {
        case BASIC:
            callbackList.add(new BasicAuthHttpClientConfigCallback(config.has(ElasticSearchIndex.ES_HTTP_AUTH_REALM) ? config.get(ElasticSearchIndex.ES_HTTP_AUTH_REALM) : "", config.get(ElasticSearchIndex.ES_HTTP_AUTH_USERNAME), config.get(ElasticSearchIndex.ES_HTTP_AUTH_PASSWORD)));
            break;
        case CUSTOM:
            callbackList.add(getCustomAuthenticator(config.get(ElasticSearchIndex.ES_HTTP_AUTHENTICATOR_CLASS), config.get(ElasticSearchIndex.ES_HTTP_AUTHENTICATOR_ARGS)));
            break;
        case NONE:
            break;
        default:
            // not expected
            throw new IllegalArgumentException("Authentication type \"" + authType + "\" is not implemented");
    }
    if (config.has(ElasticSearchIndex.CLIENT_KEEP_ALIVE)) {
        callbackList.add(new ConnectionKeepAliveConfigCallback(config.get(ElasticSearchIndex.CLIENT_KEEP_ALIVE)));
    }
    if (config.get(ElasticSearchIndex.SSL_ENABLED)) {
        // Custom SSL configuration
        final Builder sslConfCBBuilder = getSSLConfigurationCallbackBuilder();
        boolean configureSSL = false;
        if (config.has(ElasticSearchIndex.SSL_TRUSTSTORE_LOCATION)) {
            sslConfCBBuilder.withTrustStore(config.get(ElasticSearchIndex.SSL_TRUSTSTORE_LOCATION), config.get(ElasticSearchIndex.SSL_TRUSTSTORE_PASSWORD));
            configureSSL = true;
        }
        if (config.has(ElasticSearchIndex.SSL_KEYSTORE_LOCATION)) {
            final String keystorePassword = config.get(ElasticSearchIndex.SSL_KEYSTORE_PASSWORD);
            sslConfCBBuilder.withKeyStore(config.get(ElasticSearchIndex.SSL_KEYSTORE_LOCATION), keystorePassword, config.has(ElasticSearchIndex.SSL_KEY_PASSWORD) ? config.get(ElasticSearchIndex.SSL_KEY_PASSWORD) : keystorePassword);
            configureSSL = true;
        }
        if (config.has(ElasticSearchIndex.SSL_DISABLE_HOSTNAME_VERIFICATION) && config.get(ElasticSearchIndex.SSL_DISABLE_HOSTNAME_VERIFICATION)) {
            log.warn("SSL hostname verification is disabled, Elasticsearch HTTPS connections may not be secure");
            sslConfCBBuilder.disableHostNameVerification();
            configureSSL = true;
        }
        if (config.has(ElasticSearchIndex.SSL_ALLOW_SELF_SIGNED_CERTIFICATES) && config.get(ElasticSearchIndex.SSL_ALLOW_SELF_SIGNED_CERTIFICATES)) {
            log.warn("Self-signed SSL certificate support is enabled, Elasticsearch HTTPS connections may not be secure");
            sslConfCBBuilder.allowSelfSignedCertificates();
            configureSSL = true;
        }
        if (configureSSL) {
            callbackList.add(sslConfCBBuilder.build());
        }
    }
    if (callbackList.isEmpty()) {
        return null;
    }
    // will execute the chain of individual callbacks
    return httpClientBuilder -> {
        for (HttpClientConfigCallback cb : callbackList) {
            cb.customizeHttpClient(httpClientBuilder);
        }
        return httpClientBuilder;
    };
}
Also used : RestClient(org.elasticsearch.client.RestClient) Builder(org.janusgraph.diskstorage.es.rest.util.SSLConfigurationCallback.Builder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) INDEX_HOSTS(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_HOSTS) RequestConfigCallback(org.elasticsearch.client.RestClientBuilder.RequestConfigCallback) ElasticSearchClient(org.janusgraph.diskstorage.es.ElasticSearchClient) LoggerFactory(org.slf4j.LoggerFactory) ConfigOption(org.janusgraph.diskstorage.configuration.ConfigOption) RequestConfig(org.apache.http.client.config.RequestConfig) StringUtils(org.apache.commons.lang3.StringUtils) Constructor(java.lang.reflect.Constructor) BasicAuthHttpClientConfigCallback(org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback) ArrayList(java.util.ArrayList) RestClientAuthenticator(org.janusgraph.diskstorage.es.rest.util.RestClientAuthenticator) LinkedList(java.util.LinkedList) SSLConfigurationCallback(org.janusgraph.diskstorage.es.rest.util.SSLConfigurationCallback) ConnectionKeepAliveConfigCallback(org.janusgraph.diskstorage.es.rest.util.ConnectionKeepAliveConfigCallback) Logger(org.slf4j.Logger) INDEX_PORT(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_PORT) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback) Configuration(org.janusgraph.diskstorage.configuration.Configuration) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) IOException(java.io.IOException) ElasticSearchIndex(org.janusgraph.diskstorage.es.ElasticSearchIndex) List(java.util.List) Preconditions(com.google.common.base.Preconditions) HttpAuthTypes(org.janusgraph.diskstorage.es.rest.util.HttpAuthTypes) HttpHost(org.apache.http.HttpHost) HttpAuthTypes(org.janusgraph.diskstorage.es.rest.util.HttpAuthTypes) BasicAuthHttpClientConfigCallback(org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback) BasicAuthHttpClientConfigCallback(org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback) ConnectionKeepAliveConfigCallback(org.janusgraph.diskstorage.es.rest.util.ConnectionKeepAliveConfigCallback) Builder(org.janusgraph.diskstorage.es.rest.util.SSLConfigurationCallback.Builder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) LinkedList(java.util.LinkedList)

Aggregations

Preconditions (com.google.common.base.Preconditions)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 StringUtils (org.apache.commons.lang3.StringUtils)1 HttpHost (org.apache.http.HttpHost)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 CloseableHttpAsyncClient (org.apache.http.impl.nio.client.CloseableHttpAsyncClient)1 RestClient (org.elasticsearch.client.RestClient)1 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)1 HttpClientConfigCallback (org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback)1 RequestConfigCallback (org.elasticsearch.client.RestClientBuilder.RequestConfigCallback)1 ConfigOption (org.janusgraph.diskstorage.configuration.ConfigOption)1 Configuration (org.janusgraph.diskstorage.configuration.Configuration)1 ElasticSearchClient (org.janusgraph.diskstorage.es.ElasticSearchClient)1 ElasticSearchIndex (org.janusgraph.diskstorage.es.ElasticSearchIndex)1 BasicAuthHttpClientConfigCallback (org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback)1 ConnectionKeepAliveConfigCallback (org.janusgraph.diskstorage.es.rest.util.ConnectionKeepAliveConfigCallback)1