VIDA-NYU / ache

ACHE is a web crawler for domain-specific search.
http://ache.readthedocs.io
Apache License 2.0
454 stars 135 forks source link

Question: Elastic Cloud Credentials #232

Closed MonksterFX closed 2 years ago

MonksterFX commented 2 years ago

Is there a easy way to pass credentials to the Elastic Client? Or is it necessary to implement this as a new feature? Would expect some input from config like that:

target_storage.data_format.elasticsearch.rest.username
target_storage.data_format.elasticsearch.rest.password
aecio commented 2 years ago

Unfortunately, this is not implemented yet. All config options can be seen in the class ElasticSearchConfig.java and the client is initialized at ElasticSearchClientFactory.java. I think modifying these two classes should be enough to make this work (assuming Elastic Cloud uses HTTP basic auth).

MonksterFX commented 2 years ago

Thanks to pointing me in the right direction. If anyone else need to do this follow along with:

https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/_basic_authentication.html

// ElasticSearchClientFactory.java
public static RestClient createClient(ElasticSearchConfig config) {

        HttpHost[] httpHosts = parseHostAddresses(config.getRestApiHosts());

        final CredentialsProvider credentialsProvider =
                new BasicCredentialsProvider();
        credentialsProvider.setCredentials(AuthScope.ANY,
                new UsernamePasswordCredentials(config.getUsername(), config.getPassword()));

        final RestClientBuilder builder = RestClient.builder(httpHosts)
                .setRequestConfigCallback(requestConfigBuilder -> requestConfigBuilder
                        .setConnectTimeout(config.getRestConnectTimeout())
                        .setSocketTimeout(config.getRestSocketTimeout())
                )
                .setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
                    @Override
                    public HttpAsyncClientBuilder customizeHttpClient(
                            HttpAsyncClientBuilder httpClientBuilder) {
                        return httpClientBuilder
                                .setDefaultCredentialsProvider(credentialsProvider);
                    }
                })
                .setMaxRetryTimeoutMillis(config.getRestMaxRetryTimeoutMillis());

...

config.getUsername()and config.getPassword() could be easily added to ElasticSearchConfig.java

aecio commented 2 years ago

Thanks, @MonksterFX! I just added this to the dev branch and will merge it into the master later if there are no problems.