1use crate::cli::resolve::{self, ResolveArgs};
2use crate::config::{
3 AssetConfig, ComponentConfig, ComponentOperationConfig, FlowConfig, PackConfig,
4};
5use crate::extension_refs::{
6 default_extensions_file_path, default_extensions_lock_file_path, read_extensions_file,
7 read_extensions_lock_file, validate_extensions_lock_alignment,
8};
9use crate::extensions::{
10 validate_capabilities_extension, validate_components_extension, validate_deployer_extension,
11 validate_static_routes_extension,
12};
13use crate::flow_resolve::load_flow_resolve_summary;
14use crate::runtime::{NetworkPolicy, RuntimeContext};
15use anyhow::{Context, Result, anyhow};
16use greentic_distributor_client::{DistClient, DistOptions};
17use greentic_flow::add_step::normalize::normalize_node_map;
18use greentic_flow::compile_ygtc_file;
19use greentic_flow::loader::load_ygtc_from_path;
20use greentic_pack::builder::SbomEntry;
21use greentic_pack::pack_lock::read_pack_lock;
22use greentic_types::cbor::canonical;
23use greentic_types::component_source::ComponentSourceRef;
24use greentic_types::flow_resolve_summary::FlowResolveSummaryV1;
25use greentic_types::pack::extensions::component_manifests::{
26 ComponentManifestIndexEntryV1, ComponentManifestIndexV1, EXT_COMPONENT_MANIFEST_INDEX_V1,
27 ManifestEncoding,
28};
29use greentic_types::pack::extensions::component_sources::{
30 ArtifactLocationV1, ComponentSourceEntryV1, ComponentSourcesV1, EXT_COMPONENT_SOURCES_V1,
31 ResolvedComponentV1,
32};
33use greentic_types::pack_manifest::{ExtensionInline as PackManifestExtensionInline, ExtensionRef};
34use greentic_types::{
35 BootstrapSpec, ComponentCapability, ComponentConfigurators, ComponentId, ComponentManifest,
36 ComponentOperation, ExtensionInline, Flow, FlowId, PackDependency, PackFlowEntry, PackId,
37 PackKind, PackManifest, PackSignatures, SecretRequirement, SecretScope, SemverReq,
38 encode_pack_manifest,
39};
40use semver::Version;
41use serde::Serialize;
42use serde_cbor;
43use serde_json::json;
44use serde_yaml_bw::Value as YamlValue;
45use sha2::{Digest, Sha256};
46use std::collections::{BTreeMap, BTreeSet};
47use std::fs;
48use std::io::Write;
49use std::path::{Path, PathBuf};
50use std::str::FromStr;
51use tracing::{info, warn};
52use walkdir::WalkDir;
53use zip::write::SimpleFileOptions;
54use zip::{CompressionMethod, ZipWriter};
55
56const SBOM_FORMAT: &str = "greentic-sbom-v1";
57const EXT_BUILD_MODE_ID: &str = "greentic.pack-mode.v1";
58
59#[derive(Serialize)]
60struct SbomDocument {
61 format: String,
62 files: Vec<SbomEntry>,
63}
64
65#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
66pub enum BundleMode {
67 Cache,
68 None,
69}
70
71#[derive(Clone)]
72pub struct BuildOptions {
73 pub pack_dir: PathBuf,
74 pub component_out: Option<PathBuf>,
75 pub manifest_out: PathBuf,
76 pub sbom_out: Option<PathBuf>,
77 pub gtpack_out: Option<PathBuf>,
78 pub lock_path: PathBuf,
79 pub bundle: BundleMode,
80 pub dry_run: bool,
81 pub secrets_req: Option<PathBuf>,
82 pub default_secret_scope: Option<String>,
83 pub allow_oci_tags: bool,
84 pub require_component_manifests: bool,
85 pub no_extra_dirs: bool,
86 pub dev: bool,
87 pub runtime: RuntimeContext,
88 pub skip_update: bool,
89 pub allow_pack_schema: bool,
90 pub validate_extension_refs: bool,
91}
92
93impl BuildOptions {
94 pub fn from_args(args: crate::BuildArgs, runtime: &RuntimeContext) -> Result<Self> {
95 let pack_dir = args
96 .input
97 .canonicalize()
98 .with_context(|| format!("failed to canonicalize pack dir {}", args.input.display()))?;
99
100 let component_out = args
101 .component_out
102 .map(|p| if p.is_absolute() { p } else { pack_dir.join(p) });
103 let manifest_out = args
104 .manifest
105 .map(|p| if p.is_relative() { pack_dir.join(p) } else { p })
106 .unwrap_or_else(|| pack_dir.join("dist").join("manifest.cbor"));
107 let sbom_out = args
108 .sbom
109 .map(|p| if p.is_absolute() { p } else { pack_dir.join(p) });
110 let default_gtpack_name = pack_dir
111 .file_name()
112 .and_then(|name| name.to_str())
113 .unwrap_or("pack");
114 let default_gtpack_out = pack_dir
115 .join("dist")
116 .join(format!("{default_gtpack_name}.gtpack"));
117 let gtpack_out = Some(
118 args.gtpack_out
119 .map(|p| if p.is_absolute() { p } else { pack_dir.join(p) })
120 .unwrap_or(default_gtpack_out),
121 );
122 let lock_path = args
123 .lock
124 .map(|p| if p.is_absolute() { p } else { pack_dir.join(p) })
125 .unwrap_or_else(|| pack_dir.join("pack.lock.cbor"));
126
127 Ok(Self {
128 pack_dir,
129 component_out,
130 manifest_out,
131 sbom_out,
132 gtpack_out,
133 lock_path,
134 bundle: args.bundle,
135 dry_run: args.dry_run,
136 secrets_req: args.secrets_req,
137 default_secret_scope: args.default_secret_scope,
138 allow_oci_tags: args.allow_oci_tags,
139 require_component_manifests: args.require_component_manifests,
140 no_extra_dirs: args.no_extra_dirs,
141 dev: args.dev,
142 runtime: runtime.clone(),
143 skip_update: args.no_update,
144 allow_pack_schema: args.allow_pack_schema,
145 validate_extension_refs: true,
146 })
147 }
148}
149
150pub async fn run(opts: &BuildOptions) -> Result<()> {
151 info!(
152 pack_dir = %opts.pack_dir.display(),
153 manifest_out = %opts.manifest_out.display(),
154 gtpack_out = ?opts.gtpack_out,
155 dry_run = opts.dry_run,
156 "building greentic pack"
157 );
158
159 if !opts.skip_update {
160 crate::cli::update::update_pack(&opts.pack_dir, false)?;
162 }
163
164 if !(opts.dry_run && opts.lock_path.exists()) {
167 resolve::handle(
168 ResolveArgs {
169 input: opts.pack_dir.clone(),
170 lock: Some(opts.lock_path.clone()),
171 },
172 &opts.runtime,
173 false,
174 )
175 .await?;
176 }
177
178 if opts.validate_extension_refs {
179 let extensions_file = default_extensions_file_path(&opts.pack_dir);
180 let source_extensions = if extensions_file.exists() {
181 Some(read_extensions_file(&extensions_file)?)
182 } else {
183 None
184 };
185 let extensions_lock = default_extensions_lock_file_path(&opts.pack_dir);
186 if extensions_lock.exists() {
187 let lock = read_extensions_lock_file(&extensions_lock)?;
188 if let Some(source) = source_extensions.as_ref() {
189 validate_extensions_lock_alignment(source, &lock)?;
190 }
191 }
192 }
193
194 let config = crate::config::load_pack_config(&opts.pack_dir)?;
195 info!(
196 id = %config.pack_id,
197 version = %config.version,
198 kind = %config.kind,
199 components = config.components.len(),
200 flows = config.flows.len(),
201 dependencies = config.dependencies.len(),
202 "loaded pack.yaml"
203 );
204 validate_components_extension(&config.extensions, opts.allow_oci_tags)?;
205 validate_deployer_extension(&config.extensions, &opts.pack_dir)?;
206 validate_static_routes_extension(&config.extensions, &opts.pack_dir)?;
207 if !opts.lock_path.exists() {
208 anyhow::bail!(
209 "pack.lock.cbor is required (run `greentic-pack resolve`); missing: {}",
210 opts.lock_path.display()
211 );
212 }
213 let pack_lock = read_pack_lock(&opts.lock_path).with_context(|| {
214 format!(
215 "failed to read pack lock {} (try `greentic-pack resolve`)",
216 opts.lock_path.display()
217 )
218 })?;
219 let mut known_component_ids = config
220 .components
221 .iter()
222 .map(|component| component.id.clone())
223 .collect::<BTreeSet<_>>();
224 known_component_ids.extend(pack_lock.components.keys().cloned());
225 let known_component_ids = known_component_ids.into_iter().collect::<Vec<_>>();
226 validate_capabilities_extension(&config.extensions, &opts.pack_dir, &known_component_ids)?;
227
228 let secret_requirements_override =
229 resolve_secret_requirements_override(&opts.pack_dir, opts.secrets_req.as_ref());
230 let secret_requirements = aggregate_secret_requirements(
231 &config.components,
232 secret_requirements_override.as_deref(),
233 opts.default_secret_scope.as_deref(),
234 )?;
235
236 let mut build = assemble_manifest(
237 &config,
238 &opts.pack_dir,
239 &secret_requirements,
240 !opts.no_extra_dirs,
241 opts.dev,
242 opts.allow_pack_schema,
243 )?;
244 build.lock_components =
245 collect_lock_component_artifacts(&pack_lock, &opts.runtime, opts.bundle, opts.dry_run)
246 .await?;
247
248 let mut bundled_paths = BTreeMap::new();
249 for entry in &build.lock_components {
250 bundled_paths.insert(entry.component_id.clone(), entry.logical_path.clone());
251 }
252
253 let materialized = materialize_flow_components(
254 &opts.pack_dir,
255 &build.manifest.flows,
256 &pack_lock,
257 &build.components,
258 &build.lock_components,
259 opts.require_component_manifests,
260 )?;
261 build.manifest.components.extend(materialized.components);
262 build.component_manifest_files = materialized.manifest_files;
263 build.manifest.components.sort_by(|a, b| a.id.cmp(&b.id));
264
265 let component_manifest_files =
266 collect_component_manifest_files(&build.components, &build.component_manifest_files);
267 build.manifest.extensions =
268 merge_component_manifest_extension(build.manifest.extensions, &component_manifest_files)?;
269 build.manifest.extensions = merge_component_sources_extension(
270 build.manifest.extensions,
271 &pack_lock,
272 &bundled_paths,
273 materialized.manifest_paths.as_ref(),
274 )?;
275 if !opts.dry_run {
276 greentic_pack::pack_lock::write_pack_lock(&opts.lock_path, &pack_lock)?;
277 }
278
279 let manifest_bytes = encode_pack_manifest(&build.manifest)?;
280 info!(len = manifest_bytes.len(), "encoded manifest.cbor");
281
282 if opts.dry_run {
283 info!("dry-run complete; no files written");
284 return Ok(());
285 }
286
287 if let Some(component_out) = opts.component_out.as_ref() {
288 write_stub_wasm(component_out)?;
289 }
290
291 write_bytes(&opts.manifest_out, &manifest_bytes)?;
292
293 if let Some(sbom_out) = opts.sbom_out.as_ref() {
294 write_bytes(sbom_out, br#"{"files":[]} "#)?;
295 }
296
297 if let Some(gtpack_out) = opts.gtpack_out.as_ref() {
298 let mut build = build;
299 if opts.dev && !secret_requirements.is_empty() {
300 let logical = "secret-requirements.json".to_string();
301 let req_path =
302 write_secret_requirements_file(&opts.pack_dir, &secret_requirements, &logical)?;
303 build.assets.push(AssetFile {
304 logical_path: logical,
305 source: req_path,
306 });
307 }
308 let warnings = package_gtpack(gtpack_out, &manifest_bytes, &build, opts.bundle, opts.dev)?;
309 for warning in warnings {
310 warn!(warning);
311 }
312 info!(gtpack_out = %gtpack_out.display(), "gtpack archive ready");
313 eprintln!("wrote {}", gtpack_out.display());
314 }
315
316 Ok(())
317}
318
319struct BuildProducts {
320 manifest: PackManifest,
321 components: Vec<ComponentBinary>,
322 lock_components: Vec<LockComponentBinary>,
323 component_manifest_files: Vec<ComponentManifestFile>,
324 flow_files: Vec<FlowFile>,
325 assets: Vec<AssetFile>,
326 extra_files: Vec<ExtraFile>,
327}
328
329#[derive(Clone)]
330struct ComponentBinary {
331 id: String,
332 source: PathBuf,
333 manifest_bytes: Vec<u8>,
334 manifest_path: String,
335 manifest_hash_sha256: String,
336}
337
338#[derive(Clone)]
339struct LockComponentBinary {
340 component_id: String,
341 logical_path: String,
342 source: PathBuf,
343}
344
345#[derive(Clone)]
346struct ComponentManifestFile {
347 component_id: String,
348 manifest_path: String,
349 manifest_bytes: Vec<u8>,
350 manifest_hash_sha256: String,
351}
352
353struct AssetFile {
354 logical_path: String,
355 source: PathBuf,
356}
357
358struct ExtraFile {
359 logical_path: String,
360 source: PathBuf,
361}
362
363#[derive(Clone)]
364struct FlowFile {
365 logical_path: String,
366 bytes: Vec<u8>,
367 media_type: &'static str,
368}
369
370fn assemble_manifest(
371 config: &PackConfig,
372 pack_root: &Path,
373 secret_requirements: &[SecretRequirement],
374 include_extra_dirs: bool,
375 dev_mode: bool,
376 allow_pack_schema: bool,
377) -> Result<BuildProducts> {
378 let components = build_components(&config.components, allow_pack_schema)?;
379 let (flows, flow_files) = build_flows(&config.flows, pack_root)?;
380 let dependencies = build_dependencies(&config.dependencies)?;
381 let assets = collect_assets(&config.assets, pack_root)?;
382 let extra_files = if include_extra_dirs {
383 collect_extra_dir_files(pack_root)?
384 } else {
385 Vec::new()
386 };
387 let component_manifests: Vec<_> = components.iter().map(|c| c.0.clone()).collect();
388 let bootstrap = build_bootstrap(config, &flows, &component_manifests)?;
389 let extensions = normalize_extensions(&config.extensions);
390
391 let mut manifest = PackManifest {
392 schema_version: "pack-v1".to_string(),
393 pack_id: PackId::new(config.pack_id.clone()).context("invalid pack_id")?,
394 name: config.name.clone(),
395 version: Version::parse(&config.version)
396 .context("invalid pack version (expected semver)")?,
397 kind: map_kind(&config.kind)?,
398 publisher: config.publisher.clone(),
399 components: component_manifests,
400 flows,
401 dependencies,
402 capabilities: derive_pack_capabilities(&components),
403 secret_requirements: secret_requirements.to_vec(),
404 signatures: PackSignatures::default(),
405 bootstrap,
406 extensions,
407 };
408
409 annotate_manifest_build_mode(&mut manifest, dev_mode);
410
411 Ok(BuildProducts {
412 manifest,
413 components: components.into_iter().map(|(_, bin)| bin).collect(),
414 lock_components: Vec::new(),
415 component_manifest_files: Vec::new(),
416 flow_files,
417 assets,
418 extra_files,
419 })
420}
421
422fn annotate_manifest_build_mode(manifest: &mut PackManifest, dev_mode: bool) {
423 let extensions = manifest.extensions.get_or_insert_with(BTreeMap::new);
424 extensions.insert(
425 EXT_BUILD_MODE_ID.to_string(),
426 ExtensionRef {
427 kind: EXT_BUILD_MODE_ID.to_string(),
428 version: "1".to_string(),
429 digest: None,
430 location: None,
431 inline: Some(PackManifestExtensionInline::Other(json!({
432 "mode": if dev_mode { "dev" } else { "prod" }
433 }))),
434 },
435 );
436}
437
438fn build_components(
439 configs: &[ComponentConfig],
440 allow_pack_schema: bool,
441) -> Result<Vec<(ComponentManifest, ComponentBinary)>> {
442 let mut seen = BTreeSet::new();
443 let mut result = Vec::new();
444
445 for cfg in configs {
446 if !seen.insert(cfg.id.clone()) {
447 warn!(
448 id = %cfg.id,
449 "duplicate component id in pack.yaml; keeping first entry and skipping duplicate"
450 );
451 continue;
452 }
453
454 info!(id = %cfg.id, wasm = %cfg.wasm.display(), "adding component");
455 let (manifest, binary) = resolve_component_artifacts(cfg, allow_pack_schema)?;
456
457 result.push((manifest, binary));
458 }
459
460 Ok(result)
461}
462
463fn resolve_component_artifacts(
464 cfg: &ComponentConfig,
465 allow_pack_schema: bool,
466) -> Result<(ComponentManifest, ComponentBinary)> {
467 let resolved_wasm = resolve_component_wasm_path(&cfg.wasm)?;
468
469 let mut manifest = if let Some(from_disk) =
470 load_component_manifest_from_disk(&resolved_wasm, &cfg.id)?
471 {
472 if from_disk.id.to_string() != cfg.id {
473 anyhow::bail!(
474 "component manifest id {} does not match pack.yaml id {}",
475 from_disk.id,
476 cfg.id
477 );
478 }
479 if from_disk.version.to_string() != cfg.version {
480 anyhow::bail!(
481 "component manifest version {} does not match pack.yaml version {}",
482 from_disk.version,
483 cfg.version
484 );
485 }
486 from_disk
487 } else if allow_pack_schema || is_legacy_pack_schema_component(&cfg.id) {
488 warn!(
489 id = %cfg.id,
490 "migration-only path enabled: deriving component manifest/schema from pack.yaml (--allow-pack-schema)"
491 );
492 manifest_from_config(cfg)?
493 } else {
494 anyhow::bail!(
495 "component {} is missing component.manifest.json; refusing to derive schema from pack.yaml on 0.6 path (migration-only override: --allow-pack-schema)",
496 cfg.id
497 );
498 };
499
500 if manifest.operations.is_empty() && !cfg.operations.is_empty() {
502 manifest.operations = cfg
503 .operations
504 .iter()
505 .map(operation_from_config)
506 .collect::<Result<Vec<_>>>()?;
507 }
508
509 let manifest_bytes = canonical::to_canonical_cbor_allow_floats(&manifest)
510 .context("encode component manifest to canonical cbor")?;
511 let mut sha = Sha256::new();
512 sha.update(&manifest_bytes);
513 let manifest_hash_sha256 = format!("sha256:{}", hex::encode(sha.finalize()));
514 let manifest_path = format!("components/{}.manifest.cbor", cfg.id);
515
516 let binary = ComponentBinary {
517 id: cfg.id.clone(),
518 source: resolved_wasm,
519 manifest_bytes,
520 manifest_path,
521 manifest_hash_sha256,
522 };
523
524 Ok((manifest, binary))
525}
526
527fn is_legacy_pack_schema_component(component_id: &str) -> bool {
528 matches!(
529 component_id,
530 "ai.greentic.component-provision" | "ai.greentic.component-questions"
531 )
532}
533
534fn manifest_from_config(cfg: &ComponentConfig) -> Result<ComponentManifest> {
535 Ok(ComponentManifest {
536 id: ComponentId::new(cfg.id.clone())
537 .with_context(|| format!("invalid component id {}", cfg.id))?,
538 version: Version::parse(&cfg.version)
539 .context("invalid component version (expected semver)")?,
540 supports: cfg.supports.iter().map(|k| k.to_kind()).collect(),
541 world: cfg.world.clone(),
542 profiles: cfg.profiles.clone(),
543 capabilities: cfg.capabilities.clone(),
544 configurators: convert_configurators(cfg)?,
545 operations: cfg
546 .operations
547 .iter()
548 .map(operation_from_config)
549 .collect::<Result<Vec<_>>>()?,
550 config_schema: cfg.config_schema.clone(),
551 resources: cfg.resources.clone().unwrap_or_default(),
552 dev_flows: BTreeMap::new(),
553 })
554}
555
556fn resolve_component_wasm_path(path: &Path) -> Result<PathBuf> {
557 if path.is_file() {
558 return Ok(path.to_path_buf());
559 }
560 if !path.exists() {
561 anyhow::bail!("component path {} does not exist", path.display());
562 }
563 if !path.is_dir() {
564 anyhow::bail!(
565 "component path {} must be a file or directory",
566 path.display()
567 );
568 }
569
570 let mut component_candidates = Vec::new();
571 let mut wasm_candidates = Vec::new();
572 let mut stack = vec![path.to_path_buf()];
573 while let Some(current) = stack.pop() {
574 for entry in fs::read_dir(¤t)
575 .with_context(|| format!("failed to list components in {}", current.display()))?
576 {
577 let entry = entry?;
578 let entry_type = entry.file_type()?;
579 let entry_path = entry.path();
580 if entry_type.is_dir() {
581 stack.push(entry_path);
582 continue;
583 }
584 if entry_type.is_file() && entry_path.extension() == Some(std::ffi::OsStr::new("wasm"))
585 {
586 let file_name = entry_path
587 .file_name()
588 .and_then(|n| n.to_str())
589 .unwrap_or_default();
590 if file_name.ends_with(".component.wasm") {
591 component_candidates.push(entry_path);
592 } else {
593 wasm_candidates.push(entry_path);
594 }
595 }
596 }
597 }
598
599 let choose = |mut list: Vec<PathBuf>| -> Result<PathBuf> {
600 list.sort();
601 if list.len() == 1 {
602 Ok(list.remove(0))
603 } else {
604 let options = list
605 .iter()
606 .map(|p| p.strip_prefix(path).unwrap_or(p).display().to_string())
607 .collect::<Vec<_>>()
608 .join(", ");
609 anyhow::bail!(
610 "multiple wasm artifacts found under {}: {} (pick a single *.component.wasm or *.wasm)",
611 path.display(),
612 options
613 );
614 }
615 };
616
617 if !component_candidates.is_empty() {
618 return choose(component_candidates);
619 }
620 if !wasm_candidates.is_empty() {
621 return choose(wasm_candidates);
622 }
623
624 anyhow::bail!(
625 "no wasm artifact found under {}; expected *.component.wasm or *.wasm",
626 path.display()
627 );
628}
629
630fn load_component_manifest_from_disk(
631 path: &Path,
632 component_id: &str,
633) -> Result<Option<ComponentManifest>> {
634 let manifest_dir = if path.is_dir() {
635 path.to_path_buf()
636 } else {
637 path.parent()
638 .map(Path::to_path_buf)
639 .ok_or_else(|| anyhow!("component path {} has no parent directory", path.display()))?
640 };
641 let id_manifest_suffix = format!("{component_id}.manifest");
642
643 for dir in manifest_search_dirs(&manifest_dir) {
647 let candidates = [
648 dir.join("component.manifest.cbor"),
649 dir.join("component.manifest.json"),
650 dir.join("component.json"),
651 dir.join(format!("{id_manifest_suffix}.cbor")),
652 dir.join(format!("{id_manifest_suffix}.json")),
653 dir.join(format!("{component_id}.json")),
654 ];
655 for manifest_path in candidates {
656 if !manifest_path.exists() {
657 continue;
658 }
659 let manifest = load_component_manifest_from_file(&manifest_path)?;
660 return Ok(Some(manifest));
661 }
662 }
663
664 Ok(None)
665}
666
667fn manifest_search_dirs(manifest_dir: &Path) -> Vec<PathBuf> {
668 let has_target_ancestor = std::iter::successors(Some(manifest_dir), |d| d.parent())
669 .any(|dir| dir.file_name().is_some_and(|name| name == "target"));
670 if !has_target_ancestor {
671 return vec![manifest_dir.to_path_buf()];
672 }
673
674 let mut dirs = Vec::new();
675 let mut current = Some(manifest_dir.to_path_buf());
676 let mut saw_target = false;
677
678 while let Some(dir) = current {
679 dirs.push(dir.clone());
680 if dir.file_name().is_some_and(|name| name == "target") {
681 saw_target = true;
682 } else if saw_target {
683 break;
685 }
686 current = dir.parent().map(Path::to_path_buf);
687 }
688
689 dirs
690}
691
692fn operation_from_config(cfg: &ComponentOperationConfig) -> Result<ComponentOperation> {
693 Ok(ComponentOperation {
694 name: cfg.name.clone(),
695 input_schema: cfg.input_schema.clone(),
696 output_schema: cfg.output_schema.clone(),
697 })
698}
699
700fn convert_configurators(cfg: &ComponentConfig) -> Result<Option<ComponentConfigurators>> {
701 let Some(configurators) = cfg.configurators.as_ref() else {
702 return Ok(None);
703 };
704
705 let basic = match &configurators.basic {
706 Some(id) => Some(FlowId::new(id).context("invalid configurator flow id")?),
707 None => None,
708 };
709 let full = match &configurators.full {
710 Some(id) => Some(FlowId::new(id).context("invalid configurator flow id")?),
711 None => None,
712 };
713
714 Ok(Some(ComponentConfigurators { basic, full }))
715}
716
717fn build_bootstrap(
718 config: &PackConfig,
719 flows: &[PackFlowEntry],
720 components: &[ComponentManifest],
721) -> Result<Option<BootstrapSpec>> {
722 let Some(raw) = config.bootstrap.as_ref() else {
723 return Ok(None);
724 };
725
726 let flow_ids: BTreeSet<_> = flows.iter().map(|flow| flow.id.to_string()).collect();
727 let component_ids: BTreeSet<_> = components.iter().map(|c| c.id.to_string()).collect();
728
729 let mut spec = BootstrapSpec::default();
730
731 if let Some(install_flow) = &raw.install_flow {
732 if !flow_ids.contains(install_flow) {
733 anyhow::bail!(
734 "bootstrap.install_flow references unknown flow {}",
735 install_flow
736 );
737 }
738 spec.install_flow = Some(install_flow.clone());
739 }
740
741 if let Some(upgrade_flow) = &raw.upgrade_flow {
742 if !flow_ids.contains(upgrade_flow) {
743 anyhow::bail!(
744 "bootstrap.upgrade_flow references unknown flow {}",
745 upgrade_flow
746 );
747 }
748 spec.upgrade_flow = Some(upgrade_flow.clone());
749 }
750
751 if let Some(component) = &raw.installer_component {
752 if !component_ids.contains(component) {
753 anyhow::bail!(
754 "bootstrap.installer_component references unknown component {}",
755 component
756 );
757 }
758 spec.installer_component = Some(component.clone());
759 }
760
761 if spec.install_flow.is_none()
762 && spec.upgrade_flow.is_none()
763 && spec.installer_component.is_none()
764 {
765 return Ok(None);
766 }
767
768 Ok(Some(spec))
769}
770
771fn build_flows(
772 configs: &[FlowConfig],
773 pack_root: &Path,
774) -> Result<(Vec<PackFlowEntry>, Vec<FlowFile>)> {
775 let mut seen = BTreeSet::new();
776 let mut entries = Vec::new();
777 let mut flow_files = Vec::new();
778
779 for cfg in configs {
780 info!(id = %cfg.id, path = %cfg.file.display(), "compiling flow");
781 let yaml_bytes = fs::read(&cfg.file)
782 .with_context(|| format!("failed to read flow {}", cfg.file.display()))?;
783 let mut flow: Flow = compile_ygtc_file(&cfg.file)
784 .with_context(|| format!("failed to compile {}", cfg.file.display()))?;
785 populate_component_exec_operations(&mut flow, &cfg.file).with_context(|| {
786 format!(
787 "failed to resolve component.exec operations in {}",
788 cfg.file.display()
789 )
790 })?;
791 normalize_legacy_component_exec_ids(&mut flow)?;
792 let summary = load_flow_resolve_summary(pack_root, cfg, &flow)?;
793 apply_summary_component_ids(&mut flow, &summary).with_context(|| {
794 format!("failed to resolve component ids in {}", cfg.file.display())
795 })?;
796
797 let flow_id = flow.id.to_string();
798 if !seen.insert(flow_id.clone()) {
799 anyhow::bail!("duplicate flow id {}", flow_id);
800 }
801
802 let entrypoints = if cfg.entrypoints.is_empty() {
803 flow.entrypoints.keys().cloned().collect()
804 } else {
805 cfg.entrypoints.clone()
806 };
807
808 let flow_entry = PackFlowEntry {
809 id: flow.id.clone(),
810 kind: flow.kind,
811 flow,
812 tags: cfg.tags.clone(),
813 entrypoints,
814 };
815
816 let flow_id = flow_entry.id.to_string();
817 flow_files.push(FlowFile {
818 logical_path: format!("flows/{flow_id}/flow.ygtc"),
819 bytes: yaml_bytes,
820 media_type: "application/yaml",
821 });
822 flow_files.push(FlowFile {
823 logical_path: format!("flows/{flow_id}/flow.json"),
824 bytes: serde_json::to_vec(&flow_entry.flow).context("encode flow json")?,
825 media_type: "application/json",
826 });
827 entries.push(flow_entry);
828 }
829
830 Ok((entries, flow_files))
831}
832
833fn apply_summary_component_ids(flow: &mut Flow, summary: &FlowResolveSummaryV1) -> Result<()> {
834 for (node_id, node) in flow.nodes.iter_mut() {
835 let resolved = summary.nodes.get(node_id.as_str()).ok_or_else(|| {
836 anyhow!(
837 "flow resolve summary missing node {} (expected component id for node)",
838 node_id
839 )
840 })?;
841 let summary_id = resolved.component_id.as_str();
842 if node.component.id.as_str().is_empty() || node.component.id.as_str() == "component.exec" {
843 node.component.id = resolved.component_id.clone();
844 continue;
845 }
846 if node.component.id.as_str() != summary_id {
847 anyhow::bail!(
848 "node {} component id {} does not match resolve summary {}",
849 node_id,
850 node.component.id.as_str(),
851 summary_id
852 );
853 }
854 }
855 Ok(())
856}
857
858fn populate_component_exec_operations(flow: &mut Flow, path: &Path) -> Result<()> {
859 let needs_op = flow.nodes.values().any(|node| {
860 node.component.id.as_str() == "component.exec" && node.component.operation.is_none()
861 });
862 if !needs_op {
863 return Ok(());
864 }
865
866 let flow_doc = load_ygtc_from_path(path)?;
867 let mut operations = BTreeMap::new();
868
869 for (node_id, node_doc) in flow_doc.nodes {
870 let value = serde_json::to_value(&node_doc)
871 .with_context(|| format!("failed to normalize component.exec node {}", node_id))?;
872 let normalized = normalize_node_map(value)?;
873 if !normalized.operation.trim().is_empty() {
874 operations.insert(node_id, normalized.operation);
875 }
876 }
877
878 for (node_id, node) in flow.nodes.iter_mut() {
879 if node.component.id.as_str() != "component.exec" || node.component.operation.is_some() {
880 continue;
881 }
882 if let Some(op) = operations.get(node_id.as_str()) {
883 node.component.operation = Some(op.clone());
884 }
885 }
886
887 Ok(())
888}
889
890fn normalize_legacy_component_exec_ids(flow: &mut Flow) -> Result<()> {
891 for (node_id, node) in flow.nodes.iter_mut() {
892 if node.component.id.as_str() != "component.exec" {
893 continue;
894 }
895 let Some(op) = node.component.operation.as_deref() else {
896 continue;
897 };
898 if !op.contains('.') && !op.contains(':') {
899 continue;
900 }
901 node.component.id = ComponentId::new(op).with_context(|| {
902 format!("invalid component id {} resolved for node {}", op, node_id)
903 })?;
904 node.component.operation = None;
905 }
906 Ok(())
907}
908
909fn build_dependencies(configs: &[crate::config::DependencyConfig]) -> Result<Vec<PackDependency>> {
910 let mut deps = Vec::new();
911 let mut seen = BTreeSet::new();
912 for cfg in configs {
913 if !seen.insert(cfg.alias.clone()) {
914 anyhow::bail!("duplicate dependency alias {}", cfg.alias);
915 }
916 deps.push(PackDependency {
917 alias: cfg.alias.clone(),
918 pack_id: PackId::new(cfg.pack_id.clone()).context("invalid dependency pack_id")?,
919 version_req: SemverReq::parse(&cfg.version_req)
920 .context("invalid dependency version requirement")?,
921 required_capabilities: cfg.required_capabilities.clone(),
922 });
923 }
924 Ok(deps)
925}
926
927fn collect_assets(configs: &[AssetConfig], pack_root: &Path) -> Result<Vec<AssetFile>> {
928 let mut assets = Vec::new();
929 for cfg in configs {
930 let logical = cfg
931 .path
932 .strip_prefix(pack_root)
933 .unwrap_or(&cfg.path)
934 .components()
935 .map(|c| c.as_os_str().to_string_lossy().into_owned())
936 .collect::<Vec<_>>()
937 .join("/");
938 if logical.is_empty() {
939 anyhow::bail!("invalid asset path {}", cfg.path.display());
940 }
941 assets.push(AssetFile {
942 logical_path: logical,
943 source: cfg.path.clone(),
944 });
945 }
946 Ok(assets)
947}
948
949fn is_reserved_extra_file(logical_path: &str) -> bool {
950 if matches!(logical_path, "sbom.cbor" | "sbom.json") {
951 return true;
952 }
953 if let Some(name) = logical_path.rsplit('/').next()
954 && name.ends_with(".gtpack")
955 {
956 return true;
957 }
958 false
959}
960
961fn collect_extra_dir_files(pack_root: &Path) -> Result<Vec<ExtraFile>> {
962 let excluded = [
963 "components",
964 "flows",
965 "dist",
966 "target",
967 ".git",
968 ".github",
969 ".idea",
970 ".vscode",
971 "node_modules",
972 ];
973 let mut entries = Vec::new();
974 let mut seen = BTreeSet::new();
975 for entry in fs::read_dir(pack_root)
976 .with_context(|| format!("failed to list pack root {}", pack_root.display()))?
977 {
978 let entry = entry?;
979 let entry_type = entry.file_type()?;
980 let name = entry.file_name();
981 let name = name.to_string_lossy();
982 if entry_type.is_file() {
983 let logical = name.to_string();
984 if is_reserved_extra_file(&logical) {
985 continue;
986 }
987 if !logical.is_empty() && seen.insert(logical.clone()) {
988 entries.push(ExtraFile {
989 logical_path: logical,
990 source: entry.path(),
991 });
992 }
993 continue;
994 }
995 if !entry_type.is_dir() {
996 continue;
997 }
998 if name.starts_with('.') || excluded.contains(&name.as_ref()) {
999 continue;
1000 }
1001 let root = entry.path();
1002 for sub in WalkDir::new(&root)
1003 .into_iter()
1004 .filter_entry(|walk| {
1005 let name = walk.file_name().to_string_lossy();
1006 !name.starts_with('.')
1007 })
1008 .filter_map(Result::ok)
1009 {
1010 if !sub.file_type().is_file() {
1011 continue;
1012 }
1013 let logical = sub
1014 .path()
1015 .strip_prefix(pack_root)
1016 .unwrap_or(sub.path())
1017 .components()
1018 .map(|c| c.as_os_str().to_string_lossy().into_owned())
1019 .collect::<Vec<_>>()
1020 .join("/");
1021 if logical.is_empty() || !seen.insert(logical.clone()) {
1022 continue;
1023 }
1024 if is_reserved_extra_file(&logical) {
1025 continue;
1026 }
1027 entries.push(ExtraFile {
1028 logical_path: logical,
1029 source: sub.path().to_path_buf(),
1030 });
1031 }
1032 }
1033 Ok(entries)
1034}
1035
1036fn map_extra_files(
1037 extras: &[ExtraFile],
1038 asset_paths: &mut BTreeSet<String>,
1039 dev_mode: bool,
1040 warnings: &mut Vec<String>,
1041) -> Vec<(String, PathBuf)> {
1042 let mut mapped = Vec::new();
1043 for extra in extras {
1044 let logical = extra.logical_path.as_str();
1045 if logical.starts_with("assets/") {
1046 if asset_paths.insert(logical.to_string()) {
1047 mapped.push((logical.to_string(), extra.source.clone()));
1048 }
1049 continue;
1050 }
1051 if !logical.contains('/') {
1052 if is_reserved_source_file(logical) {
1053 if dev_mode || logical == "pack.lock.cbor" {
1054 mapped.push((logical.to_string(), extra.source.clone()));
1055 }
1056 continue;
1057 }
1058 let target = format!("assets/{logical}");
1059 if asset_paths.insert(target.clone()) {
1060 mapped.push((target, extra.source.clone()));
1061 } else {
1062 warnings.push(format!(
1063 "skipping root asset {logical} because assets/{logical} already exists"
1064 ));
1065 }
1066 continue;
1067 }
1068 mapped.push((logical.to_string(), extra.source.clone()));
1069 }
1070 mapped
1071}
1072
1073fn is_reserved_source_file(path: &str) -> bool {
1074 matches!(
1075 path,
1076 "pack.yaml"
1077 | "pack.manifest.json"
1078 | "pack.lock.cbor"
1079 | "manifest.json"
1080 | "manifest.cbor"
1081 | "sbom.json"
1082 | "sbom.cbor"
1083 | "provenance.json"
1084 | "secret-requirements.json"
1085 | "secrets_requirements.json"
1086 ) || path.ends_with(".ygtc")
1087}
1088
1089fn normalize_extensions(
1090 extensions: &Option<BTreeMap<String, greentic_types::ExtensionRef>>,
1091) -> Option<BTreeMap<String, greentic_types::ExtensionRef>> {
1092 extensions.as_ref().filter(|map| !map.is_empty()).cloned()
1093}
1094
1095fn merge_component_manifest_extension(
1096 extensions: Option<BTreeMap<String, ExtensionRef>>,
1097 manifest_files: &[ComponentManifestFile],
1098) -> Result<Option<BTreeMap<String, ExtensionRef>>> {
1099 if manifest_files.is_empty() {
1100 return Ok(extensions);
1101 }
1102
1103 let entries: Vec<_> = manifest_files
1104 .iter()
1105 .map(|entry| ComponentManifestIndexEntryV1 {
1106 component_id: entry.component_id.clone(),
1107 manifest_file: entry.manifest_path.clone(),
1108 encoding: ManifestEncoding::Cbor,
1109 content_hash: Some(entry.manifest_hash_sha256.clone()),
1110 })
1111 .collect();
1112
1113 let index = ComponentManifestIndexV1::new(entries);
1114 let value = index
1115 .to_extension_value()
1116 .context("serialize component manifest index extension")?;
1117
1118 let ext = ExtensionRef {
1119 kind: EXT_COMPONENT_MANIFEST_INDEX_V1.to_string(),
1120 version: "v1".to_string(),
1121 digest: None,
1122 location: None,
1123 inline: Some(ExtensionInline::Other(value)),
1124 };
1125
1126 let mut map = extensions.unwrap_or_default();
1127 map.insert(EXT_COMPONENT_MANIFEST_INDEX_V1.to_string(), ext);
1128 if map.is_empty() {
1129 Ok(None)
1130 } else {
1131 Ok(Some(map))
1132 }
1133}
1134
1135fn merge_component_sources_extension(
1136 extensions: Option<BTreeMap<String, ExtensionRef>>,
1137 lock: &greentic_pack::pack_lock::PackLockV1,
1138 bundled_paths: &BTreeMap<String, String>,
1139 manifest_paths: Option<&std::collections::BTreeMap<String, String>>,
1140) -> Result<Option<BTreeMap<String, ExtensionRef>>> {
1141 let mut entries = Vec::new();
1142 for comp in lock.components.values() {
1143 let Some(reference) = comp.r#ref.as_ref() else {
1144 continue;
1145 };
1146 if reference.starts_with("file://") {
1147 continue;
1148 }
1149 let source = match ComponentSourceRef::from_str(reference) {
1150 Ok(parsed) => parsed,
1151 Err(_) => {
1152 eprintln!(
1153 "warning: skipping pack.lock entry `{}` with unsupported ref {}",
1154 comp.component_id, reference
1155 );
1156 continue;
1157 }
1158 };
1159 let manifest_path = manifest_paths.and_then(|paths| paths.get(&comp.component_id).cloned());
1160 let artifact = if let Some(wasm_path) = bundled_paths.get(&comp.component_id) {
1161 ArtifactLocationV1::Inline {
1162 wasm_path: wasm_path.clone(),
1163 manifest_path,
1164 }
1165 } else {
1166 ArtifactLocationV1::Remote
1167 };
1168 entries.push(ComponentSourceEntryV1 {
1169 name: comp.component_id.clone(),
1170 component_id: Some(ComponentId::new(comp.component_id.clone()).map_err(|err| {
1171 anyhow!(
1172 "invalid component id {} in lock: {}",
1173 comp.component_id,
1174 err
1175 )
1176 })?),
1177 source,
1178 resolved: ResolvedComponentV1 {
1179 digest: comp.resolved_digest.clone(),
1180 signature: None,
1181 signed_by: None,
1182 },
1183 artifact,
1184 licensing_hint: None,
1185 metering_hint: None,
1186 });
1187 }
1188
1189 if entries.is_empty() {
1190 return Ok(extensions);
1191 }
1192
1193 let payload = ComponentSourcesV1::new(entries)
1194 .to_extension_value()
1195 .context("serialize component_sources extension")?;
1196
1197 let ext = ExtensionRef {
1198 kind: EXT_COMPONENT_SOURCES_V1.to_string(),
1199 version: "v1".to_string(),
1200 digest: None,
1201 location: None,
1202 inline: Some(ExtensionInline::Other(payload)),
1203 };
1204
1205 let mut map = extensions.unwrap_or_default();
1206 map.insert(EXT_COMPONENT_SOURCES_V1.to_string(), ext);
1207 if map.is_empty() {
1208 Ok(None)
1209 } else {
1210 Ok(Some(map))
1211 }
1212}
1213
1214fn derive_pack_capabilities(
1215 components: &[(ComponentManifest, ComponentBinary)],
1216) -> Vec<ComponentCapability> {
1217 let mut seen = BTreeSet::new();
1218 let mut caps = Vec::new();
1219
1220 for (component, _) in components {
1221 let mut add = |name: &str| {
1222 if seen.insert(name.to_string()) {
1223 caps.push(ComponentCapability {
1224 name: name.to_string(),
1225 description: None,
1226 });
1227 }
1228 };
1229
1230 if component.capabilities.host.secrets.is_some() {
1231 add("host:secrets");
1232 }
1233 if let Some(state) = &component.capabilities.host.state {
1234 if state.read {
1235 add("host:state:read");
1236 }
1237 if state.write {
1238 add("host:state:write");
1239 }
1240 }
1241 if component.capabilities.host.messaging.is_some() {
1242 add("host:messaging");
1243 }
1244 if component.capabilities.host.events.is_some() {
1245 add("host:events");
1246 }
1247 if component.capabilities.host.http.is_some() {
1248 add("host:http");
1249 }
1250 if component.capabilities.host.telemetry.is_some() {
1251 add("host:telemetry");
1252 }
1253 if component.capabilities.host.iac.is_some() {
1254 add("host:iac");
1255 }
1256 if let Some(fs) = component.capabilities.wasi.filesystem.as_ref() {
1257 add(&format!(
1258 "wasi:fs:{}",
1259 format!("{:?}", fs.mode).to_lowercase()
1260 ));
1261 if !fs.mounts.is_empty() {
1262 add("wasi:fs:mounts");
1263 }
1264 }
1265 if component.capabilities.wasi.random {
1266 add("wasi:random");
1267 }
1268 if component.capabilities.wasi.clocks {
1269 add("wasi:clocks");
1270 }
1271 }
1272
1273 caps
1274}
1275
1276fn map_kind(raw: &str) -> Result<PackKind> {
1277 match raw.to_ascii_lowercase().as_str() {
1278 "application" => Ok(PackKind::Application),
1279 "provider" => Ok(PackKind::Provider),
1280 "infrastructure" => Ok(PackKind::Infrastructure),
1281 "library" => Ok(PackKind::Library),
1282 other => Err(anyhow!("unknown pack kind {}", other)),
1283 }
1284}
1285
1286fn package_gtpack(
1287 out_path: &Path,
1288 manifest_bytes: &[u8],
1289 build: &BuildProducts,
1290 bundle: BundleMode,
1291 dev_mode: bool,
1292) -> Result<Vec<String>> {
1293 if let Some(parent) = out_path.parent() {
1294 fs::create_dir_all(parent)
1295 .with_context(|| format!("failed to create {}", parent.display()))?;
1296 }
1297
1298 let file = fs::File::create(out_path)
1299 .with_context(|| format!("failed to create {}", out_path.display()))?;
1300 let mut writer = ZipWriter::new(file);
1301 let options = SimpleFileOptions::default()
1302 .compression_method(CompressionMethod::Stored)
1303 .unix_permissions(0o644);
1304
1305 let mut sbom_entries = Vec::new();
1306 let mut written_paths = BTreeSet::new();
1307 let mut warnings = Vec::new();
1308 let mut asset_paths = BTreeSet::new();
1309 record_sbom_entry(
1310 &mut sbom_entries,
1311 "manifest.cbor",
1312 manifest_bytes,
1313 "application/cbor",
1314 );
1315 written_paths.insert("manifest.cbor".to_string());
1316 write_zip_entry(&mut writer, "manifest.cbor", manifest_bytes, options)?;
1317
1318 if dev_mode {
1319 let mut flow_files = build.flow_files.clone();
1320 flow_files.sort_by(|a, b| a.logical_path.cmp(&b.logical_path));
1321 for flow_file in flow_files {
1322 if written_paths.insert(flow_file.logical_path.clone()) {
1323 record_sbom_entry(
1324 &mut sbom_entries,
1325 &flow_file.logical_path,
1326 &flow_file.bytes,
1327 flow_file.media_type,
1328 );
1329 write_zip_entry(
1330 &mut writer,
1331 &flow_file.logical_path,
1332 &flow_file.bytes,
1333 options,
1334 )?;
1335 }
1336 }
1337 }
1338
1339 let mut component_wasm_paths = BTreeSet::new();
1340 if bundle != BundleMode::None {
1341 for comp in &build.components {
1342 component_wasm_paths.insert(format!("components/{}.wasm", comp.id));
1343 }
1344 }
1345 let mut manifest_component_ids = BTreeSet::new();
1346 for manifest in &build.component_manifest_files {
1347 manifest_component_ids.insert(manifest.component_id.clone());
1348 }
1349
1350 let mut lock_components = build.lock_components.clone();
1351 lock_components.sort_by(|a, b| a.logical_path.cmp(&b.logical_path));
1352 for comp in lock_components {
1353 if component_wasm_paths.contains(&comp.logical_path) {
1354 continue;
1355 }
1356 if !written_paths.insert(comp.logical_path.clone()) {
1357 continue;
1358 }
1359 let bytes = fs::read(&comp.source).with_context(|| {
1360 format!("failed to read cached component {}", comp.source.display())
1361 })?;
1362 record_sbom_entry(
1363 &mut sbom_entries,
1364 &comp.logical_path,
1365 &bytes,
1366 "application/wasm",
1367 );
1368 write_zip_entry(&mut writer, &comp.logical_path, &bytes, options)?;
1369 let describe_source = PathBuf::from(format!("{}.describe.cbor", comp.source.display()));
1370 if describe_source.exists() {
1371 let describe_bytes = fs::read(&describe_source).with_context(|| {
1372 format!(
1373 "failed to read describe cache {}",
1374 describe_source.display()
1375 )
1376 })?;
1377 let describe_logical = format!("{}.describe.cbor", comp.logical_path);
1378 if written_paths.insert(describe_logical.clone()) {
1379 record_sbom_entry(
1380 &mut sbom_entries,
1381 &describe_logical,
1382 &describe_bytes,
1383 "application/cbor",
1384 );
1385 write_zip_entry(&mut writer, &describe_logical, &describe_bytes, options)?;
1386 }
1387 }
1388
1389 if manifest_component_ids.contains(&comp.component_id) {
1390 let alias_path = format!("components/{}.wasm", comp.component_id);
1391 if written_paths.insert(alias_path.clone()) {
1392 record_sbom_entry(&mut sbom_entries, &alias_path, &bytes, "application/wasm");
1393 write_zip_entry(&mut writer, &alias_path, &bytes, options)?;
1394 }
1395 let describe_source = PathBuf::from(format!("{}.describe.cbor", comp.source.display()));
1396 if describe_source.exists() {
1397 let describe_bytes = fs::read(&describe_source).with_context(|| {
1398 format!(
1399 "failed to read describe cache {}",
1400 describe_source.display()
1401 )
1402 })?;
1403 let alias_describe = format!("{alias_path}.describe.cbor");
1404 if written_paths.insert(alias_describe.clone()) {
1405 record_sbom_entry(
1406 &mut sbom_entries,
1407 &alias_describe,
1408 &describe_bytes,
1409 "application/cbor",
1410 );
1411 write_zip_entry(&mut writer, &alias_describe, &describe_bytes, options)?;
1412 }
1413 }
1414 }
1415 }
1416
1417 let mut lock_manifests = build.component_manifest_files.clone();
1418 lock_manifests.sort_by(|a, b| a.manifest_path.cmp(&b.manifest_path));
1419 for manifest in lock_manifests {
1420 if written_paths.insert(manifest.manifest_path.clone()) {
1421 record_sbom_entry(
1422 &mut sbom_entries,
1423 &manifest.manifest_path,
1424 &manifest.manifest_bytes,
1425 "application/cbor",
1426 );
1427 write_zip_entry(
1428 &mut writer,
1429 &manifest.manifest_path,
1430 &manifest.manifest_bytes,
1431 options,
1432 )?;
1433 }
1434 }
1435
1436 if bundle != BundleMode::None {
1437 let mut components = build.components.clone();
1438 components.sort_by(|a, b| a.id.cmp(&b.id));
1439 for comp in components {
1440 let logical_wasm = format!("components/{}.wasm", comp.id);
1441 let wasm_bytes = fs::read(&comp.source)
1442 .with_context(|| format!("failed to read component {}", comp.source.display()))?;
1443 if written_paths.insert(logical_wasm.clone()) {
1444 record_sbom_entry(
1445 &mut sbom_entries,
1446 &logical_wasm,
1447 &wasm_bytes,
1448 "application/wasm",
1449 );
1450 write_zip_entry(&mut writer, &logical_wasm, &wasm_bytes, options)?;
1451 }
1452 let describe_source = PathBuf::from(format!("{}.describe.cbor", comp.source.display()));
1453 if describe_source.exists() {
1454 let describe_bytes = fs::read(&describe_source).with_context(|| {
1455 format!(
1456 "failed to read describe cache {}",
1457 describe_source.display()
1458 )
1459 })?;
1460 let describe_logical = format!("{logical_wasm}.describe.cbor");
1461 if written_paths.insert(describe_logical.clone()) {
1462 record_sbom_entry(
1463 &mut sbom_entries,
1464 &describe_logical,
1465 &describe_bytes,
1466 "application/cbor",
1467 );
1468 write_zip_entry(&mut writer, &describe_logical, &describe_bytes, options)?;
1469 }
1470 }
1471
1472 if written_paths.insert(comp.manifest_path.clone()) {
1473 record_sbom_entry(
1474 &mut sbom_entries,
1475 &comp.manifest_path,
1476 &comp.manifest_bytes,
1477 "application/cbor",
1478 );
1479 write_zip_entry(
1480 &mut writer,
1481 &comp.manifest_path,
1482 &comp.manifest_bytes,
1483 options,
1484 )?;
1485 }
1486 }
1487 }
1488
1489 let mut extra_entries: Vec<_> = Vec::new();
1490 for asset in &build.assets {
1491 let logical = format!("assets/{}", asset.logical_path);
1492 asset_paths.insert(logical.clone());
1493 extra_entries.push((logical, asset.source.clone()));
1494 }
1495 let mut mapped_extra = map_extra_files(
1496 &build.extra_files,
1497 &mut asset_paths,
1498 dev_mode,
1499 &mut warnings,
1500 );
1501 extra_entries.append(&mut mapped_extra);
1502 extra_entries.sort_by(|a, b| a.0.cmp(&b.0));
1503 for (logical, source) in extra_entries {
1504 if !written_paths.insert(logical.clone()) {
1505 continue;
1506 }
1507 let bytes = fs::read(&source)
1508 .with_context(|| format!("failed to read extra file {}", source.display()))?;
1509 record_sbom_entry(
1510 &mut sbom_entries,
1511 &logical,
1512 &bytes,
1513 "application/octet-stream",
1514 );
1515 write_zip_entry(&mut writer, &logical, &bytes, options)?;
1516 }
1517
1518 sbom_entries.sort_by(|a, b| a.path.cmp(&b.path));
1519 let sbom_doc = SbomDocument {
1520 format: SBOM_FORMAT.to_string(),
1521 files: sbom_entries,
1522 };
1523 let sbom_bytes = canonical::to_canonical_cbor_allow_floats(&sbom_doc)
1524 .context("failed to encode canonical sbom.cbor")?;
1525 write_zip_entry(&mut writer, "sbom.cbor", &sbom_bytes, options)?;
1526
1527 writer
1528 .finish()
1529 .context("failed to finalise gtpack archive")?;
1530 Ok(warnings)
1531}
1532
1533async fn collect_lock_component_artifacts(
1534 lock: &greentic_pack::pack_lock::PackLockV1,
1535 runtime: &RuntimeContext,
1536 bundle: BundleMode,
1537 allow_missing: bool,
1538) -> Result<Vec<LockComponentBinary>> {
1539 let dist = DistClient::new(DistOptions {
1540 cache_dir: runtime.cache_dir(),
1541 allow_tags: true,
1542 offline: runtime.network_policy() == NetworkPolicy::Offline,
1543 allow_insecure_local_http: false,
1544 ..DistOptions::default()
1545 });
1546
1547 let mut artifacts = Vec::new();
1548 let mut seen_paths = BTreeSet::new();
1549 for comp in lock.components.values() {
1550 let Some(reference) = comp.r#ref.as_ref() else {
1551 continue;
1552 };
1553 if reference.starts_with("file://") {
1554 continue;
1555 }
1556 let parsed = ComponentSourceRef::from_str(reference).ok();
1557 let is_tag = parsed.as_ref().map(|r| r.is_tag()).unwrap_or(false);
1558 let should_bundle = is_tag || bundle == BundleMode::Cache;
1559 if !should_bundle {
1560 continue;
1561 }
1562
1563 let resolved = if is_tag {
1564 let item = if runtime.network_policy() == NetworkPolicy::Offline {
1565 dist.open_cached(&comp.resolved_digest).map_err(|err| {
1566 anyhow!(
1567 "tag ref {} must be bundled but cache is missing ({})",
1568 reference,
1569 err
1570 )
1571 })?
1572 } else {
1573 let source = dist
1574 .parse_source(reference)
1575 .map_err(|err| anyhow!("failed to parse {}: {}", reference, err))?;
1576 let descriptor = dist
1577 .resolve(source, greentic_distributor_client::ResolvePolicy)
1578 .await
1579 .map_err(|err| anyhow!("failed to resolve {}: {}", reference, err))?;
1580 dist.fetch(&descriptor, greentic_distributor_client::CachePolicy)
1581 .await
1582 .map_err(|err| anyhow!("failed to fetch {}: {}", reference, err))?
1583 };
1584 let cache_path = item.cache_path.clone().ok_or_else(|| {
1585 anyhow!("tag ref {} resolved but cache path is missing", reference)
1586 })?;
1587 ResolvedLockItem { cache_path }
1588 } else {
1589 let mut resolved = dist
1590 .open_cached(&comp.resolved_digest)
1591 .ok()
1592 .and_then(|item| item.cache_path.clone().map(|path| (item, path)));
1593 if resolved.is_none()
1594 && runtime.network_policy() != NetworkPolicy::Offline
1595 && !allow_missing
1596 && reference.starts_with("oci://")
1597 {
1598 let source = dist
1599 .parse_source(reference)
1600 .map_err(|err| anyhow!("failed to parse {}: {}", reference, err))?;
1601 let descriptor = dist
1602 .resolve(source, greentic_distributor_client::ResolvePolicy)
1603 .await
1604 .map_err(|err| anyhow!("failed to resolve {}: {}", reference, err))?;
1605 let item = dist
1606 .fetch(&descriptor, greentic_distributor_client::CachePolicy)
1607 .await
1608 .map_err(|err| anyhow!("failed to fetch {}: {}", reference, err))?;
1609 if let Some(path) = item.cache_path.clone() {
1610 resolved = Some((item, path));
1611 }
1612 }
1613 let Some((_item, path)) = resolved else {
1614 if runtime.network_policy() == NetworkPolicy::Offline {
1615 if allow_missing {
1616 eprintln!(
1617 "warning: component {} is not cached; skipping embed",
1618 comp.component_id
1619 );
1620 continue;
1621 }
1622 anyhow::bail!(
1623 "component {} requires network access ({}) but cache is missing; offline builds cannot download artifacts",
1624 comp.component_id,
1625 reference
1626 );
1627 }
1628 eprintln!(
1629 "warning: component {} is not cached; skipping embed",
1630 comp.component_id
1631 );
1632 continue;
1633 };
1634 ResolvedLockItem { cache_path: path }
1635 };
1636
1637 let cache_path = resolved.cache_path;
1638 let bytes = fs::read(&cache_path)
1639 .with_context(|| format!("failed to read cached component {}", cache_path.display()))?;
1640 let wasm_sha256 = hex::encode(Sha256::digest(&bytes));
1641 let logical_path = if is_tag {
1642 format!("blobs/sha256/{}.wasm", wasm_sha256)
1643 } else {
1644 format!("components/{}.wasm", comp.component_id)
1645 };
1646
1647 if seen_paths.insert(logical_path.clone()) {
1648 artifacts.push(LockComponentBinary {
1649 component_id: comp.component_id.clone(),
1650 logical_path: logical_path.clone(),
1651 source: cache_path.clone(),
1652 });
1653 }
1654 }
1655
1656 Ok(artifacts)
1657}
1658
1659struct ResolvedLockItem {
1660 cache_path: PathBuf,
1661}
1662
1663struct MaterializedComponents {
1664 components: Vec<ComponentManifest>,
1665 manifest_files: Vec<ComponentManifestFile>,
1666 manifest_paths: Option<BTreeMap<String, String>>,
1667}
1668
1669fn record_sbom_entry(entries: &mut Vec<SbomEntry>, path: &str, bytes: &[u8], media_type: &str) {
1670 entries.push(SbomEntry {
1671 path: path.to_string(),
1672 size: bytes.len() as u64,
1673 hash_blake3: blake3::hash(bytes).to_hex().to_string(),
1674 media_type: media_type.to_string(),
1675 });
1676}
1677
1678fn write_zip_entry(
1679 writer: &mut ZipWriter<std::fs::File>,
1680 logical_path: &str,
1681 bytes: &[u8],
1682 options: SimpleFileOptions,
1683) -> Result<()> {
1684 writer
1685 .start_file(logical_path, options)
1686 .with_context(|| format!("failed to start {}", logical_path))?;
1687 writer
1688 .write_all(bytes)
1689 .with_context(|| format!("failed to write {}", logical_path))?;
1690 Ok(())
1691}
1692
1693fn write_bytes(path: &Path, bytes: &[u8]) -> Result<()> {
1694 if let Some(parent) = path.parent() {
1695 fs::create_dir_all(parent)
1696 .with_context(|| format!("failed to create directory {}", parent.display()))?;
1697 }
1698 fs::write(path, bytes).with_context(|| format!("failed to write {}", path.display()))?;
1699 Ok(())
1700}
1701
1702fn write_stub_wasm(path: &Path) -> Result<()> {
1703 const STUB: &[u8] = &[0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00];
1704 write_bytes(path, STUB)
1705}
1706
1707fn collect_component_manifest_files(
1708 components: &[ComponentBinary],
1709 extra: &[ComponentManifestFile],
1710) -> Vec<ComponentManifestFile> {
1711 let mut files: Vec<ComponentManifestFile> = components
1712 .iter()
1713 .map(|binary| ComponentManifestFile {
1714 component_id: binary.id.clone(),
1715 manifest_path: binary.manifest_path.clone(),
1716 manifest_bytes: binary.manifest_bytes.clone(),
1717 manifest_hash_sha256: binary.manifest_hash_sha256.clone(),
1718 })
1719 .collect();
1720 files.extend(extra.iter().cloned());
1721 files.sort_by(|a, b| a.component_id.cmp(&b.component_id));
1722 files.dedup_by(|a, b| a.component_id == b.component_id);
1723 files
1724}
1725
1726fn materialize_flow_components(
1727 pack_dir: &Path,
1728 flows: &[PackFlowEntry],
1729 pack_lock: &greentic_pack::pack_lock::PackLockV1,
1730 components: &[ComponentBinary],
1731 lock_components: &[LockComponentBinary],
1732 require_component_manifests: bool,
1733) -> Result<MaterializedComponents> {
1734 let referenced = collect_flow_component_ids(flows);
1735 if referenced.is_empty() {
1736 return Ok(MaterializedComponents {
1737 components: Vec::new(),
1738 manifest_files: Vec::new(),
1739 manifest_paths: None,
1740 });
1741 }
1742
1743 let mut existing = BTreeSet::new();
1744 for component in components {
1745 existing.insert(component.id.clone());
1746 }
1747
1748 let mut lock_by_id = BTreeMap::new();
1749 for (key, entry) in &pack_lock.components {
1750 lock_by_id.insert(key.clone(), entry);
1751 }
1752
1753 let mut bundle_sources_by_component = BTreeMap::new();
1754 for entry in lock_components {
1755 bundle_sources_by_component.insert(entry.component_id.clone(), entry.source.clone());
1756 }
1757
1758 let mut materialized_components = Vec::new();
1759 let mut manifest_files = Vec::new();
1760 let mut manifest_paths: BTreeMap<String, String> = BTreeMap::new();
1761
1762 for component_id in referenced {
1763 if existing.contains(&component_id) {
1764 continue;
1765 }
1766
1767 let lock_entry = lock_by_id.get(&component_id).copied();
1768 let Some(lock_entry) = lock_entry else {
1769 handle_missing_component_manifest(&component_id, None, require_component_manifests)?;
1770 continue;
1771 };
1772 let bundled_source = bundle_sources_by_component.get(&component_id);
1773 if bundled_source.is_none() {
1774 if require_component_manifests {
1775 anyhow::bail!(
1776 "component {} is not bundled; cannot materialize manifest without local artifacts",
1777 lock_entry.component_id
1778 );
1779 }
1780 eprintln!(
1781 "warning: component {} resolved via lock but not bundled locally",
1782 lock_entry.component_id
1783 );
1784 continue;
1785 }
1786
1787 let manifest =
1788 load_component_manifest_for_lock(pack_dir, &lock_entry.component_id, bundled_source)?;
1789
1790 let Some(manifest) = manifest else {
1791 if require_component_manifests {
1792 anyhow::bail!(
1793 "component manifest metadata missing for {} (supply component.manifest.json or use --require-component-manifests=false)",
1794 component_id
1795 );
1796 }
1797 eprintln!(
1798 "warning: component manifest metadata missing for {}; component will not appear in manifest.components",
1799 component_id
1800 );
1801 continue;
1802 };
1803
1804 if manifest.id.as_str() != lock_entry.component_id.as_str() {
1805 anyhow::bail!(
1806 "component manifest id {} does not match pack.lock component_id {}",
1807 manifest.id.as_str(),
1808 lock_entry.component_id.as_str()
1809 );
1810 }
1811
1812 let manifest_file = component_manifest_file_from_manifest(&manifest)?;
1813 manifest_paths.insert(
1814 manifest.id.as_str().to_string(),
1815 manifest_file.manifest_path.clone(),
1816 );
1817 manifest_paths.insert(
1818 lock_entry.component_id.clone(),
1819 manifest_file.manifest_path.clone(),
1820 );
1821
1822 materialized_components.push(manifest);
1823 manifest_files.push(manifest_file);
1824 }
1825
1826 let manifest_paths = if manifest_paths.is_empty() {
1827 None
1828 } else {
1829 Some(manifest_paths)
1830 };
1831
1832 Ok(MaterializedComponents {
1833 components: materialized_components,
1834 manifest_files,
1835 manifest_paths,
1836 })
1837}
1838
1839fn collect_flow_component_ids(flows: &[PackFlowEntry]) -> BTreeSet<String> {
1840 let mut ids = BTreeSet::new();
1841 for flow in flows {
1842 for node in flow.flow.nodes.values() {
1843 if node.component.pack_alias.is_some() {
1844 continue;
1845 }
1846 let id = node.component.id.as_str();
1847 if !id.is_empty() && !is_builtin_component_id(id) {
1848 ids.insert(id.to_string());
1849 }
1850 }
1851 }
1852 ids
1853}
1854
1855fn is_builtin_component_id(id: &str) -> bool {
1856 matches!(id, "session.wait" | "flow.call" | "provider.invoke") || id.starts_with("emit.")
1857}
1858
1859fn load_component_manifest_for_lock(
1860 pack_dir: &Path,
1861 component_id: &str,
1862 bundled_source: Option<&PathBuf>,
1863) -> Result<Option<ComponentManifest>> {
1864 let mut search_paths = Vec::new();
1865 search_paths.extend(component_manifest_search_paths(pack_dir, component_id));
1866 if let Some(source) = bundled_source
1867 && let Some(parent) = source.parent()
1868 {
1869 search_paths.push(parent.join("component.manifest.cbor"));
1870 search_paths.push(parent.join("component.manifest.json"));
1871 }
1872
1873 for path in search_paths {
1874 if path.exists() {
1875 return Ok(Some(load_component_manifest_from_file(&path)?));
1876 }
1877 }
1878
1879 Ok(None)
1880}
1881
1882fn component_manifest_search_paths(pack_dir: &Path, name: &str) -> Vec<PathBuf> {
1883 vec![
1884 pack_dir
1885 .join("components")
1886 .join(format!("{name}.manifest.cbor")),
1887 pack_dir
1888 .join("components")
1889 .join(format!("{name}.manifest.json")),
1890 pack_dir
1891 .join("components")
1892 .join(name)
1893 .join("component.manifest.cbor"),
1894 pack_dir
1895 .join("components")
1896 .join(name)
1897 .join("component.manifest.json"),
1898 ]
1899}
1900
1901fn load_component_manifest_from_file(path: &Path) -> Result<ComponentManifest> {
1902 let bytes = fs::read(path).with_context(|| format!("failed to read {}", path.display()))?;
1903 if path
1904 .extension()
1905 .and_then(|ext| ext.to_str())
1906 .is_some_and(|ext| ext.eq_ignore_ascii_case("cbor"))
1907 {
1908 let manifest = serde_cbor::from_slice(&bytes)
1909 .with_context(|| format!("{} is not valid CBOR", path.display()))?;
1910 return Ok(manifest);
1911 }
1912
1913 let manifest = serde_json::from_slice(&bytes)
1914 .with_context(|| format!("{} is not valid JSON", path.display()))?;
1915 Ok(manifest)
1916}
1917
1918fn component_manifest_file_from_manifest(
1919 manifest: &ComponentManifest,
1920) -> Result<ComponentManifestFile> {
1921 let manifest_bytes = canonical::to_canonical_cbor_allow_floats(manifest)
1922 .context("encode component manifest to canonical cbor")?;
1923 let mut sha = Sha256::new();
1924 sha.update(&manifest_bytes);
1925 let manifest_hash_sha256 = format!("sha256:{}", hex::encode(sha.finalize()));
1926 let manifest_path = format!("components/{}.manifest.cbor", manifest.id.as_str());
1927
1928 Ok(ComponentManifestFile {
1929 component_id: manifest.id.as_str().to_string(),
1930 manifest_path,
1931 manifest_bytes,
1932 manifest_hash_sha256,
1933 })
1934}
1935
1936fn handle_missing_component_manifest(
1937 component_id: &str,
1938 component_name: Option<&str>,
1939 require_component_manifests: bool,
1940) -> Result<()> {
1941 let label = component_name.unwrap_or(component_id);
1942 if require_component_manifests {
1943 anyhow::bail!(
1944 "component manifest metadata missing for {} (supply component.manifest.json or use --require-component-manifests=false)",
1945 label
1946 );
1947 }
1948 eprintln!(
1949 "warning: component manifest metadata missing for {}; pack will emit PACK_COMPONENT_NOT_EXPLICIT",
1950 label
1951 );
1952 Ok(())
1953}
1954
1955fn aggregate_secret_requirements(
1956 components: &[ComponentConfig],
1957 override_path: Option<&Path>,
1958 default_scope: Option<&str>,
1959) -> Result<Vec<SecretRequirement>> {
1960 let default_scope = default_scope.map(parse_default_scope).transpose()?;
1961 let mut merged: BTreeMap<(String, String, String), SecretRequirement> = BTreeMap::new();
1962
1963 let mut process_req = |req: &SecretRequirement, source: &str| -> Result<()> {
1964 let mut req = req.clone();
1965 if req.scope.is_none() {
1966 if let Some(scope) = default_scope.clone() {
1967 req.scope = Some(scope);
1968 tracing::warn!(
1969 key = %secret_key_string(&req),
1970 source,
1971 "secret requirement missing scope; applying default scope"
1972 );
1973 } else {
1974 anyhow::bail!(
1975 "secret requirement {} from {} is missing scope (provide --default-secret-scope or fix the component manifest)",
1976 secret_key_string(&req),
1977 source
1978 );
1979 }
1980 }
1981 let scope = req.scope.as_ref().expect("scope present");
1982 let fmt = fmt_key(&req);
1983 let key_tuple = (req.key.clone().into(), scope_key(scope), fmt.clone());
1984 if let Some(existing) = merged.get_mut(&key_tuple) {
1985 merge_requirement(existing, &req);
1986 } else {
1987 merged.insert(key_tuple, req);
1988 }
1989 Ok(())
1990 };
1991
1992 for component in components {
1993 if let Some(secret_caps) = component.capabilities.host.secrets.as_ref() {
1994 for req in &secret_caps.required {
1995 process_req(req, &component.id)?;
1996 }
1997 }
1998 }
1999
2000 if let Some(path) = override_path {
2001 let contents = fs::read_to_string(path)
2002 .with_context(|| format!("failed to read secrets override {}", path.display()))?;
2003 let value: serde_json::Value = if path
2004 .extension()
2005 .and_then(|ext| ext.to_str())
2006 .map(|ext| ext.eq_ignore_ascii_case("yaml") || ext.eq_ignore_ascii_case("yml"))
2007 .unwrap_or(false)
2008 {
2009 let yaml: YamlValue = serde_yaml_bw::from_str(&contents)
2010 .with_context(|| format!("{} is not valid YAML", path.display()))?;
2011 serde_json::to_value(yaml).context("failed to normalise YAML secrets override")?
2012 } else {
2013 serde_json::from_str(&contents)
2014 .with_context(|| format!("{} is not valid JSON", path.display()))?
2015 };
2016
2017 let overrides: Vec<SecretRequirement> =
2018 serde_json::from_value(value).with_context(|| {
2019 format!(
2020 "{} must be an array of secret requirements (migration bridge)",
2021 path.display()
2022 )
2023 })?;
2024 for req in &overrides {
2025 process_req(req, &format!("override:{}", path.display()))?;
2026 }
2027 }
2028
2029 let mut out: Vec<SecretRequirement> = merged.into_values().collect();
2030 out.sort_by(|a, b| {
2031 let a_scope = a.scope.as_ref().map(scope_key).unwrap_or_default();
2032 let b_scope = b.scope.as_ref().map(scope_key).unwrap_or_default();
2033 (a_scope, secret_key_string(a), fmt_key(a)).cmp(&(
2034 b_scope,
2035 secret_key_string(b),
2036 fmt_key(b),
2037 ))
2038 });
2039 Ok(out)
2040}
2041
2042fn fmt_key(req: &SecretRequirement) -> String {
2043 req.format
2044 .as_ref()
2045 .map(|f| format!("{:?}", f))
2046 .unwrap_or_else(|| "unspecified".to_string())
2047}
2048
2049fn scope_key(scope: &SecretScope) -> String {
2050 format!(
2051 "{}/{}/{}",
2052 &scope.env,
2053 &scope.tenant,
2054 scope
2055 .team
2056 .as_deref()
2057 .map(|t| t.to_string())
2058 .unwrap_or_else(|| "_".to_string())
2059 )
2060}
2061
2062fn secret_key_string(req: &SecretRequirement) -> String {
2063 let key: String = req.key.clone().into();
2064 key
2065}
2066
2067fn merge_requirement(base: &mut SecretRequirement, incoming: &SecretRequirement) {
2068 if base.description.is_none() {
2069 base.description = incoming.description.clone();
2070 }
2071 if let Some(schema) = &incoming.schema {
2072 if base.schema.is_none() {
2073 base.schema = Some(schema.clone());
2074 } else if base.schema.as_ref() != Some(schema) {
2075 tracing::warn!(
2076 key = %secret_key_string(base),
2077 "conflicting secret schema encountered; keeping first"
2078 );
2079 }
2080 }
2081
2082 if !incoming.examples.is_empty() {
2083 for example in &incoming.examples {
2084 if !base.examples.contains(example) {
2085 base.examples.push(example.clone());
2086 }
2087 }
2088 }
2089
2090 base.required = base.required || incoming.required;
2091}
2092
2093fn parse_default_scope(raw: &str) -> Result<SecretScope> {
2094 let parts: Vec<_> = raw.split('/').collect();
2095 if parts.len() < 2 || parts.len() > 3 {
2096 anyhow::bail!(
2097 "default secret scope must be ENV/TENANT or ENV/TENANT/TEAM (got {})",
2098 raw
2099 );
2100 }
2101 Ok(SecretScope {
2102 env: parts[0].to_string(),
2103 tenant: parts[1].to_string(),
2104 team: parts.get(2).map(|s| s.to_string()),
2105 })
2106}
2107
2108fn write_secret_requirements_file(
2109 pack_root: &Path,
2110 requirements: &[SecretRequirement],
2111 logical_name: &str,
2112) -> Result<PathBuf> {
2113 let path = pack_root.join(".packc").join(logical_name);
2114 if let Some(parent) = path.parent() {
2115 fs::create_dir_all(parent)
2116 .with_context(|| format!("failed to create {}", parent.display()))?;
2117 }
2118 let data = serde_json::to_vec_pretty(&requirements)
2119 .context("failed to serialise secret requirements")?;
2120 fs::write(&path, data).with_context(|| format!("failed to write {}", path.display()))?;
2121 Ok(path)
2122}
2123
2124fn resolve_secret_requirements_override(
2125 pack_root: &Path,
2126 override_path: Option<&PathBuf>,
2127) -> Option<PathBuf> {
2128 if let Some(path) = override_path {
2129 return Some(path.clone());
2130 }
2131 find_secret_requirements_file(pack_root)
2132}
2133
2134fn find_secret_requirements_file(pack_root: &Path) -> Option<PathBuf> {
2135 for name in ["secrets_requirements.json", "secret-requirements.json"] {
2136 let candidate = pack_root.join(name);
2137 if candidate.is_file() {
2138 return Some(candidate);
2139 }
2140 }
2141 None
2142}
2143
2144#[cfg(test)]
2145mod tests {
2146 use super::*;
2147 use crate::config::BootstrapConfig;
2148 use crate::runtime::resolve_runtime;
2149 use greentic_pack::pack_lock::{LockedComponent, PackLockV1};
2150 use greentic_types::cbor::canonical;
2151 use greentic_types::decode_pack_manifest;
2152 use greentic_types::flow::FlowKind;
2153 use greentic_types::schemas::common::schema_ir::{AdditionalProperties, SchemaIr};
2154 use greentic_types::schemas::component::v0_6_0::{
2155 ComponentDescribe, ComponentInfo, ComponentOperation, ComponentRunInput,
2156 ComponentRunOutput, schema_hash,
2157 };
2158 use serde_json::json;
2159 use sha2::{Digest, Sha256};
2160 use std::collections::{BTreeMap, BTreeSet};
2161 use std::fs::File;
2162 use std::io::Read;
2163 use std::path::Path;
2164 use std::{fs, path::PathBuf};
2165 use tempfile::tempdir;
2166 use zip::ZipArchive;
2167
2168 fn sample_hex(ch: char) -> String {
2169 std::iter::repeat_n(ch, 64).collect()
2170 }
2171
2172 fn sample_lock_component(
2173 component_id: &str,
2174 reference: Option<&str>,
2175 digest_hex: char,
2176 ) -> LockedComponent {
2177 LockedComponent {
2178 component_id: component_id.to_string(),
2179 r#ref: reference.map(|value| value.to_string()),
2180 abi_version: "0.6.0".to_string(),
2181 resolved_digest: format!("sha256:{}", sample_hex(digest_hex)),
2182 describe_hash: sample_hex(digest_hex),
2183 operations: Vec::new(),
2184 world: None,
2185 component_version: None,
2186 role: None,
2187 }
2188 }
2189
2190 fn write_describe_sidecar(wasm_path: &Path, component_id: &str) {
2191 let input_schema = SchemaIr::String {
2192 min_len: None,
2193 max_len: None,
2194 regex: None,
2195 format: None,
2196 };
2197 let output_schema = SchemaIr::String {
2198 min_len: None,
2199 max_len: None,
2200 regex: None,
2201 format: None,
2202 };
2203 let config_schema = SchemaIr::Object {
2204 properties: BTreeMap::new(),
2205 required: Vec::new(),
2206 additional: AdditionalProperties::Forbid,
2207 };
2208 let hash = schema_hash(&input_schema, &output_schema, &config_schema).expect("schema hash");
2209 let operation = ComponentOperation {
2210 id: "run".to_string(),
2211 display_name: None,
2212 input: ComponentRunInput {
2213 schema: input_schema,
2214 },
2215 output: ComponentRunOutput {
2216 schema: output_schema,
2217 },
2218 defaults: BTreeMap::new(),
2219 redactions: Vec::new(),
2220 constraints: BTreeMap::new(),
2221 schema_hash: hash,
2222 };
2223 let describe = ComponentDescribe {
2224 info: ComponentInfo {
2225 id: component_id.to_string(),
2226 version: "0.1.0".to_string(),
2227 role: "tool".to_string(),
2228 display_name: None,
2229 },
2230 provided_capabilities: Vec::new(),
2231 required_capabilities: Vec::new(),
2232 metadata: BTreeMap::new(),
2233 operations: vec![operation],
2234 config_schema,
2235 };
2236 let bytes = canonical::to_canonical_cbor_allow_floats(&describe).expect("encode describe");
2237 let describe_path = PathBuf::from(format!("{}.describe.cbor", wasm_path.display()));
2238 fs::write(describe_path, bytes).expect("write describe cache");
2239 }
2240
2241 #[test]
2242 fn map_kind_accepts_known_values() {
2243 assert!(matches!(
2244 map_kind("application").unwrap(),
2245 PackKind::Application
2246 ));
2247 assert!(matches!(map_kind("provider").unwrap(), PackKind::Provider));
2248 assert!(matches!(
2249 map_kind("infrastructure").unwrap(),
2250 PackKind::Infrastructure
2251 ));
2252 assert!(matches!(map_kind("library").unwrap(), PackKind::Library));
2253 assert!(map_kind("unknown").is_err());
2254 }
2255
2256 #[test]
2257 fn collect_assets_preserves_relative_paths() {
2258 let root = PathBuf::from("/packs/demo");
2259 let assets = vec![AssetConfig {
2260 path: root.join("assets").join("foo.txt"),
2261 }];
2262 let collected = collect_assets(&assets, &root).expect("collect assets");
2263 assert_eq!(collected[0].logical_path, "assets/foo.txt");
2264 }
2265
2266 fn write_sample_manifest(path: &Path, component_id: &str) {
2267 let manifest: ComponentManifest = serde_json::from_value(json!({
2268 "id": component_id,
2269 "version": "0.1.0",
2270 "supports": [],
2271 "world": "greentic:component/component@0.5.0",
2272 "profiles": { "default": "stateless", "supported": ["stateless"] },
2273 "capabilities": { "wasi": {}, "host": {} },
2274 "operations": [],
2275 "resources": {},
2276 "dev_flows": {}
2277 }))
2278 .expect("manifest");
2279 let bytes = serde_cbor::to_vec(&manifest).expect("encode manifest");
2280 fs::write(path, bytes).expect("write manifest");
2281 }
2282
2283 #[test]
2284 fn load_component_manifest_from_disk_supports_id_specific_files() {
2285 let temp = tempdir().expect("temp dir");
2286 let components = temp.path().join("components");
2287 fs::create_dir_all(&components).expect("create components dir");
2288 let wasm = components.join("component.wasm");
2289 fs::write(&wasm, b"wasm").expect("write wasm");
2290 let manifest_name = components.join("foo.component.manifest.cbor");
2291 write_sample_manifest(&manifest_name, "foo.component");
2292
2293 let manifest =
2294 load_component_manifest_from_disk(&wasm, "foo.component").expect("load manifest");
2295 let manifest = manifest.expect("manifest present");
2296 assert_eq!(manifest.id.to_string(), "foo.component");
2297 }
2298
2299 #[test]
2300 fn load_component_manifest_from_disk_accepts_generic_names() {
2301 let temp = tempdir().expect("temp dir");
2302 let components = temp.path().join("components");
2303 fs::create_dir_all(&components).expect("create components dir");
2304 let wasm = components.join("component.wasm");
2305 fs::write(&wasm, b"wasm").expect("write wasm");
2306 let manifest_name = components.join("component.manifest.cbor");
2307 write_sample_manifest(&manifest_name, "component");
2308
2309 let manifest =
2310 load_component_manifest_from_disk(&wasm, "component").expect("load manifest");
2311 let manifest = manifest.expect("manifest present");
2312 assert_eq!(manifest.id.to_string(), "component");
2313 }
2314
2315 #[test]
2316 fn load_component_manifest_from_disk_walks_up_from_nested_target_paths() {
2317 let temp = tempdir().expect("temp dir");
2318 let component_root = temp.path().join("components/demo-component");
2319 let release_dir = component_root.join("target/wasm32-wasip2/release");
2320 fs::create_dir_all(&release_dir).expect("create release dir");
2321 let wasm = release_dir.join("demo_component.wasm");
2322 fs::write(&wasm, b"wasm").expect("write wasm");
2323 let manifest_name = component_root.join("component.manifest.cbor");
2324 write_sample_manifest(&manifest_name, "dev.local.demo-component");
2325
2326 let manifest = load_component_manifest_from_disk(&wasm, "dev.local.demo-component")
2327 .expect("load manifest");
2328 let manifest = manifest.expect("manifest present");
2329 assert_eq!(manifest.id.to_string(), "dev.local.demo-component");
2330 }
2331
2332 #[test]
2333 fn load_component_manifest_from_disk_does_not_pick_unrelated_parent_manifest() {
2334 let temp = tempdir().expect("temp dir");
2335 let parent_manifest = temp.path().join("component.manifest.cbor");
2336 write_sample_manifest(&parent_manifest, "wrong.parent.component");
2337
2338 let isolated = temp.path().join("isolated");
2339 fs::create_dir_all(&isolated).expect("create isolated dir");
2340 let wasm = isolated.join("component.wasm");
2341 fs::write(&wasm, b"wasm").expect("write wasm");
2342
2343 let manifest =
2344 load_component_manifest_from_disk(&wasm, "expected.component").expect("load manifest");
2345 assert!(
2346 manifest.is_none(),
2347 "must not read unrelated parent manifest"
2348 );
2349 }
2350
2351 #[test]
2352 fn resolve_component_artifacts_requires_manifest_unless_migration_flag_set() {
2353 let temp = tempdir().expect("temp dir");
2354 let wasm = temp.path().join("component.wasm");
2355 fs::write(&wasm, [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]).expect("write wasm");
2356
2357 let cfg: ComponentConfig = serde_json::from_value(json!({
2358 "id": "demo.component",
2359 "version": "0.1.0",
2360 "world": "greentic:component/component@0.6.0",
2361 "supports": [],
2362 "profiles": { "default": "stateless", "supported": ["stateless"] },
2363 "capabilities": { "wasi": {}, "host": {} },
2364 "operations": [],
2365 "wasm": wasm.to_string_lossy()
2366 }))
2367 .expect("component config");
2368
2369 let err = match resolve_component_artifacts(&cfg, false) {
2370 Ok(_) => panic!("missing manifest must fail"),
2371 Err(err) => err,
2372 };
2373 assert!(
2374 err.to_string().contains("missing component.manifest.json"),
2375 "unexpected error: {err}"
2376 );
2377
2378 let (manifest, _binary) =
2379 resolve_component_artifacts(&cfg, true).expect("migration flag allows fallback");
2380 assert_eq!(manifest.id.to_string(), "demo.component");
2381 }
2382
2383 #[test]
2384 fn collect_extra_dir_files_skips_hidden_and_known_dirs() {
2385 let temp = tempdir().expect("temp dir");
2386 let root = temp.path();
2387 fs::create_dir_all(root.join("schemas")).expect("schemas dir");
2388 fs::create_dir_all(root.join("schemas").join(".nested")).expect("nested hidden dir");
2389 fs::create_dir_all(root.join(".hidden")).expect("hidden dir");
2390 fs::create_dir_all(root.join("assets")).expect("assets dir");
2391 fs::write(root.join("README.txt"), b"root").expect("root file");
2392 fs::write(root.join("schemas").join("config.schema.json"), b"{}").expect("schema file");
2393 fs::write(
2394 root.join("schemas").join(".nested").join("skip.json"),
2395 b"{}",
2396 )
2397 .expect("nested hidden file");
2398 fs::write(root.join(".hidden").join("secret.txt"), b"nope").expect("hidden file");
2399 fs::write(root.join("assets").join("asset.txt"), b"nope").expect("asset file");
2400
2401 let collected = collect_extra_dir_files(root).expect("collect extra dirs");
2402 let paths: BTreeSet<_> = collected.iter().map(|e| e.logical_path.as_str()).collect();
2403 assert!(paths.contains("README.txt"));
2404 assert!(paths.contains("schemas/config.schema.json"));
2405 assert!(!paths.contains("schemas/.nested/skip.json"));
2406 assert!(!paths.contains(".hidden/secret.txt"));
2407 assert!(paths.contains("assets/asset.txt"));
2408 }
2409
2410 #[test]
2411 fn collect_extra_dir_files_skips_reserved_sbom_files() {
2412 let temp = tempdir().expect("temp dir");
2413 let root = temp.path();
2414 fs::write(root.join("sbom.cbor"), b"binary").expect("sbom file");
2415 fs::write(root.join("sbom.json"), b"{}").expect("sbom json");
2416 fs::write(root.join("README.md"), b"hello").expect("root file");
2417
2418 let collected = collect_extra_dir_files(root).expect("collect extra dirs");
2419 let paths: BTreeSet<_> = collected.iter().map(|e| e.logical_path.as_str()).collect();
2420 assert!(paths.contains("README.md"));
2421 assert!(!paths.contains("sbom.cbor"));
2422 assert!(!paths.contains("sbom.json"));
2423 }
2424
2425 #[test]
2426 fn build_bootstrap_requires_known_references() {
2427 let config = pack_config_with_bootstrap(BootstrapConfig {
2428 install_flow: Some("flow.a".to_string()),
2429 upgrade_flow: None,
2430 installer_component: Some("component.a".to_string()),
2431 });
2432 let flows = vec![flow_entry("flow.a")];
2433 let components = vec![minimal_component_manifest("component.a")];
2434
2435 let bootstrap = build_bootstrap(&config, &flows, &components)
2436 .expect("bootstrap populated")
2437 .expect("bootstrap present");
2438
2439 assert_eq!(bootstrap.install_flow.as_deref(), Some("flow.a"));
2440 assert_eq!(bootstrap.upgrade_flow, None);
2441 assert_eq!(
2442 bootstrap.installer_component.as_deref(),
2443 Some("component.a")
2444 );
2445 }
2446
2447 #[test]
2448 fn build_bootstrap_rejects_unknown_flow() {
2449 let config = pack_config_with_bootstrap(BootstrapConfig {
2450 install_flow: Some("missing".to_string()),
2451 upgrade_flow: None,
2452 installer_component: Some("component.a".to_string()),
2453 });
2454 let flows = vec![flow_entry("flow.a")];
2455 let components = vec![minimal_component_manifest("component.a")];
2456
2457 let err = build_bootstrap(&config, &flows, &components).unwrap_err();
2458 assert!(
2459 err.to_string()
2460 .contains("bootstrap.install_flow references unknown flow"),
2461 "unexpected error: {err}"
2462 );
2463 }
2464
2465 #[test]
2466 fn component_manifest_without_dev_flows_defaults_to_empty() {
2467 let manifest: ComponentManifest = serde_json::from_value(json!({
2468 "id": "component.dev",
2469 "version": "1.0.0",
2470 "supports": ["messaging"],
2471 "world": "greentic:demo@1.0.0",
2472 "profiles": { "default": "default", "supported": ["default"] },
2473 "capabilities": { "wasi": {}, "host": {} },
2474 "operations": [],
2475 "resources": {}
2476 }))
2477 .expect("manifest without dev_flows");
2478
2479 assert!(manifest.dev_flows.is_empty());
2480
2481 let pack_manifest = pack_manifest_with_component(manifest.clone());
2482 let encoded = encode_pack_manifest(&pack_manifest).expect("encode manifest");
2483 let decoded: PackManifest =
2484 greentic_types::decode_pack_manifest(&encoded).expect("decode manifest");
2485 let stored = decoded
2486 .components
2487 .iter()
2488 .find(|item| item.id == manifest.id)
2489 .expect("component present");
2490 assert!(stored.dev_flows.is_empty());
2491 }
2492
2493 #[test]
2494 fn dev_flows_round_trip_in_manifest_and_gtpack() {
2495 let component = manifest_with_dev_flow();
2496 let pack_manifest = pack_manifest_with_component(component.clone());
2497 let manifest_bytes = encode_pack_manifest(&pack_manifest).expect("encode manifest");
2498
2499 let decoded: PackManifest =
2500 greentic_types::decode_pack_manifest(&manifest_bytes).expect("decode manifest");
2501 let decoded_component = decoded
2502 .components
2503 .iter()
2504 .find(|item| item.id == component.id)
2505 .expect("component present");
2506 assert_eq!(decoded_component.dev_flows, component.dev_flows);
2507
2508 let temp = tempdir().expect("temp dir");
2509 let wasm_path = temp.path().join("component.wasm");
2510 write_stub_wasm(&wasm_path).expect("write stub wasm");
2511
2512 let build = BuildProducts {
2513 manifest: pack_manifest,
2514 components: vec![ComponentBinary {
2515 id: component.id.to_string(),
2516 source: wasm_path,
2517 manifest_bytes: serde_cbor::to_vec(&component).expect("component cbor"),
2518 manifest_path: format!("components/{}.manifest.cbor", component.id),
2519 manifest_hash_sha256: {
2520 let mut sha = Sha256::new();
2521 sha.update(serde_cbor::to_vec(&component).expect("component cbor"));
2522 format!("sha256:{}", hex::encode(sha.finalize()))
2523 },
2524 }],
2525 lock_components: Vec::new(),
2526 component_manifest_files: Vec::new(),
2527 flow_files: Vec::new(),
2528 assets: Vec::new(),
2529 extra_files: Vec::new(),
2530 };
2531
2532 let out = temp.path().join("demo.gtpack");
2533 let warnings = package_gtpack(&out, &manifest_bytes, &build, BundleMode::Cache, false)
2534 .expect("package gtpack");
2535 assert!(warnings.is_empty(), "expected no packaging warnings");
2536
2537 let mut archive = ZipArchive::new(fs::File::open(&out).expect("open gtpack"))
2538 .expect("read gtpack archive");
2539 let mut manifest_entry = archive.by_name("manifest.cbor").expect("manifest.cbor");
2540 let mut stored = Vec::new();
2541 manifest_entry
2542 .read_to_end(&mut stored)
2543 .expect("read manifest");
2544 let decoded: PackManifest =
2545 greentic_types::decode_pack_manifest(&stored).expect("decode packaged manifest");
2546
2547 let stored_component = decoded
2548 .components
2549 .iter()
2550 .find(|item| item.id == component.id)
2551 .expect("component preserved");
2552 assert_eq!(stored_component.dev_flows, component.dev_flows);
2553 }
2554
2555 #[test]
2556 fn prod_gtpack_excludes_forbidden_files() {
2557 let component = manifest_with_dev_flow();
2558 let pack_manifest = pack_manifest_with_component(component.clone());
2559 let manifest_bytes = encode_pack_manifest(&pack_manifest).expect("encode manifest");
2560
2561 let temp = tempdir().expect("temp dir");
2562 let wasm_path = temp.path().join("component.wasm");
2563 write_stub_wasm(&wasm_path).expect("write stub wasm");
2564
2565 let pack_yaml = temp.path().join("pack.yaml");
2566 fs::write(&pack_yaml, "pack").expect("write pack.yaml");
2567 let pack_manifest_json = temp.path().join("pack.manifest.json");
2568 fs::write(&pack_manifest_json, "{}").expect("write manifest json");
2569
2570 let build = BuildProducts {
2571 manifest: pack_manifest,
2572 components: vec![ComponentBinary {
2573 id: component.id.to_string(),
2574 source: wasm_path,
2575 manifest_bytes: serde_cbor::to_vec(&component).expect("component cbor"),
2576 manifest_path: format!("components/{}.manifest.cbor", component.id),
2577 manifest_hash_sha256: {
2578 let mut sha = Sha256::new();
2579 sha.update(serde_cbor::to_vec(&component).expect("component cbor"));
2580 format!("sha256:{}", hex::encode(sha.finalize()))
2581 },
2582 }],
2583 lock_components: Vec::new(),
2584 component_manifest_files: Vec::new(),
2585 flow_files: Vec::new(),
2586 assets: Vec::new(),
2587 extra_files: vec![
2588 ExtraFile {
2589 logical_path: "pack.yaml".to_string(),
2590 source: pack_yaml,
2591 },
2592 ExtraFile {
2593 logical_path: "pack.manifest.json".to_string(),
2594 source: pack_manifest_json,
2595 },
2596 ],
2597 };
2598
2599 let out = temp.path().join("prod.gtpack");
2600 let warnings = package_gtpack(&out, &manifest_bytes, &build, BundleMode::Cache, false)
2601 .expect("package gtpack");
2602 assert!(
2603 warnings.is_empty(),
2604 "no warnings expected for forbidden drop"
2605 );
2606
2607 let mut archive = ZipArchive::new(fs::File::open(&out).expect("open gtpack"))
2608 .expect("read gtpack archive");
2609 assert!(archive.by_name("pack.yaml").is_err());
2610 assert!(archive.by_name("pack.manifest.json").is_err());
2611 }
2612
2613 #[test]
2614 fn asset_mapping_prefers_assets_version_on_conflict() {
2615 let component = manifest_with_dev_flow();
2616 let pack_manifest = pack_manifest_with_component(component.clone());
2617 let manifest_bytes = encode_pack_manifest(&pack_manifest).expect("encode manifest");
2618
2619 let temp = tempdir().expect("temp dir");
2620 let wasm_path = temp.path().join("component.wasm");
2621 write_stub_wasm(&wasm_path).expect("write stub wasm");
2622
2623 let assets_dir = temp.path().join("assets");
2624 fs::create_dir_all(&assets_dir).expect("create assets dir");
2625 let asset_file = assets_dir.join("README.md");
2626 fs::write(&asset_file, "asset").expect("write asset");
2627 let root_asset = temp.path().join("README.md");
2628 fs::write(&root_asset, "root").expect("write root file");
2629
2630 let build = BuildProducts {
2631 manifest: pack_manifest,
2632 components: vec![ComponentBinary {
2633 id: component.id.to_string(),
2634 source: wasm_path,
2635 manifest_bytes: serde_cbor::to_vec(&component).expect("component cbor"),
2636 manifest_path: format!("components/{}.manifest.cbor", component.id),
2637 manifest_hash_sha256: {
2638 let mut sha = Sha256::new();
2639 sha.update(serde_cbor::to_vec(&component).expect("component cbor"));
2640 format!("sha256:{}", hex::encode(sha.finalize()))
2641 },
2642 }],
2643 lock_components: Vec::new(),
2644 component_manifest_files: Vec::new(),
2645 flow_files: Vec::new(),
2646 assets: Vec::new(),
2647 extra_files: vec![
2648 ExtraFile {
2649 logical_path: "assets/README.md".to_string(),
2650 source: asset_file,
2651 },
2652 ExtraFile {
2653 logical_path: "README.md".to_string(),
2654 source: root_asset,
2655 },
2656 ],
2657 };
2658
2659 let out = temp.path().join("conflict.gtpack");
2660 let warnings = package_gtpack(&out, &manifest_bytes, &build, BundleMode::Cache, false)
2661 .expect("package gtpack");
2662 assert!(
2663 warnings
2664 .iter()
2665 .any(|w| w.contains("skipping root asset README.md"))
2666 );
2667
2668 let mut archive = ZipArchive::new(fs::File::open(&out).expect("open gtpack"))
2669 .expect("read gtpack archive");
2670 assert!(archive.by_name("README.md").is_err());
2671 assert!(archive.by_name("assets/README.md").is_ok());
2672 }
2673
2674 #[test]
2675 fn root_files_map_under_assets_directory() {
2676 let component = manifest_with_dev_flow();
2677 let pack_manifest = pack_manifest_with_component(component.clone());
2678 let manifest_bytes = encode_pack_manifest(&pack_manifest).expect("encode manifest");
2679
2680 let temp = tempdir().expect("temp dir");
2681 let wasm_path = temp.path().join("component.wasm");
2682 write_stub_wasm(&wasm_path).expect("write stub wasm");
2683 let root_asset = temp.path().join("notes.txt");
2684 fs::write(&root_asset, "notes").expect("write root asset");
2685
2686 let build = BuildProducts {
2687 manifest: pack_manifest,
2688 components: vec![ComponentBinary {
2689 id: component.id.to_string(),
2690 source: wasm_path,
2691 manifest_bytes: serde_cbor::to_vec(&component).expect("component cbor"),
2692 manifest_path: format!("components/{}.manifest.cbor", component.id),
2693 manifest_hash_sha256: {
2694 let mut sha = Sha256::new();
2695 sha.update(serde_cbor::to_vec(&component).expect("component cbor"));
2696 format!("sha256:{}", hex::encode(sha.finalize()))
2697 },
2698 }],
2699 lock_components: Vec::new(),
2700 component_manifest_files: Vec::new(),
2701 flow_files: Vec::new(),
2702 assets: Vec::new(),
2703 extra_files: vec![ExtraFile {
2704 logical_path: "notes.txt".to_string(),
2705 source: root_asset,
2706 }],
2707 };
2708
2709 let out = temp.path().join("root-assets.gtpack");
2710 let warnings = package_gtpack(&out, &manifest_bytes, &build, BundleMode::Cache, false)
2711 .expect("package gtpack");
2712 assert!(
2713 warnings.iter().all(|w| !w.contains("notes.txt")),
2714 "root asset mapping should not warn without conflict"
2715 );
2716
2717 let mut archive = ZipArchive::new(fs::File::open(&out).expect("open gtpack"))
2718 .expect("read gtpack archive");
2719 assert!(archive.by_name("assets/notes.txt").is_ok());
2720 assert!(archive.by_name("notes.txt").is_err());
2721 }
2722
2723 #[test]
2724 fn prod_gtpack_embeds_secret_requirements_cbor_only() {
2725 let component = manifest_with_dev_flow();
2726 let mut pack_manifest = pack_manifest_with_component(component.clone());
2727 let secret_requirement: SecretRequirement = serde_json::from_value(json!({
2728 "key": "demo/token",
2729 "required": true,
2730 "description": "demo secret",
2731 "scope": { "env": "dev", "tenant": "demo" }
2732 }))
2733 .expect("parse secret requirement");
2734 pack_manifest.secret_requirements = vec![secret_requirement.clone()];
2735 let manifest_bytes = encode_pack_manifest(&pack_manifest).expect("encode manifest");
2736
2737 let temp = tempdir().expect("temp dir");
2738 let wasm_path = temp.path().join("component.wasm");
2739 write_stub_wasm(&wasm_path).expect("write stub wasm");
2740 let secret_file = temp.path().join("secret-requirements.json");
2741 fs::write(&secret_file, "[{}]").expect("write secret json");
2742
2743 let build = BuildProducts {
2744 manifest: pack_manifest,
2745 components: vec![ComponentBinary {
2746 id: component.id.to_string(),
2747 source: wasm_path,
2748 manifest_bytes: serde_cbor::to_vec(&component).expect("component cbor"),
2749 manifest_path: format!("components/{}.manifest.cbor", component.id),
2750 manifest_hash_sha256: {
2751 let mut sha = Sha256::new();
2752 sha.update(serde_cbor::to_vec(&component).expect("component cbor"));
2753 format!("sha256:{}", hex::encode(sha.finalize()))
2754 },
2755 }],
2756 lock_components: Vec::new(),
2757 component_manifest_files: Vec::new(),
2758 flow_files: Vec::new(),
2759 assets: Vec::new(),
2760 extra_files: vec![ExtraFile {
2761 logical_path: "secret-requirements.json".to_string(),
2762 source: secret_file,
2763 }],
2764 };
2765
2766 let out = temp.path().join("secrets.gtpack");
2767 package_gtpack(&out, &manifest_bytes, &build, BundleMode::Cache, false)
2768 .expect("package gtpack");
2769
2770 let mut archive = ZipArchive::new(fs::File::open(&out).expect("open gtpack"))
2771 .expect("read gtpack archive");
2772 assert!(archive.by_name("secret-requirements.json").is_err());
2773 assert!(archive.by_name("assets/secret-requirements.json").is_err());
2774 assert!(archive.by_name("secrets_requirements.json").is_err());
2775 assert!(archive.by_name("assets/secrets_requirements.json").is_err());
2776
2777 let mut manifest_entry = archive
2778 .by_name("manifest.cbor")
2779 .expect("manifest.cbor present");
2780 let mut manifest_buf = Vec::new();
2781 manifest_entry
2782 .read_to_end(&mut manifest_buf)
2783 .expect("read manifest bytes");
2784 let decoded = decode_pack_manifest(&manifest_buf).expect("decode manifest");
2785 assert_eq!(decoded.secret_requirements, vec![secret_requirement]);
2786 }
2787
2788 #[test]
2789 fn component_sources_extension_respects_bundle() {
2790 let mut components = BTreeMap::new();
2791 components.insert(
2792 "demo.tagged".to_string(),
2793 sample_lock_component(
2794 "demo.tagged",
2795 Some("oci://ghcr.io/demo/component:1.0.0"),
2796 'a',
2797 ),
2798 );
2799 let lock_tag = PackLockV1::new(components);
2800
2801 let mut bundled_paths = BTreeMap::new();
2802 bundled_paths.insert(
2803 "demo.tagged".to_string(),
2804 "blobs/sha256/deadbeef.wasm".to_string(),
2805 );
2806
2807 let ext_none =
2808 merge_component_sources_extension(None, &lock_tag, &bundled_paths, None).expect("ext");
2809 let value = match ext_none
2810 .unwrap()
2811 .get(EXT_COMPONENT_SOURCES_V1)
2812 .and_then(|e| e.inline.as_ref())
2813 {
2814 Some(ExtensionInline::Other(v)) => v.clone(),
2815 _ => panic!("missing inline"),
2816 };
2817 let decoded = ComponentSourcesV1::from_extension_value(&value).expect("decode");
2818 assert!(matches!(
2819 decoded.components[0].artifact,
2820 ArtifactLocationV1::Inline { .. }
2821 ));
2822
2823 let mut components = BTreeMap::new();
2824 components.insert(
2825 "demo.component".to_string(),
2826 sample_lock_component(
2827 "demo.component",
2828 Some("oci://ghcr.io/demo/component@sha256:deadbeef"),
2829 'b',
2830 ),
2831 );
2832 let lock_digest = PackLockV1::new(components);
2833
2834 let ext_none =
2835 merge_component_sources_extension(None, &lock_digest, &BTreeMap::new(), None)
2836 .expect("ext");
2837 let value = match ext_none
2838 .unwrap()
2839 .get(EXT_COMPONENT_SOURCES_V1)
2840 .and_then(|e| e.inline.as_ref())
2841 {
2842 Some(ExtensionInline::Other(v)) => v.clone(),
2843 _ => panic!("missing inline"),
2844 };
2845 let decoded = ComponentSourcesV1::from_extension_value(&value).expect("decode");
2846 assert!(matches!(
2847 decoded.components[0].artifact,
2848 ArtifactLocationV1::Remote
2849 ));
2850
2851 let mut components = BTreeMap::new();
2852 components.insert(
2853 "demo.component".to_string(),
2854 sample_lock_component(
2855 "demo.component",
2856 Some("oci://ghcr.io/demo/component@sha256:deadbeef"),
2857 'c',
2858 ),
2859 );
2860 let lock_digest_bundled = PackLockV1::new(components);
2861
2862 let mut bundled_paths = BTreeMap::new();
2863 bundled_paths.insert(
2864 "demo.component".to_string(),
2865 "components/demo.component.wasm".to_string(),
2866 );
2867
2868 let ext_cache =
2869 merge_component_sources_extension(None, &lock_digest_bundled, &bundled_paths, None)
2870 .expect("ext");
2871 let value = match ext_cache
2872 .unwrap()
2873 .get(EXT_COMPONENT_SOURCES_V1)
2874 .and_then(|e| e.inline.as_ref())
2875 {
2876 Some(ExtensionInline::Other(v)) => v.clone(),
2877 _ => panic!("missing inline"),
2878 };
2879 let decoded = ComponentSourcesV1::from_extension_value(&value).expect("decode");
2880 assert!(matches!(
2881 decoded.components[0].artifact,
2882 ArtifactLocationV1::Inline { .. }
2883 ));
2884 }
2885
2886 #[test]
2887 fn component_sources_extension_skips_file_refs() {
2888 let mut components = BTreeMap::new();
2889 components.insert(
2890 "local.component".to_string(),
2891 sample_lock_component("local.component", Some("file:///tmp/component.wasm"), 'd'),
2892 );
2893 let lock = PackLockV1::new(components);
2894
2895 let ext_none =
2896 merge_component_sources_extension(None, &lock, &BTreeMap::new(), None).expect("ext");
2897 assert!(ext_none.is_none(), "file refs should be omitted");
2898
2899 let mut components = BTreeMap::new();
2900 components.insert(
2901 "local.component".to_string(),
2902 sample_lock_component("local.component", Some("file:///tmp/component.wasm"), 'e'),
2903 );
2904 components.insert(
2905 "remote.component".to_string(),
2906 sample_lock_component(
2907 "remote.component",
2908 Some("oci://ghcr.io/demo/component:2.0.0"),
2909 'f',
2910 ),
2911 );
2912 let lock = PackLockV1::new(components);
2913
2914 let ext_some =
2915 merge_component_sources_extension(None, &lock, &BTreeMap::new(), None).expect("ext");
2916 let value = match ext_some
2917 .unwrap()
2918 .get(EXT_COMPONENT_SOURCES_V1)
2919 .and_then(|e| e.inline.as_ref())
2920 {
2921 Some(ExtensionInline::Other(v)) => v.clone(),
2922 _ => panic!("missing inline"),
2923 };
2924 let decoded = ComponentSourcesV1::from_extension_value(&value).expect("decode");
2925 assert_eq!(decoded.components.len(), 1);
2926 assert!(matches!(
2927 decoded.components[0].source,
2928 ComponentSourceRef::Oci(_)
2929 ));
2930 }
2931
2932 #[test]
2933 fn build_embeds_lock_components_from_cache() {
2934 let rt = tokio::runtime::Runtime::new().expect("runtime");
2935 rt.block_on(async {
2936 let temp = tempdir().expect("temp dir");
2937 let pack_dir = temp.path().join("pack");
2938 fs::create_dir_all(pack_dir.join("flows")).expect("flows dir");
2939 fs::create_dir_all(pack_dir.join("components")).expect("components dir");
2940
2941 let wasm_path = pack_dir.join("components/dummy.wasm");
2942 fs::write(&wasm_path, [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00])
2943 .expect("write wasm");
2944
2945 let flow_path = pack_dir.join("flows/main.ygtc");
2946 fs::write(
2947 &flow_path,
2948 r#"id: main
2949type: messaging
2950start: call
2951nodes:
2952 call:
2953 handle_message:
2954 text: "hi"
2955 routing: out
2956"#,
2957 )
2958 .expect("write flow");
2959
2960 let cache_dir = temp.path().join("cache");
2961 let cached_bytes = b"cached-component";
2962 let seed_path = temp.path().join("cached-component.wasm");
2963 fs::write(&seed_path, cached_bytes).expect("write seed");
2964 let dist = DistClient::new(DistOptions {
2965 cache_dir: cache_dir.clone(),
2966 allow_tags: true,
2967 offline: false,
2968 allow_insecure_local_http: false,
2969 ..DistOptions::default()
2970 });
2971 let source = dist
2972 .parse_source(&format!("file://{}", seed_path.display()))
2973 .expect("parse source");
2974 let descriptor = dist
2975 .resolve(source, greentic_distributor_client::ResolvePolicy)
2976 .await
2977 .expect("resolve source");
2978 let cached = dist
2979 .fetch(&descriptor, greentic_distributor_client::CachePolicy)
2980 .await
2981 .expect("seed cache");
2982 let digest = cached.descriptor.digest.clone();
2983 let cache_path = cached.cache_path.expect("cache path");
2984 write_describe_sidecar(&cache_path, "dummy.component");
2985
2986 let summary = serde_json::json!({
2987 "schema_version": 1,
2988 "flow": "main.ygtc",
2989 "nodes": {
2990 "call": {
2991 "component_id": "dummy.component",
2992 "source": {
2993 "kind": "oci",
2994 "ref": format!("oci://ghcr.io/demo/component@{digest}")
2995 },
2996 "digest": digest
2997 }
2998 }
2999 });
3000 fs::write(
3001 flow_path.with_extension("ygtc.resolve.summary.json"),
3002 serde_json::to_vec_pretty(&summary).expect("summary json"),
3003 )
3004 .expect("write summary");
3005
3006 let pack_yaml = r#"pack_id: demo.lock-bundle
3007version: 0.1.0
3008kind: application
3009publisher: Test
3010components:
3011 - id: dummy.component
3012 version: "0.1.0"
3013 world: "greentic:component/component@0.5.0"
3014 supports: ["messaging"]
3015 profiles:
3016 default: "stateless"
3017 supported: ["stateless"]
3018 capabilities:
3019 wasi: {}
3020 host: {}
3021 operations:
3022 - name: "handle_message"
3023 input_schema: {}
3024 output_schema: {}
3025 wasm: "components/dummy.wasm"
3026flows:
3027 - id: main
3028 file: flows/main.ygtc
3029 tags: [default]
3030 entrypoints: [main]
3031"#;
3032 fs::write(pack_dir.join("pack.yaml"), pack_yaml).expect("pack.yaml");
3033
3034 let runtime = crate::runtime::resolve_runtime(
3035 Some(pack_dir.as_path()),
3036 Some(cache_dir.as_path()),
3037 true,
3038 None,
3039 )
3040 .expect("runtime");
3041
3042 let opts = BuildOptions {
3043 pack_dir: pack_dir.clone(),
3044 component_out: None,
3045 manifest_out: pack_dir.join("dist/manifest.cbor"),
3046 sbom_out: None,
3047 gtpack_out: Some(pack_dir.join("dist/pack.gtpack")),
3048 lock_path: pack_dir.join("pack.lock.cbor"),
3049 bundle: BundleMode::Cache,
3050 dry_run: false,
3051 secrets_req: None,
3052 default_secret_scope: None,
3053 allow_oci_tags: false,
3054 require_component_manifests: false,
3055 no_extra_dirs: false,
3056 dev: false,
3057 runtime,
3058 skip_update: false,
3059 allow_pack_schema: true,
3060 validate_extension_refs: true,
3061 };
3062
3063 run(&opts).await.expect("build");
3064
3065 let gtpack_path = opts.gtpack_out.expect("gtpack path");
3066 let mut archive = ZipArchive::new(File::open(>pack_path).expect("open gtpack"))
3067 .expect("read gtpack");
3068 assert!(
3069 archive.by_name("components/dummy.component.wasm").is_ok(),
3070 "missing lock component artifact in gtpack"
3071 );
3072 });
3073 }
3074
3075 #[test]
3076 #[ignore = "requires network access to fetch OCI component"]
3077 fn build_fetches_and_embeds_lock_components_online() {
3078 if std::env::var("GREENTIC_PACK_ONLINE").is_err() {
3079 return;
3080 }
3081 let rt = tokio::runtime::Runtime::new().expect("runtime");
3082 rt.block_on(async {
3083 let temp = tempdir().expect("temp dir");
3084 let pack_dir = temp.path().join("pack");
3085 fs::create_dir_all(pack_dir.join("flows")).expect("flows dir");
3086 fs::create_dir_all(pack_dir.join("components")).expect("components dir");
3087
3088 let wasm_path = pack_dir.join("components/dummy.wasm");
3089 fs::write(&wasm_path, [0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00])
3090 .expect("write wasm");
3091
3092 let flow_path = pack_dir.join("flows/main.ygtc");
3093 fs::write(
3094 &flow_path,
3095 r#"id: main
3096type: messaging
3097start: call
3098nodes:
3099 call:
3100 handle_message:
3101 text: "hi"
3102 routing: out
3103"#,
3104 )
3105 .expect("write flow");
3106
3107 let digest = "sha256:0904bee6ecd737506265e3f38f3e4fe6b185c20fd1b0e7c06ce03cdeedc00340";
3108 let summary = serde_json::json!({
3109 "schema_version": 1,
3110 "flow": "main.ygtc",
3111 "nodes": {
3112 "call": {
3113 "component_id": "dummy.component",
3114 "source": {
3115 "kind": "oci",
3116 "ref": format!("oci://ghcr.io/greenticai/components/templates@{digest}")
3117 },
3118 "digest": digest
3119 }
3120 }
3121 });
3122 fs::write(
3123 flow_path.with_extension("ygtc.resolve.summary.json"),
3124 serde_json::to_vec_pretty(&summary).expect("summary json"),
3125 )
3126 .expect("write summary");
3127
3128 let pack_yaml = r#"pack_id: demo.lock-online
3129version: 0.1.0
3130kind: application
3131publisher: Test
3132components:
3133 - id: dummy.component
3134 version: "0.1.0"
3135 world: "greentic:component/component@0.5.0"
3136 supports: ["messaging"]
3137 profiles:
3138 default: "stateless"
3139 supported: ["stateless"]
3140 capabilities:
3141 wasi: {}
3142 host: {}
3143 operations:
3144 - name: "handle_message"
3145 input_schema: {}
3146 output_schema: {}
3147 wasm: "components/dummy.wasm"
3148flows:
3149 - id: main
3150 file: flows/main.ygtc
3151 tags: [default]
3152 entrypoints: [main]
3153"#;
3154 fs::write(pack_dir.join("pack.yaml"), pack_yaml).expect("pack.yaml");
3155
3156 let cache_dir = temp.path().join("cache");
3157 let runtime = crate::runtime::resolve_runtime(
3158 Some(pack_dir.as_path()),
3159 Some(cache_dir.as_path()),
3160 false,
3161 None,
3162 )
3163 .expect("runtime");
3164
3165 let opts = BuildOptions {
3166 pack_dir: pack_dir.clone(),
3167 component_out: None,
3168 manifest_out: pack_dir.join("dist/manifest.cbor"),
3169 sbom_out: None,
3170 gtpack_out: Some(pack_dir.join("dist/pack.gtpack")),
3171 lock_path: pack_dir.join("pack.lock.cbor"),
3172 bundle: BundleMode::Cache,
3173 dry_run: false,
3174 secrets_req: None,
3175 default_secret_scope: None,
3176 allow_oci_tags: false,
3177 require_component_manifests: false,
3178 no_extra_dirs: false,
3179 dev: false,
3180 runtime,
3181 skip_update: false,
3182 allow_pack_schema: true,
3183 validate_extension_refs: true,
3184 };
3185
3186 run(&opts).await.expect("build");
3187
3188 let gtpack_path = opts.gtpack_out.expect("gtpack path");
3189 let mut archive = ZipArchive::new(File::open(>pack_path).expect("open gtpack"))
3190 .expect("read gtpack");
3191 assert!(
3192 archive.by_name("components/dummy.component.wasm").is_ok(),
3193 "missing lock component artifact in gtpack"
3194 );
3195 });
3196 }
3197
3198 #[test]
3199 fn aggregate_secret_requirements_dedupes_and_sorts() {
3200 let component: ComponentConfig = serde_json::from_value(json!({
3201 "id": "component.a",
3202 "version": "1.0.0",
3203 "world": "greentic:demo@1.0.0",
3204 "supports": [],
3205 "profiles": { "default": "default", "supported": ["default"] },
3206 "capabilities": {
3207 "wasi": {},
3208 "host": {
3209 "secrets": {
3210 "required": [
3211 {
3212 "key": "db/password",
3213 "required": true,
3214 "scope": { "env": "dev", "tenant": "t1" },
3215 "format": "text",
3216 "description": "primary"
3217 }
3218 ]
3219 }
3220 }
3221 },
3222 "wasm": "component.wasm",
3223 "operations": [],
3224 "resources": {}
3225 }))
3226 .expect("component config");
3227
3228 let dupe: ComponentConfig = serde_json::from_value(json!({
3229 "id": "component.b",
3230 "version": "1.0.0",
3231 "world": "greentic:demo@1.0.0",
3232 "supports": [],
3233 "profiles": { "default": "default", "supported": ["default"] },
3234 "capabilities": {
3235 "wasi": {},
3236 "host": {
3237 "secrets": {
3238 "required": [
3239 {
3240 "key": "db/password",
3241 "required": true,
3242 "scope": { "env": "dev", "tenant": "t1" },
3243 "format": "text",
3244 "description": "secondary",
3245 "examples": ["example"]
3246 }
3247 ]
3248 }
3249 }
3250 },
3251 "wasm": "component.wasm",
3252 "operations": [],
3253 "resources": {}
3254 }))
3255 .expect("component config");
3256
3257 let reqs = aggregate_secret_requirements(&[component, dupe], None, None)
3258 .expect("aggregate secrets");
3259 assert_eq!(reqs.len(), 1);
3260 let req = &reqs[0];
3261 assert_eq!(req.description.as_deref(), Some("primary"));
3262 assert!(req.examples.contains(&"example".to_string()));
3263 }
3264
3265 fn pack_config_with_bootstrap(bootstrap: BootstrapConfig) -> PackConfig {
3266 PackConfig {
3267 pack_id: "demo.pack".to_string(),
3268 version: "1.0.0".to_string(),
3269 kind: "application".to_string(),
3270 publisher: "demo".to_string(),
3271 name: None,
3272 bootstrap: Some(bootstrap),
3273 components: Vec::new(),
3274 dependencies: Vec::new(),
3275 flows: Vec::new(),
3276 assets: Vec::new(),
3277 extensions: None,
3278 }
3279 }
3280
3281 fn flow_entry(id: &str) -> PackFlowEntry {
3282 let flow: Flow = serde_json::from_value(json!({
3283 "schema_version": "flow/v1",
3284 "id": id,
3285 "kind": "messaging"
3286 }))
3287 .expect("flow json");
3288
3289 PackFlowEntry {
3290 id: FlowId::new(id).expect("flow id"),
3291 kind: FlowKind::Messaging,
3292 flow,
3293 tags: Vec::new(),
3294 entrypoints: Vec::new(),
3295 }
3296 }
3297
3298 fn minimal_component_manifest(id: &str) -> ComponentManifest {
3299 serde_json::from_value(json!({
3300 "id": id,
3301 "version": "1.0.0",
3302 "supports": [],
3303 "world": "greentic:demo@1.0.0",
3304 "profiles": { "default": "default", "supported": ["default"] },
3305 "capabilities": { "wasi": {}, "host": {} },
3306 "operations": [],
3307 "resources": {}
3308 }))
3309 .expect("component manifest")
3310 }
3311
3312 fn manifest_with_dev_flow() -> ComponentManifest {
3313 serde_json::from_str(include_str!(
3314 "../tests/fixtures/component_manifest_with_dev_flows.json"
3315 ))
3316 .expect("fixture manifest")
3317 }
3318
3319 fn pack_manifest_with_component(component: ComponentManifest) -> PackManifest {
3320 let flow = serde_json::from_value(json!({
3321 "schema_version": "flow/v1",
3322 "id": "flow.dev",
3323 "kind": "messaging"
3324 }))
3325 .expect("flow json");
3326
3327 PackManifest {
3328 schema_version: "pack-v1".to_string(),
3329 pack_id: PackId::new("demo.pack").expect("pack id"),
3330 name: None,
3331 version: Version::parse("1.0.0").expect("version"),
3332 kind: PackKind::Application,
3333 publisher: "demo".to_string(),
3334 components: vec![component],
3335 flows: vec![PackFlowEntry {
3336 id: FlowId::new("flow.dev").expect("flow id"),
3337 kind: FlowKind::Messaging,
3338 flow,
3339 tags: Vec::new(),
3340 entrypoints: Vec::new(),
3341 }],
3342 dependencies: Vec::new(),
3343 capabilities: Vec::new(),
3344 secret_requirements: Vec::new(),
3345 signatures: PackSignatures::default(),
3346 bootstrap: None,
3347 extensions: None,
3348 }
3349 }
3350
3351 #[tokio::test]
3352 async fn offline_build_requires_cached_remote_component() {
3353 let temp = tempdir().expect("temp dir");
3354 let cache_dir = temp.path().join("cache");
3355 fs::create_dir_all(&cache_dir).expect("create cache dir");
3356 let project_root = Path::new(env!("CARGO_MANIFEST_DIR"))
3357 .parent()
3358 .expect("workspace root");
3359 let runtime = resolve_runtime(Some(project_root), Some(cache_dir.as_path()), true, None)
3360 .expect("resolve runtime");
3361
3362 let mut components = BTreeMap::new();
3363 components.insert(
3364 "remote.component".to_string(),
3365 LockedComponent {
3366 component_id: "remote.component".to_string(),
3367 r#ref: Some("oci://example/remote@sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef".to_string()),
3368 abi_version: "0.6.0".to_string(),
3369 resolved_digest: "sha256:0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
3370 .to_string(),
3371 describe_hash: sample_hex('a'),
3372 operations: Vec::new(),
3373 world: None,
3374 component_version: None,
3375 role: None,
3376 },
3377 );
3378 let lock = PackLockV1::new(components);
3379
3380 let err = match collect_lock_component_artifacts(&lock, &runtime, BundleMode::Cache, false)
3381 .await
3382 {
3383 Ok(_) => panic!("expected offline build to fail without cached component"),
3384 Err(err) => err,
3385 };
3386 let msg = err.to_string();
3387 assert!(
3388 msg.contains("requires network access"),
3389 "error message should describe missing network access, got {}",
3390 msg
3391 );
3392 }
3393}