mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-01 20:55:16 +09:00
Merge pull request #195 from jhd3197/feat/agent-permission-levels
feat(agents): permission levels (monitor / protect / full) with signed health reporting
This commit is contained in:
38 files changed
+1669
-27
No files matched your search
@@ -22,6 +22,15 @@ release history; historical `agent-v*` tags are not panel releases.
|
||||
|
||||
### Added
|
||||
|
||||
- Agent permission levels (Monitor / Protect / Full), enforced by the agent and
|
||||
reported to the panel. Adds a signed `POST /api/v1/agent/report` endpoint for
|
||||
report-only Monitor agents, per-server level and service-check state (server
|
||||
list badge and health, server detail panel), offline / service-down alerts
|
||||
through the existing fleet alerts and notification channels, audited level
|
||||
changes, and a panel-side command gate. The panel cannot set the level; it is
|
||||
changed on the server with `serverkit-agent permissions set`. See
|
||||
[docs/AGENT_PERMISSION_LEVELS.md](docs/AGENT_PERMISSION_LEVELS.md). Migration
|
||||
112. The Add Server commands now pass `--level` / `-Level`.
|
||||
- `GET /api/v1/servers` now accepts an API key (`X-API-Key`) carrying the
|
||||
`servers:read` scope, in addition to the usual JWT, so external tools can
|
||||
poll the server list read-only without a short-lived token. JWT callers are
|
||||
|
||||
@@ -47,6 +47,12 @@ trust boundaries:
|
||||
(`system:exec`) and interactive PTY sessions are controlled by the agent's
|
||||
`Features.Exec` flag, which is **off by default**. Enable it only on servers
|
||||
where you intend the panel to run shell commands.
|
||||
- **Permission levels bound what a compromised panel can do.** An agent runs as
|
||||
Monitor (report-only, no inbound command channel), Protect (security allowlist
|
||||
only) or Full. The level is read only from the agent's local config and can
|
||||
only be changed on the server (`serverkit-agent permissions set`); the panel
|
||||
records and audits the reported level but has no API to set it. See
|
||||
[docs/AGENT_PERMISSION_LEVELS.md](docs/AGENT_PERMISSION_LEVELS.md).
|
||||
- **Transport & connection controls.** Agents authenticate to the panel with
|
||||
per-connection HMAC-SHA256 (with nonce/replay protection and a timestamp-skew
|
||||
check), and the panel enforces a per-server IP allowlist. Use `wss://`
|
||||
|
||||
@@ -136,7 +136,10 @@ class AgentNamespace(Namespace):
|
||||
return
|
||||
|
||||
metrics = data.get('metrics', {})
|
||||
agent_registry.ingest_agent_state(agent, metrics=metrics)
|
||||
agent_registry.ingest_agent_state(
|
||||
agent, metrics=metrics,
|
||||
permission_level=data.get('permission_level'),
|
||||
service_checks=data.get('service_checks'))
|
||||
|
||||
emit('heartbeat_ack', {'type': 'heartbeat_ack'})
|
||||
|
||||
|
||||
@@ -108,7 +108,9 @@ def poll():
|
||||
# Shared transport-neutral ingest: capability
|
||||
# side-effects (e.g. tunnel reconcile) fire exactly as on the WS path.
|
||||
agent_registry.ingest_agent_state(agent, metrics=metrics, sysinfo=sysinfo,
|
||||
capabilities=caps)
|
||||
capabilities=caps,
|
||||
permission_level=body.get('permission_level'),
|
||||
service_checks=body.get('service_checks'))
|
||||
|
||||
# Long-poll up to 25s for any queued command. Below the typical
|
||||
# tunnel idle-timeout (Cloudflare ~100s, ngrok ~60s) so we never
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Monitor-mode agent report endpoint.
|
||||
|
||||
``POST /api/v1/agent/report`` is the ONLY channel a monitor-level agent uses:
|
||||
a signed, outbound-only HTTPS POST. The reply never carries commands.
|
||||
|
||||
Auth (no JWT/CSRF; the HMAC is the credential):
|
||||
X-Agent-Key: <api key> -> server looked up by its key prefix
|
||||
X-Timestamp: <unix seconds> -> |skew| <= 300s
|
||||
X-Nonce: <hex, >=32 chars> -> single use (TTL cache, 5 min)
|
||||
X-Signature: hex(HMAC_SHA256(api_secret, ts + "." + nonce + "." + sha256hex(body)))
|
||||
"""
|
||||
|
||||
from flask import Blueprint, jsonify, request
|
||||
|
||||
from app import limiter
|
||||
from app.exceptions import (
|
||||
AuthenticationError, PayloadTooLargeError, PermissionDeniedError, ValidationError,
|
||||
)
|
||||
from app.services import agent_health_service, permission_levels
|
||||
from app.utils.ip_utils import is_ip_allowed
|
||||
|
||||
|
||||
agent_report_bp = Blueprint('agent_report', __name__)
|
||||
|
||||
MAX_BODY_BYTES = 64 * 1024
|
||||
MAX_SKEW_SECONDS = 300
|
||||
|
||||
|
||||
def _rate_key():
|
||||
return f"{request.remote_addr}:{(request.headers.get('X-Agent-Key') or '')[:12]}"
|
||||
|
||||
|
||||
@agent_report_bp.route('/report', methods=['POST'])
|
||||
@limiter.limit("30 per minute", key_func=_rate_key)
|
||||
def report():
|
||||
if (request.content_length or 0) > MAX_BODY_BYTES:
|
||||
raise PayloadTooLargeError()
|
||||
raw = request.get_data(cache=True)
|
||||
if len(raw) > MAX_BODY_BYTES:
|
||||
raise PayloadTooLargeError()
|
||||
|
||||
server = agent_health_service.authenticate_report(raw, request.headers, MAX_SKEW_SECONDS)
|
||||
if not server:
|
||||
raise AuthenticationError('authentication failed')
|
||||
if server.allowed_ips and not is_ip_allowed(request.remote_addr, server.allowed_ips):
|
||||
raise PermissionDeniedError('IP not allowed')
|
||||
|
||||
body = request.get_json(silent=True)
|
||||
if not isinstance(body, dict):
|
||||
raise ValidationError('JSON object required')
|
||||
level = body.get('permission_level')
|
||||
if level not in permission_levels.LEVELS:
|
||||
raise ValidationError('permission_level must be monitor, protect or full')
|
||||
|
||||
interval = agent_health_service.ingest_report(server, level, body)
|
||||
return jsonify({'ok': True, 'interval_seconds': interval})
|
||||
@@ -13,6 +13,7 @@ from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||
|
||||
from sqlalchemy.orm import joinedload
|
||||
from app import db, limiter
|
||||
from app.services import permission_levels
|
||||
from app.services.server_metrics_service import latest_metrics_by_server
|
||||
from app.api._query import apply_query, QueryParseError
|
||||
from app.api._request import json_object
|
||||
@@ -440,6 +441,8 @@ def create_server():
|
||||
registered_by=user_id,
|
||||
workspace_id=ws_id,
|
||||
registration_token_expires=expires_at,
|
||||
# What the install command asks for; the agent's report is the truth.
|
||||
requested_permission_level=permission_levels.normalize(data.get('requested_level')),
|
||||
)
|
||||
server.set_registration_token(registration_token)
|
||||
|
||||
@@ -718,6 +721,7 @@ def regenerate_token(server_id):
|
||||
registration_token = Server.generate_registration_token()
|
||||
server.set_registration_token(registration_token)
|
||||
server.registration_token_expires = expires_at
|
||||
server.requested_permission_level = permission_levels.normalize(data.get('requested_level'))
|
||||
server.status = 'pending'
|
||||
server.agent_id = None
|
||||
|
||||
@@ -792,6 +796,10 @@ def register_agent():
|
||||
server.total_disk = system_info.get('total_disk', server.total_disk)
|
||||
|
||||
server.agent_version = data.get('agent_version')
|
||||
# Level is whatever the agent says it was installed with; the panel never
|
||||
# sets it. A mismatch with the requested level is surfaced in the UI.
|
||||
from app.services import agent_health_service
|
||||
agent_health_service.record_level(server, data.get('permission_level'))
|
||||
|
||||
# Replace the panel-issued placeholder name with whatever the agent
|
||||
# reports — preferring the OS hostname over the agent's optional
|
||||
|
||||
@@ -5,6 +5,7 @@ import os
|
||||
import re
|
||||
import requests
|
||||
from app.models.server import AgentVersion, Server
|
||||
from app.services import permission_levels
|
||||
from flask import Response, current_app, jsonify, redirect, request
|
||||
from app.middleware.rbac import admin_required, developer_required
|
||||
from app.exceptions import agent_offline, field_required
|
||||
@@ -178,8 +179,11 @@ def get_install_instructions(server_id):
|
||||
# Get base URL
|
||||
base_url = _get_external_base_url()
|
||||
api_url = f"{base_url}/api/v1/servers"
|
||||
level = (permission_levels.normalize(request.args.get('level'))
|
||||
or server.requested_permission_level or 'monitor')
|
||||
|
||||
return jsonify({
|
||||
'permission_level': level,
|
||||
'linux': {
|
||||
# Download-then-run rather than `curl … | sudo bash`: in a pipeline
|
||||
# the shell reports bash's exit status, so a failed download exits 0
|
||||
@@ -187,7 +191,7 @@ def get_install_instructions(server_id):
|
||||
'one_liner': (
|
||||
f'curl -fsSL {api_url}/install.sh -o /tmp/serverkit-agent-install.sh '
|
||||
f'&& sudo bash /tmp/serverkit-agent-install.sh '
|
||||
f'--token "YOUR_TOKEN" --server "{base_url}"'
|
||||
f'--token "YOUR_TOKEN" --server "{base_url}" --level {level}'
|
||||
),
|
||||
'manual': [
|
||||
f'# Download the script',
|
||||
@@ -195,17 +199,17 @@ def get_install_instructions(server_id):
|
||||
f'chmod +x install.sh',
|
||||
f'',
|
||||
f'# Run installation',
|
||||
f'sudo ./install.sh --token "YOUR_TOKEN" --server "{base_url}"'
|
||||
f'sudo ./install.sh --token "YOUR_TOKEN" --server "{base_url}" --level {level}'
|
||||
]
|
||||
},
|
||||
'windows': {
|
||||
'one_liner': f'irm {api_url}/install.ps1 | iex; Install-ServerKitAgent -Token "YOUR_TOKEN" -Server "{base_url}"',
|
||||
'one_liner': f'irm {api_url}/install.ps1 | iex; Install-ServerKitAgent -Token "YOUR_TOKEN" -Server "{base_url}" -Level {level}',
|
||||
'manual': [
|
||||
f'# Download the script (run in PowerShell as Administrator)',
|
||||
f'Invoke-WebRequest -Uri "{api_url}/install.ps1" -OutFile install.ps1',
|
||||
f'',
|
||||
f'# Run installation',
|
||||
f'.\\install.ps1 -Token "YOUR_TOKEN" -Server "{base_url}"'
|
||||
f'.\\install.ps1 -Token "YOUR_TOKEN" -Server "{base_url}" -Level {level}'
|
||||
]
|
||||
},
|
||||
'note': 'Replace YOUR_TOKEN with the registration token shown in the UI'
|
||||
|
||||
@@ -46,6 +46,7 @@ class BlueprintSpec:
|
||||
CORE_BLUEPRINTS: tuple[BlueprintSpec, ...] = (
|
||||
BlueprintSpec('app.api.auth', 'auth_bp', '/api/v1/auth'),
|
||||
BlueprintSpec('app.api.agent_poll', 'agent_poll_bp', '/api/v1/agent'),
|
||||
BlueprintSpec('app.api.agent_report', 'agent_report_bp', '/api/v1/agent'),
|
||||
BlueprintSpec('app.api.linked_panel', 'linked_panel_bp', '/api/v1/linked-panel'),
|
||||
BlueprintSpec('app.api.apps', 'apps_bp', '/api/v1/apps'),
|
||||
BlueprintSpec('app.api.apps', 'apps_bp', '/api/v1/services', name='services'),
|
||||
|
||||
@@ -57,6 +57,12 @@ class PermissionDeniedError(ApplicationError, PermissionError):
|
||||
default_message = 'Access denied'
|
||||
|
||||
|
||||
class PayloadTooLargeError(ApplicationError):
|
||||
status_code = 413
|
||||
code = 'payload_too_large'
|
||||
default_message = 'Payload too large'
|
||||
|
||||
|
||||
class NotFoundError(ApplicationError, LookupError):
|
||||
status_code = 404
|
||||
code = 'not_found'
|
||||
|
||||
@@ -534,7 +534,7 @@ def _notify_fleet_alerts(opened, resolved):
|
||||
'metric': alert.metric,
|
||||
'value': alert.value,
|
||||
'threshold': alert.threshold,
|
||||
'message': (
|
||||
'message': alert.detail or (
|
||||
f'{hostname}: {alert.metric} averaged {alert.value} over '
|
||||
f'{alert.duration_seconds}s, past the '
|
||||
f'{alert.severity} threshold of {alert.threshold}.'),
|
||||
@@ -555,7 +555,9 @@ def _notify_fleet_alerts(opened, resolved):
|
||||
'hostname': hostname,
|
||||
'alert_type': f'{alert.metric} recovered',
|
||||
'metric': alert.metric,
|
||||
'message': (f'{hostname}: {alert.metric} is back under its '
|
||||
'message': (f'{hostname}: {alert.metric} recovered.'
|
||||
if alert.detail else
|
||||
f'{hostname}: {alert.metric} is back under its '
|
||||
f'threshold.'),
|
||||
},
|
||||
)
|
||||
|
||||
@@ -49,6 +49,8 @@ class MetricAlert(db.Model):
|
||||
threshold = db.Column(db.Float) # the threshold exceeded
|
||||
duration_seconds = db.Column(db.Integer) # how long it was exceeded
|
||||
|
||||
detail = db.Column(db.Text) # human message for offline / service-check alerts
|
||||
|
||||
status = db.Column(db.String(20), default='active', index=True) # active, acknowledged, resolved
|
||||
acknowledged_by = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=True, index=True)
|
||||
|
||||
@@ -67,6 +69,7 @@ class MetricAlert(db.Model):
|
||||
'value': self.value,
|
||||
'threshold': self.threshold,
|
||||
'duration_seconds': self.duration_seconds,
|
||||
'detail': self.detail,
|
||||
'status': self.status,
|
||||
'acknowledged_by': self.acknowledged_by,
|
||||
'created_at': self.created_at.isoformat() if self.created_at else None,
|
||||
|
||||
@@ -126,6 +126,17 @@ class Server(TimestampMixin, JsonColumnMixin, db.Model):
|
||||
allow_agent_update_observed = db.Column(db.Boolean, nullable=False,
|
||||
default=False, server_default='0')
|
||||
|
||||
# Agent permission level (monitor|protect|full). REPORTED BY THE AGENT
|
||||
# only -- no panel API writes it. NULL = unknown/legacy, treated as full.
|
||||
# requested_permission_level is what the install command asked for, kept
|
||||
# only to flag a mismatch with what the agent reports.
|
||||
permission_level = db.Column(db.String(16), nullable=True)
|
||||
permission_level_updated_at = db.Column(db.DateTime, nullable=True)
|
||||
requested_permission_level = db.Column(db.String(16), nullable=True)
|
||||
last_report_at = db.Column(db.DateTime, nullable=True)
|
||||
report_interval_seconds = db.Column(db.Integer, nullable=True)
|
||||
service_checks = db.Column(db.JSON, nullable=True) # latest state per check
|
||||
|
||||
@property
|
||||
def is_managed(self):
|
||||
"""False only for observed boxes."""
|
||||
@@ -490,6 +501,12 @@ class Server(TimestampMixin, JsonColumnMixin, db.Model):
|
||||
'last_error': self.last_error,
|
||||
'management_mode': self.management_mode or 'managed',
|
||||
'allow_agent_update_observed': bool(self.allow_agent_update_observed),
|
||||
'permission_level': self.permission_level,
|
||||
'permission_level_effective': self.permission_level or 'full',
|
||||
'permission_level_updated_at': self.permission_level_updated_at.isoformat() if self.permission_level_updated_at else None,
|
||||
'requested_permission_level': self.requested_permission_level,
|
||||
'last_report_at': self.last_report_at.isoformat() if self.last_report_at else None,
|
||||
'service_checks': self.service_checks or [],
|
||||
'onboarding_state': self.onboarding_state,
|
||||
'onboarding_updated_at': self.onboarding_updated_at.isoformat() if self.onboarding_updated_at else None,
|
||||
'agent_version': self.agent_version,
|
||||
|
||||
@@ -0,0 +1,223 @@
|
||||
"""Agent-reported health: permission level, service checks, silence, alerts.
|
||||
|
||||
Everything here consumes what the agent REPORTS (registration, auth, heartbeat,
|
||||
or the monitor-mode ``/agent/report`` POST). Alerts reuse ``MetricAlert`` and the
|
||||
fleet notification path; one active row per (server, metric) de-duplicates.
|
||||
"""
|
||||
import hashlib
|
||||
import hmac
|
||||
import logging
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from app import db
|
||||
from app.models.audit_log import AuditLog
|
||||
from app.models.metric_alert import MetricAlert
|
||||
from app.models.server import Server
|
||||
from app.services import permission_levels
|
||||
from app.services.nonce_service import nonce_service
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
MIN_SILENCE_SECONDS = 180 # monitor agents: offline after max(3 min, 3x interval)
|
||||
DEFAULT_INTERVAL_SECONDS = 60
|
||||
DEFAULT_INTERVAL = DEFAULT_INTERVAL_SECONDS
|
||||
OFFLINE_METRIC = 'offline'
|
||||
OPEN = ('active', 'acknowledged')
|
||||
BAD = ('down',)
|
||||
|
||||
|
||||
def _notify(opened=(), resolved=()):
|
||||
if not opened and not resolved:
|
||||
return
|
||||
try:
|
||||
from app.jobs.builtin_handlers import _notify_fleet_alerts
|
||||
_notify_fleet_alerts(list(opened), list(resolved))
|
||||
except Exception:
|
||||
logger.exception('Could not notify agent health alerts')
|
||||
|
||||
|
||||
def _open_alert(server, metric, detail):
|
||||
"""Open an alert unless one is already open. Returns it if newly opened."""
|
||||
if MetricAlert.query.filter(MetricAlert.server_id == server.id,
|
||||
MetricAlert.metric == metric,
|
||||
MetricAlert.status.in_(OPEN)).first():
|
||||
return None
|
||||
alert = MetricAlert(server_id=server.id, metric=metric, severity='critical',
|
||||
detail=detail, status='active')
|
||||
db.session.add(alert)
|
||||
return alert
|
||||
|
||||
|
||||
def _resolve_alerts(server, metric):
|
||||
rows = MetricAlert.query.filter(MetricAlert.server_id == server.id,
|
||||
MetricAlert.metric == metric,
|
||||
MetricAlert.status.in_(OPEN)).all()
|
||||
for row in rows:
|
||||
row.status = 'resolved'
|
||||
row.resolved_at = datetime.utcnow()
|
||||
return rows
|
||||
|
||||
|
||||
def record_level(server, reported):
|
||||
"""Persist a level reported by the agent; audit when it changes.
|
||||
|
||||
Invalid/missing values are ignored. Escalations (monitor->protect->full) are
|
||||
logged like any change but flagged, since they mean someone re-ran the
|
||||
local confirmation on the box.
|
||||
"""
|
||||
level = permission_levels.normalize(reported)
|
||||
if level is None:
|
||||
return
|
||||
old = server.permission_level
|
||||
if old == level:
|
||||
return
|
||||
server.permission_level = level
|
||||
server.permission_level_updated_at = datetime.utcnow()
|
||||
AuditLog.log(
|
||||
'server.permission_level_changed', target_type='server', target_id=server.id,
|
||||
details={'server_id': server.id, 'from': old, 'to': level,
|
||||
'escalation': permission_levels.is_escalation(old, level),
|
||||
'source': 'agent_report'})
|
||||
logger.warning('Server %s reported permission level %s -> %s', server.id, old, level)
|
||||
|
||||
|
||||
def record_service_checks(server, checks):
|
||||
"""Store the latest per-check state and open/resolve alerts on transitions."""
|
||||
if not isinstance(checks, list):
|
||||
return
|
||||
previous = {c.get('name'): c.get('status') for c in (server.service_checks or [])}
|
||||
clean, opened, resolved = [], [], []
|
||||
for raw in checks[:50]:
|
||||
if not isinstance(raw, dict) or not isinstance(raw.get('name'), str):
|
||||
continue
|
||||
item = {k: (str(raw.get(k))[:200] if raw.get(k) is not None else None)
|
||||
for k in ('name', 'kind', 'status', 'detail', 'checked_at')}
|
||||
item['name'] = item['name'][:60]
|
||||
if item['status'] not in ('up', 'down', 'degraded', 'unknown'):
|
||||
item['status'] = 'unknown'
|
||||
clean.append(item)
|
||||
metric = f"svc:{item['name']}"[:20]
|
||||
if item['status'] == 'down':
|
||||
alert = _open_alert(
|
||||
server, metric,
|
||||
f"{server.name}: service '{item['name']}' is down since "
|
||||
f"{datetime.utcnow():%Y-%m-%d %H:%M} UTC"
|
||||
+ (f" ({item['detail']})" if item['detail'] else ''))
|
||||
if alert:
|
||||
opened.append(alert)
|
||||
elif item['status'] == 'up' or (item['status'] == 'degraded'
|
||||
and previous.get(item['name']) == 'down'):
|
||||
resolved += _resolve_alerts(server, metric)
|
||||
server.service_checks = clean
|
||||
db.session.flush()
|
||||
_notify(opened, resolved)
|
||||
|
||||
|
||||
def mark_offline(server, reason):
|
||||
"""Open the (de-duplicated) offline alert. Caller commits."""
|
||||
since = server.last_report_at or server.last_seen or datetime.utcnow()
|
||||
alert = _open_alert(
|
||||
server, OFFLINE_METRIC,
|
||||
f"{server.name} is offline since {since:%Y-%m-%d %H:%M} UTC: {reason}")
|
||||
db.session.flush()
|
||||
_notify([alert] if alert else [])
|
||||
|
||||
|
||||
def mark_online(server):
|
||||
resolved = _resolve_alerts(server, OFFLINE_METRIC)
|
||||
db.session.flush()
|
||||
_notify(resolved=resolved)
|
||||
|
||||
|
||||
def silence_limit(server):
|
||||
interval = server.report_interval_seconds or DEFAULT_INTERVAL_SECONDS
|
||||
return timedelta(seconds=max(MIN_SILENCE_SECONDS, 3 * interval))
|
||||
|
||||
|
||||
def sweep_silent_reporters(now=None):
|
||||
"""Flip report-only (monitor) servers offline once their reports stop.
|
||||
|
||||
Reports are not a socket, so the registry's heartbeat reaper never sees
|
||||
them. Only servers that have ever reported are considered.
|
||||
"""
|
||||
now = now or datetime.utcnow()
|
||||
flipped = []
|
||||
for server in Server.query.filter(Server.permission_level == 'monitor',
|
||||
Server.last_report_at.isnot(None),
|
||||
Server.status != 'offline').all():
|
||||
if now - server.last_report_at > silence_limit(server):
|
||||
server.status = 'offline'
|
||||
server.last_error = 'Agent stopped reporting'
|
||||
mark_offline(server, 'no report received')
|
||||
flipped.append(server.id)
|
||||
if flipped:
|
||||
db.session.commit()
|
||||
return flipped
|
||||
|
||||
|
||||
def authenticate_report(raw, headers, max_skew):
|
||||
"""Return the Server for a validly signed report request, else None."""
|
||||
key = headers.get('X-Agent-Key') or ''
|
||||
ts = headers.get('X-Timestamp') or ''
|
||||
nonce = headers.get('X-Nonce') or ''
|
||||
signature = headers.get('X-Signature') or ''
|
||||
if not (key and ts and signature) or len(nonce) < 32:
|
||||
return None
|
||||
try:
|
||||
if abs(time.time() - int(ts)) > max_skew:
|
||||
return None
|
||||
except ValueError:
|
||||
return None
|
||||
|
||||
prefix = key[:12]
|
||||
server = Server.query.filter(
|
||||
(Server.api_key_prefix == prefix) | (Server.api_key_pending_prefix == prefix)
|
||||
).first()
|
||||
if not server:
|
||||
return None
|
||||
pending = (server.api_key_pending_prefix == prefix and server.api_key_prefix != prefix
|
||||
and server.api_key_rotation_expires
|
||||
and datetime.utcnow() <= server.api_key_rotation_expires)
|
||||
secret = server.get_pending_api_secret() if pending else server.get_api_secret()
|
||||
if not secret:
|
||||
return None
|
||||
|
||||
message = f"{ts}.{nonce}.{hashlib.sha256(raw).hexdigest()}"
|
||||
expected = hmac.new(secret.encode(), message.encode(), hashlib.sha256).hexdigest()
|
||||
if not hmac.compare_digest(signature, expected):
|
||||
return None
|
||||
# Consume the nonce only after the signature proves valid.
|
||||
if not nonce_service.check_and_record(server.id, nonce):
|
||||
return None
|
||||
return server
|
||||
|
||||
|
||||
def ingest_report(server, level, body):
|
||||
"""Apply a verified report to the server row; returns the interval in use."""
|
||||
interval = body.get('interval_seconds')
|
||||
interval = interval if isinstance(interval, int) and 5 <= interval <= 3600 else DEFAULT_INTERVAL
|
||||
|
||||
now = datetime.utcnow()
|
||||
was_offline = server.status != 'online'
|
||||
server.status = 'online'
|
||||
server.last_error = None
|
||||
server.last_seen = now
|
||||
server.last_report_at = now
|
||||
server.report_interval_seconds = interval
|
||||
if isinstance(body.get('agent_version'), str):
|
||||
server.agent_version = body['agent_version'][:20]
|
||||
record_level(server, level)
|
||||
record_service_checks(server, body.get('service_checks'))
|
||||
if was_offline:
|
||||
mark_online(server)
|
||||
db.session.commit()
|
||||
|
||||
metrics = body.get('metrics')
|
||||
if isinstance(metrics, dict):
|
||||
from app.services.server_metrics_service import ServerMetricsService
|
||||
ServerMetricsService.record_metrics(server.id, {
|
||||
**{k: metrics.get(k) for k in ('cpu_percent', 'memory_percent', 'disk_percent')},
|
||||
'extra': {k: metrics.get(k) for k in ('uptime_seconds', 'load1')},
|
||||
})
|
||||
return interval
|
||||
@@ -22,6 +22,7 @@ from collections import defaultdict
|
||||
|
||||
from app import db
|
||||
from app.models.server import Server, ServerCommand, AgentSession
|
||||
from app.services import permission_levels
|
||||
|
||||
# ---------------------------------------------------------------------------
|
||||
# Per-IP auth rate limiter — transport-neutral state shared by the WS
|
||||
@@ -245,6 +246,12 @@ class AgentRegistry:
|
||||
self._agents.pop(server_id, None)
|
||||
self._socket_to_server.pop(socket_id, None)
|
||||
|
||||
# Report-only (monitor) agents hold no socket; sweep them too.
|
||||
if self._app is not None:
|
||||
with self._app.app_context():
|
||||
from app.services import agent_health_service
|
||||
agent_health_service.sweep_silent_reporters()
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error in heartbeat checker: %s", e)
|
||||
|
||||
@@ -279,6 +286,8 @@ class AgentRegistry:
|
||||
if server:
|
||||
server.status = 'offline'
|
||||
server.last_error = 'Agent heartbeat timeout'
|
||||
from app.services import agent_health_service
|
||||
agent_health_service.mark_offline(server, 'heartbeat timeout')
|
||||
db.session.commit()
|
||||
|
||||
# Mark session as inactive
|
||||
@@ -392,6 +401,8 @@ class AgentRegistry:
|
||||
# even for healthy connected agents.
|
||||
if ip_address and ip_address != 'unknown':
|
||||
server.ip_address = ip_address
|
||||
from app.services import agent_health_service
|
||||
agent_health_service.mark_online(server)
|
||||
db.session.commit()
|
||||
|
||||
# Create session record
|
||||
@@ -519,6 +530,8 @@ class AgentRegistry:
|
||||
server.last_seen = now
|
||||
if server.status != 'online':
|
||||
server.status = 'online'
|
||||
from app.services import agent_health_service
|
||||
agent_health_service.mark_online(server)
|
||||
db.session.commit()
|
||||
|
||||
# Update session latency
|
||||
@@ -589,6 +602,12 @@ class AgentRegistry:
|
||||
|
||||
# Check permissions
|
||||
server = Server.query.get(server_id)
|
||||
# Defence in depth: never send what the agent's reported level refuses.
|
||||
refused = permission_levels.refusal(
|
||||
server.permission_level if server else None, action)
|
||||
if refused:
|
||||
return {'success': False, 'error': refused, 'code': 'LEVEL_DENIED'}
|
||||
|
||||
if server and not server.has_permission(action):
|
||||
return {
|
||||
'success': False,
|
||||
@@ -863,13 +882,15 @@ class AgentRegistry:
|
||||
# in-memory state — the adapter must not report success.
|
||||
if not session_token:
|
||||
return None, server, 'registration_failed'
|
||||
self.record_agent_health(server.id, payload.get('permission_level'))
|
||||
|
||||
logger.info("Agent %s authenticated successfully from %s (%s)",
|
||||
agent_id, ip_address, transport)
|
||||
return session_token, server, None
|
||||
|
||||
def ingest_agent_state(self, agent, metrics=None, sysinfo=None,
|
||||
capabilities=None):
|
||||
capabilities=None, permission_level=None,
|
||||
service_checks=None):
|
||||
"""One ingest path for agent-reported state on BOTH transports.
|
||||
|
||||
Owns the capability side-effects (the WireGuard tunnel reconcile
|
||||
@@ -882,6 +903,20 @@ class AgentRegistry:
|
||||
self.update_system_info(agent.server_id, sysinfo)
|
||||
if capabilities:
|
||||
self.update_capabilities(agent.server_id, capabilities)
|
||||
if permission_level is not None or service_checks is not None:
|
||||
self.record_agent_health(agent.server_id, permission_level, service_checks)
|
||||
|
||||
def record_agent_health(self, server_id, permission_level=None, service_checks=None):
|
||||
from app.services import agent_health_service
|
||||
try:
|
||||
server = Server.query.get(server_id)
|
||||
if server:
|
||||
agent_health_service.record_level(server, permission_level)
|
||||
agent_health_service.record_service_checks(server, service_checks)
|
||||
db.session.commit()
|
||||
except Exception:
|
||||
logger.exception("Error recording agent health")
|
||||
db.session.rollback()
|
||||
|
||||
def verify_agent_auth(
|
||||
self,
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
"""Agent permission levels and the one command-gate table.
|
||||
|
||||
The level is chosen on the server (``permissions.level`` in the agent's
|
||||
config.yaml) and REPORTED to the panel. The panel never sets it. This module
|
||||
is only the panel's defence in depth: it refuses to send a command the agent
|
||||
would refuse anyway. ``PROTECT_ACTIONS`` must stay identical to the agent's
|
||||
allowlist (docs/AGENT_PERMISSION_LEVELS.md; checked by a test).
|
||||
"""
|
||||
|
||||
LEVELS = ('monitor', 'protect', 'full')
|
||||
_RANK = {level: i for i, level in enumerate(LEVELS)}
|
||||
LABELS = {'monitor': 'Monitor', 'protect': 'Protect', 'full': 'Full'}
|
||||
|
||||
PROTECT_ACTIONS = frozenset({
|
||||
'firewall:allow_port', 'firewall:deny_port', 'firewall:status',
|
||||
'firewall:allow_ip', 'firewall:deny_ip',
|
||||
'fail2ban:status', 'fail2ban:ban', 'fail2ban:unban',
|
||||
'crowdsec:decisions:list', 'crowdsec:ban', 'crowdsec:unban',
|
||||
'cloudflare:origin_lock:status', 'cloudflare:origin_lock:apply',
|
||||
'cloudflare:origin_lock:remove',
|
||||
})
|
||||
|
||||
# Signature-verified key rotation is not control; allowed at every level.
|
||||
ALWAYS_ALLOWED = frozenset({'credential_update'})
|
||||
|
||||
|
||||
def normalize(level):
|
||||
"""Return a valid level string, or None for missing/unknown values."""
|
||||
level = level.strip().lower() if isinstance(level, str) else None
|
||||
return level if level in _RANK else None
|
||||
|
||||
|
||||
def effective_level(level):
|
||||
"""Unknown/legacy (None) means a pre-levels agent, i.e. full."""
|
||||
return normalize(level) or 'full'
|
||||
|
||||
|
||||
def is_escalation(old, new):
|
||||
return old is not None and _RANK[new] > _RANK[old]
|
||||
|
||||
|
||||
def refusal(level, action):
|
||||
"""Message explaining why ``action`` is refused at ``level``, else None."""
|
||||
level = effective_level(level)
|
||||
if level == 'full' or action in ALWAYS_ALLOWED:
|
||||
return None
|
||||
if level == 'protect' and action in PROTECT_ACTIONS:
|
||||
return None
|
||||
if level == 'monitor':
|
||||
return (f"server is in Monitor mode: '{action}' refused. Monitor agents "
|
||||
"are report-only and accept no commands. The level can only be "
|
||||
"changed on the server itself (serverkit-agent permissions set).")
|
||||
return (f"server is in Protect mode: '{action}' refused. Protect agents only "
|
||||
"run firewall, fail2ban, CrowdSec and Cloudflare origin-lock actions. "
|
||||
"The level can only be changed on the server itself "
|
||||
"(serverkit-agent permissions set).")
|
||||
@@ -52,6 +52,7 @@ _AUDIT_DETAIL_KEYS = frozenset({
|
||||
'application_id',
|
||||
'code',
|
||||
'endpoint',
|
||||
'escalation',
|
||||
'from',
|
||||
'kind',
|
||||
'label',
|
||||
|
||||
@@ -0,0 +1,36 @@
|
||||
"""Agent-reported permission level, report bookkeeping and service checks."""
|
||||
import sqlalchemy as sa
|
||||
from alembic import op
|
||||
|
||||
revision = '112_agent_permission_levels'
|
||||
down_revision = '111_preview_runtime'
|
||||
branch_labels = None
|
||||
depends_on = None
|
||||
|
||||
SERVER_COLUMNS = [
|
||||
('permission_level', sa.String(16)),
|
||||
('permission_level_updated_at', sa.DateTime()),
|
||||
('requested_permission_level', sa.String(16)),
|
||||
('last_report_at', sa.DateTime()),
|
||||
('report_interval_seconds', sa.Integer()),
|
||||
('service_checks', sa.JSON()),
|
||||
]
|
||||
|
||||
|
||||
def upgrade():
|
||||
inspector = sa.inspect(op.get_bind())
|
||||
existing = {c['name'] for c in inspector.get_columns('servers')}
|
||||
for name, kind in SERVER_COLUMNS:
|
||||
if name not in existing:
|
||||
op.add_column('servers', sa.Column(name, kind, nullable=True))
|
||||
existing = {c['name'] for c in inspector.get_columns('metric_alerts')}
|
||||
if 'detail' not in existing:
|
||||
op.add_column('metric_alerts', sa.Column('detail', sa.Text(), nullable=True))
|
||||
|
||||
|
||||
def downgrade():
|
||||
with op.batch_alter_table('metric_alerts') as batch:
|
||||
batch.drop_column('detail')
|
||||
with op.batch_alter_table('servers') as batch:
|
||||
for name, _ in reversed(SERVER_COLUMNS):
|
||||
batch.drop_column(name)
|
||||
@@ -0,0 +1,80 @@
|
||||
"""Seed five demo servers showing every permission-level / health state.
|
||||
|
||||
Dev sandbox only; never run automatically. Idempotent: demo rows are keyed by
|
||||
agent_id 'demo-perm-*' and are rebuilt on each run.
|
||||
|
||||
cd backend && python scripts/seed_permission_demo.py
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
|
||||
|
||||
from app import create_app, db # noqa: E402
|
||||
from app.models.metric_alert import MetricAlert # noqa: E402
|
||||
from app.models.server import Server, ServerMetrics # noqa: E402
|
||||
|
||||
UP, DOWN, DEG = 'up', 'down', 'degraded'
|
||||
|
||||
|
||||
def checks(now, web=UP, php=UP, database=UP, disk=UP):
|
||||
stamp = now.isoformat() + 'Z'
|
||||
rows = [('nginx', 'web', web, ''), ('php8.2-fpm', 'php_fpm', php, 'slow pool' if php == DEG else ''),
|
||||
('mysql', 'database', database, 'connection refused on 3306' if database == DOWN else ''),
|
||||
('disk /', 'disk', disk, ''), ('cpu', 'cpu', UP, ''), ('memory', 'memory', UP, '')]
|
||||
return [{'name': n, 'kind': k, 'status': s, 'detail': d, 'checked_at': stamp} for n, k, s, d in rows]
|
||||
|
||||
|
||||
# (name, level, status, minutes since last report, check overrides, cpu)
|
||||
DEMOS = [
|
||||
('demo-monitor-healthy', 'monitor', 'online', 1, {}, 14.0),
|
||||
('demo-monitor-degraded', 'monitor', 'online', 1, {'database': DOWN, 'php': DEG}, 71.0),
|
||||
('demo-protect-healthy', 'protect', 'online', 0, {}, 22.0),
|
||||
('demo-full-healthy', 'full', 'online', 0, {}, 35.0),
|
||||
('demo-monitor-offline', 'monitor', 'offline', 20, {'database': DOWN}, 9.0),
|
||||
]
|
||||
|
||||
|
||||
def seed():
|
||||
now = datetime.utcnow()
|
||||
for name, level, status, age, overrides, cpu in DEMOS:
|
||||
agent_id = f'demo-perm-{name}'
|
||||
old = Server.query.filter_by(agent_id=agent_id).first()
|
||||
if old:
|
||||
MetricAlert.query.filter_by(server_id=old.id).delete()
|
||||
ServerMetrics.query.filter_by(server_id=old.id).delete()
|
||||
db.session.delete(old)
|
||||
db.session.flush()
|
||||
reported = now - timedelta(minutes=age)
|
||||
srv = Server(name=name, agent_id=agent_id, hostname=name, status=status,
|
||||
os_type='linux', permission_level=level, requested_permission_level=level,
|
||||
permission_level_updated_at=reported, last_seen=reported,
|
||||
last_report_at=reported, report_interval_seconds=60,
|
||||
service_checks=checks(reported, **overrides))
|
||||
db.session.add(srv)
|
||||
db.session.flush()
|
||||
for i in range(12):
|
||||
db.session.add(ServerMetrics(
|
||||
server_id=srv.id, timestamp=reported - timedelta(minutes=i),
|
||||
cpu_percent=cpu + i % 3, memory_percent=48.0, disk_percent=61.0))
|
||||
for check in srv.service_checks:
|
||||
if check['status'] == DOWN:
|
||||
db.session.add(MetricAlert(
|
||||
server_id=srv.id, metric=f"svc:{check['name']}"[:20], severity='critical',
|
||||
status='active', created_at=reported,
|
||||
detail=f"{name}: service '{check['name']}' is down since "
|
||||
f"{reported:%Y-%m-%d %H:%M} UTC ({check['detail']})"))
|
||||
if status == 'offline':
|
||||
db.session.add(MetricAlert(
|
||||
server_id=srv.id, metric='offline', severity='critical', status='active',
|
||||
created_at=now, detail=f"{name} is offline since {reported:%Y-%m-%d %H:%M} UTC: "
|
||||
"no report received"))
|
||||
db.session.commit()
|
||||
print(f'Seeded {len(DEMOS)} demo servers.')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app = create_app(os.environ.get('FLASK_ENV') or 'development')
|
||||
with app.app_context():
|
||||
seed()
|
||||
@@ -0,0 +1,208 @@
|
||||
"""Agent permission levels: report endpoint, level reporting, dispatch gate,
|
||||
silence sweep, service-check alerts and install commands."""
|
||||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import re
|
||||
import secrets
|
||||
import time
|
||||
from datetime import datetime, timedelta
|
||||
from pathlib import Path
|
||||
|
||||
import pytest
|
||||
|
||||
from app import db
|
||||
from app.models.audit_log import AuditLog
|
||||
from app.models.metric_alert import MetricAlert
|
||||
from app.models.server import Server
|
||||
from app.services import agent_health_service, permission_levels
|
||||
from app.services.agent_registry import ConnectedAgent, agent_registry
|
||||
|
||||
REPORT = '/api/v1/agent/report'
|
||||
SECRET = 'test-secret'
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def server(app, monkeypatch):
|
||||
monkeypatch.setattr(Server, 'get_api_secret', lambda self: SECRET)
|
||||
key, _ = Server.generate_api_credentials()
|
||||
srv = Server(name='web-1', agent_id=secrets.token_hex(8))
|
||||
srv.set_api_key(key)
|
||||
db.session.add(srv)
|
||||
db.session.commit()
|
||||
srv.test_key = key
|
||||
return srv
|
||||
|
||||
|
||||
def _post(client, srv, body=None, ts=None, nonce=None, secret=SECRET, raw=None):
|
||||
body = body if body is not None else {
|
||||
'permission_level': 'monitor', 'interval_seconds': 60,
|
||||
'metrics': {'cpu_percent': 12.5, 'memory_percent': 40.0, 'disk_percent': 55.0,
|
||||
'uptime_seconds': 99, 'load1': 0.3},
|
||||
'service_checks': []}
|
||||
raw = raw if raw is not None else json.dumps(body).encode()
|
||||
ts = str(int(time.time())) if ts is None else str(ts)
|
||||
nonce = nonce or secrets.token_hex(16)
|
||||
sig = hmac.new(secret.encode(),
|
||||
f'{ts}.{nonce}.{hashlib.sha256(raw).hexdigest()}'.encode(),
|
||||
hashlib.sha256).hexdigest()
|
||||
return client.post(REPORT, data=raw, content_type='application/json', headers={
|
||||
'X-Agent-Key': srv.test_key, 'X-Timestamp': ts, 'X-Nonce': nonce,
|
||||
'X-Signature': sig})
|
||||
|
||||
|
||||
def test_good_signature_updates_server(client, server):
|
||||
resp = _post(client, server)
|
||||
assert resp.status_code == 200
|
||||
assert resp.get_json() == {'ok': True, 'interval_seconds': 60}
|
||||
srv = db.session.get(Server, server.id)
|
||||
assert srv.status == 'online' and srv.last_report_at is not None
|
||||
assert srv.permission_level == 'monitor'
|
||||
assert 'commands' not in resp.get_json()
|
||||
|
||||
|
||||
def test_bad_signature_rejected(client, server):
|
||||
assert _post(client, server, secret='wrong').status_code == 401
|
||||
|
||||
|
||||
def test_stale_timestamp_rejected(client, server):
|
||||
assert _post(client, server, ts=int(time.time()) - 400).status_code == 401
|
||||
|
||||
|
||||
def test_replayed_nonce_rejected(client, server):
|
||||
nonce = secrets.token_hex(16)
|
||||
assert _post(client, server, nonce=nonce).status_code == 200
|
||||
assert _post(client, server, nonce=nonce).status_code == 401
|
||||
|
||||
|
||||
def test_oversize_body_rejected(client, server):
|
||||
assert _post(client, server, raw=b'{"x":"' + b'a' * 70000 + b'"}').status_code == 413
|
||||
|
||||
|
||||
def test_invalid_level_rejected(client, server):
|
||||
assert _post(client, server, body={'permission_level': 'root'}).status_code == 400
|
||||
|
||||
|
||||
def test_level_persisted_and_escalation_audited(client, server):
|
||||
_post(client, server, body={'permission_level': 'monitor'})
|
||||
_post(client, server, body={'permission_level': 'full'})
|
||||
rows = AuditLog.query.filter_by(action='server.permission_level_changed').order_by(AuditLog.id).all()
|
||||
details = [r.get_details() for r in rows]
|
||||
assert [(d['from'], d['to'], d['escalation']) for d in details] == [
|
||||
(None, 'monitor', False), ('monitor', 'full', True)]
|
||||
assert db.session.get(Server, server.id).permission_level == 'full'
|
||||
|
||||
|
||||
def test_no_api_sets_level(client, server, auth_headers):
|
||||
resp = client.put(f'/api/v1/servers/{server.id}', headers=auth_headers,
|
||||
json={'permission_level': 'full', 'name': 'x'})
|
||||
assert resp.status_code in (200, 400)
|
||||
assert db.session.get(Server, server.id).permission_level is None
|
||||
writers = [r.rule for r in client.application.url_map.iter_rules()
|
||||
if 'level' in r.rule and r.methods & {'POST', 'PUT', 'PATCH'}]
|
||||
assert writers == []
|
||||
|
||||
|
||||
def _connect(server_id):
|
||||
agent_registry._agents[server_id] = ConnectedAgent(
|
||||
server_id=server_id, socket_id='s', session_token='t',
|
||||
connected_at=datetime.utcnow(), last_heartbeat=datetime.utcnow(),
|
||||
ip_address='1.1.1.1', agent_version='1', transport='poll')
|
||||
|
||||
|
||||
@pytest.mark.parametrize('level,action,refused', [
|
||||
('monitor', 'firewall:status', True),
|
||||
('monitor', 'docker:container:list', True),
|
||||
('protect', 'docker:container:list', True),
|
||||
('protect', 'system:exec', True),
|
||||
('protect', 'firewall:status', False),
|
||||
('protect', 'cloudflare:origin_lock:apply', False),
|
||||
('protect', 'credential_update', False),
|
||||
('full', 'system:exec', False),
|
||||
(None, 'system:exec', False),
|
||||
])
|
||||
def test_refusal_table(level, action, refused):
|
||||
assert bool(permission_levels.refusal(level, action)) is refused
|
||||
|
||||
|
||||
@pytest.mark.parametrize('level', ['monitor', 'protect'])
|
||||
def test_send_command_gate(server, level):
|
||||
server.permission_level = level
|
||||
db.session.commit()
|
||||
_connect(server.id)
|
||||
try:
|
||||
result = agent_registry.send_command(server.id, 'system:exec', {})
|
||||
finally:
|
||||
agent_registry._agents.pop(server.id, None)
|
||||
assert result['code'] == 'LEVEL_DENIED'
|
||||
assert result['error'].startswith(f'server is in {level.title()} mode')
|
||||
|
||||
|
||||
def test_protect_allowlist_matches_documented_contract():
|
||||
doc = Path(__file__).parents[2] / 'docs/AGENT_PERMISSION_LEVELS.md'
|
||||
block = re.search(r'```text allowlist\n(.*?)```', doc.read_text(), re.S).group(1)
|
||||
assert set(block.split()) == set(permission_levels.PROTECT_ACTIONS)
|
||||
|
||||
|
||||
def test_silent_monitor_goes_offline_with_one_alert(server):
|
||||
server.permission_level = 'monitor'
|
||||
server.status = 'online'
|
||||
server.last_report_at = datetime.utcnow() - timedelta(minutes=20)
|
||||
db.session.commit()
|
||||
assert agent_health_service.sweep_silent_reporters() == [server.id]
|
||||
assert agent_health_service.sweep_silent_reporters() == []
|
||||
assert db.session.get(Server, server.id).status == 'offline'
|
||||
assert MetricAlert.query.filter_by(metric='offline', status='active').count() == 1
|
||||
|
||||
|
||||
def test_recent_monitor_stays_online(server):
|
||||
server.permission_level, server.status = 'monitor', 'online'
|
||||
server.last_report_at = datetime.utcnow() - timedelta(minutes=2)
|
||||
db.session.commit()
|
||||
assert agent_health_service.sweep_silent_reporters() == []
|
||||
|
||||
|
||||
def _checks(status):
|
||||
return {'permission_level': 'monitor', 'service_checks': [
|
||||
{'name': 'mysql', 'kind': 'database', 'status': status, 'detail': 'refused',
|
||||
'checked_at': '2026-01-01T00:00:00Z'}]}
|
||||
|
||||
|
||||
def test_service_down_alert_dedupes_and_resolves(client, server):
|
||||
_post(client, server, body=_checks('up'))
|
||||
assert MetricAlert.query.count() == 0
|
||||
_post(client, server, body=_checks('down'))
|
||||
_post(client, server, body=_checks('down'))
|
||||
alert = MetricAlert.query.one()
|
||||
assert alert.status == 'active' and 'mysql' in alert.detail and 'web-1' in alert.detail
|
||||
_post(client, server, body=_checks('up'))
|
||||
assert MetricAlert.query.one().status == 'resolved'
|
||||
|
||||
|
||||
def test_degraded_to_down_alerts_and_report_recovers_offline(client, server):
|
||||
_post(client, server, body=_checks('degraded'))
|
||||
assert MetricAlert.query.count() == 0
|
||||
_post(client, server, body=_checks('down'))
|
||||
assert MetricAlert.query.filter_by(status='active').count() == 1
|
||||
agent_health_service.mark_offline(server, 'test')
|
||||
db.session.commit()
|
||||
server.status = 'offline'
|
||||
db.session.commit()
|
||||
_post(client, server, body=_checks('down'))
|
||||
assert MetricAlert.query.filter_by(metric='offline', status='resolved').count() == 1
|
||||
|
||||
|
||||
def test_install_instructions_include_level(client, server, auth_headers):
|
||||
server.set_registration_token('sk_reg_x')
|
||||
db.session.commit()
|
||||
data = client.get(f'/api/v1/servers/install-instructions/{server.id}?level=protect',
|
||||
headers=auth_headers).get_json()
|
||||
assert data['permission_level'] == 'protect'
|
||||
assert '--level protect' in data['linux']['one_liner']
|
||||
assert '-Level protect' in data['windows']['one_liner']
|
||||
|
||||
|
||||
def test_token_records_requested_level(client, server, auth_headers):
|
||||
client.post(f'/api/v1/servers/{server.id}/regenerate-token', headers=auth_headers,
|
||||
json={'requested_level': 'protect'})
|
||||
assert db.session.get(Server, server.id).requested_permission_level == 'protect'
|
||||
@@ -7,9 +7,9 @@ from pathlib import Path
|
||||
from app.core_blueprints import CORE_BLUEPRINTS
|
||||
|
||||
|
||||
EXPECTED_CORE_BLUEPRINT_COUNT = 116
|
||||
EXPECTED_CORE_BLUEPRINT_COUNT = 117
|
||||
EXPECTED_MANIFEST_SHA256 = (
|
||||
'25d28d92ccddddc246d5b2081d84b11f1df10cb662f33c256ebcbe390d2813d5'
|
||||
'52abbc7429d079c7cf6ea61a638f5dc4dbd991e4f02ec31c81563e01442aa7fb'
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -145,6 +145,7 @@ PUBLIC_ALT_AUTH = {
|
||||
# Public by design; the value names the mechanism that authenticates the call.
|
||||
'agent_poll.connect': 'agent HMAC handshake + per-IP throttle',
|
||||
'agent_poll.disconnect': 'X-Session-Token lookup',
|
||||
'agent_report.report': 'HMAC over timestamp+nonce+body hash, skew/replay/rate limits',
|
||||
'agent_poll.poll': 'X-Session-Token resolves the ConnectedAgent',
|
||||
'agent_poll.result': 'X-Session-Token resolves the ConnectedAgent',
|
||||
'auth.connect_session': 'loopback-only + Cloud grant verified against JWKS, 30/min',
|
||||
|
||||
@@ -221,6 +221,7 @@ VIEWER_WRITABLE = {
|
||||
'docker.get_containers_stats',
|
||||
# PUBLIC transport — agent long-poll fallback; user JWT is ignored here.
|
||||
'agent_poll.disconnect',
|
||||
'agent_report.report',
|
||||
# PUBLIC authentication challenge only; credential verification and UV are
|
||||
# required by passkey_authenticate before any session tokens are issued.
|
||||
'auth.passkey_auth_options',
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import importlib.util
|
||||
from pathlib import Path
|
||||
|
||||
from app.models.metric_alert import MetricAlert
|
||||
from app.models.server import Server
|
||||
|
||||
|
||||
def test_seed_is_idempotent(app):
|
||||
path = Path(__file__).parents[1] / 'scripts/seed_permission_demo.py'
|
||||
spec = importlib.util.spec_from_file_location('seed_demo', path)
|
||||
mod = importlib.util.module_from_spec(spec)
|
||||
spec.loader.exec_module(mod)
|
||||
mod.seed()
|
||||
mod.seed()
|
||||
assert Server.query.filter(Server.agent_id.like('demo-perm-%')).count() == 5
|
||||
assert Server.query.filter_by(status='offline').count() == 1
|
||||
assert MetricAlert.query.filter_by(metric='offline').count() == 1
|
||||
@@ -0,0 +1,81 @@
|
||||
# Agent permission levels
|
||||
|
||||
Each ServerKit agent runs at one of three levels. The level is chosen **on the
|
||||
server**, enforced **by the agent**, and merely *reported* to the panel.
|
||||
|
||||
| Level | What it can do | What it cannot do |
|
||||
|-------|----------------|-------------------|
|
||||
| **Monitor** (default for new installs) | Send CPU / memory / disk / uptime / load and service checks (web server, PHP-FPM, database, disk, CPU, memory) to the panel with signed outbound HTTPS POSTs. Opens no inbound command channel. | Run any command, open a terminal, touch files, Docker, packages or services. The panel cannot send it anything. |
|
||||
| **Protect** | Everything in Monitor, plus a fixed security allowlist: firewall (ufw) rules, fail2ban, CrowdSec decisions, Cloudflare origin lock (restrict 80/443 to Cloudflare ranges). | Docker, shell, files, terminal, packages, systemd, cron, SSH keys, WireGuard, runtimes, agent updates. |
|
||||
| **Full** | Today's behaviour: the whole agent feature set. Legacy installs with no level configured are treated as Full. | — |
|
||||
|
||||
## Install
|
||||
|
||||
The Add Server drawer shows the level picker and puts the choice in the command:
|
||||
|
||||
```bash
|
||||
curl -fsSL https://panel.example.com/api/v1/servers/install.sh -o /tmp/serverkit-agent-install.sh \
|
||||
&& sudo bash /tmp/serverkit-agent-install.sh --token "TOKEN" --server "https://panel.example.com" --level monitor
|
||||
```
|
||||
|
||||
```powershell
|
||||
irm https://panel.example.com/api/v1/servers/install.ps1 | iex; Install-ServerKitAgent -Token "TOKEN" -Server "https://panel.example.com" -Level monitor
|
||||
```
|
||||
|
||||
The installer scripts under `scripts/` are vendored from the agent repo
|
||||
(`scripts/sync-agent-installers.sh`); `--level` / `-Level` arrives with the next sync.
|
||||
|
||||
## Changing the level
|
||||
|
||||
Only on the server itself, as root, with a typed confirmation:
|
||||
|
||||
```bash
|
||||
serverkit-agent permissions show
|
||||
sudo serverkit-agent permissions set protect
|
||||
```
|
||||
|
||||
There is no panel button or API for this, by design. Re-running `install.sh` with a
|
||||
different `--level` on an installed box refuses unless `--reconfigure` is passed.
|
||||
|
||||
## What the panel does
|
||||
|
||||
- Records the level the agent reports (registration, auth, heartbeat, report). Every
|
||||
change, escalations included, is written to the audit log and shown in the server
|
||||
timeline. The Add Server flow remembers the *requested* level so a mismatch with
|
||||
the reported one is visible.
|
||||
- Monitor agents POST to `/api/v1/agent/report` (HMAC over timestamp, nonce and body
|
||||
hash; 300 s skew; single-use nonces; 64 KB cap; per-IP+key rate limit). A monitor
|
||||
server with no report for max(3 minutes, 3 x its interval) is marked offline.
|
||||
- Opens an alert (existing fleet alerts + notification channels) when a server goes
|
||||
offline or stops reporting and when a service check goes down; it resolves on
|
||||
recovery and does not repeat while the condition persists.
|
||||
- Refuses to dispatch a command the reported level forbids (`LEVEL_DENIED`,
|
||||
"server is in Monitor mode: ..."). This is defence in depth; the agent enforces
|
||||
the same table. The table lives in `backend/app/services/permission_levels.py`.
|
||||
|
||||
Protect allowlist (checked against the panel table by a test):
|
||||
|
||||
```text allowlist
|
||||
firewall:allow_port firewall:deny_port firewall:status
|
||||
firewall:allow_ip firewall:deny_ip
|
||||
fail2ban:status fail2ban:ban fail2ban:unban
|
||||
crowdsec:decisions:list crowdsec:ban crowdsec:unban
|
||||
cloudflare:origin_lock:status cloudflare:origin_lock:apply cloudflare:origin_lock:remove
|
||||
```
|
||||
|
||||
`credential_update` (signature-verified key rotation) is allowed at every level.
|
||||
|
||||
## Threat model
|
||||
|
||||
The level is read only from the agent's local config at startup; no message from
|
||||
the panel can change it. A compromised panel therefore **cannot escalate** a Monitor
|
||||
agent: it has no channel to send commands to, and the report reply never carries any.
|
||||
The panel's own gate only narrows what it will send. Someone with root on the box
|
||||
can still change the level, which is the intended re-confirmation path, and the
|
||||
panel audits the resulting change. A compromised panel can lie in the UI, but
|
||||
cannot make a Monitor agent act.
|
||||
|
||||
## Demo data
|
||||
|
||||
`python backend/scripts/seed_permission_demo.py` (never run automatically) creates five
|
||||
demo servers for the dev sandbox. It is idempotent.
|
||||
@@ -755,6 +755,7 @@ Regenerate (backend/):
|
||||
- `POST /agent/connect`
|
||||
- `POST /agent/disconnect`
|
||||
- `POST /agent/poll`
|
||||
- `POST /agent/report`
|
||||
- `POST /agent/result`
|
||||
- `POST /ai/chat`
|
||||
- `POST /ai/chat/cancel`
|
||||
|
||||
@@ -21,7 +21,7 @@ debt. Regenerate with `python scripts/generate-migration-inventory.py`.
|
||||
| Controller-boundary violations (routes doing service work) | service layer extraction | 453 | 453 | migrate when touched (first-wave ratchet) |
|
||||
| raw api.* calls in pages/ | E1: useServerQuery/useServerMutation | 374 | 398 | migrate when touched |
|
||||
| per-page toast.error extractions in pages/ | E1: query-layer error presentation | 191 | 206 | migrate when touched |
|
||||
| hand-rolled form-group blocks | F2: FormField/useForm | 314 | 321 | migrate when touched |
|
||||
| hand-rolled form-group blocks | F2: FormField/useForm | 315 | 321 | migrate when touched |
|
||||
| unencoded ?k=${v} query interpolations in services/api | C4: buildQuery/encoding template | 0 | 0 | INVARIANT at 0 |
|
||||
| raw setInterval pollers | E2: usePolling/refetchInterval | 7 | 7 | DELIBERATE RESIDUE: clock ticks, socket-fallback hooks, and sibling-repo extension timers - each listed per file |
|
||||
| direct navigator.clipboard call sites | F3: copyToClipboard | 0 | 0 | INVARIANT at 0 |
|
||||
|
||||
@@ -11,6 +11,7 @@ Welcome to the ServerKit documentation. This guide will help you install, config
|
||||
| [Video: template to live app](https://youtu.be/TXfXFz6bVjs) | Watch a 4-minute AgentSite deployment through the template catalog and live Deploy Console |
|
||||
| [Video: deploy a Git repository](https://youtu.be/Fv28qMERYDU) | Connect a repository, review detected settings and deploy through the three-step New Service wizard |
|
||||
| [Installation Guide](INSTALLATION.md) | Complete setup instructions for Docker and manual installation |
|
||||
| [Agent permission levels](AGENT_PERMISSION_LEVELS.md) | Monitor / Protect / Full: what each allows and how to change it |
|
||||
| [Architecture](ARCHITECTURE.md) | System design, request flow, and diagrams |
|
||||
| [Deployment Guide](DEPLOYMENT.md) | Production deployment, CLI commands, and configuration |
|
||||
| [Local Development](LOCAL_DEVELOPMENT.md) | Run the panel locally for development |
|
||||
|
||||
@@ -0,0 +1,102 @@
|
||||
import { useEffect, useState } from 'react';
|
||||
import api from '../../services/api';
|
||||
import { DataTable, Pill } from '@/components/ds';
|
||||
import { InfoList, InfoItem } from '../InfoList';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
import { formatDateTime } from '@/utils/intl';
|
||||
import { levelKind, serverHealth } from '@/utils/agentPermissionLevel';
|
||||
|
||||
const SET_HINT = 'serverkit-agent permissions set <monitor|protect|full>';
|
||||
|
||||
const checkKind = (status) => {
|
||||
if (status === 'up') return 'green';
|
||||
if (status === 'down') return 'red';
|
||||
if (status === 'degraded') return 'amber';
|
||||
return 'gray';
|
||||
};
|
||||
|
||||
// Read-only by design: the permission level is enforced by the agent and can
|
||||
// only be changed on the server itself, so there is deliberately no control.
|
||||
const ServerHealthCard = ({ server }) => {
|
||||
const { t } = useTranslation();
|
||||
const [alerts, setAlerts] = useState([]);
|
||||
|
||||
useEffect(() => {
|
||||
let alive = true;
|
||||
api.getFleetAlerts({ server_id: server.id, status: 'active', limit: 20 })
|
||||
.then((res) => { if (alive) setAlerts((Array.isArray(res) ? res : []).filter((a) => a.detail)); })
|
||||
.catch(() => { if (alive) setAlerts([]); });
|
||||
return () => { alive = false; };
|
||||
}, [server.id, server.status, server.last_report_at]);
|
||||
|
||||
const level = server.permission_level_effective || 'full';
|
||||
const health = serverHealth(server);
|
||||
const mismatch = server.requested_permission_level
|
||||
&& server.permission_level
|
||||
&& server.requested_permission_level !== server.permission_level;
|
||||
const levelName = {
|
||||
monitor: t('app.serverHealthCard.monitor', 'Monitor'),
|
||||
protect: t('app.serverHealthCard.protect', 'Protect'),
|
||||
full: t('app.serverHealthCard.full', 'Full'),
|
||||
}[level];
|
||||
|
||||
const columns = [
|
||||
{ key: 'name', headerKey: 'app.serverHealthCard.service', header: 'Service', sortable: false, hideable: false, render: (c) => c.name },
|
||||
{ key: 'kind', headerKey: 'app.serverHealthCard.kind', header: 'Kind', sortable: false, cellClassName: 'sk-cell-dim', render: (c) => c.kind || '—' },
|
||||
{ key: 'status', headerKey: 'common.labels.status', header: 'Status', sortable: false, render: (c) => <Pill kind={checkKind(c.status)}>{c.status}</Pill> },
|
||||
{ key: 'detail', headerKey: 'app.serverHealthCard.detail', header: 'Detail', sortable: false, cellClassName: 'sk-cell-dim', render: (c) => c.detail || '—' },
|
||||
{ key: 'checked', headerKey: 'app.serverHealthCard.lastChecked', header: 'Last checked', sortable: false, cellClassName: 'sk-cell-dim', render: (c) => (c.checked_at ? formatDateTime(c.checked_at) : '—') },
|
||||
];
|
||||
|
||||
return (
|
||||
<>
|
||||
<div className="info-card">
|
||||
<h3>{t('app.serverHealthCard.permissionLevel', 'Permission level')}</h3>
|
||||
<InfoList className="server-info-list">
|
||||
<InfoItem label={t('app.serverHealthCard.level', 'Level')}>
|
||||
<Pill kind={levelKind(level)} dot={false}>{levelName}</Pill>
|
||||
{!server.permission_level && (
|
||||
<span className="text-muted"> {t('app.serverHealthCard.legacyAgent', 'Not reported (legacy agent, treated as Full)')}</span>
|
||||
)}
|
||||
</InfoItem>
|
||||
<InfoItem
|
||||
label={t('app.serverHealthCard.changedAt', 'Last change')}
|
||||
value={server.permission_level_updated_at ? formatDateTime(server.permission_level_updated_at) : '—'}
|
||||
/>
|
||||
</InfoList>
|
||||
{mismatch && (
|
||||
<p className="form-hint">
|
||||
{t('app.serverHealthCard.mismatch', 'The install command requested a different level than the agent reports. The agent\'s level is the one in effect.')}
|
||||
</p>
|
||||
)}
|
||||
<p className="text-muted">
|
||||
{t('app.serverHealthCard.howToChange', 'This level is enforced by the agent and cannot be changed from the panel. To change it, run this on the server as root:')}
|
||||
</p>
|
||||
<code>{SET_HINT}</code>
|
||||
</div>
|
||||
|
||||
<div className="info-card">
|
||||
<h3>{t('app.serverHealthCard.health', 'Health')} <Pill kind={health.kind}>{health.code}</Pill></h3>
|
||||
{server.last_report_at && (
|
||||
<p className="text-muted">
|
||||
{t('app.serverHealthCard.lastReport', 'Last report')}: {formatDateTime(server.last_report_at)}
|
||||
</p>
|
||||
)}
|
||||
{alerts.length > 0 && (
|
||||
<ul className="notifications-list">
|
||||
{alerts.map((a) => <li key={a.id}>{a.detail}</li>)}
|
||||
</ul>
|
||||
)}
|
||||
<DataTable
|
||||
columns={columns}
|
||||
data={server.service_checks || []}
|
||||
keyField="name"
|
||||
emptyTitle={t('app.serverHealthCard.noChecks', 'No service checks reported yet.')}
|
||||
emptyMessage=""
|
||||
/>
|
||||
</div>
|
||||
</>
|
||||
);
|
||||
};
|
||||
|
||||
export default ServerHealthCard;
|
||||
@@ -2,6 +2,7 @@ import { serverStatusKind } from './serverDetailData';
|
||||
import { Pill, KpiBand } from '../ds';
|
||||
import OnboardingWizard from '../server/OnboardingWizard';
|
||||
import SystemStatusCard from './SystemStatusCard';
|
||||
import ServerHealthCard from './ServerHealthCard';
|
||||
import { formatBytes } from '@/utils/formatBytes';
|
||||
import { InfoList, InfoItem } from '../InfoList';
|
||||
import { useTranslation } from 'react-i18next';
|
||||
@@ -163,6 +164,8 @@ const ServerOverviewTab = ({ server, metrics, systemInfo, onRefreshServer }) =>
|
||||
</InfoList>
|
||||
</div>
|
||||
|
||||
<ServerHealthCard server={server} />
|
||||
|
||||
<div className="overview-grid__full">
|
||||
<SystemStatusCard server={server} onRefresh={onRefreshServer} />
|
||||
</div>
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// Small inline illustrations for the permission-level cards. They use
|
||||
// currentColor plus the panel accent variables, so they follow the theme.
|
||||
const common = {
|
||||
viewBox: '0 0 96 56',
|
||||
height: 56,
|
||||
'aria-hidden': true,
|
||||
focusable: 'false',
|
||||
fill: 'none',
|
||||
stroke: 'currentColor',
|
||||
strokeWidth: 2,
|
||||
strokeLinecap: 'round',
|
||||
strokeLinejoin: 'round',
|
||||
};
|
||||
|
||||
const Monitor = () => (
|
||||
<svg {...common}>
|
||||
<path d="M8 24c9-12 21-18 40-18s31 6 40 18c-9 12-21 18-40 18S17 36 8 24z" opacity=".45" />
|
||||
<circle cx="48" cy="24" r="9" opacity=".45" />
|
||||
<path d="M4 48h22l6-10 8 16 7-12 5 6h40" stroke="var(--accent, currentColor)" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
const Protect = () => (
|
||||
<svg {...common}>
|
||||
<path d="M48 4l28 8v16c0 13-10 21-28 26-18-5-28-13-28-26V12z" opacity=".45" />
|
||||
<path d="M34 22h28M34 30h28M34 38h28M48 22v8M41 30v8M55 30v8" opacity=".5" />
|
||||
<path d="M39 29l7 7 13-14" stroke="var(--accent, currentColor)" strokeWidth={3} />
|
||||
</svg>
|
||||
);
|
||||
|
||||
const Full = () => (
|
||||
<svg {...common}>
|
||||
<rect x="8" y="6" width="80" height="44" rx="5" opacity=".45" />
|
||||
<path d="M8 16h80" opacity=".45" />
|
||||
<path d="M16 26l7 6-7 6" stroke="var(--accent, currentColor)" />
|
||||
<path d="M28 40h10" opacity=".5" />
|
||||
<circle cx="62" cy="31" r="6" stroke="var(--accent, currentColor)" />
|
||||
<path d="M68 31h16M78 31v6M84 31v4" stroke="var(--accent, currentColor)" />
|
||||
</svg>
|
||||
);
|
||||
|
||||
const ILLUSTRATIONS = { monitor: Monitor, protect: Protect, full: Full };
|
||||
|
||||
const LevelIllustration = ({ level }) => {
|
||||
const Svg = ILLUSTRATIONS[level] || Monitor;
|
||||
return <Svg />;
|
||||
};
|
||||
|
||||
export default LevelIllustration;
|
||||
@@ -5443,6 +5443,24 @@
|
||||
"theAgentUserIsnTIn": "The agent user isn't in the",
|
||||
"theNpipeSocket": "The npipe socket"
|
||||
},
|
||||
"serverHealthCard": {
|
||||
"changedAt": "Last change",
|
||||
"detail": "Detail",
|
||||
"full": "Full",
|
||||
"health": "Health",
|
||||
"howToChange": "This level is enforced by the agent and cannot be changed from the panel. To change it, run this on the server as root:",
|
||||
"kind": "Kind",
|
||||
"lastChecked": "Last checked",
|
||||
"lastReport": "Last report",
|
||||
"legacyAgent": "Not reported (legacy agent, treated as Full)",
|
||||
"level": "Level",
|
||||
"mismatch": "The install command requested a different level than the agent reports. The agent's level is the one in effect.",
|
||||
"monitor": "Monitor",
|
||||
"noChecks": "No service checks reported yet.",
|
||||
"permissionLevel": "Permission level",
|
||||
"protect": "Protect",
|
||||
"service": "Service"
|
||||
},
|
||||
"serverKitLogo": {
|
||||
"serverkitLogo": "ServerKit logo"
|
||||
},
|
||||
@@ -5652,6 +5670,8 @@
|
||||
"agentFound": "Agent found",
|
||||
"agentPairedSuccessfully": "Agent paired",
|
||||
"autoDetectedFromAgentOptional": "Auto-detected from agent (optional)",
|
||||
"capAllowed": "allowed",
|
||||
"capBlocked": "blocked",
|
||||
"clearSelection": "Clear selection",
|
||||
"confirmThisFingerprintMatchesTheOne": "Confirm this fingerprint matches the one shown by the agent before continuing.",
|
||||
"connectionString": "Connection string",
|
||||
@@ -5674,10 +5694,29 @@
|
||||
"groupDeleted": "Group deleted",
|
||||
"groupUpdated": "Group updated",
|
||||
"groups": "Groups",
|
||||
"health": "Health",
|
||||
"healthDegraded": "Degraded",
|
||||
"healthFailing": "Failing",
|
||||
"healthHealthy": "Healthy",
|
||||
"healthOffline": "Offline",
|
||||
"healthPending": "Pending",
|
||||
"healthStale": "Stale",
|
||||
"host": "Host",
|
||||
"hostname": "Hostname:",
|
||||
"lastSeen": "Last seen",
|
||||
"leaveBlankToUseTheAgent": "Leave blank to use the agent's hostname.",
|
||||
"levelColumn": "Level",
|
||||
"levelEnforcedShort": "Enforced on the server by the agent. Change with",
|
||||
"levelFull": "Full",
|
||||
"levelFullCan": "Everything the agent supports: Docker, files, terminal, packages, services and more.",
|
||||
"levelFullCannot": "No restrictions. Choose this only for servers you want this panel to fully manage.",
|
||||
"levelInfo": "What can {{level}} do?",
|
||||
"levelMonitor": "Monitor",
|
||||
"levelMonitorCan": "Reports CPU, memory, disk and service health (web server, PHP-FPM, database) to this panel.",
|
||||
"levelMonitorCannot": "Cannot run commands, open a terminal or change anything. The panel cannot send it any instructions.",
|
||||
"levelProtect": "Protect",
|
||||
"levelProtectCan": "Everything in Monitor, plus firewall, fail2ban, CrowdSec and Cloudflare origin-lock actions.",
|
||||
"levelProtectCannot": "Cannot touch Docker, files, packages, services, shell or updates.",
|
||||
"linkPanel": "Link panel",
|
||||
"linux": "Linux",
|
||||
"manageServerGroups": "Manage server groups",
|
||||
@@ -5700,6 +5739,7 @@
|
||||
"passphrase": "Passphrase *",
|
||||
"pasteThisIntoTheAgentOr": "Paste this into the agent, or run the installer.",
|
||||
"pending": "Pending",
|
||||
"permissionLevel": "Permission level",
|
||||
"runAsAdministrator": "Run as administrator",
|
||||
"scanningFleet": "Scanning fleet…",
|
||||
"searchServers": "Search servers…",
|
||||
|
||||
@@ -288,9 +288,10 @@ export default function Incidents() {
|
||||
kind: 'alert',
|
||||
key: `fleet-${alert.id}`,
|
||||
alertId: alert.id,
|
||||
title: `${metric} over limit on ${subject}`,
|
||||
// Offline / service-check alerts carry their own message and no threshold.
|
||||
title: alert.detail || `${metric} over limit on ${subject}`,
|
||||
subject,
|
||||
detail: `${metric} ${formatValue(alert.value)} / ${alert.threshold}`,
|
||||
detail: alert.detail || `${metric} ${formatValue(alert.value)} / ${alert.threshold}`,
|
||||
state: alert.status,
|
||||
tone: alertStatusKind(alert.status),
|
||||
impact: alert.severity,
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { useState, useEffect, useCallback, useMemo, useId } from 'react';
|
||||
import { useNavigate } from 'react-router-dom';
|
||||
import { ChevronRight, Folder, Plus, RefreshCw, Server as ServerLucideIcon, X } from 'lucide-react';
|
||||
import { Ban, ChevronRight, Container, Cpu, Database, Flame, Folder, HardDrive, Info, Lock, MemoryStick, Plus, RefreshCw, Server as ServerLucideIcon, Terminal, X } from 'lucide-react';
|
||||
import { Tooltip, TooltipTrigger, TooltipContent } from '@/components/ui/tooltip';
|
||||
import LevelIllustration from '../components/servers/LevelIllustrations';
|
||||
import api from '../services/api';
|
||||
import { useToast } from '../contexts/useToast.js';
|
||||
import EmptyState from '../components/EmptyState';
|
||||
@@ -29,6 +31,9 @@ import { useTranslation } from 'react-i18next';
|
||||
import { t } from '../i18n/t';
|
||||
import { toastError } from '@/utils/errorMessage';
|
||||
import { formatDate } from '@/utils/intl';
|
||||
import {
|
||||
DEFAULT_LEVEL, PERMISSION_LEVELS, buildInstallCommands, levelAllows, levelKind, serverHealth,
|
||||
} from '@/utils/agentPermissionLevel';
|
||||
|
||||
// Radix Select items cannot use '' — stands in for "no group".
|
||||
const NO_GROUP = '__none';
|
||||
@@ -231,6 +236,38 @@ const SERVER_COLUMNS = [
|
||||
return <Pill kind={statusKind(status)}>{status}</Pill>;
|
||||
},
|
||||
},
|
||||
{
|
||||
key: 'level',
|
||||
headerKey: 'app.servers.levelColumn', header: 'Level',
|
||||
sortable: true,
|
||||
type: 'enum',
|
||||
// NULL is a legacy agent that predates levels, which acts as full.
|
||||
value: (server) => server.permission_level_effective || 'full',
|
||||
sortValue: (server) => server.permission_level_effective || 'full',
|
||||
groupable: true,
|
||||
render: (server) => {
|
||||
const level = server.permission_level_effective || 'full';
|
||||
return (
|
||||
<Pill kind={levelKind(level)} dot={false}>
|
||||
{levelLabel(level)}
|
||||
</Pill>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
key: 'health',
|
||||
headerKey: 'app.servers.health', header: 'Health',
|
||||
sortable: false,
|
||||
render: (server) => {
|
||||
const health = serverHealth(server);
|
||||
const detail = health.failing.length ? ` (${health.failing.join(', ')})` : '';
|
||||
return (
|
||||
<Pill kind={health.kind} title={detail || undefined}>
|
||||
{healthLabel(health.code) + detail}
|
||||
</Pill>
|
||||
);
|
||||
},
|
||||
},
|
||||
meterColumn('cpu', 'CPU', 'cpu_percent'),
|
||||
meterColumn('memory', 'Memory', 'memory_percent'),
|
||||
meterColumn('disk', 'Disk', 'disk_percent'),
|
||||
@@ -766,12 +803,120 @@ const EXPIRY_OPTIONS = [
|
||||
];
|
||||
const DEFAULT_EXPIRY = 7 * 24 * 60 * 60;
|
||||
|
||||
const SET_HINT = 'serverkit-agent permissions set <level>';
|
||||
|
||||
const levelLabel = (level) => {
|
||||
if (level === 'monitor') return t('app.servers.levelMonitor', 'Monitor');
|
||||
if (level === 'protect') return t('app.servers.levelProtect', 'Protect');
|
||||
return t('app.servers.levelFull', 'Full');
|
||||
};
|
||||
|
||||
const healthLabel = (code) => {
|
||||
switch (code) {
|
||||
case 'healthy': return t('app.servers.healthHealthy', 'Healthy');
|
||||
case 'degraded': return t('app.servers.healthDegraded', 'Degraded');
|
||||
case 'failing': return t('app.servers.healthFailing', 'Failing');
|
||||
case 'stale': return t('app.servers.healthStale', 'Stale');
|
||||
case 'offline': return t('app.servers.healthOffline', 'Offline');
|
||||
default: return t('app.servers.healthPending', 'Pending');
|
||||
}
|
||||
};
|
||||
|
||||
const CAPABILITY_ICONS = {
|
||||
cpu: [Cpu, 'CPU'],
|
||||
memory: [MemoryStick, 'Memory'],
|
||||
disk: [HardDrive, 'Disk'],
|
||||
services: [ServerLucideIcon, 'Web server and PHP'],
|
||||
database: [Database, 'Database'],
|
||||
firewall: [Flame, 'Firewall'],
|
||||
ban: [Ban, 'Ban (fail2ban, CrowdSec)'],
|
||||
terminal: [Terminal, 'Terminal'],
|
||||
docker: [Container, 'Docker'],
|
||||
files: [Folder, 'Files'],
|
||||
};
|
||||
|
||||
const LevelPicker = ({ value, onChange }) => {
|
||||
const { t } = useTranslation();
|
||||
const copy = {
|
||||
monitor: {
|
||||
can: t('app.servers.levelMonitorCan', 'Reports CPU, memory, disk and service health (web server, PHP-FPM, database) to this panel.'),
|
||||
cannot: t('app.servers.levelMonitorCannot', 'Cannot run commands, open a terminal or change anything. The panel cannot send it any instructions.'),
|
||||
},
|
||||
protect: {
|
||||
can: t('app.servers.levelProtectCan', 'Everything in Monitor, plus firewall, fail2ban, CrowdSec and Cloudflare origin-lock actions.'),
|
||||
cannot: t('app.servers.levelProtectCannot', 'Cannot touch Docker, files, packages, services, shell or updates.'),
|
||||
},
|
||||
full: {
|
||||
can: t('app.servers.levelFullCan', 'Everything the agent supports: Docker, files, terminal, packages, services and more.'),
|
||||
cannot: t('app.servers.levelFullCannot', 'No restrictions. Choose this only for servers you want this panel to fully manage.'),
|
||||
},
|
||||
};
|
||||
const onKeyDown = (e, level) => {
|
||||
if (e.key === ' ' || e.key === 'Enter') {
|
||||
e.preventDefault();
|
||||
onChange(level);
|
||||
}
|
||||
};
|
||||
return (
|
||||
<div className="form-group">
|
||||
<span className="form-label" id="add-server-level-label">{t('app.servers.permissionLevel', 'Permission level')}</span>
|
||||
<div className="level-picker" role="radiogroup" aria-labelledby="add-server-level-label">
|
||||
{PERMISSION_LEVELS.map((level) => (
|
||||
<div key={level} className="level-picker__cell">
|
||||
<div role="radio" tabIndex={0}
|
||||
aria-checked={value === level}
|
||||
className={`level-picker__opt${value === level ? ' is-active' : ''}`}
|
||||
onClick={() => onChange(level)}
|
||||
onKeyDown={(e) => onKeyDown(e, level)}
|
||||
>
|
||||
<span className="level-picker__art"><LevelIllustration level={level} /></span>
|
||||
<span className="level-picker__head">{levelLabel(level)}</span>
|
||||
<span className="level-picker__caps">
|
||||
{Object.entries(CAPABILITY_ICONS).map(([cap, [Icon, label]]) => {
|
||||
const allowed = levelAllows(level, cap);
|
||||
const text = `${label}: ${allowed ? t('app.servers.capAllowed', 'allowed') : t('app.servers.capBlocked', 'blocked')}`;
|
||||
return (
|
||||
<span key={cap} role="img" aria-label={text} title={text}
|
||||
className={`level-picker__cap${allowed ? '' : ' is-blocked'}`}>
|
||||
<Icon size={16} aria-hidden="true" />
|
||||
</span>
|
||||
);
|
||||
})}
|
||||
</span>
|
||||
</div>
|
||||
<Tooltip>
|
||||
<TooltipTrigger asChild>
|
||||
<Button variant="unstyled" type="button" className="level-picker__info"
|
||||
aria-label={t('app.servers.levelInfo', 'What can {{level}} do?', { level: levelLabel(level) })}
|
||||
onClick={(e) => e.stopPropagation()}
|
||||
>
|
||||
<Info size={14} aria-hidden="true" />
|
||||
</Button>
|
||||
</TooltipTrigger>
|
||||
<TooltipContent side="top" align="end" className="level-picker__tip">
|
||||
<span>{copy[level].can}</span>
|
||||
<span>{copy[level].cannot}</span>
|
||||
</TooltipContent>
|
||||
</Tooltip>
|
||||
</div>
|
||||
))}
|
||||
</div>
|
||||
<span className="form-hint level-picker__note">
|
||||
<Lock size={12} aria-hidden="true" />
|
||||
{t('app.servers.levelEnforcedShort', 'Enforced on the server by the agent. Change with')}
|
||||
{' '}<code>{SET_HINT}</code>
|
||||
</span>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
const AddServerModal = ({ groups, onClose, onCreated }) => {
|
||||
const { t } = useTranslation();
|
||||
const [mode, setMode] = useState('install');
|
||||
const [step, setStep] = useState(1);
|
||||
const [groupId, setGroupId] = useState('');
|
||||
const [expiresIn, setExpiresIn] = useState(DEFAULT_EXPIRY);
|
||||
const [level, setLevel] = useState(DEFAULT_LEVEL);
|
||||
const [registrationData, setRegistrationData] = useState(null);
|
||||
const [loading, setLoading] = useState(false);
|
||||
const [error, setError] = useState('');
|
||||
@@ -789,6 +934,7 @@ const AddServerModal = ({ groups, onClose, onCreated }) => {
|
||||
const result = await api.createServer({
|
||||
expires_in: expiresIn,
|
||||
group_id: groupId || undefined,
|
||||
requested_level: level,
|
||||
});
|
||||
setRegistrationData(result);
|
||||
setStep(2);
|
||||
@@ -808,13 +954,15 @@ const AddServerModal = ({ groups, onClose, onCreated }) => {
|
||||
// the operator sees one line of curl noise, the shell says success, and
|
||||
// nothing was installed. The `&&` makes a failed fetch stop the command and
|
||||
// surface curl's non-zero exit (issue #101).
|
||||
const linuxInstallScript = registrationData ? `curl -fsSL ${window.location.origin}/api/v1/servers/install.sh -o /tmp/serverkit-agent-install.sh && \\
|
||||
sudo bash /tmp/serverkit-agent-install.sh \\
|
||||
--server "${window.location.origin}" \\
|
||||
--token "${registrationData.registration_token}"` : '';
|
||||
|
||||
const windowsInstallScript = registrationData ? `irm ${window.location.origin}/api/v1/servers/install.ps1 | iex
|
||||
Install-ServerKitAgent -Server "${window.location.origin}" -Token "${registrationData.registration_token}"` : '';
|
||||
const installCommands = registrationData
|
||||
? buildInstallCommands({
|
||||
origin: window.location.origin,
|
||||
token: registrationData.registration_token,
|
||||
level,
|
||||
})
|
||||
: { linux: '', windows: '' };
|
||||
const linuxInstallScript = installCommands.linux;
|
||||
const windowsInstallScript = installCommands.windows;
|
||||
|
||||
// A right-side drawer, not a centred dialog: this flow is a multi-step
|
||||
// wizard with install scripts in it, and it always was a drawer — the
|
||||
@@ -887,6 +1035,8 @@ Install-ServerKitAgent -Server "${window.location.origin}" -Token "${registratio
|
||||
{t('app.servers.generateASingleConnectionStringPaste', "Generate a single connection string. Paste it into the agent's pairing wizard, or use it with the one-liner installer. The agent's hostname becomes the server name on first connect; you can rename it later from the server's Settings tab.")}
|
||||
</p>
|
||||
|
||||
<LevelPicker value={level} onChange={setLevel} />
|
||||
|
||||
<div className="form-row">
|
||||
<div className="form-group">
|
||||
<label htmlFor="add-server-group">{t('app.servers.group', 'Group')}</label>
|
||||
|
||||
@@ -2580,3 +2580,128 @@
|
||||
flex-direction: column;
|
||||
gap: $space-3;
|
||||
}
|
||||
|
||||
// Permission-level picker in the Add Server drawer
|
||||
.level-picker {
|
||||
display: grid;
|
||||
grid-template-columns: repeat(3, minmax(0, 1fr));
|
||||
gap: $space-2;
|
||||
|
||||
@media (max-width: 560px) {
|
||||
grid-template-columns: 1fr;
|
||||
}
|
||||
|
||||
// The (i) button is a sibling of the radio card, not nested in it.
|
||||
&__cell {
|
||||
position: relative;
|
||||
display: flex;
|
||||
}
|
||||
|
||||
&__opt {
|
||||
flex: 1;
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
align-items: center;
|
||||
gap: $space-2;
|
||||
padding: $space-3;
|
||||
text-align: center;
|
||||
color: $text-tertiary;
|
||||
background: $bg-card;
|
||||
border: 1px solid $border-subtle;
|
||||
border-radius: $radius-control;
|
||||
cursor: pointer;
|
||||
transition: border-color $transition-fast, background $transition-fast, box-shadow $transition-fast;
|
||||
|
||||
&:hover {
|
||||
border-color: $border-active;
|
||||
background: $bg-elevated;
|
||||
}
|
||||
|
||||
&:focus-visible {
|
||||
outline: 2px solid var(--accent-dim);
|
||||
outline-offset: 2px;
|
||||
}
|
||||
|
||||
&.is-active {
|
||||
color: var(--accent);
|
||||
border-color: var(--accent-dim);
|
||||
background: var(--accent-bg);
|
||||
}
|
||||
}
|
||||
|
||||
&__art svg {
|
||||
display: block;
|
||||
width: auto;
|
||||
max-width: 100%;
|
||||
height: 56px;
|
||||
}
|
||||
|
||||
&__head {
|
||||
font-size: $text-sm;
|
||||
font-weight: $font-weight-semibold;
|
||||
color: $text-primary;
|
||||
}
|
||||
|
||||
&__caps {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
justify-content: center;
|
||||
gap: $space-1;
|
||||
}
|
||||
|
||||
&__cap {
|
||||
position: relative;
|
||||
display: inline-flex;
|
||||
color: $text-secondary;
|
||||
|
||||
.is-active & {
|
||||
color: var(--accent);
|
||||
}
|
||||
|
||||
&.is-blocked {
|
||||
color: $text-tertiary;
|
||||
opacity: .5;
|
||||
|
||||
&::after {
|
||||
content: '';
|
||||
position: absolute;
|
||||
inset: 50% -2px auto;
|
||||
height: 1.5px;
|
||||
background: currentColor;
|
||||
transform: rotate(-45deg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
&__info {
|
||||
position: absolute;
|
||||
top: $space-1;
|
||||
inset-inline-end: $space-1;
|
||||
display: inline-flex;
|
||||
padding: $space-1;
|
||||
color: $text-tertiary;
|
||||
background: none;
|
||||
border: 0;
|
||||
border-radius: 50%;
|
||||
cursor: pointer;
|
||||
|
||||
&:hover,
|
||||
&:focus-visible {
|
||||
color: $text-primary;
|
||||
}
|
||||
}
|
||||
|
||||
&__tip {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
max-width: 260px;
|
||||
white-space: normal;
|
||||
}
|
||||
|
||||
&__note {
|
||||
display: flex;
|
||||
flex-wrap: wrap;
|
||||
align-items: center;
|
||||
gap: $space-1;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
import { buildInstallCommands, CAPABILITIES, levelAllows, normalizeLevel, serverHealth } from '../agentPermissionLevel.js';
|
||||
|
||||
const NOW = Date.parse('2026-01-01T12:00:00Z');
|
||||
const ago = (s) => new Date(NOW - s * 1000).toISOString();
|
||||
|
||||
test('install commands carry the level', () => {
|
||||
const cmds = buildInstallCommands({ origin: 'https://p.example', token: 'T', level: 'protect' });
|
||||
assert.match(cmds.linux, /--level protect$/);
|
||||
assert.match(cmds.windows, /-Level protect$/);
|
||||
});
|
||||
|
||||
test('unknown level falls back to monitor', () => {
|
||||
assert.equal(normalizeLevel('root'), null);
|
||||
const cmds = buildInstallCommands({ origin: 'o', token: 'T', level: 'root' });
|
||||
assert.match(cmds.linux, /--level monitor$/);
|
||||
});
|
||||
|
||||
test('health combines status, report age and checks', () => {
|
||||
const base = { status: 'online', last_report_at: ago(30), report_interval_seconds: 60 };
|
||||
assert.equal(serverHealth(base, NOW).code, 'healthy');
|
||||
assert.equal(serverHealth({ ...base, service_checks: [{ name: 'mysql', status: 'down' }] }, NOW).code, 'failing');
|
||||
assert.equal(serverHealth({ ...base, service_checks: [{ name: 'php', status: 'degraded' }] }, NOW).kind, 'amber');
|
||||
assert.equal(serverHealth({ ...base, last_report_at: ago(600) }, NOW).code, 'stale');
|
||||
assert.equal(serverHealth({ ...base, status: 'offline' }, NOW).kind, 'red');
|
||||
assert.equal(serverHealth({ status: 'pending' }, NOW).code, 'pending');
|
||||
});
|
||||
|
||||
test('capability matrix per level', () => {
|
||||
const blocked = (level) => CAPABILITIES.filter((c) => !levelAllows(level, c));
|
||||
assert.deepEqual(blocked('monitor'), ['firewall', 'ban', 'terminal', 'docker', 'files']);
|
||||
assert.deepEqual(blocked('protect'), ['terminal', 'docker', 'files']);
|
||||
assert.deepEqual(blocked('full'), []);
|
||||
assert.equal(levelAllows('protect', 'firewall'), true);
|
||||
assert.equal(levelAllows('protect', 'ban'), true);
|
||||
assert.equal(levelAllows('bogus', 'terminal'), false);
|
||||
});
|
||||
@@ -0,0 +1,71 @@
|
||||
// Agent permission levels: install-command builder and server health summary.
|
||||
// The level is enforced by the agent and only reported to the panel; nothing
|
||||
// here (or anywhere in the UI) changes it.
|
||||
|
||||
export const PERMISSION_LEVELS = ['monitor', 'protect', 'full'];
|
||||
export const DEFAULT_LEVEL = 'monitor';
|
||||
|
||||
const MIN_SILENCE_MS = 3 * 60 * 1000;
|
||||
|
||||
export function normalizeLevel(level) {
|
||||
return PERMISSION_LEVELS.includes(level) ? level : null;
|
||||
}
|
||||
|
||||
export function levelKind(level) {
|
||||
if (level === 'monitor') return 'cyan';
|
||||
if (level === 'protect') return 'violet';
|
||||
return 'amber';
|
||||
}
|
||||
|
||||
export function buildInstallCommands({ origin, token, level }) {
|
||||
const lvl = normalizeLevel(level) || DEFAULT_LEVEL;
|
||||
return {
|
||||
linux: `curl -fsSL ${origin}/api/v1/servers/install.sh -o /tmp/serverkit-agent-install.sh && \\
|
||||
sudo bash /tmp/serverkit-agent-install.sh \\
|
||||
--server "${origin}" \\
|
||||
--token "${token}" \\
|
||||
--level ${lvl}`,
|
||||
windows: `irm ${origin}/api/v1/servers/install.ps1 | iex
|
||||
Install-ServerKitAgent -Server "${origin}" -Token "${token}" -Level ${lvl}`,
|
||||
};
|
||||
}
|
||||
|
||||
// Health = online/offline + report age + failing service checks.
|
||||
// -> { kind: Pill kind, code, failing: [check names], degraded: [check names] }
|
||||
export function serverHealth(server, now = Date.now()) {
|
||||
const status = server.status || 'pending';
|
||||
if (status === 'pending' || status === 'connecting') {
|
||||
return { kind: 'gray', code: 'pending', failing: [], degraded: [] };
|
||||
}
|
||||
const checks = server.service_checks || [];
|
||||
const failing = checks.filter((c) => c.status === 'down').map((c) => c.name);
|
||||
const degraded = checks.filter((c) => c.status === 'degraded').map((c) => c.name);
|
||||
if (status !== 'online') return { kind: 'red', code: 'offline', failing, degraded };
|
||||
|
||||
const seen = server.last_report_at || server.last_seen;
|
||||
const limit = Math.max(MIN_SILENCE_MS, 3 * 1000 * (server.report_interval_seconds || 0));
|
||||
if (seen && now - new Date(seen).getTime() > limit) {
|
||||
return { kind: 'amber', code: 'stale', failing, degraded };
|
||||
}
|
||||
if (failing.length) return { kind: 'red', code: 'failing', failing, degraded };
|
||||
if (degraded.length) return { kind: 'amber', code: 'degraded', failing, degraded };
|
||||
return { kind: 'green', code: 'healthy', failing, degraded };
|
||||
}
|
||||
|
||||
// What each level lets the panel do on a server. Mirrors
|
||||
// backend/app/services/permission_levels.py: monitor is report-only, protect
|
||||
// adds the PROTECT_ACTIONS (firewall, fail2ban/CrowdSec, Cloudflare origin
|
||||
// lock), full is everything.
|
||||
export const CAPABILITIES = ['cpu', 'memory', 'disk', 'services', 'database', 'firewall', 'ban', 'terminal', 'docker', 'files'];
|
||||
const HEALTH = ['cpu', 'memory', 'disk', 'services', 'database'];
|
||||
const PROTECT = [...HEALTH, 'firewall', 'ban'];
|
||||
|
||||
export const LEVEL_CAPABILITIES = {
|
||||
monitor: HEALTH,
|
||||
protect: PROTECT,
|
||||
full: CAPABILITIES,
|
||||
};
|
||||
|
||||
export function levelAllows(level, capability) {
|
||||
return (LEVEL_CAPABILITIES[normalizeLevel(level) || DEFAULT_LEVEL] || []).includes(capability);
|
||||
}
|
||||
+98
-5
@@ -30,6 +30,17 @@
|
||||
.PARAMETER Version
|
||||
Specific agent version to install (optional, defaults to latest)
|
||||
|
||||
.PARAMETER Level
|
||||
Permission level: monitor (report-only, no inbound commands), protect
|
||||
(security actions only) or full (everything). If omitted: a menu on an
|
||||
interactive console, otherwise full with a warning.
|
||||
|
||||
.PARAMETER Reconfigure
|
||||
Required to change the level of an already-installed agent.
|
||||
|
||||
.PARAMETER Yes
|
||||
Skip the typed confirmation when reconfiguring.
|
||||
|
||||
.EXAMPLE
|
||||
.\install.ps1 -Token "sk_reg_xxx" -Server "https://your-serverkit.com"
|
||||
|
||||
@@ -53,7 +64,15 @@ param(
|
||||
[string]$Name,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$Version = "latest"
|
||||
[string]$Version = "latest",
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[ValidateSet("monitor", "protect", "full")]
|
||||
[string]$Level,
|
||||
|
||||
[switch]$Reconfigure,
|
||||
|
||||
[switch]$Yes
|
||||
)
|
||||
|
||||
# Configuration
|
||||
@@ -172,11 +191,61 @@ function New-ConfigDirectory {
|
||||
}
|
||||
}
|
||||
|
||||
# Level recorded in an existing config ("" when none; no key = legacy = full).
|
||||
function Get-InstalledLevel {
|
||||
$cfg = Join-Path $ConfigDir "config.yaml"
|
||||
if (-not (Test-Path $cfg)) { return "" }
|
||||
$m = Select-String -Path $cfg -Pattern '^\s+level:\s*"?(\w+)"?' | Select-Object -First 1
|
||||
if ($m) { return $m.Matches[0].Groups[1].Value }
|
||||
return "full"
|
||||
}
|
||||
|
||||
# Refuses to silently change an installed agent's level; returns the level to use.
|
||||
function Resolve-Level {
|
||||
param([string]$Level, [bool]$Reconfigure, [bool]$Yes)
|
||||
|
||||
$current = Get-InstalledLevel
|
||||
if ($current) {
|
||||
if (-not $Level) { return $current }
|
||||
if ($Level -eq $current) { return $Level }
|
||||
if (-not $Reconfigure) {
|
||||
Write-ColorOutput "This agent is already installed at level '$current'. Re-run with -Reconfigure to change it to '$Level'." "Error"
|
||||
exit 1
|
||||
}
|
||||
if (-not $Yes) {
|
||||
$answer = Read-Host "Changing level $current -> $Level. Type '$Level' to confirm"
|
||||
if ($answer -ne $Level) {
|
||||
Write-ColorOutput "Confirmation did not match; permission level unchanged (use -Yes for scripted use)" "Error"
|
||||
exit 1
|
||||
}
|
||||
}
|
||||
$script:ChangeLevelFrom = $current
|
||||
return $Level
|
||||
}
|
||||
if ($Level) { return $Level }
|
||||
if ([Environment]::UserInteractive -and -not [Console]::IsInputRedirected) {
|
||||
Write-Host ""
|
||||
Write-Host " Choose what this agent may do on this server:"
|
||||
Write-Host " 1) monitor report-only, accepts no commands from the panel"
|
||||
Write-Host " 2) protect security actions only (firewall, fail2ban, CrowdSec, origin lock)"
|
||||
Write-Host " 3) full everything"
|
||||
switch (Read-Host " Selection [1-3]") {
|
||||
"1" { return "monitor" }
|
||||
"2" { return "protect" }
|
||||
"3" { return "full" }
|
||||
default { Write-ColorOutput "Invalid selection" "Error"; exit 1 }
|
||||
}
|
||||
}
|
||||
Write-ColorOutput "No -Level given and no console: defaulting to FULL control. Pass -Level monitor|protect|full to choose." "Warning"
|
||||
return "full"
|
||||
}
|
||||
|
||||
function Register-Agent {
|
||||
param(
|
||||
[string]$Token,
|
||||
[string]$Server,
|
||||
[string]$Name
|
||||
[string]$Name,
|
||||
[string]$Level
|
||||
)
|
||||
|
||||
Write-ColorOutput "Registering agent with ServerKit..." "Info"
|
||||
@@ -188,6 +257,11 @@ function Register-Agent {
|
||||
if ($Name) {
|
||||
$arguments += @("--name", $Name)
|
||||
}
|
||||
$arguments += @("--level", $Level)
|
||||
|
||||
if ($script:ChangeLevelFrom) {
|
||||
Start-Process -FilePath $agentPath -ArgumentList @("--config", $configPath, "permissions", "set", $Level, "--yes") -Wait -NoNewWindow | Out-Null
|
||||
}
|
||||
|
||||
try {
|
||||
$process = Start-Process -FilePath $agentPath -ArgumentList $arguments -Wait -PassThru -NoNewWindow
|
||||
@@ -307,7 +381,15 @@ function Install-ServerKitAgent {
|
||||
[string]$Name,
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[string]$Version = "latest"
|
||||
[string]$Version = "latest",
|
||||
|
||||
[Parameter(Mandatory=$false)]
|
||||
[ValidateSet("monitor", "protect", "full")]
|
||||
[string]$Level,
|
||||
|
||||
[switch]$Reconfigure,
|
||||
|
||||
[switch]$Yes
|
||||
)
|
||||
|
||||
# Update script-level variables
|
||||
@@ -321,19 +403,30 @@ function Install-ServerKitAgent {
|
||||
exit 1
|
||||
}
|
||||
|
||||
$Level = Resolve-Level -Level $Level -Reconfigure $Reconfigure.IsPresent -Yes $Yes.IsPresent
|
||||
|
||||
Get-LatestVersion
|
||||
Install-Agent
|
||||
New-ConfigDirectory
|
||||
Register-Agent -Token $Token -Server $Server -Name $Name
|
||||
Register-Agent -Token $Token -Server $Server -Name $Name -Level $Level
|
||||
Install-WindowsService
|
||||
Start-AgentService
|
||||
Add-ToPath
|
||||
Write-SuccessMessage
|
||||
Write-Host "Permission level: $($Level.ToUpper())" -ForegroundColor Yellow
|
||||
switch ($Level) {
|
||||
"monitor" { Write-Host " Report-only: this agent sends metrics out and accepts NO commands." }
|
||||
"protect" { Write-Host " Security actions only (firewall, fail2ban, CrowdSec, origin lock)." }
|
||||
default { Write-Host " Full control: the panel can run any supported action." }
|
||||
}
|
||||
Write-Host " Change it (elevated prompt, this machine only):"
|
||||
Write-Host " & '$InstallDir\$AgentBinary' permissions set <monitor|protect|full>"
|
||||
Write-Host ""
|
||||
}
|
||||
|
||||
# Direct execution with parameters
|
||||
if ($Token -and $Server) {
|
||||
Install-ServerKitAgent -Token $Token -Server $Server -Name $Name -Version $Version
|
||||
Install-ServerKitAgent -Token $Token -Server $Server -Name $Name -Version $Version -Level $Level -Reconfigure:$Reconfigure -Yes:$Yes
|
||||
}
|
||||
elseif ($PSBoundParameters.Count -gt 0) {
|
||||
Write-ColorOutput "Both -Token and -Server parameters are required" "Error"
|
||||
|
||||
Regular → Executable
+112
@@ -30,8 +30,16 @@
|
||||
# --server, -s ServerKit server URL (required)
|
||||
# --name, -n Display name for this server (optional)
|
||||
# --version, -v Specific agent version to install (optional, defaults to latest)
|
||||
# --level, -l Permission level: monitor | protect | full (see below)
|
||||
# --reconfigure Allow changing the level of an already-installed agent
|
||||
# --yes, -y Skip the typed confirmation when reconfiguring
|
||||
# --help, -h Show this help message
|
||||
#
|
||||
# Permission levels: monitor = report-only (no inbound commands), protect =
|
||||
# security actions only (firewall, fail2ban, CrowdSec, Cloudflare origin lock),
|
||||
# full = everything. Omitted: a menu on a terminal; `full` (with a warning)
|
||||
# when non-interactive, for compatibility with older install commands.
|
||||
#
|
||||
|
||||
set -e
|
||||
|
||||
@@ -67,6 +75,9 @@ TOKEN=""
|
||||
SERVER_URL=""
|
||||
SERVER_NAME=""
|
||||
VERSION="latest"
|
||||
LEVEL=""
|
||||
RECONFIGURE=false
|
||||
ASSUME_YES=false
|
||||
|
||||
# Version injected by the panel when it serves this script (the panel already
|
||||
# resolves the latest agent release for its update endpoints). Empty means
|
||||
@@ -108,6 +119,9 @@ show_help() {
|
||||
echo " --server, -s ServerKit server URL (required)"
|
||||
echo " --name, -n Display name for this server (optional)"
|
||||
echo " --version, -v Specific agent version (optional, defaults to latest)"
|
||||
echo " --level, -l Permission level: monitor | protect | full (optional)"
|
||||
echo " --reconfigure Allow changing the level of an already-installed agent"
|
||||
echo " --yes, -y Skip the typed confirmation when reconfiguring"
|
||||
echo " --help, -h Show this help message"
|
||||
echo ""
|
||||
echo "Example:"
|
||||
@@ -142,6 +156,19 @@ parse_args() {
|
||||
VERSION="$2"
|
||||
shift 2
|
||||
;;
|
||||
--level|-l)
|
||||
[[ $# -ge 2 ]] || log_error "Option $1 requires a value"
|
||||
LEVEL="$(printf '%s' "$2" | tr '[:upper:]' '[:lower:]')"
|
||||
shift 2
|
||||
;;
|
||||
--reconfigure)
|
||||
RECONFIGURE=true
|
||||
shift
|
||||
;;
|
||||
--yes|-y)
|
||||
ASSUME_YES=true
|
||||
shift
|
||||
;;
|
||||
--help|-h)
|
||||
show_help
|
||||
;;
|
||||
@@ -159,6 +186,72 @@ parse_args() {
|
||||
if [[ -z "$SERVER_URL" ]]; then
|
||||
log_error "Server URL is required. Use --server or -s"
|
||||
fi
|
||||
|
||||
if [[ -n "$LEVEL" ]]; then
|
||||
case "$LEVEL" in
|
||||
monitor|protect|full) ;;
|
||||
*) log_error "Unknown permission level '$LEVEL' (use monitor, protect or full)" ;;
|
||||
esac
|
||||
fi
|
||||
}
|
||||
|
||||
# Level recorded in an existing config ("" when none; a config without the
|
||||
# key is a legacy install, which means full).
|
||||
installed_level() {
|
||||
local cfg="${CONFIG_DIR}/config.yaml"
|
||||
[[ -f "$cfg" ]] || return 0
|
||||
local lvl
|
||||
lvl="$(sed -n '/^permissions:/,/^[^ ]/ s/^[[:space:]]*level:[[:space:]]*//p' "$cfg" | head -n1 | tr -d "\"' \r")"
|
||||
printf '%s' "${lvl:-full}"
|
||||
}
|
||||
|
||||
is_tty() { [[ -t 0 && -t 1 ]]; }
|
||||
|
||||
# Refuse to silently change the level of an installed agent; --reconfigure
|
||||
# plus a typed confirmation (or --yes) is required.
|
||||
check_reconfigure() {
|
||||
local current
|
||||
current="$(installed_level)"
|
||||
[[ -n "$current" ]] || return 0
|
||||
if [[ -z "$LEVEL" ]]; then
|
||||
LEVEL="$current"
|
||||
return 0
|
||||
fi
|
||||
[[ "$LEVEL" != "$current" ]] || return 0
|
||||
if [[ "$RECONFIGURE" != true ]]; then
|
||||
log_error "This agent is already installed at level '$current'. Re-run with --reconfigure to change it to '$LEVEL'."
|
||||
fi
|
||||
if [[ "$ASSUME_YES" != true ]]; then
|
||||
local answer=""
|
||||
if is_tty; then
|
||||
read -r -p " Changing level ${current} -> ${LEVEL}. Type '${LEVEL}' to confirm: " answer
|
||||
fi
|
||||
[[ "$answer" == "$LEVEL" ]] || log_error "Confirmation did not match; permission level unchanged (use --yes for scripted use)"
|
||||
fi
|
||||
CHANGE_LEVEL_FROM="$current"
|
||||
}
|
||||
|
||||
# Pick a level when none was given: menu on a terminal, else full + warning.
|
||||
resolve_level() {
|
||||
[[ -z "$LEVEL" ]] || return 0
|
||||
if is_tty; then
|
||||
echo ""
|
||||
echo " Choose what this agent may do on this server:"
|
||||
echo " 1) monitor report-only, accepts no commands from the panel"
|
||||
echo " 2) protect security actions only (firewall, fail2ban, CrowdSec, origin lock)"
|
||||
echo " 3) full everything (docker, files, terminal, packages, ...)"
|
||||
local choice=""
|
||||
read -r -p " Selection [1-3]: " choice
|
||||
case "$choice" in
|
||||
1|monitor) LEVEL=monitor ;;
|
||||
2|protect) LEVEL=protect ;;
|
||||
3|full) LEVEL=full ;;
|
||||
*) log_error "Invalid selection" ;;
|
||||
esac
|
||||
else
|
||||
log_warn "No --level given and no terminal: defaulting to FULL control. Pass --level monitor|protect|full to choose."
|
||||
LEVEL=full
|
||||
fi
|
||||
}
|
||||
|
||||
check_root() {
|
||||
@@ -351,6 +444,14 @@ register_agent() {
|
||||
if [[ -n "$SERVER_NAME" ]]; then
|
||||
REGISTER_CMD+=(--name "${SERVER_NAME}")
|
||||
fi
|
||||
REGISTER_CMD+=(--level "${LEVEL}")
|
||||
|
||||
# Re-installing at a different level: apply it locally first so the
|
||||
# register step sees a consistent config.
|
||||
if [[ -n "${CHANGE_LEVEL_FROM:-}" ]]; then
|
||||
"${INSTALL_DIR}/${AGENT_BINARY}" --config "${CONFIG_DIR}/config.yaml" \
|
||||
permissions set "${LEVEL}" --yes >/dev/null 2>&1 || true
|
||||
fi
|
||||
|
||||
if ! "${REGISTER_CMD[@]}"; then
|
||||
log_error "Agent registration failed"
|
||||
@@ -448,6 +549,15 @@ print_success() {
|
||||
echo -e "${GREEN}║ Installation completed successfully! ║${NC}"
|
||||
echo -e "${GREEN}╚═══════════════════════════════════════════════════════════╝${NC}"
|
||||
echo ""
|
||||
echo -e "${YELLOW}Permission level: ${LEVEL^^}${NC}"
|
||||
case "$LEVEL" in
|
||||
monitor) echo " Report-only: this agent sends metrics out and accepts NO commands." ;;
|
||||
protect) echo " Security actions only (firewall, fail2ban, CrowdSec, origin lock)." ;;
|
||||
*) echo " Full control: the panel can run any supported action." ;;
|
||||
esac
|
||||
echo " Change it (as root, on this machine only):"
|
||||
echo " ${INSTALL_DIR}/${AGENT_BINARY} permissions set <monitor|protect|full>"
|
||||
echo ""
|
||||
echo "Agent Status:"
|
||||
echo " Binary: ${INSTALL_DIR}/${AGENT_BINARY}"
|
||||
echo " Config: ${CONFIG_DIR}/config.yaml"
|
||||
@@ -466,6 +576,8 @@ main() {
|
||||
print_banner
|
||||
parse_args "$@"
|
||||
check_root
|
||||
check_reconfigure
|
||||
resolve_level
|
||||
detect_platform
|
||||
check_dependencies
|
||||
get_latest_version
|
||||
|
||||
Reference in new issue
Block a user