esk 0.8.0

Encrypted Secrets Keeper with multi-target deploy
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
pub mod aws_secrets_manager;
pub mod azure_key_vault;
pub mod bitwarden;
pub mod cloud_file;
pub mod doppler;
pub mod gcp_secret_manager;
pub mod hashicorp_vault;
pub mod infisical;
pub mod onepassword;
pub mod s3;
pub mod sops;

use anyhow::Result;
use std::collections::BTreeMap;

use crate::config::Config;
use crate::store::{validate_key, StorePayload};
use crate::targets::{CommandRunner, PreflightItem};

/// The key used to store version metadata in remote payloads.
pub const ESK_VERSION_KEY: &str = "_esk_version";

/// Parse a pulled string-valued secret map back into composite-key secrets.
/// Extracts the version from `ESK_VERSION_KEY`, strips it from the map,
/// and re-adds the `:env` suffix to all remaining keys.
pub fn parse_pulled_secrets(
    data: BTreeMap<String, String>,
    env: &str,
) -> (BTreeMap<String, String>, u64) {
    let version: u64 = if let Some(v) = data.get(ESK_VERSION_KEY) {
        if let Ok(n) = v.parse() {
            n
        } else {
            let _ = cliclack::log::warning(format!(
                "Remote returned unparseable {ESK_VERSION_KEY}: '{v}'. Defaulting to version 0."
            ));
            0
        }
    } else {
        let _ = cliclack::log::warning(format!(
            "Remote did not include {ESK_VERSION_KEY}. Defaulting to version 0."
        ));
        0
    };

    let composite: BTreeMap<String, String> = data
        .into_iter()
        .filter(|(k, _)| k != ESK_VERSION_KEY)
        .filter(|(k, _)| {
            if validate_key(k).is_err() {
                let _ = cliclack::log::warning(format!("Skipping invalid key from remote: '{k}'"));
                false
            } else {
                true
            }
        })
        .map(|(k, v)| (format!("{k}:{env}"), v))
        .collect();

    (composite, version)
}

/// A sync remote that stores/retrieves the full secret state.
///
/// Unlike deploy targets, remotes
/// store or backup the entire secret list as a source of truth.
pub trait SyncRemote: Send + Sync {
    fn name(&self) -> &str;

    /// Validate that external dependencies are available before push/pull.
    fn preflight(&self) -> Result<()> {
        Ok(())
    }

    /// Push store state to this remote for a given environment.
    fn push(&self, payload: &StorePayload, config: &Config, env: &str) -> Result<()>;

    /// Pull store state from this remote for a given environment.
    /// Returns (composite_key_secrets, version), or None if nothing stored.
    fn pull(&self, config: &Config, env: &str) -> Result<Option<(BTreeMap<String, String>, u64)>>;

    /// Whether this remote passes secret values as CLI arguments (visible in `ps`).
    fn passes_value_as_cli_arg(&self) -> bool {
        false
    }

    /// Whether this remote stores secrets in cleartext format.
    fn uses_cleartext_format(&self) -> bool {
        false
    }
}

/// Health status of a configured remote.
pub struct RemoteHealth {
    pub name: String,
    pub status: crate::targets::HealthStatus,
}

pub(crate) struct RemoteCandidate<'a> {
    pub(crate) remote: Box<dyn SyncRemote + 'a>,
    pub(crate) ok_message: &'static str,
}

impl PreflightItem for RemoteCandidate<'_> {
    fn preflight_name(&self) -> &str {
        self.remote.name()
    }

    fn preflight(&self) -> Result<()> {
        self.remote.preflight()
    }

    fn ok_message(&self) -> &str {
        self.ok_message
    }
}

fn remote_candidates<'a>(
    config: &'a Config,
    runner: &'a dyn CommandRunner,
) -> Vec<RemoteCandidate<'a>> {
    use crate::config::TypedRemoteConfig;

    config
        .typed_remotes
        .iter()
        .map(|typed| {
            let ok_message = typed.ok_message();
            let remote: Box<dyn SyncRemote + 'a> = match typed {
                TypedRemoteConfig::OnePassword(cfg) => Box::new(
                    onepassword::OnePasswordRemote::new(config, cfg.clone(), runner),
                ),
                TypedRemoteConfig::CloudFile { name, config: cfg } => {
                    Box::new(cloud_file::CloudFileRemote::new(
                        name.clone(),
                        config.project.clone(),
                        cfg.clone(),
                    ))
                }
                TypedRemoteConfig::AwsSecretsManager(cfg) => Box::new(
                    aws_secrets_manager::AwsSecretsManagerRemote::new(config, cfg.clone(), runner),
                ),
                TypedRemoteConfig::Bitwarden(cfg) => {
                    Box::new(bitwarden::BitwardenRemote::new(config, cfg.clone(), runner))
                }
                TypedRemoteConfig::Vault(cfg) => Box::new(
                    hashicorp_vault::HashicorpVaultRemote::new(config, cfg.clone(), runner),
                ),
                TypedRemoteConfig::S3(cfg) => {
                    Box::new(s3::S3Remote::new(config, cfg.clone(), runner))
                }
                TypedRemoteConfig::Gcp(cfg) => Box::new(
                    gcp_secret_manager::GcpSecretManagerRemote::new(config, cfg.clone(), runner),
                ),
                TypedRemoteConfig::Azure(cfg) => Box::new(
                    azure_key_vault::AzureKeyVaultRemote::new(config, cfg.clone(), runner),
                ),
                TypedRemoteConfig::Doppler(cfg) => {
                    Box::new(doppler::DopplerRemote::new(cfg.clone(), runner))
                }
                TypedRemoteConfig::Infisical(cfg) => {
                    Box::new(infisical::InfisicalRemote::new(cfg.clone(), runner))
                }
                TypedRemoteConfig::Sops(cfg) => {
                    Box::new(sops::SopsRemote::new(config, cfg.clone(), runner))
                }
            };
            RemoteCandidate { remote, ok_message }
        })
        .collect()
}

/// Render remote health with animated spinners, returning health results.
///
/// Creates candidates from config, runs `run_preflight_section()` with the
/// given section name, and converts results to `Vec<RemoteHealth>`.
pub fn render_remote_health(
    config: &Config,
    runner: &dyn CommandRunner,
    section_name: &str,
) -> Vec<RemoteHealth> {
    let candidates = remote_candidates(config, runner);
    let items: Vec<&dyn PreflightItem> =
        candidates.iter().map(|c| c as &dyn PreflightItem).collect();
    let results = crate::targets::run_preflight_section(&items, section_name);
    candidates
        .iter()
        .zip(results)
        .map(|(c, (ok, msg))| RemoteHealth {
            name: c.remote.name().to_string(),
            status: if ok {
                crate::targets::HealthStatus::Ok(msg)
            } else {
                crate::targets::HealthStatus::Failed(msg)
            },
        })
        .collect()
}

/// Check the health of all configured remotes without filtering.
/// Returns one entry per configured remote with preflight pass/fail.
/// Runs all preflight checks in parallel.
#[cfg(test)]
fn check_remote_health(config: &Config, runner: &dyn CommandRunner) -> Vec<RemoteHealth> {
    let candidates = remote_candidates(config, runner);
    if candidates.is_empty() {
        return Vec::new();
    }

    let items: Vec<&dyn PreflightItem> =
        candidates.iter().map(|c| c as &dyn PreflightItem).collect();
    let results = crate::targets::run_preflight_section(&items, "Remotes");
    candidates
        .iter()
        .zip(results)
        .map(|(c, (ok, msg))| RemoteHealth {
            name: c.remote.name().to_string(),
            status: if ok {
                crate::targets::HealthStatus::Ok(msg)
            } else {
                crate::targets::HealthStatus::Failed(msg)
            },
        })
        .collect()
}

/// Build all configured remotes from the config.
/// Runs preflight checks in parallel and filters out remotes that fail, printing warnings.
pub fn build_remotes<'a>(
    config: &'a Config,
    runner: &'a dyn CommandRunner,
) -> Vec<Box<dyn SyncRemote + 'a>> {
    let candidates = remote_candidates(config, runner);
    if candidates.is_empty() {
        return Vec::new();
    }

    let items: Vec<&dyn PreflightItem> =
        candidates.iter().map(|c| c as &dyn PreflightItem).collect();
    let results = crate::targets::run_preflight_section(&items, "Remotes");

    // Emit security warnings for passing remotes
    let mut security_warnings: Vec<String> = Vec::new();
    for (i, (ok, _)) in results.iter().enumerate() {
        if *ok {
            let remote = &candidates[i].remote;
            if remote.passes_value_as_cli_arg() {
                security_warnings.push(format!(
                    "{}: secret values are passed as CLI args and may be visible in local process listings",
                    remote.name()
                ));
            }
            if remote.uses_cleartext_format() {
                security_warnings.push(format!(
                    "{}: secrets are stored in cleartext — set `format: encrypted` to protect them at rest",
                    remote.name()
                ));
            }
        }
    }

    for warning in &security_warnings {
        let _ = cliclack::log::warning(warning);
    }

    // Filter to passing remotes
    candidates
        .into_iter()
        .zip(results)
        .filter_map(|(c, (ok, _))| if ok { Some(c.remote) } else { None })
        .collect()
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::targets::{CommandOpts, CommandOutput};
    use crate::test_support::ErrorCommandRunner;

    struct DummyRunner;
    impl CommandRunner for DummyRunner {
        fn run(&self, _program: &str, _args: &[&str], _opts: CommandOpts) -> Result<CommandOutput> {
            Ok(CommandOutput {
                success: true,
                stdout: Vec::new(),
                stderr: Vec::new(),
            })
        }
    }

    #[test]
    fn parse_pulled_secrets_filters_invalid_keys() {
        let mut data = BTreeMap::new();
        data.insert("VALID_KEY".to_string(), "val1".to_string());
        data.insert("invalid-key".to_string(), "val2".to_string());
        data.insert("also invalid!".to_string(), "val3".to_string());
        data.insert("ANOTHER_VALID".to_string(), "val4".to_string());
        data.insert(ESK_VERSION_KEY.to_string(), "5".to_string());

        let (composite, version) = parse_pulled_secrets(data, "dev");
        assert_eq!(version, 5);
        assert_eq!(composite.len(), 2);
        assert!(composite.contains_key("VALID_KEY:dev"));
        assert!(composite.contains_key("ANOTHER_VALID:dev"));
        assert!(!composite.contains_key("invalid-key:dev"));
        assert!(!composite.contains_key("also invalid!:dev"));
    }

    #[test]
    fn parse_pulled_secrets_all_invalid_keys() {
        let mut data = BTreeMap::new();
        data.insert("bad-key".to_string(), "val".to_string());
        data.insert(ESK_VERSION_KEY.to_string(), "1".to_string());

        let (composite, version) = parse_pulled_secrets(data, "dev");
        assert_eq!(version, 1);
        assert!(composite.is_empty());
    }

    #[test]
    fn build_remotes_empty_config() {
        let dir = tempfile::tempdir().unwrap();
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, "project: x\nenvironments: [dev]").unwrap();
        let config = Config::load(&path).unwrap();
        let runner = DummyRunner;
        let built_remotes = build_remotes(&config, &runner);
        assert!(built_remotes.is_empty());
    }

    #[test]
    fn build_remotes_with_onepassword() {
        let dir = tempfile::tempdir().unwrap();
        let yaml = r"
project: x
environments: [dev]
remotes:
  1password:
    vault: V
    item_pattern: test
";
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();
        let runner = DummyRunner;
        let built_remotes = build_remotes(&config, &runner);
        assert_eq!(built_remotes.len(), 1);
        assert_eq!(built_remotes[0].name(), "1password");
    }

    #[test]
    fn build_remotes_with_cloud_file() {
        let dir = tempfile::tempdir().unwrap();
        let cloud_dir = tempfile::tempdir().unwrap();
        let yaml = format!(
            r"
project: x
environments: [dev]
remotes:
  dropbox:
    type: cloud_file
    path: {}
    format: encrypted
",
            cloud_dir.path().display()
        );
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();
        let runner = DummyRunner;
        let built_remotes = build_remotes(&config, &runner);
        assert_eq!(built_remotes.len(), 1);
        assert_eq!(built_remotes[0].name(), "dropbox");
    }

    #[test]
    fn build_remotes_filters_failing_preflight() {
        let dir = tempfile::tempdir().unwrap();
        // onepassword will fail (runner fails), cloud_file with existing dir will pass
        let cloud_dir = tempfile::tempdir().unwrap();
        let yaml = format!(
            r"
project: x
environments: [dev]
remotes:
  1password:
    vault: V
    item_pattern: test
  testcloud:
    type: cloud_file
    path: {}
    format: cleartext
",
            cloud_dir.path().display()
        );
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();

        let runner = ErrorCommandRunner::new("not found");
        let built_remotes = build_remotes(&config, &runner);
        // onepassword fails preflight, cloud_file with existing dir passes
        assert_eq!(built_remotes.len(), 1);
        assert_eq!(built_remotes[0].name(), "testcloud");
    }

    #[test]
    fn build_remotes_creates_cloud_file_missing_dir() {
        let dir = tempfile::tempdir().unwrap();
        let cloud_dir = dir.path().join("new-cloud-dir");
        let yaml = format!(
            r"
project: x
environments: [dev]
remotes:
  testcloud:
    type: cloud_file
    path: {}
    format: cleartext
",
            cloud_dir.display()
        );
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();
        let runner = DummyRunner;
        let built_remotes = build_remotes(&config, &runner);
        assert_eq!(built_remotes.len(), 1);
        assert!(cloud_dir.is_dir());
    }

    #[test]
    fn check_remote_health_op_ok() {
        let dir = tempfile::tempdir().unwrap();
        let yaml = r"
project: x
environments: [dev]
remotes:
  1password:
    vault: V
    item_pattern: test
";
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();

        let health = check_remote_health(&config, &DummyRunner);
        assert_eq!(health.len(), 1);
        assert!(health[0].status.is_ok());
        assert_eq!(health[0].name, "1password");
    }

    #[test]
    fn check_remote_health_op_fails() {
        let dir = tempfile::tempdir().unwrap();
        let yaml = r"
project: x
environments: [dev]
remotes:
  1password:
    vault: V
    item_pattern: test
";
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();

        let runner = ErrorCommandRunner::new("op not found");
        let health = check_remote_health(&config, &runner);
        assert_eq!(health.len(), 1);
        assert!(!health[0].status.is_ok());
        assert!(health[0].status.message().contains("op) is not installed"));
    }

    #[test]
    fn check_remote_health_no_remotes() {
        let dir = tempfile::tempdir().unwrap();
        let yaml = "project: x\nenvironments: [dev]";
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();

        let health = check_remote_health(&config, &DummyRunner);
        assert!(health.is_empty());
    }

    #[test]
    fn build_remotes_mixed() {
        let dir = tempfile::tempdir().unwrap();
        let cloud_dir1 = tempfile::tempdir().unwrap();
        let cloud_dir2 = tempfile::tempdir().unwrap();
        let yaml = format!(
            r"
project: x
environments: [dev]
remotes:
  1password:
    vault: V
    item_pattern: test
  dropbox:
    type: cloud_file
    path: {}
    format: encrypted
  gdrive:
    type: cloud_file
    path: {}
    format: cleartext
",
            cloud_dir1.path().display(),
            cloud_dir2.path().display()
        );
        let path = dir.path().join("esk.yaml");
        std::fs::write(&path, yaml).unwrap();
        let config = Config::load(&path).unwrap();
        let runner = DummyRunner;
        let built_remotes = build_remotes(&config, &runner);
        assert_eq!(built_remotes.len(), 3);
    }
}