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
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
//! Phase 105 — Resource Hygiene & Structural Depth: validate commands (FJ-1102, FJ-1105, FJ-1108).

use crate::core::types;
use std::collections::HashMap;
use std::path::Path;

// ============================================================================
// FJ-1102: Resource dependency depth variance
// ============================================================================

/// Depth statistics for dependency chains.
struct DepthStats {
    min: usize,
    max: usize,
    variance: f64,
    resource_count: usize,
}

/// Compute dependency chain depth for each resource (Bellman-Ford propagation).
fn compute_depths(config: &types::ForjarConfig) -> HashMap<String, usize> {
    let mut depths: HashMap<String, usize> = HashMap::new();
    let names: Vec<String> = config.resources.keys().cloned().collect();

    // Initialize all resources at depth 0
    for name in &names {
        depths.insert(name.clone(), 0);
    }

    // Iteratively propagate depths until stable (Bellman-Ford style)
    let mut changed = true;
    while changed {
        changed = false;
        for name in &names {
            let resource = &config.resources[name];
            for dep_name in &resource.depends_on {
                if let Some(&dep_depth) = depths.get(dep_name) {
                    let new_depth = dep_depth + 1;
                    let current = depths[name];
                    if new_depth > current {
                        depths.insert(name.clone(), new_depth);
                        changed = true;
                    }
                }
            }
        }
    }

    depths
}

/// Compute depth statistics across all resources.
fn compute_depth_stats(config: &types::ForjarConfig) -> Option<DepthStats> {
    if config.resources.is_empty() {
        return None;
    }
    let depths = compute_depths(config);
    let values: Vec<usize> = depths.values().copied().collect();
    let min = values.iter().copied().min().unwrap_or(0);
    let max = values.iter().copied().max().unwrap_or(0);
    let n = values.len() as f64;
    let mean = values.iter().copied().sum::<usize>() as f64 / n;
    let variance = values
        .iter()
        .map(|&v| {
            let diff = v as f64 - mean;
            diff * diff
        })
        .sum::<f64>()
        / n;

    Some(DepthStats {
        min,
        max,
        variance,
        resource_count: values.len(),
    })
}

/// Depth variance threshold: warn if `max_depth - min_depth > DEPTH_VARIANCE_THRESHOLD`.
const DEPTH_VARIANCE_THRESHOLD: usize = 3;

/// FJ-1102: Check dependency chain depth variance across resources. Warns when
/// depth spread exceeds the threshold, indicating uneven dependency topology.
pub(crate) fn cmd_validate_check_resource_dependency_depth_variance(
    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 stats = compute_depth_stats(&cfg);

    if json {
        let (min, max, variance, warnings) = match &stats {
            Some(s) => {
                let w = if s.max - s.min > DEPTH_VARIANCE_THRESHOLD {
                    1
                } else {
                    0
                };
                (s.min, s.max, s.variance, w)
            }
            None => (0, 0, 0.0, 0),
        };
        println!(
            "{}",
            serde_json::json!({
                "dependency_depth_variance": {
                    "min": min,
                    "max": max,
                    "variance": variance,
                    "warnings": warnings
                }
            })
        );
    } else {
        match stats {
            Some(s) if s.max - s.min > DEPTH_VARIANCE_THRESHOLD => {
                println!(
                    "Dependency depth variance: {} (min={}, max={}, resources={})",
                    s.variance, s.min, s.max, s.resource_count
                );
            }
            Some(_) | None => {
                println!("Dependency depth variance: OK (uniform depths)");
            }
        }
    }
    Ok(())
}

// ============================================================================
// FJ-1105: Resource tag key naming conventions
// ============================================================================

/// A violation of tag key naming conventions.
struct TagKeyViolation {
    resource: String,
    key: String,
    reason: String,
}

/// Check whether a tag key follows naming conventions (lowercase, no spaces).
fn validate_tag_key(key: &str) -> Option<String> {
    if key.is_empty() {
        return Some("empty key".to_string());
    }
    if key != key.to_lowercase() {
        return Some("key contains uppercase characters".to_string());
    }
    if key.contains(' ') {
        return Some("key contains spaces".to_string());
    }
    let valid = key
        .chars()
        .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_' || c == ':');
    if !valid {
        return Some("key contains invalid characters".to_string());
    }
    None
}

/// Find all tag key naming violations across resources.
fn find_tag_key_violations(config: &types::ForjarConfig) -> Vec<TagKeyViolation> {
    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];
        for tag in &resource.tags {
            // Tags may be "key:value" format — extract the key part
            let key = tag.split(':').next().unwrap_or(tag);
            if let Some(reason) = validate_tag_key(key) {
                violations.push(TagKeyViolation {
                    resource: name.clone(),
                    key: key.to_string(),
                    reason,
                });
            }
        }
    }
    violations
}

/// FJ-1105: Check that all tag keys follow naming conventions. Tags may be
/// `key:value` pairs; only the key portion is validated.
pub(crate) fn cmd_validate_check_resource_tag_key_naming(
    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_tag_key_violations(&cfg);

    if json {
        let items: Vec<serde_json::Value> = violations
            .iter()
            .map(|v| {
                serde_json::json!({
                    "resource": v.resource,
                    "key": v.key,
                    "reason": v.reason
                })
            })
            .collect();
        println!(
            "{}",
            serde_json::json!({
                "tag_key_naming": {
                    "warnings": violations.len(),
                    "violations": items
                }
            })
        );
    } else if violations.is_empty() {
        println!("Tag key naming: 0 warnings");
    } else {
        println!("Tag key naming: {} warnings", violations.len());
        for v in &violations {
            println!(
                "  warning: resource '{}' tag key '{}': {}",
                v.resource, v.key, v.reason
            );
        }
    }
    Ok(())
}

// ============================================================================
// FJ-1108: Resource content length limit
// ============================================================================

/// Maximum inline content length before a warning is issued.
const CONTENT_LENGTH_LIMIT: usize = 4096;

/// A violation for a resource whose content exceeds the length limit.
struct ContentLengthViolation {
    resource: String,
    length: usize,
}

/// Find resources whose inline content exceeds the length limit.
fn find_content_length_violations(
    config: &types::ForjarConfig,
    limit: usize,
) -> Vec<ContentLengthViolation> {
    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 content) = resource.content {
            if content.len() > limit {
                violations.push(ContentLengthViolation {
                    resource: name.clone(),
                    length: content.len(),
                });
            }
        }
    }
    violations
}

/// FJ-1108: Warn if inline resource content exceeds the length limit. Content
/// exceeding 4096 characters should be moved to an external file.
pub(crate) fn cmd_validate_check_resource_content_length_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_content_length_violations(&cfg, CONTENT_LENGTH_LIMIT);

    if json {
        let items: Vec<serde_json::Value> = violations
            .iter()
            .map(|v| {
                serde_json::json!({
                    "resource": v.resource,
                    "length": v.length
                })
            })
            .collect();
        println!(
            "{}",
            serde_json::json!({
                "content_length": {
                    "limit": CONTENT_LENGTH_LIMIT,
                    "violations": items
                }
            })
        );
    } else if violations.is_empty() {
        println!("Content length: 0 resources exceed limit ({CONTENT_LENGTH_LIMIT} chars)");
    } else {
        println!(
            "Content length: {} resources exceed limit ({} chars)",
            violations.len(),
            CONTENT_LENGTH_LIMIT
        );
        for v in &violations {
            println!(
                "  warning: resource '{}' has {} characters",
                v.resource, v.length
            );
        }
    }
    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-1102: Dependency depth variance tests --

    #[test]
    fn test_depth_variance_empty_config() {
        let f = write_temp_config("version: '1.0'\nname: test\nresources: {}\n");
        let result = cmd_validate_check_resource_dependency_depth_variance(f.path(), false);
        assert!(result.is_ok());
    }

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

    #[test]
    fn test_depth_variance_json_output() {
        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_depth_variance(f.path(), true);
        assert!(result.is_ok());
    }

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

    // -- FJ-1105: Tag key naming tests --

    #[test]
    fn test_tag_key_naming_empty_config() {
        let f = write_temp_config("version: '1.0'\nname: test\nresources: {}\n");
        let result = cmd_validate_check_resource_tag_key_naming(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_tag_key_naming_with_violations() {
        let yaml = "\
version: '1.0'
name: test
resources:
  web:
    type: file
    tags:
      - 'env:prod'
      - 'Bad Key'
      - 'UPPER'
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_tag_key_naming(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_tag_key_naming_json_output() {
        let yaml = "\
version: '1.0'
name: test
resources:
  web:
    type: file
    tags:
      - 'valid-key:value'
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_tag_key_naming(f.path(), true);
        assert!(result.is_ok());
    }

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

    // -- FJ-1108: Content length limit tests --

    #[test]
    fn test_content_length_empty_config() {
        let f = write_temp_config("version: '1.0'\nname: test\nresources: {}\n");
        let result = cmd_validate_check_resource_content_length_limit(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_content_length_with_violation() {
        let big_content = "x".repeat(5000);
        let yaml = format!(
            "version: '1.0'\nname: test\nresources:\n  big-file:\n    type: file\n    content: '{big_content}'\n"
        );
        let f = write_temp_config(&yaml);
        let result = cmd_validate_check_resource_content_length_limit(f.path(), false);
        assert!(result.is_ok());
    }

    #[test]
    fn test_content_length_json_output() {
        let yaml = "\
version: '1.0'
name: test
resources:
  small:
    type: file
    content: 'hello world'
";
        let f = write_temp_config(yaml);
        let result = cmd_validate_check_resource_content_length_limit(f.path(), true);
        assert!(result.is_ok());
    }

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