greentic-setup 0.6.0-dev.25310893036

End-to-end bundle setup engine for the Greentic platform — pack discovery, QA-driven configuration, secrets persistence, and bundle lifecycle management
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
//! Bundle source resolution — parse and resolve bundle references from various protocols.
//!
//! Supports local paths, file:// URIs, and remote protocols via greentic-distributor-client.

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

use anyhow::{Context, anyhow};

/// A bundle source that can be resolved to a local artifact path.
#[derive(Clone, Debug)]
pub enum BundleSource {
    /// Local directory path (absolute or relative).
    LocalDir(PathBuf),
    /// file:// URI pointing to a local path.
    FileUri(PathBuf),
    /// oci://registry/repo:tag — OCI registry reference.
    #[cfg(feature = "oci")]
    Oci { reference: String },
    /// repo://org/name — Pack repository reference (maps to OCI).
    #[cfg(feature = "oci")]
    Repo { reference: String },
    /// store://id — Component store reference (maps to OCI).
    #[cfg(feature = "oci")]
    Store { reference: String },
}

impl BundleSource {
    /// Parse a bundle source string into the appropriate variant.
    ///
    /// # Examples
    ///
    /// ```
    /// use greentic_setup::bundle_source::BundleSource;
    ///
    /// // Local path
    /// let source = BundleSource::parse("./my-bundle").unwrap();
    ///
    /// // file:// URI
    /// let source = BundleSource::parse("file:///home/user/bundle").unwrap();
    ///
    /// // OCI reference (requires "oci" feature)
    /// // let source = BundleSource::parse("oci://ghcr.io/org/bundle:latest").unwrap();
    /// ```
    pub fn parse(source: &str) -> anyhow::Result<Self> {
        let trimmed = source.trim();

        if trimmed.is_empty() {
            return Err(anyhow!("bundle source cannot be empty"));
        }

        // OCI protocol
        #[cfg(feature = "oci")]
        if trimmed.starts_with("oci://") {
            return Ok(Self::Oci {
                reference: trimmed.to_string(),
            });
        }

        // Repo protocol
        #[cfg(feature = "oci")]
        if trimmed.starts_with("repo://") {
            return Ok(Self::Repo {
                reference: trimmed.to_string(),
            });
        }

        // Store protocol
        #[cfg(feature = "oci")]
        if trimmed.starts_with("store://") {
            return Ok(Self::Store {
                reference: trimmed.to_string(),
            });
        }

        // file:// URI
        if trimmed.starts_with("file://") {
            let path = file_uri_to_path(trimmed)?;
            return Ok(Self::FileUri(path));
        }

        // Check for unsupported protocols
        #[cfg(not(feature = "oci"))]
        if trimmed.starts_with("oci://")
            || trimmed.starts_with("repo://")
            || trimmed.starts_with("store://")
        {
            return Err(anyhow!(
                "protocol not supported (compile with 'oci' feature): {}",
                trimmed.split("://").next().unwrap_or("unknown")
            ));
        }

        // Treat as local path
        let path = PathBuf::from(trimmed);
        Ok(Self::LocalDir(path))
    }

    /// Resolve the source to a local artifact path.
    ///
    /// For local sources, validates the path exists.
    /// For remote sources, fetches and extracts to a local cache directory.
    pub fn resolve(&self) -> anyhow::Result<PathBuf> {
        match self {
            Self::LocalDir(path) => resolve_local_path(path),
            Self::FileUri(path) => resolve_local_path(path),
            #[cfg(feature = "oci")]
            Self::Oci { reference } => resolve_oci_pack_reference(reference),
            #[cfg(feature = "oci")]
            Self::Repo { reference } => resolve_distributor_reference(reference),
            #[cfg(feature = "oci")]
            Self::Store { reference } => resolve_distributor_reference(reference),
        }
    }

    /// Resolve the source asynchronously.
    ///
    /// For local sources, validates the path exists.
    /// For remote sources, fetches and extracts to a local cache directory.
    pub async fn resolve_async(&self) -> anyhow::Result<PathBuf> {
        match self {
            Self::LocalDir(path) => resolve_local_path(path),
            Self::FileUri(path) => resolve_local_path(path),
            #[cfg(feature = "oci")]
            Self::Oci { reference } => resolve_oci_pack_reference_async(reference).await,
            #[cfg(feature = "oci")]
            Self::Repo { reference } => resolve_distributor_reference_async(reference).await,
            #[cfg(feature = "oci")]
            Self::Store { reference } => resolve_distributor_reference_async(reference).await,
        }
    }

    /// Returns the original source string representation.
    pub fn as_str(&self) -> String {
        match self {
            Self::LocalDir(path) => path.display().to_string(),
            Self::FileUri(path) => format!("file://{}", path.display()),
            #[cfg(feature = "oci")]
            Self::Oci { reference } => reference.clone(),
            #[cfg(feature = "oci")]
            Self::Repo { reference } => reference.clone(),
            #[cfg(feature = "oci")]
            Self::Store { reference } => reference.clone(),
        }
    }

    /// Returns true if this is a local source (LocalDir or FileUri).
    pub fn is_local(&self) -> bool {
        matches!(self, Self::LocalDir(_) | Self::FileUri(_))
    }

    /// Returns true if this is a remote source (Oci, Repo, or Store).
    #[cfg(feature = "oci")]
    pub fn is_remote(&self) -> bool {
        matches!(
            self,
            Self::Oci { .. } | Self::Repo { .. } | Self::Store { .. }
        )
    }
}

/// Convert a file:// URI to a local path.
fn file_uri_to_path(uri: &str) -> anyhow::Result<PathBuf> {
    let path_str = uri
        .strip_prefix("file://")
        .ok_or_else(|| anyhow!("invalid file URI: {}", uri))?;

    // Handle Windows paths (file:///C:/path)
    #[cfg(windows)]
    let path_str = path_str.strip_prefix('/').unwrap_or(path_str);

    let decoded = percent_decode(path_str);
    Ok(PathBuf::from(decoded))
}

/// Simple percent-decoding for file paths.
fn percent_decode(input: &str) -> String {
    let mut result = String::with_capacity(input.len());
    let mut chars = input.chars().peekable();

    while let Some(ch) = chars.next() {
        if ch == '%' {
            let hex: String = chars.by_ref().take(2).collect();
            if hex.len() == 2
                && let Ok(byte) = u8::from_str_radix(&hex, 16)
            {
                result.push(byte as char);
                continue;
            }
            result.push('%');
            result.push_str(&hex);
        } else {
            result.push(ch);
        }
    }

    result
}

/// Resolve a local path, validating it exists.
fn resolve_local_path(path: &Path) -> anyhow::Result<PathBuf> {
    let canonical = if path.is_absolute() {
        path.to_path_buf()
    } else {
        std::env::current_dir()
            .context("failed to get current directory")?
            .join(path)
    };

    if !canonical.exists() {
        return Err(anyhow!(
            "bundle path does not exist: {}",
            canonical.display()
        ));
    }

    Ok(canonical)
}

/// Resolve an OCI pack reference using the pack fetcher.
#[cfg(feature = "oci")]
fn resolve_oci_pack_reference(reference: &str) -> anyhow::Result<PathBuf> {
    use tokio::runtime::Runtime;

    let rt = Runtime::new().context("failed to create tokio runtime")?;
    rt.block_on(resolve_oci_pack_reference_async(reference))
}

/// Resolve an OCI pack reference asynchronously.
#[cfg(feature = "oci")]
async fn resolve_oci_pack_reference_async(reference: &str) -> anyhow::Result<PathBuf> {
    use greentic_distributor_client::oci_packs::DefaultRegistryClient;
    use greentic_distributor_client::{OciPackFetcher, PackFetchOptions};

    let oci_reference = reference.strip_prefix("oci://").unwrap_or(reference).trim();
    let options = PackFetchOptions {
        allow_tags: true,
        ..PackFetchOptions::default()
    };
    let fetched =
        if let Some((username, password)) = registry_basic_auth_for_reference(oci_reference) {
            let client = DefaultRegistryClient::with_basic_auth(username, password);
            OciPackFetcher::with_client(client, options)
                .fetch_pack_to_cache(oci_reference)
                .await
        } else {
            OciPackFetcher::<DefaultRegistryClient>::new(options)
                .fetch_pack_to_cache(oci_reference)
                .await
        }
        .with_context(|| format!("failed to fetch OCI pack reference: {}", reference))?;

    if fetched.path.exists() {
        return Ok(fetched.path);
    }

    anyhow::bail!(
        "resolved bundle reference without a local cached artifact: {}",
        reference
    );
}

#[cfg(feature = "oci")]
fn registry_basic_auth_for_reference(reference: &str) -> Option<(String, String)> {
    let registry = reference.split('/').next().unwrap_or_default();

    let generic_username = std::env::var("OCI_USERNAME")
        .ok()
        .filter(|value| !value.is_empty());
    let generic_password = std::env::var("OCI_PASSWORD")
        .ok()
        .filter(|value| !value.is_empty());
    if let (Some(username), Some(password)) = (generic_username, generic_password) {
        return Some((username, password));
    }

    if registry == "ghcr.io" {
        let password = std::env::var("GHCR_TOKEN")
            .ok()
            .filter(|value| !value.is_empty())
            .or_else(|| {
                std::env::var("GITHUB_TOKEN")
                    .ok()
                    .filter(|value| !value.is_empty())
            });
        let username = std::env::var("GHCR_USERNAME")
            .ok()
            .filter(|value| !value.is_empty())
            .or_else(|| {
                std::env::var("GHCR_USER")
                    .ok()
                    .filter(|value| !value.is_empty())
            })
            .or_else(|| {
                std::env::var("GITHUB_ACTOR")
                    .ok()
                    .filter(|value| !value.is_empty())
            })
            .or_else(|| std::env::var("USER").ok().filter(|value| !value.is_empty()));

        if let (Some(username), Some(password)) = (username, password) {
            return Some((username, password));
        }
    }

    None
}

/// Resolve a repo/store reference using greentic-distributor-client.
#[cfg(feature = "oci")]
fn resolve_distributor_reference(reference: &str) -> anyhow::Result<PathBuf> {
    use tokio::runtime::Runtime;

    let rt = Runtime::new().context("failed to create tokio runtime")?;
    rt.block_on(resolve_distributor_reference_async(reference))
}

/// Resolve a repo/store reference asynchronously.
#[cfg(feature = "oci")]
async fn resolve_distributor_reference_async(reference: &str) -> anyhow::Result<PathBuf> {
    use greentic_distributor_client::{CachePolicy, DistClient, DistOptions, ResolvePolicy};

    let client = DistClient::new(DistOptions::default());
    let source = client
        .parse_source(reference)
        .with_context(|| format!("failed to parse bundle reference: {}", reference))?;
    let resolved = client
        .resolve(source, ResolvePolicy)
        .await
        .with_context(|| format!("failed to resolve bundle reference: {}", reference))?;
    let fetched = client
        .fetch(&resolved, CachePolicy)
        .await
        .with_context(|| format!("failed to fetch bundle reference: {}", reference))?;

    if fetched.local_path.exists() {
        return Ok(fetched.local_path);
    }
    if let Some(path) = fetched.wasm_path
        && path.exists()
    {
        return Ok(path);
    }
    if let Some(path) = fetched.cache_path
        && path.exists()
    {
        return Ok(path);
    }

    anyhow::bail!(
        "resolved bundle reference without a local cached artifact: {}",
        reference
    );
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;
    use std::sync::{Mutex, OnceLock};

    fn env_lock() -> std::sync::MutexGuard<'static, ()> {
        static LOCK: OnceLock<Mutex<()>> = OnceLock::new();
        LOCK.get_or_init(|| Mutex::new(())).lock().unwrap()
    }

    #[test]
    fn parse_local_path() {
        let source = BundleSource::parse("./my-bundle").unwrap();
        assert!(matches!(source, BundleSource::LocalDir(_)));
    }

    #[test]
    fn parse_absolute_path() {
        let source = BundleSource::parse("/home/user/bundle").unwrap();
        assert!(matches!(source, BundleSource::LocalDir(_)));
    }

    #[test]
    fn parse_file_uri() {
        let source = BundleSource::parse("file:///home/user/bundle").unwrap();
        assert!(matches!(source, BundleSource::FileUri(_)));
        if let BundleSource::FileUri(path) = source {
            assert_eq!(path, PathBuf::from("/home/user/bundle"));
        }
    }

    #[cfg(feature = "oci")]
    #[test]
    fn parse_oci_reference() {
        let source = BundleSource::parse("oci://ghcr.io/org/bundle:latest").unwrap();
        assert!(matches!(source, BundleSource::Oci { .. }));
    }

    #[cfg(feature = "oci")]
    #[test]
    fn parse_repo_reference() {
        let source = BundleSource::parse("repo://greentic/messaging-telegram").unwrap();
        assert!(matches!(source, BundleSource::Repo { .. }));
    }

    #[cfg(feature = "oci")]
    #[test]
    fn parse_store_reference() {
        let source = BundleSource::parse("store://bundle-abc123").unwrap();
        assert!(matches!(source, BundleSource::Store { .. }));
    }

    #[test]
    fn empty_source_fails() {
        assert!(BundleSource::parse("").is_err());
        assert!(BundleSource::parse("   ").is_err());
    }

    #[test]
    fn file_uri_percent_decode() {
        let decoded = percent_decode("path%20with%20spaces");
        assert_eq!(decoded, "path with spaces");
    }

    #[test]
    fn percent_decode_preserves_invalid_sequences() {
        let decoded = percent_decode("path%2G%tail%");
        assert_eq!(decoded, "path%2G%tail%");
    }

    #[test]
    fn as_str_preserves_local_and_file_uri_sources() {
        let local = BundleSource::parse("./bundle").unwrap();
        assert_eq!(local.as_str(), "./bundle");

        let file_uri = BundleSource::parse("file:///tmp/test%20bundle").unwrap();
        assert_eq!(file_uri.as_str(), "file:///tmp/test bundle");
    }

    #[test]
    fn is_local_checks() {
        let local = BundleSource::parse("./bundle").unwrap();
        assert!(local.is_local());

        let file_uri = BundleSource::parse("file:///path").unwrap();
        assert!(file_uri.is_local());
    }

    #[cfg(feature = "oci")]
    #[test]
    fn is_remote_checks() {
        let oci = BundleSource::parse("oci://ghcr.io/test").unwrap();
        assert!(oci.is_remote());
        assert!(!oci.is_local());
    }

    #[cfg(feature = "oci")]
    #[test]
    fn remote_references_preserve_original_strings() {
        let refs = [
            "oci://ghcr.io/greentic/example-pack:latest",
            "repo://greentic/example-pack",
            "store://greentic-biz/demo/example-pack:latest",
        ];

        for raw in refs {
            let parsed = BundleSource::parse(raw).unwrap();
            assert_eq!(parsed.as_str(), raw);
            assert!(parsed.is_remote());
        }
    }

    #[cfg(feature = "oci")]
    #[test]
    fn registry_basic_auth_uses_generic_oci_credentials_first() {
        let _guard = env_lock();
        unsafe {
            std::env::set_var("OCI_USERNAME", "oci-user");
            std::env::set_var("OCI_PASSWORD", "oci-pass");
            std::env::remove_var("GHCR_TOKEN");
            std::env::remove_var("GITHUB_TOKEN");
            std::env::remove_var("GHCR_USERNAME");
            std::env::remove_var("GHCR_USER");
            std::env::remove_var("GITHUB_ACTOR");
            std::env::remove_var("USER");
        }

        let creds = registry_basic_auth_for_reference("ghcr.io/greentic/example-pack:latest");
        assert_eq!(
            creds,
            Some(("oci-user".to_string(), "oci-pass".to_string()))
        );

        unsafe {
            std::env::remove_var("OCI_USERNAME");
            std::env::remove_var("OCI_PASSWORD");
        }
    }

    #[cfg(feature = "oci")]
    #[test]
    fn registry_basic_auth_builds_ghcr_credentials_from_github_env() {
        let _guard = env_lock();
        unsafe {
            std::env::remove_var("OCI_USERNAME");
            std::env::remove_var("OCI_PASSWORD");
            std::env::set_var("GITHUB_TOKEN", "gh-token");
            std::env::set_var("GITHUB_ACTOR", "octocat");
            std::env::remove_var("GHCR_TOKEN");
            std::env::remove_var("GHCR_USERNAME");
            std::env::remove_var("GHCR_USER");
            std::env::remove_var("USER");
        }

        let creds = registry_basic_auth_for_reference("ghcr.io/greentic/example-pack:latest");
        assert_eq!(creds, Some(("octocat".to_string(), "gh-token".to_string())));

        unsafe {
            std::env::remove_var("GITHUB_TOKEN");
            std::env::remove_var("GITHUB_ACTOR");
        }
    }

    #[cfg(feature = "oci")]
    #[test]
    fn registry_basic_auth_returns_none_without_matching_env() {
        let _guard = env_lock();
        unsafe {
            std::env::remove_var("OCI_USERNAME");
            std::env::remove_var("OCI_PASSWORD");
            std::env::remove_var("GHCR_TOKEN");
            std::env::remove_var("GITHUB_TOKEN");
            std::env::remove_var("GHCR_USERNAME");
            std::env::remove_var("GHCR_USER");
            std::env::remove_var("GITHUB_ACTOR");
            std::env::remove_var("USER");
        }

        assert_eq!(
            registry_basic_auth_for_reference("example.com/greentic/example-pack:latest"),
            None
        );
    }

    #[test]
    fn resolve_local_dir_returns_relative_path_when_it_exists() {
        let temp = tempfile::tempdir().unwrap();
        let bundle = temp.path().join("bundle");
        fs::create_dir_all(&bundle).unwrap();
        let relative = bundle.strip_prefix(std::env::current_dir().unwrap()).ok();

        if let Some(relative) = relative {
            let source = BundleSource::LocalDir(relative.to_path_buf());
            assert_eq!(source.resolve().unwrap(), relative);
            return;
        }

        let source = BundleSource::LocalDir(bundle.clone());
        assert_eq!(source.resolve().unwrap(), bundle);
    }

    #[test]
    fn resolve_file_uri_returns_existing_absolute_path() {
        let temp = tempfile::tempdir().unwrap();
        let bundle = temp.path().join("bundle");
        fs::create_dir_all(&bundle).unwrap();

        let source = BundleSource::FileUri(bundle.clone());
        assert_eq!(source.resolve().unwrap(), bundle);
    }

    #[tokio::test]
    async fn resolve_async_supports_local_sources() {
        let temp = tempfile::tempdir().unwrap();
        let bundle = temp.path().join("bundle");
        fs::create_dir_all(&bundle).unwrap();

        let local = BundleSource::LocalDir(bundle.clone());
        assert_eq!(local.resolve_async().await.unwrap(), bundle);

        let file_uri = BundleSource::FileUri(bundle.clone());
        assert_eq!(file_uri.resolve_async().await.unwrap(), bundle);
    }

    #[test]
    fn resolve_missing_local_path_fails() {
        let temp = tempfile::tempdir().unwrap();
        let missing = temp.path().join("missing");
        let source = BundleSource::LocalDir(missing.clone());

        let error = source.resolve().unwrap_err().to_string();
        assert!(error.contains("bundle path does not exist"));
        assert!(error.contains(&missing.display().to_string()));
    }
}