Skip to content

Commit e25463b

Browse files
committed
adding apikey to elastic config
Signed-off-by: Laura Trotta <laura.trotta@elastic.co>
1 parent 33f0b40 commit e25463b

File tree

4 files changed

+49
-0
lines changed

4 files changed

+49
-0
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchConnectionDetails.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ default String getPassword() {
5555
return null;
5656
}
5757

58+
default String getAPIKey() {
59+
return null;
60+
}
61+
5862
/**
5963
* Prefix added to the path of every request sent to Elasticsearch.
6064
* @return prefix added to the path of every request sent to Elasticsearch or

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchProperties.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ public class ElasticsearchProperties {
4646
* Password for authentication with Elasticsearch.
4747
*/
4848
private String password;
49+
/**
50+
* APIKey for authentication with Elasticsearch.
51+
*/
52+
private String APIKey;
4953

5054
/**
5155
* Connection timeout used when communicating with Elasticsearch.
@@ -93,6 +97,15 @@ public void setPassword(String password) {
9397
this.password = password;
9498
}
9599

100+
public String getAPIKey() {
101+
return this.APIKey;
102+
}
103+
104+
public void setAPIKey(String APIKey) {
105+
this.APIKey = APIKey;
106+
}
107+
108+
96109
public Duration getConnectionTimeout() {
97110
return this.connectionTimeout;
98111
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientConfigurations.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import javax.net.ssl.HostnameVerifier;
2525
import javax.net.ssl.SSLContext;
2626

27+
import org.apache.http.Header;
2728
import org.apache.http.HttpHost;
2829
import org.apache.http.auth.AuthScope;
2930
import org.apache.http.auth.Credentials;
@@ -32,6 +33,7 @@
3233
import org.apache.http.impl.client.BasicCredentialsProvider;
3334
import org.apache.http.impl.nio.client.HttpAsyncClientBuilder;
3435
import org.apache.http.impl.nio.reactor.IOReactorConfig;
36+
import org.apache.http.message.BasicHeader;
3537
import org.apache.http.nio.conn.ssl.SSLIOSessionStrategy;
3638
import org.elasticsearch.client.RestClient;
3739
import org.elasticsearch.client.RestClientBuilder;
@@ -94,6 +96,11 @@ RestClientBuilder elasticsearchRestClientBuilder(ElasticsearchConnectionDetails
9496
.stream()
9597
.map((node) -> new HttpHost(node.hostname(), node.port(), node.protocol().getScheme()))
9698
.toArray(HttpHost[]::new));
99+
if (connectionDetails.getAPIKey() != null) {
100+
builder.setDefaultHeaders(new Header[]{
101+
new BasicHeader("Authorization", "ApiKey " + connectionDetails.getAPIKey()),
102+
});
103+
}
97104
builder.setHttpClientConfigCallback((httpClientBuilder) -> {
98105
builderCustomizers.orderedStream().forEach((customizer) -> customizer.customize(httpClientBuilder));
99106
SslBundle sslBundle = connectionDetails.getSslBundle();
@@ -260,6 +267,11 @@ public String getPassword() {
260267
return this.properties.getPassword();
261268
}
262269

270+
@Override
271+
public String getAPIKey() {
272+
return this.properties.getAPIKey();
273+
}
274+
263275
@Override
264276
public String getPathPrefix() {
265277
return this.properties.getPathPrefix();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/elasticsearch/ElasticsearchRestClientAutoConfigurationTests.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,9 @@
1919
import java.time.Duration;
2020
import java.util.ArrayList;
2121
import java.util.List;
22+
import java.util.Optional;
2223

24+
import org.apache.http.Header;
2325
import org.apache.http.HttpHost;
2426
import org.apache.http.auth.AuthScope;
2527
import org.apache.http.auth.Credentials;
@@ -128,6 +130,24 @@ void configureUriWithNoScheme() {
128130
});
129131
}
130132

133+
@Test
134+
void configureUriWithAPiKey() {
135+
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200","spring.elasticsearch.apikey=some-apiKey").run((context) -> {
136+
RestClient client = context.getBean(RestClient.class);
137+
assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString))
138+
.containsExactly("http://localhost:9200");
139+
assertThat(client)
140+
.extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class))
141+
.satisfies(( defaultHeaders) -> {
142+
Optional<? extends Header> authHeader = defaultHeaders.stream()
143+
.filter(x -> x.getName().equals("Authorization"))
144+
.findFirst();
145+
assertThat(authHeader).isPresent();
146+
assertThat(authHeader.get().getValue()).isEqualTo("ApiKey some-apiKey");
147+
});
148+
});
149+
}
150+
131151
@Test
132152
void configureUriWithUsernameOnly() {
133153
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200").run((context) -> {

0 commit comments

Comments
 (0)