From a71e6f27ec26586c16ef5bc660e7157a4fe429d3 Mon Sep 17 00:00:00 2001 From: Ruben Fiszel Date: Tue, 17 Jan 2023 11:30:22 +0100 Subject: [PATCH] various fixes --- backend/tests/worker.rs | 4 +- backend/windmill-api/src/jobs.rs | 7 ++- backend/windmill-api/src/workspaces.rs | 65 +++++++++++----------- backend/windmill-worker/src/worker.rs | 23 +++----- backend/windmill-worker/src/worker_flow.rs | 1 - 5 files changed, 47 insertions(+), 53 deletions(-) diff --git a/backend/tests/worker.rs b/backend/tests/worker.rs index 84d32eb87a..e621bdbcd2 100644 --- a/backend/tests/worker.rs +++ b/backend/tests/worker.rs @@ -262,7 +262,7 @@ mod suspend_resume { // print_job(second, &db).await; let tx = db.begin().await.unwrap(); - let (tx, token) = windmill_worker::create_token_for_owner(tx, "test-workspace", "u/test-user", "", 100, "").await.unwrap(); + let (tx, token) = windmill_worker::create_token_for_owner(tx, "test-workspace", "u/test-user", "", 100).await.unwrap(); tx.commit().await.unwrap(); let secret = reqwest::get(format!( "http://localhost:{port}/api/w/test-workspace/jobs/job_signature/{second}/0?token={token}&approver=ruben" @@ -365,7 +365,7 @@ mod suspend_resume { let second = completed.next().await.unwrap(); let tx = db.begin().await.unwrap(); - let (tx, token) = windmill_worker::create_token_for_owner(tx, "test-workspace", "u/test-user", "", 100, "").await.unwrap(); + let (tx, token) = windmill_worker::create_token_for_owner(tx, "test-workspace", "u/test-user", "", 100).await.unwrap(); tx.commit().await.unwrap(); let secret = reqwest::get(format!( "http://localhost:{port}/api/w/test-workspace/jobs/job_signature/{second}/0?token={token}" diff --git a/backend/windmill-api/src/jobs.rs b/backend/windmill-api/src/jobs.rs index 5d6dd73c3f..565d8499cd 100644 --- a/backend/windmill-api/src/jobs.rs +++ b/backend/windmill-api/src/jobs.rs @@ -1132,6 +1132,11 @@ pub async fn run_flow_by_path( let scheduled_for = run_query.get_scheduled_for(&mut tx).await?; let args = run_query.add_include_headers(headers, args.unwrap_or_default()); + let permissioned_as = if authed.username.contains('@') { + authed.username.clone() + } else { + owner_to_token_owner(&authed.username, false) + }; let (uuid, tx) = push( tx, &w_id, @@ -1139,7 +1144,7 @@ pub async fn run_flow_by_path( args, &authed.username, &authed.email, - owner_to_token_owner(&authed.username, false), + permissioned_as, scheduled_for, None, run_query.parent_job, diff --git a/backend/windmill-api/src/workspaces.rs b/backend/windmill-api/src/workspaces.rs index de203bf38e..15940e2ab2 100644 --- a/backend/windmill-api/src/workspaces.rs +++ b/backend/windmill-api/src/workspaces.rs @@ -206,40 +206,39 @@ async fn premium_info( } async fn stripe_checkout(authed: Authed, Extension(base_url): Extension>) { - let client = stripe::Client::new("sk_test_51MCt8dGU3NdFi9eLNhKb8RXkvb267we5HyeJ46wyLCESokSLsqsY2EKdcOsapepcEBlUYTQXJ8957tYN6h3zE2ir00Nwfl41w0"); - // finally, create a checkout session for this product / price - let success_rd = format!( - "{}/workspace_settings?session={{CHECKOUT_SESSION_ID}}", - base_url.0 - ); - let failure_rd = format!("{}/workspace_settings", base_url.0); - let checkout_session = { - let mut params = stripe::CreateCheckoutSession::new(&failure_rd, &success_rd); - params.mode = Some(stripe::CheckoutSessionMode::Subscription); - params.line_items = Some(vec![ - stripe::CreateCheckoutSessionLineItems { - quantity: None, - price: Some("price_1MQzMHGU3NdFi9eLWFC7IXEv".to_string()), - ..Default::default() - }, - stripe::CreateCheckoutSessionLineItems { - quantity: None, - price: Some("price_1MR2BZGU3NdFi9eLNRuibxPx".to_string()), - ..Default::default() - }, - ]); - params.customer_email = Some(&authed.email); - params.client_reference_id = Some("foo"); - stripe::CheckoutSession::create(&client, params) - .await - .unwrap() - }; + // let client = stripe::Client::new(std::env::var("STRIPE_KEY").expect("STRIPE_KEY")); + // let success_rd = format!( + // "{}/workspace_settings?session={{CHECKOUT_SESSION_ID}}", + // base_url.0 + // ); + // let failure_rd = format!("{}/workspace_settings", base_url.0); + // let checkout_session = { + // let mut params = stripe::CreateCheckoutSession::new(&failure_rd, &success_rd); + // params.mode = Some(stripe::CheckoutSessionMode::Subscription); + // params.line_items = Some(vec![ + // stripe::CreateCheckoutSessionLineItems { + // quantity: None, + // price: Some("price_1MQzMHGU3NdFi9eLWFC7IXEv".to_string()), + // ..Default::default() + // }, + // stripe::CreateCheckoutSessionLineItems { + // quantity: None, + // price: Some("price_1MR2BZGU3NdFi9eLNRuibxPx".to_string()), + // ..Default::default() + // }, + // ]); + // params.customer_email = Some(&authed.email); + // params.client_reference_id = Some("foo"); + // stripe::CheckoutSession::create(&client, params) + // .await + // .unwrap() + // }; - println!( - "created a {} at {}", - checkout_session.payment_status, - checkout_session.url.unwrap() - ); + // println!( + // "created a {} at {}", + // checkout_session.payment_status, + // checkout_session.url.unwrap() + // ); } async fn exists_workspace( diff --git a/backend/windmill-worker/src/worker.rs b/backend/windmill-worker/src/worker.rs index 30c67902b5..8135a627e5 100644 --- a/backend/windmill-worker/src/worker.rs +++ b/backend/windmill-worker/src/worker.rs @@ -231,18 +231,14 @@ pub async fn create_token_for_owner<'c>( owner: &str, label: &str, expires_in: i32, - username: &str, ) -> error::Result<(Transaction<'c, Postgres>, String)> { // TODO: Bad implementation. We should not have access to this DB here. let token: String = rd_string(30); - let is_super_admin = username.contains('@') - && sqlx::query_scalar!( - "SELECT super_admin FROM password WHERE email = $1", - owner.split_once('/').map(|x| x.1).unwrap_or("") - ) - .fetch_optional(&mut tx) - .await? - .unwrap_or(false); + let is_super_admin = owner.contains('@') + && sqlx::query_scalar!("SELECT super_admin FROM password WHERE email = $1", owner) + .fetch_optional(&mut tx) + .await? + .unwrap_or(false); sqlx::query_scalar!( "INSERT INTO token @@ -571,7 +567,6 @@ pub async fn run_worker( &job.permissioned_as, "ephemeral-script", timeout * 2, - &job.created_by, ) .await.expect("could not create job token"); tx.commit().await.expect("could not commit job token"); @@ -944,13 +939,10 @@ async fn transform_json_value( let v = client .get_variable(workspace, path, Some(true)) .await - .map_err(to_anyhow) + .map_err(|_| Error::NotFound(format!("Variable {path} not found"))) .map(|v| v.into_inner())? .value - .map_or_else( - || Err(Error::NotFound(format!("Variable {path} not found"))), - |e| Ok(e), - )?; + .unwrap_or_else(|| String::new()); Ok(Value::String(v)) } Value::String(y) if y.starts_with("$res:") => { @@ -2597,7 +2589,6 @@ async fn handle_zombie_jobs(db: &Pool, timeout: i32, base_url: &str) { &job.permissioned_as, "ephemeral-zombie-jobs", timeout * 2, - &job.created_by, ) .await .expect("could not create job token"); diff --git a/backend/windmill-worker/src/worker_flow.rs b/backend/windmill-worker/src/worker_flow.rs index db2e372cde..adc4f73d49 100644 --- a/backend/windmill-worker/src/worker_flow.rs +++ b/backend/windmill-worker/src/worker_flow.rs @@ -1881,7 +1881,6 @@ async fn get_transform_context( &flow_job.permissioned_as, "transform-input", 10, - &flow_job.created_by, ) .await?; //we need to commit asap otherwise the token won't be valid for auth to check outside of this transaction