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
//! Validated form of a [`TriggersFile`]. `compile` surfaces every problem at
//! startup (unique names, webhook-path collisions, resolvable pipeline ref,
//! interval/threshold bounds, missing backend feature) so a watcher never fails
//! mid-run from a config mistake. Pure (no IO except reading a path's existence,
//! which is done by the caller; here we only validate shapes).
//!
//! Name charset: trigger names must match `^[A-Za-z0-9_-]+$`; they are embedded
//! verbatim into the webhook route `/v1/triggers/{name}`, so whitespace or slashes
//! would silently break routing.

use super::spec::{PipelineRef, QueueSpec, StoreSpec, TriggerKind, TriggerSpec, TriggersFile};
use std::collections::HashSet;

#[derive(Debug)]
pub struct CompiledTriggers {
    pub triggers: Vec<CompiledTrigger>,
}

#[derive(Debug, Clone)]
pub struct CompiledTrigger {
    pub spec: TriggerSpec,
    /// For webhook triggers: the route path `/v1/triggers/{name}`.
    pub webhook_path: Option<String>,
}

impl CompiledTrigger {
    pub fn name(&self) -> &str {
        &self.spec.name
    }
    pub fn kind_label(&self) -> &'static str {
        match self.spec.kind {
            TriggerKind::ObjectArrival { .. } => "object_arrival",
            TriggerKind::Webhook { .. } => "webhook",
            TriggerKind::QueueDepth { .. } => "queue_depth",
            TriggerKind::Schedule { .. } => "schedule",
        }
    }
}

impl CompiledTriggers {
    /// Validate a parsed file. `err` strings are user-facing.
    pub fn compile(file: TriggersFile) -> Result<Self, String> {
        if file.version != 1 {
            return Err(format!(
                "triggers: unsupported version {} (expected 1)",
                file.version
            ));
        }
        let mut names = HashSet::new();
        let mut compiled = Vec::with_capacity(file.triggers.len());

        for t in file.triggers {
            if t.name.trim().is_empty() {
                return Err("triggers: a trigger has an empty `name`".into());
            }
            if !t
                .name
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
            {
                return Err(format!(
                    "triggers: invalid trigger name '{}' (letters, digits, '_' and '-' only)",
                    t.name
                ));
            }
            if !names.insert(t.name.clone()) {
                return Err(format!("triggers: duplicate trigger name '{}'", t.name));
            }
            // Exactly one of a pipeline ref and a template; the ref non-empty.
            match (&t.config, &t.template) {
                (Some(_), Some(_)) => {
                    return Err(format!(
                        "triggers: '{}' sets both `config` and `template`; pick one",
                        t.name
                    ));
                }
                (None, None) => {
                    return Err(format!(
                        "triggers: '{}' needs a `config` or a `template`",
                        t.name
                    ));
                }
                (Some(PipelineRef::Path(p)), None) if p.trim().is_empty() => {
                    return Err(format!("triggers: '{}' has an empty config path", t.name));
                }
                (Some(PipelineRef::Inline(v)), None) if !v.is_object() => {
                    return Err(format!(
                        "triggers: '{}' inline config must be a mapping",
                        t.name
                    ));
                }
                (None, Some(tpl)) => {
                    require_feature(&t.name, "templates")?;
                    if tpl.id.trim().is_empty() {
                        return Err(format!("triggers: '{}' template id is empty", t.name));
                    }
                }
                _ => {}
            }
            if let Some(sel) = &t.tenants {
                require_feature(&t.name, "tenants")?;
                sel.validate()
                    .map_err(|e| format!("triggers: '{}' {e}", t.name))?;
            }

            let webhook_path = match &t.kind {
                TriggerKind::ObjectArrival {
                    poll_interval_secs,
                    store,
                    ..
                } => {
                    if *poll_interval_secs == 0 {
                        return Err(format!(
                            "triggers: '{}' poll_interval_secs must be >= 1",
                            t.name
                        ));
                    }
                    require_feature(&t.name, store_feature(store))?;
                    validate_store(&t.name, store)?;
                    None
                }
                TriggerKind::Webhook { methods, .. } => {
                    if methods.is_empty() {
                        return Err(format!("triggers: '{}' methods must not be empty", t.name));
                    }
                    for m in methods {
                        let mu = m.to_ascii_uppercase();
                        if mu != "POST" && mu != "PUT" {
                            return Err(format!(
                                "triggers: '{}' unsupported webhook method '{}' (POST|PUT)",
                                t.name, m
                            ));
                        }
                    }
                    // Paths are unique because trigger names are already deduplicated above.
                    let path = format!("/v1/triggers/{}", t.name);
                    Some(path)
                }
                TriggerKind::QueueDepth {
                    poll_interval_secs,
                    queue,
                    threshold,
                } => {
                    if *poll_interval_secs == 0 {
                        return Err(format!(
                            "triggers: '{}' poll_interval_secs must be >= 1",
                            t.name
                        ));
                    }
                    if *threshold == 0 {
                        return Err(format!("triggers: '{}' threshold must be >= 1", t.name));
                    }
                    require_feature(&t.name, queue_feature(queue))?;
                    validate_queue(&t.name, queue)?;
                    None
                }
                TriggerKind::Schedule { cron, timezone } => {
                    require_feature(&t.name, "schedule")?;
                    validate_schedule(&t.name, cron, timezone)?;
                    None
                }
            };

            compiled.push(CompiledTrigger {
                spec: t,
                webhook_path,
            });
        }
        Ok(Self { triggers: compiled })
    }

    pub fn webhooks(&self) -> impl Iterator<Item = &CompiledTrigger> {
        self.triggers.iter().filter(|t| t.webhook_path.is_some())
    }
}

fn store_feature(store: &StoreSpec) -> &'static str {
    match store {
        StoreSpec::S3 { .. } | StoreSpec::Gcs { .. } => "triggers-object-store",
    }
}

fn queue_feature(queue: &QueueSpec) -> &'static str {
    match queue {
        QueueSpec::Redis { .. } => "triggers-redis",
        QueueSpec::Kafka { .. } => "triggers-kafka",
    }
}

/// Validates that non-optional string fields in a `StoreSpec` are non-empty.
fn validate_store(name: &str, store: &StoreSpec) -> Result<(), String> {
    match store {
        StoreSpec::S3 { bucket, .. } => {
            if bucket.trim().is_empty() {
                return Err(format!("triggers: '{name}' store bucket must not be empty"));
            }
        }
        StoreSpec::Gcs { bucket, .. } => {
            if bucket.trim().is_empty() {
                return Err(format!("triggers: '{name}' store bucket must not be empty"));
            }
        }
    }
    Ok(())
}

/// Validates that non-optional string fields in a `QueueSpec` are non-empty.
fn validate_queue(name: &str, queue: &QueueSpec) -> Result<(), String> {
    match queue {
        QueueSpec::Redis { url, key, .. } => {
            if url.trim().is_empty() {
                return Err(format!("triggers: '{name}' queue url must not be empty"));
            }
            if key.trim().is_empty() {
                return Err(format!("triggers: '{name}' queue key must not be empty"));
            }
        }
        QueueSpec::Kafka {
            brokers,
            topic,
            group,
        } => {
            if brokers.trim().is_empty() {
                return Err(format!(
                    "triggers: '{name}' queue brokers must not be empty"
                ));
            }
            if topic.trim().is_empty() {
                return Err(format!("triggers: '{name}' queue topic must not be empty"));
            }
            if group.trim().is_empty() {
                return Err(format!("triggers: '{name}' queue group must not be empty"));
            }
        }
    }
    Ok(())
}

/// Returns Ok if the named feature is compiled in; else a clear error naming it.
/// Compile a `schedule` trigger's cron + timezone the way `faucet schedule`
/// does, so both runtimes accept exactly the same expressions.
#[cfg(feature = "schedule")]
pub fn compile_schedule(
    trigger: &str,
    cron: &str,
    timezone: &str,
) -> Result<crate::schedule::compiled::CompiledSchedule, String> {
    let spec: crate::schedule::spec::ScheduleSpec =
        serde_json::from_value(serde_json::json!({ "cron": cron, "timezone": timezone }))
            .map_err(|e| format!("triggers: '{trigger}' {e}"))?;
    crate::schedule::compiled::CompiledSchedule::compile(&spec)
        .map_err(|e| format!("triggers: '{trigger}' {e}"))
}

#[cfg(feature = "schedule")]
fn validate_schedule(trigger: &str, cron: &str, timezone: &str) -> Result<(), String> {
    compile_schedule(trigger, cron, timezone).map(|_| ())
}

#[cfg(not(feature = "schedule"))]
fn validate_schedule(_trigger: &str, _cron: &str, _timezone: &str) -> Result<(), String> {
    Ok(())
}

fn require_feature(trigger: &str, feature: &str) -> Result<(), String> {
    let compiled = match feature {
        "triggers" => cfg!(feature = "triggers"),
        "triggers-object-store" => cfg!(feature = "triggers-object-store"),
        "triggers-redis" => cfg!(feature = "triggers-redis"),
        "triggers-kafka" => cfg!(feature = "triggers-kafka"),
        "schedule" => cfg!(feature = "schedule"),
        "templates" => cfg!(feature = "templates"),
        "tenants" => cfg!(feature = "tenants"),
        _ => true,
    };
    if compiled {
        Ok(())
    } else {
        Err(format!(
            "triggers: '{trigger}' requires a build with the `{feature}` feature"
        ))
    }
}

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

    fn file(yaml: &str) -> TriggersFile {
        serde_yaml::from_str(yaml).unwrap()
    }

    fn compile_err(yaml: &str) -> String {
        CompiledTriggers::compile(file(yaml)).unwrap_err()
    }

    #[test]
    fn config_template_tenants_and_schedule_are_validated() {
        let base = "version: 1\ntriggers:\n  - name: t\n    type: webhook\n";
        assert!(
            compile_err(&format!(
                "{base}    config: ./x.yaml\n    template: {{ id: a }}\n"
            ))
            .contains("both")
        );
        assert!(compile_err(base).contains("needs a `config` or a `template`"));
        assert!(
            compile_err(&format!("{base}    template: {{ id: \" \" }}\n"))
                .contains("template id is empty")
        );
        assert!(
            compile_err(&format!("{base}    config: ./x.yaml\n    tenants: every\n"))
                .contains("\"all\"")
        );
        let ok = CompiledTriggers::compile(file(&format!(
            "{base}    template: {{ id: tpl }}\n    tenants: all\n"
        )))
        .unwrap();
        assert!(ok.triggers[0].spec.template.is_some());
        let sched =
            "version: 1\ntriggers:\n  - name: s\n    type: schedule\n    config: ./x.yaml\n";
        assert!(
            compile_err(&format!("{sched}    cron: \"not a cron\"\n")).contains("invalid cron")
        );
        assert!(
            compile_err(&format!(
                "{sched}    cron: \"* * * * *\"\n    timezone: Mars/Base\n"
            ))
            .contains("timezone")
        );
        let c =
            CompiledTriggers::compile(file(&format!("{sched}    cron: \"0 * * * *\"\n"))).unwrap();
        assert_eq!(c.triggers[0].kind_label(), "schedule");
        assert!(c.triggers[0].webhook_path.is_none());
    }

    #[test]
    fn rejects_duplicate_names() {
        let f = file(
            "version: 1\ntriggers:\n  - { name: a, type: webhook, config: ./x.yaml }\n  - { name: a, type: webhook, config: ./y.yaml }\n",
        );
        let err = CompiledTriggers::compile(f).unwrap_err();
        assert!(err.contains("duplicate trigger name 'a'"), "{err}");
    }

    #[test]
    fn rejects_bad_version() {
        let f = file("version: 2\ntriggers: []\n");
        assert!(
            CompiledTriggers::compile(f)
                .unwrap_err()
                .contains("version")
        );
    }

    #[test]
    fn webhook_gets_path_and_collisions_rejected() {
        // Two webhooks with distinct names → distinct paths, both compile.
        let f = file(
            "version: 1\ntriggers:\n  - { name: a, type: webhook, config: ./x.yaml }\n  - { name: b, type: webhook, config: ./y.yaml }\n",
        );
        let c = CompiledTriggers::compile(f).unwrap();
        assert_eq!(c.webhooks().count(), 2);
        assert_eq!(
            c.triggers[0].webhook_path.as_deref(),
            Some("/v1/triggers/a")
        );
        assert_eq!(
            c.triggers[1].webhook_path.as_deref(),
            Some("/v1/triggers/b")
        );
    }

    #[test]
    fn rejects_zero_threshold() {
        let f = file(
            "version: 1\ntriggers:\n  - name: q\n    type: queue_depth\n    config: ./x.yaml\n    threshold: 0\n    queue: { type: redis, url: \"redis://x\", key: k }\n",
        );
        // Only assert the threshold check when the redis backend is compiled;
        // otherwise the missing-feature error fires first (also acceptable).
        let err = CompiledTriggers::compile(f).unwrap_err();
        assert!(
            err.contains("threshold must be >= 1") || err.contains("triggers-redis"),
            "{err}"
        );
    }

    #[test]
    fn rejects_empty_webhook_methods() {
        let f = file(
            "version: 1\ntriggers:\n  - name: hook\n    type: webhook\n    config: ./x.yaml\n    methods: []\n",
        );
        let err = CompiledTriggers::compile(f).unwrap_err();
        assert!(err.contains("methods must not be empty"), "{err}");
    }

    #[test]
    fn rejects_empty_bucket() {
        let f = file(
            "version: 1\ntriggers:\n  - name: obj\n    type: object_arrival\n    config: ./x.yaml\n    store: { type: s3, bucket: \"\" }\n",
        );
        let err = CompiledTriggers::compile(f).unwrap_err();
        assert!(
            err.contains("store bucket must not be empty") || err.contains("triggers-object-store"),
            "{err}"
        );
    }

    #[test]
    fn rejects_empty_redis_url() {
        let f = file(
            "version: 1\ntriggers:\n  - name: q\n    type: queue_depth\n    config: ./x.yaml\n    queue: { type: redis, url: \"\", key: k }\n",
        );
        let err = CompiledTriggers::compile(f).unwrap_err();
        assert!(
            err.contains("queue url must not be empty") || err.contains("triggers-redis"),
            "{err}"
        );
    }

    #[test]
    fn rejects_name_with_slash() {
        let f = file(
            "version: 1\ntriggers:\n  - { name: \"foo/bar\", type: webhook, config: ./x.yaml }\n",
        );
        let err = CompiledTriggers::compile(f).unwrap_err();
        assert!(err.contains("invalid trigger name 'foo/bar'"), "{err}");
    }
}