Skip to content

Add X.509 workload identity (mTLS client certificate) to the Python SDK - #126

Open
devin-ai-integration[bot] wants to merge 7 commits into
mainfrom
devin/1793329155-x509-workload-identity
Open

devin-ai-integration[bot] wants to merge 7 commits into
mainfrom
devin/1793329155-x509-workload-identity

Conversation

@devin-ai-integration

@devin-ai-integration devin-ai-integration Bot commented Oct 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Based on main, after #125 (OIDC WIF v1) shipped in 0.27.0 (#131). The branch was rebuilt from main with the X.509 commits cherry-picked; the code matches the earlier stacked head. This PR implements spec section 5 (Python) of the X.509 workload identity federation series.

from reducto.auth import x509_workload_identity

client = Reducto(workload_identity={
    "identity_provider_id": "wip_...",
    "provider": x509_workload_identity(cert_chain="chain.pem", key="key.pem"),
})
# or REDUCTO_IDENTITY_PROVIDER_ID + REDUCTO_MTLS_CERT_CHAIN + REDUCTO_MTLS_KEY
  • Exchange: POST <base_url>/oauth/token with subject_token_type=urn:reducto:params:oauth:token-type:x509. It sends no subject_token: the client presents its certificate in the TLS handshake. It reuses Add workload identity federation (workload_identity option and reducto.auth helpers) #125's WorkloadIdentityAuth: the token cache, the scheduled refresh, the single 401 re-exchange and the retry loop. JWT exchanges go through the client's http_client, as in Add workload identity federation (workload_identity option and reducto.auth helpers) #125. An X.509 exchange uses a short-lived httpx.Client / httpx.AsyncClient with verify=<the just-loaded SSLContext>, because the client's transport presents the current certificate, and a rotated certificate must only be published (use()) after it gets a token.

  • base_url: with x509, the default comes from MTLS_ENVIRONMENTS. These are the exact hosts that the edge PR (reductoai/api#13503) routes:

    environment mTLS host
    production https://mtls.platform.reducto.ai
    eu https://mtls.eu.platform.reducto.ai
    au https://mtls.au.platform.reducto.ai

    An explicit base_url or REDUCTO_BASE_URL is kept.

    • copy() / with_options() re-derives the host from environment only while base_url still equals the environment default it was built from (_default_base_url). So a copy that switches to or from X.509 gets the matching host, also after chained copies. An explicit or reassigned base URL is kept.
  • Client certificate on every request (sync and async): httpx has no per-request client certificate. The client therefore builds its own httpx client. Its transport (SyncClientCertificateTransport / AsyncClientCertificateTransport) forwards each request to an inner httpx.Client(verify=ssl_context).

  • Rotation:

    • ClientCertificate.load() re-reads both files on every exchange, both on the scheduled refresh and on a 401 re-exchange. Requests do not read the files.
    • When the files changed, the new SSLContext is published with use() only after the exchange succeeds.
    • The transport then switches to a new inner client and connection pool. The old pool is closed after its last in-flight response closes.
    • Why: tokens are certificate-bound (RFC 8705). A pooled keep-alive connection that still presents the old leaf would get a 401 with the new token.
    • Tokens end at the leaf's not_after, and the server rejects certificates within 5 s of not_after. The scheduled refresh runs at least half a token lifetime before expiry, so it picks up rotated files before then.
  • Env: REDUCTO_MTLS_CERT_CHAIN and REDUCTO_MTLS_KEY must be set together. Setting them together with REDUCTO_IDENTITY_TOKEN(_FILE) is an error.

  • Errors when the client reads or loads the cert or key raise SubjectTokenProviderError.

  • CHANGELOG.md: adds an X.509 entry under a new "Unreleased" section above 0.27.0. The version is not bumped.

Decisions where the spec is open

  • A custom http_client with x509 raises ReductoError. The SDK cannot add a client certificate to a caller's httpx client. The inner client keeps trust_env, so HTTPS_PROXY, NO_PROXY and SSL_CERT_FILE still apply. OpenAI's SDK takes no certificate material: the caller configures the certificate on their own transport. The spec asks for cert_chain / key paths, so this PR follows the spec.
  • Key: unencrypted PEM only. The spec has no password option, so there is none.

Tests

tests/test_workload_identity_x509.py:

  • respx tests:
    • The exchange body.
    • The regional hosts.
    • The base_url override.
    • The env vars.
    • copy(): API key ↔ X.509, chained with_options, a reassigned base_url, and an explicit standard host.
    • File and key errors.
  • Real mutual-TLS tests, sync and async. A local HTTPS server with CERT_REQUIRED and keep-alive records the leaf fingerprint per request. It rejects a token presented with a different leaf. The tests check that:
    • The exchange and the API calls present the cert.
    • Rotated files are not read per request.
    • After a re-exchange (401 path and scheduled refresh), the exchange and the API calls present the new leaf.

The tests generate certs with the openssl CLI and are skipped if it is not installed. Local runs of ./scripts/lint (ruff, pyright, mypy), ./scripts/test (pydantic v2 and v1), uv build and scripts/spec_drift.py pass. In CI, lint, test and build run only on the push event (if: github.event_name == 'push' || fork), so they show as skipped on the pull_request event.

Review checklist

  • Rolling deploy: N/A (client SDK).
  • Backwards compatibility:
  • Impact: only clients that configure x509 get the mTLS base_url and HTTP client.
  • Antipatterns: no broad catches beyond Add workload identity federation (workload_identity option and reducto.auth helpers) #125's subject-token wrapper. Errors are ReductoError subclasses.
  • Correctness:
    • Self-review fixed an async pool-swap race.
    • Devin Review: the copy() host selection on an X.509 switch went through three rounds. It now compares base_url with the default it was built from.
    • Dismissed: "certificate presented to redirect targets". A server only gets the cert if it sends a CertificateRequest, and TLS client auth cannot be replayed. httpx drops Authorization on cross-origin redirects, and the token is key-bound.
  • On-prem and regional: tests cover the mTLS hosts for production, eu and au. On-prem sets base_url, which the client keeps.

Link to Devin session: https://app.devin.ai/sessions/e5b7c17091f3496a9cf3202775fa1e1a
Link to Bot: https://app.devin.ai/org/reducto/bots/9b23f8f844124bfd9469b9f27d2e6638
Open in Devin Desktop: https://app.devin.ai/desktop/session/e5b7c17091f3496a9cf3202775fa1e1a?variant=devin


🟢 Devin Review found nothing to fix · View findings · Follow Devin's work

Devin Review


View with [code]smith Autofix with [code]smith
Need help on this PR? Tag @codesmith-bot with what you need.

@devin-ai-integration

Copy link
Copy Markdown
Contributor Author

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

Original prompt from Devin Bot

Task X3, repo reductoai/reducto-python-sdk, base branch = the head branch of PR #125. Implement spec section 5 (Python): reducto.auth.x509_workload_identity(cert_chain, key), the env vars REDUCTO_MTLS_CERT_CHAIN / REDUCTO_MTLS_KEY, the exchange with subject_token_type urn:reducto:params:oauth:token-type:x509 and no subject_token, switching base_url to the mTLS host for the region unless base_url is set explicitly, the client cert on every request (sync and async), and re-reading the files on each re-exchange. Add unit tests and a README section. Reuse the existing WIF token cache and refresh logic from #125.

You are a child of a lead Devin session. Reducto is adding X.509 workload identity federation as a stacked PR series on top of the OIDC WIF v1 PRs (api: https://github.com/reductoai/api/pull/13498, docs: https://github.com/reductoai/api/pull/13497, python: #125, node: reductoai/reducto-node-sdk#89). The lead writes the API side (X1) in parallel, strictly to the spec below. Build strictly against this spec. If it is ambiguous, pick the option closest to OpenAI's behavior and say so in the PR description. Rules: open the PR as ready for review (never draft), base it on the branch named below so it is stacked, do not merge, do not deploy or apply anything to production, do not message anyone in Slack. Keep the code minimal and clean, follow the repo conventions (read AGENTS.md / CLAUDE.md first), and run the repo lint, typecheck and tests before you push. When done, reply with the PR URL and a 3-line summary, then stop.

=== SPEC ===
# X.509 workload identity federation (stacked on OIDC WIF v1)

Parity target: OpenAI X.509 WIF + Mutual TLS (developers.openai.com/api/docs/guides/workload-identity-federation/x509, /guides/mutual-tls). Differences are marked DIFF.

#``# 1. Flow

  1. Org Owner/Admin uploads a PEM trust anchor (root or stable intermediate CA) in Studio > Settings > Security > Mutua... (6554 chars truncated...)

@devin-ai-integration devin-ai-integration Bot left a comment •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Note

Newer findings are available below. Devin Review posted a newer report on this PR, in addition to the findings presented here.

✅ Devin resolved all 4 findings on 221449e

Fixed by Devin (4)

  • Chained copies retain the wrong host
  • Custom host lost after URL reassignment
  • Explicit base URL changes on copy
  • Copied X.509 client uses non-mTLS host

View all findings in Devin Review

Devin Review

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Devin Review found 5 findings. Devin automatically fixed all 5 findings.

Fixed by Devin (5)
  • Explicit base URL changes on copy — Fixed by the Devin session
  • Cancelled certificate rotation leaks connection pools — Moved await retired.retire() inside the try so a cancellation releases the new pool's slot; regression test cancels during the old pool's aclose and checks the new pool's count is 0 (fails before, passes after).
  • Copied X.509 client uses non-mTLS host — Fixed by the Devin session
  • Chained copies retain the wrong host — Fixed by the Devin session
  • Custom host lost after URL reassignment — Fixed by the Devin session

View findings in Devin Review · Follow Devin's work

Devin Review

@devin-ai-integration devin-ai-integration Bot mentioned this pull request Nov 14, 2026
devin-ai-integration Bot and others added 7 commits November 14, 2026 23:29
…nv, mTLS host, client cert on every request)

Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
…uled refresh

Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
… not by matching its text

Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
…he old one

Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
@devin-ai-integration
devin-ai-integration Bot changed the base branch from devin/1793326808-workload-identity to main November 14, 2026 23:30
@devin-ai-integration
devin-ai-integration Bot force-pushed the devin/1793329155-x509-workload-identity branch from ffd35c5 to 868de95 Compare November 14, 2026 23:30

This branch has not been deployed

No deployments
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