Repository navigation
Add X.509 workload identity (mTLS client certificate) to the Python SDK - #126
devin-ai-integration[bot] wants to merge 7 commits into
Conversation
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
Original prompt from Devin Bot
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
…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>
ffd35c5 to
868de95
Compare
Summary
Based on
main, after #125 (OIDC WIF v1) shipped in 0.27.0 (#131). The branch was rebuilt frommainwith 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.Exchange:
POST <base_url>/oauth/tokenwithsubject_token_type=urn:reducto:params:oauth:token-type:x509. It sends nosubject_token: the client presents its certificate in the TLS handshake. It reuses Add workload identity federation (workload_identity option and reducto.auth helpers) #125'sWorkloadIdentityAuth: the token cache, the scheduled refresh, the single 401 re-exchange and the retry loop. JWT exchanges go through the client'shttp_client, as in Add workload identity federation (workload_identity option and reducto.auth helpers) #125. An X.509 exchange uses a short-livedhttpx.Client/httpx.AsyncClientwithverify=<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:productionhttps://mtls.platform.reducto.aieuhttps://mtls.eu.platform.reducto.aiauhttps://mtls.au.platform.reducto.aiAn explicit
base_urlorREDUCTO_BASE_URLis kept.copy()/with_options()re-derives the host fromenvironmentonly whilebase_urlstill 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 innerhttpx.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.SSLContextis published withuse()only after the exchange succeeds.not_after, and the server rejects certificates within 5 s ofnot_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_CHAINandREDUCTO_MTLS_KEYmust be set together. Setting them together withREDUCTO_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
http_clientwith x509 raisesReductoError. The SDK cannot add a client certificate to a caller's httpx client. The inner client keepstrust_env, soHTTPS_PROXY,NO_PROXYandSSL_CERT_FILEstill apply. OpenAI's SDK takes no certificate material: the caller configures the certificate on their own transport. The spec asks forcert_chain/keypaths, so this PR follows the spec.passwordoption, so there is none.Tests
tests/test_workload_identity_x509.py:copy(): API key ↔ X.509, chainedwith_options, a reassignedbase_url, and an explicit standard host.CERT_REQUIREDand keep-alive records the leaf fingerprint per request. It rejects a token presented with a different leaf. The tests check that:The tests generate certs with the
opensslCLI and are skipped if it is not installed. Local runs of./scripts/lint(ruff, pyright, mypy),./scripts/test(pydantic v2 and v1),uv buildandscripts/spec_drift.pypass. In CI, lint, test and build run only on thepushevent (if: github.event_name == 'push' || fork), so they show as skipped on thepull_requestevent.Review checklist
api_key,REDUCTO_API_KEYor the auth headers changed._base_client.py,resources/andtypes/are untouched.client_certificate(...) is not None. They keep Add workload identity federation (workload_identity option and reducto.auth helpers) #125's exchange body,http_clientexchange path, retries and default HTTP client.copy()gives the same base URL as before, because without a switch the environment default resolves to the same host.WorkloadIdentity["provider"]is now aUnion, so typed code that readsprovider["get_token"]has to narrow ontoken_typefirst.ReductoErrorsubclasses.copy()host selection on an X.509 switch went through three rounds. It now comparesbase_urlwith the default it was built from.Authorizationon cross-origin redirects, and the token is key-bound.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
Need help on this PR? Tag
@codesmith-botwith what you need.