Skip to content

Commit 095ffc8

Browse files
committed
Polish "Add support for Elasticsearch API-key-based authentication"
See spring-projectsgh-46167
1 parent afbdb49 commit 095ffc8

File tree

4 files changed

+26
-35
lines changed

4 files changed

+26
-35
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ public interface ElasticsearchConnectionDetails extends ConnectionDetails {
5959

6060
/**
6161
* APIKey for authentication with Elasticsearch.
62-
* @return APIKey for authentication with Elasticsearch or {@code null}
62+
* @return the API key for authentication with Elasticsearch or {@code null}
6363
*/
64-
default @Nullable String getAPIKey() {
64+
default @Nullable String getApiKey() {
6565
return null;
6666
}
6767

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,11 @@ public class ElasticsearchProperties {
4848
* Password for authentication with Elasticsearch.
4949
*/
5050
private @Nullable String password;
51+
5152
/**
52-
* APIKey for authentication with Elasticsearch.
53+
* API key for authentication with Elasticsearch.
5354
*/
54-
private @Nullable String APIKey;
55+
private @Nullable String apiKey;
5556

5657
/**
5758
* Connection timeout used when communicating with Elasticsearch.
@@ -99,12 +100,12 @@ public void setPassword(@Nullable String password) {
99100
this.password = password;
100101
}
101102

102-
public @Nullable String getAPIKey() {
103-
return this.APIKey;
103+
public @Nullable String getApiKey() {
104+
return this.apiKey;
104105
}
105106

106-
public void setAPIKey(@Nullable String APIKey) {
107-
this.APIKey = APIKey;
107+
public void setApiKey(@Nullable String apiKey) {
108+
this.apiKey = apiKey;
108109
}
109110

110111
public Duration getConnectionTimeout() {

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

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,9 @@ Rest5ClientBuilder elasticsearchRestClientBuilder(ElasticsearchConnectionDetails
102102
.stream()
103103
.map((node) -> new HttpHost(node.protocol().getScheme(), node.hostname(), node.port()))
104104
.toArray(HttpHost[]::new));
105-
if (connectionDetails.getAPIKey() != null) {
106-
builder.setDefaultHeaders(new Header[]{
107-
new BasicHeader("Authorization", "ApiKey " + connectionDetails.getAPIKey()),
108-
});
105+
if (connectionDetails.getApiKey() != null) {
106+
builder.setDefaultHeaders(
107+
new Header[] { new BasicHeader("Authorization", "ApiKey " + connectionDetails.getApiKey()), });
109108
}
110109
builder.setHttpClientConfigCallback((httpClientBuilder) -> builderCustomizers.orderedStream()
111110
.forEach((customizer) -> customizer.customize(httpClientBuilder)));
@@ -284,8 +283,8 @@ public List<Node> getNodes() {
284283
}
285284

286285
@Override
287-
public @Nullable String getAPIKey() {
288-
return this.properties.getAPIKey();
286+
public @Nullable String getApiKey() {
287+
return this.properties.getApiKey();
289288
}
290289

291290
@Override

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

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.time.Duration;
2020
import java.util.ArrayList;
2121
import java.util.List;
22-
import java.util.Optional;
2322

2423
import co.elastic.clients.transport.rest5_client.low_level.Node;
2524
import co.elastic.clients.transport.rest5_client.low_level.Rest5Client;
@@ -49,7 +48,6 @@
4948
import org.springframework.boot.testsupport.classpath.resources.WithPackageResources;
5049
import org.springframework.context.annotation.Bean;
5150
import org.springframework.context.annotation.Configuration;
52-
import org.springframework.web.client.RestClient;
5351

5452
import static org.assertj.core.api.Assertions.assertThat;
5553
import static org.mockito.BDDMockito.then;
@@ -138,25 +136,6 @@ void configureUriWithNoScheme() {
138136
});
139137
}
140138

141-
@Test
142-
void configureUriWithAPiKey() {
143-
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200","spring.elasticsearch.apikey=some-apiKey").run((context) -> {
144-
Rest5Client client = context.getBean(Rest5Client.class);
145-
assertThat(client.getNodes().stream().map(Node::getHost).map(HttpHost::toString))
146-
.containsExactly("http://localhost:9200");
147-
assertThat(client)
148-
.extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class))
149-
.satisfies(( defaultHeaders) -> {
150-
Optional<? extends Header> authHeader = defaultHeaders.stream()
151-
.filter(x -> x.getName().equals("Authorization"))
152-
.findFirst();
153-
assertThat(authHeader).isPresent();
154-
assertThat(authHeader.get().getValue()).isEqualTo("ApiKey some-apiKey");
155-
});
156-
});
157-
}
158-
159-
160139
@Test
161140
void configureUriWithUsernameOnly() {
162141
this.contextRunner.withPropertyValues("spring.elasticsearch.uris=http://user@localhost:9200").run((context) -> {
@@ -216,6 +195,18 @@ void configureUriWithUsernameAndPasswordWhenUsernameAndPasswordPropertiesSet() {
216195
});
217196
}
218197

198+
@Test
199+
void whenApiKeyIsConfiguredThenAuthorizationHeaderIsPresent() {
200+
this.contextRunner.withPropertyValues("spring.elasticsearch.api-key=some-api-key").run((context) -> {
201+
Rest5Client client = context.getBean(Rest5Client.class);
202+
assertThat(client).extracting("defaultHeaders", InstanceOfAssertFactories.list(Header.class))
203+
.satisfiesOnlyOnce((header) -> {
204+
assertThat(header.getName().equals("Authorization"));
205+
assertThat(header.getValue().equals("ApiKey some-api-key"));
206+
});
207+
});
208+
}
209+
219210
@Test
220211
void configureWithCustomPathPrefix() {
221212
this.contextRunner.withPropertyValues("spring.elasticsearch.path-prefix=/some/prefix").run((context) -> {

0 commit comments

Comments
 (0)