zentinel-proxy 0.6.11

A security-first reverse proxy built on Pingora with sleepable ops at the edge
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
//! Bundle CLI command handlers
//!
//! Implements the `zentinel bundle` subcommand and its subcommands.

use crate::bundle::fetch::{detect_arch, detect_os, download_agent};
use crate::bundle::install::{
    generate_default_config, generate_systemd_service, install_binary, install_config,
    install_systemd_service, uninstall_binary, InstallPaths,
};
use crate::bundle::lock::BundleLock;
use crate::bundle::status::BundleStatus;
use anyhow::{Context, Result};
use clap::{Args, Subcommand};
use std::path::PathBuf;

/// Bundle command arguments
#[derive(Args, Debug)]
pub struct BundleArgs {
    #[command(subcommand)]
    pub command: BundleCommand,
}

/// Bundle subcommands
#[derive(Subcommand, Debug)]
pub enum BundleCommand {
    /// Install bundled agents
    Install {
        /// Specific agent to install (installs all if not specified)
        agent: Option<String>,

        /// Preview what would be installed without making changes
        #[arg(long, short = 'n')]
        dry_run: bool,

        /// Force reinstallation even if already installed
        #[arg(long, short = 'f')]
        force: bool,

        /// Also install systemd service files
        #[arg(long)]
        systemd: bool,

        /// Custom installation prefix
        #[arg(long)]
        prefix: Option<PathBuf>,

        /// Skip checksum verification
        #[arg(long)]
        skip_verify: bool,
    },

    /// Show status of installed agents
    Status {
        /// Show detailed output
        #[arg(long, short = 'v')]
        verbose: bool,
    },

    /// List available agents in the bundle
    List {
        /// Show detailed information
        #[arg(long, short = 'v')]
        verbose: bool,
    },

    /// Uninstall bundled agents
    Uninstall {
        /// Specific agent to uninstall (uninstalls all if not specified)
        agent: Option<String>,

        /// Preview what would be uninstalled
        #[arg(long, short = 'n')]
        dry_run: bool,
    },

    /// Check for updates to bundled agents
    Update {
        /// Actually perform the update
        #[arg(long)]
        apply: bool,
    },
}

/// Run the bundle command
pub fn run_bundle_command(args: BundleArgs) -> Result<()> {
    // Load the embedded lock file
    let lock = BundleLock::embedded().context("Failed to load bundle lock file")?;

    match args.command {
        BundleCommand::Install {
            agent,
            dry_run,
            force,
            systemd,
            prefix,
            skip_verify,
        } => cmd_install(&lock, agent, dry_run, force, systemd, prefix, skip_verify),

        BundleCommand::Status { verbose } => cmd_status(&lock, verbose),

        BundleCommand::List { verbose } => cmd_list(&lock, verbose),

        BundleCommand::Uninstall { agent, dry_run } => cmd_uninstall(&lock, agent, dry_run),

        BundleCommand::Update { apply } => cmd_update(&lock, apply),
    }
}

/// Install command implementation
fn cmd_install(
    lock: &BundleLock,
    agent: Option<String>,
    dry_run: bool,
    force: bool,
    install_systemd: bool,
    prefix: Option<PathBuf>,
    skip_verify: bool,
) -> Result<()> {
    let paths = match prefix {
        Some(p) => InstallPaths::with_prefix(&p),
        None => InstallPaths::detect(),
    };

    println!("Zentinel Bundle Installer");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("Bundle version: {}", lock.bundle.version);
    println!("Platform:       {}-{}", detect_os(), detect_arch());
    println!("Install path:   {}", paths.bin_dir.display());
    if paths.system_wide {
        println!("Mode:           system-wide (requires root)");
    } else {
        println!("Mode:           user-local");
    }
    println!();

    // Get agents to install
    let agents: Vec<_> = match &agent {
        Some(name) => {
            let agent_info = lock
                .agent(name)
                .ok_or_else(|| anyhow::anyhow!("Unknown agent: {}", name))?;
            vec![agent_info]
        }
        None => lock.agents(),
    };

    if agents.is_empty() {
        println!("No agents to install.");
        return Ok(());
    }

    // Check current status
    let status = BundleStatus::check(lock, &paths);

    if dry_run {
        println!("[DRY RUN] Would install the following agents:");
        println!();
        for agent in &agents {
            let agent_status = status.agents.iter().find(|a| a.name == agent.name);

            let action = match agent_status {
                Some(s) if s.status == crate::bundle::status::Status::UpToDate && !force => {
                    "skip (already installed)"
                }
                Some(s) if s.status == crate::bundle::status::Status::Outdated => "upgrade",
                _ => "install",
            };

            println!(
                "  {} {} -> {} ({})",
                agent.name,
                agent.version,
                paths.bin_dir.display(),
                action
            );
        }
        return Ok(());
    }

    // Ensure directories exist
    paths
        .ensure_dirs()
        .context("Failed to create installation directories")?;

    // Create temporary directory for downloads
    let temp_dir = tempfile::tempdir().context("Failed to create temporary directory")?;

    // Create async runtime for downloads
    let rt = tokio::runtime::Runtime::new()?;

    // Install each agent
    let mut installed = 0;
    let mut skipped = 0;
    let mut failed = 0;

    for agent in &agents {
        let agent_status = status.agents.iter().find(|a| a.name == agent.name);

        // Skip if already installed (unless forced)
        if !force {
            if let Some(s) = agent_status {
                if s.status == crate::bundle::status::Status::UpToDate {
                    println!(
                        "  [skip] {} {} (already installed)",
                        agent.name, agent.version
                    );
                    skipped += 1;
                    continue;
                }
            }
        }

        print!("  Installing {} {}...", agent.name, agent.version);

        // Download
        let download_result =
            rt.block_on(async { download_agent(agent, temp_dir.path(), !skip_verify).await });

        let download = match download_result {
            Ok(d) => d,
            Err(e) => {
                println!(" FAILED");
                eprintln!("    Error: {}", e);
                failed += 1;
                continue;
            }
        };

        // Install binary
        if let Err(e) = install_binary(&download.binary_path, &paths.bin_dir, &agent.binary_name) {
            println!(" FAILED");
            eprintln!("    Error installing binary: {}", e);
            failed += 1;
            continue;
        }

        // Install config
        let config_content = generate_default_config(&agent.name);
        let config_path = install_config(&paths.config_dir, &agent.name, &config_content, force)
            .context("Failed to install config")?;

        // Install systemd service if requested
        if install_systemd {
            if let Some(ref systemd_dir) = paths.systemd_dir {
                let bin_path = paths.bin_dir.join(&agent.binary_name);
                let service_content =
                    generate_systemd_service(&agent.name, &bin_path, &config_path);
                install_systemd_service(systemd_dir, &agent.name, &service_content)
                    .context("Failed to install systemd service")?;
            }
        }

        let checksum_status = if download.checksum_verified {
            "verified"
        } else {
            "unverified"
        };

        println!(
            " OK ({} KB, {})",
            download.archive_size / 1024,
            checksum_status
        );
        installed += 1;
    }

    println!();
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!(
        "Installed: {} | Skipped: {} | Failed: {}",
        installed, skipped, failed
    );

    if installed > 0 {
        println!();
        println!("To start the agents:");
        if paths.system_wide && install_systemd {
            println!("  sudo systemctl daemon-reload");
            println!("  sudo systemctl start zentinel.target");
        } else {
            println!("  # Add agent endpoints to your zentinel.kdl config");
            println!("  # See: https://zentinelproxy.io/docs/bundle");
        }
    }

    if failed > 0 {
        anyhow::bail!("{} agent(s) failed to install", failed);
    }

    Ok(())
}

/// Status command implementation
fn cmd_status(lock: &BundleLock, verbose: bool) -> Result<()> {
    let paths = InstallPaths::detect();
    let status = BundleStatus::check(lock, &paths);

    println!("{}", status.display());

    if verbose {
        println!();
        println!("Paths:");
        println!("  Binaries: {}", paths.bin_dir.display());
        println!("  Configs:  {}", paths.config_dir.display());
        if let Some(ref sd) = paths.systemd_dir {
            println!("  Systemd:  {}", sd.display());
        }
    }

    Ok(())
}

/// List command implementation
fn cmd_list(lock: &BundleLock, verbose: bool) -> Result<()> {
    println!("Zentinel Bundle Agents");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");
    println!("Bundle version: {}", lock.bundle.version);
    println!();

    for agent in lock.agents() {
        println!("  {} v{}", agent.name, agent.version);
        if verbose {
            println!("    Repository: {}", agent.repository);
            println!("    Binary:     {}", agent.binary_name);
            println!(
                "    URL:        {}",
                agent.download_url(detect_os(), detect_arch())
            );
            println!();
        }
    }

    if !verbose {
        println!();
        println!("Use --verbose for more details");
    }

    Ok(())
}

/// Uninstall command implementation
fn cmd_uninstall(lock: &BundleLock, agent: Option<String>, dry_run: bool) -> Result<()> {
    let paths = InstallPaths::detect();

    println!("Zentinel Bundle Uninstaller");
    println!("━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━");

    let agents: Vec<_> = match &agent {
        Some(name) => {
            let agent_info = lock
                .agent(name)
                .ok_or_else(|| anyhow::anyhow!("Unknown agent: {}", name))?;
            vec![agent_info]
        }
        None => lock.agents(),
    };

    if dry_run {
        println!("[DRY RUN] Would uninstall:");
        for agent in &agents {
            let bin_path = paths.bin_dir.join(&agent.binary_name);
            if bin_path.exists() {
                println!("  {} ({})", agent.name, bin_path.display());
            }
        }
        return Ok(());
    }

    let mut removed = 0;
    for agent in &agents {
        if uninstall_binary(&paths.bin_dir, &agent.binary_name)? {
            println!("  Removed {}", agent.name);
            removed += 1;
        }
    }

    println!();
    println!("Removed {} agent(s)", removed);
    println!();
    println!(
        "Note: Configuration files in {} were preserved",
        paths.config_dir.display()
    );

    Ok(())
}

/// Update command implementation
fn cmd_update(current_lock: &BundleLock, apply: bool) -> Result<()> {
    println!("Checking for bundle updates...");
    println!();

    // Fetch latest lock file
    let rt = tokio::runtime::Runtime::new()?;
    let latest_lock = rt
        .block_on(BundleLock::fetch_latest())
        .context("Failed to fetch latest bundle versions")?;

    println!("Current bundle: {}", current_lock.bundle.version);
    println!("Latest bundle:  {}", latest_lock.bundle.version);
    println!();

    // Compare versions
    let mut updates_available = false;
    println!("{:<15} {:<12} {:<12}", "Agent", "Current", "Latest");
    println!("{}", "".repeat(40));

    for (name, latest_version) in &latest_lock.agents {
        let current_version = current_lock
            .agents
            .get(name)
            .map(|s| s.as_str())
            .unwrap_or("-");
        let is_update = current_version != latest_version;

        if is_update {
            updates_available = true;
            println!(
                "{:<15} {:<12} {:<12} ←",
                name, current_version, latest_version
            );
        } else {
            println!(
                "{:<15} {:<12} {:<12}",
                name, current_version, latest_version
            );
        }
    }

    if !updates_available {
        println!();
        println!("All agents are up to date.");
        return Ok(());
    }

    println!();
    if apply {
        println!("To update, run: zentinel bundle install --force");
    } else {
        println!("Updates are available. Run with --apply to update.");
        println!("  zentinel bundle update --apply");
    }

    Ok(())
}