feat(ops): protect metrics, rate-limit auth, agent secret timing, e2e smoke
CI / changes (push) Successful in 7s
CI / openapi (push) Failing after 40s
CI / web (push) Successful in 56s
CI / commitlint (push) Skipped
CI / go (push) Failing after 34s
CI / bird2 (push) Skipped
CI / release (push) Skipped

Bearer для /metrics (EVOBGP_METRICS_TOKEN); rate limit /v1/auth/config; constant-time agent secret; OTel stub; Playwright smoke; HTTP_PROXY note; checklist обновлён.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
Denozordec
2026-07-31 12:29:00 +07:00
co-authored by Cursor
parent 26f5172f88
commit 6c6e76fca3
15 changed files with 224 additions and 6 deletions
+32
View File
@@ -0,0 +1,32 @@
import { test, expect } from '@playwright/test'
/**
* Smoke against local API (+ optional UI). Profile: compose microvps-full.
*
* Env:
* - EVOBGP_E2E_API_URL (default http://127.0.0.1:8080)
* - EVOBGP_E2E_BASE_URL (default http://127.0.0.1:5173)
* - EVOBGP_E2E_TOKEN (default dev)
*/
const apiBase = process.env.EVOBGP_E2E_API_URL ?? 'http://127.0.0.1:8080'
const uiBase = process.env.EVOBGP_E2E_BASE_URL ?? 'http://127.0.0.1:5173'
const token = process.env.EVOBGP_E2E_TOKEN ?? 'dev'
test.describe('EvoBGP smoke', () => {
test('API health and modules list', async ({ request }) => {
const health = await request.get(`${apiBase}/v1/health`)
expect(health.ok()).toBeTruthy()
const mods = await request.get(`${apiBase}/v1/modules?limit=10`, {
headers: { Authorization: `Bearer ${token}` },
})
expect([200, 401]).toContain(mods.status())
})
test('UI loads when available', async ({ page }) => {
test.skip(!process.env.EVOBGP_E2E_UI, 'set EVOBGP_E2E_UI=1 to enable UI smoke')
await page.goto(uiBase)
await page.waitForLoadState('domcontentloaded')
await expect(page.locator('body')).toBeVisible()
})
})
+2
View File
@@ -10,6 +10,7 @@
"typecheck": "tsr generate && tsc --noEmit",
"lint": "eslint .",
"test": "vitest run",
"test:e2e": "playwright test",
"openapi:gen": "openapi-typescript ../../docs/openapi.yaml -o src/types/api.gen.ts",
"openapi:check": "openapi-typescript ../../docs/openapi.yaml -o src/types/api.gen.check.ts && diff -q src/types/api.gen.ts src/types/api.gen.check.ts && rm -f src/types/api.gen.check.ts"
},
@@ -43,6 +44,7 @@
},
"devDependencies": {
"@eslint/js": "^9.0.0",
"@playwright/test": "^1.62.1",
"@tailwindcss/vite": "^4.1.0",
"@tanstack/router-cli": "^1.130.0",
"@tanstack/router-plugin": "^1.130.0",
+14
View File
@@ -0,0 +1,14 @@
import { defineConfig, devices } from '@playwright/test'
export default defineConfig({
testDir: './e2e',
timeout: 60_000,
fullyParallel: false,
forbidOnly: !!process.env.CI,
retries: process.env.CI ? 1 : 0,
use: {
baseURL: process.env.EVOBGP_E2E_BASE_URL ?? 'http://127.0.0.1:5173',
trace: 'on-first-retry',
},
projects: [{ name: 'chromium', use: { ...devices['Desktop Chrome'] } }],
})
File diff suppressed because one or more lines are too long
+1
View File
@@ -35,5 +35,6 @@ export default defineConfig({
test: {
environment: 'happy-dom',
globals: true,
exclude: ['**/node_modules/**', '**/e2e/**', '**/dist/**'],
},
})
+2
View File
@@ -52,6 +52,8 @@ func main() {
observability.SetBuildInfo(version.Version, version.GitSHA)
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
defer stop()
otelShutdown := observability.StartOTelIfEnabled(ctx)
defer func() { _ = otelShutdown(context.Background()) }()
srv.StartBackground(ctx)
startBirdMetricsPoller(ctx)
+6 -2
View File
@@ -17,8 +17,12 @@
- `EVOBGP_JOB_MAX_CONCURRENT=16`, `EVOBGP_DB_MAX_CONNS=25`, `EVOBGP_COLLECT_CONCURRENCY=16` при росте tenants.
- `EVOBGP_NODE_DISPATCH_INSECURE_TLS=0` — только валидный TLS к agent.
- Ограничить `/metrics` сетевой политикой или reverse proxy.
- Профиль `evobgp-all` или HA API + персистентная `job_audit` (PostgreSQL).
- Ограничить `/metrics`: `EVOBGP_METRICS_TOKEN` (Bearer) и/или сетевая политика / reverse proxy.
- `EVOBGP_AUTH_RATE_LIMIT` (default 60/min per IP) на `GET /v1/auth/config`.
- `HTTP_PROXY` / `HTTPS_PROXY` / `NO_PROXY` для CDN/DoH в censored сетях.
- Опционально `EVOBGP_OTEL_ENDPOINT` (stub opt-in для будущего OTel export).
- Профиль `evobgp-all` или HA API + персистентная `job_audit` (PostgreSQL SKIP LOCKED reclaim).
- E2E smoke: `pnpm --filter @evobgp/web run test:e2e` против API (`EVOBGP_E2E_API_URL`) / UI (`EVOBGP_E2E_UI=1`).
- Мониторинг drift: `evobgp-deploy`, `last_applied_revision_id` vs published.
## Не использовать в prod
+3 -1
View File
@@ -2,6 +2,7 @@ package agentserver
import (
"context"
"crypto/subtle"
"encoding/json"
"fmt"
"log"
@@ -157,7 +158,8 @@ func (s *Server) authorize(r *http.Request) bool {
if !strings.HasPrefix(h, prefix) {
return false
}
return strings.TrimSpace(h[len(prefix):]) == secret
got := strings.TrimSpace(h[len(prefix):])
return subtle.ConstantTimeCompare([]byte(got), []byte(secret)) == 1
}
func writeJSON(w http.ResponseWriter, status int, v any) {
+68
View File
@@ -0,0 +1,68 @@
package httpapi
import (
"net/http"
"os"
"strconv"
"strings"
"sync"
"time"
)
// authRateLimiter is a simple per-IP token bucket for public auth-ish endpoints.
type authRateLimiter struct {
mu sync.Mutex
hits map[string][]time.Time
limit int
window time.Duration
}
func newAuthRateLimiter() *authRateLimiter {
limit := 60
if n, err := strconv.Atoi(strings.TrimSpace(os.Getenv("EVOBGP_AUTH_RATE_LIMIT"))); err == nil && n > 0 {
limit = n
}
return &authRateLimiter{
hits: make(map[string][]time.Time),
limit: limit,
window: time.Minute,
}
}
func (l *authRateLimiter) allow(ip string) bool {
if l == nil {
return true
}
now := time.Now()
l.mu.Lock()
defer l.mu.Unlock()
cut := now.Add(-l.window)
arr := l.hits[ip]
kept := arr[:0]
for _, t := range arr {
if t.After(cut) {
kept = append(kept, t)
}
}
if len(kept) >= l.limit {
l.hits[ip] = kept
return false
}
kept = append(kept, now)
l.hits[ip] = kept
return true
}
func (s *Server) withAuthRateLimit(next http.HandlerFunc) http.HandlerFunc {
if s.authLimiter == nil {
s.authLimiter = newAuthRateLimiter()
}
return func(w http.ResponseWriter, r *http.Request) {
ip := clientIP(r)
if !s.authLimiter.allow(ip) {
writeProblem(w, http.StatusTooManyRequests, "Too Many Requests", "auth rate limit exceeded")
return
}
next(w, r)
}
}
+2 -2
View File
@@ -30,12 +30,12 @@ func (s *Server) Handler() http.Handler {
s.registerV1(v1)
wrappedV1 := http.StripPrefix("/v1", v1)
s.mux.Handle("GET /metrics", observability.MetricsHandler())
s.mux.Handle("GET /metrics", observability.ProtectMetrics(observability.MetricsHandler()))
s.mux.HandleFunc("GET /version", s.handleVersion)
s.mux.HandleFunc("GET /v1/health", s.handleHealth)
s.mux.HandleFunc("GET /v1/ready", s.handleReady)
s.mux.HandleFunc("GET /v1/version", s.handleVersion)
s.mux.HandleFunc("GET /v1/auth/config", s.handleAuthConfigPublic)
s.mux.HandleFunc("GET /v1/auth/config", s.withAuthRateLimit(s.handleAuthConfigPublic))
// Firewall subsystem moved to the standalone EvoFirewall service; see docs/firewall.md.
// Registered on the public mux so it wins over the "/v1/" subtree below regardless of auth.
s.mux.HandleFunc("/v1/firewall/", s.handleFirewallGone)
+1
View File
@@ -29,6 +29,7 @@ type Server struct {
maintConfig *maintenance.ConfigProvider
maintStats *maintenance.DBStatsProvider
jobs *jobs.Registry
authLimiter *authRateLimiter
bundlePriv ed25519.PrivateKey
keyResolver *apiKeyResolver
firewallResolver *firewallTokenResolver
+1
View File
@@ -14,6 +14,7 @@ import (
const DefaultTimeout = 45 * time.Second
// New returns an HTTP client with timeout and tuned idle connection pooling.
// Standard proxy env (HTTP_PROXY / HTTPS_PROXY / NO_PROXY) is honored via the cloned DefaultTransport.
func New(timeout time.Duration) *http.Client {
if timeout <= 0 {
timeout = DefaultTimeout
+32
View File
@@ -0,0 +1,32 @@
package observability
import (
"crypto/subtle"
"net/http"
"os"
"strings"
)
// ProtectMetrics wraps the Prometheus handler. When EVOBGP_METRICS_TOKEN is set,
// scrapes must send Authorization: Bearer <token> (constant-time compare).
// Empty token keeps /metrics open (dev / private network).
func ProtectMetrics(next http.Handler) http.Handler {
token := strings.TrimSpace(os.Getenv("EVOBGP_METRICS_TOKEN"))
if token == "" {
return next
}
want := []byte(token)
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
h := r.Header.Get("Authorization")
const p = "Bearer "
got := ""
if strings.HasPrefix(h, p) {
got = strings.TrimSpace(h[len(p):])
}
if subtle.ConstantTimeCompare([]byte(got), want) != 1 {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}
+21
View File
@@ -0,0 +1,21 @@
package observability
import (
"context"
"os"
"strings"
"evobgp/internal/logging"
)
// StartOTelIfEnabled is an opt-in stub for OpenTelemetry (EVOBGP_OTEL_ENDPOINT).
// Full exporter wiring lands when the ops stack provides a collector; this logs intent only.
func StartOTelIfEnabled(ctx context.Context) (shutdown func(context.Context) error) {
ep := strings.TrimSpace(os.Getenv("EVOBGP_OTEL_ENDPOINT"))
if ep == "" {
return func(context.Context) error { return nil }
}
logging.Default().Info("otel opt-in enabled (stub; export not wired yet)", "endpoint", ep)
_ = ctx
return func(context.Context) error { return nil }
}
+38
View File
@@ -117,6 +117,9 @@ importers:
'@eslint/js':
specifier: ^9.0.0
version: 9.39.4
'@playwright/test':
specifier: ^1.62.1
version: 1.62.1
'@tailwindcss/vite':
specifier: ^4.1.0
version: 4.3.2(vite@7.3.6(@types/node@22.20.0)(jiti@2.7.0)(lightningcss@1.32.0))
@@ -770,6 +773,11 @@ packages:
'@octokit/types@16.0.0':
resolution: {integrity: sha512-sKq+9r1Mm4efXW1FCk7hFSeJo4QKreL/tTbR0rz/qx/r1Oa2VV83LTA/H/MuCOX7uCIJmQVRKBcbmWoySjAnSg==}
'@playwright/test@1.62.1':
resolution: {integrity: sha512-DTcUc8qii+cpHvtOwggMtBRMjKZHXYWdw8syRYu2vtzuq4Wxphqq4NfCs5Zt44L6mA8rfDfj+PHnxFc/FeK6mQ==}
engines: {node: '>=20'}
hasBin: true
'@pnpm/config.env-replace@1.1.0':
resolution: {integrity: sha512-htyl8TWnKL7K/ESFa1oW2UB5lVDxuF5DpM7tBi6Hu2LNL3mWkIzNLG6N4zoCUP1lCKNxWy/3iu8mS8MvToGd6w==}
engines: {node: '>=12.22.0'}
@@ -2239,6 +2247,11 @@ packages:
resolution: {integrity: sha512-w8ZNZr2mKIc7qeNaQ9AVPT1+iFaI+Avd4xudVOvdDJ8VytREi1Ft5Ih7hd9jjehod8vAM5GMsfQ/TpPf4EyoEA==}
engines: {node: '>=14.14'}
fsevents@2.3.2:
resolution: {integrity: sha512-xiqMQR4xAeHTuB9uWm+fFRcIOgKBMiOBP+eXiyT7jsgVCq1bkVygt00oASowB7EdtpOHaaPgKt812P9ab+DDKA==}
engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
os: [darwin]
fsevents@2.3.3:
resolution: {integrity: sha512-5xoDfX+fL7faATnagmWPpbFtwh/R77WmMMqqHGS65C3vvB0YHrgF+B1YmZ3441tMj5n63k0212XNoJwzlhffQw==}
engines: {node: ^8.16.0 || ^10.6.0 || >=11.0.0}
@@ -3124,6 +3137,16 @@ packages:
resolution: {integrity: sha512-C+VUP+8jis7EsQZIhDYmS5qlNtjv2yP4SNtjXK9AP1ZcTRlnSfuumaTnRfYZnYgUUYVIKqL0fRvmUGDV2fmp6g==}
engines: {node: '>=4'}
playwright-core@1.62.1:
resolution: {integrity: sha512-wPYSwEBJY9GHraISXqyqtx0na0LpO3XEX7jNDhntbex7tzUS7kLnZsOlFruFJB4Hi/rhDMjXGqHewDZ68nYZVw==}
engines: {node: '>=20'}
hasBin: true
playwright@1.62.1:
resolution: {integrity: sha512-0M+L3LAD8/nm554LOla9Ayx0j0tmFZ0FBcoQ7F1VuVHpM/XpiC8RcDzBQB8W5+hA8L22THxELzeF+2WcUzvcLg==}
engines: {node: '>=20'}
hasBin: true
pluralize@8.0.0:
resolution: {integrity: sha512-Nc3IT5yHzflTfbjgqWcCPpo7DaKy4FnpB0l/zCAW0Tc7jxAiuqSxHasntB3D7887LSrA93kDJ9IXovxJYxyLCA==}
engines: {node: '>=4'}
@@ -4492,6 +4515,10 @@ snapshots:
dependencies:
'@octokit/openapi-types': 27.0.0
'@playwright/test@1.62.1':
dependencies:
playwright: 1.62.1
'@pnpm/config.env-replace@1.1.0': {}
'@pnpm/network.ca-file@1.0.2':
@@ -5996,6 +6023,9 @@ snapshots:
jsonfile: 6.2.1
universalify: 2.0.1
fsevents@2.3.2:
optional: true
fsevents@2.3.3:
optional: true
@@ -6688,6 +6718,14 @@ snapshots:
find-up: 2.1.0
load-json-file: 4.0.0
playwright-core@1.62.1: {}
playwright@1.62.1:
dependencies:
playwright-core: 1.62.1
optionalDependencies:
fsevents: 2.3.2
pluralize@8.0.0: {}
postcss@8.5.16: