diff --git a/README.md b/README.md index 0faa745..2c48774 100644 --- a/README.md +++ b/README.md @@ -152,14 +152,15 @@ Identity: `SHOPWARE_SHOP_ID`, `SHOPWARE_DEPLOY_ENV`, optional ### env init Does not overwrite the whole `.env`. Does not invent MYSQL passwords or -`APP_URL`. `--shop-id` is required unless `SHOPWARE_SHOP_ID` is already set. -`--env` defaults to `live`. `--vps` comments out `COMPOSE_PROJECT_NAME`. -`.env` may still be missing; missing keys are merged from `.env.example`. +`APP_URL`. Does not generate `APP_SECRET` (`shopware-cli project create` +already writes it). `--shop-id` is required unless `SHOPWARE_SHOP_ID` is +already set. `--env` defaults to `live`. `--vps` comments out +`COMPOSE_PROJECT_NAME`. `.env` may still be missing; missing keys are +merged from `.env.example`. ```bash fyrst-cli shopware env init --shop-id acme --vps --dry-run fyrst-cli shopware env init --shop-id acme --env live --vps --image ghcr.io/example/acme -fyrst-cli shopware env init --shop-id acme --generate-app-secret ``` ### db import diff --git a/docs/command-matrix.md b/docs/command-matrix.md index 3eb86ca..493a804 100644 --- a/docs/command-matrix.md +++ b/docs/command-matrix.md @@ -29,7 +29,7 @@ those scripts. Use the CLI column. | Historical overlay (removed) | CLI | Nested verbs | Flags | Status | | --- | --- | --- | --- | --- | -| `deploy/init-env.sh` | `fyrst-cli shopware env init` | `init` | `--shop-id`, `--env`, `--image`, `--vps`, `--generate-app-secret`, `--dry-run` | **implemented** | +| `deploy/init-env.sh` | `fyrst-cli shopware env init` | `init` | `--shop-id`, `--env`, `--image`, `--vps`, `--dry-run` | **implemented** | | `deploy/vps-release.sh` | `fyrst-cli shopware deploy release` | `release` | `--dry-run`, `--skip-pull` | **implemented** | | `deploy/vps-rollback.sh` | `fyrst-cli shopware deploy rollback` | `rollback` | `--dry-run`, `--skip-pull` | **implemented** | | `restore_db_*` (sync-runtime) | `fyrst-cli shopware db import` | `import` | `--file`, `--dry-run`, `--allow-live` | **implemented** | @@ -90,7 +90,7 @@ Rewrite: `APP_URL` only. CI `VPS_*` secrets stay secrets — they are not shop ## Exact env init CLI ```text -fyrst-cli shopware env init [--shop-id SLUG] [--env live|staging|playground|dev] [--image REPO] [--vps] [--generate-app-secret] [--dry-run] +fyrst-cli shopware env init [--shop-id SLUG] [--env live|staging|playground|dev] [--image REPO] [--vps] [--dry-run] ``` ## Exact deploy release CLI @@ -164,7 +164,8 @@ volumes (`media`, `files`, `thumbnail`, `theme`, `sitemap`) restore from Finish shop-root `.env` after `shopware-cli project create` + Flex. Historically mapped to overlay `deploy/init-env.sh` (removed). Does **not** overwrite the -whole file, invent `MYSQL_*` passwords, or set `APP_URL`. This is **not** a +whole file, invent `MYSQL_*` passwords, set `APP_URL`, or generate +`APP_SECRET` (`shopware-cli project create` writes that). This is **not** a dump command. Resolves shop root (`COMPOSE_DIR` if set, else walk from cwd for `.env` or @@ -180,14 +181,13 @@ shell expansion**), same parser as import (`src/shopware/envfile.rs`). 4. `--image` sets `IMAGE` (no whitespace). Unset leaves `IMAGE` as-is. 5. `--vps` comments out uncommented `COMPOSE_PROJECT_NAME=…` lines (create footgun on a VPS). Does not leave an empty `COMPOSE_PROJECT_NAME=`. -6. `--generate-app-secret`: `openssl rand -hex 32` only if `APP_SECRET` is - empty; the value is never printed. -7. `--dry-run` prints the summary and does not write `.env`. -8. After a real write: `chmod 600 .env`. +6. `--dry-run` prints the summary and does not write `.env`. +7. After a real write: `chmod 600 .env`. -Refuses a bash xtrace equivalent (`SHELLOPTS=xtrace` / `BASH_XTRACEFD`) so -credentials in `.env` cannot leak via trace. Summary never prints secret -values (MYSQL passwords, `APP_SECRET`). +Does not generate `APP_SECRET` (no dedicated read/write of that key). Refuses a +bash xtrace equivalent (`SHELLOPTS=xtrace` / `BASH_XTRACEFD`) so credentials +in `.env` cannot leak via trace. Summary never prints secret values (MYSQL +passwords). ## Exact deploy rollback CLI @@ -555,7 +555,7 @@ This CLI reads shop identity and secrets from the environment / shop-root | Area | Variables | | --- | --- | | Shop identity | `COMPOSE_DIR`, `SHOPWARE_SHOP_ID`, `SHOPWARE_DEPLOY_ENV`, `SHOPWARE_DATA_BASE`, `SHOPWARE_DATA_ROOT`, `COMPOSE_PROJECT_NAME` | -| Env init | `APP_SECRET` (optional `--generate-app-secret`; never logged), `IMAGE` | +| Env init | `IMAGE` (`APP_SECRET` is Shopware's; `shopware-cli project create` writes it) | | Import | `MYSQL_DATABASE`, `DATABASE_URL`, `SHOPWARE_ALLOW_LIVE_RESTORE` | | Restore volumes | `SHOPWARE_DATA_ROOT`, `SHOPWARE_DATA_BASE` | | Rewrite | `APP_URL`, `IMAGE` | diff --git a/src/cli.rs b/src/cli.rs index 76eec9b..46973cc 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -94,9 +94,9 @@ pub enum Command { deploy (VPS compose), db (SQL import), sync (between environments / workdir), \ backup (off-host disaster recovery).\n\n\ `env init` is implemented: finish shop-root `.env` after create + Flex (merge missing \ -keys from `.env.example`, shop id / deploy env, optional IMAGE / APP_SECRET, `--vps` \ -comments COMPOSE_PROJECT_NAME). `--dry-run` prints the plan and does not write. \ -Passwords and APP_SECRET are never printed.\n\n\ +keys from `.env.example`, shop id / deploy env, optional IMAGE, `--vps` \ +comments COMPOSE_PROJECT_NAME). Does not generate APP_SECRET. `--dry-run` prints the \ +plan and does not write. Passwords are never printed.\n\n\ `db import` is implemented: MySQL/MariaDB client import (Compose `mysql` exec, else a \ one-shot client image for DATABASE_URL). `sync apply` uses that same import module, \ restores bind-mount volumes from --snapshot-dir, stops/starts web/worker/scheduler, and \ @@ -181,13 +181,13 @@ impl DeployEnv { #[derive(Debug, Args)] #[command( - after_help = "Does not overwrite the whole .env. Does not invent MYSQL passwords or APP_URL.\n\n\ + after_help = "Does not overwrite the whole .env. Does not invent MYSQL passwords or APP_URL. \ +Does not generate APP_SECRET (shopware-cli project create writes that).\n\n\ Environment:\n \ COMPOSE_DIR Shop checkout (default: walk from cwd for .env / .env.example + deploy/)\n\n\ Examples:\n \ fyrst-cli shopware env init --shop-id acme\n \ fyrst-cli shopware env init --shop-id acme --env live --vps --image ghcr.io/example/acme\n \ - fyrst-cli shopware env init --shop-id acme --generate-app-secret\n \ fyrst-cli shopware env init --shop-id acme --vps --dry-run\n" )] pub struct InitEnvArgs { @@ -207,10 +207,6 @@ pub struct InitEnvArgs { #[arg(long)] pub vps: bool, - /// Set APP_SECRET with openssl rand -hex 32 if empty (value is never printed) - #[arg(long = "generate-app-secret")] - pub generate_app_secret: bool, - /// Print the summary; do not write .env #[arg(long)] pub dry_run: bool, @@ -515,7 +511,6 @@ mod tests { "--image", "ghcr.io/example/acme", "--vps", - "--generate-app-secret", "--dry-run", ]) .unwrap(); @@ -527,13 +522,31 @@ mod tests { assert_eq!(op.env, Some(DeployEnv::Staging)); assert_eq!(op.image.as_deref(), Some("ghcr.io/example/acme")); assert!(op.vps); - assert!(op.generate_app_secret); assert!(op.dry_run); } other => panic!("unexpected parse: {other:?}"), } } + #[test] + fn env_init_rejects_generate_app_secret() { + let err = Cli::try_parse_from([ + "fyrst-cli", + "shopware", + "env", + "init", + "--shop-id", + "acme", + "--generate-app-secret", + ]) + .unwrap_err(); + let text = err.to_string(); + assert!( + text.contains("unexpected argument") || text.contains("unexpected"), + "{text}" + ); + } + #[test] fn deploy_rollback_parses_flags() { let cli = Cli::try_parse_from([ diff --git a/src/shopware/init_env.rs b/src/shopware/init_env.rs index 2a362cf..6b5358d 100644 --- a/src/shopware/init_env.rs +++ b/src/shopware/init_env.rs @@ -1,7 +1,9 @@ //! `fyrst-cli shopware env init` — finish shop-root `.env` after create + Flex. //! -//! Behaviour matches fyrst-dev/recipes `deploy/init-env.sh`. Does not overwrite -//! the whole file, does not invent MYSQL passwords or `APP_URL`, and never +//! Sets fyrst identity (shop id, deploy env, optional IMAGE, `--vps`) and +//! merges missing keys from `.env.example`. Does not overwrite the whole file, +//! does not invent MYSQL passwords or `APP_URL`, and does not generate or +//! rewrite `APP_SECRET` (`shopware-cli project create` writes that). Never //! prints secrets. This is not a dump command. use super::env::resolve_compose_dir_init; @@ -14,14 +16,6 @@ use std::collections::HashMap; use std::fmt::Write as _; use std::fs; use std::path::{Path, PathBuf}; -use std::process::Command; - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(crate) enum SecretAction { - None, - SkippedExisting, - WillGenerate, -} pub(crate) struct Plan { pub compose_dir: PathBuf, @@ -35,7 +29,6 @@ pub(crate) struct Plan { pub deploy_env: String, pub deploy_env_changed: bool, pub image_set: Option, - pub secret_action: SecretAction, pub vps: bool, pub vps_commented: usize, } @@ -53,7 +46,6 @@ impl std::fmt::Debug for Plan { .field("deploy_env", &self.deploy_env) .field("deploy_env_changed", &self.deploy_env_changed) .field("image_set", &self.image_set) - .field("secret_action", &self.secret_action) .field("vps", &self.vps) .field("vps_commented", &self.vps_commented) .finish_non_exhaustive() @@ -71,7 +63,7 @@ pub fn run(args: InitEnvArgs) -> Result<(), Error> { refuse_xtrace()?; let process_env: HashMap = std::env::vars().collect(); let cwd = std::env::current_dir().map_err(|e| Error::fail(format!("cannot read cwd: {e}")))?; - let plan = plan(&process_env, &cwd, &args, openssl_app_secret)?; + let plan = plan(&process_env, &cwd, &args)?; execute(&plan) } @@ -95,7 +87,6 @@ pub(crate) fn plan( process_env: &HashMap, cwd: &Path, args: &InitEnvArgs, - secret_gen: impl FnOnce() -> Result, ) -> Result { let compose_dir = resolve_compose_dir_init(process_env, cwd)?; let env_file = compose_dir.join(".env"); @@ -131,7 +122,6 @@ pub(crate) fn plan( let existing_shop_id = last_value(&contents, "SHOPWARE_SHOP_ID"); let existing_deploy_env = last_value(&contents, "SHOPWARE_DEPLOY_ENV"); - let existing_app_secret = last_value(&contents, "APP_SECRET"); let existing_image = last_value(&contents, "IMAGE"); let shop_id_flag = nonempty_flag(args.shop_id.as_deref(), "--shop-id")?; @@ -162,19 +152,6 @@ pub(crate) fn plan( } } - let mut secret_action = SecretAction::None; - if args.generate_app_secret { - if !existing_app_secret.is_empty() { - secret_action = SecretAction::SkippedExisting; - } else { - secret_action = SecretAction::WillGenerate; - if !args.dry_run { - let secret = secret_gen()?; - contents = set_key(&contents, "APP_SECRET", &secret); - } - } - } - contents = set_key(&contents, "SHOPWARE_SHOP_ID", &shop_id); contents = set_key(&contents, "SHOPWARE_DEPLOY_ENV", &deploy_env); let mut image_set = None; @@ -206,7 +183,6 @@ pub(crate) fn plan( deploy_env, deploy_env_changed, image_set, - secret_action, vps: args.vps, vps_commented, }) @@ -256,23 +232,6 @@ fn validate_deploy_env(name: &str) -> Result<(), Error> { } } -fn openssl_app_secret() -> Result { - let out = Command::new("openssl") - .args(["rand", "-hex", "32"]) - .output() - .map_err(|_| Error::fail("--generate-app-secret needs openssl (openssl rand -hex 32)."))?; - if !out.status.success() { - return Err(Error::fail( - "--generate-app-secret needs openssl (openssl rand -hex 32).", - )); - } - let secret = String::from_utf8_lossy(&out.stdout).trim().to_string(); - if secret.len() != 64 || !secret.bytes().all(|c| c.is_ascii_hexdigit()) { - return Err(Error::fail("openssl rand -hex 32 did not return 32 bytes.")); - } - Ok(secret) -} - fn execute(plan: &Plan) -> Result<(), Error> { if !plan.dry_run { fs::write(&plan.env_file, &plan.contents) @@ -320,19 +279,6 @@ pub(crate) fn summary_text(plan: &Plan) -> String { let _ = writeln!(s, " set IMAGE={image}"); changes += 1; } - match plan.secret_action { - SecretAction::None => {} - SecretAction::SkippedExisting => { - let _ = writeln!(s, " APP_SECRET already set; skipped --generate-app-secret"); - } - SecretAction::WillGenerate => { - let _ = writeln!( - s, - " set APP_SECRET (openssl rand -hex 32; value not printed)" - ); - changes += 1; - } - } if plan.vps { if plan.vps_commented > 0 { let _ = writeln!( @@ -350,7 +296,7 @@ pub(crate) fn summary_text(plan: &Plan) -> String { } let _ = writeln!( s, - " left unchanged: MYSQL passwords, APP_URL (fill those by hand)" + " left unchanged: MYSQL passwords, APP_URL, APP_SECRET (fill MYSQL/APP_URL by hand)" ); if !plan.dry_run { let _ = writeln!(s, "==> chmod 600 .env"); @@ -424,50 +370,44 @@ mod tests { env: None, image: None, vps: false, - generate_app_secret: false, dry_run: true, } } - fn panic_secret() -> Result { - panic!("openssl generator must not run"); - } - const EXISTING_SECRET: &str = "existing-app-secret-do-not-print-0123456789abcdef"; const MYSQL_PASS: &str = "super-secret-pass"; - const FAKE_SECRET: &str = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; #[test] - fn dry_run_does_not_need_generator_and_summary_hides_passwords() { + fn dry_run_summary_hides_passwords_and_does_not_write() { let shop = TempShop::new("dry"); shop.write_env(&format!( "\ SHOPWARE_SHOP_ID= SHOPWARE_DEPLOY_ENV= MYSQL_PASSWORD={MYSQL_PASS} +APP_SECRET={EXISTING_SECRET} COMPOSE_PROJECT_NAME=sw-shop-acme " )); let mut a = args(); - a.generate_app_secret = true; a.vps = true; a.image = Some("ghcr.io/example/acme".into()); - let p = plan(&process(shop.path()), shop.path(), &a, panic_secret).unwrap(); + let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); assert!(p.dry_run); assert_eq!(p.shop_id, "acme"); assert_eq!(p.deploy_env, "live"); assert_eq!(p.image_set.as_deref(), Some("ghcr.io/example/acme")); - assert_eq!(p.secret_action, SecretAction::WillGenerate); assert_eq!(p.vps_commented, 1); - assert_eq!(last_value(p.contents(), "APP_SECRET"), ""); + assert_eq!(last_value(p.contents(), "APP_SECRET"), EXISTING_SECRET); assert!(!has_key(p.contents(), "COMPOSE_PROJECT_NAME")); let text = summary_text(&p); assert!(text.contains("DRY-RUN (no write)")); assert!(text.contains("set SHOPWARE_SHOP_ID=acme")); assert!(text.contains("set SHOPWARE_DEPLOY_ENV=live")); assert!(text.contains("set IMAGE=ghcr.io/example/acme")); - assert!(text.contains("value not printed")); assert!(text.contains("commented 1 COMPOSE_PROJECT_NAME")); + assert!(text.contains("APP_SECRET")); + assert!(!text.contains("generate-app-secret")); assert!(!text.contains(MYSQL_PASS)); assert!(!text.contains(EXISTING_SECRET)); assert!( @@ -501,7 +441,7 @@ NEW_FROM_EXAMPLE=1 a.dry_run = false; a.vps = true; a.env = Some(DeployEnv::Staging); - let p = plan(&process(shop.path()), shop.path(), &a, panic_secret).unwrap(); + let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); assert!(p.merged_keys.contains(&"NEW_FROM_EXAMPLE".into())); assert!(!p.merged_keys.contains(&"MYSQL_PASSWORD".into())); assert_eq!(last_value(p.contents(), "SHOPWARE_SHOP_ID"), "acme"); @@ -520,8 +460,8 @@ NEW_FROM_EXAMPLE=1 } #[test] - fn skip_generate_when_secret_exists() { - let shop = TempShop::new("skip-secret"); + fn leaves_existing_app_secret_unchanged() { + let shop = TempShop::new("keep-secret"); shop.write_env(&format!( "\ SHOPWARE_SHOP_ID=acme @@ -532,22 +472,20 @@ MYSQL_PASSWORD={MYSQL_PASS} )); let mut a = args(); a.shop_id = None; - a.generate_app_secret = true; a.dry_run = false; - let p = plan(&process(shop.path()), shop.path(), &a, panic_secret).unwrap(); - assert_eq!(p.secret_action, SecretAction::SkippedExisting); + let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); assert_eq!(last_value(p.contents(), "APP_SECRET"), EXISTING_SECRET); let text = summary_text(&p); - assert!(text.contains("APP_SECRET already set; skipped --generate-app-secret")); assert!(!text.contains(EXISTING_SECRET)); assert!(!text.contains(MYSQL_PASS)); + assert!(!text.contains("generate-app-secret")); assert!(!p.shop_id_changed); assert!(!p.deploy_env_changed); } #[test] - fn generate_secret_on_write_uses_generator_once() { - let shop = TempShop::new("gen"); + fn does_not_generate_empty_app_secret() { + let shop = TempShop::new("empty-secret"); shop.write_env( "\ SHOPWARE_SHOP_ID=acme @@ -557,16 +495,12 @@ MYSQL_PASSWORD=super-secret-pass ); let mut a = args(); a.dry_run = false; - a.generate_app_secret = true; - let p = plan(&process(shop.path()), shop.path(), &a, || { - Ok(FAKE_SECRET.to_string()) - }) - .unwrap(); - assert_eq!(p.secret_action, SecretAction::WillGenerate); - assert_eq!(last_value(p.contents(), "APP_SECRET"), FAKE_SECRET); + let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); + assert_eq!(last_value(p.contents(), "APP_SECRET"), ""); + assert!(p.contents().contains("APP_SECRET=")); let text = summary_text(&p); - assert!(text.contains("value not printed")); - assert!(!text.contains(FAKE_SECRET)); + assert!(!text.contains("set APP_SECRET")); + assert!(!text.contains("generate-app-secret")); assert!(!text.contains("super-secret-pass")); } @@ -576,7 +510,7 @@ MYSQL_PASSWORD=super-secret-pass shop.write_env("SHOPWARE_DEPLOY_ENV=live\n"); let mut a = args(); a.shop_id = None; - let err = plan(&process(shop.path()), shop.path(), &a, panic_secret).unwrap_err(); + let err = plan(&process(shop.path()), shop.path(), &a).unwrap_err(); match err { Error::Fail(m) => { assert!(m.contains("SHOPWARE_SHOP_ID is empty"), "{m}"); @@ -590,7 +524,7 @@ MYSQL_PASSWORD=super-secret-pass fn invalid_slug_and_env_and_image() { let shop = TempShop::new("bad"); shop.write_env("SHOPWARE_SHOP_ID=\nSHOPWARE_DEPLOY_ENV=prod\n"); - let err = plan(&process(shop.path()), shop.path(), &args(), panic_secret).unwrap_err(); + let err = plan(&process(shop.path()), shop.path(), &args()).unwrap_err(); match err { Error::Fail(m) => assert!(m.contains("Invalid --env 'prod'"), "{m}"), other => panic!("{other:?}"), @@ -599,7 +533,7 @@ MYSQL_PASSWORD=super-secret-pass shop.write_env("SHOPWARE_SHOP_ID=\n"); let mut a = args(); a.shop_id = Some("ACME".into()); - let err = plan(&process(shop.path()), shop.path(), &a, panic_secret).unwrap_err(); + let err = plan(&process(shop.path()), shop.path(), &a).unwrap_err(); match err { Error::Fail(m) => assert!(m.contains("Invalid --shop-id 'ACME'"), "{m}"), other => panic!("{other:?}"), @@ -607,7 +541,7 @@ MYSQL_PASSWORD=super-secret-pass a.shop_id = Some("acme".into()); a.image = Some("ghcr.io/example/acme with space".into()); - let err = plan(&process(shop.path()), shop.path(), &a, panic_secret).unwrap_err(); + let err = plan(&process(shop.path()), shop.path(), &a).unwrap_err(); match err { Error::Fail(m) => assert!(m.contains("no whitespace"), "{m}"), other => panic!("{other:?}"), @@ -624,7 +558,7 @@ SHOPWARE_DEPLOY_ENV=live MYSQL_PASSWORD=example-secret ", ); - let p = plan(&process(shop.path()), shop.path(), &args(), panic_secret).unwrap(); + let p = plan(&process(shop.path()), shop.path(), &args()).unwrap(); assert!(p.copied); assert!(p.merged_keys.is_empty()); assert_eq!(last_value(p.contents(), "MYSQL_PASSWORD"), "example-secret"); @@ -640,7 +574,7 @@ MYSQL_PASSWORD=example-secret shop.write_env("SHOPWARE_SHOP_ID=acme\nSHOPWARE_DEPLOY_ENV=playground\n"); let mut a = args(); a.shop_id = None; - let p = plan(&process(shop.path()), shop.path(), &a, panic_secret).unwrap(); + let p = plan(&process(shop.path()), shop.path(), &a).unwrap(); assert_eq!(p.deploy_env, "playground"); assert!(!p.deploy_env_changed); assert!(summary_text(&p).contains("no changes (already up to date)")); diff --git a/tests/init_env.rs b/tests/init_env.rs index c3d7b73..6a3d956 100644 --- a/tests/init_env.rs +++ b/tests/init_env.rs @@ -109,12 +109,19 @@ fn init_env_help_lists_flags() { "--env", "--image", "--vps", - "--generate-app-secret", "--dry-run", "COMPOSE_DIR", ] { assert!(help.contains(needle), "missing {needle} in:\n{help}"); } + assert!( + !help.contains("generate-app-secret"), + "env init --help must not list --generate-app-secret:\n{help}" + ); + assert!( + help.contains("APP_SECRET"), + "env init --help should say APP_SECRET is not generated:\n{help}" + ); assert!( !help.to_ascii_lowercase().contains("wrap dump"), "env init --help must not wrap dump:\n{help}" @@ -129,7 +136,7 @@ fn dry_run_prints_plan_and_does_not_write() { SHOPWARE_SHOP_ID= SHOPWARE_DEPLOY_ENV= MYSQL_PASSWORD={MYSQL_PASS} -APP_SECRET= +APP_SECRET={EXISTING_SECRET} COMPOSE_PROJECT_NAME=sw-shop-acme " ); @@ -142,7 +149,6 @@ COMPOSE_PROJECT_NAME=sw-shop-acme "--vps", "--image", "ghcr.io/example/acme", - "--generate-app-secret", "--dry-run", ], ); @@ -159,7 +165,8 @@ COMPOSE_PROJECT_NAME=sw-shop-acme assert!(log.contains("set SHOPWARE_DEPLOY_ENV=live"), "{log}"); assert!(log.contains("set IMAGE=ghcr.io/example/acme"), "{log}"); assert!(log.contains("commented 1 COMPOSE_PROJECT_NAME"), "{log}"); - assert!(log.contains("value not printed"), "{log}"); + assert!(!log.contains("generate-app-secret"), "{log}"); + assert!(!log.contains("set APP_SECRET"), "{log}"); assert!( log.contains("left unchanged: MYSQL passwords, APP_URL"), "{log}" @@ -239,8 +246,8 @@ NEW_FROM_EXAMPLE=1 } #[test] -fn skip_generate_when_secret_exists() { - let shop = TempShop::new("skip-sec"); +fn leaves_existing_app_secret_unchanged() { + let shop = TempShop::new("keep-sec"); fs::write( shop.path().join(".env"), format!( @@ -253,21 +260,19 @@ MYSQL_PASSWORD={MYSQL_PASS} ), ) .unwrap(); - let out = init_env(shop.path(), &["--generate-app-secret"]); + let out = init_env(shop.path(), &[]); assert_eq!(out.status.code(), Some(0), "stderr={}", stderr(&out)); let log = stdout(&out); - assert!( - log.contains("APP_SECRET already set; skipped --generate-app-secret"), - "{log}" - ); + assert!(!log.contains("generate-app-secret"), "{log}"); + assert!(!log.contains("set APP_SECRET"), "{log}"); assert_no_secrets(&out, &[]); let env = fs::read_to_string(shop.path().join(".env")).unwrap(); assert!(env.contains(&format!("APP_SECRET={EXISTING_SECRET}"))); } #[test] -fn generate_app_secret_writes_hex_and_never_prints_it() { - let shop = TempShop::new("gen"); +fn does_not_generate_empty_app_secret() { + let shop = TempShop::new("empty-sec"); fs::write( shop.path().join(".env"), format!( @@ -279,7 +284,7 @@ MYSQL_PASSWORD={MYSQL_PASS} ), ) .unwrap(); - let out = init_env(shop.path(), &["--generate-app-secret"]); + let out = init_env(shop.path(), &[]); assert_eq!( out.status.code(), Some(0), @@ -288,18 +293,31 @@ MYSQL_PASSWORD={MYSQL_PASS} stdout(&out) ); let log = stdout(&out); - assert!(log.contains("value not printed"), "{log}"); + assert!(!log.contains("generate-app-secret"), "{log}"); + assert!(!log.contains("set APP_SECRET"), "{log}"); + assert_no_secrets(&out, &[]); let env = fs::read_to_string(shop.path().join(".env")).unwrap(); let secret = env .lines() .find_map(|l| l.strip_prefix("APP_SECRET=")) - .unwrap_or(""); - assert_eq!(secret.len(), 64, "expected 32-byte hex, got {secret:?}"); + .unwrap_or("missing"); + assert_eq!( + secret, "", + "env init must not write APP_SECRET, got {secret:?}" + ); +} + +#[test] +fn generate_app_secret_flag_is_rejected() { + let shop = TempShop::new("gone-flag"); + fs::write(shop.path().join(".env"), "SHOPWARE_SHOP_ID=acme\n").unwrap(); + let out = init_env(shop.path(), &["--generate-app-secret"]); + assert_ne!(out.status.code(), Some(0), "stderr={}", stderr(&out)); + let text = combined(&out); assert!( - secret.bytes().all(|c| c.is_ascii_hexdigit()), - "not hex: {secret}" + text.contains("unexpected argument") || text.contains("unexpected"), + "{text}" ); - assert_no_secrets(&out, &[secret]); } #[test]