diff --git a/BOOTSTRAP.md b/BOOTSTRAP.md index 2ffea51..970498f 100644 --- a/BOOTSTRAP.md +++ b/BOOTSTRAP.md @@ -89,7 +89,7 @@ API Endpoint (внутренний LAN): `http://192.168.1.102:8080/api` - `NETBIRD_API_TOKEN`: сервисный API-токен управления (создаётся и сохраняется пайплайном автоматически). - `NETBIRD_SETUP_KEY`: многоразовый ключ регистрации роутеров (`homelab-routers`). Панель и ноды им не пользуются. - `NETBIRD_REMNAWAVE_SETUP_KEY`: многоразовый ключ `remnawave-peers` для core-net и ger-nod-02. В группу роутеров не входит. - - `NETBIRD_EXIT_CLIENTS_SETUP_KEY`: многоразовый ключ `exit-clients-key` для клиентских устройств, выходящих в интернет через Exit Node `ger-nod-02` (группа `exit-clients`). + - `NETBIRD_EXIT_CLIENTS_SETUP_KEY`: многоразовый ключ `exit-clients-key` для клиентских устройств, выходящих в интернет через Exit Node `ger-nod-02` (группа `exit-clients`). Дополнительные клиентские ключи из `ansible/roles/rus-central/netbird/files/access.yml` сохраняются сюда же по имени поля `secret`. - *(Параметры `NETBIRD_ADMIN_NAME`, `NETBIRD_ADMIN_PASSWORD`, `NETBIRD_ADMIN_EMAIL` наследуются из `/USER/ADMIN`)*. - `/services/opnsense`: - `OPNSENSE_API_KEY`: API-ключ администратора OPNsense. @@ -209,5 +209,8 @@ API Endpoint (внутренний LAN): `http://192.168.1.102:8080/api` ### Шаг 5. NetBird (rus-central) и связка с OPNsense 1. Запустить пайплайн **`ansible-rus-central`** в Gitea Actions. Ручной запуск сверяет машину целиком. Push в `roles/rus-central/netbird` ставит только NetBird. -2. Раннер считывает ключи из `/services/netbird`, деплоит стек в `/opt/netbird` на `45.154.185.208`, автоматически регистрирует первого суперпользователя (`POST /api/setup`), создаёт сервисный токен (`NETBIRD_API_TOKEN`), группу `homelab-routers`, маршрут к подсети `192.168.1.0/24` и многоразовый ключ установки (`NETBIRD_SETUP_KEY`). Отдельно создаётся группа `remnawave` и ключ `remnawave-peers` (`NETBIRD_REMNAWAVE_SETUP_KEY`): он не добавляет peer в `homelab-routers`. Для Exit Node создаются группы `exit-ger-nod-02` и `exit-clients`, маршрут `0.0.0.0/0` через `ger-nod-02` (только для `exit-clients`, чтобы не затронуть OPNsense и `core-net`), DNS `exit-dns` (`1.1.1.1`, `8.8.8.8`) и ключ `exit-clients-key` (`NETBIRD_EXIT_CLIENTS_SETUP_KEY`). Все ключи сохраняются в Infisical. +2. Раннер считывает ключи из `/services/netbird`, деплоит стек в `/opt/netbird` на `45.154.185.208`, автоматически регистрирует первого суперпользователя (`POST /api/setup`), создаёт сервисный токен (`NETBIRD_API_TOKEN`), группу `homelab-routers`, маршрут к подсети `192.168.1.0/24` и многоразовый ключ установки (`NETBIRD_SETUP_KEY`). Отдельно создаётся группа `remnawave` и ключ `remnawave-peers` (`NETBIRD_REMNAWAVE_SETUP_KEY`): он не добавляет peer в `homelab-routers`. Клиентский доступ (`client_groups`, `exit_nodes`, `dns_nameservers`, `setup_keys`, `peers`) задаётся декларативно в `ansible/roles/rus-central/netbird/files/access.yml` и применяется задачей `tasks/access.yml` (`sync_access.py`): + - **Добавить ключ клиента**: дописать новую запись в `setup_keys` (`name`, `secret`, `groups`) и запушить — ключ создастся в NetBird и сохранится в Infisical `/services/netbird`. + - **Отозвать ключ**: выставить `enabled: false` (или удалить запись) в `setup_keys`. + - **Управлять устройством**: в `peers` можно зафиксировать группы пира (`groups`) или удалить/отключить устройство через `state: absent`. 3. Запустить пайплайн **`ansible-opnsense`** в Gitea Actions. Раннер обращается по HTTPS REST API к `192.168.1.1`, проверяет/устанавливает плагин `os-netbird`, включает службу и регистрирует шлюз OPNsense на `https://central.gunihome.ru:443` по `NETBIRD_SETUP_KEY`. diff --git a/ansible/roles/rus-central/netbird/defaults/main.yml b/ansible/roles/rus-central/netbird/defaults/main.yml index 8209cc1..833b5ab 100644 --- a/ansible/roles/rus-central/netbird/defaults/main.yml +++ b/ansible/roles/rus-central/netbird/defaults/main.yml @@ -24,13 +24,3 @@ netbird_remnawave_setup_key_name: "remnawave-peers" netbird_remnawave_setup_key: "{{ lookup('env', 'NETBIRD_REMNAWAVE_SETUP_KEY') }}" netbird_lan_network_id: "homelab-lan" netbird_lan_subnet: "192.168.1.0/24" -netbird_exit_group_name: "exit-ger-nod-02" -netbird_exit_peer_hostname: "ger-nod-02" -netbird_exit_route_network_id: "exit-ger-nod-02" -netbird_exit_clients_group_name: "exit-clients" -netbird_exit_clients_setup_key_name: "exit-clients-key" -netbird_exit_clients_setup_key: "{{ lookup('env', 'NETBIRD_EXIT_CLIENTS_SETUP_KEY') }}" -netbird_exit_dns_name: "exit-dns" -netbird_exit_dns_servers: - - "1.1.1.1" - - "8.8.8.8" diff --git a/ansible/roles/rus-central/netbird/files/access.yml b/ansible/roles/rus-central/netbird/files/access.yml new file mode 100644 index 0000000..f21762c --- /dev/null +++ b/ansible/roles/rus-central/netbird/files/access.yml @@ -0,0 +1,58 @@ +# Декларативное описание клиентского доступа NetBird. +# Применяется задачей rus-central/netbird (tasks/access.yml → sync_access.py). +# +# Секреты setup-ключей сохраняются в Infisical (/services/netbird) автоматически +# и не попадают в Git или логи CI. + +# Группы клиентов +client_groups: + - exit-clients + +# Выходные шлюзы (Exit Nodes) +exit_nodes: + - network_id: exit-ger-nod-02 + description: "Exit Node DE (0.0.0.0/0 via ger-nod-02)" + peer: ger-nod-02 + peer_group: exit-ger-nod-02 + client_groups: + - exit-clients + metric: 100 + masquerade: true + keep_route: true + skip_auto_apply: false + enabled: true + +# DNS для клиентских групп +dns_nameservers: + - name: exit-dns + description: "Public DNS for Exit Node clients" + servers: + - "1.1.1.1" + - "8.8.8.8" + groups: + - exit-clients + primary: true + enabled: true + +# Ключи подключения клиентов (секреты сохраняются в Infisical /services/netbird). +# Чтобы выпустить отдельный ключ — добавьте запись с новым name и secret. +# Чтобы отозвать ключ — поставьте enabled: false или удалите запись. +setup_keys: + - name: exit-clients-key + secret: NETBIRD_EXIT_CLIENTS_SETUP_KEY + type: reusable + expires_in: 31536000 + usage_limit: 0 + ephemeral: false + groups: + - exit-clients + enabled: true + +# Управление подключёнными клиентскими устройствами (пирами). +# Новые пиры, подключившиеся по setup-ключу, сохраняют свои группы автоматически. +# Здесь можно явно зафиксировать группы пира или удалить/отключить устройство (state: absent). +peers: + - name: "A55 пользователя Vacheslav" + groups: + - exit-clients + state: present diff --git a/ansible/roles/rus-central/netbird/files/sync_access.py b/ansible/roles/rus-central/netbird/files/sync_access.py new file mode 100644 index 0000000..b12e240 --- /dev/null +++ b/ansible/roles/rus-central/netbird/files/sync_access.py @@ -0,0 +1,339 @@ +#!/usr/bin/env python3 +"""Идемпотентная синхронизация клиентских групп, Exit Nodes, DNS, setup-ключей и пиров NetBird.""" + +import json +import os +import subprocess +import sys +from typing import Any + + +class NetBirdAPI: + """Минимальный клиент NetBird API через curl с --resolve на 127.0.0.1.""" + + def __init__(self, domain: str, token: str) -> None: + self.domain = domain + self.base_url = f"https://{domain}/api" + self.token = token + + def request( + self, + method: str, + path: str, + payload: dict[str, Any] | None = None, + ) -> Any: + cmd = [ + "curl", + "-sSk", + "--resolve", + f"{self.domain}:443:127.0.0.1", + "-w", + "\n%{http_code}", + "-X", + method, + f"{self.base_url}{path}", + "-H", + f"Authorization: Token {self.token}", + ] + stdin_bytes = None + if payload is not None: + cmd.extend(["-H", "Content-Type: application/json", "--data-binary", "@-"]) + stdin_bytes = json.dumps(payload, ensure_ascii=False).encode("utf-8") + + proc = subprocess.run(cmd, input=stdin_bytes, capture_output=True, check=False) + if proc.returncode != 0: + raise RuntimeError( + f"curl error for {method} {path}: {proc.stderr.decode(errors='replace')}" + ) + + raw = proc.stdout.decode("utf-8", errors="replace") + body, _, code = raw.rpartition("\n") + code = code.strip() + if code not in ("200", "201", "204"): + raise RuntimeError(f"NetBird API {method} {path} returned HTTP {code}: {body}") + + if not body.strip(): + return None + return json.loads(body) + + def get(self, path: str) -> Any: + return self.request("GET", path) + + def post(self, path: str, payload: dict[str, Any]) -> Any: + return self.request("POST", path, payload) + + def put(self, path: str, payload: dict[str, Any]) -> Any: + return self.request("PUT", path, payload) + + def delete(self, path: str) -> Any: + return self.request("DELETE", path) + + +def group_peer_ids(grp: dict[str, Any]) -> list[str]: + return [p["id"] for p in (grp.get("peers") or []) if p.get("id")] + + +def sync(cfg: dict[str, Any], api: NetBirdAPI) -> dict[str, Any]: + changes: list[str] = [] + warnings: list[str] = [] + updated_secrets: dict[str, str] = {} + + peers_raw = api.get("/peers") or [] + groups_raw = api.get("/groups") or [] + + groups_by_name: dict[str, dict[str, Any]] = {g["name"]: g for g in groups_raw if g.get("name")} + peers_by_name: dict[str, dict[str, Any]] = {} + for p in peers_raw: + if p.get("name"): + peers_by_name[p["name"]] = p + if p.get("hostname"): + peers_by_name.setdefault(p["hostname"], p) + + def ensure_group(name: str) -> dict[str, Any]: + if name in groups_by_name: + return groups_by_name[name] + created = api.post("/groups", {"name": name, "peers": []}) + groups_by_name[name] = created + changes.append(f"group.create:{name}") + return created + + def set_group_peers(grp: dict[str, Any], new_ids: list[str]) -> None: + unique_ids = sorted(set(new_ids)) + payload: dict[str, Any] = {"name": grp["name"], "peers": unique_ids} + if grp.get("resources"): + payload["resources"] = [ + {"id": r["id"], "type": r["type"]} + for r in grp["resources"] + if r.get("id") and r.get("type") + ] + updated = api.put(f"/groups/{grp['id']}", payload) + if isinstance(updated, dict) and updated.get("id"): + groups_by_name[grp["name"]] = updated + else: + grp["peers"] = [{"id": pid} for pid in unique_ids] + groups_by_name[grp["name"]] = grp + + # 1. Группы клиентов + managed_client_groups: set[str] = set() + for gname in cfg.get("client_groups") or []: + ensure_group(gname) + managed_client_groups.add(gname) + + # 2. Управление пирами + for item in cfg.get("peers") or []: + pname = item["name"] + state = item.get("state", "present") + peer = peers_by_name.get(pname) + + if state == "absent": + if peer is not None: + api.delete(f"/peers/{peer['id']}") + changes.append(f"peer.delete:{pname}") + peers_by_name = {k: v for k, v in peers_by_name.items() if v.get("id") != peer["id"]} + for grp in groups_by_name.values(): + if grp.get("peers"): + grp["peers"] = [p for p in grp["peers"] if p.get("id") != peer["id"]] + continue + + desired_groups = set(item.get("groups") or []) + for gname in desired_groups: + ensure_group(gname) + + if peer is None: + warnings.append(f"peer not found yet: {pname}") + continue + + pid = peer["id"] + for gname in sorted(desired_groups): + grp = groups_by_name[gname] + current_ids = group_peer_ids(grp) + if pid not in current_ids: + set_group_peers(grp, current_ids + [pid]) + changes.append(f"group.add_peer:{gname}:{pname}") + + for gname in sorted(managed_client_groups - desired_groups): + if gname not in groups_by_name: + continue + grp = groups_by_name[gname] + current_ids = group_peer_ids(grp) + if pid in current_ids: + set_group_peers(grp, [x for x in current_ids if x != pid]) + changes.append(f"group.remove_peer:{gname}:{pname}") + + # 3. Выходные шлюзы (Exit Nodes) + routes_raw = api.get("/routes") or [] + for item in cfg.get("exit_nodes") or []: + net_id = item["network_id"] + peer_name = item.get("peer", "") + peer_group_name = item["peer_group"] + client_group_names = item.get("client_groups") or [] + + peer_grp = ensure_group(peer_group_name) + if peer_name and peer_name in peers_by_name: + pid = peers_by_name[peer_name]["id"] + current_ids = group_peer_ids(peer_grp) + if pid not in current_ids: + set_group_peers(peer_grp, current_ids + [pid]) + changes.append(f"group.add_peer:{peer_group_name}:{peer_name}") + + client_group_ids = [ensure_group(g)["id"] for g in client_group_names] + peer_group_ids = [groups_by_name[peer_group_name]["id"]] + + desired = { + "description": item.get("description", f"Exit Node ({net_id})"), + "network_id": net_id, + "enabled": bool(item.get("enabled", True)), + "peer_groups": peer_group_ids, + "network": "0.0.0.0/0", + "metric": int(item.get("metric", 100)), + "masquerade": bool(item.get("masquerade", True)), + "groups": client_group_ids, + "keep_route": bool(item.get("keep_route", True)), + "skip_auto_apply": bool(item.get("skip_auto_apply", False)), + } + + existing = next( + (r for r in routes_raw if r.get("network_id") == net_id and r.get("network") == "0.0.0.0/0"), + None, + ) + if existing is None: + if not group_peer_ids(groups_by_name[peer_group_name]): + warnings.append(f"exit_node {net_id}: no peers in {peer_group_name} yet") + continue + api.post("/routes", desired) + changes.append(f"route.create:{net_id}") + else: + needs_update = ( + (existing.get("description") or "") != desired["description"] + or bool(existing.get("enabled")) != desired["enabled"] + or int(existing.get("metric", 100)) != desired["metric"] + or bool(existing.get("masquerade")) != desired["masquerade"] + or bool(existing.get("keep_route")) != desired["keep_route"] + or bool(existing.get("skip_auto_apply")) != desired["skip_auto_apply"] + or sorted(existing.get("peer_groups") or []) != sorted(desired["peer_groups"]) + or sorted(existing.get("groups") or []) != sorted(desired["groups"]) + ) + if needs_update: + api.put(f"/routes/{existing['id']}", desired) + changes.append(f"route.update:{net_id}") + + # 4. DNS Nameservers + ns_raw = api.get("/dns/nameservers") or [] + for item in cfg.get("dns_nameservers") or []: + name = item["name"] + existing = next((n for n in ns_raw if n.get("name") == name), None) + if not bool(item.get("enabled", True)): + if existing is not None: + api.delete(f"/dns/nameservers/{existing['id']}") + changes.append(f"dns.delete:{name}") + continue + + group_ids = [ensure_group(g)["id"] for g in (item.get("groups") or [])] + desired = { + "name": name, + "description": item.get("description", ""), + "nameservers": [ + {"ip": s, "ns_type": "udp", "port": 53} + for s in (item.get("servers") or []) + ], + "enabled": True, + "groups": group_ids, + "primary": bool(item.get("primary", True)), + "domains": item.get("domains") or [], + "search_domains_enabled": bool(item.get("search_domains_enabled", False)), + } + + if existing is None: + api.post("/dns/nameservers", desired) + changes.append(f"dns.create:{name}") + else: + needs_update = ( + (existing.get("description") or "") != desired["description"] + or (existing.get("nameservers") or []) != desired["nameservers"] + or bool(existing.get("enabled")) != desired["enabled"] + or sorted(existing.get("groups") or []) != sorted(desired["groups"]) + or bool(existing.get("primary")) != desired["primary"] + or (existing.get("domains") or []) != desired["domains"] + or bool(existing.get("search_domains_enabled")) != desired["search_domains_enabled"] + ) + if needs_update: + api.put(f"/dns/nameservers/{existing['id']}", desired) + changes.append(f"dns.update:{name}") + + # 5. Setup-ключи клиентов + keys_raw = api.get("/setup-keys") or [] + for item in cfg.get("setup_keys") or []: + name = item["name"] + secret_var = item["secret"] + enabled = bool(item.get("enabled", True)) + matching = [k for k in keys_raw if k.get("name") == name] + + if not enabled: + for k in matching: + api.delete(f"/setup-keys/{k['id']}") + changes.append(f"setup_key.delete:{name}") + continue + + group_ids = [ensure_group(g)["id"] for g in (item.get("groups") or [])] + env_present = bool(os.environ.get(secret_var, "")) + valid_match = next( + ( + k + for k in matching + if k.get("valid") is True + and k.get("revoked") is False + and sorted(k.get("auto_groups") or []) == sorted(group_ids) + ), + None, + ) + + if valid_match is not None and env_present: + continue + + for k in matching: + api.delete(f"/setup-keys/{k['id']}") + + key_payload = { + "name": name, + "type": item.get("type", "reusable"), + "expires_in": int(item.get("expires_in", 31536000)), + "revoked": False, + "auto_groups": group_ids, + "usage_limit": int(item.get("usage_limit", 0)), + "ephemeral": bool(item.get("ephemeral", False)), + } + created_key = api.post("/setup-keys", key_payload) + plain_key = (created_key or {}).get("key", "") + if not plain_key: + raise RuntimeError(f"NetBird API did not return key for setup_key {name}") + updated_secrets[secret_var] = plain_key + changes.append(f"setup_key.reissue:{name}") + + return { + "changed": bool(changes or updated_secrets), + "changes": changes, + "warnings": warnings, + "updated_secret_names": sorted(updated_secrets.keys()), + "updated_secrets": updated_secrets, + } + + +def main() -> None: + if len(sys.argv) < 2: + raise SystemExit("Usage: sync_access.py ") + + domain = os.environ.get("NB_DOMAIN", "") + token = os.environ.get("NB_API_TOKEN", "") + if not domain or not token: + raise SystemExit("NB_DOMAIN and NB_API_TOKEN are required") + + with open(sys.argv[1], "r", encoding="utf-8") as fh: + cfg = json.load(fh) + + api = NetBirdAPI(domain=domain, token=token) + result = sync(cfg, api) + sys.stdout.write(json.dumps(result, ensure_ascii=False) + "\n") + + +if __name__ == "__main__": + main() diff --git a/ansible/roles/rus-central/netbird/tasks/access.yml b/ansible/roles/rus-central/netbird/tasks/access.yml new file mode 100644 index 0000000..c91a7a2 --- /dev/null +++ b/ansible/roles/rus-central/netbird/tasks/access.yml @@ -0,0 +1,77 @@ +--- +- name: Загрузить декларативную конфигурацию доступа NetBird + ansible.builtin.set_fact: + netbird_access_cfg: "{{ lookup('file', role_path ~ '/files/access.yml') | from_yaml }}" + +- name: Положить скрипт синхронизации доступа NetBird + ansible.builtin.copy: + src: sync_access.py + dest: "{{ netbird_dir }}/sync_access.py" + owner: root + group: root + mode: "0700" + +- name: Записать конфигурацию доступа NetBird + ansible.builtin.copy: + dest: "{{ netbird_dir }}/access.json" + content: "{{ netbird_access_cfg | to_json }}" + owner: root + group: root + mode: "0600" + +- name: Подготовить базовое окружение для sync_access.py + ansible.builtin.set_fact: + netbird_access_env: + NB_DOMAIN: "{{ netbird_domain }}" + NB_API_TOKEN: "{{ netbird_effective_api_token }}" + no_log: true + +- name: Добавить текущие клиентские setup-ключи в окружение sync_access.py + ansible.builtin.set_fact: + netbird_access_env: "{{ netbird_access_env | combine({item.secret: lookup('env', item.secret)}) }}" + loop: "{{ netbird_access_cfg.setup_keys | default([]) }}" + loop_control: + label: "{{ item.name }}" + no_log: true + +- name: Синхронизировать группы, Exit Nodes, DNS, ключи и пиры NetBird + ansible.builtin.command: "python3 {{ netbird_dir }}/sync_access.py {{ netbird_dir }}/access.json" + environment: "{{ netbird_access_env }}" + register: netbird_access_raw + changed_when: (netbird_access_raw.stdout | from_json).changed + no_log: true + +- name: Разобрать результат синхронизации доступа NetBird + ansible.builtin.set_fact: + netbird_access_state: "{{ netbird_access_raw.stdout | from_json }}" + netbird_access_summary: + changed: "{{ (netbird_access_raw.stdout | from_json).changed }}" + changes: "{{ (netbird_access_raw.stdout | from_json).changes }}" + warnings: "{{ (netbird_access_raw.stdout | from_json).warnings }}" + updated_secret_names: "{{ (netbird_access_raw.stdout | from_json).updated_secret_names }}" + no_log: true + +- name: Сохранить клиентские setup-ключи NetBird в Infisical + ansible.builtin.shell: | + set -euo pipefail + infisical secrets set \ + --domain="${INFISICAL_DOMAIN}" \ + --token="${INFISICAL_TOKEN}" \ + --env=prod \ + --projectId="${INFISICAL_PROJECT_ID}" \ + --path="/services/netbird" \ + "${NB_SECRET_KEY}=${NB_SECRET_VAL}" \ + --silent + args: + executable: /bin/bash + delegate_to: localhost + become: false + environment: + NB_SECRET_KEY: "{{ item.key }}" + NB_SECRET_VAL: "{{ item.value }}" + loop: "{{ netbird_access_state.updated_secrets | default({}) | dict2items }}" + loop_control: + label: "{{ item.key }}" + when: (netbird_access_state.updated_secrets | default({}) | length) > 0 + changed_when: true + no_log: true diff --git a/ansible/roles/rus-central/netbird/tasks/bootstrap.yml b/ansible/roles/rus-central/netbird/tasks/bootstrap.yml new file mode 100644 index 0000000..6fdf9a4 --- /dev/null +++ b/ansible/roles/rus-central/netbird/tasks/bootstrap.yml @@ -0,0 +1,308 @@ +--- +- name: Инициализировать системный каркас NetBird (администратор, токен, базовые группы и маршруты) + ansible.builtin.shell: | + set -euo pipefail + + API_BASE="https://{{ netbird_domain }}/api" + CURL_OPTS=(-fsSk --resolve "{{ netbird_domain }}:443:127.0.0.1") + + admin_created=0 + token_updated=0 + setup_key_updated=0 + remna_key_updated=0 + route_created=0 + + setup_req=$(curl "${CURL_OPTS[@]}" "${API_BASE}/instance" | jq -r '.setup_required') + bootstrap_auth_header="" + + if [ "${setup_req}" = "true" ]; then + setup_payload=$(jq -n \ + --arg email "$NB_ADMIN_EMAIL" \ + --arg password "$NB_ADMIN_PASSWORD" \ + --arg name "$NB_ADMIN_NAME" \ + '{email: $email, password: $password, name: $name, create_pat: true}') + setup_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/setup" \ + -H "Content-Type: application/json" \ + -d "${setup_payload}") + bootstrap_token=$(printf '%s' "${setup_resp}" | jq -r '.personal_access_token // empty') + if [ -z "${bootstrap_token}" ]; then + echo "Не удалось получить personal_access_token из /api/setup" >&2 + exit 1 + fi + bootstrap_auth_header="Authorization: Token ${bootstrap_token}" + admin_created=1 + fi + + active_token="${NB_API_TOKEN:-}" + token_valid=0 + if [ -n "${active_token}" ]; then + http_code=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{http_code}' \ + -H "Authorization: Token ${active_token}" "${API_BASE}/users" || true) + if [ "${http_code}" = "200" ]; then + token_valid=1 + fi + fi + + if [ "${token_valid}" -eq 0 ]; then + if [ -z "${bootstrap_auth_header}" ]; then + verifier="dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk" + challenge="E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM" + redirect_uri="https://{{ netbird_domain }}/nb-auth" + auth_url="https://{{ netbird_domain }}/oauth2/auth?client_id=netbird-dashboard&redirect_uri=${redirect_uri}&response_type=code&scope=openid+profile+email+groups&code_challenge=${challenge}&code_challenge_method=S256&state=ansible" + + login_page=$(curl -sLk --resolve "{{ netbird_domain }}:443:127.0.0.1" "${auth_url}") + login_action=$(printf '%s' "${login_page}" | grep -oE 'action="[^"]+"' | head -n 1 | cut -d'"' -f2 | sed 's/&/\&/g') + if [ -z "${login_action}" ]; then + echo "Не удалось получить форму входа OAuth2 для выпуска токена" >&2 + exit 1 + fi + + redir_loc=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{redirect_url}' \ + -X POST "https://{{ netbird_domain }}${login_action}" \ + --data-urlencode "login=${NB_ADMIN_EMAIL}" \ + --data-urlencode "password=${NB_ADMIN_PASSWORD}") + + if printf '%s' "${redir_loc}" | grep -q '/oauth2/approval'; then + redir_loc=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{redirect_url}' "${redir_loc}") + fi + + auth_code=$(printf '%s' "${redir_loc}" | sed -n 's/.*[?&]code=\([^&]*\).*/\1/p') + if [ -z "${auth_code}" ]; then + echo "Не удалось получить authorization code через OAuth2 (проверьте NETBIRD_ADMIN_EMAIL / NETBIRD_ADMIN_PASSWORD)" >&2 + exit 1 + fi + + token_resp=$(curl "${CURL_OPTS[@]}" -X POST "https://{{ netbird_domain }}/oauth2/token" \ + --data-urlencode "grant_type=authorization_code" \ + --data-urlencode "client_id=netbird-dashboard" \ + --data-urlencode "redirect_uri=${redirect_uri}" \ + --data-urlencode "code=${auth_code}" \ + --data-urlencode "code_verifier=${verifier}") + jwt_token=$(printf '%s' "${token_resp}" | jq -r '.access_token // .id_token // empty') + if [ -z "${jwt_token}" ]; then + echo "Не удалось обменять authorization code на JWT токен" >&2 + exit 1 + fi + bootstrap_auth_header="Authorization: Bearer ${jwt_token}" + fi + + users_json=$(curl "${CURL_OPTS[@]}" -H "${bootstrap_auth_header}" "${API_BASE}/users") + svc_user_id=$(printf '%s' "${users_json}" | jq -r '.[] | select(.is_service_user == true and .name == "infra-automation") | .id' | head -n 1) + + if [ -z "${svc_user_id}" ]; then + user_payload=$(jq -n '{name: "infra-automation", role: "admin", auto_groups: [], is_service_user: true}') + svc_user_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users" \ + -H "${bootstrap_auth_header}" \ + -H "Content-Type: application/json" \ + -d "${user_payload}") + svc_user_id=$(printf '%s' "${svc_user_resp}" | jq -r '.id') + fi + + old_tokens=$(curl "${CURL_OPTS[@]}" -H "${bootstrap_auth_header}" "${API_BASE}/users/${svc_user_id}/tokens" || echo "[]") + for old_tid in $(printf '%s' "${old_tokens}" | jq -r '.[] | select(.name == "infisical-ci-token") | .id'); do + curl "${CURL_OPTS[@]}" -X DELETE -H "${bootstrap_auth_header}" "${API_BASE}/users/${svc_user_id}/tokens/${old_tid}" >/dev/null || true + done + + pat_payload=$(jq -n '{name: "infisical-ci-token", expires_in: 365}') + pat_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users/${svc_user_id}/tokens" \ + -H "${bootstrap_auth_header}" \ + -H "Content-Type: application/json" \ + -d "${pat_payload}") + active_token=$(printf '%s' "${pat_resp}" | jq -r '.plain_token // .plain_text // empty') + if [ -z "${active_token}" ]; then + echo "Не удалось выпустить сервисный токен через /api/users/${svc_user_id}/tokens" >&2 + exit 1 + fi + token_updated=1 + fi + + groups_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/groups") + all_group_id=$(printf '%s' "${groups_json}" | jq -r '.[] | select(.name == "All") | .id' | head -n 1) + router_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "$NB_ROUTER_GROUP" '.[] | select(.name == $g) | .id' | head -n 1) + + if [ -z "${router_group_id}" ]; then + grp_payload=$(jq -n --arg name "$NB_ROUTER_GROUP" '{name: $name, peers: []}') + grp_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/groups" \ + -H "Authorization: Token ${active_token}" \ + -H "Content-Type: application/json" \ + -d "${grp_payload}") + router_group_id=$(printf '%s' "${grp_resp}" | jq -r '.id') + fi + + remna_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "$NB_REMNAWAVE_GROUP" '.[] | select(.name == $g) | .id' | head -n 1) + if [ -z "${remna_group_id}" ]; then + grp_payload=$(jq -n --arg name "$NB_REMNAWAVE_GROUP" '{name: $name, peers: []}') + grp_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/groups" \ + -H "Authorization: Token ${active_token}" \ + -H "Content-Type: application/json" \ + -d "${grp_payload}") + remna_group_id=$(printf '%s' "${grp_resp}" | jq -r '.id // empty') + if [ -z "${remna_group_id}" ]; then + echo "Не удалось создать группу NetBird для Remnawave" >&2 + exit 1 + fi + fi + + routes_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/routes") + existing_route_id=$(printf '%s' "${routes_json}" | jq -r --arg nid "$NB_LAN_NET_ID" --arg net "$NB_LAN_SUBNET" '.[] | select(.network_id == $nid or .network == $net) | .id' | head -n 1) + + if [ -z "${existing_route_id}" ]; then + route_payload=$(jq -n \ + --arg desc "Homelab LAN (${NB_LAN_SUBNET} via OPNsense)" \ + --arg nid "$NB_LAN_NET_ID" \ + --arg net "$NB_LAN_SUBNET" \ + --arg rg "$router_group_id" \ + --arg ag "$all_group_id" \ + '{ + description: $desc, + network_id: $nid, + enabled: true, + peer_groups: [$rg], + network: $net, + metric: 100, + masquerade: true, + groups: [$ag], + keep_route: true + }') + curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/routes" \ + -H "Authorization: Token ${active_token}" \ + -H "Content-Type: application/json" \ + -d "${route_payload}" >/dev/null + route_created=1 + fi + + active_setup_key="${NB_SETUP_KEY:-}" + keys_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys") + valid_key_id=$(printf '%s' "${keys_json}" | jq -r --arg kname "$NB_SETUP_KEY_NAME" '.[] | select(.name == $kname and .valid == true and .revoked == false) | .id' | head -n 1) + + if [ -z "${active_setup_key}" ] || [ -z "${valid_key_id}" ]; then + for old_kid in $(printf '%s' "${keys_json}" | jq -r --arg kname "$NB_SETUP_KEY_NAME" '.[] | select(.name == $kname) | .id'); do + curl "${CURL_OPTS[@]}" -X DELETE -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys/${old_kid}" >/dev/null || true + done + key_payload=$(jq -n \ + --arg name "$NB_SETUP_KEY_NAME" \ + --arg rg "$router_group_id" \ + '{ + name: $name, + type: "reusable", + expires_in: 31536000, + revoked: false, + auto_groups: [$rg], + usage_limit: 0, + ephemeral: false + }') + key_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/setup-keys" \ + -H "Authorization: Token ${active_token}" \ + -H "Content-Type: application/json" \ + -d "${key_payload}") + active_setup_key=$(printf '%s' "${key_resp}" | jq -r '.key // empty') + if [ -z "${active_setup_key}" ]; then + echo "Не удалось создать ключ установки через /api/setup-keys" >&2 + exit 1 + fi + setup_key_updated=1 + fi + + active_remna_key="${NB_REMNAWAVE_SETUP_KEY:-}" + valid_remna_id=$(printf '%s' "${keys_json}" | jq -r --arg kname "$NB_REMNAWAVE_KEY_NAME" '.[] | select(.name == $kname and .valid == true and .revoked == false) | .id' | head -n 1) + if [ -z "${active_remna_key}" ] || [ -z "${valid_remna_id}" ]; then + fresh_keys=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys") + for old_kid in $(printf '%s' "${fresh_keys}" | jq -r --arg kname "$NB_REMNAWAVE_KEY_NAME" '.[] | select(.name == $kname) | .id'); do + curl "${CURL_OPTS[@]}" -X DELETE -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys/${old_kid}" >/dev/null || true + done + key_payload=$(jq -n \ + --arg name "$NB_REMNAWAVE_KEY_NAME" \ + --arg gid "$remna_group_id" \ + '{ + name: $name, + type: "reusable", + expires_in: 31536000, + revoked: false, + auto_groups: [$gid], + usage_limit: 0, + ephemeral: false + }') + key_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/setup-keys" \ + -H "Authorization: Token ${active_token}" \ + -H "Content-Type: application/json" \ + -d "${key_payload}") + active_remna_key=$(printf '%s' "${key_resp}" | jq -r '.key // empty') + if [ -z "${active_remna_key}" ]; then + echo "Не удалось создать ключ установки remnawave-peers" >&2 + exit 1 + fi + remna_key_updated=1 + fi + + jq -n \ + --arg api_token "${active_token}" \ + --arg setup_key "${active_setup_key}" \ + --arg remna_key "${active_remna_key}" \ + --argjson admin_created "${admin_created}" \ + --argjson token_updated "${token_updated}" \ + --argjson setup_key_updated "${setup_key_updated}" \ + --argjson remna_key_updated "${remna_key_updated}" \ + --argjson route_created "${route_created}" \ + '{ + api_token: $api_token, + setup_key: $setup_key, + remnawave_setup_key: $remna_key, + admin_created: ($admin_created == 1), + token_updated: ($token_updated == 1), + setup_key_updated: ($setup_key_updated == 1), + remnawave_setup_key_updated: ($remna_key_updated == 1), + route_created: ($route_created == 1), + changed: (($admin_created + $token_updated + $setup_key_updated + $remna_key_updated + $route_created) > 0) + }' + args: + executable: /bin/bash + environment: + NB_ADMIN_EMAIL: "{{ netbird_admin_email }}" + NB_ADMIN_NAME: "{{ netbird_admin_name }}" + NB_ADMIN_PASSWORD: "{{ netbird_admin_password }}" + NB_API_TOKEN: "{{ netbird_api_token }}" + NB_SETUP_KEY: "{{ netbird_setup_key }}" + NB_ROUTER_GROUP: "{{ netbird_router_group_name }}" + NB_SETUP_KEY_NAME: "{{ netbird_setup_key_name }}" + NB_REMNAWAVE_GROUP: "{{ netbird_remnawave_group_name }}" + NB_REMNAWAVE_KEY_NAME: "{{ netbird_remnawave_setup_key_name }}" + NB_REMNAWAVE_SETUP_KEY: "{{ netbird_remnawave_setup_key }}" + NB_LAN_NET_ID: "{{ netbird_lan_network_id }}" + NB_LAN_SUBNET: "{{ netbird_lan_subnet }}" + register: netbird_bootstrap_raw + changed_when: (netbird_bootstrap_raw.stdout | from_json).changed + no_log: true + +- name: Сохранить системный токен NetBird в фактах + ansible.builtin.set_fact: + netbird_bootstrap_state: "{{ netbird_bootstrap_raw.stdout | from_json }}" + netbird_effective_api_token: "{{ (netbird_bootstrap_raw.stdout | from_json).api_token }}" + no_log: true + +- name: Сохранить системные ключи NetBird в Infisical + ansible.builtin.shell: | + set -euo pipefail + infisical secrets set \ + --domain="${INFISICAL_DOMAIN}" \ + --token="${INFISICAL_TOKEN}" \ + --env=prod \ + --projectId="${INFISICAL_PROJECT_ID}" \ + --path="/services/netbird" \ + "NETBIRD_API_TOKEN=${NEW_API_TOKEN}" \ + "NETBIRD_SETUP_KEY=${NEW_SETUP_KEY}" \ + "NETBIRD_REMNAWAVE_SETUP_KEY=${NEW_REMNAWAVE_SETUP_KEY}" \ + --silent + args: + executable: /bin/bash + delegate_to: localhost + become: false + environment: + NEW_API_TOKEN: "{{ netbird_bootstrap_state.api_token }}" + NEW_SETUP_KEY: "{{ netbird_bootstrap_state.setup_key }}" + NEW_REMNAWAVE_SETUP_KEY: "{{ netbird_bootstrap_state.remnawave_setup_key }}" + when: >- + netbird_bootstrap_state.token_updated or + netbird_bootstrap_state.setup_key_updated or + netbird_bootstrap_state.remnawave_setup_key_updated + changed_when: true + no_log: true diff --git a/ansible/roles/rus-central/netbird/tasks/main.yml b/ansible/roles/rus-central/netbird/tasks/main.yml index 04718cc..373b2bd 100644 --- a/ansible/roles/rus-central/netbird/tasks/main.yml +++ b/ansible/roles/rus-central/netbird/tasks/main.yml @@ -99,459 +99,11 @@ delay: 5 until: instance_status.rc == 0 and ('setup_required' in instance_status.stdout) -- name: Инициализировать NetBird API (администратор, токен, группа, маршрут и ключ установки) - ansible.builtin.shell: | - set -euo pipefail +- name: Настроить системный каркас NetBird + ansible.builtin.include_tasks: bootstrap.yml - API_BASE="https://{{ netbird_domain }}/api" - CURL_OPTS=(-fsSk --resolve "{{ netbird_domain }}:443:127.0.0.1") - - admin_created=0 - token_updated=0 - setup_key_updated=0 - route_created=0 - exit_group_updated=0 - exit_route_created=0 - exit_dns_created=0 - exit_clients_key_updated=0 - - setup_req=$(curl "${CURL_OPTS[@]}" "${API_BASE}/instance" | jq -r '.setup_required') - bootstrap_auth_header="" - - if [ "${setup_req}" = "true" ]; then - setup_payload=$(jq -n \ - --arg email "$NB_ADMIN_EMAIL" \ - --arg password "$NB_ADMIN_PASSWORD" \ - --arg name "$NB_ADMIN_NAME" \ - '{email: $email, password: $password, name: $name, create_pat: true}') - setup_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/setup" \ - -H "Content-Type: application/json" \ - -d "${setup_payload}") - bootstrap_token=$(printf '%s' "${setup_resp}" | jq -r '.personal_access_token // empty') - if [ -z "${bootstrap_token}" ]; then - echo "Не удалось получить personal_access_token из /api/setup" >&2 - exit 1 - fi - bootstrap_auth_header="Authorization: Token ${bootstrap_token}" - admin_created=1 - fi - - active_token="${NB_API_TOKEN:-}" - token_valid=0 - if [ -n "${active_token}" ]; then - http_code=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{http_code}' \ - -H "Authorization: Token ${active_token}" "${API_BASE}/users" || true) - if [ "${http_code}" = "200" ]; then - token_valid=1 - fi - fi - - if [ "${token_valid}" -eq 0 ]; then - if [ -z "${bootstrap_auth_header}" ]; then - verifier="dBjftJeZ4CVP-mB92K27uhbUJU1p1r_wW1gFWFOEjXk" - challenge="E9Melhoa2OwvFrEMTJguCHaoeK1t8URWbuGJSstw-cM" - redirect_uri="https://{{ netbird_domain }}/nb-auth" - auth_url="https://{{ netbird_domain }}/oauth2/auth?client_id=netbird-dashboard&redirect_uri=${redirect_uri}&response_type=code&scope=openid+profile+email+groups&code_challenge=${challenge}&code_challenge_method=S256&state=ansible" - - login_page=$(curl -sLk --resolve "{{ netbird_domain }}:443:127.0.0.1" "${auth_url}") - login_action=$(printf '%s' "${login_page}" | grep -oE 'action="[^"]+"' | head -n 1 | cut -d'"' -f2 | sed 's/&/\&/g') - if [ -z "${login_action}" ]; then - echo "Не удалось получить форму входа OAuth2 для выпуска токена" >&2 - exit 1 - fi - - redir_loc=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{redirect_url}' \ - -X POST "https://{{ netbird_domain }}${login_action}" \ - --data-urlencode "login=${NB_ADMIN_EMAIL}" \ - --data-urlencode "password=${NB_ADMIN_PASSWORD}") - - if printf '%s' "${redir_loc}" | grep -q '/oauth2/approval'; then - redir_loc=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" -o /dev/null -w '%{redirect_url}' "${redir_loc}") - fi - - auth_code=$(printf '%s' "${redir_loc}" | sed -n 's/.*[?&]code=\([^&]*\).*/\1/p') - if [ -z "${auth_code}" ]; then - echo "Не удалось получить authorization code через OAuth2 (проверьте NETBIRD_ADMIN_EMAIL / NETBIRD_ADMIN_PASSWORD)" >&2 - exit 1 - fi - - token_resp=$(curl "${CURL_OPTS[@]}" -X POST "https://{{ netbird_domain }}/oauth2/token" \ - --data-urlencode "grant_type=authorization_code" \ - --data-urlencode "client_id=netbird-dashboard" \ - --data-urlencode "redirect_uri=${redirect_uri}" \ - --data-urlencode "code=${auth_code}" \ - --data-urlencode "code_verifier=${verifier}") - jwt_token=$(printf '%s' "${token_resp}" | jq -r '.access_token // .id_token // empty') - if [ -z "${jwt_token}" ]; then - echo "Не удалось обменять authorization code на JWT токен" >&2 - exit 1 - fi - bootstrap_auth_header="Authorization: Bearer ${jwt_token}" - fi - - users_json=$(curl "${CURL_OPTS[@]}" -H "${bootstrap_auth_header}" "${API_BASE}/users") - svc_user_id=$(printf '%s' "${users_json}" | jq -r '.[] | select(.is_service_user == true and .name == "infra-automation") | .id' | head -n 1) - - if [ -z "${svc_user_id}" ]; then - user_payload=$(jq -n '{name: "infra-automation", role: "admin", auto_groups: [], is_service_user: true}') - svc_user_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users" \ - -H "${bootstrap_auth_header}" \ - -H "Content-Type: application/json" \ - -d "${user_payload}") - svc_user_id=$(printf '%s' "${svc_user_resp}" | jq -r '.id') - fi - - old_tokens=$(curl "${CURL_OPTS[@]}" -H "${bootstrap_auth_header}" "${API_BASE}/users/${svc_user_id}/tokens" || echo "[]") - for old_tid in $(printf '%s' "${old_tokens}" | jq -r '.[] | select(.name == "infisical-ci-token") | .id'); do - curl "${CURL_OPTS[@]}" -X DELETE -H "${bootstrap_auth_header}" "${API_BASE}/users/${svc_user_id}/tokens/${old_tid}" >/dev/null || true - done - - pat_payload=$(jq -n '{name: "infisical-ci-token", expires_in: 365}') - pat_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/users/${svc_user_id}/tokens" \ - -H "${bootstrap_auth_header}" \ - -H "Content-Type: application/json" \ - -d "${pat_payload}") - active_token=$(printf '%s' "${pat_resp}" | jq -r '.plain_token // .plain_text // empty') - if [ -z "${active_token}" ]; then - echo "Не удалось выпустить сервисный токен через /api/users/${svc_user_id}/tokens" >&2 - exit 1 - fi - token_updated=1 - fi - - nb_api_call() { - local method="$1" path="$2" body="$3" - local resp code - resp=$(curl -sSk --resolve "{{ netbird_domain }}:443:127.0.0.1" \ - -w '\n%{http_code}' -X "$method" "${API_BASE}${path}" \ - -H "Authorization: Token ${active_token}" \ - -H "Content-Type: application/json" \ - -d "${body}") - code=${resp##*$'\n'} - resp=${resp%$'\n'*} - if [ "${code}" != "200" ] && [ "${code}" != "201" ]; then - echo "NetBird API ${method} ${path} failed (HTTP ${code}): ${resp}" >&2 - return 1 - fi - printf '%s' "${resp}" - } - - peers_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/peers") - ger_peer_id=$(printf '%s' "${peers_json}" | jq -r --arg h "$NB_EXIT_PEER_HOST" '.[] | select((.name // "") == $h or (.hostname // "") == $h) | .id' | head -n 1) - - groups_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/groups") - all_group_id=$(printf '%s' "${groups_json}" | jq -r '.[] | select(.name == "All") | .id' | head -n 1) - router_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "$NB_ROUTER_GROUP" '.[] | select(.name == $g) | .id' | head -n 1) - - if [ -z "${router_group_id}" ]; then - grp_payload=$(jq -n --arg name "$NB_ROUTER_GROUP" '{name: $name, peers: []}') - grp_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/groups" \ - -H "Authorization: Token ${active_token}" \ - -H "Content-Type: application/json" \ - -d "${grp_payload}") - router_group_id=$(printf '%s' "${grp_resp}" | jq -r '.id') - fi - - remna_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "$NB_REMNAWAVE_GROUP" '.[] | select(.name == $g) | .id' | head -n 1) - if [ -z "${remna_group_id}" ]; then - grp_payload=$(jq -n --arg name "$NB_REMNAWAVE_GROUP" '{name: $name, peers: []}') - grp_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/groups" \ - -H "Authorization: Token ${active_token}" \ - -H "Content-Type: application/json" \ - -d "${grp_payload}") - remna_group_id=$(printf '%s' "${grp_resp}" | jq -r '.id // empty') - if [ -z "${remna_group_id}" ]; then - echo "Не удалось создать группу NetBird для Remnawave" >&2 - exit 1 - fi - fi - - exit_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "$NB_EXIT_GROUP" '.[] | select(.name == $g) | .id' | head -n 1) - if [ -z "${exit_group_id}" ]; then - if [ -n "${ger_peer_id}" ]; then - grp_payload=$(jq -n --arg name "$NB_EXIT_GROUP" --arg pid "$ger_peer_id" '{name: $name, peers: [$pid]}') - else - grp_payload=$(jq -n --arg name "$NB_EXIT_GROUP" '{name: $name, peers: []}') - fi - grp_resp=$(nb_api_call POST "/groups" "${grp_payload}") - exit_group_id=$(printf '%s' "${grp_resp}" | jq -r '.id // empty') - if [ -z "${exit_group_id}" ]; then - echo "Не удалось создать группу NetBird ${NB_EXIT_GROUP}" >&2 - exit 1 - fi - exit_group_updated=1 - elif [ -n "${ger_peer_id}" ]; then - exit_grp_obj=$(printf '%s' "${groups_json}" | jq -c --arg id "$exit_group_id" '.[] | select(.id == $id)') - has_ger=$(printf '%s' "${exit_grp_obj}" | jq -r --arg pid "$ger_peer_id" '[.peers[]?.id] | index($pid) != null') - if [ "${has_ger}" != "true" ]; then - upd_payload=$(printf '%s' "${exit_grp_obj}" | jq -c --arg pid "$ger_peer_id" '{name: .name, peers: (([.peers[]?.id] + [$pid]) | unique)}') - nb_api_call PUT "/groups/${exit_group_id}" "${upd_payload}" >/dev/null - exit_group_updated=1 - fi - fi - - exit_clients_group_id=$(printf '%s' "${groups_json}" | jq -r --arg g "$NB_EXIT_CLIENTS_GROUP" '.[] | select(.name == $g) | .id' | head -n 1) - if [ -z "${exit_clients_group_id}" ]; then - grp_payload=$(jq -n --arg name "$NB_EXIT_CLIENTS_GROUP" '{name: $name, peers: []}') - grp_resp=$(nb_api_call POST "/groups" "${grp_payload}") - exit_clients_group_id=$(printf '%s' "${grp_resp}" | jq -r '.id // empty') - if [ -z "${exit_clients_group_id}" ]; then - echo "Не удалось создать группу NetBird ${NB_EXIT_CLIENTS_GROUP}" >&2 - exit 1 - fi - fi - - routes_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/routes") - existing_route_id=$(printf '%s' "${routes_json}" | jq -r --arg nid "$NB_LAN_NET_ID" --arg net "$NB_LAN_SUBNET" '.[] | select(.network_id == $nid or .network == $net) | .id' | head -n 1) - - if [ -z "${existing_route_id}" ]; then - route_payload=$(jq -n \ - --arg desc "Homelab LAN (${NB_LAN_SUBNET} via OPNsense)" \ - --arg nid "$NB_LAN_NET_ID" \ - --arg net "$NB_LAN_SUBNET" \ - --arg rg "$router_group_id" \ - --arg ag "$all_group_id" \ - '{ - description: $desc, - network_id: $nid, - enabled: true, - peer_groups: [$rg], - network: $net, - metric: 100, - masquerade: true, - groups: [$ag], - keep_route: true - }') - curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/routes" \ - -H "Authorization: Token ${active_token}" \ - -H "Content-Type: application/json" \ - -d "${route_payload}" >/dev/null - route_created=1 - fi - - existing_exit_route_id=$(printf '%s' "${routes_json}" | jq -r --arg nid "$NB_EXIT_NET_ID" '.[] | select(.network_id == $nid and .network == "0.0.0.0/0") | .id' | head -n 1) - if [ -z "${existing_exit_route_id}" ] && [ -n "${exit_group_id}" ] && [ -n "${ger_peer_id}" ]; then - exit_route_payload=$(jq -n \ - --arg desc "Exit Node DE (0.0.0.0/0 via ${NB_EXIT_PEER_HOST})" \ - --arg nid "$NB_EXIT_NET_ID" \ - --arg eg "$exit_group_id" \ - --arg cg "$exit_clients_group_id" \ - '{ - description: $desc, - network_id: $nid, - enabled: true, - peer_groups: [$eg], - network: "0.0.0.0/0", - metric: 100, - masquerade: true, - groups: [$cg], - keep_route: true, - skip_auto_apply: false - }') - nb_api_call POST "/routes" "${exit_route_payload}" >/dev/null - exit_route_created=1 - fi - - ns_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/dns/nameservers") - existing_exit_dns_id=$(printf '%s' "${ns_json}" | jq -r --arg n "$NB_EXIT_DNS_NAME" '.[] | select(.name == $n) | .id' | head -n 1) - if [ -z "${existing_exit_dns_id}" ]; then - dns_payload=$(jq -n \ - --arg name "$NB_EXIT_DNS_NAME" \ - --arg cg "$exit_clients_group_id" \ - --argjson servers "$NB_EXIT_DNS_SERVERS" \ - '{ - name: $name, - description: "Public DNS for Exit Node clients", - nameservers: ($servers | map({ip: ., ns_type: "udp", port: 53})), - enabled: true, - groups: [$cg], - primary: true, - domains: [], - search_domains_enabled: false - }') - nb_api_call POST "/dns/nameservers" "${dns_payload}" >/dev/null - exit_dns_created=1 - fi - - active_setup_key="${NB_SETUP_KEY:-}" - keys_json=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys") - valid_key_id=$(printf '%s' "${keys_json}" | jq -r --arg kname "$NB_SETUP_KEY_NAME" '.[] | select(.name == $kname and .valid == true and .revoked == false) | .id' | head -n 1) - - if [ -z "${active_setup_key}" ] || [ -z "${valid_key_id}" ]; then - for old_kid in $(printf '%s' "${keys_json}" | jq -r --arg kname "$NB_SETUP_KEY_NAME" '.[] | select(.name == $kname) | .id'); do - curl "${CURL_OPTS[@]}" -X DELETE -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys/${old_kid}" >/dev/null || true - done - key_payload=$(jq -n \ - --arg name "$NB_SETUP_KEY_NAME" \ - --arg rg "$router_group_id" \ - '{ - name: $name, - type: "reusable", - expires_in: 31536000, - revoked: false, - auto_groups: [$rg], - usage_limit: 0, - ephemeral: false - }') - key_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/setup-keys" \ - -H "Authorization: Token ${active_token}" \ - -H "Content-Type: application/json" \ - -d "${key_payload}") - active_setup_key=$(printf '%s' "${key_resp}" | jq -r '.key // empty') - if [ -z "${active_setup_key}" ]; then - echo "Не удалось создать ключ установки через /api/setup-keys" >&2 - exit 1 - fi - setup_key_updated=1 - fi - - active_remna_key="${NB_REMNAWAVE_SETUP_KEY:-}" - remna_key_updated=0 - valid_remna_id=$(printf '%s' "${keys_json}" | jq -r --arg kname "$NB_REMNAWAVE_KEY_NAME" '.[] | select(.name == $kname and .valid == true and .revoked == false) | .id' | head -n 1) - if [ -z "${active_remna_key}" ] || [ -z "${valid_remna_id}" ]; then - fresh_keys=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys") - for old_kid in $(printf '%s' "${fresh_keys}" | jq -r --arg kname "$NB_REMNAWAVE_KEY_NAME" '.[] | select(.name == $kname) | .id'); do - curl "${CURL_OPTS[@]}" -X DELETE -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys/${old_kid}" >/dev/null || true - done - key_payload=$(jq -n \ - --arg name "$NB_REMNAWAVE_KEY_NAME" \ - --arg gid "$remna_group_id" \ - '{ - name: $name, - type: "reusable", - expires_in: 31536000, - revoked: false, - auto_groups: [$gid], - usage_limit: 0, - ephemeral: false - }') - key_resp=$(curl "${CURL_OPTS[@]}" -X POST "${API_BASE}/setup-keys" \ - -H "Authorization: Token ${active_token}" \ - -H "Content-Type: application/json" \ - -d "${key_payload}") - active_remna_key=$(printf '%s' "${key_resp}" | jq -r '.key // empty') - if [ -z "${active_remna_key}" ]; then - echo "Не удалось создать ключ установки remnawave-peers" >&2 - exit 1 - fi - remna_key_updated=1 - fi - - active_exit_clients_key="${NB_EXIT_CLIENTS_SETUP_KEY:-}" - valid_exit_client_id=$(printf '%s' "${keys_json}" | jq -r --arg kname "$NB_EXIT_CLIENTS_KEY_NAME" '.[] | select(.name == $kname and .valid == true and .revoked == false) | .id' | head -n 1) - if [ -z "${active_exit_clients_key}" ] || [ -z "${valid_exit_client_id}" ]; then - fresh_keys=$(curl "${CURL_OPTS[@]}" -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys") - for old_kid in $(printf '%s' "${fresh_keys}" | jq -r --arg kname "$NB_EXIT_CLIENTS_KEY_NAME" '.[] | select(.name == $kname) | .id'); do - curl "${CURL_OPTS[@]}" -X DELETE -H "Authorization: Token ${active_token}" "${API_BASE}/setup-keys/${old_kid}" >/dev/null || true - done - key_payload=$(jq -n \ - --arg name "$NB_EXIT_CLIENTS_KEY_NAME" \ - --arg gid "$exit_clients_group_id" \ - '{ - name: $name, - type: "reusable", - expires_in: 31536000, - revoked: false, - auto_groups: [$gid], - usage_limit: 0, - ephemeral: false - }') - key_resp=$(nb_api_call POST "/setup-keys" "${key_payload}") - active_exit_clients_key=$(printf '%s' "${key_resp}" | jq -r '.key // empty') - if [ -z "${active_exit_clients_key}" ]; then - echo "Не удалось создать ключ установки ${NB_EXIT_CLIENTS_KEY_NAME}" >&2 - exit 1 - fi - exit_clients_key_updated=1 - fi - - jq -n \ - --arg api_token "${active_token}" \ - --arg setup_key "${active_setup_key}" \ - --arg remna_key "${active_remna_key}" \ - --arg exit_clients_key "${active_exit_clients_key}" \ - --argjson admin_created "${admin_created}" \ - --argjson token_updated "${token_updated}" \ - --argjson setup_key_updated "${setup_key_updated}" \ - --argjson remna_key_updated "${remna_key_updated}" \ - --argjson route_created "${route_created}" \ - --argjson exit_group_updated "${exit_group_updated}" \ - --argjson exit_route_created "${exit_route_created}" \ - --argjson exit_dns_created "${exit_dns_created}" \ - --argjson exit_clients_key_updated "${exit_clients_key_updated}" \ - '{ - api_token: $api_token, - setup_key: $setup_key, - remnawave_setup_key: $remna_key, - exit_clients_setup_key: $exit_clients_key, - admin_created: ($admin_created == 1), - token_updated: ($token_updated == 1), - setup_key_updated: ($setup_key_updated == 1), - remnawave_setup_key_updated: ($remna_key_updated == 1), - route_created: ($route_created == 1), - exit_group_updated: ($exit_group_updated == 1), - exit_route_created: ($exit_route_created == 1), - exit_dns_created: ($exit_dns_created == 1), - exit_clients_setup_key_updated: ($exit_clients_key_updated == 1), - changed: (($admin_created + $token_updated + $setup_key_updated + $remna_key_updated + $route_created + $exit_group_updated + $exit_route_created + $exit_dns_created + $exit_clients_key_updated) > 0) - }' - args: - executable: /bin/bash - environment: - NB_ADMIN_EMAIL: "{{ netbird_admin_email }}" - NB_ADMIN_NAME: "{{ netbird_admin_name }}" - NB_ADMIN_PASSWORD: "{{ netbird_admin_password }}" - NB_API_TOKEN: "{{ netbird_api_token }}" - NB_SETUP_KEY: "{{ netbird_setup_key }}" - NB_ROUTER_GROUP: "{{ netbird_router_group_name }}" - NB_SETUP_KEY_NAME: "{{ netbird_setup_key_name }}" - NB_REMNAWAVE_GROUP: "{{ netbird_remnawave_group_name }}" - NB_REMNAWAVE_KEY_NAME: "{{ netbird_remnawave_setup_key_name }}" - NB_REMNAWAVE_SETUP_KEY: "{{ netbird_remnawave_setup_key }}" - NB_LAN_NET_ID: "{{ netbird_lan_network_id }}" - NB_LAN_SUBNET: "{{ netbird_lan_subnet }}" - NB_EXIT_GROUP: "{{ netbird_exit_group_name }}" - NB_EXIT_PEER_HOST: "{{ netbird_exit_peer_hostname }}" - NB_EXIT_NET_ID: "{{ netbird_exit_route_network_id }}" - NB_EXIT_CLIENTS_GROUP: "{{ netbird_exit_clients_group_name }}" - NB_EXIT_CLIENTS_KEY_NAME: "{{ netbird_exit_clients_setup_key_name }}" - NB_EXIT_CLIENTS_SETUP_KEY: "{{ netbird_exit_clients_setup_key }}" - NB_EXIT_DNS_NAME: "{{ netbird_exit_dns_name }}" - NB_EXIT_DNS_SERVERS: "{{ netbird_exit_dns_servers | to_json }}" - register: netbird_bootstrap_raw - changed_when: (netbird_bootstrap_raw.stdout | from_json).changed - no_log: true - -- name: Сохранить сгенерированные ключи NetBird в Infisical - ansible.builtin.shell: | - set -euo pipefail - infisical secrets set \ - --domain="${INFISICAL_DOMAIN}" \ - --token="${INFISICAL_TOKEN}" \ - --env=prod \ - --projectId="${INFISICAL_PROJECT_ID}" \ - --path="/services/netbird" \ - "NETBIRD_API_TOKEN=${NEW_API_TOKEN}" \ - "NETBIRD_SETUP_KEY=${NEW_SETUP_KEY}" \ - "NETBIRD_REMNAWAVE_SETUP_KEY=${NEW_REMNAWAVE_SETUP_KEY}" \ - "NETBIRD_EXIT_CLIENTS_SETUP_KEY=${NEW_EXIT_CLIENTS_SETUP_KEY}" \ - --silent - args: - executable: /bin/bash - delegate_to: localhost - become: false - environment: - NEW_API_TOKEN: "{{ (netbird_bootstrap_raw.stdout | from_json).api_token }}" - NEW_SETUP_KEY: "{{ (netbird_bootstrap_raw.stdout | from_json).setup_key }}" - NEW_REMNAWAVE_SETUP_KEY: "{{ (netbird_bootstrap_raw.stdout | from_json).remnawave_setup_key }}" - NEW_EXIT_CLIENTS_SETUP_KEY: "{{ (netbird_bootstrap_raw.stdout | from_json).exit_clients_setup_key }}" - when: >- - (netbird_bootstrap_raw.stdout | from_json).token_updated or - (netbird_bootstrap_raw.stdout | from_json).setup_key_updated or - (netbird_bootstrap_raw.stdout | from_json).remnawave_setup_key_updated or - (netbird_bootstrap_raw.stdout | from_json).exit_clients_setup_key_updated - changed_when: true - no_log: true +- name: Синхронизировать доступ и клиентов NetBird из access.yml + ansible.builtin.include_tasks: access.yml # Токен перевыпускается ролью, поэтому регистрация берёт его из Infisical # при каждом прогоне: агент не остаётся с протухшим креденшлом. @@ -571,17 +123,14 @@ agent_vault_secret_key: NETBIRD_API_TOKEN - name: Итог установки - vars: - nb_state: "{{ netbird_bootstrap_raw.stdout | from_json }}" ansible.builtin.debug: msg: - "Панель: https://{{ netbird_domain }}" - - "Суперпользователь создан в этом прогоне: {{ nb_state.admin_created }}" - - "API-токен обновлен в Infisical: {{ nb_state.token_updated }}" - - "Setup-ключ обновлен в Infisical: {{ nb_state.setup_key_updated }}" - - "Ключ remnawave-peers обновлен в Infisical: {{ nb_state.remnawave_setup_key_updated }}" - - "Ключ exit-clients-key обновлен в Infisical: {{ nb_state.exit_clients_setup_key_updated }}" - - "Маршрут {{ netbird_lan_subnet }} создан в этом прогоне: {{ nb_state.route_created }}" - - "Группа {{ netbird_exit_group_name }} обновлена в этом прогоне: {{ nb_state.exit_group_updated }}" - - "Маршрут Exit Node 0.0.0.0/0 создан в этом прогоне: {{ nb_state.exit_route_created }}" - - "DNS {{ netbird_exit_dns_name }} создан в этом прогоне: {{ nb_state.exit_dns_created }}" + - "Суперпользователь создан в этом прогоне: {{ netbird_bootstrap_state.admin_created }}" + - "API-токен обновлен в Infisical: {{ netbird_bootstrap_state.token_updated }}" + - "Setup-ключ роутеров обновлен в Infisical: {{ netbird_bootstrap_state.setup_key_updated }}" + - "Ключ remnawave-peers обновлен в Infisical: {{ netbird_bootstrap_state.remnawave_setup_key_updated }}" + - "Маршрут {{ netbird_lan_subnet }} создан в этом прогоне: {{ netbird_bootstrap_state.route_created }}" + - "Изменения доступа (access.yml): {{ netbird_access_summary.changes }}" + - "Обновлённые секреты клиентов в Infisical: {{ netbird_access_summary.updated_secret_names }}" + - "Предупреждения access.yml: {{ netbird_access_summary.warnings }}"