Search in sources :

Example 1 with HttpClientConfigCallback

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;
}
Also used : HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback)

Example 2 with HttpClientConfigCallback

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);
}
Also used : HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 3 with HttpClientConfigCallback

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();
}
Also used : BasicAuthHttpClientConfigCallback(org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) RequestConfigCallback(org.elasticsearch.client.RestClientBuilder.RequestConfigCallback)

Example 4 with HttpClientConfigCallback

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;
    };
}
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) Constructor(java.lang.reflect.Constructor) StringUtils(org.apache.commons.lang3.StringUtils) ArrayList(java.util.ArrayList) BasicAuthHttpClientConfigCallback(org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback) RestClientAuthenticator(org.janusgraph.diskstorage.es.rest.util.RestClientAuthenticator) LinkedList(java.util.LinkedList) SSLConfigurationCallback(org.janusgraph.diskstorage.es.rest.util.SSLConfigurationCallback) INDEX_PORT(org.janusgraph.graphdb.configuration.GraphDatabaseConfiguration.INDEX_PORT) Logger(org.slf4j.Logger) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback) Configuration(org.janusgraph.diskstorage.configuration.Configuration) IOException(java.io.IOException) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) 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) Builder(org.janusgraph.diskstorage.es.rest.util.SSLConfigurationCallback.Builder) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) LinkedList(java.util.LinkedList)

Example 5 with HttpClientConfigCallback

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;
}
Also used : BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) HttpClientConfigCallback(org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder)

Aggregations

HttpClientConfigCallback (org.elasticsearch.client.RestClientBuilder.HttpClientConfigCallback)5 HttpAsyncClientBuilder (org.apache.http.impl.nio.client.HttpAsyncClientBuilder)2 RestClientBuilder (org.elasticsearch.client.RestClientBuilder)2 RequestConfigCallback (org.elasticsearch.client.RestClientBuilder.RequestConfigCallback)2 BasicAuthHttpClientConfigCallback (org.janusgraph.diskstorage.es.rest.util.BasicAuthHttpClientConfigCallback)2 Preconditions (com.google.common.base.Preconditions)1 IOException (java.io.IOException)1 Constructor (java.lang.reflect.Constructor)1 ArrayList (java.util.ArrayList)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 StringUtils (org.apache.commons.lang3.StringUtils)1 HttpHost (org.apache.http.HttpHost)1 CredentialsProvider (org.apache.http.client.CredentialsProvider)1 RequestConfig (org.apache.http.client.config.RequestConfig)1 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)1 CloseableHttpAsyncClient (org.apache.http.impl.nio.client.CloseableHttpAsyncClient)1 RestClient (org.elasticsearch.client.RestClient)1 ConfigOption (org.janusgraph.diskstorage.configuration.ConfigOption)1 Configuration (org.janusgraph.diskstorage.configuration.Configuration)1