feat(netbird): настроить ger-nod-02 как Exit Node для клиентов
This commit is contained in:
1 parent
2eadd3aedc
commit
3178455672
4 files changed
+303
-4
No files matched your search
@@ -24,3 +24,13 @@ 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"
|
||||
@@ -110,6 +110,10 @@
|
||||
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=""
|
||||
@@ -215,6 +219,26 @@
|
||||
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)
|
||||
@@ -242,6 +266,41 @@
|
||||
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)
|
||||
|
||||
@@ -270,6 +329,50 @@
|
||||
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)
|
||||
@@ -334,25 +437,63 @@
|
||||
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),
|
||||
changed: (($admin_created + $token_updated + $setup_key_updated + $remna_key_updated + $route_created) > 0)
|
||||
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
|
||||
@@ -369,6 +510,14 @@
|
||||
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
|
||||
@@ -385,6 +534,7 @@
|
||||
"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
|
||||
@@ -394,10 +544,12 @@
|
||||
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
|
||||
(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
|
||||
|
||||
@@ -428,4 +580,8 @@
|
||||
- "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 }}"
|
||||
Reference in new issue
Block a user