From 58aba40bc065cf34eb311921c268b2f1101ea9a4 Mon Sep 17 00:00:00 2001 From: karpenok Date: Wed, 11 Nov 2026 22:29:10 +0700 Subject: [PATCH] =?UTF-8?q?fix(remnawave):=20admin=20password=20v3=20+=20b?= =?UTF-8?q?ootstrap=20=D0=BF=D0=BE=D0=B4=20Remnawave=203.4=20API?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Генерируем REMNAWAVE_ADMIN_* (≥24) в /services/remnawave; токены через name/expiresInDays/scopes; SECRET_KEY ноды из GET /api/keygen/. Co-authored-by: Cursor --- ansible/ci/deploy-machine.sh | 2 + .../core-net/remnawave/defaults/main.yml | 6 +- .../roles/core-net/remnawave/tasks/main.yml | 63 +++++++++--- .../remnawave/templates/bootstrap-panel.sh.j2 | 96 +++++++++++-------- 4 files changed, 112 insertions(+), 55 deletions(-) diff --git a/ansible/ci/deploy-machine.sh b/ansible/ci/deploy-machine.sh index 8c99eeb..c2f2692 100755 --- a/ansible/ci/deploy-machine.sh +++ b/ansible/ci/deploy-machine.sh @@ -56,6 +56,8 @@ case "$machine" in fi extra+=(-e "authentik_enabled=false") eval "$(infisical export --domain="${INFISICAL_DOMAIN}" --token="${TOKEN}" --env=prod --projectId="${INFISICAL_PROJECT_ID}" --path="/services/remnawave" --format=dotenv-export --silent 2>/dev/null || true)" + export REMNAWAVE_ADMIN_USER="${REMNAWAVE_ADMIN_USER:-${ADMIN_USER:-karpenok}}" + export REMNAWAVE_ADMIN_PASSWORD="${REMNAWAVE_ADMIN_PASSWORD:-}" export REMNAWAVE_APP_SECRET="${REMNAWAVE_APP_SECRET:-}" export REMNAWAVE_POSTGRES_PASSWORD="${REMNAWAVE_POSTGRES_PASSWORD:-}" export REMNAWAVE_METRICS_PASS="${REMNAWAVE_METRICS_PASS:-}" diff --git a/ansible/roles/core-net/remnawave/defaults/main.yml b/ansible/roles/core-net/remnawave/defaults/main.yml index 0d29ccd..da9a0d5 100644 --- a/ansible/roles/core-net/remnawave/defaults/main.yml +++ b/ansible/roles/core-net/remnawave/defaults/main.yml @@ -15,8 +15,10 @@ remnawave_app_port: 3000 remnawave_metrics_port: 3001 remnawave_subscription_port: 3010 -remnawave_admin_username: "{{ lookup('env', 'ADMIN_USER', default='karpenok') }}" -remnawave_admin_password: "{{ lookup('env', 'ADMIN_PASSWORD', default='') }}" +# Логин панели: отдельный от /USER/ADMIN — Remnawave v3 требует пароль ≥24 +# с upper+lower+digit. Хранится в Infisical /services/remnawave. +remnawave_admin_username: "{{ lookup('env', 'REMNAWAVE_ADMIN_USER', default=lookup('env', 'ADMIN_USER', default='karpenok')) }}" +remnawave_admin_password: "{{ lookup('env', 'REMNAWAVE_ADMIN_PASSWORD', default='') }}" remnawave_app_secret: "{{ lookup('env', 'REMNAWAVE_APP_SECRET', default='') }}" remnawave_postgres_password: "{{ lookup('env', 'REMNAWAVE_POSTGRES_PASSWORD', default='') }}" diff --git a/ansible/roles/core-net/remnawave/tasks/main.yml b/ansible/roles/core-net/remnawave/tasks/main.yml index 7b9cff5..da29f41 100644 --- a/ansible/roles/core-net/remnawave/tasks/main.yml +++ b/ansible/roles/core-net/remnawave/tasks/main.yml @@ -7,22 +7,25 @@ - lookup('env', 'INFISICAL_PROJECT_ID') | length > 0 fail_msg: "Для роли remnawave нужны INFISICAL_TOKEN / INFISICAL_DOMAIN / INFISICAL_PROJECT_ID (деплой через deploy-machine.sh)." -- name: Проверить пароль администратора панели +- name: Проверить имя администратора панели ansible.builtin.assert: that: - remnawave_admin_username | length > 0 - - remnawave_admin_password | length >= 8 - fail_msg: "Нужны ADMIN_USER / ADMIN_PASSWORD из /USER/ADMIN (пароль ≥ 8)." + fail_msg: "Нужен REMNAWAVE_ADMIN_USER или ADMIN_USER." +# Remnawave v3: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{24,}$/ - name: Запомнить, какие секреты панели нужно сгенерировать ansible.builtin.set_fact: + remnawave_need_admin_password: "{{ (remnawave_admin_password | length < 24) or (remnawave_admin_password is not match('^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9]).{24,}$')) }}" remnawave_need_app_secret: "{{ remnawave_app_secret | length == 0 }}" remnawave_need_postgres_password: "{{ remnawave_postgres_password | length == 0 }}" remnawave_need_metrics_pass: "{{ remnawave_metrics_pass | length == 0 }}" remnawave_need_webhook_secret: "{{ remnawave_webhook_secret | length == 0 }}" + remnawave_need_node_secret: "{{ lookup('env', 'REMNANODE_SECRET_KEY', default='') | length == 0 }}" - name: Сгенерировать отсутствующие секреты панели ansible.builtin.set_fact: + remnawave_admin_password: "{{ remnawave_admin_password if not remnawave_need_admin_password else ('Rw9' ~ lookup('password', '/dev/null length=29 chars=ascii_letters,digits')) }}" remnawave_app_secret: "{{ remnawave_app_secret if not remnawave_need_app_secret else lookup('password', '/dev/null length=64 chars=hexdigits') }}" remnawave_postgres_password: "{{ remnawave_postgres_password if not remnawave_need_postgres_password else lookup('password', '/dev/null length=48 chars=hexdigits') }}" remnawave_metrics_pass: "{{ remnawave_metrics_pass if not remnawave_need_metrics_pass else lookup('password', '/dev/null length=64 chars=hexdigits') }}" @@ -81,16 +84,32 @@ - name: Сохранить сгенерированные секреты панели в Infisical ansible.builtin.shell: | set -euo pipefail + args=() + if [ "${NEED_ADMIN}" = "True" ] || [ "${NEED_ADMIN}" = "true" ]; then + args+=("REMNAWAVE_ADMIN_USER=${RW_ADMIN_USER}" "REMNAWAVE_ADMIN_PASSWORD=${RW_ADMIN_PASS}") + fi + if [ "${NEED_APP}" = "True" ] || [ "${NEED_APP}" = "true" ]; then + args+=("REMNAWAVE_APP_SECRET=${RW_APP_SECRET}") + fi + if [ "${NEED_PG}" = "True" ] || [ "${NEED_PG}" = "true" ]; then + args+=("REMNAWAVE_POSTGRES_PASSWORD=${RW_PG_PASS}") + fi + if [ "${NEED_METRICS}" = "True" ] || [ "${NEED_METRICS}" = "true" ]; then + args+=("REMNAWAVE_METRICS_PASS=${RW_METRICS_PASS}") + fi + if [ "${NEED_WEBHOOK}" = "True" ] || [ "${NEED_WEBHOOK}" = "true" ]; then + args+=("REMNAWAVE_WEBHOOK_SECRET_HEADER=${RW_WEBHOOK}") + fi + if [ "${#args[@]}" -eq 0 ]; then + exit 0 + fi out=$(infisical secrets set \ --domain="${INFISICAL_DOMAIN}" \ --token="${INFISICAL_TOKEN}" \ --env=prod \ --projectId="${INFISICAL_PROJECT_ID}" \ --path="/services/remnawave" \ - "REMNAWAVE_APP_SECRET=${RW_APP_SECRET}" \ - "REMNAWAVE_POSTGRES_PASSWORD=${RW_PG_PASS}" \ - "REMNAWAVE_METRICS_PASS=${RW_METRICS_PASS}" \ - "REMNAWAVE_WEBHOOK_SECRET_HEADER=${RW_WEBHOOK}" \ + "${args[@]}" \ --silent 2>&1) || { echo "infisical secrets set failed: ${out}" >&2 exit 1 @@ -103,13 +122,21 @@ INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}" INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}" INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}" + NEED_ADMIN: "{{ remnawave_need_admin_password }}" + NEED_APP: "{{ remnawave_need_app_secret }}" + NEED_PG: "{{ remnawave_need_postgres_password }}" + NEED_METRICS: "{{ remnawave_need_metrics_pass }}" + NEED_WEBHOOK: "{{ remnawave_need_webhook_secret }}" + RW_ADMIN_USER: "{{ remnawave_admin_username }}" + RW_ADMIN_PASS: "{{ remnawave_admin_password }}" RW_APP_SECRET: "{{ remnawave_app_secret }}" RW_PG_PASS: "{{ remnawave_postgres_password }}" RW_METRICS_PASS: "{{ remnawave_metrics_pass }}" RW_WEBHOOK: "{{ remnawave_webhook_secret }}" when: >- - remnawave_need_app_secret or remnawave_need_postgres_password - or remnawave_need_metrics_pass or remnawave_need_webhook_secret + remnawave_need_admin_password or remnawave_need_app_secret + or remnawave_need_postgres_password or remnawave_need_metrics_pass + or remnawave_need_webhook_secret register: remnawave_infisical_panel_secrets changed_when: true no_log: true @@ -201,15 +228,21 @@ ansible.builtin.assert: that: - (remnawave_bootstrap.node_secret | default('') | length > 0) or (lookup('env', 'REMNANODE_SECRET_KEY', default='') | length > 0) - fail_msg: "Панель не вернула SECRET_KEY ноды Ger-nod-02. Проверьте ответ POST /api/nodes/ (версия Remnawave) и повторите прогон." + fail_msg: "Панель не вернула SECRET_KEY ноды (GET /api/keygen/). Проверьте версию Remnawave и повторите прогон." - name: Сохранить API-токен и SECRET_KEY ноды в Infisical ansible.builtin.shell: | set -euo pipefail - args=("REMNAWAVE_API_TOKEN=${RW_API_TOKEN}") - if [ -n "${RW_NODE_SECRET}" ]; then + args=() + if [ "${NEED_TOKEN}" = "True" ] || [ "${NEED_TOKEN}" = "true" ]; then + args+=("REMNAWAVE_API_TOKEN=${RW_API_TOKEN}") + fi + if [ -n "${RW_NODE_SECRET}" ] && { [ "${NEED_NODE}" = "True" ] || [ "${NEED_NODE}" = "true" ]; }; then args+=("REMNANODE_SECRET_KEY=${RW_NODE_SECRET}") fi + if [ "${#args[@]}" -eq 0 ]; then + exit 0 + fi out=$(infisical secrets set \ --domain="${INFISICAL_DOMAIN}" \ --token="${INFISICAL_TOKEN}" \ @@ -229,11 +262,13 @@ INFISICAL_DOMAIN: "{{ lookup('env', 'INFISICAL_DOMAIN') }}" INFISICAL_TOKEN: "{{ lookup('env', 'INFISICAL_TOKEN') }}" INFISICAL_PROJECT_ID: "{{ lookup('env', 'INFISICAL_PROJECT_ID') }}" + NEED_TOKEN: "{{ remnawave_bootstrap.token_updated | default(false) }}" + NEED_NODE: "{{ remnawave_need_node_secret }}" RW_API_TOKEN: "{{ remnawave_api_token }}" RW_NODE_SECRET: "{{ remnawave_bootstrap.node_secret | default('') }}" when: >- remnawave_bootstrap.token_updated | default(false) - or (remnawave_bootstrap.node_secret | default('') | length > 0 and remnawave_bootstrap.node_created | default(false)) + or (remnawave_need_node_secret and (remnawave_bootstrap.node_secret | default('') | length > 0)) changed_when: true no_log: true @@ -277,6 +312,8 @@ msg: - "Панель: https://{{ remnawave_panel_domain }}" - "Подписка: https://{{ remnawave_sub_domain }}" + - "Админ-логин в Infisical /services/remnawave: REMNAWAVE_ADMIN_USER={{ remnawave_admin_username }}" + - "Админ-пароль сгенерирован в этом прогоне: {{ remnawave_need_admin_password }}" - "Админ создан в этом прогоне: {{ remnawave_bootstrap.admin_created | default(false) }}" - "API-токен обновлён в Infisical: {{ remnawave_bootstrap.token_updated | default(false) }}" - "Профиль {{ remnawave_profile_name }}: {{ remnawave_bootstrap.profile_uuid | default('n/a') }}" diff --git a/ansible/roles/core-net/remnawave/templates/bootstrap-panel.sh.j2 b/ansible/roles/core-net/remnawave/templates/bootstrap-panel.sh.j2 index d8e2a8a..06e4473 100644 --- a/ansible/roles/core-net/remnawave/templates/bootstrap-panel.sh.j2 +++ b/ansible/roles/core-net/remnawave/templates/bootstrap-panel.sh.j2 @@ -2,7 +2,7 @@ set -euo pipefail API_BASE="https://${RW_PANEL_DOMAIN}/api" -CURL=(curl -fsSk --resolve "${RW_PANEL_DOMAIN}:443:127.0.0.1") +CURL=(curl -sSk --resolve "${RW_PANEL_DOMAIN}:443:127.0.0.1") AUTH_HDR=() admin_created=0 @@ -16,30 +16,46 @@ active_token="${RW_API_TOKEN:-}" api() { local method="$1"; shift local path="$1"; shift + local tmp code + tmp=$(mktemp) if [ "${#AUTH_HDR[@]}" -gt 0 ]; then - "${CURL[@]}" -X "$method" "${AUTH_HDR[@]}" -H "Content-Type: application/json" "${API_BASE}${path}" "$@" + code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" "${AUTH_HDR[@]}" \ + -H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true else - "${CURL[@]}" -X "$method" -H "Content-Type: application/json" "${API_BASE}${path}" "$@" + code=$("${CURL[@]}" -o "$tmp" -w '%{http_code}' -X "$method" \ + -H "Content-Type: application/json" "${API_BASE}${path}" "$@") || true fi -} - -json_first() { - jq -r "$1" 2>/dev/null || true + if [[ ! "${code}" =~ ^2 ]]; then + echo "API ${method} ${path} → HTTP ${code}" >&2 + cat "$tmp" >&2 || true + echo >&2 + rm -f "$tmp" + return 1 + fi + cat "$tmp" + rm -f "$tmp" } status_json=$("${CURL[@]}" "${API_BASE}/auth/status") -is_login=$(printf '%s' "${status_json}" | jq -r '.response.isLoginAllowed // .isLoginAllowed // false') is_register=$(printf '%s' "${status_json}" | jq -r '.response.isRegisterAllowed // .isRegisterAllowed // false') if [ "${is_register}" = "true" ]; then reg_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}') - api POST /auth/register -d "${reg_payload}" >/dev/null + reg_resp=$(api POST /auth/register -d "${reg_payload}") + access=$(printf '%s' "${reg_resp}" | jq -r '.response.accessToken // .accessToken // empty') + if [ -z "${access}" ]; then + echo "Не удалось зарегистрировать админа Remnawave" >&2 + printf '%s\n' "${reg_resp}" >&2 + exit 1 + fi + AUTH_HDR=(-H "Authorization: Bearer ${access}") admin_created=1 - is_login=true + login_ok=1 +else + login_ok=0 fi -login_ok=0 -if [ -n "${active_token}" ]; then +if [ "${login_ok:-0}" -eq 0 ] && [ -n "${active_token}" ]; then code=$("${CURL[@]}" -o /dev/null -w '%{http_code}' \ -H "Authorization: Bearer ${active_token}" "${API_BASE}/nodes/" || true) if [ "${code}" = "200" ]; then @@ -48,7 +64,7 @@ if [ -n "${active_token}" ]; then fi fi -if [ "${login_ok}" -eq 0 ]; then +if [ "${login_ok:-0}" -eq 0 ]; then login_payload=$(jq -n --arg u "$RW_ADMIN_USER" --arg p "$RW_ADMIN_PASS" '{username: $u, password: $p}') login_resp=$(api POST /auth/login -d "${login_payload}") access=$(printf '%s' "${login_resp}" | jq -r '.response.accessToken // .accessToken // empty') @@ -58,18 +74,28 @@ if [ "${login_ok}" -eq 0 ]; then exit 1 fi AUTH_HDR=(-H "Authorization: Bearer ${access}") +fi +# Постоянный API-токен (v3: name + expiresInDays + scopes) +if [ -z "${active_token}" ] || [ "${admin_created}" -eq 1 ]; then tokens_json=$(api GET /tokens/ || echo '{"response":[]}') old_uuid=$(printf '%s' "${tokens_json}" | jq -r --arg n "$RW_TOKEN_NAME" ' (.response // .) | if type=="array" then . elif .tokens then .tokens else [] end - | map(select(.tokenName == $n or .name == $n)) + | map(select(.name == $n or .tokenName == $n)) | .[0].uuid // empty') if [ -n "${old_uuid}" ]; then api DELETE "/tokens/${old_uuid}" >/dev/null || true fi - token_payload=$(jq -n --arg n "$RW_TOKEN_NAME" '{tokenName: $n}') + scopes_json=$(api GET /tokens/scopes || echo '{"response":[]}') + scopes=$(printf '%s' "${scopes_json}" | jq -c ' + (.response // .) + | if type=="array" then . + elif .scopes then .scopes + else [] end') + token_payload=$(jq -n --arg n "$RW_TOKEN_NAME" --argjson s "${scopes}" \ + '{name: $n, expiresInDays: 3650, scopes: $s}') token_resp=$(api POST /tokens/ -d "${token_payload}") active_token=$(printf '%s' "${token_resp}" | jq -r ' .response.token // .response.apiToken // .token // .apiToken // empty') @@ -82,6 +108,19 @@ if [ "${login_ok}" -eq 0 ]; then token_updated=1 fi +# SECRET_KEY ноды — отдельный endpoint /api/keygen/ (не ответ create node) +if [ -n "${RW_NODE_SECRET:-}" ]; then + node_secret="${RW_NODE_SECRET}" +else + keygen_resp=$(api GET /keygen/) + node_secret=$(printf '%s' "${keygen_resp}" | jq -r '.response.secretKey // .secretKey // empty') + if [ -z "${node_secret}" ]; then + echo "Не удалось получить SECRET_KEY ноды из /api/keygen/" >&2 + printf '%s\n' "${keygen_resp}" >&2 + exit 1 + fi +fi + profiles_json=$(api GET /config-profiles/) profile_uuid=$(printf '%s' "${profiles_json}" | jq -r --arg n "$RW_PROFILE_NAME" ' (.response.configProfiles // .response // []) @@ -89,7 +128,7 @@ profile_uuid=$(printf '%s' "${profiles_json}" | jq -r --arg n "$RW_PROFILE_NAME" pub="" if [ -z "${profile_uuid}" ]; then - key_resp=$(api GET /system/tools/x25519/generate 2>/dev/null || api GET /keygen/ 2>/dev/null || echo '{}') + key_resp=$(api GET /system/tools/x25519/generate 2>/dev/null || echo '{}') priv=$(printf '%s' "${key_resp}" | jq -r ' .response.privateKey // .response.privKey // .privateKey // .privKey // empty') pub=$(printf '%s' "${key_resp}" | jq -r ' @@ -134,7 +173,6 @@ fi nodes_json=$(api GET /nodes/) node_uuid=$(printf '%s' "${nodes_json}" | jq -r --arg n "$RW_NODE_NAME" ' (.response // []) | map(select(.name == $n)) | .[0].uuid // empty') -node_secret="${RW_NODE_SECRET:-}" if [ -z "${node_uuid}" ]; then node_payload=$(jq -n \ @@ -154,24 +192,8 @@ if [ -z "${node_uuid}" ]; then activeInbounds: [$iu] } }') - node_resp=$(api POST /nodes/ -d "${node_payload}" 2>/dev/null || true) + node_resp=$(api POST /nodes/ -d "${node_payload}") node_uuid=$(printf '%s' "${node_resp}" | jq -r '.response.uuid // .uuid // empty') - node_secret=$(printf '%s' "${node_resp}" | jq -r ' - .response.secretKey // .response.SECRET_KEY // .secretKey // .SECRET_KEY // empty') - - if [ -z "${node_uuid}" ]; then - node_payload=$(jq -n \ - --arg name "$RW_NODE_NAME" \ - --arg addr "$RW_NODE_ADDR" \ - --argjson port "$RW_NODE_PORT" \ - --arg cc "$RW_NODE_CC" \ - '{name: $name, address: $addr, port: $port, countryCode: $cc}') - node_resp=$(api POST /nodes/ -d "${node_payload}") - node_uuid=$(printf '%s' "${node_resp}" | jq -r '.response.uuid // .uuid // empty') - node_secret=$(printf '%s' "${node_resp}" | jq -r ' - .response.secretKey // .response.SECRET_KEY // .secretKey // .SECRET_KEY // empty') - fi - if [ -z "${node_uuid}" ]; then echo "Не удалось создать ноду" >&2 printf '%s\n' "${node_resp}" >&2 @@ -195,12 +217,6 @@ api PATCH /nodes/ -d "${node_update}" >/dev/null 2>&1 \ || api PUT /nodes/ -d "${node_update}" >/dev/null 2>&1 \ || true -if [ -z "${node_secret}" ] && [ "${node_created}" -eq 1 ]; then - node_full=$(api GET "/nodes/${node_uuid}") - node_secret=$(printf '%s' "${node_full}" | jq -r ' - .response.secretKey // .response.SECRET_KEY // .secretKey // empty') -fi - hosts_json=$(api GET /hosts/) host_uuid=$(printf '%s' "${hosts_json}" | jq -r --arg a "$RW_SELFSTEAL" ' (.response // []) | map(select(.address == $a)) | .[0].uuid // empty')