forjar 1.4.2

Rust-native Infrastructure as Code — bare-metal first, BLAKE3 state, provenance tracing
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
//! Phase 106 — Resource Audit & Coverage Analysis: validate commands (FJ-1110, FJ-1113, FJ-1116).

use crate::core::types;
use std::collections::{BTreeMap, BTreeSet};
use std::path::Path;

// ============================================================================
// FJ-1110: Resource dependency completeness audit
// ============================================================================

/// A missing dependency reference.
struct MissingDep {
    resource: String,
    missing_dep: String,
}

/// Find all declared dependencies that do not exist as resource keys.
fn find_missing_deps(config: &types::ForjarConfig) -> Vec<MissingDep> {
    let mut results = Vec::new();
    let mut names: Vec<&String> = config.resources.keys().collect();
    names.sort();
    for name in names {
        let resource = &config.resources[name];
        for dep in &resource.depends_on {
            if !config.resources.contains_key(dep) {
                results.push(MissingDep {
                    resource: name.clone(),
                    missing_dep: dep.clone(),
                });
            }
        }
    }
    results
}

/// FJ-1110: Verify all declared dependencies exist in config.resources.
/// Reports missing dependency references that would cause apply failures.
pub(crate) fn cmd_validate_check_resource_dependency_completeness_audit(
    file: &Path,
    json: bool,
) -> Result<(), String> {
    let txt = std::fs::read_to_string(file).map_err(|e| e.to_string())?;
    let cfg: crate::core::types::ForjarConfig =
        serde_yaml_ng::from_str(&txt).map_err(|e| e.to_string())?;

    let missing = find_missing_deps(&cfg);

    if json {
        let items: Vec<serde_json::Value> = missing
            .iter()
            .map(|m| {
                serde_json::json!({
                    "resource": m.resource,
                    "missing_dep": m.missing_dep
                })
            })
            .collect();
        println!(
            "{}",
            serde_json::json!({
                "dependency_completeness": {
                    "warnings": missing.len(),
                    "missing": items
                }
            })
        );
    } else {
        println!("Dependency completeness: {} warnings", missing.len());
        for m in &missing {
            println!(
                "  warning: resource '{}' depends on '{}' which does not exist",
                m.resource, m.missing_dep
            );
        }
    }
    Ok(())
}

// ============================================================================
// FJ-1113: Resource machine coverage gap
// ============================================================================

/// A coverage gap: a machine missing resource types that other machines have.
struct CoverageGap {
    machine: String,
    missing_types: Vec<String>,
}

/// Compare resource types present on each machine vs fleet-wide types.
fn find_machine_coverage_gaps(config: &types::ForjarConfig) -> Vec<CoverageGap> {
    // Collect resource types per machine
    let mut machine_types: BTreeMap<String, BTreeSet<String>> = BTreeMap::new();
    let mut fleet_types: BTreeSet<String> = BTreeSet::new();

    for resource in config.resources.values() {
        let rtype = resource.resource_type.to_string();
        fleet_types.insert(rtype.clone());
        for machine in resource.machine.iter() {
            machine_types
                .entry(machine.to_owned())
                .or_default()
                .insert(rtype.clone());
        }
    }

    // Find gaps: machines missing types present in the fleet
    let mut gaps = Vec::new();
    for (machine, types_set) in &machine_types {
        let missing: Vec<String> = fleet_types.difference(types_set).cloned().collect();
        if !missing.is_empty() {
            gaps.push(CoverageGap {
                machine: machine.clone(),
                missing_types: missing,
            });
        }
    }
    gaps
}

/// FJ-1113: Check for machine coverage gaps — machines missing resource types
/// that are present on other machines in the fleet.
pub(crate) fn cmd_validate_check_resource_machine_coverage_gap(
    file: &Path,
    json: bool,
) -> Result<(), String> {
    let txt = std::fs::read_to_string(file).map_err(|e| e.to_string())?;
    let cfg: crate::core::types::ForjarConfig =
        serde_yaml_ng::from_str(&txt).map_err(|e| e.to_string())?;

    let gaps = find_machine_coverage_gaps(&cfg);

    if json {
        let items: Vec<serde_json::Value> = gaps
            .iter()
            .map(|g| {
                serde_json::json!({
                    "machine": g.machine,
                    "missing_types": g.missing_types
                })
            })
            .collect();
        println!(
            "{}",
            serde_json::json!({
                "machine_coverage_gaps": {
                    "warnings": gaps.len(),
                    "gaps": items
                }
            })
        );
    } else {
        println!("Machine coverage gaps: {} warnings", gaps.len());
        for g in &gaps {
            println!(
                "  warning: machine '{}' is missing types: [{}]",
                g.machine,
                g.missing_types.join(", ")
            );
        }
    }
    Ok(())
}

// ============================================================================
// FJ-1116: Resource path depth limit
// ============================================================================

/// Maximum allowed directory depth (number of '/' separators).
const PATH_DEPTH_LIMIT: usize = 8;

/// A path depth violation.
struct PathDepthViolation {
    resource: String,
    path: String,
    depth: usize,
}

/// Find file resources whose path exceeds the depth limit.
fn find_path_depth_violations(
    config: &types::ForjarConfig,
    limit: usize,
) -> Vec<PathDepthViolation> {
    let mut violations = Vec::new();
    let mut names: Vec<&String> = config.resources.keys().collect();
    names.sort();
    for name in names {
        let resource = &config.resources[name];
        if let Some(ref path) = resource.path {
            let depth = path.chars().filter(|&c| c == '/').count();
            if depth > limit {
                violations.push(PathDepthViolation {
                    resource: name.clone(),
                    path: path.clone(),
                    depth,
                });
            }
        }
    }
    violations
}

/// FJ-1116: Check file resource paths for directory depth exceeding the limit.
/// Deep paths may indicate overly nested directory structures.
pub(crate) fn cmd_validate_check_resource_path_depth_limit(
    file: &Path,
    json: bool,
) -> Result<(), String> {
    let txt = std::fs::read_to_string(file).map_err(|e| e.to_string())?;
    let cfg: crate::core::types::ForjarConfig =
        serde_yaml_ng::from_str(&txt).map_err(|e| e.to_string())?;

    let violations = find_path_depth_violations(&cfg, PATH_DEPTH_LIMIT);

    if json {
        let items: Vec<serde_json::Value> = violations
            .iter()
            .map(|v| {
                serde_json::json!({
                    "resource": v.resource,
                    "path": v.path,
                    "depth": v.depth
                })
            })
            .collect();
        println!(
            "{}",
            serde_json::json!({
                "path_depth": {
                    "limit": PATH_DEPTH_LIMIT,
                    "violations": items
                }
            })
        );
    } else if violations.is_empty() {
        println!("Path depth: 0 resources exceed limit ({PATH_DEPTH_LIMIT} levels)");
    } else {
        println!(
            "Path depth: {} resources exceed limit ({} levels)",
            violations.len(),
            PATH_DEPTH_LIMIT
        );
        for v in &violations {
            println!(
                "  warning: resource '{}' path '{}' has depth {}",
                v.resource, v.path, v.depth
            );
        }
    }
    Ok(())
}

// ============================================================================
// Tests
// ============================================================================

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

    /// Write a YAML config string to a temporary file and return the path.
    fn write_temp_config(yaml: &str) -> tempfile::NamedTempFile {
        let mut f = tempfile::NamedTempFile::new().unwrap();
        f.write_all(yaml.as_bytes()).unwrap();
        f.flush().unwrap();
        f
    }

    // -- FJ-1110: Dependency completeness tests --

    #[test]
    fn test_dependency_completeness_no_warnings() {
        let yaml = "\
version: '1.0'
name: test
resources:
  a:
    type: file
  b:
    type: file
    depends_on: [a]
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_dependency_completeness_audit(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dependency_completeness_with_missing() {
        let yaml = "\
version: '1.0'
name: test
resources:
  web:
    type: service
    depends_on: [db, cache]
  db:
    type: package
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_dependency_completeness_audit(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dependency_completeness_json_output() {
        let yaml = "\
version: '1.0'
name: test
resources:
  app:
    type: file
    depends_on: [missing-res]
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_dependency_completeness_audit(f.path(), true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_dependency_completeness_file_not_found() {
        let result = cmd_validate_check_resource_dependency_completeness_audit(
            Path::new("/nonexistent/forjar.yaml"),
            false,
        );
        assert!(result.is_err());
    }

    // -- FJ-1113: Machine coverage gap tests --

    #[test]
    fn test_machine_coverage_no_gaps() {
        let yaml = "\
version: '1.0'
name: test
resources:
  web-pkg:
    type: package
    machine: web
  db-pkg:
    type: package
    machine: db
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_machine_coverage_gap(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_machine_coverage_with_gaps() {
        let yaml = "\
version: '1.0'
name: test
resources:
  web-pkg:
    type: package
    machine: web
  web-svc:
    type: service
    machine: web
  db-pkg:
    type: package
    machine: db
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_machine_coverage_gap(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_machine_coverage_json_output() {
        let yaml = "\
version: '1.0'
name: test
resources:
  web-file:
    type: file
    machine: web
  db-svc:
    type: service
    machine: db
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_machine_coverage_gap(f.path(), true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_machine_coverage_file_not_found() {
        let result = cmd_validate_check_resource_machine_coverage_gap(
            Path::new("/nonexistent/forjar.yaml"),
            false,
        );
        assert!(result.is_err());
    }

    // -- FJ-1116: Path depth limit tests --

    #[test]
    fn test_path_depth_no_violations() {
        let yaml = "\
version: '1.0'
name: test
resources:
  cfg:
    type: file
    path: /etc/nginx/nginx.conf
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_path_depth_limit(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_path_depth_with_violation() {
        let yaml = "\
version: '1.0'
name: test
resources:
  deep-file:
    type: file
    path: /a/b/c/d/e/f/g/h/i/j/k.conf
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_path_depth_limit(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_path_depth_json_output() {
        let yaml = "\
version: '1.0'
name: test
resources:
  shallow:
    type: file
    path: /etc/app.conf
  deep:
    type: file
    path: /a/b/c/d/e/f/g/h/i/deep.txt
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_path_depth_limit(f.path(), true);
        assert!(result.is_ok());
    }

    #[test]
    fn test_path_depth_file_not_found() {
        let result = cmd_validate_check_resource_path_depth_limit(
            Path::new("/nonexistent/forjar.yaml"),
            false,
        );
        assert!(result.is_err());
    }
}