Skip to content

Conversation

@jinnovation
Copy link
Contributor

@jinnovation jinnovation commented Dec 19, 2025

Contributes to #4365.

This PR adds an optional column expires_at TIMESTAMPTZ to the
tensorzero_auth_api_key table. When authenticating, if the API key has expires_at set, the key is rejected if the current time exceeds the expiration time.

At the same time, we extend the gateway CLI such that we can optionally provide a datetime argument to --create-api-key to set the expiration time for the created API key; if not provided, this defaults to "infinite" expiration.

Follow-up tasks:

  • UI changes, e.g. displaying the expiration time, greying out expired API keys, etc.

Important

Add support for API key expiration, including database changes, CLI updates, middleware adjustments, and tests.

  • Database:
    • Add expires_at TIMESTAMPTZ column to tensorzero_auth_api_key table.
    • Update queries in query-4cf7253a341ad5783871470cfda9cbe56d7b2b225359837ed4dfb08771eb53e7.json, query-6a67bd25a67a31bddb65330de2b21ae20fb4d201300d2e56ed8c0f1de2d490ae.json, and query-97c1ae769f9c03c5eda14cde29d338153023b36becd12a4a1c708b2058e7462a.json to handle expires_at.
  • CLI:
    • Add --expiration option to --create-api-key command in cli.rs and main.rs.
    • Validate expiration date is in the future in handle_create_api_key() in main.rs.
  • Middleware:
    • Update tensorzero_auth_middleware() in middleware.rs to check for expired API keys.
  • Tests:
    • Add tests for API key expiration in auth.rs and db/mod.rs.
    • Ensure expired keys are handled correctly in authentication and relay scenarios.
  • Documentation:
    • Update set-up-auth-for-tensorzero.mdx to include instructions for setting API key expiration.

This description was created by Ellipsis for 29e5e71. You can customize this summary. It will automatically update as commits are pushed.

@jinnovation jinnovation changed the title Add optional expires_at col to API key table Add partial support for API key expiration Dec 22, 2025
@jinnovation jinnovation marked this pull request as draft December 22, 2025 12:05
@jinnovation jinnovation changed the title Add partial support for API key expiration Add foundational support for API key expiration Dec 22, 2025
@jinnovation jinnovation marked this pull request as ready for review December 22, 2025 16:49
Copy link
Member

@GabrielBianconi GabrielBianconi left a comment

Choose a reason for hiding this comment

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

Almost there!

You can optionally set an expiration date and time for the new API key by providing the UTC timestamp as an argument, like so:

```bash
docker compose run --rm gateway --create-api-key "2025-12-20 23:00:00.000000 UTC"
Copy link
Member

@GabrielBianconi GabrielBianconi Dec 24, 2025

Choose a reason for hiding this comment

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

Could you make the argument an independent flag? We'll likely want to add additional arguments in the future (e.g. RBAC).

... --create-api-key --expiration "2025-12-20 23:00:00.000000 UTC"

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Let me know what you think of the approach I took here. Basically: --expiration is on the same level as --create-api-key and --disable-api-key, but requires the former to be present as well if it's set.

This is a little inelegant since --expiration is now a separate entry in the EarlyExitCommands collection. That said, this feels like simplest path forward, short of creating a new sub-command entirely for --expiration to live under, e.g. gateway create-api-key --expiration blablabla.

"my_org",
"my_workspace",
None,
Some(Utc::now() - Duration::seconds(2)),
Copy link
Member

Choose a reason for hiding this comment

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

Let's do something like now + 10ms and sleep 10ms to account for the error check above.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done elsewhere, i.e. at the gateway-auth integration test level. Had to do a 2s wait for some reason.

I think we can leave this specific test as-is, since our ability to inject directly into the test database means we can bypass the CLI-level checks for an expiration timestamp that's in the future. That said, I'm happy to take the same wait-based approach here if you'd prefer, for consistency.

@jinnovation jinnovation marked this pull request as draft December 29, 2025 22:43
jinnovation and others added 8 commits December 29, 2025 18:16
```
> ./target/debug/gateway --expiration "2025-12-20 23:00:00.000000 UTC"
error: the following required arguments were not provided:
  --create-api-key
```

```
> ./target/debug/gateway --create-api-key --expiration "2025-12-20 23:00:00.000000 UTC"
2025-12-30T00:34:59.998179Z ERROR gateway: Failed to create API key: Expiration datetime needs to be in the future
```
…ation' into 4365/optional-api-key-expiration
@jinnovation jinnovation marked this pull request as ready for review December 30, 2025 15:35
@jinnovation
Copy link
Contributor Author

@GabrielBianconi: This is ready for your team's review again whenever convenient.

@GabrielBianconi GabrielBianconi self-assigned this Jan 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

trusted-external-pr Automatically approves all workflow runs from this external PR.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants