Skip to content

SQL: add support for API key to JDBC and CLI#142021

Merged
luigidellaquila merged 14 commits intoelastic:mainfrom
luigidellaquila:sql/jdbc_api_key
Feb 24, 2026
Merged

SQL: add support for API key to JDBC and CLI#142021
luigidellaquila merged 14 commits intoelastic:mainfrom
luigidellaquila:sql/jdbc_api_key

Conversation

@luigidellaquila
Copy link
Copy Markdown
Member

@luigidellaquila luigidellaquila commented Feb 6, 2026

Adding support for API key authentication to SQL HTTP client, used by JDBC and CLI.
Also adding an option to use it in JDBC connection URL and as a CLI parameter

jdbc:es://http://server:9200/?apiKey=<encoded-api-key>
./bin/elasticsearch-sql-cli --apikey <encoded-api-key> https://some.server:9200

Developed using AI-assisted tooling

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 6, 2026

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Feb 6, 2026

ℹ️ Important: Docs version tagging

👋 Thanks for updating the docs! Just a friendly reminder that our docs are now cumulative. This means all 9.x versions are documented on the same page and published off of the main branch, instead of creating separate pages for each minor version.

We use applies_to tags to mark version-specific features and changes.

Expand for a quick overview

When to use applies_to tags:

✅ At the page level to indicate which products/deployments the content applies to (mandatory)
✅ When features change state (e.g. preview, ga) in a specific version
✅ When availability differs across deployments and environments

What NOT to do:

❌ Don't remove or replace information that applies to an older version
❌ Don't add new information that applies to a specific version without an applies_to tag
❌ Don't forget that applies_to tags can be used at the page, section, and inline level

🤔 Need help?

@luigidellaquila luigidellaquila marked this pull request as ready for review February 9, 2026 14:53
@elasticsearchmachine elasticsearchmachine added the Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) label Feb 9, 2026
@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Pinging @elastic/es-analytical-engine (Team:Analytics)

@elasticsearchmachine
Copy link
Copy Markdown
Collaborator

Hi @luigidellaquila, I've created a changelog YAML for you.

@luigidellaquila luigidellaquila requested a review from a team as a code owner February 23, 2026 09:42
if (options.has(silentOption)) {
terminal.setVerbosity(Terminal.Verbosity.SILENT);
loggerFactory.setRootLevel(Level.OFF);
if (loggerFactory != null) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this needed now but wasn't needed so far?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not a consequence of this PR.
The factory is null since #123742, so it started throwing NPE. I don't know why we didn't notice it before.
The problem is that our CLI does not depend on server, so we can't access LogConfigurator and LoggerFactoryImpl.
Let me see if we can do better, maybe we can setup a custom log factory

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's more complex than I thought.
SQL CLI runs as a shadow JAR, so I can't access LoggerFactory.setInstance() directly.
I'll open an issue, but I guess the null checks are enough to unblock the component

apiKey = settings.getProperty(AUTH_API_KEY);

// validate that only one authentication method is specified
if (StringUtils.hasText(apiKey) && StringUtils.hasText(user)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this validation also apply in other constructor as well?

$ ./bin/elasticsearch-sql-cli https://sql_user:strongpassword@some.server:9200
```

### API Key Authentication [sql-cli-apikey]
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am wondering about --apikey <value> on the command line being visible via ps aux or /proc/<pid>/cmdline. The docs mention not to combine API key with basic auth, but don't
warn about this security aspect. Should this be mentioned in docs, and provide alternative suggestions?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. I'm adding a note to the docs.


### API Key Authentication [sql-cli-apikey]

As an alternative to basic authentication, you can use API key authentication with the `--apikey` option. API keys can be created using the [Create API key API](docs-content://deploy-manage/api-keys/elasticsearch-api-keys.md). The API key should be provided in its encoded form (the `encoded` value returned by the Create API key API):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

--apiKey? (uppercase K)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is for consistency, JDBC and CLI use all lowercase parameters.

As an alternative to basic authentication, you can use API key authentication with the `--apikey` option. API keys can be created using the [Create API key API](docs-content://deploy-manage/api-keys/elasticsearch-api-keys.md). The API key should be provided in its encoded form (the `encoded` value returned by the Create API key API):

```bash
$ ./bin/elasticsearch-sql-cli --apikey <encoded-api-key> https://some.server:9200
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. Uppercase K?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

}

@SuppressWarnings("this-escape")
private EmbeddedCli(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think only this constructor needs the @suppressWarnings.

@@ -0,0 +1,218 @@
/*
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like comments in code, but in these test classes I think there are too many comments imo.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me remove the redundant comments

return cluster.split(",")[0];
}

private String createApiKey(String name, String body) throws IOException {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should clean up these api keys when the test finishes (or fails midway) in an @After method.

}
}

private String createApiKey(String name, String body) throws IOException {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. You should clean up these api keys when the test finishes (or fails midway) in an @After method.

Copy link
Copy Markdown
Contributor

@astefan astefan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's LGTM, but I would look into extracting common code from both CliApiKeyIT and JdbcApiKeyIT in a base class and extend that instead (like the clean up logic, the method that creates the api keys etc)

@luigidellaquila
Copy link
Copy Markdown
Member Author

Thanks @astefan!

@luigidellaquila luigidellaquila enabled auto-merge (squash) February 24, 2026 11:05
@luigidellaquila luigidellaquila merged commit 88ea038 into elastic:main Feb 24, 2026
35 checks passed
szybia added a commit to szybia/elasticsearch that referenced this pull request Feb 24, 2026
…on-sliced-reindex

* upstream/main:
  Activity logging improvements (elastic#142901)
  Fix serialization of NodeGpuStatsResponse when no GPU is present (elastic#142937)
  Add doc on master elections in DistributedArchitectureGuide (elastic#142435)
  ESQL: Account for missing StubRelation due to SurrogateExpressions replacement (elastic#142882)
  Add BulkByScrollTask Serialization Tests (elastic#142697)
  Rebalance CI test partitions to reduce Part3 bottleneck (elastic#142930)
  Mute org.elasticsearch.xpack.esql.qa.multi_node.EsqlClientYamlIT test {p0=esql/40_tsdb/to_aggregate_metric_double with multi_values} elastic#142964
  Bump OpenTelemetry dependencies (elastic#142323)
  SQL: add support for API key to JDBC and CLI (elastic#142021)
  Ensure requested capability exists (elastic#142695)
  Warn and fall back to local branches.json (elastic#142606)
  [CI] Mute testWithFetchFailures, testAddCompletionListenerScheduleErr… (elastic#142926)
  ESQL: Add support for ORC file format (elastic#142900)
  Update wolfi (versioned) (elastic#142948)
  Add BulkByScrollResponse Serialization Tests (elastic#142688)
  Run 25_id_generation with and without synthetic id (elastic#142770)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

:Analytics/SQL SQL querying >enhancement Team:Analytics Meta label for analytical engine team (ESQL/Aggs/Geo) v9.4.0

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants