Skip to content

ServerKit is a lightweight, modern server control panel for managing web applications, databases, and services on your VPS or dedicated server. Built with Python/Flask backend and React frontend.

feat(connect): publish inventory, diagnostics and bounded logs to Cloud [skip version]

Juan Denis3f601482146a
diff --git a/backend/app/services/connect_client.py b/backend/app/services/connect_client.py
index 412e3043..f6316957 100644
--- a/backend/app/services/connect_client.py
+++ b/backend/app/services/connect_client.py
@@ -37,7 +37,9 @@ import requests
from app import paths
from app.services import connect_keys
-from app.services import connect_commands, connect_policy, connect_storage
+from app.services import (
+ connect_commands, connect_inventory, connect_logs, connect_policy, connect_storage,
+)
from app.services.connect_metrics import MetricsPublisher
from app.services.connect_updates import UpdateCheck, check_and_apply, fetch_jwks
@@ -61,6 +63,15 @@ STORAGE_INTERVAL_S = 300.0
# hours is the plan's own number and the abuse control that goes with it;
# "Check now" is the policy.report command, not a shorter interval.
POLICY_INTERVAL_S = 6 * 3600.0
+# The inventory: on connect, then every five minutes, which
+# is the interval Cloud's own answer asks for. Diagnostics is a live reading
+# and goes at most once a minute — and only while Cloud says `collect: true`,
+# because process detail is a separate consent held there, not here. A
+# refusal backs off to a slow retry so a consent granted later takes effect
+# without a restart.
+INVENTORY_INTERVAL_S = 300.0
+DIAGNOSTICS_INTERVAL_S = 60.0
+DIAGNOSTICS_REFUSED_RETRY_S = 15 * 60.0
def _peek_command_id(token) -> str:
@@ -681,6 +692,22 @@ class RelayClient:
self._storage_next_at = 0.0
self._policy_stream = 0
self._policy_next_at = 0.0
+ # The inventory and diagnostics streams. The
+ # generation is this process's own counter; it starts over on a
+ # restart and Cloud names that rather than ignoring it.
+ self._inventory_stream = 0
+ self._inventory_generation = 0
+ self._inventory_next_at = 0.0
+ self._inventory_inflight = {}
+ self._diagnostics_stream = 0
+ self._diagnostics_next_at = 0.0
+ self._diagnostics_inflight = {}
+ # Bounded log collection. The publisher owns the
+ # buffer and the bounds; Cloud's answer to each batch owns whether
+ # to keep going and which sources are wanted.
+ self._logs = connect_logs.LogPublisher(app)
+ self._logs_stream = 0
+ self._logs_inflight = {}
# -- lifecycle -----------------------------------------------------
@@ -808,6 +835,10 @@ class RelayClient:
self._load_jwks(cfg)
self._publish_storage(ws)
self._publish_policy(ws)
+ # A fresh connection publishes the inventory straight away: the
+ # first thing Cloud shows for a server is what runs on it.
+ self._inventory_next_at = 0.0
+ self._publish_inventory(ws)
while self.running:
try:
raw = ws.recv(timeout=PING_INTERVAL_S)
@@ -816,11 +847,17 @@ class RelayClient:
self._publish_metrics(ws)
self._publish_storage(ws)
self._publish_policy(ws)
+ self._publish_inventory(ws)
+ self._publish_diagnostics(ws)
+ self._publish_logs(ws)
continue
self._handle_frame(ws, raw)
self._publish_metrics(ws)
self._publish_storage(ws)
self._publish_policy(ws)
+ self._publish_inventory(ws)
+ self._publish_diagnostics(ws)
+ self._publish_logs(ws)
return 'stopped'
except Exception as exc:
from websockets.exceptions import ConnectionClosed
@@ -938,6 +975,20 @@ class RelayClient:
self._metrics.apply_ack(payload)
else:
self._metrics.requeue(self._metrics_inflight.pop(stream_id, []))
+ elif stream_id in self._inventory_inflight:
+ self._inventory_inflight.pop(stream_id, None)
+ self._apply_inventory_ack(frame.get('p') or {})
+ elif stream_id in self._diagnostics_inflight:
+ self._diagnostics_inflight.pop(stream_id, None)
+ self._apply_diagnostics_ack(frame.get('p') or {})
+ elif stream_id in self._logs_inflight:
+ batch = self._logs_inflight.pop(stream_id, None)
+ payload = frame.get('p') or {}
+ self._logs.apply_ack(payload)
+ if not payload.get('ok') and self._logs.collect:
+ # Refused for a reason that is not "stop": the lines are
+ # still wanted and their ids make the resend safe.
+ self._logs.requeue(batch)
return
if frame.get('t') == 'open':
if frame.get('k') == 'command':
@@ -1051,6 +1102,93 @@ class RelayClient:
self._policy_stream += 1
self._send(ws, connect_policy.facts_frame(f'pol{self._policy_stream}', facts))
+ # -- the inventory and diagnostics streams ----------
+
+ def publish_inventory(self) -> bool:
+ """Send one inventory document now. Used after a deploy, a rename
+ or a removal so Cloud does not wait five minutes to learn of it."""
+ ws = self._ws
+ if ws is None:
+ return False
+ self._inventory_next_at = 0.0
+ return self._publish_inventory(ws)
+
+ def _publish_inventory(self, ws) -> bool:
+ now = time.monotonic()
+ if now < self._inventory_next_at:
+ return False
+ self._inventory_next_at = now + INVENTORY_INTERVAL_S
+ self._inventory_generation += 1
+ try:
+ doc = connect_inventory.build_inventory(self.app, self._inventory_generation)
+ except Exception:
+ logger.debug('Connect inventory: could not build the document', exc_info=True)
+ return False
+ self._inventory_stream += 1
+ stream_id = f'inv{self._inventory_stream}'
+ self._inventory_inflight[stream_id] = doc.get('generation')
+ return self._send(ws, connect_inventory.inventory_frame(stream_id, doc))
+
+ def _apply_inventory_ack(self, payload: dict):
+ """Cloud's answer carries the interval it wants; a refusal is logged
+ once and the next attempt waits the normal interval."""
+ if not payload.get('ok'):
+ logger.info('Connect inventory: refused (%s)', payload.get('reason') or 'unknown')
+ return
+ interval = payload.get('next_interval_s')
+ if isinstance(interval, (int, float)) and 30 <= interval <= 3600:
+ self._inventory_next_at = time.monotonic() + float(interval)
+ if payload.get('generation_reset'):
+ logger.info('Connect inventory: Cloud noted a generation reset after restart')
+
+ def _publish_diagnostics(self, ws) -> bool:
+ now = time.monotonic()
+ if now < self._diagnostics_next_at:
+ return False
+ self._diagnostics_next_at = now + DIAGNOSTICS_INTERVAL_S
+ try:
+ doc = connect_inventory.build_diagnostics(int(DIAGNOSTICS_INTERVAL_S))
+ except Exception:
+ logger.debug('Connect diagnostics: could not build the document', exc_info=True)
+ return False
+ self._diagnostics_stream += 1
+ stream_id = f'dia{self._diagnostics_stream}'
+ self._diagnostics_inflight[stream_id] = True
+ return self._send(ws, connect_inventory.inventory_frame(stream_id, doc))
+
+ def _apply_diagnostics_ack(self, payload: dict):
+ """`collect: false` is Cloud saying the consent is not held. Stop, and
+ try again slowly so a consent granted later takes effect."""
+ if payload.get('ok') and payload.get('collect', True):
+ interval = payload.get('next_interval_s')
+ if isinstance(interval, (int, float)) and 30 <= interval <= 3600:
+ self._diagnostics_next_at = time.monotonic() + float(interval)
+ return
+ self._diagnostics_next_at = time.monotonic() + DIAGNOSTICS_REFUSED_RETRY_S
+
+ # -- the logs stream ---------------------------------
+
+ def _publish_logs(self, ws) -> bool:
+ """Collect from the wanted sources and send one bounded batch when
+ one is due. Before Cloud has answered once, the first batch is
+ empty and its answer is the configuration."""
+ self._logs.retry_after_refusal()
+ try:
+ self._logs.collect_now()
+ except Exception:
+ logger.debug('Connect logs: collection failed', exc_info=True)
+ if not self._logs.due():
+ return False
+ batch = self._logs.take_batch()
+ self._logs_stream += 1
+ stream_id = f'log{self._logs_stream}'
+ self._logs_inflight[stream_id] = batch
+ sent = self._send(ws, connect_logs.logs_frame(stream_id, batch))
+ if not sent:
+ self._logs_inflight.pop(stream_id, None)
+ self._logs.requeue(batch)
+ return sent
+
def _publish_storage(self, ws):
"""Where backups go and how they went, on its own slow cadence: this
changes when a backup runs, not every minute."""
diff --git a/backend/app/services/connect_inventory.py b/backend/app/services/connect_inventory.py
new file mode 100644
index 00000000..7aaff429
--- /dev/null
+++ b/backend/app/services/connect_inventory.py
@@ -0,0 +1,534 @@
+"""The panel half of the Cloud inventory.
+
+Two documents, both read from things the panel already knows, both sent up
+the existing `facts` stream, neither of them deciding anything:
+
+**`inventory/1`** — the host, the applications, their domains and their
+deployment history, and the managed databases. Cloud keys every application
+on `key`, which is this panel's own row id: a name is a label and a rename
+must not create a second application in Cloud.
+
+**`diagnostics/1`** — the busiest processes and the mounted filesystems.
+That is a separate disclosure with its own consent on the Cloud side, and
+Cloud says so in its answer: `collect: false` means stop asking, and this
+module's caller backs off until the answer changes.
+
+Neither builder raises. A collector that fails leaves its section out of
+`capabilities`, names the reason in `sections`, and the other sections are
+still sent. What this never sends, because Cloud drops it on arrival and a
+reader should not wonder where it went: environment values, command lines,
+connection strings, credentials, repository URLs (a slug, a branch and a
+commit instead — a URL can carry a token), and arbitrary filesystem paths.
+"""
+import logging
+import platform
+from datetime import datetime, timezone
+
+logger = logging.getLogger(__name__)
+
+INVENTORY_SCHEMA = 'inventory/1'
+DIAGNOSTICS_SCHEMA = 'diagnostics/1'
+
+# Cloud's own bounds (backend/inventory.py there). Kept here so a valid but
+# very large inventory is capped with `complete: false` rather than sent
+# whole and refused whole.
+MAX_APPLICATIONS = 500
+MAX_DATABASES = 500
+MAX_DEPLOYMENTS = 500
+MAX_DEPLOYMENTS_PER_APP = 20
+MAX_DOMAINS_PER_APP = 32
+MAX_PORTS_PER_APP = 32
+MAX_PROCESSES = 20
+MAX_MOUNTS = 64
+
+# The panel's application states, in Cloud's vocabulary. Anything else is
+# passed through as reported rather than guessed at.
+_APP_STATE = {
+ 'running': 'running',
+ 'stopped': 'stopped',
+ 'error': 'failed',
+ 'deploying': 'restarting',
+}
+
+# Deployment statuses likewise.
+_DEPLOY_STATUS = {
+ 'live': 'succeeded',
+ 'failed': 'failed',
+ 'rolled_back': 'cancelled',
+ 'pending': 'queued',
+ 'building': 'building',
+ 'deploying': 'running',
+}
+
+
+def _utc(dt):
+ if dt is None:
+ return None
+ dt = dt.replace(tzinfo=timezone.utc) if dt.tzinfo is None else dt
+ return dt.isoformat()
+
+
+def _now():
+ return datetime.now(timezone.utc).isoformat()
+
+
+def _in_app(app, fn):
+ """Run `fn` inside the Flask app context when one was given."""
+ if app is None:
+ return fn()
+ with app.app_context():
+ return fn()
+
+
+# ==================== inventory ====================
+
+
+def build_inventory(app=None, generation: int = 0) -> dict:
+ """One inventory document. Never raises.
+
+ `generation` is the caller's counter — it increments per publish for
+ the life of this process, and Cloud treats a value that goes backwards
+ (a panel restart) as a reset it names rather than ignores.
+ """
+ doc = {
+ 'schema': INVENTORY_SCHEMA,
+ 'generation': int(generation or 0),
+ 'observed_at': _now(),
+ 'capabilities': [],
+ 'sections': {},
+ }
+
+ host = _host()
+ if host:
+ doc['host'] = host
+ doc['capabilities'].append('host')
+
+ for name, collect in (('applications', lambda: _in_app(app, _applications)),
+ ('databases', lambda: _in_app(app, _databases)),
+ ('deployments', lambda: _in_app(app, _deployments))):
+ try:
+ rows, complete = collect()
+ except Exception as exc: # noqa: BLE001 - a collector must not take the document down
+ logger.debug('Connect inventory: could not list %s', name, exc_info=True)
+ doc['sections'][name] = {'complete': False, 'error': _reason(exc)}
+ continue
+ doc[name] = rows
+ doc['sections'][name] = {'complete': bool(complete)}
+ doc['capabilities'].append(name)
+
+ return doc
+
+
+def _reason(exc) -> str:
+ text = f'{type(exc).__name__}: {exc}'.strip()
+ return text[:200]
+
+
+def _host() -> dict | None:
+ """OS, kernel, CPU, memory, Docker and boot time. Provider and region are
+ not guessed from a hostname; they are omitted until something reports
+ them."""
+ out = {}
+ try:
+ out['os'] = platform.system() or None
+ out['os_version'] = _os_version()
+ out['kernel'] = platform.release() or None
+ out['arch'] = platform.machine() or None
+ except Exception: # noqa: BLE001
+ logger.debug('Connect inventory: platform probe failed', exc_info=True)
+ try:
+ import psutil
+ out['cpu_count'] = psutil.cpu_count(logical=True) or None
+ out['memory_bytes'] = int(psutil.virtual_memory().total)
+ out['boot_at'] = datetime.fromtimestamp(psutil.boot_time(), tz=timezone.utc).isoformat()
+ except Exception: # noqa: BLE001
+ logger.debug('Connect inventory: psutil probe failed', exc_info=True)
+ try:
+ from app.services.system_service import SystemService
+ info = SystemService.get_system_info() or {}
+ model = (info.get('cpu') or {}).get('model')
+ if model and model != 'Unknown':
+ out['cpu_model'] = model
+ except Exception: # noqa: BLE001
+ logger.debug('Connect inventory: cpu model probe failed', exc_info=True)
+ try:
+ from app.utils.version import get_panel_version
+ out['panel_version'] = get_panel_version()
+ except Exception: # noqa: BLE001
+ pass
+ docker = _docker_version()
+ if docker:
+ out['docker_version'] = docker
+ out = {k: v for k, v in out.items() if v not in (None, '')}
+ return out or None
+
+
+def _os_version() -> str | None:
+ try:
+ import os
+ release = {}
+ with open('/etc/os-release', encoding='utf-8') as fh: # noqa: PTH123
+ for line in fh:
+ if '=' in line:
+ key, _, value = line.strip().partition('=')
+ release[key] = value.strip('"')
+ return release.get('PRETTY_NAME') or release.get('VERSION_ID') or None
+ except Exception: # noqa: BLE001
+ return platform.version()[:60] or None
+
+
+def _docker_version() -> str | None:
+ try:
+ from app.services.docker_service import DockerService
+ version = DockerService.get_version() if hasattr(DockerService, 'get_version') else None
+ if isinstance(version, dict):
+ return version.get('Version') or version.get('version')
+ return version or None
+ except Exception: # noqa: BLE001
+ return None
+
+
+def _applications():
+ from app.models.application import Application
+
+ rows = (Application.query
+ .order_by(Application.id.asc())
+ .limit(MAX_APPLICATIONS + 1)
+ .all())
+ complete = len(rows) <= MAX_APPLICATIONS
+ out = []
+ for row in rows[:MAX_APPLICATIONS]:
+ out.append(_application(row))
+ return out, complete
+
+
+def _application(row) -> dict:
+ """One application as Cloud's contract wants it. The key is the row id."""
+ item = {
+ 'key': str(row.id),
+ 'name': row.name,
+ 'runtime': _runtime(row),
+ 'state': _APP_STATE.get(str(row.status or '').lower(), row.status),
+ }
+ source = _source(row)
+ if source:
+ item['source'] = source
+ domains = _app_domains(row)
+ if domains:
+ item['domains'] = domains
+ ports = _app_ports(row)
+ if ports:
+ item['ports'] = ports
+ admin = _admin_url(row, domains)
+ if admin:
+ item['admin_url'] = admin
+ current = _current_deployment(row)
+ if current:
+ item['deployment'] = current
+ return item
+
+
+def _runtime(row) -> str:
+ managed = str(getattr(row, 'managed_by', '') or '').lower()
+ app_type = str(getattr(row, 'app_type', '') or '').lower()
+ if managed == 'docker_compose' or getattr(row, 'compose_file', None):
+ return 'compose'
+ if managed == 'systemd' or getattr(row, 'systemd_unit', None):
+ return 'systemd'
+ if app_type == 'docker' or getattr(row, 'docker_image', None):
+ return 'docker'
+ if app_type == 'static':
+ return 'static'
+ return 'native'
+
+
+def _source(row) -> dict | None:
+ """Slug, branch and commit only. A repository URL is reduced to its
+ `owner/name` path; the host and any token in it never leave."""
+ out = {}
+ try:
+ from app.models.webhook import GitWebhook
+ hook = (GitWebhook.query.filter_by(app_id=row.id)
+ .order_by(GitWebhook.id.desc()).first())
+ except Exception: # noqa: BLE001
+ hook = None
+ if hook is not None:
+ slug = _repo_slug(getattr(hook, 'source_repo_url', None))
+ if slug:
+ out['slug'] = slug
+ if getattr(hook, 'source_branch', None):
+ out['branch'] = hook.source_branch
+ latest = _latest_deployment_row(row)
+ if latest is not None and getattr(latest, 'commit_hash', None):
+ out['commit'] = latest.commit_hash
+ return out or None
+
+
+def _repo_slug(url) -> str | None:
+ if not url:
+ return None
+ text = str(url).strip()
+ # git@host:owner/name.git or https://host/owner/name(.git)
+ if ':' in text and '://' not in text:
+ text = text.split(':', 1)[1]
+ else:
+ text = text.split('://', 1)[-1]
+ text = text.split('/', 1)[1] if '/' in text else ''
+ text = text.rstrip('/')
+ if text.endswith('.git'):
+ text = text[:-4]
+ parts = [p for p in text.split('/') if p]
+ return '/'.join(parts[-2:]) if len(parts) >= 2 else None
+
+
+def _app_domains(row) -> list:
+ out = []
+ try:
+ domains = row.live_domains
+ except Exception: # noqa: BLE001
+ domains = []
+ for d in list(domains)[:MAX_DOMAINS_PER_APP]:
+ entry = {'host': d.name}
+ if getattr(d, 'ssl_enabled', False) and getattr(d, 'ssl_expires_at', None):
+ entry['tls_expires_at'] = _utc(d.ssl_expires_at)
+ entry['state'] = 'active' if getattr(d, 'ssl_enabled', False) else 'no-tls'
+ out.append(entry)
+ return out
+
+
+def _app_ports(row) -> list:
+ ports = []
+ if getattr(row, 'port', None):
+ ports.append(int(row.port))
+ raw = getattr(row, 'ports', None)
+ if raw:
+ try:
+ import json
+ declared = json.loads(raw) if isinstance(raw, str) else raw
+ for item in declared or []:
+ value = item.get('host') if isinstance(item, dict) else item
+ if isinstance(value, int) and value not in ports:
+ ports.append(value)
+ except Exception: # noqa: BLE001
+ pass
+ return ports[:MAX_PORTS_PER_APP]
+
+
+def _admin_url(row, domains) -> str | None:
+ """Only where this panel itself knows the application's admin page.
+
+ WordPress is the one type the panel installs and configures, so its
+ dashboard path is a fact about what the panel set up rather than a guess
+ about what might be there. Every other type reports nothing.
+ """
+ if str(getattr(row, 'app_type', '') or '').lower() != 'wordpress':
+ return None
+ primary = None
+ for d in domains or []:
+ primary = d['host']
+ break
+ if not primary:
+ return None
+ scheme = 'https' if any(d.get('tls_expires_at') for d in domains) else 'http'
+ return f'{scheme}://{primary}/wp-admin/'
+
+
+def _latest_deployment_row(row):
+ try:
+ from app.models.deployment import Deployment
+ return (Deployment.query.filter_by(app_id=row.id)
+ .order_by(Deployment.version.desc()).first())
+ except Exception: # noqa: BLE001
+ return None
+
+
+def _current_deployment(row) -> dict | None:
+ latest = _latest_deployment_row(row)
+ if latest is None:
+ return None
+ return {
+ 'key': f'{row.id}:{latest.version}',
+ 'status': _DEPLOY_STATUS.get(str(latest.status or '').lower(), latest.status),
+ 'at': _utc(latest.deploy_completed_at or latest.build_started_at or latest.created_at),
+ }
+
+
+def _deployments():
+ """Recent deployments per application, newest first, bounded per app and
+ overall. Stage times come from the panel's own build/deploy timestamps."""
+ from app.models.application import Application
+ from app.models.deployment import Deployment
+
+ out = []
+ complete = True
+ app_ids = [r.id for r in Application.query.with_entities(Application.id)
+ .order_by(Application.id.asc()).limit(MAX_APPLICATIONS).all()]
+ for app_id in app_ids:
+ rows = (Deployment.query.filter_by(app_id=app_id)
+ .order_by(Deployment.version.desc())
+ .limit(MAX_DEPLOYMENTS_PER_APP + 1).all())
+ if len(rows) > MAX_DEPLOYMENTS_PER_APP:
+ complete = False
+ for d in rows[:MAX_DEPLOYMENTS_PER_APP]:
+ out.append(_deployment(app_id, d))
+ if len(out) >= MAX_DEPLOYMENTS:
+ return out, False
+ return out, complete
+
+
+def _deployment(app_id, d) -> dict:
+ started = d.build_started_at or d.created_at
+ finished = d.deploy_completed_at
+ item = {
+ 'key': f'{app_id}:{d.version}',
+ 'app_key': str(app_id),
+ 'status': _DEPLOY_STATUS.get(str(d.status or '').lower(), d.status),
+ 'started_at': _utc(started),
+ 'finished_at': _utc(finished),
+ }
+ if d.duration is not None:
+ item['duration_s'] = float(d.duration)
+ artifact = {}
+ if getattr(d, 'image_tag', None):
+ artifact['image'] = d.image_tag
+ if getattr(d, 'commit_hash', None):
+ artifact['commit'] = d.commit_hash
+ if artifact:
+ item['artifact'] = artifact
+ stages = []
+ if d.build_started_at and d.build_completed_at:
+ stages.append({'name': 'build', 'status': 'succeeded' if d.status != 'failed' or d.deploy_started_at else 'failed',
+ 'duration_s': float(d.build_duration or 0)})
+ if d.deploy_started_at and d.deploy_completed_at:
+ stages.append({'name': 'deploy',
+ 'status': 'failed' if d.status == 'failed' else 'succeeded',
+ 'duration_s': (d.deploy_completed_at - d.deploy_started_at).total_seconds()})
+ if stages:
+ item['stages'] = stages
+ return item
+
+
+def _databases():
+ """Names, engines and states only. The row also holds an admin secret
+ and a host; neither is read here."""
+ from app.models.managed_database import ManagedDatabase
+
+ rows = (ManagedDatabase.query
+ .order_by(ManagedDatabase.id.asc())
+ .limit(MAX_DATABASES + 1).all())
+ complete = len(rows) <= MAX_DATABASES
+ out = []
+ for row in rows[:MAX_DATABASES]:
+ out.append({
+ 'key': str(row.id),
+ 'name': row.name,
+ 'engine': _engine(row.engine),
+ 'state': 'running' if getattr(row, 'host_kind', 'host') == 'host'
+ else _container_state(getattr(row, 'container_ref', None)),
+ })
+ return out, complete
+
+
+def _engine(value) -> str | None:
+ text = str(value or '').lower()
+ return {'postgresql': 'postgres'}.get(text, text) or None
+
+
+def _container_state(ref) -> str | None:
+ if not ref:
+ return None
+ try:
+ from app.services.docker_service import DockerService
+ info = DockerService.get_container(ref) if hasattr(DockerService, 'get_container') else None
+ state = str((info or {}).get('status') or (info or {}).get('state') or '').lower()
+ if 'running' in state or 'up' in state:
+ return 'running'
+ return 'stopped' if state else None
+ except Exception: # noqa: BLE001
+ return None
+
+
+# ==================== diagnostics ====================
+
+
+def build_diagnostics(interval_s: int = 60) -> dict:
+ """The busiest processes and every mount. Executable name, pid, user
+ and resource use — never a command line, never an environment."""
+ doc = {
+ 'schema': DIAGNOSTICS_SCHEMA,
+ 'observed_at': _now(),
+ 'interval_s': int(interval_s),
+ 'completeness': {},
+ }
+ try:
+ processes, total = _processes()
+ doc['processes'] = processes
+ doc['completeness']['processes'] = {'complete': total <= MAX_PROCESSES,
+ 'truncated': total > MAX_PROCESSES,
+ 'total': total}
+ except Exception as exc: # noqa: BLE001
+ doc['processes'] = []
+ doc['completeness']['processes'] = {'complete': False, 'error': _reason(exc)}
+ try:
+ mounts, total = _mounts()
+ doc['mounts'] = mounts
+ doc['completeness']['mounts'] = {'complete': total <= MAX_MOUNTS,
+ 'truncated': total > MAX_MOUNTS, 'total': total}
+ except Exception as exc: # noqa: BLE001
+ doc['mounts'] = []
+ doc['completeness']['mounts'] = {'complete': False, 'error': _reason(exc)}
+ return doc
+
+
+def _processes():
+ import psutil
+ rows = []
+ for proc in psutil.process_iter(['pid', 'name', 'username', 'cpu_percent',
+ 'memory_info', 'create_time']):
+ try:
+ info = proc.info
+ mem = info.get('memory_info')
+ rows.append({
+ 'pid': info.get('pid'),
+ 'name': info.get('name'),
+ 'user': info.get('username'),
+ 'cpu_pct': float(info.get('cpu_percent') or 0.0),
+ 'rss_bytes': int(mem.rss) if mem else None,
+ 'started_at': datetime.fromtimestamp(info['create_time'], tz=timezone.utc).isoformat()
+ if info.get('create_time') else None,
+ })
+ except (psutil.NoSuchProcess, psutil.AccessDenied, psutil.ZombieProcess):
+ continue
+ total = len(rows)
+ rows.sort(key=lambda r: (r['cpu_pct'], r['rss_bytes'] or 0), reverse=True)
+ return rows[:MAX_PROCESSES], total
+
+
+def _mounts():
+ import psutil
+ rows = []
+ for part in psutil.disk_partitions(all=False):
+ try:
+ usage = psutil.disk_usage(part.mountpoint)
+ except (PermissionError, OSError):
+ continue
+ rows.append({
+ 'mount': part.mountpoint,
+ 'device': part.device,
+ 'fstype': part.fstype,
+ 'total_bytes': int(usage.total),
+ 'used_bytes': int(usage.used),
+ 'used_pct': round(float(usage.percent), 2),
+ })
+ total = len(rows)
+ rows.sort(key=lambda r: r['used_pct'], reverse=True)
+ return rows[:MAX_MOUNTS], total
+
+
+# ==================== frames ====================
+
+
+def inventory_frame(stream_id: str, doc: dict) -> dict:
+ """Same `facts` stream kind the older shapeless document used; Cloud keys
+ on `schema` and an older Cloud answers exactly as it always did."""
+ return {'s': stream_id, 't': 'open', 'k': 'facts', 'p': doc}
diff --git a/backend/app/services/connect_logs.py b/backend/app/services/connect_logs.py
new file mode 100644
index 00000000..9d4180be
--- /dev/null
+++ b/backend/app/services/connect_logs.py
@@ -0,0 +1,438 @@
+"""The panel half of bounded log collection.
+
+Cloud keeps a short, searchable copy of log lines from sources the operator
+chose — never every file on the server, never authentication logs — and
+this module is what sends them. Four sources, each mapped to something the
+panel already reads:
+
+| source | what is read |
+|---------------------|-----------------------------------------------------------|
+| `serverkit.service` | the panel's own log file under the ServerKit log directory |
+| `web.error` | the host web server's error log |
+| `app.output` | each managed application's output (journal or compose) |
+| `deploy.output` | the build log kept with each deployment |
+
+The rules Cloud enforces are applied here first, so a line that would be
+refused never crosses the relay: a batch is at most 200 records and 256 KiB,
+a line at most 8 KiB, flushes are at least five seconds apart, and while the
+relay is down at most 10 MiB or five minutes are held. Every record carries a
+stable `id` — a hash of source, generation and offset — so a batch that was
+sent but never acknowledged is safely resent and counted as a duplicate.
+
+Cloud's answer is the configuration: `collect: false` means stop, and which
+`sources` are wanted is Cloud's list, not this file's. Nothing here sends a
+path, and secrets are redacted locally before Cloud redacts them again.
+"""
+import hashlib
+import json
+import logging
+import os
+import re
+import time
+from datetime import datetime, timezone
+
+from app import paths
+
+logger = logging.getLogger(__name__)
+
+LOGS_SCHEMA = 'logs/1'
+SOURCES = ('serverkit.service', 'web.error', 'app.output', 'deploy.output')
+
+MAX_BATCH_RECORDS = 200
+MAX_BATCH_BYTES = 256 * 1024
+MAX_LINE_BYTES = 8 * 1024
+FLUSH_INTERVAL_S = 5.0
+OUTAGE_BUFFER_BYTES = 10 * 1024 * 1024
+OUTAGE_BUFFER_S = 300.0
+# How often to ask Cloud again once it said `collect: false`. A withdrawn
+# consent stops collection at once; one granted later takes effect without a
+# restart, slowly.
+REFUSED_RETRY_S = 15 * 60.0
+# Per read, per source: a file that grew by more than this between reads is
+# read in pieces, never in one go.
+MAX_READ_BYTES = 512 * 1024
+MAX_APP_LINES = 100
+
+WEB_ERROR_LOG = '/var/log/nginx/error.log'
+
+_SEVERITY = re.compile(
+ r'\b(emerg|alert|crit|critical|error|err|warn|warning|notice|info|debug)\b', re.I)
+
+# Redacted locally, so a secret never crosses the relay at all; Cloud runs
+# its own, wider set on arrival.
+_REDACTIONS = (
+ (re.compile(r'(?i)\b(bearer|basic)\s+[A-Za-z0-9._~+/=-]{8,}'), r'\1 [redacted]'),
+ (re.compile(r'(?i)\b(pass(?:word|wd)?|secret|token|api[_-]?key|access[_-]?key|'
+ r'private[_-]?key|authorization|credential)\b(\s*[:=]\s*)(\"[^\"]*\"|\'[^\']*\'|\S+)'),
+ r'\1\2[redacted]'),
+ (re.compile(r'(?i)\b([a-z][a-z0-9+.-]*://)([^/\s:@]+):([^/\s@]+)@'), r'\1\2:[redacted]@'),
+)
+
+
+def redact(text: str) -> str:
+ for pattern, replacement in _REDACTIONS:
+ text = pattern.sub(replacement, text)
+ return text
+
+
+def severity_of(text: str) -> str | None:
+ match = _SEVERITY.search(text[:120])
+ return match.group(1).lower() if match else None
+
+
+def _now_iso() -> str:
+ return datetime.now(timezone.utc).isoformat()
+
+
+def record_id(source: str, generation: int, offset) -> str:
+ """Stable per device: the same line at the same place is the same id."""
+ raw = f'{source}|{generation}|{offset}'.encode('utf-8', 'replace')
+ return hashlib.sha1(raw).hexdigest()[:40]
+
+
+def _bounded_line(text: str) -> tuple[str, bool]:
+ encoded = text.encode('utf-8', 'replace')
+ if len(encoded) <= MAX_LINE_BYTES:
+ return text, False
+ return encoded[:MAX_LINE_BYTES].decode('utf-8', 'ignore'), True
+
+
+# ==================== file tailers ====================
+
+
+class FileTail:
+ """Follow one file from where it was left. Rotation or truncation
+ increments `generation`; the offset restarts at zero. Never reads more
+ than `MAX_READ_BYTES` per call and never follows a symlink elsewhere."""
+
+ def __init__(self, source: str, path: str):
+ self.source = source
+ self.path = path
+ self.generation = 0
+ self.offset = 0
+ self.inode = None
+ self.missing_logged = False
+
+ def read(self) -> list[dict]:
+ try:
+ st = os.stat(self.path)
+ except OSError:
+ if not self.missing_logged:
+ logger.debug('Connect logs: %s has no file to read', self.source)
+ self.missing_logged = True
+ return []
+ self.missing_logged = False
+ inode = getattr(st, 'st_ino', None)
+ if self.inode is None:
+ # First sight: start at the end. History is not what Cloud keeps.
+ self.inode, self.offset = inode, st.st_size
+ return []
+ if inode != self.inode or st.st_size < self.offset:
+ self.generation += 1
+ self.inode, self.offset = inode, 0
+ if st.st_size == self.offset:
+ return []
+ out = []
+ try:
+ with open(self.path, 'rb') as fh:
+ fh.seek(self.offset)
+ chunk = fh.read(MAX_READ_BYTES)
+ except OSError:
+ return []
+ lines = chunk.split(b'\n')
+ # A partial last line waits for its newline.
+ tail = lines.pop() if not chunk.endswith(b'\n') else b''
+ consumed = len(chunk) - len(tail)
+ position = self.offset
+ for raw in lines:
+ text = raw.decode('utf-8', 'replace').rstrip('\r')
+ if text.strip():
+ out.append(self._record(position, text))
+ position += len(raw) + 1
+ self.offset += consumed
+ return out
+
+ def _record(self, offset: int, text: str) -> dict:
+ text, truncated = _bounded_line(redact(text))
+ rec = {
+ 'id': record_id(self.source, self.generation, offset),
+ 'source': self.source,
+ 'generation': self.generation,
+ 'offset': offset,
+ 'observed_at': _now_iso(),
+ 'severity': severity_of(text),
+ 'text': text,
+ }
+ if truncated:
+ rec['truncated'] = True
+ return rec
+
+
+def _panel_log_path() -> str | None:
+ """The panel's own log file, if one is written."""
+ candidates = [os.path.join(paths.SERVERKIT_LOG_DIR, name)
+ for name in ('serverkit.log', 'app.log', 'panel.log')]
+ for path in candidates:
+ if os.path.isfile(path):
+ return path
+ return candidates[0]
+
+
+# ==================== command-backed sources ====================
+
+
+class AppOutput:
+ """Recent output of every managed application, read through the same
+ services the panel's own Logs tab uses. Lines are identified by their
+ content and time rather than an offset, so the id is stable across
+ overlapping reads and Cloud counts the overlap as duplicates."""
+
+ def __init__(self, app=None):
+ self.app = app
+ self.since = time.time()
+
+ def read(self) -> list[dict]:
+ from app.services.log_service import LogService
+ out = []
+ since = datetime.fromtimestamp(self.since, tz=timezone.utc)
+ self.since = time.time()
+ for row in self._applications():
+ lines = []
+ unit = getattr(row, 'systemd_unit', None)
+ managed = str(getattr(row, 'managed_by', '') or '')
+ try:
+ if unit:
+ result = LogService.get_journalctl_logs(
+ unit=unit, lines=MAX_APP_LINES,
+ since=since.strftime('%Y-%m-%d %H:%M:%S'))
+ elif managed == 'docker_compose' or getattr(row, 'app_type', '') == 'docker':
+ app_dir = os.path.join(paths.APPS_DIR, row.name)
+ result = LogService.get_docker_app_logs(row.name, app_dir, MAX_APP_LINES,
+ getattr(row, 'compose_file', None))
+ else:
+ result = LogService.get_app_logs(row.name, 'error', MAX_APP_LINES)
+ if result.get('success'):
+ lines = [l for l in (result.get('lines') or []) if l and l.strip()]
+ except Exception: # noqa: BLE001
+ logger.debug('Connect logs: could not read output for %s', row.name, exc_info=True)
+ continue
+ for text in lines[-MAX_APP_LINES:]:
+ text, truncated = _bounded_line(redact(text))
+ rec = {
+ 'id': record_id('app.output', row.id, hashlib.sha1(text.encode('utf-8', 'replace')).hexdigest()[:16]),
+ 'source': 'app.output',
+ 'app_key': str(row.id),
+ 'observed_at': _now_iso(),
+ 'severity': severity_of(text),
+ 'text': text,
+ }
+ if truncated:
+ rec['truncated'] = True
+ out.append(rec)
+ return out
+
+ def _applications(self):
+ try:
+ from app.models.application import Application
+ if self.app is not None:
+ with self.app.app_context():
+ return list(Application.query.order_by(Application.id.asc()).all())
+ return list(Application.query.order_by(Application.id.asc()).all())
+ except Exception: # noqa: BLE001
+ return []
+
+
+class DeployOutput:
+ """The build log kept with each deployment, sent once per deployment.
+ Only the lines; the file's path stays here."""
+
+ def __init__(self, app=None):
+ self.app = app
+ self.sent: set[int] = set()
+
+ def read(self) -> list[dict]:
+ out = []
+ for dep in self._recent():
+ if dep.id in self.sent:
+ continue
+ path = getattr(dep, 'build_log_path', None)
+ if not path or not os.path.isfile(path):
+ continue
+ try:
+ with open(path, 'r', encoding='utf-8', errors='replace') as fh:
+ data = json.load(fh)
+ lines = data.get('logs') if isinstance(data, dict) else data
+ except (OSError, ValueError):
+ continue
+ self.sent.add(dep.id)
+ for offset, entry in enumerate((lines or [])[:MAX_BATCH_RECORDS]):
+ text = entry.get('message') if isinstance(entry, dict) else str(entry)
+ if not text or not str(text).strip():
+ continue
+ text, truncated = _bounded_line(redact(str(text)))
+ rec = {
+ 'id': record_id('deploy.output', dep.id, offset),
+ 'source': 'deploy.output',
+ 'app_key': str(dep.app_id),
+ 'deployment_key': f'{dep.app_id}:{dep.version}',
+ 'observed_at': _now_iso(),
+ 'severity': severity_of(text),
+ 'text': text,
+ }
+ if truncated:
+ rec['truncated'] = True
+ out.append(rec)
+ return out
+
+ def _recent(self):
+ try:
+ from app.models.deployment import Deployment
+
+ def query():
+ return list(Deployment.query.order_by(Deployment.id.desc()).limit(20).all())
+ if self.app is not None:
+ with self.app.app_context():
+ return query()
+ return query()
+ except Exception: # noqa: BLE001
+ return []
+
+
+# ==================== the publisher ====================
+
+
+class LogPublisher:
+ """Collects from the wanted sources, batches within the bounds, and holds
+ a bounded buffer while the relay is away. `apply_ack` is Cloud's answer."""
+
+ def __init__(self, app=None):
+ self.app = app
+ self.collect = True
+ self.sources: list[str] | None = None # None until Cloud has answered
+ self.flush_interval = FLUSH_INTERVAL_S
+ self.next_flush_at = 0.0
+ self.refused_until = 0.0
+ self.pending: list[dict] = []
+ self.pending_bytes = 0
+ self.oldest_pending_at = None
+ self.generation = 0
+ self._readers = {
+ 'serverkit.service': FileTail('serverkit.service', _panel_log_path()),
+ 'web.error': FileTail('web.error', WEB_ERROR_LOG),
+ 'app.output': AppOutput(app),
+ 'deploy.output': DeployOutput(app),
+ }
+
+ # -- collection ------------------------------------------------------
+
+ def wanted(self) -> list[str]:
+ if self.sources is None:
+ return []
+ return [s for s in self.sources if s in self._readers]
+
+ def collect_now(self) -> int:
+ """Read every wanted source into the pending buffer. Returns how many
+ records were added."""
+ if not self.collect or time.monotonic() < self.refused_until:
+ return 0
+ added = 0
+ for source in self.wanted():
+ try:
+ records = self._readers[source].read()
+ except Exception: # noqa: BLE001
+ logger.debug('Connect logs: reader %s failed', source, exc_info=True)
+ continue
+ for rec in records:
+ size = len(json.dumps(rec).encode('utf-8'))
+ if self.pending_bytes + size > OUTAGE_BUFFER_BYTES:
+ # Full: the newest line is the one dropped, and the
+ # application keeps writing locally regardless.
+ break
+ self.pending.append(rec)
+ self.pending_bytes += size
+ added += 1
+ if self.pending and self.oldest_pending_at is None:
+ self.oldest_pending_at = time.monotonic()
+ self._expire()
+ return added
+
+ def _expire(self):
+ """Lines older than the outage window are dropped rather than held
+ forever: the bound is the promise."""
+ if self.oldest_pending_at is None:
+ return
+ if time.monotonic() - self.oldest_pending_at > OUTAGE_BUFFER_S:
+ self.pending.clear()
+ self.pending_bytes = 0
+ self.oldest_pending_at = None
+
+ # -- batches -----------------------------------------------------------
+
+ def due(self) -> bool:
+ if not self.collect or time.monotonic() < self.refused_until:
+ return False
+ if time.monotonic() < self.next_flush_at:
+ return False
+ # Before Cloud has said which sources it wants, one empty batch asks.
+ return bool(self.pending) or self.sources is None
+
+ def take_batch(self) -> dict:
+ """The next batch within the bounds, removed from the buffer."""
+ self.next_flush_at = time.monotonic() + self.flush_interval
+ batch, size = [], 0
+ while self.pending and len(batch) < MAX_BATCH_RECORDS:
+ rec = self.pending[0]
+ rec_size = len(json.dumps(rec).encode('utf-8'))
+ if batch and size + rec_size > MAX_BATCH_BYTES - 1024:
+ break
+ batch.append(self.pending.pop(0))
+ size += rec_size
+ self.pending_bytes = max(0, self.pending_bytes - rec_size)
+ if not self.pending:
+ self.oldest_pending_at = None
+ self.generation += 1
+ return {'schema': LOGS_SCHEMA, 'generation': self.generation, 'records': batch}
+
+ def requeue(self, batch: dict):
+ """A batch that was sent and not acknowledged goes back to the front;
+ its ids make the resend safe."""
+ records = list((batch or {}).get('records') or [])
+ self.pending[:0] = records
+ self.pending_bytes += sum(len(json.dumps(r).encode('utf-8')) for r in records)
+ if self.pending and self.oldest_pending_at is None:
+ self.oldest_pending_at = time.monotonic()
+
+ def apply_ack(self, payload: dict):
+ """Cloud's answer is the configuration."""
+ sources = payload.get('sources')
+ if isinstance(sources, list):
+ self.sources = [s for s in sources if isinstance(s, str)]
+ # `collect` is the instruction when it is present; a refusal that
+ # does not carry one (an unknown device, say) is also a stop.
+ stop = not payload.get('collect', bool(payload.get('ok')))
+ if stop:
+ self.collect = False
+ self.refused_until = time.monotonic() + REFUSED_RETRY_S
+ # Whatever was waiting is not wanted: consent was withdrawn or
+ # never given, and holding lines for a Cloud that refused them
+ # would be keeping what it said not to send.
+ self.pending.clear()
+ self.pending_bytes = 0
+ self.oldest_pending_at = None
+ logger.info('Connect logs: Cloud is not collecting (%s)', payload.get('reason') or 'off')
+ return
+ self.collect = True
+ self.refused_until = 0.0
+ interval = payload.get('flush_interval_s')
+ if isinstance(interval, (int, float)) and 1 <= interval <= 300:
+ self.flush_interval = float(interval)
+
+ def retry_after_refusal(self):
+ """Called on the slow retry: ask again with one empty batch."""
+ if not self.collect and time.monotonic() >= self.refused_until:
+ self.collect = True
+ self.sources = None
+
+
+def logs_frame(stream_id: str, batch: dict) -> dict:
+ return {'s': stream_id, 't': 'open', 'k': 'logs', 'p': batch}
diff --git a/backend/tests/test_connect_inventory.py b/backend/tests/test_connect_inventory.py
new file mode 100644
index 00000000..f0319901
--- /dev/null
+++ b/backend/tests/test_connect_inventory.py
@@ -0,0 +1,158 @@
+"""The panel half of the Cloud inventory.
+
+What matters: the key is the row id and survives a rename; a collector that
+fails leaves its section out of `capabilities` and names the reason; nothing
+that could carry a secret is in the document; the frame is the existing
+`facts` stream kind so an older Cloud is not confused by it.
+"""
+import json
+from datetime import datetime, timedelta
+
+import pytest
+
+from app.services import connect_inventory
+
+
+def apps(app):
+ from app import db
+ from app.models.application import Application
+ from app.models.deployment import Deployment
+ from app.models.domain import Domain
+ from app.models.user import User
+
+ with app.app_context():
+ user = User.query.first()
+ if user is None:
+ user = User(username='inv', email='[email protected]')
+ if hasattr(user, 'set_password'):
+ user.set_password('x' * 12)
+ db.session.add(user)
+ db.session.flush()
+ shop = Application(name='shop', app_type='wordpress', status='running',
+ port=8080, user_id=user.id)
+ worker = Application(name='worker', app_type='docker', status='error',
+ docker_image='registry.test/worker:1', user_id=user.id)
+ db.session.add_all([shop, worker])
+ db.session.flush()
+ db.session.add(Domain(name='shop.example.test', is_primary=True, ssl_enabled=True,
+ ssl_expires_at=datetime.utcnow() + timedelta(days=30),
+ application_id=shop.id))
+ started = datetime.utcnow() - timedelta(minutes=10)
+ db.session.add(Deployment(app_id=shop.id, version=3, status='live',
+ commit_hash='a41f9c2b1d5e', image_tag='registry.test/shop:3',
+ build_started_at=started,
+ build_completed_at=started + timedelta(seconds=32),
+ deploy_started_at=started + timedelta(seconds=32),
+ deploy_completed_at=started + timedelta(seconds=50)))
+ db.session.add(Deployment(app_id=shop.id, version=2, status='failed',
+ build_started_at=started - timedelta(hours=1)))
+ db.session.commit()
+ yield {'shop': shop.id, 'worker': worker.id}
+
+
+def _by_key(doc, key):
+ return next(a for a in doc['applications'] if a['key'] == str(key))
+
+
+def test_applications_are_keyed_on_the_row_id_and_carry_only_safe_fields(app, apps):
+ doc = connect_inventory.build_inventory(app, generation=4)
+ assert doc['schema'] == 'inventory/1'
+ assert doc['generation'] == 4
+ assert 'applications' in doc['capabilities']
+ assert doc['sections']['applications'] == {'complete': True}
+
+ shop = _by_key(doc, apps['shop'])
+ assert shop['name'] == 'shop'
+ assert shop['runtime'] == 'native'
+ assert shop['state'] == 'running'
+ assert shop['domains'][0]['host'] == 'shop.example.test'
+ assert shop['domains'][0]['tls_expires_at']
+ assert shop['ports'] == [8080]
+ assert shop['deployment']['key'] == f'{apps["shop"]}:3'
+ assert shop['deployment']['status'] == 'succeeded'
+ assert shop['source'] == {'commit': 'a41f9c2b1d5e'}
+ # The panel installed this WordPress, so its dashboard is a fact.
+ assert shop['admin_url'] == 'https://shop.example.test/wp-admin/'
+
+ worker = _by_key(doc, apps['worker'])
+ assert worker['runtime'] == 'docker'
+ assert worker['state'] == 'failed'
+ assert 'admin_url' not in worker
+
+ text = json.dumps(doc)
+ for forbidden in ('root_path', 'admin_secret', 'container_id', 'env', 'password'):
+ assert forbidden not in text, forbidden
+
+
+def test_a_rename_keeps_the_key(app, apps):
+ from app import db
+ from app.models.application import Application
+ before = _by_key(connect_inventory.build_inventory(app), apps['shop'])['key']
+ with app.app_context():
+ row = db.session.get(Application, apps['shop'])
+ row.name = 'shop-v2'
+ db.session.commit()
+ after = _by_key(connect_inventory.build_inventory(app), apps['shop'])
+ assert after['key'] == before and after['name'] == 'shop-v2'
+
+
+def test_deployments_are_history_with_stages_only_where_recorded(app, apps):
+ doc = connect_inventory.build_inventory(app)
+ assert 'deployments' in doc['capabilities']
+ mine = [d for d in doc['deployments'] if d['app_key'] == str(apps['shop'])]
+ assert [d['key'] for d in mine] == [f'{apps["shop"]}:3', f'{apps["shop"]}:2']
+ live = mine[0]
+ assert live['status'] == 'succeeded'
+ assert live['artifact'] == {'image': 'registry.test/shop:3', 'commit': 'a41f9c2b1d5e'}
+ assert [s['name'] for s in live['stages']] == ['build', 'deploy']
+ assert round(live['stages'][0]['duration_s']) == 32
+ assert round(live['duration_s']) == 50
+ failed = mine[1]
+ assert failed['status'] == 'failed' and 'stages' not in failed
+
+
+def test_a_collector_that_raises_leaves_its_section_out_and_says_why(app, apps, monkeypatch):
+ def boom():
+ raise RuntimeError('docker did not answer')
+ monkeypatch.setattr(connect_inventory, '_databases', boom)
+ doc = connect_inventory.build_inventory(app)
+ assert 'databases' not in doc
+ assert 'databases' not in doc['capabilities']
+ assert doc['sections']['databases']['complete'] is False
+ assert 'docker did not answer' in doc['sections']['databases']['error']
+ # The other sections are still there.
+ assert 'applications' in doc['capabilities']
+
+
+def test_the_repo_slug_never_carries_a_host_or_a_token():
+ slug = connect_inventory._repo_slug
+ assert slug('https://oauth2:[email protected]/acme/api.git') == 'acme/api'
+ assert slug('[email protected]:acme/api.git') == 'acme/api'
+ assert slug('https://gitlab.example.test/group/sub/api') == 'sub/api'
+ assert slug('') is None and slug(None) is None
+
+
+def test_the_frame_is_the_facts_stream_kind():
+ frame = connect_inventory.inventory_frame('inv1', {'schema': 'inventory/1'})
+ assert frame == {'s': 'inv1', 't': 'open', 'k': 'facts', 'p': {'schema': 'inventory/1'}}
+
+
+def test_diagnostics_never_include_a_command_line(monkeypatch):
+ doc = connect_inventory.build_diagnostics(60)
+ assert doc['schema'] == 'diagnostics/1'
+ assert doc['interval_s'] == 60
+ assert len(doc['processes']) <= connect_inventory.MAX_PROCESSES
+ for proc in doc['processes']:
+ assert set(proc) <= {'pid', 'name', 'user', 'cpu_pct', 'rss_bytes', 'started_at'}
+ assert 'processes' in doc['completeness'] and 'mounts' in doc['completeness']
+
+
+def test_the_client_backs_off_when_cloud_refuses_diagnostics():
+ from app.services import connect_client
+ client = connect_client.RelayClient(None)
+ client._apply_diagnostics_ack({'ok': False, 'reason': 'scope_not_granted', 'collect': False})
+ import time
+ assert client._diagnostics_next_at > time.monotonic() + connect_client.DIAGNOSTICS_INTERVAL_S
+ client._apply_inventory_ack({'ok': True, 'next_interval_s': 300})
+ assert client._inventory_next_at > time.monotonic() + 200
diff --git a/backend/tests/test_connect_logs.py b/backend/tests/test_connect_logs.py
new file mode 100644
index 00000000..fb909b1f
--- /dev/null
+++ b/backend/tests/test_connect_logs.py
@@ -0,0 +1,175 @@
+"""The panel half of bounded log collection.
+
+What matters: a file is followed from where it was left and rotation starts
+a new generation; ids are stable so a resend is a duplicate, not a second
+line; every bound Cloud enforces is applied here first; Cloud's answer is the
+configuration and `collect: false` stops collection at once; nothing that
+looks like a secret crosses the relay.
+"""
+import json
+import os
+
+import pytest
+
+from app.services import connect_logs
+
+
+def logfile(tmp_path):
+ path = tmp_path / 'serverkit.log'
+ path.write_text('')
+ return path
+
+
+def test_a_file_is_followed_from_where_it_was_left_and_rotation_is_a_generation(logfile):
+ tail = connect_logs.FileTail('serverkit.service', str(logfile))
+ # First sight starts at the end: history is not what Cloud keeps.
+ logfile.write_text('old line\n', newline='\n')
+ assert tail.read() == []
+
+ with open(logfile, 'a', encoding='utf-8', newline='\n') as fh:
+ fh.write('INFO one\nERROR two\npartial')
+ got = tail.read()
+ assert [r['text'] for r in got] == ['INFO one', 'ERROR two']
+ assert got[0]['severity'] == 'info' and got[1]['severity'] == 'error'
+ assert got[0]['generation'] == 0 and got[0]['offset'] == len('old line\n')
+ ids = {r['id'] for r in got}
+
+ # The partial line arrives once its newline does, and nothing is resent.
+ with open(logfile, 'a', encoding='utf-8', newline='\n') as fh:
+ fh.write(' finished\n')
+ again = tail.read()
+ assert [r['text'] for r in again] == ['partial finished']
+ assert not ids & {r['id'] for r in again}
+
+ # Truncation is a rotation: generation moves on, the offset restarts.
+ logfile.write_text('fresh\n', newline='\n')
+ rotated = tail.read()
+ assert rotated[0]['generation'] == 1 and rotated[0]['offset'] == 0
+
+
+def test_ids_are_stable_and_lines_are_bounded_and_redacted(logfile):
+ assert connect_logs.record_id('a', 1, 2) == connect_logs.record_id('a', 1, 2)
+ assert connect_logs.record_id('a', 1, 2) != connect_logs.record_id('a', 2, 2)
+
+ tail = connect_logs.FileTail('web.error', str(logfile))
+ tail.read()
+ with open(logfile, 'a', encoding='utf-8', newline='\n') as fh:
+ fh.write('token=ghp_abcdefghijklmnop123456 password: hunter2 '
+ 'https://u:[email protected]/ Authorization: Bearer abcdefghijkl\n')
+ fh.write('x' * (connect_logs.MAX_LINE_BYTES + 100) + '\n')
+ got = tail.read()
+ assert 'hunter2' not in got[0]['text'] and 'ghp_' not in got[0]['text']
+ assert 'u:p@' not in got[0]['text'] and 'abcdefghijkl' not in got[0]['text']
+ assert len(got[1]['text'].encode()) <= connect_logs.MAX_LINE_BYTES
+ assert got[1]['truncated'] is True
+ text = json.dumps(got)
+ assert str(logfile) not in text, 'a path never crosses the relay'
+
+
+def test_batches_respect_every_bound(monkeypatch):
+ pub = connect_logs.LogPublisher(None)
+ pub.sources = []
+ pub.apply_ack({'ok': True, 'collect': True, 'sources': ['serverkit.service'],
+ 'flush_interval_s': 5})
+ for i in range(450):
+ pub.pending.append({'id': f'r{i}', 'source': 'serverkit.service',
+ 'observed_at': 'now', 'text': 'x' * 900})
+ pub.pending_bytes = sum(len(json.dumps(r).encode()) for r in pub.pending)
+ first = pub.take_batch()
+ assert first['schema'] == 'logs/1'
+ assert len(first['records']) <= connect_logs.MAX_BATCH_RECORDS
+ assert len(json.dumps(first).encode()) <= connect_logs.MAX_BATCH_BYTES
+ # Flushes are spaced; the next one is not due yet.
+ assert pub.due() is False
+ pub.next_flush_at = 0.0
+ second = pub.take_batch()
+ assert second['generation'] == first['generation'] + 1
+ assert first['records'][0]['id'] != second['records'][0]['id']
+
+
+def test_a_refused_batch_goes_back_to_the_front_and_a_stop_clears_it():
+ pub = connect_logs.LogPublisher(None)
+ pub.apply_ack({'ok': True, 'collect': True, 'sources': ['web.error']})
+ pub.pending = [{'id': 'b', 'source': 'web.error', 'observed_at': 'now', 'text': 'b'}]
+ batch = {'schema': 'logs/1', 'generation': 1,
+ 'records': [{'id': 'a', 'source': 'web.error', 'observed_at': 'now', 'text': 'a'}]}
+ pub.requeue(batch)
+ assert [r['id'] for r in pub.pending] == ['a', 'b']
+
+ # Consent withdrawn: stop now, hold nothing, ask again only slowly.
+ pub.apply_ack({'ok': False, 'reason': 'scope_not_granted', 'collect': False, 'sources': []})
+ assert pub.collect is False and pub.pending == [] and pub.pending_bytes == 0
+ assert pub.due() is False
+ pub.retry_after_refusal()
+ assert pub.collect is False, 'not before the retry window'
+ pub.refused_until = 0.0
+ pub.retry_after_refusal()
+ assert pub.collect is True and pub.sources is None
+ assert pub.due() is True, 'one empty batch asks for the configuration'
+
+
+def test_before_cloud_answers_nothing_is_read_and_the_first_batch_is_empty(monkeypatch):
+ pub = connect_logs.LogPublisher(None)
+ assert pub.wanted() == []
+ assert pub.collect_now() == 0
+ assert pub.due() is True
+ batch = pub.take_batch()
+ assert batch['records'] == []
+ # Cloud names the sources; only those are read from then on.
+ pub.apply_ack({'ok': True, 'collect': True, 'sources': ['web.error', 'not-a-source']})
+ assert pub.wanted() == ['web.error']
+
+
+def test_deploy_output_is_sent_once_per_deployment(tmp_path):
+ log = tmp_path / 'build-7.json'
+ log.write_text(json.dumps({'logs': [{'message': 'Step 1'}, {'message': 'token=abc123456789'},
+ {'message': ''}]}))
+
+ class Dep:
+ id = 7
+ app_id = 3
+ version = 2
+ build_log_path = str(log)
+
+ reader = connect_logs.DeployOutput(None)
+ reader._recent = lambda: [Dep()]
+ got = reader.read()
+ assert [r['text'] for r in got] == ['Step 1', 'token=[redacted]']
+ assert got[0]['app_key'] == '3' and got[0]['deployment_key'] == '3:2'
+ assert reader.read() == [], 'once'
+ assert str(log) not in json.dumps(got)
+
+
+def test_the_client_wires_the_stream_and_requeues_an_unacknowledged_batch():
+ from app.services import connect_client
+ client = connect_client.RelayClient(None)
+ client._logs.apply_ack({'ok': True, 'collect': True, 'sources': []})
+ client._logs.pending = [{'id': 'a', 'source': 'web.error', 'observed_at': 'now', 'text': 'a'}]
+ client._logs.pending_bytes = 60
+ sent = []
+
+ class WS:
+ def send(self, raw):
+ sent.append(json.loads(raw))
+ assert client._publish_logs(WS()) is True
+ assert sent[0]['k'] == 'logs' and sent[0]['p']['records'][0]['id'] == 'a'
+ stream_id = sent[0]['s']
+ assert stream_id in client._logs_inflight
+
+ # A refusal that is not a stop: the batch goes back.
+ client._handle_frame(WS(), json.dumps({'s': stream_id, 't': 'close',
+ 'p': {'ok': False, 'reason': 'malformed',
+ 'collect': True, 'sources': ['web.error']}}))
+ assert [r['id'] for r in client._logs.pending] == ['a']
+ assert stream_id not in client._logs_inflight
+
+ # A stop: nothing is held and nothing more is sent.
+ client._logs.next_flush_at = 0.0
+ client._publish_logs(WS())
+ stream_id = sent[-1]['s']
+ client._handle_frame(WS(), json.dumps({'s': stream_id, 't': 'close',
+ 'p': {'ok': False, 'reason': 'scope_not_granted',
+ 'collect': False, 'sources': []}}))
+ assert client._logs.collect is False and client._logs.pending == []
+ assert client._publish_logs(WS()) is False