Skip to content

feat(auth): add key-pair authentication support#774

Merged
everpcpc merged 11 commits into
databendlabs:mainfrom
everpcpc:feat/key-pair-auth
May 19, 2026
Merged

feat(auth): add key-pair authentication support#774
everpcpc merged 11 commits into
databendlabs:mainfrom
everpcpc:feat/key-pair-auth

Conversation

@everpcpc
Copy link
Copy Markdown
Member

@everpcpc everpcpc commented May 7, 2026

Problem

Key-pair authentication support needs more than unit-level JWT signing coverage: BendSQL should prove it can authenticate against a Databend server that actually supports key_pair users. The existing CI already resolves the latest Databend *-nightly release tag for integration tests, which keeps the tested server version visible in the GitHub summary and ensures key-pair server-side support is available.

Approach

Implement JWT-based key-pair auth in the shared client path and wire it through both DSN options and BendSQL CLI flags. The client sends a bearer JWT plus X-DATABEND-AUTH-METHOD: keypair, and key-pair auth is reloadable so retry paths can re-sign a fresh token after authentication failures.

The integration suite now creates a real Databend user with IDENTIFIED WITH key_pair, then validates BendSQL authentication through both --private-key-file and DSN private_key_file. CI keeps the previous concrete nightly release resolution instead of switching to the floating nightly tag, so the exact Databend version remains visible.

Scope

  • Add KeyPairAuth support for RSA, ECDSA P-256, Ed25519, and encrypted PKCS#8 private keys
  • Support private_key_file and private_key_passphrase_file DSN parameters
  • Add --private-key-file and --private-key-passphrase-file BendSQL CLI flags
  • Send X-DATABEND-AUTH-METHOD: keypair with key-pair bearer JWT requests
  • Add client-side coverage for DSN key-pair request wrapping
  • Add BendSQL HTTP integration coverage against a real Databend key_pair user

Testing

  • cargo fmt --all -- --check
  • cargo test -p databend-client keypair_auth_rsa --lib
  • cargo test -p databend-client parse_dsn_with_private_key_uses_keypair_auth --lib
  • cargo clippy -p databend-client -p bendsql --all-targets --all-features -- -D warnings
  • cargo build --bin bendsql
  • bash -n cli/tests/http/08-keypair-auth.sh
  • git diff --check

Docker-based integration tests were not run locally because the Docker daemon is unavailable in this environment; CI runs the BendSQL integration suite with the resolved Databend nightly release tag.

@everpcpc everpcpc force-pushed the feat/key-pair-auth branch 3 times, most recently from 6ccd6ad to a5fa36e Compare May 7, 2026 11:55
Implement JWT-based key-pair authentication as per the key-pair-auth RFC.

- Support RSA, ECDSA (ES256), and Ed25519 private keys
- Support encrypted PKCS#8 private keys with passphrase
- Handle PKCS#1, SEC1, and PKCS#8 key formats
- Rebuild EC PKCS#8 with named curve for Ring compatibility
- Extract PKCS#1 DER from PKCS#8 for RSA Ring compatibility
- Add --private-key-file and --private-key-passphrase-file CLI flags
- Send X-DATABEND-AUTH-METHOD: keypair header with Bearer token
@everpcpc everpcpc force-pushed the feat/key-pair-auth branch 3 times, most recently from 6601f84 to c963106 Compare May 19, 2026 07:08
Add real BendSQL CLI key-pair authentication coverage against Databend integration tests, including direct private key flag usage and DSN private_key_file usage.

Also mark key-pair auth reloadable so retry paths can re-sign requests with a fresh JWT.
@everpcpc everpcpc force-pushed the feat/key-pair-auth branch from c963106 to 41469e2 Compare May 19, 2026 07:14
@everpcpc everpcpc marked this pull request as ready for review May 19, 2026 07:58
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 41469e26c3

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/src/client.rs
Comment thread core/src/auth.rs Outdated
Use the effective default username for key-pair DSNs without an explicit user so generated JWT subjects do not become empty.

Reject unsupported EC private key curves instead of advertising ES256 for non-P-256 keys, covering PKCS#8, encrypted PKCS#8, and SEC1 inputs.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 73c3eac9f0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread cli/src/main.rs
Comment thread core/src/auth.rs Outdated
Reject key-pair DSNs without an explicit username instead of defaulting to root, since server-side key-pair authentication requires a configured key-pair user and root is not a valid implicit key-pair fallback.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 41736e3428

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/src/auth.rs Outdated
Comment thread cli/src/main.rs
everpcpc added 2 commits May 19, 2026 16:49
Match Databend server key-pair support by accepting P-384 EC private keys and signing JWTs with ES384. Keep rejecting unsupported EC curves such as P-521.
Reject key-pair authentication with FlightSQL because the FlightSQL path only supports password handshakes today.

Apply --user overrides before generating DSNs so --dsn plus --user plus --private-key-file works, and cover it in the BendSQL HTTP integration test.

Clarify passphrase support to encrypted PKCS#8 keys and fail fast for encrypted traditional PEM formats.
@everpcpc everpcpc requested review from sundy-li and youngsofun May 19, 2026 09:32
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 09a216d9c5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread cli/src/main.rs
Comment thread core/src/auth.rs Outdated
Comment thread cli/src/args.rs
everpcpc added 2 commits May 19, 2026 18:21
Select the EC PRIVATE KEY block when SEC1 PEM files include an EC PARAMETERS block before the private key.

Reject key-pair parameters in the FlightSQL DSN parser so direct driver users get a clear unsupported-auth error.
When a CLI private key override is supplied without a matching passphrase override, remove any existing private_key_passphrase_file from the DSN/config arguments so unencrypted replacement keys are not treated as encrypted.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 7f8b935fd5

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/src/auth.rs Outdated
Comment thread core/src/auth.rs Outdated
Comment thread core/src/auth.rs Outdated
Preserve passphrase whitespace except for the trailing line ending from passphrase files.

Select PRIVATE KEY and ENCRYPTED PRIVATE KEY blocks from PEM bundles before decoding, matching the SEC1 block-selection behavior.
Copy link
Copy Markdown

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 5d32827110

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread core/src/auth.rs Outdated
Comment thread cli/src/main.rs
@everpcpc everpcpc merged commit f0c05c1 into databendlabs:main May 19, 2026
42 checks passed
@everpcpc everpcpc deleted the feat/key-pair-auth branch May 19, 2026 12:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants