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

Conversation

l-trotta
Copy link

APIKey is a popular authentication method for elasticsearch, available both for local installations and for cloud instances; in fact, it's the recommended way to setup the Elasticsearch Java client!

This PR adds spring.elasticsearch.apikey as one of the elasticsearch properties, so that users will be able to setup a connection simply with:

spring.elasticsearch.uris=${ES_SERVER_URL}
spring.elasticsearch.apikey=${ES_APIKEY}

If this gets merged I will also take care of the documentation of affected repos (spring-ai, spring-data-elasticsearch) to add and describe the new property.

Disclaimer: I am one of the maintainer of https://github.com/elastic/elasticsearch-java

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jun 23, 2025
Signed-off-by: Laura Trotta <laura.trotta@elastic.co>
Signed-off-by: Laura Trotta <laura.trotta@elastic.co>
@snicoll snicoll added type: enhancement A general enhancement and removed status: waiting-for-triage An issue we've not yet triaged labels Jun 23, 2025
@snicoll snicoll added this to the 4.0.x milestone Jun 23, 2025
@snicoll snicoll changed the title Adding APIKey as authentication method for Elasticsearch Allow APIKey to be used as authentication method for Elasticsearch Jun 23, 2025
@snicoll
Copy link
Member

snicoll commented Jun 23, 2025

Thanks for the PR

it's the recommended way to setup the Elasticsearch Java client!

I wasn't aware of that. How does it work with Testcontainers and Docker compose then? Both of them retrieve a password from the environment. I can see you have updated ElasticsearchConnectionDetails but none of their two implementations.

@snicoll snicoll added the status: waiting-for-feedback We need additional information before we can continue label Jun 23, 2025
@l-trotta
Copy link
Author

Creating the API key has to be done through either an API call or in the dedicated Kibana menu, this is because you can decide to create one with certain privileges, or with an expiration date. So the default for both implementations of ElasticsearchConnectionDetails would be null, because the API key cannot be retreived from the environment, but only created after the elasticsearch instance is running.

But it works the exact same for cloud, testcontainers or docker, it's just an additional step unlike username and password credentials.

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels Jun 23, 2025
@snicoll snicoll added the for: team-attention An issue we'd like other members of the team to review label Jun 23, 2025
@ezimuel
Copy link

ezimuel commented Jun 23, 2025

@l-trotta and @snicoll maybe we can simplify the Elasticsearch testing using start-local. This script installs Elasticsearch with a .env file containing all the information for the connection, including the API key (ES_LOCAL_API_KEY). If you only need Elasticsearch and not Kibana you can use the --esonly option.

@philwebb philwebb removed the for: team-attention An issue we'd like other members of the team to review label Jun 23, 2025
@eddumelendez
Copy link
Contributor

Hi, what about providing and env var in Elasticsearch image which allows to configure the API Key? That would benefit Docker Compose and Testcontainers implementations. Also, if this is the recommended way then the image could provide a default way to enable this.

On the Testcontainers side we can add a custom implementation to generate the API Key but I am more in favor to provide the same experience for Docker Compose and Testcontainers users by just enabling the API key with a env var.

@bclozel
Copy link
Member

bclozel commented Jun 23, 2025

@eddumelendez Looking at what @l-trotta pointed out, it seems that one of the main driver behind API keys is to have fine-grained roles per index, since you need to provide those during API key generation. I guess that for a given user, you want to use different keys for specific usage patterns and restrict roles accordingly.
Is this something that testcontainers is already supporting in some way, or is it something that developers should write as part of their tests?

@eddumelendez
Copy link
Contributor

eddumelendez commented Jun 23, 2025

Hi @bclozel, currently, developers must extend and override ElasticsearchContainer, implement containerIsStarted as part of the Testcontainers Lifecycle and make a similar call to start-local using a HTTP library.

@eddumelendez
Copy link
Contributor

I've uploaded an example using Testcontainers and Elasticsearch using API Key.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status: feedback-provided Feedback has been provided type: enhancement A general enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants