From 0bf8682dafc7be5e2d8ff787238172f516e00b42 Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Sat, 8 Jun 2024 23:54:23 +0200 Subject: [PATCH 1/2] refactor(backend-api)!: Rename GetCurrentUser, add GetCurrentUserWithNamespaces Make the GetCurrentUser query only return a plain user without namespaces. Rename the current function/query to GetCurrentUserWithNamespaces, get_current_user_with_namespaces. This makes more sense , since the namespace query adds quite a lot of extra work. --- lib/backend-api/src/query.rs | 32 ++++++++++++++++++++++---------- lib/backend-api/src/types.rs | 14 ++++++++++---- 2 files changed, 32 insertions(+), 14 deletions(-) diff --git a/lib/backend-api/src/query.rs b/lib/backend-api/src/query.rs index e8774a1784c..0c7d0dd438e 100644 --- a/lib/backend-api/src/query.rs +++ b/lib/backend-api/src/query.rs @@ -164,7 +164,15 @@ pub async fn tag_package_release( .map(|r| r.tag_package_release) } -/// Get the currently logged in used, together with all accessible namespaces. +/// Get the currently logged in user. +pub async fn current_user(client: &WasmerClient) -> Result, anyhow::Error> { + client + .run_graphql(types::GetCurrentUser::build(())) + .await + .map(|x| x.viewer) +} + +/// Get the currently logged in user, together with all accessible namespaces. /// /// You can optionally filter the namespaces by the user role. pub async fn current_user_with_namespaces( @@ -172,9 +180,9 @@ pub async fn current_user_with_namespaces( namespace_role: Option, ) -> Result { client - .run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars { - namespace_role, - })) + .run_graphql(types::GetCurrentUserWithNamespaces::build( + types::GetCurrentUserWithNamespacesVars { namespace_role }, + )) .await? .viewer .context("not logged in") @@ -544,9 +552,11 @@ pub async fn user_accessible_apps( // Get all aps in user-accessible namespaces. let namespace_res = client - .run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars { - namespace_role: None, - })) + .run_graphql(types::GetCurrentUserWithNamespaces::build( + types::GetCurrentUserWithNamespacesVars { + namespace_role: None, + }, + )) .await?; let active_user = namespace_res.viewer.context("not logged in")?; let namespace_names = active_user @@ -655,9 +665,11 @@ pub async fn user_namespaces( client: &WasmerClient, ) -> Result, anyhow::Error> { let user = client - .run_graphql(types::GetCurrentUser::build(types::GetCurrentUserVars { - namespace_role: None, - })) + .run_graphql(types::GetCurrentUserWithNamespaces::build( + types::GetCurrentUserWithNamespacesVars { + namespace_role: None, + }, + )) .await? .viewer .context("not logged in")?; diff --git a/lib/backend-api/src/types.rs b/lib/backend-api/src/types.rs index 9f381c87dd9..6167c4e85ac 100644 --- a/lib/backend-api/src/types.rs +++ b/lib/backend-api/src/types.rs @@ -41,14 +41,20 @@ mod queries { Viewer, } + #[derive(cynic::QueryFragment, Debug)] + #[cynic(graphql_type = "Query")] + pub struct GetCurrentUser { + pub viewer: Option, + } + #[derive(cynic::QueryVariables, Debug)] - pub struct GetCurrentUserVars { + pub struct GetCurrentUserWithNamespacesVars { pub namespace_role: Option, } #[derive(cynic::QueryFragment, Debug)] - #[cynic(graphql_type = "Query", variables = "GetCurrentUserVars")] - pub struct GetCurrentUser { + #[cynic(graphql_type = "Query", variables = "GetCurrentUserWithNamespacesVars")] + pub struct GetCurrentUserWithNamespaces { pub viewer: Option, } @@ -447,7 +453,7 @@ mod queries { } #[derive(cynic::QueryFragment, Debug, Clone, Serialize)] - #[cynic(graphql_type = "User", variables = "GetCurrentUserVars")] + #[cynic(graphql_type = "User", variables = "GetCurrentUserWithNamespacesVars")] pub struct UserWithNamespaces { pub id: cynic::Id, pub username: String, From 10d3c3391bc35996f22fcbb795d0a9bc25a7d11d Mon Sep 17 00:00:00 2001 From: Christoph Herzog Date: Sat, 8 Jun 2024 23:56:14 +0200 Subject: [PATCH 2/2] fix(cli): Fix app resolution - respect owner in config The backend refactor that removed global aliases was not fully reflected in the CLI, breaking "wasmer app get" and similar commands that all back the reading the local app.yaml file. This commit * Removes the `Alias` variant of AppIdent, since there conceptually are no global aliases anymore, just domains * Makes the resolution respect the `owner` in the config * Falls back to trying to get the app by name with the current user as the owner --- lib/cli/src/commands/app/util.rs | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/lib/cli/src/commands/app/util.rs b/lib/cli/src/commands/app/util.rs index 514f0c286be..c4f93c94b28 100644 --- a/lib/cli/src/commands/app/util.rs +++ b/lib/cli/src/commands/app/util.rs @@ -23,7 +23,7 @@ pub enum AppIdent { /// Backend app VERSION id like "dav_xxysw34234" AppVersionId(String), NamespacedName(String, String), - Alias(String), + Name(String), } impl AppIdent { @@ -40,9 +40,18 @@ impl AppIdent { .with_context(|| format!("Could not query for app version id '{}'", id))?; Ok(app) } - AppIdent::Alias(name) => wasmer_api::query::get_app_by_alias(client, name.clone()) - .await? - .with_context(|| format!("Could not find app with name '{name}'")), + AppIdent::Name(name) => { + // The API only allows to query by owner + name, + // so default to the current user as the owner. + // To to so the username must first be retrieved. + let user = wasmer_api::query::current_user(client) + .await? + .context("not logged in")?; + + wasmer_api::query::get_app(client, user.username, name.clone()) + .await? + .with_context(|| format!("Could not find app with name '{name}'")) + } AppIdent::NamespacedName(owner, name) => { wasmer_api::query::get_app(client, owner.clone(), name.clone()) .await? @@ -80,7 +89,7 @@ impl std::str::FromStr for AppIdent { } } } else { - Ok(Self::Alias(s.to_string())) + Ok(Self::Name(s.to_string())) } } } @@ -160,8 +169,10 @@ impl AppIdentOpts { let ident = if let Some(id) = &config.app_id { AppIdent::AppId(id.clone()) + } else if let Some(owner) = &config.owner { + AppIdent::NamespacedName(owner.clone(), config.name.clone()) } else { - AppIdent::Alias(config.name.clone()) + AppIdent::Name(config.name.clone()) }; Ok(ResolvedAppIdent::Config { @@ -197,7 +208,7 @@ mod tests { ); assert_eq!( AppIdent::from_str("lala").unwrap(), - AppIdent::Alias("lala".to_string()), + AppIdent::Name("lala".to_string()), ); assert_eq!(