Skip to content

oidc: Refactor lookup strategies into single functions #18169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

facutuesca
Copy link
Contributor

This PR refactors the Trusted Publishing "lookup strategies" pattern into a single lookup_by_claims() method for each of the publishers.

Context

A strategy is a way of, given a set of OIDC claims, query the database for a matching Trusted Publisher. Concretely, a strategy is a function that takes a set of claims and returns a Query object. Each publisher has a list of these strategies, ordered from specific to general. When trying to find a Trusted Publisher, the more specific strategies are tried first, and if they fail the more general ones are tried.

For example, for the given set of claims for a GitHub OIDC token:

{
    "repository": "foo/bar",
    "job_workflow_ref": "foo/bar/.github/workflows/release.yml@refs/heads/main",
    "environment": "my_environment",
}

first we ran a strategy that tried to find publishers with exactly those values (in particular, environment==my_environment). If that strategy failed, we tried the second strategy, where we tried to find publishers with environment==None (that is, allowing any environment).
The "specific to general" order in this case meant going from "Publishers that only allow my_environment as an environment" to "Publishers that allow any environment".

New implementation

This PR changes the above approach to a single function per provider called lookup_by_claims() which takes a set of claims and returns a Publisher.

The multiple strategies are collapsed into a single query: we query for all publishers where the non-optional fields match the claims. In the example above, this means our single query looks for all publishers that match repository and job_workflow_ref, ignoring the environment value.

We then look at the resulting Python Publisher objects, and select the most specific one.

Rationale

The reasons for this change are:

cc @woodruffw @miketheman @di

@facutuesca facutuesca requested a review from a team as a code owner May 21, 2025 22:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants