diff --git a/agent/agent.config.example.json b/agent/agent.config.example.json index 3ba571c..1f4a9c5 100644 --- a/agent/agent.config.example.json +++ b/agent/agent.config.example.json @@ -1,7 +1,7 @@ { "agent": { "listen": "0.0.0.0:9100", - "token": "CHANGE_ME", + "token": "MANAGERD_AGENT_TOKEN", "require_token_for_read": false, "discovery_enable": true, diff --git a/scripts/deploy.sh b/scripts/deploy.sh index 72b03f2..d316cfc 100755 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -3,6 +3,8 @@ # 部署到 /opt,包含完整的运行时目录、权限、日志轮转等 # # 用法: sudo ./deploy.sh [install|upgrade|status|logs|clean-hls|uninstall] +# 安装/升级 Agent 时必须显式传入后台管理统一主钥匙: +# sudo AGENT_TOKEN= ./deploy.sh install set -e @@ -44,6 +46,26 @@ DEPLOY_CONFIG_OVERLAYS="${DEPLOY_CONFIG_OVERLAYS:-configs/overlays/face_debug.js DEPLOY_CONFIG_ID="${DEPLOY_CONFIG_ID:-local_3588_face_debug}" DEPLOY_CONFIG_VERSION="${DEPLOY_CONFIG_VERSION:-$(date +%Y%m%d.%H%M%S)}" +# 后台管理系统访问所有设备 agent 的统一主钥匙。 +# 必须由部署调用方显式传入,禁止设备本地随机生成,避免后台和设备 token 漂移。 +# 示例: +# sudo AGENT_TOKEN=4fe2d69fda23d0d5d04a1486d4920e68 ./scripts/deploy.sh install +AGENT_TOKEN="${AGENT_TOKEN:-}" + +require_agent_token() { + if [ -z "$AGENT_TOKEN" ]; then + echo -e "${RED}错误: AGENT_TOKEN 未设置${NC}" + echo "Agent token 是后台管理系统访问所有设备的统一管理主钥匙,部署脚本不会本地生成 token。" + echo "请使用:sudo AGENT_TOKEN= ./scripts/deploy.sh install" + exit 1 + fi + if ! printf '%s' "$AGENT_TOKEN" | grep -Eq '^[A-Za-z0-9._:-]+$'; then + echo -e "${RED}错误: AGENT_TOKEN 包含不支持的字符${NC}" + echo "请使用字母、数字、点、下划线、冒号或短横线组成的 token。" + exit 1 + fi +} + # 创建必要的运行时目录 create_runtime_dirs() { echo -e "${CYAN}[创建运行时目录]${NC}" @@ -469,6 +491,7 @@ EOF # 安装 Agent cmd_install_agent() { echo -e "${BLUE}[2/5] 安装 Agent...${NC}" + require_agent_token # 查找 Agent 编译产物(优先使用预编译的 arm64 二进制) AGENT_SOURCE="" @@ -496,7 +519,7 @@ cmd_install_agent() { { "agent": { "listen": "0.0.0.0:9100", - "token": "$(openssl rand -hex 16)", + "token": "$AGENT_TOKEN", "require_token_for_read": false, "discovery_enable": true, "discovery_port": 35688, @@ -518,7 +541,7 @@ EOF chown -R orangepi:orangepi "/var/lib/rk3588-agent" - echo -e "${GREEN}✓${NC} Agent 安装完成" + echo -e "${GREEN}✓${NC} Agent 安装完成,已写入后台管理统一主钥匙" } # 安装 systemd 服务 @@ -846,7 +869,7 @@ case "${1:-install}" in echo " uninstall 卸载服务" echo "" echo "示例:" - echo " sudo ./deploy.sh install" + echo " sudo AGENT_TOKEN= ./deploy.sh install" echo " sudo ./deploy.sh status" echo " sudo ./deploy.sh clean-hls 7 # 保留最近7天" exit 1 diff --git a/tests/test_deploy_script.py b/tests/test_deploy_script.py new file mode 100644 index 0000000..1d399c0 --- /dev/null +++ b/tests/test_deploy_script.py @@ -0,0 +1,20 @@ +from pathlib import Path +import unittest + + +ROOT = Path(__file__).resolve().parents[1] + + +class DeployScriptTest(unittest.TestCase): + def test_deploy_requires_managerd_agent_token_without_fallback(self): + text = (ROOT / "scripts" / "deploy.sh").read_text(encoding="utf-8") + + self.assertIn('AGENT_TOKEN="${AGENT_TOKEN:-}"', text) + self.assertIn("require_agent_token", text) + self.assertIn('"token": "$AGENT_TOKEN"', text) + self.assertNotIn("openssl rand", text) + self.assertIn("不会本地生成 token", text) + + +if __name__ == "__main__": + unittest.main()