-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Provision GitHub gatekeeper OAuth credentials in previews #419
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
Maximo-Guk
wants to merge
1
commit into
kenton/worktrees
Choose a base branch
from
maximo/preview-github-oauth-secrets
base: kenton/worktrees
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,8 +19,10 @@ | |
| // namespace / R2 bucket per preview, and where service bindings get patched to point at | ||
| // sibling previews rather than the baselines. | ||
| // | ||
| // Gatekeeper OAuth app credentials (CLIENT_ID/CLIENT_SECRET) are deliberately absent: previews | ||
| // exercise routing, auth and the agent, not third-party connector flows. | ||
| // Gatekeeper OAuth app credentials (CLIENT_ID/CLIENT_SECRET) are absent from the generated configs | ||
| // for the same reason the backend's secrets are — Wrangler prints what it finds in one, and this | ||
| // workflow's logs are public. Where an OAuth app is configured for previews, preview.ts uploads the | ||
| // pair to that gatekeeper's Previews settings instead; see resolveGatekeeperSecrets. | ||
|
|
||
| import { writeFileSync } from "node:fs"; | ||
| import { execFileSync } from "node:child_process"; | ||
|
|
@@ -610,6 +612,71 @@ export function resolveAiGateway({ | |
| }; | ||
| } | ||
|
|
||
| /** | ||
| * Record one gatekeeper's OAuth app credentials under the variable names the worker reads them as. | ||
| * | ||
| * The pair moves together: a gatekeeper holding one half is not half-connectable, it throws "The | ||
| * GitHub gatekeeper is not configured." on the first click, so a typo in one secret's name fails | ||
| * the deploy rather than surfacing in a preview nobody is reading the logs of. | ||
| */ | ||
| function addOAuthApp( | ||
| into: Map<string, Record<string, string>>, | ||
| pkgName: string, | ||
| envPrefix: string, | ||
| clientId: string | undefined, | ||
| clientSecret: string | undefined, | ||
| ): void { | ||
| if (clientId && clientSecret) { | ||
| into.set(pkgName, { CLIENT_ID: clientId, CLIENT_SECRET: clientSecret }); | ||
| return; | ||
| } | ||
| if (clientId || clientSecret) { | ||
| throw new Error(`${envPrefix}_CLIENT_ID and ${envPrefix}_CLIENT_SECRET must be set together: ` + | ||
| `they are one OAuth app, and ${pkgName} refuses to start a flow with half of it`); | ||
| } | ||
| console.warn(`${envPrefix}_CLIENT_ID is unset: ${pkgName} is deployed unconfigured, and ` + | ||
| "connecting it in this preview will fail."); | ||
|
Comment on lines
+637
to
+638
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🟡 Removed OAuth credentials remain active When both repository secrets are removed, Prompt for agentsWas this helpful? React with 👍 or 👎 to provide feedback. |
||
| } | ||
|
|
||
| /** | ||
| * The OAuth app credentials a gatekeeper's previews are given, as `package name -> secrets`. | ||
| * | ||
| * Optional, per gatekeeper: an unconfigured one still deploys, and only that connector is dead in | ||
| * the preview — unlike {@link resolveAccess}, whose absence changes how the whole instance | ||
| * authenticates. Most gatekeepers have no preview OAuth app at all, which is why previews are for | ||
| * routing, auth and the agent first and third-party flows only where someone registered one. | ||
| * | ||
| * Registering one is not just a pair of secrets. A preview's redirect URI is | ||
| * `https://<preview>-router.<workers.dev subdomain>/gatekeeper/<short>/oauth`, and the host changes | ||
| * with every pull request — so the app's callback URL has to be | ||
| * `https://<workers.dev subdomain>/gatekeeper/<short>/oauth` with GitHub's wildcard matching left | ||
| * on, which is what lets each preview's subdomain validate against it. That also means any worker | ||
| * on that subdomain can receive an authorization code for this app, so the app it belongs to should | ||
| * be a throwaway registered for previews and never the one a real deployment uses. | ||
| * | ||
| * `PREVIEW_`-prefixed rather than the `GITHUB_CLIENT_ID` run-dev-server.ts reads from a developer's | ||
| * shell: GitHub refuses to store a repository secret whose name begins with `GITHUB_`, and the | ||
| * distinct name keeps the preview app and a maintainer's local app from being confused for one | ||
| * another. | ||
| * | ||
| * Adding a second gatekeeper is a parameter pair here, one `addOAuthApp` line, the matching pair in | ||
| * .github/workflows/preview.yml, and an entry in env-passthrough.test.ts. | ||
| * | ||
| * The environment is read in the parameter defaults rather than the body so that | ||
| * env-passthrough.test.ts, whose discovery is textual, can see every name. | ||
| */ | ||
| export function resolveGatekeeperSecrets({ | ||
| githubClientId = process.env.PREVIEW_GITHUB_CLIENT_ID, | ||
| githubClientSecret = process.env.PREVIEW_GITHUB_CLIENT_SECRET, | ||
| }: { | ||
| githubClientId?: string; | ||
| githubClientSecret?: string; | ||
| } = {}): Map<string, Record<string, string>> { | ||
| const configured = new Map<string, Record<string, string>>(); | ||
| addOAuthApp(configured, "gatekeeper-github", "PREVIEW_GITHUB", githubClientId, githubClientSecret); | ||
| return configured; | ||
| } | ||
|
|
||
| /** | ||
| * The backend's secrets, keyed by the variable name it reads them as. | ||
| * | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When both repository secrets are later unset,
oauthis absent and this skips reconciliation entirely. The pinned Wranglerpreview secret bulkcommand merge-patches the worker Preview defaults, so omitted keys are preserved; every future GitHub preview will therefore keep inheriting the old credentials despite the resolver claiming the connector is unconfigured. Please explicitly uploadCLIENT_ID: nullandCLIENT_SECRET: null(or otherwise delete those defaults) for the unconfigured case.