greentic-setup 0.4.28

End-to-end bundle setup engine for the Greentic platform — pack discovery, QA-driven configuration, secrets persistence, and bundle lifecycle management
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
//! Capability validation and auto-upgrade for provider gtpacks.
//!
//! Provider gtpacks must contain a `greentic.ext.capabilities.v1` extension
//! in their `manifest.cbor` for the operator to discover and mount them.
//! Old gtpacks built before the capabilities extension was introduced will
//! silently fail at runtime.
//!
//! This module provides validation during `gtc setup` and auto-upgrade from
//! known source locations when a newer pack with capabilities is found.

use std::io::Read;
use std::path::{Path, PathBuf};

use anyhow::Context;
use zip::ZipArchive;

use crate::discovery;

const EXT_CAPABILITIES_V1: &str = "greentic.ext.capabilities.v1";

fn canonicalize_or_path(path: &Path) -> PathBuf {
    std::fs::canonicalize(path).unwrap_or_else(|_| path.to_path_buf())
}

/// Result of validating and upgrading packs in a bundle.
pub struct UpgradeReport {
    pub checked: usize,
    pub upgraded: Vec<UpgradedPack>,
    pub warnings: Vec<PackWarning>,
}

pub struct UpgradedPack {
    pub provider_id: String,
    pub source_path: PathBuf,
}

pub struct PackWarning {
    pub provider_id: String,
    pub message: String,
}

/// Check whether a gtpack has the `greentic.ext.capabilities.v1` extension.
pub fn has_capabilities_extension(pack_path: &Path) -> bool {
    read_has_capabilities(pack_path).unwrap_or(false)
}

fn read_has_capabilities(pack_path: &Path) -> anyhow::Result<bool> {
    let file = std::fs::File::open(pack_path)?;
    let mut archive = ZipArchive::new(file)?;
    let mut entry = match archive.by_name("manifest.cbor") {
        Ok(e) => e,
        Err(_) => return Ok(false),
    };
    let mut bytes = Vec::new();
    entry.read_to_end(&mut bytes)?;
    // Search for the capabilities extension key in the raw CBOR bytes.
    // This avoids depending on the exact CBOR schema which may vary between
    // greentic-types versions. The string is unique enough to be reliable.
    Ok(bytes
        .windows(EXT_CAPABILITIES_V1.len())
        .any(|w| w == EXT_CAPABILITIES_V1.as_bytes()))
}

/// Search known source locations for a replacement gtpack that has capabilities.
///
/// Search order:
/// 1. Sibling bundles in the same parent directory
/// 2. `greentic-messaging-providers/target/packs/` in ancestor dirs
fn find_replacement_pack(pack_filename: &str, bundle_path: &Path, domain: &str) -> Option<PathBuf> {
    let bundle_abs = canonicalize_or_path(bundle_path);
    let parent = bundle_abs.parent()?;

    // 1. Sibling bundles: ../*/providers/{domain}/{filename}
    if let Ok(entries) = std::fs::read_dir(parent) {
        for entry in entries.flatten() {
            let candidate_bundle = canonicalize_or_path(&entry.path());
            if candidate_bundle == bundle_abs || !candidate_bundle.is_dir() {
                continue;
            }
            let candidate = candidate_bundle
                .join("providers")
                .join(domain)
                .join(pack_filename);
            if candidate.is_file() && has_capabilities_extension(&candidate) {
                return Some(candidate);
            }
        }
    }

    // 2. greentic-messaging-providers build output in ancestor dirs
    for ancestor in parent.ancestors().take(4) {
        let candidate = ancestor
            .join("greentic-messaging-providers")
            .join("target")
            .join("packs")
            .join(pack_filename);
        if candidate.is_file() && has_capabilities_extension(&candidate) {
            return Some(candidate);
        }
    }

    None
}

/// Validate all provider gtpacks in a bundle and auto-upgrade those missing capabilities.
pub fn validate_and_upgrade_packs(bundle_path: &Path) -> anyhow::Result<UpgradeReport> {
    let discovered = discovery::discover(bundle_path)
        .context("failed to discover providers for capability validation")?;

    let mut report = UpgradeReport {
        checked: 0,
        upgraded: Vec::new(),
        warnings: Vec::new(),
    };

    for provider in &discovered.providers {
        report.checked += 1;

        if has_capabilities_extension(&provider.pack_path) {
            continue;
        }

        let pack_filename = provider
            .pack_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("");

        if pack_filename.is_empty() {
            continue;
        }

        // Try to find a replacement
        if let Some(replacement) =
            find_replacement_pack(pack_filename, bundle_path, &provider.domain)
        {
            // Backup original
            let backup = provider.pack_path.with_extension("gtpack.bak");
            std::fs::copy(&provider.pack_path, &backup).with_context(|| {
                format!(
                    "failed to backup {} before upgrade",
                    provider.pack_path.display()
                )
            })?;

            // Copy replacement
            std::fs::copy(&replacement, &provider.pack_path).with_context(|| {
                format!(
                    "failed to copy replacement pack from {}",
                    replacement.display()
                )
            })?;

            println!(
                "  [upgrade] {}: replaced with {} (capabilities extension added)",
                provider.provider_id,
                replacement.display()
            );

            report.upgraded.push(UpgradedPack {
                provider_id: provider.provider_id.clone(),
                source_path: replacement,
            });
        } else {
            let msg = format!(
                "pack missing greentic.ext.capabilities.v1 — operator will not detect this provider. \
                 Replace with a newer build of {}",
                pack_filename,
            );
            println!("  [warn] {}: {}", provider.provider_id, msg);
            report.warnings.push(PackWarning {
                provider_id: provider.provider_id.clone(),
                message: msg,
            });
        }
    }

    Ok(report)
}

// ---------------------------------------------------------------------------
// Dependency capability validation
// ---------------------------------------------------------------------------

/// Report of dependency capability validation across all packs in the bundle.
pub struct DependencyReport {
    pub satisfied: Vec<SatisfiedCapability>,
    pub missing: Vec<MissingCapability>,
}

pub struct SatisfiedCapability {
    pub capability: String,
    pub required_by: String,
    pub provided_by: String,
}

pub struct MissingCapability {
    pub capability: String,
    pub required_by: String,
}

/// Validate that all pack dependencies have their required_capabilities
/// satisfied by other packs in the bundle.
pub fn validate_dependency_capabilities(bundle_path: &Path) -> anyhow::Result<DependencyReport> {
    let discovered = discovery::discover(bundle_path)
        .context("failed to discover providers for dependency validation")?;

    let mut report = DependencyReport {
        satisfied: Vec::new(),
        missing: Vec::new(),
    };

    // Build capability index: capability_name → provider_id.
    let mut capability_providers: std::collections::BTreeMap<String, String> =
        std::collections::BTreeMap::new();
    for provider in &discovered.providers {
        if let Ok(caps) = read_pack_capabilities(&provider.pack_path) {
            for cap_name in caps {
                capability_providers
                    .entry(cap_name)
                    .or_insert_with(|| provider.provider_id.clone());
            }
        }
    }

    // Check each pack's dependencies.
    let mut pack_id_set: std::collections::BTreeSet<String> = std::collections::BTreeSet::new();
    for provider in &discovered.providers {
        pack_id_set.insert(provider.provider_id.clone());
    }

    for provider in &discovered.providers {
        let deps = match read_pack_dependencies(&provider.pack_path) {
            Ok(d) => d,
            Err(_) => continue,
        };
        for (dep_pack_id, required_caps) in deps {
            // Skip if dependency pack_id is present directly.
            if pack_id_set.contains(&dep_pack_id) {
                continue;
            }
            for cap in &required_caps {
                if let Some(provided_by) = capability_providers.get(cap) {
                    report.satisfied.push(SatisfiedCapability {
                        capability: cap.clone(),
                        required_by: provider.provider_id.clone(),
                        provided_by: provided_by.clone(),
                    });
                } else {
                    report.missing.push(MissingCapability {
                        capability: cap.clone(),
                        required_by: provider.provider_id.clone(),
                    });
                }
            }
        }
    }

    Ok(report)
}

/// Read capability names from a gtpack manifest.
fn read_pack_capabilities(pack_path: &Path) -> anyhow::Result<Vec<String>> {
    let file = std::fs::File::open(pack_path)?;
    let mut archive = ZipArchive::new(file)?;
    let mut entry = archive.by_name("manifest.cbor")?;
    let mut bytes = Vec::new();
    entry.read_to_end(&mut bytes)?;
    let cbor: serde_cbor::Value = serde_cbor::from_slice(&bytes)?;

    let mut caps = Vec::new();
    if let serde_cbor::Value::Map(ref map) = cbor
        && let Some(serde_cbor::Value::Array(arr)) =
            map.get(&serde_cbor::Value::Text("capabilities".to_string()))
    {
        for item in arr {
            if let serde_cbor::Value::Map(cap_map) = item
                && let Some(serde_cbor::Value::Text(name)) =
                    cap_map.get(&serde_cbor::Value::Text("name".to_string()))
            {
                caps.push(name.clone());
            }
        }
    }
    Ok(caps)
}

/// Read dependencies from a gtpack manifest.
/// Returns Vec of (pack_id, required_capabilities).
fn read_pack_dependencies(pack_path: &Path) -> anyhow::Result<Vec<(String, Vec<String>)>> {
    let file = std::fs::File::open(pack_path)?;
    let mut archive = ZipArchive::new(file)?;
    let mut entry = archive.by_name("manifest.cbor")?;
    let mut bytes = Vec::new();
    entry.read_to_end(&mut bytes)?;
    let cbor: serde_cbor::Value = serde_cbor::from_slice(&bytes)?;

    let mut deps = Vec::new();
    if let serde_cbor::Value::Map(ref map) = cbor
        && let Some(serde_cbor::Value::Array(arr)) =
            map.get(&serde_cbor::Value::Text("dependencies".to_string()))
    {
        for item in arr {
            if let serde_cbor::Value::Map(dep_map) = item {
                let pack_id = dep_map
                    .get(&serde_cbor::Value::Text("pack_id".to_string()))
                    .and_then(|v| {
                        if let serde_cbor::Value::Text(s) = v {
                            Some(s.clone())
                        } else {
                            None
                        }
                    })
                    .unwrap_or_default();
                let req_caps: Vec<String> = dep_map
                    .get(&serde_cbor::Value::Text(
                        "required_capabilities".to_string(),
                    ))
                    .and_then(|v| {
                        if let serde_cbor::Value::Array(arr) = v {
                            Some(
                                arr.iter()
                                    .filter_map(|item| {
                                        if let serde_cbor::Value::Text(s) = item {
                                            Some(s.clone())
                                        } else {
                                            None
                                        }
                                    })
                                    .collect(),
                            )
                        } else {
                            None
                        }
                    })
                    .unwrap_or_default();
                if !pack_id.is_empty() && !req_caps.is_empty() {
                    deps.push((pack_id, req_caps));
                }
            }
        }
    }
    Ok(deps)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::BTreeMap;
    use std::fs::File;
    use std::io::Write;
    use zip::write::{FileOptions, ZipWriter};

    use serde_cbor::value::Value as CV;

    /// Write a minimal gtpack zip with a CBOR manifest.
    fn write_test_gtpack(path: &Path, with_capabilities: bool) {
        let mut map = BTreeMap::new();
        map.insert(
            CV::Text("schema_version".into()),
            CV::Text("pack-v1".into()),
        );
        map.insert(CV::Text("pack_id".into()), CV::Text("test-provider".into()));
        map.insert(CV::Text("version".into()), CV::Text("0.1.0".into()));
        map.insert(CV::Text("kind".into()), CV::Text("provider".into()));
        map.insert(CV::Text("publisher".into()), CV::Text("test".into()));

        if with_capabilities {
            let mut ext_inner = BTreeMap::new();
            ext_inner.insert(
                CV::Text("kind".into()),
                CV::Text(EXT_CAPABILITIES_V1.into()),
            );
            ext_inner.insert(CV::Text("version".into()), CV::Text("1.0.0".into()));

            let mut exts = BTreeMap::new();
            exts.insert(CV::Text(EXT_CAPABILITIES_V1.into()), CV::Map(ext_inner));
            map.insert(CV::Text("extensions".into()), CV::Map(exts));
        }

        let manifest = CV::Map(map);
        let bytes = serde_cbor::to_vec(&manifest).expect("encode cbor");
        let file = File::create(path).expect("create file");
        let mut zip = ZipWriter::new(file);
        zip.start_file("manifest.cbor", FileOptions::<()>::default())
            .expect("start file");
        zip.write_all(&bytes).expect("write manifest");
        zip.finish().expect("finish zip");
    }

    #[test]
    fn has_capabilities_returns_true_when_present() {
        let dir = tempfile::tempdir().unwrap();
        let pack = dir.path().join("test.gtpack");
        write_test_gtpack(&pack, true);
        assert!(has_capabilities_extension(&pack));
    }

    #[test]
    fn has_capabilities_returns_false_when_missing() {
        let dir = tempfile::tempdir().unwrap();
        let pack = dir.path().join("test.gtpack");
        write_test_gtpack(&pack, false);
        assert!(!has_capabilities_extension(&pack));
    }

    #[test]
    fn has_capabilities_returns_false_for_nonexistent() {
        assert!(!has_capabilities_extension(Path::new(
            "/nonexistent.gtpack"
        )));
    }

    #[test]
    fn find_replacement_from_sibling_bundle() {
        let root = tempfile::tempdir().unwrap();

        // Bundle A: no capabilities
        let bundle_a = root.path().join("bundle-a");
        let providers_a = bundle_a.join("providers").join("messaging");
        std::fs::create_dir_all(&providers_a).unwrap();
        write_test_gtpack(&providers_a.join("messaging-test.gtpack"), false);

        // Bundle B: has capabilities
        let bundle_b = root.path().join("bundle-b");
        let providers_b = bundle_b.join("providers").join("messaging");
        std::fs::create_dir_all(&providers_b).unwrap();
        write_test_gtpack(&providers_b.join("messaging-test.gtpack"), true);

        let result = find_replacement_pack("messaging-test.gtpack", &bundle_a, "messaging");
        assert!(result.is_some());
        assert!(
            canonicalize_or_path(&result.unwrap()).starts_with(canonicalize_or_path(&bundle_b))
        );
    }

    #[test]
    fn find_replacement_returns_none_when_no_better_pack() {
        let root = tempfile::tempdir().unwrap();
        let bundle = root.path().join("bundle");
        std::fs::create_dir_all(bundle.join("providers").join("messaging")).unwrap();
        write_test_gtpack(
            &bundle
                .join("providers")
                .join("messaging")
                .join("test.gtpack"),
            false,
        );

        let result = find_replacement_pack("test.gtpack", &bundle, "messaging");
        assert!(result.is_none());
    }
}