Repository navigation
Add workload identity federation (workload_identity option and reducto.auth helpers) - #125
Conversation
…o.auth helpers) Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
|
I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".
Original prompt from Devin Bot
|
…3, connection errors) Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
There was a problem hiding this comment.
✅ Devin resolved the finding on 4c9568c
Fixed by Devin (1)
- Base URL changes leave exchanges behind
Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
The /oauth/token exchange now uses the client's own httpx client (sync and async), so custom proxies, CA bundles and transports apply to it. Redirects stay off and retries keep the client's max_retries and backoff. The async exchange is now async-native with an anyio lock for single-flight. Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
…load-identity Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> # Conflicts: # CHANGELOG.md
Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
There was a problem hiding this comment.
Devin Review found 3 findings. Devin automatically fixed all 3 findings.
Fixed by Devin (3)
- Custom HTTP settings break token exchange — Fixed by the Devin session
- Base URL changes leave exchanges behind — Fixed by the Devin session
- Exchange retries ignore HTTP-date delays — A 429 with
Retry-After: <HTTP date>used to fall back to the exponential delay._retry_delaynow parses the HTTP date likeBaseClient._parse_retry_after_header. Covered by test_slow_down_honors_retry_after_http_date.
Summary
Adds workload identity federation (WIF). A workload sends a platform JWT to
<base_url>/oauth/tokenand gets a short-lived Reducto access token. No long-lived API key is needed. The shape copies the OpenAI SDK (openai.auth). The code is written by hand in lib code, not generated, soscripts/spec_drift.pystill passes. API-key behavior does not change.How it works
WorkloadIdentityAuth(anhttpx.Auth) is returned from_custom_auth()for requests withskippable_http_bearer._auth_headerssends a placeholderAuthorization. The auth flow replaces it with the cached token, so a user-suppliedAuthorizationheader still wins.grant_type,subject_token_type(jwt→...:jwt,id→...:id_token),subject_token,identity_provider_id._post_with_retriesuses the same schedule as API requests. It retries connection errors, 408, 409, 429 and >=500 (and obeysx-should-retry) up to the client'smax_retries. The backoff is0.5 s * 2^nwith jitter, capped at 8 s, and aretry-after/retry-after-msof 60 s or less is used as given. After the last retry it raisesOAuthError. So a 503temporarily_unavailableor an edge 429slow_downgivesmax_retries + 1exchange attempts per API call, never a tight loop. A 400 is not retried.threading.Condition; async tasks through ananyio.Lock. Other callers wait, or keep using the old token until it expires. Refresh happens atexpires_in - min(refresh_buffer_seconds or 1200, expires_in / 2). The async exchange is async-native (only the sync token provider runs into_thread), so the event loop does not block.self._client, sync or async), so a customhttp_client=(proxy, CA bundle, transport) applies to/oauth/tokentoo.follow_redirects=Falseper request, timeout 60 s (same as the Node SDK).max_retries).copy()/with_options()reuse the parent's token cache when the identity, base URL,max_retriesandhttp_clientdo not change.resolve_credentials):api_key=→workload_identity=→REDUCTO_API_KEY→REDUCTO_IDENTITY_PROVIDER_ID+ (REDUCTO_IDENTITY_TOKEN_FILE, re-read on every exchange, orREDUCTO_IDENTITY_TOKEN). Passing both arguments raisesReductoError.OAuthError(APIStatusError)with.error/.error_descriptionfor any non-2xx exchange response, andSubjectTokenProviderError(ReductoError).reducto.authhelpers:identity_token_file,k8s_service_account_token_provider,gcp_id_token_provider(token typeid),azure_managed_identity_token_provider,github_actions_token_provider,aws_web_identity_token_provider(imports boto3 when called, or takessts_client=).Changes to
_base_client.pyReductoErrorbeforeexcept Exception. Without this, anOAuthErrorraised inside the auth flow would be wrapped asAPIConnectionErrorand retried a second time by the outer loop.custom_authproperty. It now callsself._custom_auth(options.security), as the sync loop does. Before this fix the async client never applied a custom auth. API-key requests returnNonefrom_custom_auth, as before.Choices where the spec is open (I picked the option closest to OpenAI)
resourceishttps://management.azure.com/, as in OpenAI. Most users will pass their own app ID URI.REDUCTO_IDENTITY_TOKEN_FILEandREDUCTO_IDENTITY_TOKENare set, the file wins. IfREDUCTO_IDENTITY_PROVIDER_IDis set without either one, the client raises an error.expires_infrom the response.expires_atis ignored.trust_env=False, so proxy env vars do not catch the link-local metadata calls. GitHub Actions keepstrust_env.duration_secondsunset (AWS default is 300 s). Because the access token cannot outlive the subject token, the docstring and README tell users to set it.Tests:
tests/test_workload_identity.pyuses respx. It covers the exchange body,idtoken type, EU base URL, caching acrosswith_options, refresh timing with a fake clock, 10 threads and 10 tasks sharing one exchange,OAuthErroron 400 (not retried), 503 retried with backoff then raised, 429 withretry-after, 429 aftermax_retries,max_retries=0, connection errors, async 429 backoff, a customhttp_client(sync and async, mock transport) receiving the exchange, redirects not followed, provider errors, explicit header override, the 401 retry (sync and async), env vars (file re-read, static token), precedence,copy(), and every helper../scripts/lint(ruff, pyright, mypy),./scripts/test(including Pydantic v1) andscripts/spec_drift.pypass. CHANGELOG has an Unreleased entry. The version is not bumped.README: new "Workload identity federation" section with one example per helper.
Link to Devin session: https://app.devin.ai/sessions/9f1d4f341680422993c4f289b1948a89
Open in Devin Desktop: https://app.devin.ai/desktop/session/9f1d4f341680422993c4f289b1948a89?variant=devin
Requested by: @raunakdoesdev
🟢 Devin fixed all 3 findings · Follow Devin's work
Need help on this PR? Tag
@codesmith-botwith what you need.