Skip to content
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

fix: route deployment fallback for restricted permission API tokens #1188

Merged

Conversation

petebacondarwin
Copy link
Contributor

@petebacondarwin petebacondarwin commented Jun 5, 2022

It could be worth reviewing this commit by commit, given that quite a few lines changed.

Fixes #651

@changeset-bot
Copy link

changeset-bot bot commented Jun 5, 2022

🦋 Changeset detected

Latest commit: 70e220a

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
wrangler Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@github-actions
Copy link
Contributor

github-actions bot commented Jun 5, 2022

A wrangler prerelease is available for testing. You can install this latest build in your project with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/runs/2461310367/npm-package-wrangler-1188

You can reference the automatically updated head of this PR with:

npm install --save-dev https://prerelease-registry.developers.workers.dev/prs/1188/npm-package-wrangler-1188

Or you can use npx with this latest build directly:

npx https://prerelease-registry.developers.workers.dev/runs/2461310367/npm-package-wrangler-1188 dev path/to/script.js

@petebacondarwin petebacondarwin force-pushed the route-permission-fallback branch from 69fa0b6 to 1fae2c3 Compare June 7, 2022 13:33
This makes it available to use elsewhere in the code.
While we wait for changes to the CF API to support API tokens that do not have
"All Zone" permissions, this change provides a workaround for most scenarios.

If the bulk-route request fails with an authorization error, then we fallback
to the Wrangler 1 approach, which sends individual route updates via a zone-based
endpoint.

Fixes cloudflare#651
@petebacondarwin petebacondarwin force-pushed the route-permission-fallback branch from 1fae2c3 to 7e11bf8 Compare June 7, 2022 13:47
@petebacondarwin petebacondarwin changed the title WIP - Route permission fallback fix: route deployment fallback for restricted permission API tokens Jun 7, 2022
@petebacondarwin petebacondarwin marked this pull request as ready for review June 7, 2022 13:48
Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the work here! I'm certain it works, but I'd love for the implementation o be a tad simpler. I dropped some notes, and I'd be happy to discuss in more detail if you'd like

continue;
} else {
throw new Error(
`The route with pattern "${routePattern}" is already associated with another worker called "${knownScript}".`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the api will already throw an error if this is true , so I wonder whether we should throw an error here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is true that the API will throw an error but this will only contain the error code 10020 and the message workers.api.error.duplicate_route [code: 10020].

We would need to add code to catch errors when making the API call, then to filter out that error, and even then we would need to do a look up on the allRoutes map in order to work out the name of the worker that currently holds this route.

So we would end up with about the same amount of code, we would be relying upon parsing the error message from the API and still need to work out the worker name in order to provide a helpful error message. In addition the API would get hit unnecessarily.

So I don't think we should make the suggested change here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moreover, waiting for the API to fail might result in some routes being deployed and some not. By failing early we are consistently only deploying routes if all are valid.

Comment on lines 580 to 585
async function publishRoutes(
workerUrl: string,
routes: Route[],
scriptName: string,
notProd: boolean
): Promise<string[]> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function signature is a bit weird, how about:

  scriptName: string,
  routes: Route[], 
  {
    accountId:string, 
    legacyEnv: boolean, 
    env: string | undefined
  }

We'll also have to make a helper to generate workerUrl, that should also be reused in publishCustomDomains() (which should also change it s signature)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Such a refactoring is a larger effort outside of the scope of this PR.
workerUrl is used in a number of places throughout the code of this file.
Let's consider that in a follow up.

I will rearrange these parameters to make it a bit less weird for now.

Comment on lines 616 to 618
routes: Route[],
scriptName: string,
notProd: boolean
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

like above, I think the signature here should be

scriptName: string, 
routes: Route[],
{
  legacyEnv: boolean, 
  env: string | undefined
}

Copy link
Contributor

@threepointone threepointone left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok this is extremely dope, let's land it and start testing it!

@petebacondarwin petebacondarwin merged commit b44cc26 into cloudflare:main Jun 9, 2022
@petebacondarwin petebacondarwin deleted the route-permission-fallback branch June 9, 2022 08:25
@github-actions github-actions bot mentioned this pull request Jun 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

🐛 BUG: Routes do not work if api token is restricted to zone
2 participants