use of org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback in project janusgraph by JanusGraph.
the class RestClientSetupTest method authTestBase.
private HttpClientConfigCallback authTestBase(Map<String, String> extraConfigValues) throws Exception {
baseConfigTest(ImmutableMap.<String, String>builder().put("index." + INDEX_NAME + ".backend", "elasticsearch").put("index." + INDEX_NAME + ".hostname", ES_HOST_01).putAll(extraConfigValues).build());
final ArgumentCaptor<HttpClientConfigCallback> hcccCaptor = ArgumentCaptor.forClass(HttpClientConfigCallback.class);
// callback is passed to the client builder
verify(restClientBuilderMock).setHttpClientConfigCallback(hcccCaptor.capture());
final HttpClientConfigCallback hccc = hcccCaptor.getValue();
assertNotNull(hccc);
return hccc;
}
use of org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback in project janusgraph by JanusGraph.
the class RestClientSetupTest method testCustomAuthenticator.
@Test
public void testCustomAuthenticator() throws Exception {
final String uniqueInstanceKey = String.valueOf(instanceCount.getAndIncrement());
final String[] customAuthArgs = new String[] { uniqueInstanceKey, "arg1", "arg2" };
final String serializedArgList = StringUtils.join(customAuthArgs, ',');
final HttpClientConfigCallback hccc = authTestBase(ImmutableMap.<String, String>builder().put("index." + INDEX_NAME + ".elasticsearch.interface", "REST_CLIENT").put("index." + INDEX_NAME + ".elasticsearch.http.auth.type", HttpAuthTypes.CUSTOM.toString()).put("index." + INDEX_NAME + ".elasticsearch.http.auth.custom.authenticator-class", TestCustomAuthenticator.class.getName()).put("index." + INDEX_NAME + ".elasticsearch.http.auth.custom.authenticator-args", serializedArgList).build());
verify(restClientSetup).getCustomAuthenticator(eq(TestCustomAuthenticator.class.getName()), eq(customAuthArgs));
TestCustomAuthenticator customAuth = TestCustomAuthenticator.instanceMap.get(uniqueInstanceKey);
assertNotNull(customAuth);
// authenticator has been instantiated, verifying it has been called
assertEquals(1, customAuth.numInitCalls);
// verifying that the custom callback is in the chain
final HttpAsyncClientBuilder hacb = PowerMockito.mock(HttpAsyncClientBuilder.class);
doReturn(hacb).when(hacb).setDefaultCredentialsProvider(anyObject());
hccc.customizeHttpClient(hacb);
assertEquals(1, customAuth.customizeHttpClientHistory.size());
assertSame(hacb, customAuth.customizeHttpClientHistory.get(0));
assertArrayEquals(customAuthArgs, customAuth.args);
}
use of org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback in project janusgraph by JanusGraph.
the class RestClientSetup method getRestClient.
protected RestClient getRestClient(HttpHost[] hosts, Configuration config) {
final RestClientBuilder restClientBuilder = getRestClientBuilder(hosts);
final HttpClientConfigCallback httpClientConfigCallback = getHttpClientConfigCallback(config);
if (httpClientConfigCallback != null) {
restClientBuilder.setHttpClientConfigCallback(httpClientConfigCallback);
}
final RequestConfigCallback requestConfigCallback = getRequestConfigCallback(config);
if (requestConfigCallback != null) {
restClientBuilder.setRequestConfigCallback(requestConfigCallback);
}
if (config.has(ElasticSearchIndex.MAX_RETRY_TIMEOUT)) {
restClientBuilder.setMaxRetryTimeoutMillis(config.get(ElasticSearchIndex.MAX_RETRY_TIMEOUT));
}
return restClientBuilder.build();
}
use of org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback 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.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;
};
}
use of org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback in project janusgraph by JanusGraph.
the class RestClientSetupTest method basicAuthTestBase.
private CredentialsProvider basicAuthTestBase(final Map<String, String> extraConfigValues, final String realm, final String username, final String password) throws Exception {
final HttpClientConfigCallback hccc = authTestBase(ImmutableMap.<String, String>builder().put("index." + INDEX_NAME + ".elasticsearch.interface", "REST_CLIENT").put("index." + INDEX_NAME + ".elasticsearch.http.auth.type", HttpAuthTypes.BASIC.toString()).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.username", username).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.password", password).put("index." + INDEX_NAME + ".elasticsearch.http.auth.basic.realm", realm).putAll(extraConfigValues).build());
final HttpAsyncClientBuilder hacb = PowerMockito.mock(HttpAsyncClientBuilder.class);
doReturn(hacb).when(hacb).setDefaultCredentialsProvider(anyObject());
hccc.customizeHttpClient(hacb);
final ArgumentCaptor<BasicCredentialsProvider> cpCaptor = ArgumentCaptor.forClass(BasicCredentialsProvider.class);
verify(hacb).setDefaultCredentialsProvider(cpCaptor.capture());
final CredentialsProvider cp = cpCaptor.getValue();
assertNotNull(cp);
return cp;
}
Aggregations