feat(api): implement self-update mechanism for Linux agents
- Added a `maybe_self_update` function in `evofw-firewall.sh` to allow agents to pull the latest version of the sync script from the server, enhancing the agent's ability to stay updated. - Updated the `/v1/agent/sync-script` endpoint to return ETag and script SHA256 headers, enabling efficient caching and conditional requests. - Modified the agent policy response to include `script_sha256`, providing visibility into the current version of the sync script. - Enhanced tests to verify the self-update functionality and ensure correct behavior of the sync script endpoint. These changes improve the maintainability and reliability of Linux agents by enabling automatic updates of critical scripts.
This commit is contained in:
+16
-1
@@ -42,6 +42,21 @@ Install сам ставит зависимости через apt/dnf/yum/apk: `
|
||||
|
||||
Если `/etc/evofw/agent.conf` уже есть — install переходит в **update**: скачивает свежий `sync-script` + `uninstall.sh`, перезаписывает unit/timer, оставляет токен. `EVOFW_INSTALL_FORCE=1` — полный re-enroll (новый токен; для уже Approved install-link обычно не сработает).
|
||||
|
||||
### Linux self-update (sync-script)
|
||||
|
||||
После того как на хосте стоит скрипт с `maybe_self_update`, агент **сам** подтягивает новую версию при каждом timer (~1 мин), **до** `GET /v1/agent/policy` (pending тоже обновляются):
|
||||
|
||||
1. sha256 локального `/usr/local/sbin/evofw-firewall.sh`
|
||||
2. `GET /v1/agent/sync-script` с `If-None-Match: "<sha256>"` → **304** = без изменений
|
||||
3. **200**: проверка shebang + sha256 тела → `install -m 755`, сброс `last_hash`, `exec EVOFW_SKIP_SELF_UPDATE=1` новой копии
|
||||
4. Ошибка download/verify — лог и продолжение **текущим** скриптом
|
||||
|
||||
`GET /v1/agent/policy` содержит `script_sha256` (не входит в `policy.hash`).
|
||||
|
||||
**Bootstrap:** агенты без `maybe_self_update` не умеют самообновляться. После деплоя API — **один** re-run install-ссылки (или ручная замена `sync-script`). Дальше curl не нужен.
|
||||
|
||||
MikroTik scheduler **не** обновляется этим путём — только повторный import install `.rsc`.
|
||||
|
||||
**Uninstall (Linux):**
|
||||
```bash
|
||||
curl -fsSL https://<cp>/v1/agent/uninstall.sh | bash
|
||||
@@ -77,7 +92,7 @@ IPv6 skipped.
|
||||
- `GET /api/v1/agents/:id/blocked-ports` — aggregate top 50 портов; в `blocked-ips` у каждого IP — `ports` top 5.
|
||||
- UI: **Top ports** + колонка Ports в Blocked IPs (только `platform=linux`).
|
||||
- **ipset/iptables:** `port_hits: []`. MikroTik — без port hits.
|
||||
- Чтобы подтянуть правила на уже установленном агенте: **re-run install one-liner** (см. выше).
|
||||
- Агенты с self-update подтянут nft-правила сами; без него — **один** re-run install one-liner (см. выше).
|
||||
|
||||
IPv6 skipped.
|
||||
|
||||
|
||||
@@ -17,9 +17,10 @@
|
||||
|
||||
1. **Enroll** — `POST /v1/agent/enroll` + `X-EvoFW-Seed` → pending agent
|
||||
2. **Approve** — UI/API → status approved
|
||||
3. **Policy** — `GET /v1/agent/policy` → deny/allow CIDRs + `default_action` + optional `port_rules` + hash (`apply_version: 3`)
|
||||
4. **Apply** — agent пишет kernel rules (L3 + L4 port ACL на nft), `POST /v1/agent/apply-report` + stats + optional `host_firewall` snapshot
|
||||
5. **Lists refresh** — cron каждые 5 мин (json_url / domains / evobgp_community)
|
||||
3. **Policy** — `GET /v1/agent/policy` → deny/allow CIDRs + `default_action` + optional `port_rules` + hash (`apply_version: 3`) + `script_sha256` (Linux; не в `policy.hash`)
|
||||
4. **Linux self-update** — timer: `GET /v1/agent/sync-script` (`ETag` / `If-None-Match`) → при новой версии заменить `/usr/local/sbin/evofw-firewall.sh` и `exec` до policy
|
||||
5. **Apply** — agent пишет kernel rules (L3 + L4 port ACL на nft), `POST /v1/agent/apply-report` + stats + optional `host_firewall` snapshot
|
||||
6. **Lists refresh** — cron каждые 5 мин (json_url / domains / evobgp_community)
|
||||
|
||||
## Политика
|
||||
|
||||
|
||||
+23
-1
@@ -675,6 +675,28 @@ paths:
|
||||
'200':
|
||||
description: Shell script
|
||||
|
||||
/v1/agent/sync-script:
|
||||
get:
|
||||
summary: Linux sync agent (evofw-firewall.sh)
|
||||
tags: [agent-public]
|
||||
parameters:
|
||||
- name: If-None-Match
|
||||
in: header
|
||||
schema: { type: string }
|
||||
description: ETag from a previous GET (`"<sha256>"`)
|
||||
responses:
|
||||
'200':
|
||||
description: Shell script
|
||||
headers:
|
||||
ETag:
|
||||
schema: { type: string }
|
||||
description: '"<sha256 of file bytes>"'
|
||||
X-Evofw-Script-Sha256:
|
||||
schema: { type: string }
|
||||
description: Hex sha256 of the script body
|
||||
'304':
|
||||
description: Script unchanged
|
||||
|
||||
/v1/agent/enroll:
|
||||
post:
|
||||
summary: Enroll agent (X-EvoFW-Seed)
|
||||
@@ -690,7 +712,7 @@ paths:
|
||||
security: [{ agentToken: [] }]
|
||||
responses:
|
||||
'200':
|
||||
description: Policy
|
||||
description: Policy (includes script_sha256 for Linux self-update; not part of policy.hash)
|
||||
|
||||
/v1/agent/policy.rsc:
|
||||
get:
|
||||
|
||||
Reference in New Issue
Block a user