Search in sources :

Example 21 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project metron by apache.

the class ElasticsearchClientFactory method create.

/**
 * Creates an Elasticsearch client from settings provided via the global config.
 *
 * @return new client
 */
public static ElasticsearchClient create(Map<String, Object> globalConfig) {
    ElasticsearchClientConfig esClientConfig = new ElasticsearchClientConfig(getEsSettings(globalConfig));
    HttpHost[] httpHosts = getHttpHosts(globalConfig, esClientConfig.getConnectionScheme());
    RestClientBuilder builder = RestClient.builder(httpHosts);
    builder.setRequestConfigCallback(reqConfigBuilder -> {
        // Modifies request config builder with connection and socket timeouts.
        // https://www.elastic.co/guide/en/elasticsearch/client/java-rest/5.6/_timeouts.html
        reqConfigBuilder.setConnectTimeout(esClientConfig.getConnectTimeoutMillis());
        reqConfigBuilder.setSocketTimeout(esClientConfig.getSocketTimeoutMillis());
        return reqConfigBuilder;
    });
    builder.setMaxRetryTimeoutMillis(esClientConfig.getMaxRetryTimeoutMillis());
    builder.setHttpClientConfigCallback(clientBuilder -> {
        clientBuilder.setDefaultIOReactorConfig(getIOReactorConfig(esClientConfig));
        clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider(esClientConfig));
        clientBuilder.setSSLContext(getSSLContext(esClientConfig));
        return clientBuilder;
    });
    RestClient lowLevelClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(lowLevelClient);
    return new ElasticsearchClient(lowLevelClient, client);
}
Also used : HttpHost(org.apache.http.HttpHost) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) ElasticsearchClientConfig(org.apache.metron.elasticsearch.config.ElasticsearchClientConfig)

Example 22 with RestClientBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.client.RestClientBuilder in project warn-report by saaavsaaa.

the class ElasticClient method get.

@Test
public void get() throws IOException {
    RestClientBuilder builder = RestClient.builder(new HttpHost("sl010a-analysisdb1", 9200, "https"), new HttpHost("sl010a-analysisdb2", 9200, "https"), new HttpHost("sl010a-analysisdb3", 9200, "https"));
    Header[] defaultHeaders = new Header[] { new BasicHeader("Authorization", "Basic YWRtaW46YWRtaW4=") };
    builder.setDefaultHeaders(defaultHeaders);
    RestClient restClient = builder.build();
    RestHighLevelClient client = new RestHighLevelClient(restClient);
    GetRequest getRequest = new GetRequest("test-index", "test-all", "26269");
    GetResponse getResponse = client.get(getRequest);
    System.out.println(getResponse.getSourceAsString());
}
Also used : Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) HttpHost(org.apache.http.HttpHost) GetRequest(org.elasticsearch.action.get.GetRequest) RestClient(org.elasticsearch.client.RestClient) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) GetResponse(org.elasticsearch.action.get.GetResponse) BasicHeader(org.apache.http.message.BasicHeader) Test(org.junit.Test)

Example 23 with RestClientBuilder

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

the class ElasticSearchMgr method connect.

synchronized void connect() {
    if (client == null) {
        synchronized (ElasticSearchAuditDestination.class) {
            if (client == null) {
                String urls = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_URLS);
                String protocol = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PROTOCOL, "http");
                user = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_USER, "");
                password = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PWRD, "");
                int port = Integer.parseInt(PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_PORT));
                this.index = PropertiesUtil.getProperty(CONFIG_PREFIX + "." + CONFIG_INDEX, "ranger_audits");
                String parameterString = String.format(Locale.ROOT, "User:%s, %s://%s:%s/%s", user, protocol, urls, port, index);
                logger.info("Initializing ElasticSearch " + parameterString);
                if (urls != null) {
                    urls = urls.trim();
                }
                if (StringUtils.isBlank(urls) || "NONE".equalsIgnoreCase(urls.trim())) {
                    logger.info(String.format("Clearing URI config value: %s", urls));
                    urls = null;
                }
                try {
                    if (StringUtils.isNotBlank(user) && StringUtils.isNotBlank(password) && password.contains("keytab") && new File(password).exists()) {
                        subject = CredentialsProviderUtil.login(user, password);
                    }
                    RestClientBuilder restClientBuilder = getRestClientBuilder(urls, protocol, user, password, port);
                    client = new RestHighLevelClient(restClientBuilder);
                } catch (Throwable t) {
                    logger.error("Can't connect to ElasticSearch: " + parameterString, t);
                }
            }
        }
    }
}
Also used : ElasticSearchAuditDestination(org.apache.ranger.audit.destination.ElasticSearchAuditDestination) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) File(java.io.File)

Example 24 with RestClientBuilder

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

the class ElasticSearchMgr 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 {
        logger.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) 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) ElasticSearchAuditDestination(org.apache.ranger.audit.destination.ElasticSearchAuditDestination) AuthSchemes(org.apache.http.client.config.AuthSchemes) KerberosCredentialsProvider(org.apache.ranger.authorization.credutils.kerberos.KerberosCredentialsProvider) Locale(java.util.Locale) Lookup(org.apache.http.config.Lookup) SPNegoSchemeFactory(org.apache.http.impl.auth.SPNegoSchemeFactory) PrivilegedActionException(java.security.PrivilegedActionException) Logger(org.slf4j.Logger) KerberosTicket(javax.security.auth.kerberos.KerberosTicket) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient) Subject(javax.security.auth.Subject) File(java.io.File) Component(org.springframework.stereotype.Component) MiscUtil(org.apache.ranger.audit.provider.MiscUtil) PropertiesUtil(org.apache.ranger.common.PropertiesUtil) CredentialsProviderUtil(org.apache.ranger.authorization.credutils.CredentialsProviderUtil) 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 25 with RestClientBuilder

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

the class ElasticSearchIndexBootStrapper method createClient.

private void createClient() {
    try {
        RestClientBuilder restClientBuilder = getRestClientBuilder(hosts, protocol, user, password, port);
        client = new RestHighLevelClient(restClientBuilder);
    } catch (Throwable t) {
        lastLoggedAt.updateAndGet(lastLoggedAt -> {
            long now = System.currentTimeMillis();
            long elapsed = now - lastLoggedAt;
            if (elapsed > TimeUnit.MINUTES.toMillis(1)) {
                LOG.severe("Can't connect to ElasticSearch server: " + connectionString() + t);
                return now;
            } else {
                return lastLoggedAt;
            }
        });
    }
}
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) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RestHighLevelClient(org.elasticsearch.client.RestHighLevelClient)

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