sherpack-core 0.4.0

Core types and utilities for Sherpack - the Kubernetes package manager
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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
//! Files API for accessing pack files from templates
//!
//! This module provides a sandboxed file access API that allows templates
//! to read files from within the pack directory. All operations are restricted
//! to the pack root to prevent path traversal attacks.
//!
//! # Security
//!
//! - All paths are resolved relative to the pack root
//! - Absolute paths are rejected
//! - Path traversal attempts (../) are detected and rejected
//! - Files outside the pack directory cannot be accessed
//!
//! # Example
//!
//! ```jinja2
//! {# Read a file #}
//! data:
//!   nginx.conf: {{ files.get("config/nginx.conf") | b64encode }}
//!
//! {# Check if file exists #}
//! {% if files.exists("config/custom.yaml") %}
//!   custom: {{ files.get("config/custom.yaml") }}
//! {% endif %}
//!
//! {# Iterate over files matching a glob pattern #}
//! {% for file in files.glob("scripts/*.sh") %}
//!   {{ file.name }}: {{ file.content | b64encode }}
//! {% endfor %}
//! ```

use std::collections::HashMap;
use std::path::{Path, PathBuf};
use std::sync::{Arc, RwLock};

use serde::{Deserialize, Serialize};

use crate::error::{CoreError, Result};

/// Trait for file access providers
///
/// This trait allows for different implementations:
/// - `SandboxedFileProvider`: Real filesystem access (sandboxed to pack root)
/// - `MockFileProvider`: In-memory files for testing
/// - `ArchiveFileProvider`: Read files from a tar.gz archive (future)
pub trait FileProvider: Send + Sync {
    /// Read the contents of a file as bytes
    fn get(&self, path: &str) -> Result<Vec<u8>>;

    /// Check if a file exists
    fn exists(&self, path: &str) -> bool;

    /// List files matching a glob pattern
    fn glob(&self, pattern: &str) -> Result<Vec<FileEntry>>;

    /// Read a file as lines
    fn lines(&self, path: &str) -> Result<Vec<String>>;

    /// Read the contents of a file as a string (UTF-8)
    fn get_string(&self, path: &str) -> Result<String> {
        let bytes = self.get(path)?;
        String::from_utf8(bytes).map_err(|e| CoreError::FileAccess {
            path: path.to_string(),
            message: format!("file is not valid UTF-8: {}", e),
        })
    }
}

/// A file entry returned by glob operations
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileEntry {
    /// Relative path from pack root
    pub path: String,
    /// File name (without directory)
    pub name: String,
    /// File content as string (UTF-8 lossy)
    pub content: String,
    /// File size in bytes
    pub size: usize,
}

/// Sandboxed file provider that restricts access to the pack directory
///
/// This is the default provider used during template rendering.
/// It ensures that templates cannot access files outside the pack root.
#[derive(Debug)]
pub struct SandboxedFileProvider {
    /// The root directory of the pack (all paths are relative to this)
    root: PathBuf,
    /// Canonicalized root for security checks
    canonical_root: PathBuf,
    /// Cache of file contents to avoid repeated reads
    cache: Arc<RwLock<HashMap<PathBuf, Vec<u8>>>>,
}

impl SandboxedFileProvider {
    /// Create a new sandboxed file provider
    ///
    /// # Arguments
    ///
    /// * `pack_root` - The root directory of the pack
    ///
    /// # Errors
    ///
    /// Returns an error if the pack root doesn't exist or cannot be canonicalized.
    pub fn new(pack_root: impl AsRef<Path>) -> Result<Self> {
        let root = pack_root.as_ref().to_path_buf();

        if !root.exists() {
            return Err(CoreError::FileAccess {
                path: root.display().to_string(),
                message: "pack root directory does not exist".to_string(),
            });
        }

        let canonical_root = root.canonicalize().map_err(|e| CoreError::FileAccess {
            path: root.display().to_string(),
            message: format!("failed to canonicalize pack root: {}", e),
        })?;

        Ok(Self {
            root,
            canonical_root,
            cache: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Resolve a relative path and verify it's within the sandbox
    ///
    /// # Security
    ///
    /// This method:
    /// 1. Rejects absolute paths
    /// 2. Joins the path with the pack root
    /// 3. Canonicalizes to resolve symlinks and `..` components
    /// 4. Verifies the result is still within the pack root
    fn resolve_path(&self, relative: &str) -> Result<PathBuf> {
        let requested = Path::new(relative);

        // Reject absolute paths
        if requested.is_absolute() {
            return Err(CoreError::FileAccess {
                path: relative.to_string(),
                message: "absolute paths are not allowed in templates".to_string(),
            });
        }

        // Quick check for obvious traversal attempts
        if relative.contains("..") {
            // Still do the full check, but this catches simple cases early
        }

        // Build the full path
        let full_path = self.root.join(relative);

        // Check if the file exists before canonicalizing
        if !full_path.exists() {
            return Err(CoreError::FileAccess {
                path: relative.to_string(),
                message: "file not found".to_string(),
            });
        }

        // Canonicalize to resolve symlinks and .. components
        let canonical = full_path
            .canonicalize()
            .map_err(|e| CoreError::FileAccess {
                path: relative.to_string(),
                message: format!("failed to resolve path: {}", e),
            })?;

        // Verify the path is within the sandbox
        if !canonical.starts_with(&self.canonical_root) {
            return Err(CoreError::FileAccess {
                path: relative.to_string(),
                message: "path escapes pack directory (sandbox violation)".to_string(),
            });
        }

        Ok(canonical)
    }

    /// Check if a path is valid without reading the file
    fn is_valid_path(&self, relative: &str) -> bool {
        self.resolve_path(relative).is_ok()
    }
}

impl FileProvider for SandboxedFileProvider {
    fn get(&self, path: &str) -> Result<Vec<u8>> {
        let resolved = self.resolve_path(path)?;

        // Check cache first
        {
            let cache = self.cache.read().map_err(|_| CoreError::FileAccess {
                path: path.to_string(),
                message: "cache lock poisoned".to_string(),
            })?;

            if let Some(content) = cache.get(&resolved) {
                return Ok(content.clone());
            }
        }

        // Read the file
        let content = std::fs::read(&resolved).map_err(|e| CoreError::FileAccess {
            path: path.to_string(),
            message: format!("failed to read file: {}", e),
        })?;

        // Update cache
        {
            let mut cache = self.cache.write().map_err(|_| CoreError::FileAccess {
                path: path.to_string(),
                message: "cache lock poisoned".to_string(),
            })?;

            cache.insert(resolved, content.clone());
        }

        Ok(content)
    }

    fn exists(&self, path: &str) -> bool {
        self.is_valid_path(path)
    }

    fn glob(&self, pattern: &str) -> Result<Vec<FileEntry>> {
        // Validate the glob pattern
        let glob_pattern = glob::Pattern::new(pattern).map_err(|e| CoreError::GlobPattern {
            message: format!("invalid glob pattern '{}': {}", pattern, e),
        })?;

        let mut entries = Vec::new();

        // Walk the pack directory
        for entry in walkdir::WalkDir::new(&self.root)
            .into_iter()
            .filter_map(|e| e.ok())
            .filter(|e| e.file_type().is_file())
        {
            // Get relative path
            let rel_path = match entry.path().strip_prefix(&self.root) {
                Ok(p) => p,
                Err(_) => continue,
            };

            let rel_str = rel_path.to_string_lossy();

            // Check if it matches the pattern
            if glob_pattern.matches(&rel_str) {
                // Read the file content
                let content = match std::fs::read_to_string(entry.path()) {
                    Ok(c) => c,
                    Err(_) => {
                        // For binary files, use lossy conversion
                        match std::fs::read(entry.path()) {
                            Ok(bytes) => String::from_utf8_lossy(&bytes).to_string(),
                            Err(_) => continue,
                        }
                    }
                };

                let size = content.len();

                entries.push(FileEntry {
                    path: rel_str.to_string(),
                    name: entry.file_name().to_string_lossy().to_string(),
                    content,
                    size,
                });
            }
        }

        // Sort for deterministic output (important for reproducible templates)
        entries.sort_by(|a, b| a.path.cmp(&b.path));

        Ok(entries)
    }

    fn lines(&self, path: &str) -> Result<Vec<String>> {
        let content = self.get_string(path)?;
        Ok(content.lines().map(String::from).collect())
    }
}

/// Mock file provider for testing
///
/// This provider stores files in memory, allowing tests to run
/// without filesystem access.
#[derive(Debug, Default, Clone)]
pub struct MockFileProvider {
    files: HashMap<String, Vec<u8>>,
}

impl MockFileProvider {
    /// Create a new empty mock provider
    pub fn new() -> Self {
        Self::default()
    }

    /// Add a file to the mock filesystem
    pub fn with_file(mut self, path: &str, content: impl Into<Vec<u8>>) -> Self {
        self.files.insert(path.to_string(), content.into());
        self
    }

    /// Add a text file to the mock filesystem
    pub fn with_text_file(self, path: &str, content: &str) -> Self {
        self.with_file(path, content.as_bytes().to_vec())
    }

    /// Add multiple files at once
    pub fn with_files(
        mut self,
        files: impl IntoIterator<Item = (&'static str, &'static str)>,
    ) -> Self {
        for (path, content) in files {
            self.files
                .insert(path.to_string(), content.as_bytes().to_vec());
        }
        self
    }
}

impl FileProvider for MockFileProvider {
    fn get(&self, path: &str) -> Result<Vec<u8>> {
        self.files
            .get(path)
            .cloned()
            .ok_or_else(|| CoreError::FileAccess {
                path: path.to_string(),
                message: "file not found".to_string(),
            })
    }

    fn exists(&self, path: &str) -> bool {
        self.files.contains_key(path)
    }

    fn glob(&self, pattern: &str) -> Result<Vec<FileEntry>> {
        let glob_pattern = glob::Pattern::new(pattern).map_err(|e| CoreError::GlobPattern {
            message: format!("invalid glob pattern '{}': {}", pattern, e),
        })?;

        let mut entries: Vec<_> = self
            .files
            .iter()
            .filter(|(path, _)| glob_pattern.matches(path))
            .map(|(path, content)| {
                let name = Path::new(path)
                    .file_name()
                    .map(|s| s.to_string_lossy().to_string())
                    .unwrap_or_default();

                FileEntry {
                    path: path.clone(),
                    name,
                    content: String::from_utf8_lossy(content).to_string(),
                    size: content.len(),
                }
            })
            .collect();

        // Sort for deterministic output
        entries.sort_by(|a, b| a.path.cmp(&b.path));

        Ok(entries)
    }

    fn lines(&self, path: &str) -> Result<Vec<String>> {
        let content = self.get_string(path)?;
        Ok(content.lines().map(String::from).collect())
    }
}

/// A wrapper that provides the Files API to templates
///
/// This struct is what gets injected into the template context as `files`.
/// It wraps any `FileProvider` implementation.
#[derive(Clone)]
pub struct Files {
    provider: Arc<dyn FileProvider>,
}

impl Files {
    /// Create a new Files wrapper from a provider
    pub fn new(provider: impl FileProvider + 'static) -> Self {
        Self {
            provider: Arc::new(provider),
        }
    }

    /// Create Files from an Arc'd provider (avoids double-Arc)
    pub fn from_arc(provider: Arc<dyn FileProvider>) -> Self {
        Self { provider }
    }

    /// Create a sandboxed Files instance for a pack
    pub fn for_pack(pack_root: impl AsRef<Path>) -> Result<Self> {
        let provider = SandboxedFileProvider::new(pack_root)?;
        Ok(Self::new(provider))
    }

    /// Create a mock Files instance for testing
    pub fn mock() -> MockFileProvider {
        MockFileProvider::new()
    }

    /// Get file contents as string
    pub fn get(&self, path: &str) -> Result<String> {
        self.provider.get_string(path)
    }

    /// Get file contents as bytes
    pub fn get_bytes(&self, path: &str) -> Result<Vec<u8>> {
        self.provider.get(path)
    }

    /// Check if file exists
    pub fn exists(&self, path: &str) -> bool {
        self.provider.exists(path)
    }

    /// Glob for files
    pub fn glob(&self, pattern: &str) -> Result<Vec<FileEntry>> {
        self.provider.glob(pattern)
    }

    /// Read file as lines
    pub fn lines(&self, path: &str) -> Result<Vec<String>> {
        self.provider.lines(path)
    }
}

impl std::fmt::Debug for Files {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("Files").finish_non_exhaustive()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use tempfile::TempDir;

    fn create_test_pack() -> TempDir {
        let temp = TempDir::new().unwrap();

        // Create directory structure
        std::fs::create_dir_all(temp.path().join("config")).unwrap();
        std::fs::create_dir_all(temp.path().join("scripts")).unwrap();

        // Create test files
        std::fs::write(temp.path().join("config/app.yaml"), "key: value").unwrap();
        std::fs::write(temp.path().join("config/db.yaml"), "host: localhost").unwrap();
        std::fs::write(
            temp.path().join("scripts/init.sh"),
            "#!/bin/bash\necho hello",
        )
        .unwrap();
        std::fs::write(temp.path().join("README.md"), "# Test Pack").unwrap();

        temp
    }

    #[test]
    fn test_sandboxed_provider_read_file() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        let content = provider.get_string("config/app.yaml").unwrap();
        assert_eq!(content, "key: value");
    }

    #[test]
    fn test_sandboxed_provider_exists() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        assert!(provider.exists("config/app.yaml"));
        assert!(provider.exists("README.md"));
        assert!(!provider.exists("nonexistent.txt"));
    }

    #[test]
    fn test_sandboxed_provider_glob() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        let entries = provider.glob("config/*.yaml").unwrap();
        assert_eq!(entries.len(), 2);

        // Check sorted order
        assert_eq!(entries[0].name, "app.yaml");
        assert_eq!(entries[1].name, "db.yaml");
    }

    #[test]
    fn test_sandboxed_provider_lines() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        let lines = provider.lines("scripts/init.sh").unwrap();
        assert_eq!(lines.len(), 2);
        assert_eq!(lines[0], "#!/bin/bash");
        assert_eq!(lines[1], "echo hello");
    }

    #[test]
    fn test_sandbox_prevents_absolute_paths() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        // Use platform-appropriate absolute path
        #[cfg(windows)]
        let abs_path = "C:\\Windows\\System32\\config\\SAM";
        #[cfg(not(windows))]
        let abs_path = "/etc/passwd";

        let result = provider.get(abs_path);
        assert!(result.is_err());
        assert!(result.unwrap_err().to_string().contains("absolute paths"));
    }

    #[test]
    fn test_sandbox_prevents_path_traversal() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        // Create a file outside the pack
        let parent = temp.path().parent().unwrap();
        std::fs::write(parent.join("secret.txt"), "secret data").unwrap();

        // Try to access it via path traversal
        let result = provider.get("../secret.txt");
        assert!(result.is_err());

        let err = result.unwrap_err().to_string();
        // Either "sandbox violation" or "file not found" depending on resolution order
        assert!(err.contains("sandbox") || err.contains("not found"));
    }

    #[test]
    fn test_sandbox_prevents_deep_traversal() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        let result = provider.get("config/../../../../../../etc/passwd");
        assert!(result.is_err());
    }

    #[test]
    fn test_mock_provider() {
        let provider = MockFileProvider::new()
            .with_text_file("config/app.yaml", "key: value")
            .with_text_file("config/db.yaml", "host: localhost");

        assert!(provider.exists("config/app.yaml"));
        assert!(!provider.exists("nonexistent.txt"));

        let content = provider.get_string("config/app.yaml").unwrap();
        assert_eq!(content, "key: value");
    }

    #[test]
    fn test_mock_provider_glob() {
        let provider = MockFileProvider::new()
            .with_text_file("config/a.yaml", "a")
            .with_text_file("config/b.yaml", "b")
            .with_text_file("other/c.yaml", "c");

        let entries = provider.glob("config/*.yaml").unwrap();
        assert_eq!(entries.len(), 2);

        // Verify sorted order
        assert_eq!(entries[0].path, "config/a.yaml");
        assert_eq!(entries[1].path, "config/b.yaml");
    }

    #[test]
    fn test_files_wrapper() {
        let mock = MockFileProvider::new().with_text_file("test.txt", "hello world");

        let files = Files::new(mock);

        assert!(files.exists("test.txt"));
        assert_eq!(files.get("test.txt").unwrap(), "hello world");
    }

    #[test]
    fn test_glob_deterministic_order() {
        // Create files in non-alphabetical order
        let provider = MockFileProvider::new()
            .with_text_file("z.yaml", "z")
            .with_text_file("a.yaml", "a")
            .with_text_file("m.yaml", "m");

        let entries = provider.glob("*.yaml").unwrap();
        let paths: Vec<_> = entries.iter().map(|e| e.path.as_str()).collect();

        assert_eq!(paths, vec!["a.yaml", "m.yaml", "z.yaml"]);
    }

    #[test]
    fn test_file_caching() {
        let temp = create_test_pack();
        let provider = SandboxedFileProvider::new(temp.path()).unwrap();

        // First read
        let content1 = provider.get("config/app.yaml").unwrap();

        // Modify the file
        std::fs::write(temp.path().join("config/app.yaml"), "modified").unwrap();

        // Second read should return cached content
        let content2 = provider.get("config/app.yaml").unwrap();

        assert_eq!(content1, content2);
    }

    #[test]
    fn test_binary_file_handling() {
        let temp = TempDir::new().unwrap();

        // Create a binary file
        let binary_data = vec![0u8, 1, 2, 255, 254, 253];
        std::fs::write(temp.path().join("binary.bin"), &binary_data).unwrap();

        let provider = SandboxedFileProvider::new(temp.path()).unwrap();
        let content = provider.get("binary.bin").unwrap();

        assert_eq!(content, binary_data);
    }

    #[test]
    fn test_glob_pattern_validation() {
        let provider = MockFileProvider::new();

        // Invalid glob pattern (unclosed bracket)
        let result = provider.glob("[invalid");
        assert!(result.is_err());
        assert!(
            result
                .unwrap_err()
                .to_string()
                .contains("invalid glob pattern")
        );
    }
}