Skip to content

Commit

Permalink
fix(cli): Pass auth token when querying packages
Browse files Browse the repository at this point in the history
Need to pass an auth token to the package query, otherwise it would fail
due to not being able to query the private package.
  • Loading branch information
theduke committed Nov 13, 2023
1 parent 9f7b7eb commit 2de48f4
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 16 deletions.
16 changes: 10 additions & 6 deletions lib/cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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.
Expand Down
8 changes: 7 additions & 1 deletion lib/cli/src/commands/publish.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use anyhow::Context as _;
use clap::Parser;
use wasmer_registry::wasmer_env::WasmerEnv;

Expand Down Expand Up @@ -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(),
};
Expand Down
5 changes: 3 additions & 2 deletions lib/registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ pub fn query_package_from_registry(
registry_url: &str,
name: &str,
version: Option<&str>,
auth_token: Option<&str>,
) -> Result<PackageDownloadInfo, QueryPackageError> {
use crate::graphql::{
execute_query,
Expand All @@ -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}"))
})?;

Expand Down
26 changes: 19 additions & 7 deletions lib/registry/src/package/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub struct Publish {
pub package_name: Option<String>,
/// Override the package version of the uploaded package in the wasmer.toml
pub version: Option<semver::Version>,
/// Override the token (by default, it will use the current logged in user)
pub token: Option<String>,
/// 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)
Expand Down Expand Up @@ -132,7 +132,13 @@ impl Publish {
let mut policy = self.validation_policy();

if !policy.skip_validation() {
validate::validate_directory(&manifest, &registry, manifest_dir, &mut *policy)?;
validate::validate_directory(
&manifest,
&registry,
manifest_dir,
&mut *policy,
&self.token,
)?;
}

let archive_path = &archive_meta.archive_path;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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() {
Expand All @@ -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 {
Expand All @@ -631,9 +638,14 @@ mod validate {
fn would_change_package_privacy(
manifest: &wasmer_toml::Manifest,
registry: &str,
auth_token: &str,
) -> Result<bool, ValidationError> {
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)
}
Expand Down

0 comments on commit 2de48f4

Please sign in to comment.