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);
}
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());
}
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);
}
}
}
}
}
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;
}
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;
}
});
}
}
Aggregations