|
2 | 2 |
|
3 | 3 | import java.net.InetSocketAddress; |
4 | 4 | import java.util.ArrayList; |
| 5 | +import java.util.Collections; |
5 | 6 | import java.util.List; |
6 | 7 |
|
| 8 | +import org.apache.http.Header; |
| 9 | +import org.apache.http.HttpHeaders; |
7 | 10 | import org.apache.http.HttpHost; |
8 | 11 | import org.apache.http.auth.AuthScope; |
9 | 12 | import org.apache.http.auth.UsernamePasswordCredentials; |
|
12 | 15 | import org.apache.http.impl.client.BasicCredentialsProvider; |
13 | 16 | import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; |
14 | 17 | import org.apache.http.impl.nio.reactor.IOReactorConfig; |
| 18 | +import org.apache.http.message.BasicHeader; |
15 | 19 | import org.apache.http.nio.conn.NoopIOSessionStrategy; |
16 | 20 | import org.elasticsearch.client.RestClient; |
17 | 21 | import org.elasticsearch.client.RestClientBuilder; |
|
24 | 28 | import io.quarkus.arc.Arc; |
25 | 29 | import io.quarkus.arc.InstanceHandle; |
26 | 30 | import io.quarkus.elasticsearch.restclient.lowlevel.ElasticsearchClientConfig; |
| 31 | +import io.quarkus.runtime.configuration.ConfigurationException; |
27 | 32 |
|
28 | 33 | public final class RestClientBuilderHelper { |
29 | 34 |
|
@@ -54,16 +59,7 @@ public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder reques |
54 | 59 | builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { |
55 | 60 | @Override |
56 | 61 | public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { |
57 | | - if (config.username().isPresent()) { |
58 | | - if (!"https".equalsIgnoreCase(config.protocol())) { |
59 | | - LOG.warn("Using Basic authentication in HTTP implies sending plain text passwords over the wire, " + |
60 | | - "use the HTTPS protocol instead."); |
61 | | - } |
62 | | - CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
63 | | - credentialsProvider.setCredentials(AuthScope.ANY, |
64 | | - new UsernamePasswordCredentials(config.username().get(), config.password().orElse(null))); |
65 | | - httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); |
66 | | - } |
| 62 | + applyAuthentication(httpClientBuilder, config); |
67 | 63 |
|
68 | 64 | if (config.ioThreadCounts().isPresent()) { |
69 | 65 | IOReactorConfig ioReactorConfig = IOReactorConfig.custom() |
@@ -112,4 +108,28 @@ public static Sniffer createSniffer(RestClient client, ElasticsearchConfig confi |
112 | 108 |
|
113 | 109 | return builder.build(); |
114 | 110 | } |
| 111 | + |
| 112 | + private static void applyAuthentication(HttpAsyncClientBuilder httpClientBuilder, ElasticsearchConfig config) { |
| 113 | + boolean hasBasic = config.username().isPresent(); |
| 114 | + boolean hasApiKey = config.apiKey().isPresent(); |
| 115 | + if (hasBasic && hasApiKey) { |
| 116 | + throw new ConfigurationException("You must provide either a valid username/password pair for Basic " + |
| 117 | + "authentication OR only a valid API key for ApiKey authentication. Both methods are currently " + |
| 118 | + "enabled."); |
| 119 | + } |
| 120 | + if (hasBasic) { |
| 121 | + if (!"https".equalsIgnoreCase(config.protocol())) { |
| 122 | + LOG.warn("Using Basic authentication in HTTP implies sending plain text passwords over the wire, " + |
| 123 | + "use the HTTPS protocol instead."); |
| 124 | + } |
| 125 | + CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); |
| 126 | + credentialsProvider.setCredentials(AuthScope.ANY, |
| 127 | + new UsernamePasswordCredentials(config.username().get(), config.password().orElse(null))); |
| 128 | + httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); |
| 129 | + } else if (hasApiKey) { |
| 130 | + String apiKey = config.apiKey().get(); |
| 131 | + Header apiKeyHeader = new BasicHeader(HttpHeaders.AUTHORIZATION, "ApiKey " + apiKey); |
| 132 | + httpClientBuilder.setDefaultHeaders(Collections.singleton(apiKeyHeader)); |
| 133 | + } |
| 134 | + } |
115 | 135 | } |
0 commit comments