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

Changes to include apikey configuration for elastic search rest client #45688

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import io.quarkus.runtime.annotations.ConfigRoot;
import io.smallrye.config.ConfigMapping;
import io.smallrye.config.WithDefault;
import io.smallrye.config.WithParentName;

@ConfigMapping(prefix = "quarkus.elasticsearch")
@ConfigRoot(phase = ConfigPhase.RUN_TIME)
Expand All @@ -28,6 +29,23 @@ public interface ElasticsearchConfig {
@WithDefault("http")
String protocol();

/**
* Retrieves the Configuration for API Key Authentication
*/
@WithParentName
Optional<EsApiKeyAuth> esApiKeyAuth();

/**
* Represents the API Key authentication details for Elasticsearch
*/
interface EsApiKeyAuth {

/**
* Retrieves the API key used for authentication.
*/
String apiKey();
}

Comment on lines +32 to +48
Copy link
Contributor

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:

# basic
quarkus.elasticsearch.auth.basic.username=foo
quarkus.elasticsearch.auth.basic.password=bar
# or API key:
quarkus.elasticsearch.auth.api-key=foobar

but that would mean deprecating existing username/password properties and creating new alternatives for them too. @yrodiere do you think it's worth doing ?

/**
* The username for basic HTTP authentication.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -92,6 +89,7 @@ public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpCli
}
return result;
}

});

return builder;
Expand All @@ -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()) {
Copy link
Contributor

Choose a reason for hiding this comment

The 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 ifs aren't necessary anymore, right?

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.");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
LOG.info("API Key authentication is enabled.");

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
Copy link
Contributor

Choose a reason for hiding this comment

The 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;
}
}