Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[PROPOSAL] Improve usability of ApacheHttpClient5TransportBuilder #943

Open
dblock opened this issue Apr 14, 2024 · 0 comments
Open

[PROPOSAL] Improve usability of ApacheHttpClient5TransportBuilder #943

dblock opened this issue Apr 14, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@dblock
Copy link
Member

dblock commented Apr 14, 2024

What/Why

What are you proposing?

Reduce the amount of code one needs to write to connect to OpenSearch with ApacheHttpClient5TransportBuilder.

Instead of

val host = org.apache.hc.core5.http.HttpHost.create(endpoint)
                transport = ApacheHttpClient5TransportBuilder.builder(host)
                        .setMapper(JacksonJsonpMapper())
                        .setHttpClientConfigCallback({ httpClientBuilder ->

                            // BASIC auth, username/password
                            val username: String? = System.getenv().getOrDefault("USERNAME", null)
                            val password: String? = System.getenv().getOrDefault("PASSWORD", null)
                            if (username != null && password != null) {
                                val credentialsProvider = BasicCredentialsProvider();
                                credentialsProvider.setCredentials(
                                    AuthScope(host),
                                    UsernamePasswordCredentials(username, password.toCharArray()));
                                httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider)
                            }

                            // localhost, disable TLS
                            if (endpoint.startsWith("https://localhost:")) {
                                val sslContext = SSLContextBuilder
                                    .create()
                                    .loadTrustMaterial(null, {_, _ -> true })
                                    .build();
                                val tlsStrategy = ClientTlsStrategyBuilder.create()
                                    .setSslContext(sslContext)
                                    .setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
                                    .build();
                                val connectionManager = PoolingAsyncClientConnectionManagerBuilder.create()
                                    .setTlsStrategy(tlsStrategy)
                                    .build();
                                httpClientBuilder.setConnectionManager(connectionManager);
                            }

                            httpClientBuilder
                        })
                        .build()
                transport = ApacheHttpClient5TransportBuilder.builder(endpoint)
                    .withBasicAuth(username, password)
                    .verifySSLHosts(false)
                    .build()

The two changes above are a helper for BASIC auth and a simply way to disable SSL verification, which are common things people do for local testing. Finally, all builders could take a String endpoints (URLs).

What users have asked for this feature?

I wrote a [Kotlin sample](https://github.com/dblock/opensearch-kotlin-client-demo] that attempts to connect to either a local (docker) instance of OpenSearch or Amazon OpenSearch. This requires using two different transports and causes at least one imported namespace to conflict, making code cumbersome.

The sample in https://github.com/opensearch-project/opensearch-java/blob/main/samples/src/main/java/org/opensearch/client/samples/SampleClient.java is similarly annoying to write.

What problems are you trying to solve?

Simplify writing code that works in common scenarios and in connecting to either a self-hosted OpenSearch and/or AWS.

What is the developer experience going to be?

Less code.

Are there any security considerations?

Making it very easy to disable TLS or add basic auth may lead to unexpected side effects.

Are there any breaking changes to the API

No.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant