Skip to content

Commit

Permalink
fix: don't require auth for wrangler r2 object --local operations (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
mrbbot authored Jan 10, 2024
1 parent 0a488f6 commit 5af6df1
Show file tree
Hide file tree
Showing 6 changed files with 195 additions and 161 deletions.
7 changes: 7 additions & 0 deletions .changeset/funny-otters-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"wrangler": patch
---

fix: don't require auth for `wrangler r2 object --local` operations

Previously, Wrangler would ask you to login when reading or writing from local R2 buckets. This change ensures no login prompt is displayed, as authentication isn't required for these operations.
21 changes: 16 additions & 5 deletions packages/wrangler/src/__tests__/helpers/mock-account-id.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import { reinitialiseAuthTokens } from "../../user";

const ORIGINAL_CLOUDFLARE_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
const ORIGINAL_CLOUDFLARE_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;

/**
* Mock the API token so that we don't need to read it from user configuration files.
*
Expand All @@ -12,6 +9,7 @@ const ORIGINAL_CLOUDFLARE_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;
export function mockApiToken({
apiToken = "some-api-token",
}: { apiToken?: string | null } = {}) {
const ORIGINAL_CLOUDFLARE_API_TOKEN = process.env.CLOUDFLARE_API_TOKEN;
beforeEach(() => {
if (apiToken === null) {
delete process.env.CLOUDFLARE_API_TOKEN;
Expand All @@ -22,7 +20,12 @@ export function mockApiToken({
reinitialiseAuthTokens();
});
afterEach(() => {
process.env.CLOUDFLARE_API_TOKEN = ORIGINAL_CLOUDFLARE_API_TOKEN;
if (ORIGINAL_CLOUDFLARE_API_TOKEN === undefined) {
// `process.env`'s assigned property values are coerced to strings
delete process.env.CLOUDFLARE_API_TOKEN;
} else {
process.env.CLOUDFLARE_API_TOKEN = ORIGINAL_CLOUDFLARE_API_TOKEN;
}
});
}

Expand All @@ -35,14 +38,22 @@ export function mockApiToken({
export function mockAccountId({
accountId = "some-account-id",
}: { accountId?: string | null } = {}) {
const ORIGINAL_CLOUDFLARE_ACCOUNT_ID = process.env.CLOUDFLARE_ACCOUNT_ID;
beforeEach(() => {
if (accountId === null) {
delete process.env.CLOUDFLARE_ACCOUNT_ID;
} else {
process.env.CLOUDFLARE_ACCOUNT_ID = accountId;
}
// Now we have updated the environment, we must reinitialize the user auth state.
reinitialiseAuthTokens();
});
afterEach(() => {
process.env.CLOUDFLARE_ACCOUNT_ID = ORIGINAL_CLOUDFLARE_ACCOUNT_ID;
if (ORIGINAL_CLOUDFLARE_ACCOUNT_ID === undefined) {
// `process.env`'s assigned property values are coerced to strings
delete process.env.CLOUDFLARE_ACCOUNT_ID;
} else {
process.env.CLOUDFLARE_ACCOUNT_ID = ORIGINAL_CLOUDFLARE_ACCOUNT_ID;
}
});
}
14 changes: 9 additions & 5 deletions packages/wrangler/src/__tests__/pages-deployment-tail.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,15 @@ import type WebSocket from "ws";

describe("pages deployment tail", () => {
runInTempDir();

// This `afterEach()` must come before `mockAccountId()` and `mockApiToken()`.
// Closing the sockets will trigger an authenticated fetch call which would
// otherwise fail.
afterEach(() => {
mockWebSockets.forEach((ws) => ws.close());
mockWebSockets.splice(0);
});

mockAccountId();
mockApiToken();
const std = mockConsoleMethods();
Expand All @@ -42,11 +51,6 @@ describe("pages deployment tail", () => {
delete process.env.CF_PAGES;
});

afterEach(() => {
mockWebSockets.forEach((ws) => ws.close());
mockWebSockets.splice(0);
});

/**
* Interaction with the tailing API, including tail creation,
* deletion, and connection.
Expand Down
Loading

0 comments on commit 5af6df1

Please sign in to comment.