Skip to content

Commit 0e6ad1c

Browse files
committed
feat: cli supports ASTRBOT_DASHBOARD_INITIAL_PASSWORD env
1 parent e05dd65 commit 0e6ad1c

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

astrbot/cli/commands/cmd_init.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,24 @@
11
import asyncio
2+
import os
23
from pathlib import Path
34

45
import click
56
from filelock import FileLock, Timeout
67

78
from ..utils import check_dashboard, get_astrbot_root
89

10+
DASHBOARD_INITIAL_PASSWORD_ENV = "ASTRBOT_DASHBOARD_INITIAL_PASSWORD"
11+
12+
13+
def _initialize_config_from_env(astrbot_root: Path) -> None:
14+
if DASHBOARD_INITIAL_PASSWORD_ENV not in os.environ:
15+
return
16+
17+
from astrbot.core.config.astrbot_config import AstrBotConfig
18+
19+
AstrBotConfig(config_path=str(astrbot_root / "data" / "cmd_config.json"))
20+
click.echo("Initialized data/cmd_config.json with dashboard initial password.")
21+
922

1023
async def initialize_astrbot(astrbot_root: Path) -> None:
1124
"""Execute AstrBot initialization logic"""
@@ -31,6 +44,8 @@ async def initialize_astrbot(astrbot_root: Path) -> None:
3144
path.mkdir(parents=True, exist_ok=True)
3245
click.echo(f"{'Created' if not path.exists() else 'Directory exists'}: {path}")
3346

47+
_initialize_config_from_env(astrbot_root)
48+
3449
await check_dashboard(astrbot_root / "data")
3550

3651

tests/test_cli_init.py

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
import json
2+
3+
import pytest
4+
5+
from astrbot.cli.commands import cmd_init
6+
from astrbot.core.utils.auth_password import verify_dashboard_password
7+
8+
9+
@pytest.mark.asyncio
10+
async def test_init_without_initial_password_env_does_not_create_config(
11+
monkeypatch,
12+
tmp_path,
13+
):
14+
async def fake_check_dashboard(_data_path):
15+
return None
16+
17+
monkeypatch.delenv(cmd_init.DASHBOARD_INITIAL_PASSWORD_ENV, raising=False)
18+
monkeypatch.setattr(cmd_init, "check_dashboard", fake_check_dashboard)
19+
(tmp_path / ".astrbot").touch()
20+
21+
await cmd_init.initialize_astrbot(tmp_path)
22+
23+
assert not (tmp_path / "data" / "cmd_config.json").exists()
24+
25+
26+
@pytest.mark.asyncio
27+
async def test_init_uses_initial_password_env_to_create_config(
28+
monkeypatch,
29+
tmp_path,
30+
):
31+
async def fake_check_dashboard(_data_path):
32+
return None
33+
34+
initial_password = "AstrBotInitialPassword123"
35+
monkeypatch.setenv(cmd_init.DASHBOARD_INITIAL_PASSWORD_ENV, initial_password)
36+
monkeypatch.setattr(cmd_init, "check_dashboard", fake_check_dashboard)
37+
(tmp_path / ".astrbot").touch()
38+
39+
await cmd_init.initialize_astrbot(tmp_path)
40+
41+
config_path = tmp_path / "data" / "cmd_config.json"
42+
config = json.loads(config_path.read_text(encoding="utf-8-sig"))
43+
dashboard_config = config["dashboard"]
44+
45+
assert verify_dashboard_password(
46+
dashboard_config["pbkdf2_password"],
47+
initial_password,
48+
)
49+
assert verify_dashboard_password(
50+
dashboard_config["password"],
51+
initial_password,
52+
)
53+
assert dashboard_config["password_change_required"] is True
54+
assert dashboard_config["password_storage_upgraded"] is True

0 commit comments

Comments
 (0)