faucet-cli 1.13.1

Config-driven CLI runner for faucet-stream pipelines (YAML / JSON, Meltano-style)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
//! `/v1/runs*` HTTP handlers. Thin glue: deserialize, call into `runner`/history,
//! map to status codes. All run-mutating logic lives in `runner.rs`.

use crate::serve::error::ServeError;
use crate::serve::history::{DeleteOutcome, ListFilter, RunRecord, RunStatus};
use crate::serve::rbac::AuthContext;
use crate::serve::runner::{self, ConfigFormatWire, SubmitOutcome, SubmitRequest};
use crate::serve::state::ServerState;
use axum::Json;
use axum::extract::{Extension, Path, Query, State};
use axum::http::StatusCode;
use axum::response::IntoResponse;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Scrub any resolved secret that reached a run's error fields before the record
/// is serialized into an HTTP response. The serve log subscriber's redaction
/// writer only covers tracing/log output — API response bodies are a separate
/// egress and must be scrubbed here.
fn redact_record(rec: &mut RunRecord) {
    if let Some(e) = &rec.error {
        rec.error = Some(crate::secrets::registry::redact(e).into_owned());
    }
    for inv in &mut rec.invocations {
        if let Some(e) = &inv.error {
            inv.error = Some(crate::secrets::registry::redact(e).into_owned());
        }
    }
}

/// `POST /v1/runs` → 202. The auth middleware injects the resolved
/// [`AuthContext`] so `submit` can attribute the `run.submit` audit record.
pub async fn submit_run(
    State(state): State<ServerState>,
    Extension(actor): Extension<AuthContext>,
    Json(req): Json<SubmitRequest>,
) -> Result<axum::response::Response, ServeError> {
    match runner::submit_gated(state, req, actor).await? {
        SubmitOutcome::Accepted(resp) => Ok((StatusCode::ACCEPTED, Json(resp)).into_response()),
        // The server (or the caller) wants approval first (#703): the body
        // is the pending change request, not a run.
        SubmitOutcome::PendingApproval(change) => Ok((
            StatusCode::ACCEPTED,
            Json(runner::pending_approval_body(&change)),
        )
            .into_response()),
    }
}

/// 404 unless a tenant-scoped principal (#709) owns the run. An unscoped
/// principal sees every run without a lookup.
pub(crate) async fn ensure_visible(
    state: &ServerState,
    actor: &AuthContext,
    id: &str,
) -> Result<(), ServeError> {
    if actor.tenant.is_none() {
        return Ok(());
    }
    let rec = state
        .history()
        .get(id)
        .await
        .map_err(|e| ServeError::Internal(e.to_string()))?
        .ok_or(ServeError::NotFound)?;
    if actor.sees_tenant(rec.tenant.as_deref()) {
        Ok(())
    } else {
        Err(ServeError::NotFound)
    }
}

/// `GET /v1/runs/{id}` → 200 RunRecord. Fills live `elapsed_secs` for running runs.
pub async fn get_run(
    State(state): State<ServerState>,
    Extension(actor): Extension<AuthContext>,
    Path(id): Path<String>,
) -> Result<Json<RunRecord>, ServeError> {
    let mut rec = state
        .history()
        .get(&id)
        .await
        .map_err(|e| ServeError::Internal(e.to_string()))?
        .ok_or(ServeError::NotFound)?;
    if !actor.sees_tenant(rec.tenant.as_deref()) {
        return Err(ServeError::NotFound);
    }
    if rec.status == RunStatus::Running
        && let Some(started) = rec.started_at
    {
        rec.elapsed_secs = (Utc::now() - started)
            .to_std()
            .ok()
            .map(|d| d.as_secs_f64());
    }
    redact_record(&mut rec);
    Ok(Json(rec))
}

/// `POST /v1/runs/{id}/cancel` → 202 (cancel requested) / 200 (terminal no-op) / 404.
/// In cluster mode, cancel is best-effort: the run may complete on its owning
/// instance before that instance processes the flag.
pub async fn cancel_run(
    State(state): State<ServerState>,
    Extension(actor): Extension<AuthContext>,
    Path(id): Path<String>,
) -> Result<impl IntoResponse, ServeError> {
    ensure_visible(&state, &actor, &id).await?;
    // 1. A live local token (this instance is running/queued it) → cancel now.
    if state.registry().cancel(&id) {
        crate::serve::audit::write(&state, &actor, "run.cancel", Some(id.clone()), None, "ok")
            .await;
        return Ok(StatusCode::ACCEPTED);
    }

    // 2. Look up the record.
    let rec = match state
        .history()
        .get(&id)
        .await
        .map_err(|e| ServeError::Internal(e.to_string()))?
    {
        Some(r) => r,
        None => return Err(ServeError::NotFound),
    };

    if rec.status.is_terminal() {
        return Ok(StatusCode::OK); // already done — no-op.
    }

    // 3. Cluster mode: the run is owned by another instance (or unclaimed).
    if state.cluster().enabled() {
        // Unclaimed (Pending) → cancel it directly.
        if state
            .history()
            .cancel_pending(&id)
            .await
            .map_err(|e| ServeError::Internal(e.to_string()))?
        {
            crate::serve::audit::write(&state, &actor, "run.cancel", Some(id.clone()), None, "ok")
                .await;
            // Terminal transition that bypasses `runner::finalize` entirely, so
            // it must fire the completion callback itself (#481).
            match state.history().get(&id).await {
                Ok(Some(rec)) => crate::serve::callback::fire(&rec).await,
                Ok(None) => {}
                Err(e) => tracing::warn!(
                    run_id = %id,
                    error = %e,
                    "could not read cancelled run for its completion callback"
                ),
            }
            return Ok(StatusCode::ACCEPTED);
        }
        // Otherwise it is running on a peer → flag it; the peer cancels on its
        // next claim-loop tick.
        state
            .history()
            .request_cancel(&id)
            .await
            .map_err(|e| ServeError::Internal(e.to_string()))?;
        crate::serve::audit::write(&state, &actor, "run.cancel", Some(id.clone()), None, "ok")
            .await;
        return Ok(StatusCode::ACCEPTED);
    }

    // Single-instance, non-terminal, no local token: a transient race (the token
    // was removed between the registry check and the history read as the run
    // completes). Treat as a no-op — the run is finishing or just finished.
    Ok(StatusCode::OK)
}

/// `DELETE /v1/runs/{id}` → 204 / 404 / 409 (still running).
pub async fn delete_run(
    State(state): State<ServerState>,
    Extension(actor): Extension<AuthContext>,
    Path(id): Path<String>,
) -> Result<StatusCode, ServeError> {
    ensure_visible(&state, &actor, &id).await?;
    match state
        .history()
        .delete(&id)
        .await
        .map_err(|e| ServeError::Internal(e.to_string()))?
    {
        DeleteOutcome::Deleted => {
            crate::serve::audit::write(&state, &actor, "run.delete", Some(id.clone()), None, "ok")
                .await;
            Ok(StatusCode::NO_CONTENT)
        }
        DeleteOutcome::NotFound => Err(ServeError::NotFound),
        DeleteOutcome::StillRunning => Err(ServeError::Conflict(
            "run is still in flight — cancel it before deleting".into(),
        )),
    }
}

/// Query-param wrapper for an RFC3339 timestamp. `application/x-www-form-urlencoded`
/// decodes `+` as a space, which corrupts explicit UTC offsets like `+05:30`; we
/// restore the `+` before parsing, so both `…Z` and `…+05:30` query values work.
#[derive(Debug, Clone, Copy)]
pub(crate) struct DateTimeUtcParam(DateTime<Utc>);

impl<'de> Deserialize<'de> for DateTimeUtcParam {
    fn deserialize<D: serde::Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let raw = String::deserialize(d)?;
        // Undo form-encoding's '+' → ' ' substitution before RFC3339 parsing.
        let restored = raw.replace(' ', "+");
        DateTime::parse_from_rfc3339(&restored)
            .map(|dt| DateTimeUtcParam(dt.to_utc()))
            .map_err(serde::de::Error::custom)
    }
}

/// `GET /v1/runs` query string.
#[derive(Debug, Deserialize)]
pub struct ListQuery {
    /// Comma-separated status names to include (e.g. `running,completed`); empty
    /// / absent = every status. An unknown token is a 400 — silently ignoring it
    /// would return *unfiltered* results to a caller who typo'd `failed`.
    pub status: Option<String>,
    pub name: Option<String>,
    pub(crate) since: Option<DateTimeUtcParam>,
    pub(crate) until: Option<DateTimeUtcParam>,
    pub limit: Option<usize>,
    pub cursor: Option<String>,
    /// Only runs started for this tenant (#709).
    pub tenant: Option<String>,
}

/// `GET /v1/runs` response body.
#[derive(Debug, Serialize)]
pub struct ListResponse {
    pub runs: Vec<RunRecord>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next_cursor: Option<String>,
}

const DEFAULT_LIMIT: usize = 50;
const MAX_LIMIT: usize = 500;

impl ListQuery {
    fn into_filter(self, actor: &AuthContext) -> Result<ListFilter, ServeError> {
        // Reject unknown status tokens instead of dropping them: a dropped
        // token leaves the vec empty, and an empty vec means "every status" —
        // a monitoring script that typo'd `?status=faild` would silently
        // receive ALL runs instead of an error.
        let status = match self.status.as_deref() {
            None => Vec::new(),
            Some(raw) => raw
                .split(',')
                .map(str::trim)
                .filter(|t| !t.is_empty())
                .map(|t| {
                    RunStatus::parse(t).ok_or_else(|| {
                        ServeError::BadConfig(format!(
                            "unknown status '{t}' — valid values: queued, pending, running, \
                             sharded, completed, failed, cancelled"
                        ))
                    })
                })
                .collect::<Result<Vec<_>, _>>()?,
        };
        Ok(ListFilter {
            status,
            name: self.name,
            since: self.since.map(|p| p.0),
            until: self.until.map(|p| p.0),
            limit: self.limit.unwrap_or(DEFAULT_LIMIT).clamp(1, MAX_LIMIT),
            cursor: self.cursor,
            tenant: actor.tenant_filter(self.tenant)?,
        })
    }
}

/// `GET /v1/runs` → 200.
pub async fn list_runs(
    State(state): State<ServerState>,
    Extension(actor): Extension<AuthContext>,
    Query(query): Query<ListQuery>,
) -> Result<Json<ListResponse>, ServeError> {
    let page = state
        .history()
        .list(&query.into_filter(&actor)?)
        .await
        .map_err(|e| ServeError::Internal(e.to_string()))?;
    let mut runs = page.runs;
    for rec in &mut runs {
        redact_record(rec);
    }
    Ok(Json(ListResponse {
        runs,
        next_cursor: page.next_cursor,
    }))
}

/// `POST /v1/runs/{id}/rollback` request body (#706).
#[derive(Debug, Deserialize, Default)]
pub struct RollbackRequest {
    /// The invocation to undo — one of the run's `invocations[].run_id`.
    /// Optional when the run has exactly one invocation.
    #[serde(default)]
    pub invocation_id: Option<String>,
    /// The row that invocation wrote (default: search every root's state).
    #[serde(default)]
    pub row: Option<String>,
    /// The pipeline config the run was made with. Optional when the server
    /// stored the run's config (cluster mode); required otherwise.
    #[serde(default)]
    pub config: Option<String>,
    #[serde(default)]
    pub config_format: ConfigFormatWire,
    #[serde(default)]
    pub dry_run: bool,
    #[serde(default)]
    pub force: bool,
}

/// `POST /v1/runs/{id}/rollback` → 200 with the
/// [`RollbackReport`](crate::rollback::RollbackReport) (`applied: false` +
/// `conflicts > 0` means the rollback was refused because a later run changed
/// the keys — pass `force`). Admin-only; audited as `run.rollback`.
pub async fn rollback_run(
    State(state): State<ServerState>,
    Extension(actor): Extension<AuthContext>,
    Path(id): Path<String>,
    body: Option<Json<RollbackRequest>>,
) -> Result<Json<crate::rollback::RollbackReport>, ServeError> {
    let req = body.map(|Json(b)| b).unwrap_or_default();
    let rec = state
        .history()
        .get(&id)
        .await
        .map_err(|e| ServeError::Internal(e.to_string()))?
        .ok_or(ServeError::NotFound)?;
    if !rec.status.is_terminal() {
        return Err(ServeError::Conflict(format!(
            "run {id} is {} — a run can only be rolled back once it has finished",
            rec.status.as_str()
        )));
    }
    // Which invocation: the caller's pick, else the only one.
    let known: Vec<String> = rec
        .invocations
        .iter()
        .filter_map(|i| i.run_id.clone())
        .collect();
    let invocation_id = match req.invocation_id {
        Some(inv) => {
            if !known.is_empty() && !known.contains(&inv) {
                return Err(ServeError::BadConfig(format!(
                    "invocation_id {inv} is not one of run {id}'s invocations ({})",
                    known.join(", ")
                )));
            }
            inv
        }
        None if known.len() == 1 => known[0].clone(),
        None if known.is_empty() => {
            return Err(ServeError::Unprocessable {
                message: format!(
                    "run {id} recorded no invocation ids (it predates rollback support, or \
                     no pipeline ran); pass `invocation_id` explicitly"
                ),
                details: None,
            });
        }
        None => {
            return Err(ServeError::BadConfig(format!(
                "run {id} has {} invocations — pass `invocation_id` (one of: {})",
                known.len(),
                known.join(", ")
            )));
        }
    };
    // Which config: the caller's, else the stored one (cluster mode).
    let (config, format) = match (req.config, rec.config_body.as_deref()) {
        (Some(c), _) => (c, req.config_format.into()),
        (None, Some(stored)) => (stored.to_string(), rec.config_format.unwrap_or_default()),
        (None, None) => {
            return Err(ServeError::Unprocessable {
                message: format!(
                    "run {id}'s config is not stored on this server; pass `config` (the \
                     pipeline config the run was made with) in the request body"
                ),
                details: None,
            });
        }
    };
    let loaded = crate::serve::load::load_submission(
        &config,
        format,
        state.default_base().as_ref(),
        crate::serve::runner::server_policy(&state).as_deref(),
    )
    .await?;
    let auth = crate::auth_catalog::build_auth_catalog(loaded.cfg.auth.as_ref())
        .map_err(|e| ServeError::BadConfig(e.to_string()))?;
    let pipeline_name = loaded
        .cfg
        .name
        .clone()
        .or(rec.name.clone())
        .unwrap_or_else(|| "pipeline".to_string());
    let (node, store, marker) = crate::rollback::locate(
        &loaded.nodes,
        &pipeline_name,
        &invocation_id,
        req.row.as_deref(),
    )
    .await
    .map_err(cli_to_serve)?;
    let report = crate::rollback::rollback_node(
        &node,
        store,
        marker,
        &crate::rollback::RollbackInputs {
            run_id: invocation_id,
            row: req.row,
            dry_run: req.dry_run,
            force: req.force,
            pipeline_name,
            auth,
        },
    )
    .await
    .map_err(cli_to_serve)?;
    let result = if req.dry_run {
        "dry_run"
    } else if report.outcome.applied {
        "ok"
    } else {
        "blocked"
    };
    crate::serve::audit::write(&state, &actor, "run.rollback", Some(id), None, result).await;
    Ok(Json(report))
}

/// Map a CLI-layer error to an HTTP-facing one: a bad config / missing run is
/// a client error (400); anything else is internal (500).
fn cli_to_serve(e: crate::error::CliError) -> ServeError {
    match e {
        crate::error::CliError::Config(m) => ServeError::BadConfig(m),
        other => ServeError::Internal(other.to_string()),
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn datetime_param_restores_form_encoded_plus() {
        // form-encoding turns the '+' of an offset into a space; the wrapper must
        // restore it so both offset and Z forms parse to the same UTC instant.
        let spaced: DateTimeUtcParam =
            serde_json::from_value(serde_json::json!("2026-01-01T00:00:00 05:30")).unwrap();
        let plus = chrono::DateTime::parse_from_rfc3339("2026-01-01T00:00:00+05:30")
            .unwrap()
            .to_utc();
        assert_eq!(spaced.0, plus);

        let zulu: DateTimeUtcParam =
            serde_json::from_value(serde_json::json!("2026-01-01T00:00:00Z")).unwrap();
        assert_eq!(
            zulu.0,
            chrono::DateTime::parse_from_rfc3339("2026-01-01T00:00:00Z")
                .unwrap()
                .to_utc()
        );
    }

    fn global() -> AuthContext {
        AuthContext::system("test")
    }

    #[test]
    fn list_query_scopes_a_tenant_principal() {
        let q = |tenant: Option<&str>| ListQuery {
            status: None,
            name: None,
            since: None,
            until: None,
            limit: None,
            cursor: None,
            tenant: tenant.map(str::to_string),
        };
        let mut scoped = global();
        scoped.tenant = Some("acme".into());
        assert_eq!(
            q(None).into_filter(&scoped).unwrap().tenant.as_deref(),
            Some("acme")
        );
        assert!(q(Some("globex")).into_filter(&scoped).is_err());
        assert_eq!(
            q(Some("globex"))
                .into_filter(&global())
                .unwrap()
                .tenant
                .as_deref(),
            Some("globex")
        );
    }

    #[test]
    fn list_query_clamps_limit() {
        let q = ListQuery {
            status: None,
            name: None,
            since: None,
            until: None,
            limit: Some(99999),
            cursor: None,
            tenant: None,
        };
        assert_eq!(q.into_filter(&global()).unwrap().limit, MAX_LIMIT);
        let q = ListQuery {
            status: Some("failed, completed".to_string()),
            name: None,
            since: None,
            until: None,
            limit: None,
            cursor: None,
            tenant: None,
        };
        let f = q.into_filter(&global()).unwrap();
        assert_eq!(f.limit, DEFAULT_LIMIT);
        assert_eq!(f.status, vec![RunStatus::Failed, RunStatus::Completed]);
    }

    #[test]
    fn list_query_rejects_unknown_status_token() {
        // A typo must be a 400, not a silently-unfiltered result: dropping the
        // token would leave the vec empty, and empty means "every status".
        let q = ListQuery {
            status: Some("faild".to_string()),
            name: None,
            since: None,
            until: None,
            limit: None,
            cursor: None,
            tenant: None,
        };
        let err = q.into_filter(&global()).unwrap_err();
        assert!(matches!(err, ServeError::BadConfig(ref m) if m.contains("faild")));
        // …and one bad token among good ones still rejects the request.
        let q = ListQuery {
            status: Some("failed,Completed".to_string()),
            name: None,
            since: None,
            until: None,
            limit: None,
            cursor: None,
            tenant: None,
        };
        assert!(q.into_filter(&global()).is_err(), "case-sensitive tokens");
        // A trailing comma (empty token) is tolerated.
        let q = ListQuery {
            status: Some("failed,".to_string()),
            name: None,
            since: None,
            until: None,
            limit: None,
            cursor: None,
            tenant: None,
        };
        assert_eq!(
            q.into_filter(&global()).unwrap().status,
            vec![RunStatus::Failed]
        );
    }

    #[tokio::test]
    async fn cancel_pending_run_in_cluster_mode_cancels_it() {
        use crate::serve::history::{RunRecord, RunStatus};
        use crate::serve::test_support::test_state_clustered;
        use chrono::Utc;

        let state = test_state_clustered();
        // A pending run with no local token (simulating an unclaimed cluster run).
        let mut rec = RunRecord::queued("p1".into(), None, Default::default(), None, Utc::now());
        rec.status = RunStatus::Pending;
        state.history().upsert(&rec).await.unwrap();

        let resp = cancel_run(
            axum::extract::State(state.clone()),
            axum::extract::Extension(AuthContext {
                principal: "test".into(),
                role: crate::serve::rbac::Role::Admin,
                source_ip: None,
                tenant: None,
            }),
            axum::extract::Path("p1".into()),
        )
        .await
        .unwrap()
        .into_response();
        assert_eq!(resp.status(), StatusCode::ACCEPTED);
        assert_eq!(
            state.history().get("p1").await.unwrap().unwrap().status,
            RunStatus::Cancelled
        );
    }
}