Search in sources :

Example 31 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder 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)

Example 32 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project jnosql-diana-driver by eclipse.

the class ElasticsearchDocumentConfiguration method get.

@Override
public ElasticsearchDocumentCollectionManagerFactory get(Settings settings) throws NullPointerException {
    requireNonNull(settings, "settings is required");
    Map<String, String> configurations = new HashMap<>();
    settings.forEach((key, value) -> configurations.put(key, value.toString()));
    configurations.keySet().stream().filter(k -> k.startsWith(HOST_PREFIX)).sorted().map(h -> ElasticsearchAddress.of(configurations.get(h), DEFAULT_PORT)).map(ElasticsearchAddress::toHttpHost).forEach(httpHosts::add);
    RestClientBuilder builder = RestClient.builder(httpHosts.toArray(new HttpHost[httpHosts.size()]));
    builder.setDefaultHeaders(headers.stream().toArray(Header[]::new));
    String maxRetry = configurations.get("elasticsearch-maxRetryTimeoutMillis");
    if (maxRetry != null) {
        maxRetryTimoutMillis = Integer.valueOf(maxRetry);
    }
    builder.setMaxRetryTimeoutMillis(maxRetryTimoutMillis);
    RestHighLevelClient client = new RestHighLevelClient(builder);
    return new ElasticsearchDocumentCollectionManagerFactory(client);
}
Also used : Header(org.apache.http.Header) HashMap(java.util.HashMap) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 33 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project presto by prestodb.

the class ElasticsearchClient method createClient.

private static RestHighLevelClient createClient(ElasticsearchConfig config, Optional<AwsSecurityConfig> awsSecurityConfig) {
    RestClientBuilder builder = RestClient.builder(new HttpHost(config.getHost(), config.getPort(), config.isTlsEnabled() ? "https" : "http")).setMaxRetryTimeoutMillis((int) config.getMaxRetryTime().toMillis());
    builder.setHttpClientConfigCallback(ignored -> {
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(toIntExact(config.getConnectTimeout().toMillis())).setSocketTimeout(toIntExact(config.getRequestTimeout().toMillis())).build();
        IOReactorConfig reactorConfig = IOReactorConfig.custom().setIoThreadCount(config.getHttpThreadCount()).build();
        // the client builder passed to the call-back is configured to use system properties, which makes it
        // impossible to configure concurrency settings, so we need to build a new one from scratch
        HttpAsyncClientBuilder clientBuilder = HttpAsyncClientBuilder.create().setDefaultRequestConfig(requestConfig).setDefaultIOReactorConfig(reactorConfig).setMaxConnPerRoute(config.getMaxHttpConnections()).setMaxConnTotal(config.getMaxHttpConnections());
        if (config.isTlsEnabled()) {
            buildSslContext(config.getKeystorePath(), config.getKeystorePassword(), config.getTrustStorePath(), config.getTruststorePassword()).ifPresent(clientBuilder::setSSLContext);
            if (config.isVerifyHostnames()) {
                clientBuilder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE);
            }
        }
        awsSecurityConfig.ifPresent(securityConfig -> clientBuilder.addInterceptorLast(new AwsRequestSigner(securityConfig.getRegion(), getAwsCredentialsProvider(securityConfig))));
        return clientBuilder;
    });
    return new RestHighLevelClient(builder);
}
Also used : IOReactorConfig(org.apache.http.impl.nio.reactor.IOReactorConfig) RequestConfig(org.apache.http.client.config.RequestConfig) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder)

Example 34 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project vorto by eclipse.

the class ElasticSearchConfiguration method awsIndexingClient.

@Bean
@Profile({ "prod", "int" })
public RestHighLevelClient awsIndexingClient() {
    logger.info("Creating an elastic server client with config(serviceName=" + serviceName + " region=" + region + " aesEndpoint=" + aesEndpoint);
    AWS4Signer signer = new AWS4Signer();
    signer.setServiceName(serviceName);
    signer.setRegionName(region);
    HttpRequestInterceptor interceptor = new AWSRequestSigningApacheInterceptor(serviceName, signer, credentialsProvider);
    RestClientBuilder builder = RestClient.builder(HttpHost.create(aesEndpoint)).setHttpClientConfigCallback(httpClientConfig(getProxy(), interceptor));
    return new RestHighLevelClient(builder);
}
Also used : AWS4Signer(com.amazonaws.auth.AWS4Signer) HttpRequestInterceptor(org.apache.http.HttpRequestInterceptor) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) AWSRequestSigningApacheInterceptor(com.amazonaws.http.AWSRequestSigningApacheInterceptor) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Profile(org.springframework.context.annotation.Profile) Bean(org.springframework.context.annotation.Bean)

Example 35 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project sagacity-sqltoy by chenrenfei.

the class ElasticEndpoint method initRestClient.

/**
 * @param restClient the restClient to set
 */
public void initRestClient() {
    if (StringUtil.isBlank(this.getUrl())) {
        return;
    }
    if (restClient == null) {
        // 替换全角字符
        String[] urls = this.getUrl().replaceAll("\\;", ";").replaceAll("\\,", ",").replaceAll("\\;", ",").split("\\,");
        // 当为单一地址时使用httpclient直接调用
        if (urls.length < 2) {
            return;
        }
        List<HttpHost> hosts = new ArrayList<HttpHost>();
        for (String urlStr : urls) {
            try {
                if (StringUtil.isNotBlank(urlStr)) {
                    URL url = new java.net.URL(urlStr.trim());
                    hosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
                }
            } catch (MalformedURLException e) {
                e.printStackTrace();
            }
        }
        if (!hosts.isEmpty()) {
            HttpHost[] hostAry = new HttpHost[hosts.size()];
            hosts.toArray(hostAry);
            RestClientBuilder builder = RestClient.builder(hostAry);
            final ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Charset.forName(this.charset == null ? "UTF-8" : this.charset)).build();
            RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(this.requestTimeout).setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).build();
            final CredentialsProvider credsProvider = new BasicCredentialsProvider();
            final boolean hasCrede = (StringUtil.isNotBlank(this.getUsername()) && StringUtil.isNotBlank(getPassword())) ? true : false;
            // 是否ssl证书模式
            final boolean hasSsl = StringUtil.isNotBlank(this.keyStore);
            // 凭据提供器
            if (hasCrede) {
                credsProvider.setCredentials(AuthScope.ANY, // 认证用户名和密码
                new UsernamePasswordCredentials(getUsername(), getPassword()));
            }
            SSLContextBuilder sslBuilder = null;
            try {
                if (hasSsl) {
                    KeyStore truststore = KeyStore.getInstance(StringUtil.isBlank(keyStoreType) ? KeyStore.getDefaultType() : keyStoreType);
                    truststore.load(FileUtil.getFileInputStream(keyStore), (keyStorePass == null) ? null : keyStorePass.toCharArray());
                    sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, keyStoreSelfSign ? new TrustSelfSignedStrategy() : null);
                }
                final SSLContext sslContext = (sslBuilder == null) ? null : sslBuilder.build();
                final boolean disableAuthCaching = !authCaching;
                builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {

                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
                        httpClientBuilder.setDefaultConnectionConfig(connectionConfig).setDefaultRequestConfig(requestConfig);
                        // 禁用抢占式身份验证
                        if (disableAuthCaching) {
                            httpClientBuilder.disableAuthCaching();
                        }
                        // 用户名密码
                        if (hasCrede) {
                            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
                        }
                        // 证书
                        if (hasSsl) {
                            httpClientBuilder.setSSLContext(sslContext);
                        }
                        return httpClientBuilder;
                    }
                });
                restClient = builder.build();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ArrayList(java.util.ArrayList) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) HttpHost(org.apache.http.HttpHost) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ConnectionConfig(org.apache.http.config.ConnectionConfig) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

RestClientBuilder (org.elasticsearch.client.RestClientBuilder)34 HttpHost (org.apache.http.HttpHost)27 RestHighLevelClient (org.elasticsearch.client.RestHighLevelClient)23 RestClient (org.elasticsearch.client.RestClient)15 IOException (java.io.IOException)11 CredentialsProvider (org.apache.http.client.CredentialsProvider)11 ArrayList (java.util.ArrayList)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)7 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)7 File (java.io.File)6 KeyStore (java.security.KeyStore)6 List (java.util.List)6 Locale (java.util.Locale)6 Map (java.util.Map)5 StringUtils (org.apache.commons.lang.StringUtils)5 AuthSchemeProvider (org.apache.http.auth.AuthSchemeProvider)5 AuthSchemes (org.apache.http.client.config.AuthSchemes)5 Lookup (org.apache.http.config.Lookup)5 RegistryBuilder (org.apache.http.config.RegistryBuilder)5 SPNegoSchemeFactory (org.apache.http.impl.auth.SPNegoSchemeFactory)5