From f9448bf25b20fb63ddb057e32f9aba7370c3a1de Mon Sep 17 00:00:00 2001 From: hugocasa Date: Wed, 4 Nov 2026 20:34:23 +0100 Subject: [PATCH] feat: filter the audit log by run (#12190) * feat: filter the audit log by run Co-Authored-By: Claude Opus 5.5 * chore: update ee-repo-ref Co-Authored-By: Claude Opus 5.5 * docs: say a nested approval lists under its subflow Co-Authored-By: Claude Opus 5.5 * feat: put the run's audit logs in a menu on the left of its actions Co-Authored-By: Claude Opus 5.5 * perf: open a run's audit logs on the run's own time range Co-Authored-By: Claude Opus 5.5 * feat: include the run's steps and subflows in the run filter Co-Authored-By: Claude Opus 5.5 * feat: name each step and mark mocked or skipped steps in run audit entries Co-Authored-By: Claude Opus 5.5 * fix: show a step's id in the resource cell instead of next to the operation Co-Authored-By: Claude Opus 5.5 * fix: keep step details in the audit drawer and show its parameters as json Co-Authored-By: Claude Opus 5.5 * chore: update ee-repo-ref to 0bb5b777e634dadd154c0adfe6d26251560f9283 This commit updates the EE repository reference after PR #946 was merged in windmill-ee-private. Previous ee-repo-ref: b2f1890730a3368019183985161744187b5323a7 New ee-repo-ref: 0bb5b777e634dadd154c0adfe6d26251560f9283 Automated by sync-ee-ref workflow. --------- Co-authored-by: Claude Opus 5.5 Co-authored-by: windmill-internal-app[bot] Co-authored-by: Ruben Fiszel --- backend/ee-repo-ref.txt | 2 +- .../tests/audit.rs | 77 +++++++++++++ backend/windmill-api/openapi.yaml | 9 ++ backend/windmill-api/src/jobs.rs | 107 ++++++++++++------ backend/windmill-audit/src/lib.rs | 2 + backend/windmill-queue/src/jobs.rs | 21 +++- backend/windmill-types/src/jobs.rs | 9 +- backend/windmill-worker/src/worker_flow.rs | 62 ++++++---- .../auditLogs/AuditLogDetails.svelte | 9 +- .../auditLogs/AuditLogsFilters.svelte | 19 +++- .../auditLogs/useAuditLogsLoader.svelte.ts | 2 + .../src/lib/components/runs/RunDetail.svelte | 73 ++++++++---- frontend/src/lib/utils/uuid.ts | 4 + .../(root)/(logged)/audit_logs/+page.svelte | 5 + 14 files changed, 310 insertions(+), 91 deletions(-) diff --git a/backend/ee-repo-ref.txt b/backend/ee-repo-ref.txt index 6af6c609f0..325f547779 100644 --- a/backend/ee-repo-ref.txt +++ b/backend/ee-repo-ref.txt @@ -1 +1 @@ -0d933e22cac4578cb14d8bac36347c75d6736dfe +0bb5b777e634dadd154c0adfe6d26251560f9283 diff --git a/backend/windmill-api-integration-tests/tests/audit.rs b/backend/windmill-api-integration-tests/tests/audit.rs index 81c3ae70be..907eaf827e 100644 --- a/backend/windmill-api-integration-tests/tests/audit.rs +++ b/backend/windmill-api-integration-tests/tests/audit.rs @@ -135,3 +135,80 @@ async fn test_job_run_is_searchable_by_token_and_by_caller( Ok(()) } + +/// The run filter must find a run's entries under each key they keep its id in: the run itself +/// (`parameters.uuid`), a cancel (`resource`) and a resume (`parameters.job_id`), and only those. +#[cfg(all(feature = "enterprise", feature = "private"))] +#[sqlx::test(migrations = "../migrations", fixtures("base"))] +async fn test_audit_list_filters_by_run(db: Pool) -> anyhow::Result<()> { + use serde_json::json; + + initialize_tracing().await; + let server = ApiServer::start(db.clone()).await?; + let port = server.addr.port(); + let w = format!("http://localhost:{port}/api/w/test-workspace"); + + let mut ids = vec![]; + for _ in 0..2 { + let resp = authed(client().post(format!("{w}/jobs/run/preview"))) + .json( + &json!({ "content": "export function main() {}", "language": "deno", "args": {} }), + ) + .send() + .await?; + assert_eq!(resp.status(), 201); + ids.push(resp.text().await?); + } + let (run, other) = (&ids[0], &ids[1]); + + let resp = authed(client().post(format!("{w}/jobs_u/queue/cancel/{run}"))) + .json(&json!({})) + .send() + .await?; + assert_2xx(resp.status().as_u16(), &resp.text().await?, "cancel"); + // A subflow of the run and a step of that subflow, whose approval and start are filed under + // their own ids. + let (sub, step) = (uuid::Uuid::new_v4(), uuid::Uuid::new_v4()); + sqlx::query( + "INSERT INTO v2_job (id, workspace_id, parent_job) + VALUES ($1, 'test-workspace', $2::uuid), ($3, 'test-workspace', $1)", + ) + .bind(sub) + .bind(run) + .bind(step) + .execute(&db) + .await?; + sqlx::query( + "INSERT INTO audit_partitioned (workspace_id, username, operation, action_kind, parameters) + VALUES ('test-workspace', 'test-user', 'jobs.suspend_resume', 'update', $1), + ('test-workspace', 'test-user', 'jobs.run.script', 'execute', $2)", + ) + .bind(json!({ "job_id": sub })) + .bind(json!({ "uuid": step })) + .execute(&db) + .await?; + + let resp = authed(client().get(format!("{w}/audit/list?job_id={run}"))) + .send() + .await?; + assert_eq!(resp.status(), 200); + let mut ops = resp + .json::>() + .await? + .into_iter() + .map(|l| l["operation"].as_str().unwrap_or_default().to_string()) + .collect::>(); + ops.sort(); + assert_eq!( + ops, + [ + "jobs.cancel", + "jobs.run.preview", + "jobs.run.script", + "jobs.suspend_resume" + ], + "entries of {run} (not of {other})" + ); + + Ok(()) +} diff --git a/backend/windmill-api/openapi.yaml b/backend/windmill-api/openapi.yaml index 9d01fedddb..917ccfbcd2 100644 --- a/backend/windmill-api/openapi.yaml +++ b/backend/windmill-api/openapi.yaml @@ -282,6 +282,15 @@ paths: description: get audit logs for all workspaces schema: type: boolean + - name: job_id + in: query + description: > + only the entries about this run and the jobs under it (steps, loop iterations + and subflows at any depth): who started, viewed, cancelled, approved, resumed + or deleted each + schema: + type: string + format: uuid responses: "200": diff --git a/backend/windmill-api/src/jobs.rs b/backend/windmill-api/src/jobs.rs index 76f5440cdd..bf193ada35 100644 --- a/backend/windmill-api/src/jobs.rs +++ b/backend/windmill-api/src/jobs.rs @@ -5082,18 +5082,31 @@ pub async fn resume_suspended_flow_as_owner( job_id, &flow, value, - Some(authed.username), + Some(authed.username.clone()), true, &mut tx, ) .await?; + let (flow_id, flow_path) = (flow.id, flow.script_path.clone()); if is_wac { decrement_suspend(flow.id, &mut tx).await?; } else { resume_immediately_if_relevant(flow, job_id, &mut tx).await?; } + audit_suspend_resume( + &mut tx, + &AuditAuthor::from(&authed), + &w_id, + flow_path.as_deref(), + flow_id, + job_id, + true, + &authed.username, + ) + .await?; + tx.commit().await?; Ok(StatusCode::CREATED) } @@ -5234,6 +5247,7 @@ async fn resume_suspended( ) .await?; + let (flow_id, flow_path) = (flow.id, flow.script_path.clone()); if !approved { sqlx::query("UPDATE v2_job_queue SET suspend = 0 WHERE id = $1") .bind(&flow.id) @@ -5257,25 +5271,15 @@ async fn resume_suspended( } }; - audit_log( - &mut *tx, + audit_suspend_resume( + &mut tx, &audit_author, - "jobs.suspend_resume", - ActionKind::Update, &w_id, - Some( - &serde_json::json!({ - "approved": approved, - "job_id": job_id, - "details": if approved { - format!("Approved by {}", &approver) - } else { - format!("Cancelled by {}", &approver) - } - }) - .to_string(), - ), - None, + flow_path.as_deref(), + flow_id, + resume_job_id, + approved, + &approver, ) .await?; @@ -5788,6 +5792,7 @@ async fn resume_suspended_job_internal( ) .await?; + let (flow_id, flow_path) = (flow_info.id, flow_info.script_path.clone()); if !approved { sqlx::query!( "UPDATE v2_job_queue SET suspend = 0 WHERE id = $1", @@ -5820,31 +5825,61 @@ async fn resume_suspended_job_internal( .map(|s| s[0..TOKEN_PREFIX_LEN].to_string()), }, }; - audit_log( - &mut *tx, + audit_suspend_resume( + &mut tx, &audit_author, - "jobs.suspend_resume", - ActionKind::Update, &w_id, - Some( - &serde_json::json!({ - "approved": approved, - "job_id": job_id, - "details": if approved { - format!("Approved by {}", &approver) - } else { - format!("Cancelled by {}", &approver) - } - }) - .to_string(), - ), - None, + flow_path.as_deref(), + flow_id, + job_id, + approved, + &approver, ) .await?; tx.commit().await?; Ok(StatusCode::CREATED) } +/// `job_id` is the flow the suspended step belongs to, whichever id the resume request named; the +/// step itself is kept as `step_job_id`. +async fn audit_suspend_resume( + tx: &mut Transaction<'_, Postgres>, + author: &AuditAuthor, + w_id: &str, + flow_path: Option<&str>, + flow_id: Uuid, + step_job_id: Uuid, + approved: bool, + approver: &str, +) -> error::Result<()> { + let flow_id_s = flow_id.to_string(); + let step_job_id_s = step_job_id.to_string(); + let details = if approved { + format!("Approved by {approver}") + } else { + format!("Cancelled by {approver}") + }; + let mut params = HashMap::from([ + ("approved", if approved { "true" } else { "false" }), + ("job_id", flow_id_s.as_str()), + ("details", details.as_str()), + ]); + if step_job_id != flow_id { + params.insert("step_job_id", step_job_id_s.as_str()); + } + audit_log( + &mut **tx, + author, + "jobs.suspend_resume", + ActionKind::Update, + w_id, + flow_path, + Some(params), + ) + .await?; + Ok(()) +} + async fn verify_suspended_secret( w_id: &String, db: &DB, @@ -9213,7 +9248,7 @@ async fn run_preview_script( tx, &w_id, match preview.kind { - Some(PreviewKind::Identity) => JobPayload::Identity, + Some(PreviewKind::Identity) => JobPayload::Identity { mocked: false, skipped: false }, Some(PreviewKind::Noop) => JobPayload::Noop, _ => JobPayload::Code(RawCode { hash: preview diff --git a/backend/windmill-audit/src/lib.rs b/backend/windmill-audit/src/lib.rs index e9c71f1336..3b407b191f 100644 --- a/backend/windmill-audit/src/lib.rs +++ b/backend/windmill-audit/src/lib.rs @@ -42,4 +42,6 @@ pub struct ListAuditLogQuery { // without paying a growing OFFSET on every batch. pub before_id: Option, pub all_workspaces: Option, + // The entries about one run: started, viewed, cancelled, resumed, deleted... + pub job_id: Option, } diff --git a/backend/windmill-queue/src/jobs.rs b/backend/windmill-queue/src/jobs.rs index d4dbf5a6a1..b4b3a37abb 100644 --- a/backend/windmill-queue/src/jobs.rs +++ b/backend/windmill-queue/src/jobs.rs @@ -6719,6 +6719,10 @@ async fn push_inner<'c, 'd>( flow_timeout: Option, } let mut preprocessed = None; + let (identity_mocked, identity_skipped) = match &job_payload { + JobPayload::Identity { mocked, skipped } => (*mocked, *skipped), + _ => (false, false), + }; #[allow(unused)] let JobPayloadUntagged { runnable_id, @@ -7498,7 +7502,7 @@ async fn push_inner<'c, 'd>( ..Default::default() } } - JobPayload::Identity => { + JobPayload::Identity { .. } => { JobPayloadUntagged { job_kind: JobKind::Identity, ..Default::default() } } JobPayload::Noop => JobPayloadUntagged { job_kind: JobKind::Noop, ..Default::default() }, @@ -8067,6 +8071,21 @@ async fn push_inner<'c, 'd>( if let Some(client) = authed.and_then(|a| a.mcp_client.as_deref()) { hm.insert("mcp_client", client); } + // A step's entry names its step: a no-code step has no runnable path to show as the + // resource, and a script step's path does not say which step of which flow ran it. + let parent_job_string = parent_job.map(|p| p.to_string()); + if let Some(step) = flow_step_id.as_deref() { + hm.insert("step_id", step); + } + if let Some(p) = parent_job_string.as_deref() { + hm.insert("parent_job", p); + } + if identity_mocked { + hm.insert("mocked", "true"); + } + if identity_skipped { + hm.insert("skipped", "true"); + } let s: String; let operation_name = match job_kind { diff --git a/backend/windmill-types/src/jobs.rs b/backend/windmill-types/src/jobs.rs index 17e5235c5f..5b435ca383 100644 --- a/backend/windmill-types/src/jobs.rs +++ b/backend/windmill-types/src/jobs.rs @@ -631,7 +631,12 @@ pub enum JobPayload { // instead of serializing through a single workspace-wide key. concurrency_key_append: Option, }, - Identity, + /// Passes its input through. Also stands in for a step that is mocked or skipped, which + /// the audit entry records. + Identity { + mocked: bool, + skipped: bool, + }, Noop, AIAgent { path: String, @@ -690,7 +695,7 @@ impl JobPayload { pub fn job_kind(&self) -> JobKind { match self { JobPayload::Noop => JobKind::Noop, - JobPayload::Identity => JobKind::Identity, + JobPayload::Identity { .. } => JobKind::Identity, JobPayload::Code { .. } => JobKind::Preview, JobPayload::AIAgent { .. } => JobKind::AIAgent, JobPayload::FlowNode { .. } => JobKind::FlowNode, diff --git a/backend/windmill-worker/src/worker_flow.rs b/backend/windmill-worker/src/worker_flow.rs index c0a8519335..4691d1fe69 100644 --- a/backend/windmill-worker/src/worker_flow.rs +++ b/backend/windmill-worker/src/worker_flow.rs @@ -4045,16 +4045,23 @@ async fn push_next_flow_job( resume_messages.push(to_raw_value(&js)); audit_log( - &mut *tx, - &audit_author, - "jobs.suspend_resume", - ActionKind::Update, - &flow_job.workspace_id, - Some(&serde_json::json!({"approved": false, "job_id": flow_job.id, "details": "Suspend timed out without approval but can continue".to_string()}).to_string()), - None, - ) - .warn_after_seconds(3) - .await?; + &mut *tx, + &audit_author, + "jobs.suspend_resume", + ActionKind::Update, + &flow_job.workspace_id, + flow_job.runnable_path.as_deref(), + Some(HashMap::from([ + ("approved", "false"), + ("job_id", flow_job.id.to_string().as_str()), + ( + "details", + "Suspend timed out without approval but can continue", + ), + ])), + ) + .warn_after_seconds(3) + .await?; } sqlx::query!( @@ -4139,16 +4146,23 @@ async fn push_next_flow_job( } else { if is_disapproved.is_none() { audit_log( - &mut *tx, - &audit_author, - "jobs.suspend_resume", - ActionKind::Update, - &flow_job.workspace_id, - Some(&serde_json::json!({"approved": false, "job_id": flow_job.id, "details": "Suspend timed out without approval and is cancelled".to_string()}).to_string()), - None, - ) - .warn_after_seconds(3) - .await?; + &mut *tx, + &audit_author, + "jobs.suspend_resume", + ActionKind::Update, + &flow_job.workspace_id, + flow_job.runnable_path.as_deref(), + Some(HashMap::from([ + ("approved", "false"), + ("job_id", flow_job.id.to_string().as_str()), + ( + "details", + "Suspend timed out without approval and is cancelled", + ), + ])), + ) + .warn_after_seconds(3) + .await?; } tx.commit().warn_after_seconds(3).await?; @@ -5749,7 +5763,7 @@ async fn compute_next_flow_transform( if module.mock.is_some() && module.mock.as_ref().unwrap().enabled { return Ok(NextFlowTransform::Continue( ContinuePayload::SingleJob(JobPayloadWithTag { - payload: JobPayload::Identity, + payload: JobPayload::Identity { mocked: true, skipped: false }, tag: None, delete_after_use: false, delete_after_secs: None, @@ -5777,11 +5791,13 @@ async fn compute_next_flow_transform( tracing::debug!(id = %flow_job.id, "computing next flow transform for {:?}", &module.value); if is_skipped { - return trivial_next_job(JobPayload::Identity); + return trivial_next_job(JobPayload::Identity { mocked: false, skipped: true }); } match module.get_value()? { - FlowModuleValue::Identity => trivial_next_job(JobPayload::Identity), + FlowModuleValue::Identity => { + trivial_next_job(JobPayload::Identity { mocked: false, skipped: false }) + } FlowModuleValue::Flow { path, .. } => { let mut payload = flow_to_payload( path, diff --git a/frontend/src/lib/components/auditLogs/AuditLogDetails.svelte b/frontend/src/lib/components/auditLogs/AuditLogDetails.svelte index 508543b49b..1289deb222 100644 --- a/frontend/src/lib/components/auditLogs/AuditLogDetails.svelte +++ b/frontend/src/lib/components/auditLogs/AuditLogDetails.svelte @@ -3,6 +3,9 @@ import { type AuditLog } from '$lib/gen' import { displayDate } from '$lib/utils' import { ExternalLink, X } from 'lucide-svelte' + import { Highlight } from 'svelte-highlight' + import { json } from 'svelte-highlight/languages' + import HighlightTheme from '$lib/components/HighlightTheme.svelte' interface Props { logs: AuditLog[] @@ -21,6 +24,8 @@ const ViewAppOp: AuditLog['operation'][] = ['apps.create', 'apps.update'] + +
{#if selectedId} {@const log = logs.find((e) => e.id === selectedId)} @@ -59,8 +64,8 @@ {/if}
Parameters -
- {JSON.stringify(log.parameters, null, 2)} +
+
diff --git a/frontend/src/lib/components/auditLogs/AuditLogsFilters.svelte b/frontend/src/lib/components/auditLogs/AuditLogsFilters.svelte index e2e502b30c..ae1b7c5b11 100644 --- a/frontend/src/lib/components/auditLogs/AuditLogsFilters.svelte +++ b/frontend/src/lib/components/auditLogs/AuditLogsFilters.svelte @@ -40,6 +40,8 @@ import { usePromise } from '$lib/svelte5Utils.svelte' import { safeSelectItems } from '../select/utils.svelte' import { sendUserToast } from '$lib/toast' + import TextInput from '../text_input/TextInput.svelte' + import { isUuid } from '$lib/utils/uuid' let usernames: string[] | undefined = $state() let resources = usePromise(() => loadResources($workspaceStore!), { loadInit: false }) @@ -52,6 +54,8 @@ perPage?: number | undefined operation?: string resource?: string | undefined + /** Only the entries about this run. */ + jobId?: string | undefined actionKind?: ActionKind | 'all' scope?: undefined | 'all_workspaces' | 'instance' loading?: boolean @@ -68,6 +72,7 @@ perPage = $bindable(100), operation = $bindable(), resource = $bindable() as string | undefined, + jobId = $bindable(), actionKind = $bindable(undefined), scope = $bindable(undefined), loading = false, @@ -109,6 +114,7 @@ addQueryParam('after', after) addQueryParam('operation', operation) addQueryParam('resource', resource) + addQueryParam('job_id', jobId?.trim()) addQueryParam('actionKind', actionKind) if (scope && $workspaceStore == 'admins') { addQueryParam('scope', scope) @@ -128,6 +134,7 @@ perPage = Number(urlSearchParams.get('perPage')) || 100 operation = urlSearchParams.get('operation') ?? 'all' resource = urlSearchParams.get('resource') ?? undefined + jobId = urlSearchParams.get('job_id') ?? undefined actionKind = (urlSearchParams.get('actionKind') as ActionKind) ?? 'all' } @@ -265,7 +272,7 @@ // observe all the variables that should be reflected in the url $effect(() => { - ;[username, perPage, before, after, operation, resource, actionKind, scope] + ;[username, perPage, before, after, operation, resource, jobId, actionKind, scope] untrack(() => updateQueryParams()) }) @@ -374,6 +381,15 @@ />
+
+ +
+