dataferry 0.1.0-beta.1

Data migration engine: move and reshape data from MongoDB and Postgres into Postgres, resumably, one committed partition at a time. The `dataferry` command line.
Documentation
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
//! `dataferry validate`: everything that can be checked without connecting
//! (0004 ยง6). Loads the directory (B1), substitutes `${ENV}`, then builds
//! every connection through the registry with the roles its flows use.
//! `build` must not do I/O, so nothing here ever connects. Reports every
//! error it finds, not just the first.

use std::cell::RefCell;
use std::collections::BTreeMap;
use std::path::Path;
use std::sync::Arc;

use dataferry_dsl::config::model::{FILE_BACKEND, From, OrderedMap, RawKind, RawValue};
use dataferry_dsl::config::{
    self, Config, Connection, Location, ResolvedConnection, ResolvedState,
};
use dataferry_registry::secret::SECRET_KEYWORD;
use dataferry_registry::{
    BuildError, Built, ConnectorRegistry, DynConnector, Endpoints, RawConnection, Roles, StateStore,
};
use schemars::SchemaGenerator;
use serde_json::Value;

use crate::diagnostic::{Diagnostic, Redaction, code};
use crate::exit::Exit;

/// What `validate` found.
pub(crate) struct Report {
    pub diagnostics: Vec<Diagnostic>,
    /// Set when the config is valid.
    pub valid: Option<Summary>,
}

impl Report {
    fn invalid(diagnostics: Vec<Diagnostic>) -> Self {
        Self {
            diagnostics,
            valid: None,
        }
    }

    pub fn exit(&self) -> Exit {
        match self.valid {
            Some(_) => Exit::Success,
            None => Exit::ConfigInvalid,
        }
    }
}

pub(crate) struct Summary {
    pub name: String,
    pub flows: usize,
    /// `(name, type, roles, description)`, in load order.
    pub connections: Vec<ConnectionLine>,
    /// The config, for `plan` and `run`.
    pub config: Config,
    /// `runtime.state`, substituted.
    pub state: Option<ResolvedState>,
    /// What each connection built, by name: built once, with the roles its
    /// flows use, so `plan` and `run` connect through the same endpoints
    /// `validate` checked.
    pub endpoints: BTreeMap<String, Endpoints>,
    /// The state store, when `runtime.state.connection` names a connection.
    pub state_store: Option<Arc<dyn StateStore>>,
    /// Each connection's entry after `${ENV}` substitution, by name. Holds
    /// secrets: never printed.
    pub raw: BTreeMap<String, RawConnection>,
}

/// Checks that `dir` is a `ferry/` directory before the loader looks
/// inside, so the error names the path and says what was expected.
pub(crate) fn check_config_dir(dir: &Path) -> Result<(), Diagnostic> {
    let shown = absolute(dir);
    if dir.is_dir() {
        return Ok(());
    }
    if dir.exists() {
        let parent = dir
            .parent()
            .filter(|p| !p.as_os_str().is_empty())
            .unwrap_or(Path::new("."));
        return Err(Diagnostic::error(
            code::CONFIG_DIR,
            format!(
                "`{}` is a file; --config expects the `ferry/` directory that holds ferry.yaml",
                shown.display()
            ),
        )
        .help(format!("try `--config {}`", parent.display())));
    }
    Err(Diagnostic::error(
        code::CONFIG_DIR,
        format!("no config directory at `{}`", shown.display()),
    )
    .help("pass `--config <dir>` or set FERRY_CONFIG; the default is `./ferry`"))
}

fn absolute(path: &Path) -> std::path::PathBuf {
    std::path::absolute(path).unwrap_or_else(|_| path.to_owned())
}

pub(crate) fn validate(
    registry: &ConnectorRegistry,
    dir: &Path,
    env: &dyn Fn(&str) -> Option<String>,
    redaction: &mut Redaction,
) -> Report {
    if let Err(d) = check_config_dir(dir) {
        return Report::invalid(vec![d]);
    }
    let config = match config::load(dir) {
        Ok(config) => config,
        Err(errors) => return Report::invalid(errors.into_iter().map(Into::into).collect()),
    };

    // Every value read from the environment is redacted, even when
    // substitution fails partway (a missing variable next to a set one).
    let seen = RefCell::new(Vec::new());
    let resolved = config.resolve_with(|name| {
        let value = env(name);
        if let Some(v) = &value {
            seen.borrow_mut().push(v.clone());
        }
        value
    });
    for value in seen.into_inner() {
        redaction.add(&value);
    }
    let resolved = match resolved {
        Ok(resolved) => resolved,
        Err(errors) => return Report::invalid(errors.into_iter().map(Into::into).collect()),
    };

    let uses = uses(&config);
    let mut checker = Checker {
        registry,
        diagnostics: Vec::new(),
        redaction,
    };
    let mut connections = Vec::new();
    let mut endpoints = BTreeMap::new();
    let mut state_store = None;
    let mut raws = BTreeMap::new();
    for (raw, resolved_conn) in config.connections.iter().zip(&resolved.connections) {
        raws.insert(resolved_conn.name.clone(), raw_connection(resolved_conn));
        let used = uses
            .get(resolved_conn.name.as_str())
            .map_or(&[][..], Vec::as_slice);
        if let Some((entry, built, store)) =
            checker.connection(&config, raw, resolved_conn, used, resolved.state.as_ref())
        {
            endpoints.insert(entry.0.clone(), built);
            state_store = state_store.or(store);
            connections.push(entry);
        }
    }

    let diagnostics = checker.diagnostics;
    if diagnostics.iter().any(Diagnostic::is_error) {
        return Report::invalid(diagnostics);
    }
    Report {
        diagnostics,
        valid: Some(Summary {
            name: config.name.as_str().to_owned(),
            flows: config.flows.len(),
            connections,
            state: resolved.state.clone(),
            config,
            endpoints,
            state_store,
            raw: raws,
        }),
    }
}

fn raw_connection(conn: &ResolvedConnection) -> RawConnection {
    RawConnection {
        name: conn.name.clone(),
        r#type: conn.r#type.clone(),
        fields: conn.fields.as_object().cloned().unwrap_or_default(),
    }
}

/// `(name, type, roles, description)`.
pub(crate) type ConnectionLine = (String, String, Roles, String);

/// A connection's summary line, its endpoints, and the state store if it
/// holds it.
type BuiltConnection = (ConnectionLine, Endpoints, Option<Arc<dyn StateStore>>);

/// One place a flow uses a connection.
struct Use<'a> {
    flow: &'a str,
    role: Roles,
    location: &'a Location,
}

/// How each connection is used, by connection name.
fn uses(config: &Config) -> BTreeMap<&str, Vec<Use<'_>>> {
    let mut out: BTreeMap<&str, Vec<Use<'_>>> = BTreeMap::new();
    for flow in &config.flows {
        let name = flow.name.as_str();
        let mut add = |connection, role, location| {
            out.entry(connection).or_default().push(Use {
                flow: name,
                role,
                location,
            })
        };
        match flow.from.value() {
            From::Entity(entity) => add(
                entity.connection.as_str(),
                Roles::SOURCE,
                flow.from.location(),
            ),
            From::Merge(entries) => {
                for entry in entries {
                    add(
                        entry.entity.connection.as_str(),
                        Roles::SOURCE,
                        entry.entity.location(),
                    );
                }
            }
        }
        add(flow.sink.as_str(), Roles::SINK, flow.sink.location());
    }
    out
}

struct Checker<'a> {
    registry: &'a ConnectorRegistry,
    diagnostics: Vec<Diagnostic>,
    redaction: &'a mut Redaction,
}

impl Checker<'_> {
    /// Checks and builds one connection: its summary line, its endpoints,
    /// and the state store if it holds it. `None` if it couldn't be built.
    fn connection(
        &mut self,
        config: &Config,
        raw: &Connection,
        conn: &ResolvedConnection,
        uses: &[Use<'_>],
        state: Option<&ResolvedState>,
    ) -> Option<BuiltConnection> {
        let entry = format!("connections[{}]", conn.name);
        let Some(connector) = self.registry.connector(&conn.r#type) else {
            self.diagnostics.push(
                Diagnostic::error(
                    code::UNKNOWN_TYPE,
                    format!("unknown connection type `{}`", conn.r#type),
                )
                .at(Some(raw.r#type.location()))
                .path(format!("{entry}.type"))
                .help(self.registered_types()),
            );
            return None;
        };
        let provides = connector.roles();
        let type_name = connector.type_name().to_owned();
        // Before building, so a secret is masked in the source line shown
        // with an error even when its value fails to deserialize.
        for secret in secret_values(connector, &conn.fields) {
            self.redaction.add(&secret);
        }

        let mut wants = Roles::NONE;
        for u in uses {
            if provides.contains(u.role) {
                wants = wants.union(u.role);
                continue;
            }
            let (verb, key, missing) = match u.role == Roles::SINK {
                true => ("writes to", "sink", "sink"),
                false => ("reads from", "from", "source"),
            };
            self.diagnostics.push(
                Diagnostic::error(
                    code::ROLE,
                    format!(
                        "flow `{}` {verb} connection `{}`, but the `{type_name}` connector in this build has no {missing}",
                        u.flow, conn.name
                    ),
                )
                .at(Some(u.location))
                .path(format!("flows[{}].{key}", u.flow))
                .help(format!("`{type_name}` provides: {provides}")),
            );
        }

        let state_ok = match state {
            Some(s)
                if s.backend != FILE_BACKEND
                    && s.connection.as_deref() == Some(conn.name.as_str()) =>
            {
                self.state_connection(config, conn, s)
            }
            _ => false,
        };

        let raw_conn = raw_connection(conn);
        let built = match state_ok {
            true => self
                .registry
                .build_with_state(&raw_conn, wants)
                .map(|(built, store)| (built, Some(store))),
            false => self.registry.build(&raw_conn, wants).map(|b| (b, None)),
        };
        match built {
            Ok((built, store)) => {
                for secret in &built.secrets {
                    self.redaction.add(secret);
                }
                self.literal_secrets(raw, &built, &entry);
                Some((
                    (conn.name.clone(), type_name, wants, built.description),
                    built.endpoints,
                    store,
                ))
            }
            Err(error) => {
                self.build_error(raw, conn, &entry, error);
                None
            }
        }
    }

    fn registered_types(&self) -> String {
        let types: Vec<String> = self
            .registry
            .types()
            .map(|(name, roles)| format!("`{name}` ({roles})"))
            .collect();
        match types.is_empty() {
            true => "this binary has no connection types registered".to_owned(),
            false => format!("registered types: {}", types.join(", ")),
        }
    }

    /// `runtime.state.connection` names `conn` (0004 ยง8): its type must be
    /// the backend, and that type must be able to hold the state store.
    /// Errors point at `runtime.yaml`.
    fn state_connection(
        &mut self,
        config: &Config,
        conn: &ResolvedConnection,
        state: &ResolvedState,
    ) -> bool {
        let written = config.runtime.state.as_ref();
        let backend_at = written.map(|s| s.backend.location());
        let connection_at = written
            .and_then(|s| s.connection.as_ref())
            .map(|c| c.location());
        if conn.r#type != state.backend {
            self.diagnostics.push(
                Diagnostic::error(
                    code::STATE_TYPE,
                    format!(
                        "the state connection `{}` is a `{}` connection, but `backend:` is `{}`",
                        conn.name, conn.r#type, state.backend
                    ),
                )
                .at(connection_at)
                .path("runtime.state.connection")
                .help(format!(
                    "name a `{}` connection here, or set `backend:` to the connection's type",
                    state.backend
                )),
            );
            return false;
        }
        let capable = self
            .registry
            .connector(&state.backend)
            .is_some_and(|c| c.state_capable());
        if !capable {
            let capable: Vec<String> = self
                .registry
                .types()
                .filter(|(t, _)| {
                    self.registry
                        .connector(t)
                        .is_some_and(|c| c.state_capable())
                })
                .map(|(t, _)| format!("`{t}`"))
                .collect();
            self.diagnostics.push(
                Diagnostic::error(
                    code::STATE_NOT_CAPABLE,
                    format!(
                        "the `{}` connector in this build can't hold the state store",
                        state.backend
                    ),
                )
                .at(backend_at)
                .path("runtime.state.backend")
                .help(match capable.is_empty() {
                    true => {
                        "use `backend: file`: no connector in this build can hold state".to_owned()
                    }
                    false => format!(
                        "use `backend: file`, or a connection of a state-capable type: {}",
                        capable.join(", ")
                    ),
                }),
            );
            return false;
        }
        true
    }

    fn build_error(
        &mut self,
        raw: &Connection,
        conn: &ResolvedConnection,
        entry: &str,
        error: BuildError,
    ) {
        let entry_at = raw.name.location();
        match error {
            BuildError::Options(errors) => {
                for e in errors {
                    // An unknown field is wrong as a key; anything else is
                    // a wrong value.
                    let at_key = e.message.starts_with("unknown field");
                    let (path, at) = match e.path.as_str() {
                        "" => (entry.to_owned(), entry_at),
                        p => (
                            format!("{entry}.{p}"),
                            at_key
                                .then(|| key_location(&raw.fields, p))
                                .flatten()
                                .or_else(|| conn.location(p))
                                .unwrap_or(entry_at),
                        ),
                    };
                    self.diagnostics.push(
                        Diagnostic::error(code::OPTIONS, e.message)
                            .at(Some(at))
                            .path(path),
                    );
                }
            }
            BuildError::Other(e) => self.diagnostics.push(
                Diagnostic::error(code::BUILD, e.to_string())
                    .at(Some(entry_at))
                    .path(entry),
            ),
        }
    }

    /// Warns about secret values written into the config as literals, since
    /// config directories get committed (0004 ยง4). Not an error: local runs
    /// against throwaway databases legitimately do it.
    fn literal_secrets(&mut self, raw: &Connection, built: &Built, entry: &str) {
        let mut found = Vec::new();
        for (key, value) in raw.fields.iter() {
            literals(value, key.as_str().to_owned(), &built.secrets, &mut found);
        }
        for (path, location) in found {
            self.diagnostics.push(
                Diagnostic::warning(
                    code::SECRET_LITERAL,
                    "holds a secret as a literal value, and config directories get committed",
                )
                .at(Some(&location))
                .path(format!("{entry}.{path}"))
                .help("write `${NAME}` and export the value in the environment instead"),
            );
        }
    }
}

/// The value of every field the connector's schema marks `x-ferry-secret`,
/// numbers included: `password: 123456` is still a password.
fn secret_values(connector: &dyn DynConnector, fields: &Value) -> Vec<String> {
    let mut generator = SchemaGenerator::default();
    let schema = connector.options_schema(&mut generator).to_value();
    let defs = Value::Object(generator.take_definitions(true));
    let mut out = Vec::new();
    walk_secrets(&schema, &defs, fields, &mut out, 0);
    out
}

fn walk_secrets(schema: &Value, defs: &Value, value: &Value, out: &mut Vec<String>, depth: usize) {
    // Deeper than any real options type: the schema is recursive.
    if depth > 32 {
        return;
    }
    if let Some(name) = schema
        .get("$ref")
        .and_then(Value::as_str)
        .and_then(|r| r.strip_prefix("#/$defs/"))
        && let Some(target) = defs.get(name)
    {
        walk_secrets(target, defs, value, out, depth + 1);
    }
    if schema.get(SECRET_KEYWORD) == Some(&Value::Bool(true)) {
        match value {
            Value::String(s) => out.push(s.clone()),
            Value::Number(n) => out.push(n.to_string()),
            _ => {}
        }
    }
    for combinator in ["allOf", "anyOf", "oneOf"] {
        for sub in schema
            .get(combinator)
            .and_then(Value::as_array)
            .into_iter()
            .flatten()
        {
            walk_secrets(sub, defs, value, out, depth + 1);
        }
    }
    if let (Some(properties), Value::Object(fields)) =
        (schema.get("properties").and_then(Value::as_object), value)
    {
        for (key, field) in fields {
            if let Some(sub) = properties.get(key) {
                walk_secrets(sub, defs, field, out, depth + 1);
            }
        }
    }
}

/// Where the key at the end of a dotted `path` was written. `None` for a
/// path through a list, or one that isn't in the config.
fn key_location<'a>(fields: &'a OrderedMap<RawValue>, path: &str) -> Option<&'a Location> {
    let (first, rest) = match path.split_once('.') {
        Some((first, rest)) => (first, Some(rest)),
        None => (path, None),
    };
    let (key, value) = fields.iter().find(|(k, _)| k.as_str() == first)?;
    match (rest, value.value()) {
        (None, _) => Some(key.location()),
        (Some(rest), RawKind::Map(map)) => key_location(map, rest),
        _ => None,
    }
}

/// Strings with no `${` that contain one of `secrets`, with their paths.
fn literals(value: &RawValue, path: String, secrets: &[String], out: &mut Vec<(String, Location)>) {
    match value.value() {
        RawKind::Str(text) => {
            if !text.contains("${")
                && secrets
                    .iter()
                    .any(|s| !s.is_empty() && text.contains(s.as_str()))
            {
                out.push((path, value.location().clone()));
            }
        }
        RawKind::Seq(items) => {
            for (i, item) in items.iter().enumerate() {
                literals(item, format!("{path}[{i}]"), secrets, out);
            }
        }
        RawKind::Map(map) => {
            for (key, item) in map.iter() {
                literals(item, format!("{path}.{}", key.as_str()), secrets, out);
            }
        }
        _ => {}
    }
}