Skip to content

Add workload identity federation (workload_identity option and reducto.auth helpers) - #125

Merged
devin-ai-integration[bot] merged 8 commits into
mainfrom
devin/1793326808-workload-identity
Nov 14, 2026
Merged

devin-ai-integration[bot] merged 8 commits into
mainfrom
devin/1793326808-workload-identity

Conversation

@devin-ai-integration

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

Copy link
Copy Markdown
Contributor

Summary

Adds workload identity federation (WIF). A workload sends a platform JWT to <base_url>/oauth/token and 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, so scripts/spec_drift.py still passes. API-key behavior does not change.

from reducto import Reducto
from reducto.auth import k8s_service_account_token_provider

client = Reducto(workload_identity={"identity_provider_id": "wip_...", "provider": k8s_service_account_token_provider()})

How it works

  • WorkloadIdentityAuth (an httpx.Auth) is returned from _custom_auth() for requests with skippable_http_bearer. _auth_headers sends a placeholder Authorization. The auth flow replaces it with the cached token, so a user-supplied Authorization header still wins.
  • Exchange body (RFC 8693 JSON): grant_type, subject_token_type (jwt → ...:jwt, id → ...:id_token), subject_token, identity_provider_id.
  • Exchange retries: _post_with_retries uses the same schedule as API requests. It retries connection errors, 408, 409, 429 and >=500 (and obeys x-should-retry) up to the client's max_retries. The backoff is 0.5 s * 2^n with jitter, capped at 8 s, and a retry-after/retry-after-ms of 60 s or less is used as given. After the last retry it raises OAuthError. So a 503 temporarily_unavailable or an edge 429 slow_down gives max_retries + 1 exchange attempts per API call, never a tight loop. A 400 is not retried.
  • Cache: one in-flight exchange. Sync callers share it through a threading.Condition; async tasks through an anyio.Lock. Other callers wait, or keep using the old token until it expires. Refresh happens at expires_in - min(refresh_buffer_seconds or 1200, expires_in / 2). The async exchange is async-native (only the sync token provider runs in to_thread), so the event loop does not block.
  • Transport: the exchange is sent through the client's own httpx client (self._client, sync or async), so a custom http_client= (proxy, CA bundle, transport) applies to /oauth/token too. follow_redirects=False per request, timeout 60 s (same as the Node SDK).
  • On a 401 from the API: invalidate the token, exchange once more, retry once (inside the auth flow, separate from max_retries).
  • copy()/with_options() reuse the parent's token cache when the identity, base URL, max_retries and http_client do not change.
  • Credential precedence (resolve_credentials): api_key= → workload_identity= → REDUCTO_API_KEY → REDUCTO_IDENTITY_PROVIDER_ID + (REDUCTO_IDENTITY_TOKEN_FILE, re-read on every exchange, or REDUCTO_IDENTITY_TOKEN). Passing both arguments raises ReductoError.
  • New errors: OAuthError(APIStatusError) with .error / .error_description for any non-2xx exchange response, and SubjectTokenProviderError(ReductoError).
  • reducto.auth helpers: identity_token_file, k8s_service_account_token_provider, gcp_id_token_provider (token type id), azure_managed_identity_token_provider, github_actions_token_provider, aws_web_identity_token_provider (imports boto3 when called, or takes sts_client=).

Changes to _base_client.py

  • The request loops re-raise ReductoError before except Exception. Without this, an OAuthError raised inside the auth flow would be wrapped as APIConnectionError and retried a second time by the outer loop.
  • The async loop used the custom_auth property. It now calls self._custom_auth(options.security), as the sync loop does. Before this fix the async client never applied a custom auth. API-key requests return None from _custom_auth, as before.

Choices where the spec is open (I picked the option closest to OpenAI)

  • The Azure helper's default resource is https://management.azure.com/, as in OpenAI. Most users will pass their own app ID URI.
  • If both REDUCTO_IDENTITY_TOKEN_FILE and REDUCTO_IDENTITY_TOKEN are set, the file wins. If REDUCTO_IDENTITY_PROVIDER_ID is set without either one, the client raises an error.
  • The token is cached for expires_in from the response. expires_at is ignored.
  • The GCP and Azure metadata requests use trust_env=False, so proxy env vars do not catch the link-local metadata calls. GitHub Actions keeps trust_env.
  • The AWS helper leaves duration_seconds unset (AWS default is 300 s). Because the access token cannot outlive the subject token, the docstring and README tell users to set it.
  • If a request body is a one-shot iterator, it cannot be replayed on the 401 retry. JSON bodies and seekable multipart files can.

Tests: tests/test_workload_identity.py uses respx. It covers the exchange body, id token type, EU base URL, caching across with_options, refresh timing with a fake clock, 10 threads and 10 tasks sharing one exchange, OAuthError on 400 (not retried), 503 retried with backoff then raised, 429 with retry-after, 429 after max_retries, max_retries=0, connection errors, async 429 backoff, a custom http_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) and scripts/spec_drift.py pass. 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

Devin Review


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

…o.auth helpers)

Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
@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: implement workload identity federation in the Python SDK, repo reductoai/reducto-python-sdk.

Context: Reducto is adding workload identity federation (WIF), modeled on OpenAI (https://developers.openai.com/api/docs/guides/workload-identity-federation and https://developers.openai.com/api/reference/workload-identity-federation; append .md to any OpenAI docs URL for markdown) and Anthropic (https://platform.claude.com/docs/en/manage-claude/workload-identity-federation). The full spec, including the exact wire contract in Appendix A, is pasted below. I (the parent session) am building the server side in reductoai/api in parallel: the /oauth/token exchange endpoint and the /workload-identity/* management API. Build strictly against the contract in the spec. If the contract is ambiguous, pick the option closest to OpenAI behavior and say so in the PR description. Rules: open the PR as ready for review (never draft). Do not merge. Do not deploy anything to production. Do not message anyone in Slack. Keep code minimal and clean, follow the repo conventions (read AGENTS.md / CLAUDE.md / CONTRIBUTING.md first), run the repo lint, typecheck and tests before you push. When done, reply with the PR URL and a 3-line summary, then stop.

Deliverable: one PR in reductoai/reducto-python-sdk that adds:

  • A workload_identity= option to Reducto and AsyncReducto (TypedDict shape like OpenAI: identity_provider_id, provider (token_type "jwt"|"id", get_token callable), refresh_buffer_seconds optional). Exchange at &lt;base_url&gt;/oauth/token (RFC 8693 JSON body per spec 2.6). Thread-safe token cache with a single in-flight exchange, refresh buffer min(1200s, lifetime/2), async variant does not block the event loop, on a 401 from an API call invalidate the cached token, exchange once more and retry once.
  • Env vars: REDUCTO_IDENTITY_PROVIDER_ID plus REDUCTO_IDENTITY_TOKEN_FILE (re-read on every exchange) or REDUCTO_IDENTITY_TOKEN. Precedence exactly as spec 2.10. Clear error when c... (17159 chars truncated...)

devin-ai-integration Bot and others added 2 commits October 30, 2026 07:14
…3, connection errors)

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

@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 resolved the finding on 4c9568c

Fixed by Devin (1)

  • Base URL changes leave exchanges behind

View all findings in Devin Review

Devin Review

Co-Authored-By: Raunak Chowdhuri <raunak@reducto.ai>
devin-ai-integration[bot]

This comment was marked as resolved.

devin-ai-integration Bot and others added 4 commits October 30, 2026 07:44
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>

@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 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_delay now parses the HTTP date like BaseClient._parse_retry_after_header. Covered by test_slow_down_honors_retry_after_http_date.

View findings in Devin Review · Follow Devin's work

Devin Review

@devin-ai-integration
devin-ai-integration Bot merged commit 6ef1a5d into main Nov 14, 2026
9 checks passed
@devin-ai-integration devin-ai-integration Bot mentioned this pull request Nov 14, 2026
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