-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Changes to include apikey configuration for elastic search rest client #45688
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||
---|---|---|---|---|
|
@@ -2,8 +2,11 @@ | |||
|
||||
import java.net.InetSocketAddress; | ||||
import java.util.ArrayList; | ||||
import java.util.Collections; | ||||
import java.util.List; | ||||
|
||||
import org.apache.http.Header; | ||||
import org.apache.http.HttpHeaders; | ||||
import org.apache.http.HttpHost; | ||||
import org.apache.http.auth.AuthScope; | ||||
import org.apache.http.auth.UsernamePasswordCredentials; | ||||
|
@@ -12,6 +15,7 @@ | |||
import org.apache.http.impl.client.BasicCredentialsProvider; | ||||
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder; | ||||
import org.apache.http.impl.nio.reactor.IOReactorConfig; | ||||
import org.apache.http.message.BasicHeader; | ||||
import org.apache.http.nio.conn.NoopIOSessionStrategy; | ||||
import org.elasticsearch.client.RestClient; | ||||
import org.elasticsearch.client.RestClientBuilder; | ||||
|
@@ -54,16 +58,9 @@ public RequestConfig.Builder customizeRequestConfig(RequestConfig.Builder reques | |||
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() { | ||||
@Override | ||||
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) { | ||||
if (config.username().isPresent()) { | ||||
if (!"https".equalsIgnoreCase(config.protocol())) { | ||||
LOG.warn("Using Basic authentication in HTTP implies sending plain text passwords over the wire, " + | ||||
"use the HTTPS protocol instead."); | ||||
} | ||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | ||||
credentialsProvider.setCredentials(AuthScope.ANY, | ||||
new UsernamePasswordCredentials(config.username().get(), config.password().orElse(null))); | ||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); | ||||
} | ||||
|
||||
EsAuth authMethod = checkAuthMethod(config); | ||||
authMethod.apply(httpClientBuilder, config); | ||||
|
||||
if (config.ioThreadCounts().isPresent()) { | ||||
IOReactorConfig ioReactorConfig = IOReactorConfig.custom() | ||||
|
@@ -92,6 +89,7 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli | |||
} | ||||
return result; | ||||
} | ||||
|
||||
}); | ||||
|
||||
return builder; | ||||
|
@@ -112,4 +110,58 @@ public static Sniffer createSniffer(RestClient client, ElasticsearchConfig confi | |||
|
||||
return builder.build(); | ||||
} | ||||
|
||||
public enum EsAuth { | ||||
NONE { | ||||
@Override | ||||
public void apply(HttpAsyncClientBuilder httpClientBuilder, ElasticsearchConfig config) { | ||||
// No authentication needed | ||||
} | ||||
}, | ||||
|
||||
BASIC { | ||||
@Override | ||||
public void apply(HttpAsyncClientBuilder httpClientBuilder, ElasticsearchConfig config) { | ||||
if (config.username().isPresent()) { | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. since you are returning an auth type after you've checked the presence of the properties these very outer |
||||
if (!"https".equalsIgnoreCase(config.protocol())) { | ||||
LOG.warn("Using Basic authentication in HTTP implies sending plain text passwords over the wire, " | ||||
+ "use the HTTPS protocol instead."); | ||||
} | ||||
|
||||
CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | ||||
credentialsProvider.setCredentials(AuthScope.ANY, | ||||
new UsernamePasswordCredentials(config.username().get(), | ||||
config.password().orElse(null))); | ||||
httpClientBuilder.setDefaultCredentialsProvider(credentialsProvider); | ||||
} | ||||
} | ||||
}, | ||||
|
||||
API_KEY { | ||||
@Override | ||||
public void apply(HttpAsyncClientBuilder httpClientBuilder, ElasticsearchConfig config) { | ||||
if (config.esApiKeyAuth().isPresent()) { | ||||
ElasticsearchConfig.EsApiKeyAuth auth = config.esApiKeyAuth().get(); | ||||
Header apiKeyHeader = new BasicHeader(HttpHeaders.AUTHORIZATION, | ||||
"ApiKey " + auth.apiKey()); | ||||
httpClientBuilder.setDefaultHeaders(Collections.singleton(apiKeyHeader)); | ||||
LOG.info("API Key authentication is enabled."); | ||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I'd say let's be consistent here, since we aren't logging at info for the basic auth, we shouldn't log here too. |
||||
} | ||||
} | ||||
}; | ||||
|
||||
public abstract void apply(HttpAsyncClientBuilder httpClientBuilder, ElasticsearchConfig config); | ||||
} | ||||
|
||||
private static EsAuth checkAuthMethod(ElasticsearchConfig config) { | ||||
boolean hasBasic = config.username().isPresent(); | ||||
boolean hasApiKey = config.esApiKeyAuth().isPresent(); | ||||
|
||||
if (hasBasic && hasApiKey) { | ||||
LOG.warn("Multiple authentication methods configured. Defaulting to Basic Authentication."); | ||||
return EsAuth.BASIC; | ||||
} | ||||
Comment on lines
+160
to
+163
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd still say it's better to throw an exception here instead of logging a warning since this is a "misconfiguration", and a user would want to be precise about the auth methods. |
||||
|
||||
return hasApiKey ? EsAuth.API_KEY : hasBasic ? EsAuth.BASIC : EsAuth.NONE; | ||||
} | ||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
since it's a single property.. do we want to wrap it in the interface? This would probably make more sense for the basic auth where there's a pair of properties... if we want to have auth grouped somehow maybe we can consider going with something like:
but that would mean deprecating existing
username/password
properties and creating new alternatives for them too. @yrodiere do you think it's worth doing ?