Search in sources :

Example 6 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project gora by apache.

the class ElasticsearchStore method createClient.

public static RestHighLevelClient createClient(ElasticsearchParameters parameters) {
    RestClientBuilder clientBuilder = RestClient.builder(new HttpHost(parameters.getHost(), parameters.getPort()));
    // Choosing the authentication method.
    switch(parameters.getAuthenticationType()) {
        case BASIC:
            if (parameters.getUsername() != null && parameters.getPassword() != null) {
                final CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
                credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(parameters.getUsername(), parameters.getPassword()));
                clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider));
            } else {
                throw new IllegalArgumentException("Missing username or password for BASIC authentication.");
            }
            break;
        case TOKEN:
            if (parameters.getAuthorizationToken() != null) {
                Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", parameters.getAuthorizationToken()) };
                clientBuilder.setDefaultHeaders(defaultHeaders);
            } else {
                throw new IllegalArgumentException("Missing authorization token for TOKEN authentication.");
            }
            break;
        case APIKEY:
            if (parameters.getApiKeyId() != null && parameters.getApiKeySecret() != null) {
                String apiKeyAuth = Base64.getEncoder().encodeToString((parameters.getApiKeyId() + ":" + parameters.getApiKeySecret()).getBytes(StandardCharsets.UTF_8));
                Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "ApiKey " + apiKeyAuth) };
                clientBuilder.setDefaultHeaders(defaultHeaders);
            } else {
                throw new IllegalArgumentException("Missing API Key ID or API Key Secret for APIKEY authentication.");
            }
            break;
    }
    if (parameters.getConnectTimeout() != 0) {
        clientBuilder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setConnectTimeout(parameters.getConnectTimeout()));
    }
    if (parameters.getSocketTimeout() != 0) {
        clientBuilder.setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder.setSocketTimeout(parameters.getSocketTimeout()));
    }
    if (parameters.getIoThreadCount() != 0) {
        clientBuilder.setHttpClientConfigCallback(httpClientBuilder -> httpClientBuilder.setDefaultIOReactorConfig(IOReactorConfig.custom().setIoThreadCount(parameters.getIoThreadCount()).build()));
    }
    return new RestHighLevelClient(clientBuilder);
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 7 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project sonarqube by SonarSource.

the class EsConnectorImpl method buildRestHighLevelClient.

private RestHighLevelClient buildRestHighLevelClient() {
    HttpHost[] httpHosts = hostAndPorts.stream().map(hostAndPort -> new HttpHost(hostAndPort.getHost(), hostAndPort.getPortOrDefault(9001))).toArray(HttpHost[]::new);
    if (LOG.isDebugEnabled()) {
        String addresses = Arrays.stream(httpHosts).map(t -> t.getHostName() + ":" + t.getPort()).collect(Collectors.joining(", "));
        LOG.debug("Connected to Elasticsearch node: [{}]", addresses);
    }
    RestClientBuilder builder = RestClient.builder(httpHosts).setHttpClientConfigCallback(httpClientBuilder -> {
        if (searchPassword != null) {
            BasicCredentialsProvider provider = getBasicCredentialsProvider(searchPassword);
            httpClientBuilder.setDefaultCredentialsProvider(provider);
        }
        return httpClientBuilder;
    });
    return new RestHighLevelClient(builder);
}
Also used : RestClient(org.elasticsearch.client.RestClient) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) IOException(java.io.IOException) ClusterHealthResponse(org.elasticsearch.action.admin.cluster.health.ClusterHealthResponse) TimeValue.timeValueSeconds(org.elasticsearch.core.TimeValue.timeValueSeconds) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) HostAndPort(com.google.common.net.HostAndPort) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ClusterHealthRequest(org.elasticsearch.action.admin.cluster.health.ClusterHealthRequest) AuthScope(org.apache.http.auth.AuthScope) RequestOptions(org.elasticsearch.client.RequestOptions) Optional(java.util.Optional) ClusterHealthStatus(org.elasticsearch.cluster.health.ClusterHealthStatus) HttpHost(org.apache.http.HttpHost) Nullable(javax.annotation.Nullable) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

Example 8 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project ranger by apache.

the class ElasticSearchIndexBootStrapper method getRestClientBuilder.

public static RestClientBuilder getRestClientBuilder(String urls, String protocol, String user, String password, int port) {
    RestClientBuilder restClientBuilder = RestClient.builder(EmbeddedServerUtil.toArray(urls, ",").stream().map(x -> new HttpHost(x, port, protocol)).<HttpHost>toArray(i -> new HttpHost[i]));
    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && !user.equalsIgnoreCase("NONE") && !password.equalsIgnoreCase("NONE")) {
        if (password.contains("keytab") && new File(password).exists()) {
            final KerberosCredentialsProvider credentialsProvider = CredentialsProviderUtil.getKerberosCredentials(user, password);
            Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).build();
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> {
                clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
                return clientBuilder;
            });
        } else {
            final CredentialsProvider credentialsProvider = CredentialsProviderUtil.getBasicCredentials(user, password);
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        }
    } else {
        LOG.severe("ElasticSearch Credentials not provided!!");
        final CredentialsProvider credentialsProvider = null;
        restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    }
    return restClientBuilder;
}
Also used : RestClient(org.elasticsearch.client.RestClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) StringUtils(org.apache.commons.lang.StringUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) XContentType(org.elasticsearch.common.xcontent.XContentType) AuthSchemes(org.apache.http.client.config.AuthSchemes) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) Settings(org.elasticsearch.common.settings.Settings) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) Locale(java.util.Locale) CredentialReader(org.apache.ranger.credentialapi.CredentialReader) TimeValue(org.elasticsearch.common.unit.TimeValue) Lookup(org.apache.http.config.Lookup) RequestOptions(org.elasticsearch.client.RequestOptions) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) Path(java.nio.file.Path) CreateIndexRequest(org.elasticsearch.client.indices.CreateIndexRequest) Files(java.nio.file.Files) IOException(java.io.IOException) KeyStore(java.security.KeyStore) Logger(java.util.logging.Logger) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) Paths(java.nio.file.Paths) CredentialsProviderUtil(org.apache.ranger.authorization.credutils.CredentialsProviderUtil) CreateIndexResponse(org.elasticsearch.client.indices.CreateIndexResponse) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) File(java.io.File)

Example 9 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project ranger by apache.

the class ElasticSearchAuditDestination method newClient.

private RestHighLevelClient newClient() {
    try {
        if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && password.contains("keytab") && new File(password).exists()) {
            subject = CredentialsProviderUtil.login(user, password);
        }
        RestClientBuilder restClientBuilder = getRestClientBuilder(hosts, protocol, user, password, port);
        RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder);
        if (LOG.isDebugEnabled()) {
            LOG.debug("Initialized client");
        }
        boolean exits = false;
        try {
            exits = restHighLevelClient.indices().open(new OpenIndexRequest(this.index), RequestOptions.DEFAULT).isShardsAcknowledged();
        } catch (Exception e) {
            LOG.warn("Error validating index " + this.index);
        }
        if (exits) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Index exists");
            }
        } else {
            LOG.info("Index does not exist");
        }
        return restHighLevelClient;
    } catch (Throwable t) {
        lastLoggedAt.updateAndGet(lastLoggedAt -> {
            long now = System.currentTimeMillis();
            long elapsed = now - lastLoggedAt;
            if (elapsed > TimeUnit.MINUTES.toMillis(1)) {
                LOG.error("Can't connect to ElasticSearch server: " + connectionString(), t);
                return now;
            } else {
                return lastLoggedAt;
            }
        });
        return null;
    }
}
Also used : RestClient(org.elasticsearch.client.RestClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AuthSchemes(org.apache.http.client.config.AuthSchemes) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) ArrayList(java.util.ArrayList) IndexRequest(org.elasticsearch.action.index.IndexRequest) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) Locale(java.util.Locale) Map(java.util.Map) Lookup(org.apache.http.config.Lookup) RequestOptions(org.elasticsearch.client.RequestOptions) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) AuthzAuditEvent(org.apache.ranger.audit.model.AuthzAuditEvent) PrivilegedActionException(java.security.PrivilegedActionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File) Subject(javax.security.auth.Subject) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) MiscUtil(org.apache.ranger.audit.provider.MiscUtil) CredentialsProviderUtil(org.apache.ranger.authorization.credutils.CredentialsProviderUtil) AuditEventBase(org.apache.ranger.audit.model.AuditEventBase) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File) PrivilegedActionException(java.security.PrivilegedActionException)

Example 10 with RestClientBuilder

use of org.elasticsearch.client.RestClientBuilder in project ranger by apache.

the class ElasticSearchAuditDestination method getRestClientBuilder.

public static RestClientBuilder getRestClientBuilder(String urls, String protocol, String user, String password, int port) {
    RestClientBuilder restClientBuilder = RestClient.builder(MiscUtil.toArray(urls, ",").stream().map(x -> new HttpHost(x, port, protocol)).<HttpHost>toArray(i -> new HttpHost[i]));
    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && !user.equalsIgnoreCase("NONE") && !password.equalsIgnoreCase("NONE")) {
        if (password.contains("keytab") && new File(password).exists()) {
            final KerberosCredentialsProvider credentialsProvider = CredentialsProviderUtil.getKerberosCredentials(user, password);
            Lookup<AuthSchemeProvider> authSchemeRegistry = RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO, new SPNegoSchemeFactory()).build();
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> {
                clientBuilder.setDefaultCredentialsProvider(credentialsProvider);
                clientBuilder.setDefaultAuthSchemeRegistry(authSchemeRegistry);
                return clientBuilder;
            });
        } else {
            final CredentialsProvider credentialsProvider = CredentialsProviderUtil.getBasicCredentials(user, password);
            restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
        }
    } else {
        LOG.error("ElasticSearch Credentials not provided!!");
        final CredentialsProvider credentialsProvider = null;
        restClientBuilder.setHttpClientConfigCallback(clientBuilder -> clientBuilder.setDefaultCredentialsProvider(credentialsProvider));
    }
    return restClientBuilder;
}
Also used : RestClient(org.elasticsearch.client.RestClient) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) RegistryBuilder(org.apache.http.config.RegistryBuilder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) AuthSchemes(org.apache.http.client.config.AuthSchemes) OpenIndexRequest(org.elasticsearch.action.admin.indices.open.OpenIndexRequest) ArrayList(java.util.ArrayList) IndexRequest(org.elasticsearch.action.index.IndexRequest) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) Locale(java.util.Locale) Map(java.util.Map) Lookup(org.apache.http.config.Lookup) RequestOptions(org.elasticsearch.client.RequestOptions) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) AuthzAuditEvent(org.apache.ranger.audit.model.AuthzAuditEvent) PrivilegedActionException(java.security.PrivilegedActionException) Properties(java.util.Properties) Logger(org.slf4j.Logger) BulkItemResponse(org.elasticsearch.action.bulk.BulkItemResponse) Collection(java.util.Collection) BulkResponse(org.elasticsearch.action.bulk.BulkResponse) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File) Subject(javax.security.auth.Subject) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) MiscUtil(org.apache.ranger.audit.provider.MiscUtil) CredentialsProviderUtil(org.apache.ranger.authorization.credutils.CredentialsProviderUtil) AuditEventBase(org.apache.ranger.audit.model.AuditEventBase) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpHost(org.apache.http.HttpHost) BulkRequest(org.elasticsearch.action.bulk.BulkRequest) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) HttpHost(org.apache.http.HttpHost) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) AuthSchemeProvider(org.apache.http.auth.AuthSchemeProvider) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) File(java.io.File)

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 List (java.util.List)6 Locale (java.util.Locale)6 KeyStore (java.security.KeyStore)5 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