-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[wrangler] Add Access Service Token support for CI/non-interactive environment #13031
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
Merged
+281
−64
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3c71642
[wrangler] Add Access Service Token support for CI/non-interactive en…
WalshyDev fa2432e
add `@param`, `@return` and `@throws` to `getAccessHeaders` jsdoc com…
dario-piotrowicz 291e3a9
fix formatting
dario-piotrowicz ba628c8
add expect to test
dario-piotrowicz 55580e4
add missing `vi.mocked(ci).isCI = false`
dario-piotrowicz 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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| Add support for Cloudflare Access Service Token authentication via environment variables | ||
|
|
||
| When running `wrangler dev` with remote bindings behind a Cloudflare Access-protected domain, Wrangler previously required `cloudflared access login` which opens a browser for interactive authentication. This does not work in CI/CD environments. | ||
|
|
||
| You can now set the `CLOUDFLARE_ACCESS_CLIENT_ID` and `CLOUDFLARE_ACCESS_CLIENT_SECRET` environment variables to authenticate using an Access Service Token instead: | ||
|
|
||
| ```sh | ||
| export CLOUDFLARE_ACCESS_CLIENT_ID="<your-client-id>.access" | ||
| export CLOUDFLARE_ACCESS_CLIENT_SECRET="<your-client-secret>" | ||
| wrangler dev | ||
| ``` | ||
|
|
||
| Additionally, when running in a non-interactive environment (CI) without these credentials, Wrangler now throws a clear, actionable error instead of hanging on `cloudflared access login`. |
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 |
|---|---|---|
| @@ -1,31 +1,142 @@ | ||
| import { UserError } from "@cloudflare/workers-utils"; | ||
| import { beforeEach, describe, it } from "vitest"; | ||
| import { domainUsesAccess, getAccessToken } from "../user/access"; | ||
| import ci from "ci-info"; | ||
| import { beforeEach, describe, it, vi } from "vitest"; | ||
| import { | ||
| clearAccessCaches, | ||
| domainUsesAccess, | ||
| getAccessHeaders, | ||
| } from "../user/access"; | ||
| import { mockConsoleMethods } from "./helpers/mock-console"; | ||
| import { useMockIsTTY } from "./helpers/mock-istty"; | ||
| import { msw, mswAccessHandlers } from "./helpers/msw"; | ||
|
|
||
| describe("access", () => { | ||
| const { setIsTTY } = useMockIsTTY(); | ||
| const std = mockConsoleMethods(); | ||
|
|
||
| beforeEach(() => { | ||
| clearAccessCaches(); | ||
| msw.use(...mswAccessHandlers); | ||
| }); | ||
|
|
||
| describe("basic", () => { | ||
| describe("domainUsesAccess", () => { | ||
| it("should correctly detect an access protected domain", async ({ | ||
| expect, | ||
| }) => { | ||
| expect(await domainUsesAccess("access-protected.com")).toBeTruthy(); | ||
| expect(await domainUsesAccess("not-access-protected.com")).toBeFalsy(); | ||
| }); | ||
| it("should not fail without cloudflared installed", async ({ expect }) => { | ||
| expect(await getAccessToken("not-access-protected.com")).toBeFalsy(); | ||
| }); | ||
| it("should error without cloudflared installed on an access protected domain", async ({ | ||
| }); | ||
|
|
||
| describe("getAccessHeaders", () => { | ||
| it("should return empty headers for non-access-protected domains", async ({ | ||
| expect, | ||
| }) => { | ||
| await expect(getAccessToken("access-protected.com")).rejects.toEqual( | ||
| new UserError( | ||
| "To use Wrangler with Cloudflare Access, please install `cloudflared` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation" | ||
| ) | ||
| ); | ||
| expect(await getAccessHeaders("not-access-protected.com")).toEqual({}); | ||
| }); | ||
|
|
||
| describe("service token authentication", () => { | ||
| it("should return service token headers when both env vars are set", async ({ | ||
| expect, | ||
| }) => { | ||
| vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "test-client-id.access"); | ||
| vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "test-client-secret"); | ||
|
|
||
| const headers = await getAccessHeaders("access-protected.com"); | ||
| expect(headers).toEqual({ | ||
| "CF-Access-Client-Id": "test-client-id.access", | ||
| "CF-Access-Client-Secret": "test-client-secret", | ||
| }); | ||
| // No warning is presented since both env variables are set | ||
| expect(std.warn).toMatchInlineSnapshot(`""`); | ||
| }); | ||
|
|
||
| it("should warn when only CLOUDFLARE_ACCESS_CLIENT_ID is set", async ({ | ||
| expect, | ||
| }) => { | ||
| vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_ID", "test-client-id.access"); | ||
| setIsTTY(false); | ||
|
|
||
| await expect( | ||
| getAccessHeaders("access-protected.com") | ||
| ).rejects.toThrowErrorMatchingInlineSnapshot( | ||
| `[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive. | ||
| Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token. | ||
| See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]` | ||
| ); | ||
| expect(std.warn).toContain( | ||
| "Both CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET must be set" | ||
| ); | ||
| expect(std.warn).toContain( | ||
| "Only CLOUDFLARE_ACCESS_CLIENT_ID was found" | ||
| ); | ||
| }); | ||
|
|
||
| it("should warn when only CLOUDFLARE_ACCESS_CLIENT_SECRET is set", async ({ | ||
| expect, | ||
| }) => { | ||
| vi.stubEnv("CLOUDFLARE_ACCESS_CLIENT_SECRET", "test-client-secret"); | ||
| setIsTTY(false); | ||
|
|
||
| await expect( | ||
| getAccessHeaders("access-protected.com") | ||
| ).rejects.toThrowErrorMatchingInlineSnapshot( | ||
| `[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive. | ||
| Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token. | ||
| See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]` | ||
| ); | ||
| expect(std.warn).toContain( | ||
| "Both CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET must be set" | ||
| ); | ||
| expect(std.warn).toContain( | ||
| "Only CLOUDFLARE_ACCESS_CLIENT_SECRET was found" | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("non-interactive environment", () => { | ||
| it("should throw actionable error when non-interactive and no service token", async ({ | ||
| expect, | ||
| }) => { | ||
| setIsTTY(false); | ||
|
|
||
| await expect( | ||
| getAccessHeaders("access-protected.com") | ||
| ).rejects.toThrowErrorMatchingInlineSnapshot( | ||
| `[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive. | ||
| Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token. | ||
| See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]` | ||
| ); | ||
| }); | ||
|
|
||
| it("should throw actionable error when in CI and no service token", async ({ | ||
| expect, | ||
| }) => { | ||
| setIsTTY(true); | ||
| vi.mocked(ci).isCI = true; | ||
|
|
||
| await expect( | ||
| getAccessHeaders("access-protected.com") | ||
| ).rejects.toThrowErrorMatchingInlineSnapshot( | ||
| `[Error: The domain "access-protected.com" is behind Cloudflare Access, but no Access Service Token credentials were found and the current environment is non-interactive. | ||
| Set the CLOUDFLARE_ACCESS_CLIENT_ID and CLOUDFLARE_ACCESS_CLIENT_SECRET environment variables to authenticate with an Access Service Token. | ||
| See https://developers.cloudflare.com/cloudflare-one/access-controls/service-credentials/service-tokens/]` | ||
| ); | ||
| }); | ||
| }); | ||
|
|
||
| describe("interactive environment (cloudflared fallback)", () => { | ||
| it("should error without cloudflared installed on an access protected domain", async ({ | ||
| expect, | ||
| }) => { | ||
| setIsTTY(true); | ||
| vi.mocked(ci).isCI = false; | ||
|
|
||
| await expect( | ||
| getAccessHeaders("access-protected.com") | ||
| ).rejects.toThrowErrorMatchingInlineSnapshot( | ||
| `[Error: To use Wrangler with Cloudflare Access, please install \`cloudflared\` from https://developers.cloudflare.com/cloudflare-one/connections/connect-apps/install-and-setup/installation]` | ||
| ); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
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
26 changes: 9 additions & 17 deletions
26
packages/wrangler/src/__tests__/helpers/msw/handlers/access.ts
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 |
|---|---|---|
| @@ -1,21 +1,13 @@ | ||
| import { http, HttpResponse } from "msw"; | ||
|
|
||
| export default [ | ||
| http.get( | ||
| "*access-protected.com*", | ||
| () => { | ||
| return HttpResponse.json(null, { | ||
| status: 302, | ||
| headers: { location: "access-protected-com.cloudflareaccess.com" }, | ||
| }); | ||
| }, | ||
| { once: true } | ||
| ), | ||
| http.get( | ||
| "*not-access-protected.com*", | ||
| () => { | ||
| return HttpResponse.json("OK", { status: 200 }); | ||
| }, | ||
| { once: true } | ||
| ), | ||
| http.get("https://access-protected.com/", () => { | ||
| return HttpResponse.json(null, { | ||
| status: 302, | ||
| headers: { location: "access-protected-com.cloudflareaccess.com" }, | ||
| }); | ||
| }), | ||
| http.get("https://not-access-protected.com/", () => { | ||
| return HttpResponse.json("OK", { status: 200 }); | ||
| }), | ||
| ]; |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.