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 1 commit 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 @@ -57,6 +57,14 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
return null;
}

/**
* APIKey for authentication with Elasticsearch.
* @return APIKey for authentication with Elasticsearch or {@code null}
*/
default @Nullable 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 @@ -48,6 +48,10 @@ public class ElasticsearchProperties {
* Password for authentication with Elasticsearch.
*/
private @Nullable String password;
/**
* APIKey for authentication with Elasticsearch.
*/
private @Nullable String APIKey;

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

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

public void setAPIKey(@Nullable 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 @@ -36,7 +36,9 @@
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.message.BasicHeader;
import org.apache.hc.core5.reactor.IOReactorConfig;
import org.apache.hc.core5.reactor.ssl.SSLBufferMode;
import org.apache.hc.core5.util.Timeout;
Expand Down Expand Up @@ -67,6 +69,7 @@
* @author Moritz Halbritter
* @author Andy Wilkinson
* @author Phillip Webb
* @author Laura Trotta
*/
class ElasticsearchRestClientConfigurations {

Expand Down Expand Up @@ -99,6 +102,11 @@ Rest5ClientBuilder elasticsearchRestClientBuilder(ElasticsearchConnectionDetails
.stream()
.map((node) -> new HttpHost(node.protocol().getScheme(), node.hostname(), node.port()))
.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)));
builder.setConnectionManagerCallback((connectionManagerBuilder) -> builderCustomizers.orderedStream()
Expand Down Expand Up @@ -275,6 +283,11 @@ public List<Node> getNodes() {
return this.properties.getPassword();
}

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

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

import co.elastic.clients.transport.rest5_client.low_level.Node;
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
Expand All @@ -33,6 +34,7 @@
import org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder;
import org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManagerBuilder;
import org.apache.hc.core5.function.Resolver;
import org.apache.hc.core5.http.Header;
import org.apache.hc.core5.http.HttpHost;
import org.apache.hc.core5.http.config.Registry;
import org.apache.hc.core5.util.Timeout;
Expand All @@ -47,6 +49,7 @@
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestClient;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.then;
Expand All @@ -62,6 +65,7 @@
* @author Andy Wilkinson
* @author Moritz Halbritter
* @author Phillip Webb
* @author Laura Trotta
*/
class ElasticsearchRestClientAutoConfigurationTests {

Expand Down Expand Up @@ -134,6 +138,25 @@ void configureUriWithNoScheme() {
});
}

@Test
void configureUriWithAPiKey() {
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200","spring.elasticsearch.apikey=some-apiKey").run((context) -> {
Rest5Client client = context.getBean(Rest5Client.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