-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add r2 bucket cors command (list, set, delete) #7382
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
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "wrangler": minor | ||
| --- | ||
|
|
||
| Added r2 bucket cors command to Wrangler including list, set, delete |
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 |
|---|---|---|
| @@ -0,0 +1,173 @@ | ||
| import path from "node:path"; | ||
| import { createCommand, createNamespace } from "../core/create-command"; | ||
| import { confirm } from "../dialogs"; | ||
| import { UserError } from "../errors"; | ||
| import { logger } from "../logger"; | ||
| import { parseJSON, readFileSync } from "../parse"; | ||
| import { requireAuth } from "../user"; | ||
| import formatLabelledValues from "../utils/render-labelled-values"; | ||
| import { | ||
| deleteCORSPolicy, | ||
| getCORSPolicy, | ||
| putCORSPolicy, | ||
| tableFromCORSPolicyResponse, | ||
| } from "./helpers"; | ||
| import type { CORSRule } from "./helpers"; | ||
|
|
||
| export const r2BucketCORSNamespace = createNamespace({ | ||
| metadata: { | ||
| description: "Manage CORS configuration for an R2 bucket", | ||
| status: "stable", | ||
| owner: "Product: R2", | ||
| }, | ||
| }); | ||
|
|
||
| export const r2BucketCORSListCommand = createCommand({ | ||
| metadata: { | ||
| description: "List the CORS rules for an R2 bucket", | ||
| status: "stable", | ||
| owner: "Product: R2", | ||
| }, | ||
| positionalArgs: ["bucket"], | ||
| args: { | ||
| bucket: { | ||
| describe: "The name of the R2 bucket to list the CORS rules for", | ||
| type: "string", | ||
| demandOption: true, | ||
| }, | ||
| jurisdiction: { | ||
| describe: "The jurisdiction where the bucket exists", | ||
| alias: "J", | ||
| requiresArg: true, | ||
| type: "string", | ||
| }, | ||
| }, | ||
| async handler({ bucket, jurisdiction }, { config }) { | ||
| const accountId = await requireAuth(config); | ||
|
|
||
| logger.log(`Listing CORS rules for bucket '${bucket}'...`); | ||
| const corsPolicy = await getCORSPolicy(accountId, bucket, jurisdiction); | ||
|
|
||
| if (corsPolicy.length === 0) { | ||
| logger.log( | ||
| `There is no CORS configuration defined for bucket '${bucket}'.` | ||
| ); | ||
| } else { | ||
| const tableOutput = tableFromCORSPolicyResponse(corsPolicy); | ||
| logger.log(tableOutput.map((x) => formatLabelledValues(x)).join("\n\n")); | ||
| } | ||
| }, | ||
| }); | ||
|
|
||
| export const r2BucketCORSSetCommand = createCommand({ | ||
| metadata: { | ||
| description: "Set the CORS configuration for an R2 bucket from a JSON file", | ||
| status: "stable", | ||
| owner: "Product: R2", | ||
| }, | ||
| positionalArgs: ["bucket"], | ||
| args: { | ||
| bucket: { | ||
| describe: "The name of the R2 bucket to set the CORS configuration for", | ||
| type: "string", | ||
| demandOption: true, | ||
| }, | ||
| file: { | ||
| describe: "Path to the JSON file containing the CORS configuration", | ||
| type: "string", | ||
| demandOption: true, | ||
| requiresArg: true, | ||
| }, | ||
| jurisdiction: { | ||
| describe: "The jurisdiction where the bucket exists", | ||
| alias: "J", | ||
| requiresArg: true, | ||
| type: "string", | ||
| }, | ||
| force: { | ||
| describe: "Skip confirmation", | ||
| type: "boolean", | ||
| alias: "y", | ||
| default: false, | ||
| }, | ||
| }, | ||
| async handler({ bucket, file, jurisdiction, force }, { config }) { | ||
| const accountId = await requireAuth(config); | ||
|
|
||
| const jsonFilePath = path.resolve(file); | ||
|
|
||
| const corsConfig = parseJSON<{ rules: CORSRule[] }>( | ||
| readFileSync(jsonFilePath), | ||
| jsonFilePath | ||
| ); | ||
|
|
||
| if (!corsConfig.rules || !Array.isArray(corsConfig.rules)) { | ||
| throw new UserError( | ||
| `The CORS configuration file must contain a 'rules' array as expected by the request body of the CORS API: ` + | ||
| `https://developers.cloudflare.com/api/operations/r2-put-bucket-cors-policy` | ||
| ); | ||
| } | ||
|
|
||
| if (!force) { | ||
| const confirmedRemoval = await confirm( | ||
| `Are you sure you want to overwrite the existing CORS configuration for bucket '${bucket}'?` | ||
| ); | ||
| if (!confirmedRemoval) { | ||
| logger.log("Set cancelled."); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| logger.log( | ||
| `Setting CORS configuration (${corsConfig.rules.length} rules) for bucket '${bucket}'...` | ||
| ); | ||
| await putCORSPolicy(accountId, bucket, corsConfig.rules, jurisdiction); | ||
| logger.log(`✨ Set CORS configuration for bucket '${bucket}'.`); | ||
| }, | ||
| }); | ||
|
|
||
| export const r2BucketCORSDeleteCommand = createCommand({ | ||
| metadata: { | ||
| description: "Clear the CORS configuration for an R2 bucket", | ||
| status: "stable", | ||
| owner: "Product: R2", | ||
| }, | ||
| positionalArgs: ["bucket"], | ||
| args: { | ||
| bucket: { | ||
| describe: | ||
| "The name of the R2 bucket to delete the CORS configuration for", | ||
| type: "string", | ||
| demandOption: true, | ||
| }, | ||
| jurisdiction: { | ||
| describe: "The jurisdiction where the bucket exists", | ||
| alias: "J", | ||
| requiresArg: true, | ||
| type: "string", | ||
| }, | ||
| force: { | ||
| describe: "Skip confirmation", | ||
| type: "boolean", | ||
| alias: "y", | ||
jonesphillip marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| default: false, | ||
| }, | ||
| }, | ||
| async handler({ bucket, jurisdiction, force }, { config }) { | ||
| const accountId = await requireAuth(config); | ||
|
|
||
| if (!force) { | ||
| const confirmedRemoval = await confirm( | ||
| `Are you sure you want to clear the existing CORS configuration for bucket '${bucket}'?` | ||
| ); | ||
| if (!confirmedRemoval) { | ||
| logger.log("Set cancelled."); | ||
| return; | ||
| } | ||
| } | ||
|
|
||
| logger.log(`Deleting the CORS configuration for bucket '${bucket}'...`); | ||
| await deleteCORSPolicy(accountId, bucket, jurisdiction); | ||
| logger.log(`CORS configuration deleted for bucket '${bucket}'.`); | ||
| }, | ||
| }); | ||
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.