-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Supporting basic authentication for Elasticsearch connector #1838
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 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 |
|---|---|---|
|
|
@@ -48,11 +48,14 @@ public class ElasticsearchConfig | |
| { | ||
| public enum Security | ||
| { | ||
| AWS | ||
| AWS, | ||
| X_PACK | ||
| } | ||
|
|
||
| private String host; | ||
| private int port = 9200; | ||
| private String username; | ||
|
Member
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. Let's put these in a separate config similar to how we handle |
||
| private String password; | ||
| private String defaultSchema = "default"; | ||
| private int scrollSize = 1_000; | ||
| private Duration scrollTimeout = new Duration(1, MINUTES); | ||
|
|
@@ -95,6 +98,33 @@ public ElasticsearchConfig setPort(int port) | |
| return this; | ||
| } | ||
|
|
||
| public String getUsername() | ||
| { | ||
| return username; | ||
| } | ||
|
|
||
| @Config("elasticsearch.username") | ||
| @ConfigDescription("Username for connecting to X-Pack secured Elasticsearch") | ||
| public ElasticsearchConfig setUsername(String username) | ||
| { | ||
| this.username = username; | ||
| return this; | ||
| } | ||
|
|
||
| public String getPassword() | ||
| { | ||
| return password; | ||
| } | ||
|
|
||
| @Config("elasticsearch.password") | ||
| @ConfigSecuritySensitive | ||
| @ConfigDescription("Password for connecting to X-Pack secured Elasticsearch") | ||
| public ElasticsearchConfig setPassword(String password) | ||
| { | ||
| this.password = password; | ||
| return this; | ||
| } | ||
|
|
||
| @NotNull | ||
| public String getDefaultSchema() | ||
| { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -32,7 +32,11 @@ | |
| import io.prestosql.spi.PrestoException; | ||
| import org.apache.http.HttpEntity; | ||
| import org.apache.http.HttpHost; | ||
| import org.apache.http.auth.AuthScope; | ||
| import org.apache.http.auth.UsernamePasswordCredentials; | ||
| import org.apache.http.client.CredentialsProvider; | ||
| import org.apache.http.conn.ssl.NoopHostnameVerifier; | ||
| import org.apache.http.impl.client.BasicCredentialsProvider; | ||
| import org.apache.http.util.EntityUtils; | ||
| import org.elasticsearch.ElasticsearchStatusException; | ||
| import org.elasticsearch.action.search.ClearScrollRequest; | ||
|
|
@@ -86,6 +90,7 @@ | |
| import static com.google.common.collect.ImmutableMap.toImmutableMap; | ||
| import static io.airlift.concurrent.Threads.daemonThreadsNamed; | ||
| import static io.airlift.json.JsonCodec.jsonCodec; | ||
| import static io.prestosql.elasticsearch.ElasticsearchConfig.Security.X_PACK; | ||
| import static io.prestosql.elasticsearch.ElasticsearchErrorCode.ELASTICSEARCH_CONNECTION_ERROR; | ||
| import static io.prestosql.elasticsearch.ElasticsearchErrorCode.ELASTICSEARCH_INVALID_RESPONSE; | ||
| import static io.prestosql.elasticsearch.ElasticsearchErrorCode.ELASTICSEARCH_QUERY_FAILURE; | ||
|
|
@@ -184,6 +189,15 @@ private static RestHighLevelClient createClient(ElasticsearchConfig config, Opti | |
| .setMaxRetryTimeoutMillis((int) config.getMaxRetryTime().toMillis()); | ||
|
|
||
| builder.setHttpClientConfigCallback(clientBuilder -> { | ||
| if (config.getSecurity().isPresent()) { | ||
|
Member
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. Once you move the configs to a separate object similar to |
||
| if (config.getSecurity().get().equals(X_PACK)) { | ||
| CredentialsProvider xPackCredentialsProvider = getXPackCredentialsProvider(config); | ||
| if (xPackCredentialsProvider != null) { | ||
| clientBuilder.setDefaultCredentialsProvider(xPackCredentialsProvider); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| if (config.isTlsEnabled()) { | ||
| buildSslContext(config.getKeystorePath(), config.getKeystorePassword(), config.getTrustStorePath(), config.getTruststorePassword()) | ||
| .ifPresent(clientBuilder::setSSLContext); | ||
|
|
@@ -203,6 +217,17 @@ private static RestHighLevelClient createClient(ElasticsearchConfig config, Opti | |
| return new RestHighLevelClient(builder); | ||
| } | ||
|
|
||
| private static CredentialsProvider getXPackCredentialsProvider(ElasticsearchConfig config) | ||
| { | ||
| if (config.getUsername() != null && config.getPassword() != null) { | ||
| final CredentialsProvider credentialsProvider = new BasicCredentialsProvider(); | ||
| credentialsProvider.setCredentials(AuthScope.ANY, | ||
| new UsernamePasswordCredentials(config.getUsername(), config.getPassword())); | ||
| return credentialsProvider; | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| private static AWSCredentialsProvider getAwsCredentialsProvider(AwsSecurityConfig config) | ||
| { | ||
| if (config.getAccessKey().isPresent() && config.getSecretKey().isPresent()) { | ||
|
|
||
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.
We should this to "Password Authentication".
X-Packis a plugin framework for Elasticsearch. From the point of view of Presto, this is about using simple user/password scheme to authenticate with ES.