ssmm 0.6.0

AWS SSM Parameter Store helper for team-scoped .env sync (systemd friendly)
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
use anyhow::{Context, Result, anyhow, bail};
use aws_sdk_ssm::Client;
use aws_sdk_ssm::types::ParameterType;
use colored::Colorize;
use std::collections::HashSet;
use std::fmt::Write as _;
use std::path::{Path, PathBuf};

use crate::app::app_prefix;
use crate::cli::{MigrateToExecArgs, OnboardArgs};
use crate::config::prefix_root;
use crate::env_map::{parse_tags, read_env_file};
use crate::ssm::{
    TypeReason, build_param_name, build_plain_secure_sets, get_parameters_by_path, resolve_type,
};
use crate::systemd::{SystemdScope, build_drop_in};

use super::migrate_to_exec::cmd_migrate_to_exec;
use super::put::put_kvs;

struct OnboardPlan<'a> {
    app: &'a str,
    prefix: &'a str,
    kvs: &'a [(String, String)],
    plain_all: bool,
    plain_keys: &'a HashSet<String>,
    secure_keys: &'a HashSet<String>,
    extra_tags: &'a [(String, String)],
    existing_collisions: &'a [String],
    overwrite: bool,
    drop_in_path: &'a Path,
    drop_in_content: &'a str,
    revert_cmd: &'a str,
}

/// Pure formatter for onboard dry-run output. No `Client`, no SSM calls —
/// callable from tests to verify the "never leak values to stdout" property.
fn format_onboard_plan(plan: &OnboardPlan) -> String {
    let mut out = String::new();
    let _ = writeln!(
        out,
        "# dry-run: onboard app={} with {} key(s)",
        plan.app,
        plan.kvs.len()
    );
    let _ = writeln!(out);

    if !plan.existing_collisions.is_empty() {
        let verb = if plan.overwrite {
            "WILL OVERWRITE"
        } else {
            "WOULD CONFLICT WITH"
        };
        let _ = writeln!(
            out,
            "# {} {} existing SSM key(s):",
            verb,
            plan.existing_collisions.len()
        );
        for n in plan.existing_collisions {
            let _ = writeln!(out, "#   {}", n);
        }
        let _ = writeln!(out);
    }

    let _ = writeln!(out, "# [1/2] put {} key(s) to {}:", plan.kvs.len(), plan.prefix);
    let tag_str = if plan.extra_tags.is_empty() {
        format!("[app={}]", plan.app)
    } else {
        let extras = plan
            .extra_tags
            .iter()
            .map(|(k, v)| format!("{}={}", k, v))
            .collect::<Vec<_>>()
            .join(", ");
        format!("[app={}, {}]", plan.app, extras)
    };
    for (k, v) in plan.kvs {
        let name = build_param_name(plan.prefix, k);
        let (ptype, reason) = resolve_type(k, plan.plain_all, plan.plain_keys, plan.secure_keys);
        let type_label = match ptype {
            ParameterType::SecureString => "SecureString",
            _ => "String",
        };
        // 値そのものは絶対に出さない (value-leak 回避)。len() のみ出力。
        // env key (UPPER_SNAKE) と SSM name (kebab-case) の両方を出すと、
        // 新規ユーザーに命名規則が見え、mapping 誤認のリスクを減らせる。
        let _ = writeln!(
            out,
            "  + {}{} ({} [{}], len={}) {}",
            k,
            name,
            type_label,
            TypeReason::label(reason),
            v.len(),
            tag_str
        );
    }
    let _ = writeln!(out);

    let _ = writeln!(
        out,
        "# [2/2] write drop-in to {} + systemctl daemon-reload",
        plan.drop_in_path.display()
    );
    let _ = writeln!(out, "# revert: {}", plan.revert_cmd);
    let _ = writeln!(out);
    let _ = write!(out, "{}", plan.drop_in_content);

    let _ = writeln!(out);
    let _ = writeln!(out, "# apply with: `ssmm onboard ... --apply`");
    out
}

pub async fn cmd_onboard(client: &Client, args: OnboardArgs) -> Result<()> {
    let OnboardArgs {
        unit,
        app,
        env,
        exec_cmd,
        plain_all,
        plain_keys,
        secure_keys,
        tags: raw_tags,
        system,
        keep_env_files,
        pre_execs,
        ssmm_bin,
        overwrite,
        apply,
    } = args;
    let prefix = app_prefix(&app);

    // Collision detection (below) must run against the SAME filtered set that
    // put_kvs will actually put. Filter empty values here so a trailing FOO=
    // in the .env doesn't show up as a spurious "would overwrite".
    let mut kvs = read_env_file(&env)?;
    let before = kvs.len();
    kvs.retain(|(_, v)| !v.is_empty());
    if kvs.len() < before {
        eprintln!(
            "  ({} key(s) skipped due to empty value)",
            before - kvs.len()
        );
    }
    if kvs.is_empty() {
        bail!("no key=value in {} after filtering empty values", env.display());
    }

    let (plain_set, secure_set) = build_plain_secure_sets(plain_keys, secure_keys)?;
    let extra_tags = parse_tags(&raw_tags)?;
    if extra_tags.iter().any(|(k, _)| k == "app") {
        bail!("`app` tag is reserved; do not pass --tag app=...");
    }

    // Collision check runs ALWAYS, including under --overwrite, so the dry-run
    // "WILL OVERWRITE N keys" banner shows destructive intent before --apply.
    let desired: HashSet<String> = kvs
        .iter()
        .map(|(k, _)| build_param_name(&prefix, k))
        .collect();
    let existing = get_parameters_by_path(client, &prefix).await?;
    let mut collisions: Vec<String> = existing
        .iter()
        .filter_map(|p| p.name())
        .filter(|n| desired.contains(*n))
        .map(|n| n.to_string())
        .collect();
    collisions.sort();

    if !collisions.is_empty() && !overwrite {
        bail!(
            "{} existing SSM key(s) under {} would be overwritten:\n  {}\n\n\
             Pass --overwrite to replace them (values will be SILENTLY replaced),\n\
             or `ssmm delete {} -r` first if you want a clean slate.",
            collisions.len(),
            prefix,
            collisions.join("\n  "),
            app
        );
    }

    let resolved_ssmm_bin = ssmm_bin
        .clone()
        .or_else(|| {
            std::env::var("HOME")
                .ok()
                .map(|h| PathBuf::from(h).join(".cargo/bin/ssmm"))
        })
        .ok_or_else(|| anyhow!("cannot resolve default ssmm bin path (HOME unset)"))?;
    if !resolved_ssmm_bin.is_absolute() {
        bail!("--ssmm-bin must be an absolute path: {:?}", resolved_ssmm_bin);
    }
    let scope = if system {
        SystemdScope::System
    } else {
        SystemdScope::User
    };
    let drop_in_dir = scope.drop_in_dir(&unit)?;
    let drop_in_path = drop_in_dir.join("exec-mode.conf");
    let drop_in_content = build_drop_in(
        &app,
        &exec_cmd,
        &keep_env_files,
        &pre_execs,
        &resolved_ssmm_bin,
        prefix_root(),
    );

    if !apply {
        let revert_cmd = format!(
            "rm {} && systemctl {} daemon-reload",
            drop_in_path.display(),
            scope.as_cli_flag()
        );
        let plan = OnboardPlan {
            app: &app,
            prefix: &prefix,
            kvs: &kvs,
            plain_all,
            plain_keys: &plain_set,
            secure_keys: &secure_set,
            extra_tags: &extra_tags,
            existing_collisions: &collisions,
            overwrite,
            drop_in_path: &drop_in_path,
            drop_in_content: &drop_in_content,
            revert_cmd: &revert_cmd,
        };
        print!("{}", format_onboard_plan(&plan));
        return Ok(());
    }

    println!(
        "{} putting {} key(s) to SSM (app={})",
        "[1/2]".bold(),
        kvs.len(),
        app
    );
    put_kvs(
        client,
        &kvs,
        &app,
        plain_all,
        &plain_set,
        &secure_set,
        &extra_tags,
    )
    .await
    .context("SSM put failed; systemd step was not attempted")?;

    println!();
    println!(
        "{} writing systemd drop-in + daemon-reload",
        "[2/2]".bold()
    );
    cmd_migrate_to_exec(MigrateToExecArgs {
        unit,
        app: app.clone(),
        exec_cmd,
        system,
        keep_env_files,
        pre_execs,
        ssmm_bin,
        apply: true,
    })
    .map_err(|e| {
        anyhow!(
            "SSM values WERE written, but systemd step failed: {}\n\
             Revert SSM with `ssmm delete {} -r` if you need to abort the onboarding.",
            e,
            app
        )
    })?;

    Ok(())
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::cli::{Cli, Command};
    use clap::Parser;

    #[test]
    fn onboard_parses_minimal() {
        let cli = Cli::try_parse_from([
            "ssmm",
            "--prefix",
            "/myteam",
            "onboard",
            "--unit",
            "myapp.service",
            "--app",
            "myapp",
            "--env",
            "/tmp/myapp.env",
            "--exec-cmd",
            "/usr/bin/echo hi",
        ])
        .expect("parse");
        match cli.command {
            Command::Onboard(args) => {
                assert_eq!(args.unit, "myapp.service");
                assert_eq!(args.app, "myapp");
                assert_eq!(args.env.to_str().unwrap(), "/tmp/myapp.env");
                assert_eq!(args.exec_cmd, "/usr/bin/echo hi");
                assert!(!args.apply, "--apply defaults to false");
                assert!(!args.overwrite, "--overwrite defaults to false");
            }
            _ => panic!("expected Onboard variant"),
        }
    }

    #[test]
    fn format_onboard_plan_never_leaks_values() {
        let secret = "super-secret-value-1234567890abcdef";
        let kvs = vec![
            ("API_KEY".to_string(), secret.to_string()),
            ("DB_HOST".to_string(), "localhost".to_string()),
        ];
        let empty_set = HashSet::new();
        let plan = OnboardPlan {
            app: "myapp",
            prefix: "/myteam/myapp",
            kvs: &kvs,
            plain_all: false,
            plain_keys: &empty_set,
            secure_keys: &empty_set,
            extra_tags: &[],
            existing_collisions: &[],
            overwrite: false,
            drop_in_path: Path::new("/home/me/.config/systemd/user/myapp.service.d/exec-mode.conf"),
            drop_in_content: "[Service]\nExecStart=/ssmm exec --app myapp -- /bin/true\n",
            revert_cmd: "rm ... && systemctl --user daemon-reload",
        };
        let out = format_onboard_plan(&plan);
        assert!(
            !out.contains(secret),
            "dry-run output leaked API_KEY value; full output:\n{}",
            out
        );
        // "localhost" is DB_HOST's value. Also must not appear in the plan.
        assert!(
            !out.contains("=localhost"),
            "dry-run output leaked DB_HOST value; full output:\n{}",
            out
        );
        assert!(out.contains("API_KEY"), "expected key name in output");
        assert!(out.contains("DB_HOST"), "expected key name in output");
        assert!(
            out.contains("SecureString"),
            "API_KEY should auto-detect as SecureString"
        );
        assert!(
            out.contains(&format!("len={}", secret.len())),
            "should show length instead of value"
        );
    }

    #[test]
    fn format_onboard_plan_collision_overwrite_warning() {
        let kvs = vec![("FOO".to_string(), "bar".to_string())];
        let collisions = vec![
            "/myteam/myapp/foo".to_string(),
            "/myteam/myapp/legacy-key".to_string(),
        ];
        let empty_set = HashSet::new();
        let plan = OnboardPlan {
            app: "myapp",
            prefix: "/myteam/myapp",
            kvs: &kvs,
            plain_all: false,
            plain_keys: &empty_set,
            secure_keys: &empty_set,
            extra_tags: &[],
            existing_collisions: &collisions,
            overwrite: true,
            drop_in_path: Path::new("/tmp/x.conf"),
            drop_in_content: "",
            revert_cmd: "rm /tmp/x.conf",
        };
        let out = format_onboard_plan(&plan);
        assert!(
            out.contains("WILL OVERWRITE 2"),
            "overwrite=true with collisions should say 'WILL OVERWRITE', got:\n{}",
            out
        );
        assert!(out.contains("/myteam/myapp/foo"));
        assert!(out.contains("/myteam/myapp/legacy-key"));
    }

    #[test]
    fn format_onboard_plan_collision_without_overwrite_warning() {
        let kvs = vec![("FOO".to_string(), "bar".to_string())];
        let collisions = vec!["/myteam/myapp/foo".to_string()];
        let empty_set = HashSet::new();
        let plan = OnboardPlan {
            app: "myapp",
            prefix: "/myteam/myapp",
            kvs: &kvs,
            plain_all: false,
            plain_keys: &empty_set,
            secure_keys: &empty_set,
            extra_tags: &[],
            existing_collisions: &collisions,
            overwrite: false,
            drop_in_path: Path::new("/tmp/x.conf"),
            drop_in_content: "",
            revert_cmd: "rm /tmp/x.conf",
        };
        let out = format_onboard_plan(&plan);
        assert!(
            out.contains("WOULD CONFLICT WITH 1"),
            "overwrite=false should say 'WOULD CONFLICT WITH', got:\n{}",
            out
        );
    }
}