Skip to content

feat(workload-identity): Add workload identity federation - #122

Draft
gjtorikian wants to merge 15 commits into
mainfrom
feat/workload-identity
Draft

gjtorikian wants to merge 15 commits into
mainfrom
feat/workload-identity

Conversation

@gjtorikian

Copy link
Copy Markdown
Contributor

Summary

  • Adds workload identity federation as a second way to authenticate, next to an API key. Start WorkOS.WorkloadIdentity.Credentials and pass it to WorkOS.Client.new(credentials: ...). The SDK exchanges the identity token that the workload's platform issues for a short-lived WorkOS access token, keeps it in memory and renews it before it expires. A CI job or a cloud workload then needs no stored API key.
  • Adds three provider packages that fetch the identity token on their platform: workos_workload_identity_github_actions, workos_workload_identity_gcp and workos_workload_identity_aws. A callback, a token file or an environment variable covers any other platform. The workos package gains no dependency: aws and aws_credentials are dependencies of the AWS package only.
  • The exchange follows one design shared by all nine backend SDKs: the token URL must be https, a failed exchange is retried like an API request, a 401 on an exchanged token is recovered once per request, and no token appears in an error or a log.
  • Releases: the repository becomes a release-please manifest with one entry per package, so the SDK and each provider get their own release pull request and publish job.
  • Tested with the 31 conformance cases that all nine SDKs share, and end to end with real identity tokens from GitHub Actions, Google Cloud and AWS.

Authenticate with the identity the platform already gives a workload,
instead of a stored API key. `WorkOS.Client.new(credentials: pid)` takes a
`WorkOS.WorkloadIdentity.Credentials` process, which exchanges the
workload's identity token for a short-lived access token (jwt-bearer),
caches it, and refreshes it before it expires.

SDK:
- Every client now authenticates through `WorkOS.Credentials`. Credentials
  are resolved before every HTTP attempt, including transport retries. On
  a 401 the rejected token generation is invalidated and the same request
  is sent once more. A static API key is the trivial implementation and
  behaves as before.
- `WorkOS.WorkloadIdentity.Credentials` runs at most one exchange at a
  time for all callers, bounded at 30 seconds. A caller that stops waiting
  does not stop the exchange. The exchange retries transport failures, 429
  and 5xx with the SDK's own retry count and backoff.
- `WorkOS.WorkloadIdentity.token_file/1` and `token_env/1` providers, and
  `WorkOS.WorkloadIdentityError` with a code, description and hint.
- Passing both `api_key:` and `credentials:` raises. With `credentials:`,
  `WORKOS_API_KEY` is ignored.

Provider packages, each its own Mix project with no dependency on workos:
- workload_identity_github_actions (workos_workload_identity_github_actions)
- workload_identity_gcp (workos_workload_identity_gcp)
- workload_identity_aws (workos_workload_identity_aws), on `aws` and
  `aws_credentials`

Release and CI:
- release-please entries for the three packages, starting at 0.1.0, with
  separate release PRs. release.yml publishes each package from its own
  tag; the SDK job now runs only for `v*` tags.
- script/ci, ci.yml and lint.yml build, lint and test each package.

The SDK does not list the provider packages as optional dependencies: Hex
rejects path dependencies when it builds a package, and the providers are
not on Hex yet.

Spec: docs/ideation/wif-backend-sdks/spec-phase-8.md
Review passed in 2 cycles.
release-please gives the root package every commit in the repo, so a fix
that only touches a provider package would also open an SDK release PR and
appear in the SDK changelog. `exclude-paths` on the root entry drops a
commit from the SDK release when every file it touches is under one of the
three provider directories.

A commit that touches a provider and anything outside it still counts
toward the SDK.

Spec: docs/ideation/wif-backend-sdks/spec-phase-8.md
The Google Cloud provider made its request on the default :httpc
profile, which is shared with the application. When the application had
given :httpc a proxy with :httpc.set_options/1, the request for an
identity token went to that proxy. The metadata server exists only
inside the workload's own VM or container, so a proxy cannot reach it:
the request failed, or the provider returned whatever the proxy
answered as the token.

The provider now starts an :httpc profile of its own on first use and
sends the request through it. Nothing sets a proxy on that profile. A
test gives the default profile a proxy and checks that it is never
contacted.

Spec: docs/ideation/wif-backend-sdks/spec-phase-8.md
The rule that the GCP metadata request never goes through a proxy is now
conformance case WIF-GCP-03. The conformance check reads case IDs from
test names, so the existing test for the rule now carries the ID. No
behavior changes.

Spec: docs/ideation/wif-backend-sdks/spec-phase-8.md
…hints with the server

A reading of the server code on 2026-11-13 showed three things the SDK
had slightly wrong.

The server binds a workload identity client to exactly one environment
and takes the environment from that binding, so the
`x-url-environment-id` header selects nothing. `:environment_id` is now
optional: starting credentials without it (and without
`WORKOS_ENVIRONMENT_ID`) is no longer an `invalid_configuration` error,
and the header is sent only when an environment ID is set. An empty
string counts as not set. `WORKOS_ENVIRONMENT_ID` still fills the option
when it is omitted, and an explicit value still wins. Setting it is a
safety check: a value that names a different environment gets
`403 environment_mismatch`.

The hints for `invalid_grant`, `invalid_target`, `invalid_client` and
`unauthorized_client` are reworded to match what the server returns each
code for. The other hints are unchanged.

The tests use the real scope name, `orgs:write`. WIF-02 gains part (b):
with no environment ID the API request carries the access token and no
environment header.

The README says that the server accepts an identity token for one
exchange, and what that means for `token_file/1` and `token_env/1`.

Spec: docs/ideation/wif-backend-sdks/shared-design.md (decisions 6, 7, 8)
…t the README examples

`:environment_id` now comes only from the caller's code. The SDK no
longer reads `WORKOS_ENVIRONMENT_ID`: left out, `nil` or an empty string
all mean "not set", so no `x-url-environment-id` header is sent and
nothing is an error. The header is an optional safety check, and a
variable left in the environment could turn it into a
`403 environment_mismatch` with no line of code to point at. The
variable was never on `main`, so removing it breaks no one.
`WORKOS_CLIENT_ID`, `WORKOS_WIF_TOKEN_URL` and `WORKOS_WIF_SCOPE` are
unchanged.

WIF-02 (b) now sets the variable and checks that the API request still
carries no environment header. WIF-22 (c) and (d) no longer involve the
environment ID.

The README and the module docs are corrected:

- The `token_file/1` examples no longer use a Kubernetes service account
  token path, which the server refuses. They name a file that another
  process writes, such as a sidecar or an earlier CI step.
- `token_file/1` and `token_env/1` suit a job shorter than four minutes,
  not five: the SDK exchanges again at four.
- The environment ID is described as optional and set in code.

Spec: docs/ideation/wif-backend-sdks/shared-design.md (decisions 9, 10)
…WIF_SCOPE

`WORKOS_CLIENT_ID` is now the only environment variable the credentials
read.

`:token_url` is required and comes only from the caller's code. Left
out, `nil` or an empty string is an `invalid_configuration` error when
the credentials start, with the description `token_url is required`.
`WORKOS_WIF_TOKEN_URL` in the environment is ignored.

`:scope` is optional and comes only from the caller's code. Not set,
`nil` or an empty string means no `scope` field in the exchange.
`WORKOS_WIF_SCOPE` is ignored. With no scope the token carries every
scope the application holds, so a variable left in the environment
would change permissions with no line of code to point at.

`:client_id` is unchanged: it still defaults from `WORKOS_CLIENT_ID`,
and its error still names that variable.

Neither variable was ever on `main`, so removing them breaks no one.

WIF-22 now sets all three variables and checks that only the client ID
is taken from the environment, that an explicit client ID wins, and
that a missing token URL is an error. The README and the module docs
name `WORKOS_CLIENT_ID` as the only variable, and the supervised
examples pass the token URL in code.

Spec: docs/ideation/wif-backend-sdks/shared-design.md (decision 11)
…eq 0.7

With Req 0.7.x installed, every request raised `FunctionClauseError` in
`WorkOS.Client.run_adapter/2`, for API-key clients as well. `mix.exs`
allows `{:req, "~> 0.5"}`, which a new install resolves to 0.7.5, while
`mix.lock` pinned 0.6.3, so the SDK's own tests passed.

The client resolves the credentials before every HTTP attempt: the
first try, each transport retry, each redirect and the 401 recovery. Up
to Req 0.6 the only thing Req runs again for the later attempts is the
adapter, a function, so the `workos_credentials` request step wrapped
it. Req 0.7.0 changed both halves of that:

- The adapter is a module (`Req.Finch`, `Req.Plug`), called as
  `mod.run/1`. The wrapper accepted only a function or an MFA tuple. A
  function adapter still works on 0.7 but warns on every request.
- `current_request_steps` is gone: every request step runs again for
  each attempt.

The step now picks one of two ways, by whether the request still has
`:current_request_steps`:

- Req 0.7 and later: the step authorizes the attempt itself and leaves
  the adapter alone, so the SDK sets no function adapter and causes no
  deprecation warning.
- Req 0.5 and 0.6: the step wraps the adapter, as before.

Both share one function that resolves the credentials and sets the
headers, or removes them for a redirect to another origin. The origin
of the first attempt moved from the wrapper's closure into the request's
private data. On Req 0.7 the step runs again after a redirect, and
reading the origin there would take the redirect target for it.

`mix.lock` now pins Req 0.7.5, the version a new install gets, so CI
tests it. Only the `req` entry changed.

New tests in `test/workos/client_adapter_test.exs` send requests through
the adapters Req itself provides: the default adapter against a
loopback server (`WorkOS.TestServer`) and the plug adapter. They check
that an API-key request works with no adapter chosen, that the
credentials are resolved exactly once per attempt across a retry, a 401
recovery and a redirect, that a redirect to another origin gets no
credential header, and that Req prints no warning. Until now every test
used a plug, and none counted resolves.

The suite passes on Req 0.5.13, 0.5.17, 0.5.18, 0.6.0, 0.6.3, 0.7.0 and
0.7.5.
`:token_url` must now be an absolute URL with the scheme `https` and a
host. The request to it carries the workload's identity token and the
response carries the access token. Until now the credentials checked
only that the option was present.

Anything else is refused when the credentials are started, with
`invalid_configuration` and the description
`token_url must be an absolute https URL`: an `http://` URL, a URL with
no scheme or no host, any other scheme. The scheme is compared without
regard to case, so `HTTPS://` is accepted. There is no exception for
`localhost` or loopback addresses, and nothing turns the check off: no
option, no application config, no environment variable. workos-php
already has the same rule.

The exchange also sets the URL again after the caller's `:req_options`.
A `:url` among them used to replace the token URL, which would have let
a documented option send the identity token somewhere the check never
saw.

No test needed the check relaxed. The tests reach the token endpoint
through a plug behind an https URL and never open a socket, and the
provider packages do not start credentials at all.

New tests cover the refused forms (http, http to localhost and to
loopback addresses, no host, no scheme, other schemes, malformed URLs),
upper-case `HTTPS://`, Req options that try to get around the check,
and Req options that try to move the exchange. The README and the
module docs say the token URL has to be https.
When the exchange POST got no answer, the error was the standard
sentence for `exchange_failed` and nothing else:

    exchange_failed: The token endpoint could not be reached or kept
    failing. Check the token URL and try again.

A reader could not tell a host name that does not resolve from a
refused connection, a timeout or a TLS failure. The exchange kept the
transport error in `:reason` but built the error with an empty
description, and the message is made of the code, the hint and the
description only.

The description now says where the request went and why it failed:

    the request to auth.example.com:443 failed: the host name could not
    be resolved (nxdomain)
    the request to auth.example.com:443 failed: the connection was
    refused (econnrefused)
    the request to auth.example.com:443 failed: it timed out (timeout)

Any other transport error, a TLS alert for example, is put in Req's own
words on one line. An exception that is not a transport error is named
by its module, which is all that `:reason` keeps of it as well.

Of the token URL, only the host and port are named, never the userinfo,
the path or the query. A transport error holds the socket's reason and
nothing from the request, so no token can get into the text.

Only the description changed. The hint, the code, `:reason`, the retry
count and the return shapes are as before.

New tests cover name resolution, a refused connection, a timeout and a
TLS alert, a token URL with userinfo, a port and a query, and a Req
error that is not a transport error.
The GitHub Actions provider gave a reason for a failed request only
when `:httpc` reported it as a bare atom on the connection attempt. A
TLS failure arrives as `{:tls_alert, {alert, text}}`, so an untrusted,
expired or wrong-host certificate all read the same:

    The GitHub Actions OIDC token endpoint could not be reached:
    connection failed.

A server that closed the connection read `request failed`.

The message now names the host and port and says what went wrong, in
the wording the token exchange uses:

    The GitHub Actions OIDC token endpoint at example.com:443 could not
    be reached: the server's certificate is not from a trusted
    certificate authority (unknown_ca).

- TLS: a certificate from an authority that is not trusted, one that
  has expired or is not valid yet, one for another host name, a
  self-signed one, and `the TLS handshake failed (<alert>)` for any
  other alert.
- Connection: the host name could not be resolved, the connection was
  refused, the request timed out, the server closed the connection.
  Any other socket reason stays `the connection failed (<atom>)`.

Nothing raw is put in the message. The words are fixed; the only terms
printed are the socket's reason when it is an atom, the name of a TLS
alert, and the host and port of the request URL. A TLS alert's text is
searched for `hostname_check_failed` and `selfsigned_peer`, because
OTP reports both under a general alert that differs by release (a
wrong host name is `handshake_failure` on OTP 25 and 26 and
`bad_certificate` from OTP 27). The text is never copied. OTP 25 gives
no word for a self-signed certificate, so there it reads
`the TLS handshake failed (bad_certificate)`.

The provider also refuses a request token that is not visible ASCII
(0x21 to 0x7E) before it makes any request, with a message that names
`ACTIONS_ID_TOKEN_REQUEST_TOKEN` and not its value. The token goes
into the `Authorization` header as it is, and `:httpc` sends a line
break in a header value unchanged, on OTP 25 through 29: a trailing
newline sent a broken header, and a line break followed by text sent
a header of its own. Nothing is stripped, since a token that needs
repair is not the runner's token. The token did not appear in an
error before either, but only because the raw reason was dropped: for
a non-ASCII value `:httpc` returns the whole header in the reason
from OTP 26, and on OTP 25 its handler crashes and the request waits
out the timeout.

`__describe__/1` is a test seam for the wording, like `__provider__`
in the other provider packages, because a timeout and a name that
does not resolve cannot be produced on a loopback port.

The tests start a TLS listener on a loopback port with certificates
made at test time by `:public_key.pkix_test_data/1`, and make the test
root the trust store with `:public_key.cacerts_load/1`. They cover a
trusted certificate, an untrusted authority, another host name, an
expired and a not-yet-valid certificate, a self-signed certificate, a
refused connection and a server that closes without answering. A
table covers every phrase, with a secret in the raw reason that must
not come out. Other tests check that only the host and port of the
URL are named, that tokens with a line break, a space, a control
character or a non-ASCII character reach no server, and that every
visible ASCII character is sent unchanged.
…alls

The sign-in calls on /user_management/authenticate and the SSO token
exchange on /sso/token send the API key as `client_secret`. A client
built with `credentials:` has no API key, so such a call sends the
request without one.

`WorkOS.Client.put_client_secret/3` is the one place that decides what
those requests carry:

- a client with an API key: the key, as before
- a public client (`WorkOS.PublicClient`): what it got before
- a client that authenticates with workload identity and has no key:
  `WorkOS.ConfigurationError`, raised before any request is sent and
  before the credentials are asked for a token, naming the function
  that was called

A `client_secret` the caller passes is kept in every case.

The generated functions still read the key themselves. They call this
function once they are regenerated; it is added first so that the
regenerated code finds it. The README names the calls that need a
client built with an API key.
@gjtorikian

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

@greptile-apps

greptile-apps Bot commented Nov 23, 2026 •

Copy link
Copy Markdown

RetriggerView in GreptileConfidence Score: 5/5

[Medium impact] The latest changes appear safe to merge, with no new blocking issue found.

Summary

Adds workload identity authentication with cached access tokens and separate GitHub Actions, Google Cloud, and AWS provider packages.

  • WorkOS clients can exchange workload identity for short-lived API tokens.
  • GitHub Actions, Google Cloud, and AWS workloads can fetch their platform identity tokens.
  • Each workload identity provider now has its own checks and release path.

Reviews (4) · Last reviewed commit: "refactor(workload-identity): turn the AW..." · Reviewed by Greptile

Comment thread lib/workos/client.ex Resolved
When an identity token provider raised, the exchange copied the
exception's message into the error's description and kept the exception
as its reason. Some exceptions print the value they failed on. A
provider that matches on the response it fetched raises a `MatchError`
when the match fails, and its message is the whole response:

    no match of right hand side value:

        %{"value" => "<the identity token>"}

So the identity token could end up in the message of the
`WorkOS.WorkloadIdentityError` and in its inspected form, which is where
an application logs it from.

An exception the provider raises is now reported by its module name
alone: the description is `the provider raised MatchError` and the
reason is `MatchError`. Neither the message nor the exception is kept.

A reason the provider returns in `{:error, reason}` is unchanged: it is
the provider's own choice of words, and it is how the platform
providers say what to fix. The documentation now says that a provider
must not put the token in it.
@gjtorikian

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

…as [redacted]

A token endpoint may repeat the assertion it was sent in the `error` or
`error_description` of its answer. The exchange already replaced it
before the error kept either string, but with `[REDACTED]`. The marker
is now `[redacted]`, the same text in every WorkOS SDK, so a log search
or an alert written for one SDK matches them all.

The rule, as the tests now pin it:

- every occurrence is replaced, in `error` and in `error_description`
- for a final error (a 400) and for a retried one (a 503 until retries
  run out)
- before the description is cut to 500 characters, so a cut cannot
  leave the start of a token behind

The identity token then appears in no message, no inspected form of
the error and no log line.
One call of the AWS provider has to be one request to STS: the token
exchange calls the provider again when it fails, so a retry inside the
provider multiplies the requests. That was already so, because `aws`
sends a request again only when `enable_retries?` is set and the
provider did not set it. With the option on, one provider call made 11
requests to an STS that answered 429, 500 or 503.

The provider now passes `enable_retries?: false` itself, so the rule
does not depend on the default of a dependency.

Tests, against a local server that stands in for STS:

- one provider call is one request when STS answers with a throttling
  error, 429, 500 or 503
- a request that gets no answer fails at the time limit
- the limit is 10 seconds, for connecting and for the request, the
  same as in the GitHub Actions and Google Cloud providers

The `:httpc` adapter reads the limit from its options so that a test
can shorten it. The provider sets no options.
@gjtorikian

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

1 similar comment
@gjtorikian

Copy link
Copy Markdown
Contributor Author

@greptileai review this draft

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

Development

Successfully merging this pull request may close these issues.

1 participant