From 2de48f4ab9c2d3a7ea866a8aa3d0638ecc2b2398 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Mon, 13 Nov 2023 12:27:47 +0100 Subject: [PATCH] fix(cli): Pass auth token when querying packages Need to pass an auth token to the package query, otherwise it would fail due to not being able to query the private package. --- lib/cli/src/cli.rs | 16 ++++++++++------ lib/cli/src/commands/publish.rs | 8 +++++++- lib/registry/src/lib.rs | 5 +++-- lib/registry/src/package/builder.rs | 26 +++++++++++++++++++------- 4 files changed, 39 insertions(+), 16 deletions(-) diff --git a/lib/cli/src/cli.rs b/lib/cli/src/cli.rs index 1575ee140dd..22d1dad04ff 100644 --- a/lib/cli/src/cli.rs +++ b/lib/cli/src/cli.rs @@ -9,8 +9,7 @@ use crate::commands::CreateExe; #[cfg(feature = "wast")] use crate::commands::Wast; use crate::commands::{ - Add, Cache, CmdPackage, Config, Init, Inspect, Login, Publish, Run, SelfUpdate, Validate, - Whoami, + Add, Cache, Config, Init, Inspect, Login, Package, Publish, Run, SelfUpdate, Validate, Whoami, }; #[cfg(feature = "static-artifact-create")] use crate::commands::{CreateObj, GenCHeader}; @@ -110,9 +109,11 @@ impl Args { Some(Cmd::Login(login)) => login.execute(), Some(Cmd::Publish(publish)) => publish.execute(), Some(Cmd::Package(cmd)) => match cmd { - CmdPackage::Download(cmd) => cmd.execute(), - CmdPackage::Unpack(cmd) => cmd.execute(), - CmdPackage::Build(cmd) => cmd.execute(), + Package::Download(cmd) => cmd.execute(), + Package::Build(cmd) => cmd.execute(), + }, + Some(Cmd::Container(cmd)) => match cmd { + crate::commands::Container::Unpack(cmd) => cmd.execute(), }, /* Some(Cmd::Connect(connect)) => connect.execute(), @@ -265,7 +266,10 @@ enum Cmd { Run(Run), #[clap(subcommand)] - Package(crate::commands::CmdPackage), + Package(crate::commands::Package), + + #[clap(subcommand)] + Container(crate::commands::Container), // Edge commands /// Deploy apps to the Wasmer Edge. diff --git a/lib/cli/src/commands/publish.rs b/lib/cli/src/commands/publish.rs index 055bc9bc196..b141cece830 100644 --- a/lib/cli/src/commands/publish.rs +++ b/lib/cli/src/commands/publish.rs @@ -1,3 +1,4 @@ +use anyhow::Context as _; use clap::Parser; use wasmer_registry::wasmer_env::WasmerEnv; @@ -31,13 +32,18 @@ pub struct Publish { impl Publish { /// Executes `wasmer publish` pub fn execute(&self) -> Result<(), anyhow::Error> { + let token = self + .env + .token() + .context("could not determine auth token for registry - runer 'wasmer login'")?; + let publish = wasmer_registry::package::builder::Publish { registry: self.env.registry_endpoint().map(|u| u.to_string()).ok(), dry_run: self.dry_run, quiet: self.quiet, package_name: self.package_name.clone(), version: self.version.clone(), - token: self.env.token(), + token, no_validate: self.no_validate, package_path: self.package_path.clone(), }; diff --git a/lib/registry/src/lib.rs b/lib/registry/src/lib.rs index 18cae5825e3..3fbc5422dd4 100644 --- a/lib/registry/src/lib.rs +++ b/lib/registry/src/lib.rs @@ -153,6 +153,7 @@ pub fn query_package_from_registry( registry_url: &str, name: &str, version: Option<&str>, + auth_token: Option<&str>, ) -> Result { use crate::graphql::{ execute_query, @@ -164,8 +165,8 @@ pub fn query_package_from_registry( version: version.map(|s| s.to_string()), }); - let response: get_package_version_query::ResponseData = execute_query(registry_url, "", &q) - .map_err(|e| { + let response: get_package_version_query::ResponseData = + execute_query(registry_url, auth_token.unwrap_or_default(), &q).map_err(|e| { QueryPackageError::ErrorSendingQuery(format!("Error sending GetPackagesQuery: {e}")) })?; diff --git a/lib/registry/src/package/builder.rs b/lib/registry/src/package/builder.rs index d381a8c87c7..544b403e2e6 100644 --- a/lib/registry/src/package/builder.rs +++ b/lib/registry/src/package/builder.rs @@ -32,8 +32,8 @@ pub struct Publish { pub package_name: Option, /// Override the package version of the uploaded package in the wasmer.toml pub version: Option, - /// Override the token (by default, it will use the current logged in user) - pub token: Option, + /// The auth token to use. + pub token: String, /// Skip validation of the uploaded package pub no_validate: bool, /// Directory containing the `wasmer.toml` (defaults to current root dir) @@ -132,7 +132,13 @@ impl Publish { let mut policy = self.validation_policy(); if !policy.skip_validation() { - validate::validate_directory(&manifest, ®istry, manifest_dir, &mut *policy)?; + validate::validate_directory( + &manifest, + ®istry, + manifest_dir, + &mut *policy, + &self.token, + )?; } let archive_path = &archive_meta.archive_path; @@ -164,7 +170,7 @@ impl Publish { crate::publish::try_chunked_uploading( Some(registry), - self.token.clone(), + Some(self.token.clone()), &manifest.package, &archive_meta.manifest_toml, &archive_meta.license, @@ -602,6 +608,7 @@ mod validate { registry: &str, pkg_path: PathBuf, callbacks: &mut dyn ValidationPolicy, + auth_token: &str, ) -> anyhow::Result<()> { // validate as dir for module in manifest.modules.iter() { @@ -612,7 +619,7 @@ mod validate { } } - if would_change_package_privacy(manifest, registry)? + if would_change_package_privacy(manifest, registry, auth_token)? && callbacks.on_package_privacy_changed(manifest).is_break() { if manifest.package.private { @@ -631,9 +638,14 @@ mod validate { fn would_change_package_privacy( manifest: &wasmer_toml::Manifest, registry: &str, + auth_token: &str, ) -> Result { - let package_version = - crate::query_package_from_registry(registry, &manifest.package.name, None)?; + let package_version = crate::query_package_from_registry( + registry, + &manifest.package.name, + None, + Some(auth_token), + )?; Ok(package_version.is_private != manifest.package.private) }