Skip to content

armgit / ARES

Public Imported from GitHub
0

ARES - authorized red-team engagement automation with dashboard, campaign scope, module orchestration, OPSEC controls, encrypted vault, and reporting.

fix(cli): resolve ares-setup CHANGE_ME infinite loop and add force regeneration (fixes #54)

Mafifrizi8520f7e540bc
diff --git a/ares/cli/typer_main.py b/ares/cli/typer_main.py
index a731a98..e37c755 100644
--- a/ares/cli/typer_main.py
+++ b/ares/cli/typer_main.py
@@ -2213,11 +2213,20 @@ def _setup_admin_password(length: int = 20) -> str:
return "".join(secrets.choice(alphabet) for _ in range(length))
+def _extract_env_value(content: str, key: str) -> str | None:
+ prefix = f"{key}="
+ for line in content.splitlines():
+ line = line.strip()
+ if line.startswith(prefix):
+ return line[len(prefix):].strip()
+ return None
+
+
def _upsert_env_value(template: str, key: str, value: str) -> str:
prefix = f"{key}="
lines = template.splitlines()
for index, line in enumerate(lines):
- if line.startswith(prefix):
+ if line.strip().startswith(prefix):
lines[index] = f"{prefix}{value}"
break
else:
@@ -2230,6 +2239,7 @@ def _run_python_setup(
platform_name: str | None = None,
os_name: str | None = None,
version_info: object | None = None,
+ force: bool = False,
) -> None:
import os
import secrets
@@ -2271,9 +2281,52 @@ def _run_python_setup(
example_path = root_path / ".env.example"
try:
+ admin_password = None
if env_path.exists():
- console.print("[green]OK[/green] .env already exists; leaving it unchanged.")
- admin_password = None
+ existing = env_path.read_text(encoding="utf-8")
+ secret_val = _extract_env_value(existing, "ARES_SECRET_KEY")
+ enc_val = _extract_env_value(existing, "ARES_ENCRYPTION_KEY")
+ admin_val = _extract_env_value(existing, "ARES_DEFAULT_ADMIN_PASSWORD")
+
+ has_placeholders = (
+ "CHANGE_ME" in existing
+ or (admin_val is not None and "YOUR_STRONG_ADMIN_PASSWORD_HERE" in admin_val)
+ )
+ missing_critical = (
+ secret_val is None or secret_val == "" or "CHANGE_ME" in secret_val
+ or enc_val is None or enc_val == "" or "CHANGE_ME" in enc_val
+ )
+
+ if force or has_placeholders or missing_critical:
+ updated = existing
+
+ # Check ARES_SECRET_KEY
+ if force or secret_val is None or secret_val == "" or "CHANGE_ME" in secret_val:
+ new_secret = secrets.token_hex(32)
+ updated = _upsert_env_value(updated, "ARES_SECRET_KEY", new_secret)
+
+ # Check ARES_ENCRYPTION_KEY
+ if force or enc_val is None or enc_val == "" or "CHANGE_ME" in enc_val:
+ new_enc = _setup_fernet_key()
+ updated = _upsert_env_value(updated, "ARES_ENCRYPTION_KEY", new_enc)
+
+ # Check ARES_DEFAULT_ADMIN_PASSWORD
+ if force or admin_val is None or admin_val == "" or "CHANGE_ME" in admin_val or "YOUR_STRONG_ADMIN_PASSWORD_HERE" in admin_val:
+ admin_password = _setup_admin_password()
+ updated = _upsert_env_value(updated, "ARES_DEFAULT_ADMIN_PASSWORD", admin_password)
+
+ env_path.write_text(updated, encoding="utf-8")
+
+ if force:
+ console.print(f"[green]OK[/green] .env regenerated with fresh secure keys (--force).")
+ else:
+ console.print(f"[green]OK[/green] Replaced CHANGE_ME placeholders in .env with secure generated keys.")
+
+ if admin_password is not None:
+ console.print(f"Admin password: {admin_password}")
+ else:
+ console.print("[green]OK[/green] .env already exists; leaving it unchanged.")
+ admin_password = None
else:
if example_path.exists():
template = example_path.read_text(encoding="utf-8")
@@ -2306,7 +2359,21 @@ def _run_python_setup(
def setup_entrypoint() -> None:
"""Entry point for `ares-setup` using Python-native cross-platform setup."""
- _run_python_setup()
+ import sys
+
+ args = sys.argv[1:]
+ if "--help" in args or "-h" in args:
+ console.print("[bold]ARES Setup[/bold]")
+ console.print("Usage: ares-setup [--force | -f]")
+ console.print("Initializes or updates .env with secure random keys and admin credentials.")
+ console.print("Options:")
+ console.print(" --force, -f Force regeneration of ARES_SECRET_KEY, ARES_ENCRYPTION_KEY, and admin password.")
+ return
+ force = "--force" in args or "-f" in args
+ if force:
+ _run_python_setup(force=True)
+ else:
+ _run_python_setup()
# ── backup command ────────────────────────────────────────────────────────────
diff --git a/scripts/setup.sh b/scripts/setup.sh
index d730b9e..60c4523 100644
--- a/scripts/setup.sh
+++ b/scripts/setup.sh
@@ -38,6 +38,10 @@ pip install -e ".[dev,pdf]" -q
if [ ! -f ".env" ]; then
echo "[*] Generating .env from .env.example..."
cp .env.example .env
+fi
+
+if grep -q "CHANGE_ME" .env 2>/dev/null; then
+ echo "[*] Replacing CHANGE_ME placeholders in .env with secure keys..."
SECRET=$(python3 -c "import secrets; print(secrets.token_hex(32))")
ENC=$(python3 -c "from cryptography.fernet import Fernet; print(Fernet.generate_key().decode())")
ARES_GENERATED_SECRET="$SECRET" ARES_GENERATED_ENC="$ENC" python3 - <<'PY'
@@ -52,12 +56,12 @@ env_path = Path(".env")
lines = env_path.read_text(encoding="utf-8").splitlines()
for index, line in enumerate(lines):
for prefix, value in updates.items():
- if line.startswith(prefix):
+ if line.strip().startswith(prefix) and ("CHANGE_ME" in line or line.strip() == prefix):
lines[index] = value
break
env_path.write_text("\n".join(lines) + "\n", encoding="utf-8")
PY
- echo "[!] .env created - set ARES_DEFAULT_ADMIN_PASSWORD before starting"
+ echo "[OK] Secure keys generated in .env"
fi
echo ""
diff --git a/tests/unit/test_cli_doctor.py b/tests/unit/test_cli_doctor.py
index 7f602dd..9367916 100644
--- a/tests/unit/test_cli_doctor.py
+++ b/tests/unit/test_cli_doctor.py
@@ -612,3 +612,91 @@ def test_python_setup_rejects_python_313_before_writing_env(tmp_path, capsys):
assert "Python 3.10-3.12 is required" in output
assert "Python 3.12.x" in output
assert not (tmp_path / ".env").exists()
+
+
+def test_python_setup_replaces_change_me_placeholders_in_existing_env(tmp_path, capsys):
+ from ares.cli.typer_main import _run_python_setup
+
+ (tmp_path / ".env").write_text(
+ "ARES_SECRET_KEY=CHANGE_ME_USE_openssl_rand_-hex_32\n"
+ "ARES_ENCRYPTION_KEY=CHANGE_ME_USE_Fernet_generate_key\n"
+ "ARES_DEFAULT_ADMIN_PASSWORD=YOUR_STRONG_ADMIN_PASSWORD_HERE\n"
+ "ARES_API_PORT=8080\n",
+ encoding="utf-8",
+ )
+
+ _run_python_setup(root=tmp_path, platform_name="win32", os_name="nt")
+
+ output = capsys.readouterr().out
+ assert "Replaced CHANGE_ME placeholders in .env with secure generated keys" in output
+ assert "Admin password:" in output
+
+ env_content = (tmp_path / ".env").read_text(encoding="utf-8")
+ assert "CHANGE_ME" not in env_content
+ assert "YOUR_STRONG_ADMIN_PASSWORD_HERE" not in env_content
+ assert "ARES_API_PORT=8080" in env_content
+
+
+def test_python_setup_leaves_configured_env_unchanged(tmp_path, capsys):
+ from ares.cli.typer_main import _run_python_setup
+
+ original = (
+ "ARES_SECRET_KEY=0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef\n"
+ "ARES_ENCRYPTION_KEY=dGVzdF9rZXlfdGhhdF9pc18zMl9ieXRlc19sb25nISE=\n"
+ "ARES_DEFAULT_ADMIN_PASSWORD=CustomAdminPassword123!\n"
+ )
+ (tmp_path / ".env").write_text(original, encoding="utf-8")
+
+ _run_python_setup(root=tmp_path, platform_name="win32", os_name="nt")
+
+ output = capsys.readouterr().out
+ assert ".env already exists; leaving it unchanged" in output
+
+ env_content = (tmp_path / ".env").read_text(encoding="utf-8")
+ assert env_content == original
+
+
+def test_python_setup_force_regenerates_existing_env(tmp_path, capsys):
+ from ares.cli.typer_main import _run_python_setup
+
+ original = (
+ "ARES_SECRET_KEY=old_secret\n"
+ "ARES_ENCRYPTION_KEY=old_enc\n"
+ "ARES_DEFAULT_ADMIN_PASSWORD=old_pass\n"
+ "CUSTOM_VAR=keep_me\n"
+ )
+ (tmp_path / ".env").write_text(original, encoding="utf-8")
+
+ _run_python_setup(root=tmp_path, platform_name="win32", os_name="nt", force=True)
+
+ output = capsys.readouterr().out
+ assert ".env regenerated with fresh secure keys (--force)" in output
+ assert "Admin password:" in output
+
+ env_content = (tmp_path / ".env").read_text(encoding="utf-8")
+ assert "old_secret" not in env_content
+ assert "CUSTOM_VAR=keep_me" in env_content
+
+
+def test_setup_entrypoint_parses_force(monkeypatch):
+ import ares.cli.typer_main as typer_main
+
+ calls = []
+ monkeypatch.setattr(typer_main, "_run_python_setup", lambda force=False: calls.append(force))
+ monkeypatch.setattr(sys, "argv", ["ares-setup", "--force"])
+
+ typer_main.setup_entrypoint()
+
+ assert calls == [True]
+
+
+def test_setup_entrypoint_help(monkeypatch, capsys):
+ import ares.cli.typer_main as typer_main
+
+ monkeypatch.setattr(sys, "argv", ["ares-setup", "--help"])
+
+ typer_main.setup_entrypoint()
+
+ output = capsys.readouterr().out
+ assert "Usage: ares-setup" in output
+ assert "--force" in output