Skip to content

feat: enforce API key authentication on all data routes - #45

Merged
ajainvivek merged 2 commits into
mainfrom
feat/api-key-auth-enforcement
Mar 15, 2026
Merged

ajainvivek merged 2 commits into
mainfrom
feat/api-key-auth-enforcement

Conversation

@ajainvivek

Copy link
Copy Markdown
Contributor

Summary

  • API key auth middleware: All /v1/* routes now require a valid API key or master key when REASONDB_AUTH_ENABLED=true. Public paths (/health, /metrics, /swagger-ui, /api-docs) bypass auth so monitoring and docs stay accessible
  • FromRequestParts impl: AuthenticatedKey can be used as an axum extractor in route handlers directly
  • Terraform null_resource updater: terraform apply now SSHes into the running instance and hot-swaps the Docker container whenever reasondb_image, LLM config, or auth settings change — no need to taint/recreate the EC2 instance
  • New Terraform variables: auth_enabled (bool), master_key (sensitive), ssh_private_key_path

How auth works

# Blocked — no key
curl http://<ip>:4444/v1/tables
# → 401 {"error":"authentication_error","message":"API key required..."}

# Allowed — master key
curl -H "Authorization: Bearer rdb_master_..." http://<ip>:4444/v1/tables

# Allowed — scoped API key (created via POST /v1/auth/keys)
curl -H "Authorization: Bearer rdb_test_..." http://<ip>:4444/v1/query

Test plan

  • REASONDB_AUTH_ENABLED=false — all routes still work without a key (backward compatible)
  • REASONDB_AUTH_ENABLED=true, no key → 401 on any /v1/* route
  • REASONDB_AUTH_ENABLED=true, wrong key → 401
  • REASONDB_AUTH_ENABLED=true, master key → 200
  • REASONDB_AUTH_ENABLED=true, valid API key → 200
  • /health, /metrics, /swagger-ui accessible without any key when auth is enabled
  • terraform apply after changing reasondb_image in tfvars SSHes in and restarts the container

Add auth_middleware that intercepts every request when REASONDB_AUTH_ENABLED=true.
Public paths (/health, /metrics, /swagger-ui, /api-docs) bypass auth so monitoring
stays accessible. All /v1/* routes now require a valid API key or the master key.

- Add auth_middleware (axum from_fn_with_state) with header extraction for
  both Authorization: Bearer and X-API-Key
- Add FromRequestParts impl for AuthenticatedKey (async_trait, axum 0.7)
- Add async-trait dependency to reasondb-server
- Wire middleware into create_server before rate limiting
- Add auth_enabled and master_key Terraform variables
- Pass REASONDB_AUTH_ENABLED and REASONDB_MASTER_KEY through user_data.sh.tpl
Add a null_resource with remote-exec provisioner so terraform apply
updates the running Docker container whenever the image tag, LLM config,
or auth settings change — without recreating the EC2 instance.

Triggers on: reasondb_image, llm_provider, llm_model, llm_base_url,
auth_enabled, master_key hash, llm_api_key hash, instance_id.

Also adds ssh_private_key_path variable (default ~/.ssh/id_ed25519)
used by the provisioner SSH connection.
@ajainvivek
ajainvivek merged commit 08d4beb into main Mar 15, 2026
2 checks passed
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.

1 participant