Skip to content

Allow APIKey to be used as authentication method for Elasticsearch #46167

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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 @@ -55,6 +55,10 @@ default String getPassword() {
return null;
}

default String getAPIKey() {
return null;
}

/**
* Prefix added to the path of every request sent to Elasticsearch.
* @return prefix added to the path of every request sent to Elasticsearch or
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ public class ElasticsearchProperties {
* Password for authentication with Elasticsearch.
*/
private String password;
/**
* APIKey for authentication with Elasticsearch.
*/
private String APIKey;

/**
* Connection timeout used when communicating with Elasticsearch.
Expand Down Expand Up @@ -93,6 +97,15 @@ public void setPassword(String password) {
this.password = password;
}

public String getAPIKey() {
return this.APIKey;
}

public void setAPIKey(String APIKey) {
this.APIKey = APIKey;
}


public Duration getConnectionTimeout() {
return this.connectionTimeout;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
Expand All @@ -32,6 +33,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.ssl.SSLIOSessionStrategy;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestClientBuilder;
Expand Down Expand Up @@ -62,6 +64,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Laura Trotta
*/
class ElasticsearchRestClientConfigurations {

Expand Down Expand Up @@ -94,6 +97,11 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchConnectionDetails
.stream()
.map((node) -> new HttpHost(node.hostname(), node.port(), node.protocol().getScheme()))
.toArray(HttpHost[]::new));
if (connectionDetails.getAPIKey() != null) {
builder.setDefaultHeaders(new Header[]{
new BasicHeader("Authorization", "ApiKey " + connectionDetails.getAPIKey()),
});
}
builder.setHttpClientConfigCallback((httpClientBuilder) -> {
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(httpClientBuilder));
SslBundle sslBundle = connectionDetails.getSslBundle();
Expand Down Expand Up @@ -260,6 +268,11 @@ public String getPassword() {
return this.properties.getPassword();
}

@Override
public String getAPIKey() {
return this.properties.getAPIKey();
}

@Override
public String getPathPrefix() {
return this.properties.getPathPrefix();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import org.apache.http.Header;
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.Credentials;
Expand Down Expand Up @@ -60,6 +62,7 @@
* @author Andy Wilkinson
* @author Moritz Halbritter
* @author Phillip Webb
* @author Laura Trotta
*/
class ElasticsearchRestClientAutoConfigurationTests {

Expand Down Expand Up @@ -128,6 +131,24 @@ void configureUriWithNoScheme() {
});
}

@Test
void configureUriWithAPiKey() {
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200","spring.elasticsearch.apikey=some-apiKey").run((context) -> {
RestClient client = context.getBean(RestClient.class);
assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString))
.containsExactly("http://localhost:9200");
assertThat(client)
.extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class))
.satisfies(( defaultHeaders) -> {
Optional<? extends Header> authHeader = defaultHeaders.stream()
.filter(x -> x.getName().equals("Authorization"))
.findFirst();
assertThat(authHeader).isPresent();
assertThat(authHeader.get().getValue()).isEqualTo("ApiKey some-apiKey");
});
});
}

@Test
void configureUriWithUsernameOnly() {
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200").run((context) -> {
Expand Down