Skip to content
This repository has been archived by the owner on Aug 3, 2023. It is now read-only.

feat: authenticate calls to preview service when possible #429

Merged
merged 15 commits into from
Aug 15, 2019

Conversation

ashleymichal
Copy link
Contributor

@ashleymichal ashleymichal commented Aug 12, 2019

Today Wrangler uses the basic service on cloudflareworkers.com to run its preview command. This PR utilizes the authenticated endpoints, which unlocks desirable features such as kv binding support for wrangler preview specifically.

This PR also includes a refactor to Project, which moves the validate_project function out of publish and makes it a public method on Project. In hindsight, preview could probably have used it as a function? It could go either way.

Another refactor makes the preview_address a static &str const, as it is always the same, hard coded value.

Finally, I unwrapped the method passed as an arg and passed it unwrapped.

Closes #423 and #431

@ashleymichal ashleymichal self-assigned this Aug 12, 2019
@ashleymichal ashleymichal added feature Feature requests and suggestions changelog - feature regression Something is broken, but works in previous releases labels Aug 12, 2019
@ashleymichal ashleymichal changed the title feat: authenticate calls to preview service when possible [WIP] - feat: authenticate calls to preview service when possible Aug 12, 2019
Copy link
Contributor

@xortive xortive left a comment

Choose a reason for hiding this comment

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

Instead of splitting the auth and no_auth previews up, keeping one preview function with a function that performs the upload and returns the script Id (and encapsulates the auth_client, checking for user config, etc stuff) will make this much easier to merge with the livereload branch

@ashleymichal
Copy link
Contributor Author

Instead of splitting the auth and no_auth previews up, keeping one preview function with a function that performs the upload and returns the script Id (and encapsulates the auth_client, checking for user config, etc stuff) will make this much easier to merge with the livereload branch

I am glad that is the case, because that's approximately the refactor i was headed toward!

src/commands/publish/preview/mod.rs Outdated Show resolved Hide resolved
src/commands/publish/preview/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@gabbifish gabbifish left a comment

Choose a reason for hiding this comment

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

This looks like great progress so far! Excited to see the necessary token/email logic (headers on the POST request for preview... unless I'm misunderstanding preview auth works) :)

src/commands/publish/preview/mod.rs Outdated Show resolved Hide resolved
src/commands/publish/preview/mod.rs Show resolved Hide resolved
@ashleymichal ashleymichal removed the feature Feature requests and suggestions label Aug 13, 2019
Copy link
Contributor

@xortive xortive left a comment

Choose a reason for hiding this comment

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

Just realized after testing on my machine that this currently doesn't account for the case where there is a global user config, but account_id isn't configured. This also breaks tests locally if one has a global user config, since the tests for preview don't configure account_id

@ashleymichal
Copy link
Contributor Author

ashleymichal commented Aug 14, 2019

This also breaks tests locally if one has a global user config, since the tests for preview don't configure account_id

oo good point. mine probably pass because i have account id as an environment variable as well. I'll try to update the tests, but also, our tests should maybe set/unset env rather than taking it from execution env? dunno. gonna have to think on that.

src/commands/publish/mod.rs Outdated Show resolved Hide resolved
src/commands/publish/preview/mod.rs Show resolved Hide resolved
src/commands/publish/preview/mod.rs Outdated Show resolved Hide resolved
src/commands/publish/preview/mod.rs Outdated Show resolved Hide resolved
src/commands/publish/preview/mod.rs Outdated Show resolved Hide resolved
src/commands/publish/preview/mod.rs Show resolved Hide resolved
src/commands/publish/preview/mod.rs Show resolved Hide resolved
src/commands/publish/preview/mod.rs Show resolved Hide resolved
src/settings/project/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

🔥 lgtm

Copy link
Contributor

@gabbifish gabbifish left a comment

Choose a reason for hiding this comment

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

LGTM!

// so we omit them and provide the user with a little guidance. We don't error out, though,
// because there are valid workarounds for this for testing purposes.
if project.kv_namespaces.is_some() {
message::warn("KV Namespaces are not supported without setting API credentials");
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

src/commands/publish/preview/mod.rs Outdated Show resolved Hide resolved
Copy link
Contributor

@xortive xortive left a comment

Choose a reason for hiding this comment

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

lgtm!

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

Breaking changes for newly generated projects. You shouldn't need to use authenticated preview w/o KV

❯ wrangler generate
🌀  🐑  Generating a new webpack worker project with name 'worker'...
🔧   Creating project called `worker`...
✨   Done! New project created /Users/averyharnish/Documents/work/wrangler/worker
❯ cd worker
❯ wrangler preview
Error: Your wrangler.toml is missing the field ["account_id"] which is required to publish to your subdomain!

@ashleymichal
Copy link
Contributor Author

Breaking changes for newly generated projects. You shouldn't need to use authenticated preview w/o KV

❯ wrangler generate
🌀  🐑  Generating a new webpack worker project with name 'worker'...
🔧   Creating project called `worker`...
✨   Done! New project created /Users/averyharnish/Documents/work/wrangler/worker
❯ cd worker
❯ wrangler preview
Error: Your wrangler.toml is missing the field ["account_id"] which is required to publish to your subdomain!

The quick fix to this (and the general complaint that one has to populate their account id) would be to tell folks to set the appropriate env var. But i'm open to other thoughts, we could potentially fallback to unauthenticated and warn about it the way we do with kv.

@EverlastingBugstopper
Copy link
Contributor

EverlastingBugstopper commented Aug 14, 2019

This might just be a problem with wrangler config but if I wrangler config and just press enter twice it stores my credentials as empty strings. When I run wrangler preview with no account_id it falls back to unauthenticated. Fine. When I put in my account id into the toml, I get this

$ wrangler preview
✨   Built successfully, built project size is 517 bytes.
Error: https://api.cloudflare.com/client/v4/accounts/e04d3d27a6829775c8ef937145d7f3ec/workers/scripts/worker/preview: Client Error: 400 Bad Request

yuck

EDIT: I think this is a separate issue, so I opened one here at #439

@EverlastingBugstopper
Copy link
Contributor

EverlastingBugstopper commented Aug 14, 2019

I also don't get a warning about empty config + falling back to unauthenticated if I don't have a global config set up

❯ rm /Users/averyharnish/.wrangler/config/default.toml 
❯ wrangler whoami
Error: ⚠️  Your global config has an error, run `wrangler config`: missing field `email`
❯ wrangler preview 
✨   Built successfully, built project size is 517 bytes.
👷  GET https://00000000000000000000000000000000.cloudflareworkers.com
👷  Your worker responded with: Hello worker!

This one I think we should probably address here

@ashleymichal
Copy link
Contributor Author

I also don't get a warning about empty config + falling back to unauthenticated if I don't have a global config set up

❯ rm /Users/averyharnish/.wrangler/config/default.toml 
❯ wrangler whoami
Error: ⚠️  Your global config has an error, run `wrangler config`: missing field `email`
❯ wrangler preview 
✨   Built successfully, built project size is 517 bytes.
👷  GET https://00000000000000000000000000000000.cloudflareworkers.com
👷  Your worker responded with: Hello worker!

This one I think we should probably address here

I'll check this out today.

Copy link
Contributor

@EverlastingBugstopper EverlastingBugstopper left a comment

Choose a reason for hiding this comment

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

LGTM!

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
regression Something is broken, but works in previous releases
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Preview throws Server Error: 502 Bad Gateway
5 participants