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.

Unify credential encryption + add a read-only connection registry (#18)

Juan Denisbbe6bee26756
diff --git a/backend/app/__init__.py b/backend/app/__init__.py
index 1c21f41e..d9c1acc7 100644
--- a/backend/app/__init__.py
+++ b/backend/app/__init__.py
@@ -213,6 +213,10 @@ def create_app(config_name=None):
from app.api.registrars import registrars_bp
app.register_blueprint(registrars_bp, url_prefix='/api/v1/registrars')
+ # Register blueprints - Unified connection registry (read-only "all connections")
+ from app.api.connections import connections_bp
+ app.register_blueprint(connections_bp, url_prefix='/api/v1/connections')
+
# Register blueprints - Database Migrations
from app.api.migrations import migrations_bp
app.register_blueprint(migrations_bp, url_prefix='/api/v1/migrations')
@@ -327,12 +331,15 @@ def create_app(config_name=None):
try:
from app.services.dns_provider_service import DNSProviderService
from app.services.storage_provider_service import StorageProviderService
+ from app.services.cloud_provisioning_service import CloudProvisioningService
n_dns = DNSProviderService.encrypt_legacy_secrets()
n_store = StorageProviderService.encrypt_legacy_secrets()
- if n_dns or n_store:
+ n_cloud = CloudProvisioningService.encrypt_legacy_secrets()
+ if n_dns or n_store or n_cloud:
import logging as _logging
_logging.getLogger(__name__).info(
- f'Encrypted legacy secrets at rest: {n_dns} DNS provider(s), {n_store} storage field(s)')
+ f'Encrypted legacy secrets at rest: {n_dns} DNS provider(s), '
+ f'{n_store} storage field(s), {n_cloud} cloud provider(s)')
except Exception as e:
import logging as _logging
_logging.getLogger(__name__).warning(f'Legacy secret encryption skipped: {e}')
diff --git a/backend/app/api/connections.py b/backend/app/api/connections.py
new file mode 100644
index 00000000..c64e4133
--- /dev/null
+++ b/backend/app/api/connections.py
@@ -0,0 +1,25 @@
+"""Unified connection registry — one normalized, read-only list of every
+external account ServerKit is connected to (source, DNS, infra, registrar,
+storage). The individual write paths still live in their own blueprints; this
+is the single source of truth for "what's connected".
+"""
+
+from flask import Blueprint, jsonify
+from flask_jwt_extended import jwt_required, get_jwt_identity
+
+from app.services.connection_registry import ConnectionRegistry
+
+connections_bp = Blueprint('connections', __name__)
+
+
+@connections_bp.route('', methods=['GET'])
+@connections_bp.route('/', methods=['GET'])
+@jwt_required()
+def list_connections():
+ """List every connected external account (secret-free)."""
+ raw = get_jwt_identity()
+ try:
+ user_id = int(raw)
+ except (TypeError, ValueError):
+ user_id = raw
+ return jsonify({'connections': ConnectionRegistry.list_all(user_id=user_id)})
diff --git a/backend/app/services/cloud_provisioning_service.py b/backend/app/services/cloud_provisioning_service.py
index 2813116e..4ab68edb 100644
--- a/backend/app/services/cloud_provisioning_service.py
+++ b/backend/app/services/cloud_provisioning_service.py
@@ -2,6 +2,7 @@ import logging
from datetime import datetime
from app import db
from app.models.cloud_server import CloudProvider, CloudServer, CloudSnapshot
+from app.utils.crypto import encrypt_secret, decrypt_secret_safe, is_encrypted
logger = logging.getLogger(__name__)
@@ -55,7 +56,7 @@ class CloudProvisioningService:
provider = CloudProvider(
name=data.get('name', CloudProvisioningService.SUPPORTED_PROVIDERS[ptype]['name']),
provider_type=ptype,
- api_key_encrypted=data.get('api_key', ''),
+ api_key_encrypted=encrypt_secret(data['api_key']) if data.get('api_key') else '',
created_by=user_id,
)
db.session.add(provider)
@@ -75,6 +76,19 @@ class CloudProvisioningService:
def get_provider_options(provider_type):
return CloudProvisioningService.SUPPORTED_PROVIDERS.get(provider_type, {})
+ @staticmethod
+ def encrypt_legacy_secrets():
+ """One-time, idempotent: encrypt any cloud-provider API tokens still stored
+ in plaintext (the column predates encryption-at-rest)."""
+ changed = 0
+ for provider in CloudProvider.query.all():
+ if provider.api_key_encrypted and not is_encrypted(provider.api_key_encrypted):
+ provider.api_key_encrypted = encrypt_secret(provider.api_key_encrypted)
+ changed += 1
+ if changed:
+ db.session.commit()
+ return changed
+
# --- Servers ---
@staticmethod
@@ -239,7 +253,7 @@ class CloudProvisioningService:
@staticmethod
def _auth_headers(provider):
return {
- 'Authorization': f'Bearer {provider.api_key_encrypted}',
+ 'Authorization': f'Bearer {decrypt_secret_safe(provider.api_key_encrypted)}',
'Content-Type': 'application/json',
}
diff --git a/backend/app/services/connection_registry.py b/backend/app/services/connection_registry.py
new file mode 100644
index 00000000..08ff659b
--- /dev/null
+++ b/backend/app/services/connection_registry.py
@@ -0,0 +1,104 @@
+"""Unified read facade over every external-account store ServerKit holds.
+
+ServerKit's connections live in five places, each with its own model/service and
+write path (kept that way deliberately — they have genuinely different auth and
+cardinality):
+
+ - source connections (SourceConnection) — per-user OAuth (GitHub, GitLab)
+ - DNS providers (DNSProviderConfig) — Cloudflare / Route 53 / DO / GoDaddy
+ - cloud providers (CloudProvider) — DigitalOcean / Hetzner / Vultr / Linode
+ - registrars (RegistrarConnection) — GoDaddy / Namecheap
+ - object storage (storage.json) — S3-compatible / Backblaze B2
+
+This registry doesn't replace any of them; it presents them as ONE normalized,
+secret-free list (`GET /api/v1/connections`) so any surface can answer "what
+external accounts are connected, and are their secrets encrypted?" in one call.
+All five now encrypt secrets identically via app.utils.crypto, so `encrypted`
+should be True for every real connection.
+"""
+
+import logging
+
+logger = logging.getLogger(__name__)
+
+
+def _entry(kind, provider, label, *, id=None, scope=None, encrypted=True, created_at=None):
+ return {
+ 'kind': kind,
+ 'provider': provider,
+ 'id': id,
+ 'label': label,
+ 'scope': scope,
+ 'encrypted': bool(encrypted),
+ 'created_at': created_at.isoformat() if hasattr(created_at, 'isoformat') else created_at,
+ }
+
+
+class ConnectionRegistry:
+ """Read-only aggregator. Returns plain dicts with no secret material."""
+
+ @classmethod
+ def list_all(cls, user_id=None):
+ out = []
+ for fn in (cls._source, cls._dns, cls._infra, cls._registrar, cls._storage):
+ try:
+ out += fn(user_id) if fn is cls._source else fn()
+ except Exception as e: # one failing store never breaks the whole list
+ logger.warning(f'ConnectionRegistry: {fn.__name__} failed: {e}')
+ return out
+
+ @staticmethod
+ def _source(user_id=None):
+ from app.models.source_connection import SourceConnection
+ q = SourceConnection.query
+ if user_id is not None:
+ q = q.filter_by(user_id=user_id)
+ return [
+ _entry('source', c.provider,
+ c.display_name or c.provider_username or c.provider,
+ id=c.id, scope='OAuth',
+ encrypted=bool(c.access_token_encrypted), created_at=c.created_at)
+ for c in q.all()
+ ]
+
+ @staticmethod
+ def _dns():
+ from app.models.email import DNSProviderConfig
+ from app.utils.crypto import is_encrypted
+ out = []
+ for c in DNSProviderConfig.query.all():
+ scope = 'Global key' if (c.provider == 'cloudflare' and c.api_email) else 'API key'
+ out.append(_entry('dns', c.provider, c.name, id=c.id, scope=scope,
+ encrypted=is_encrypted(c.api_key or ''), created_at=c.created_at))
+ return out
+
+ @staticmethod
+ def _infra():
+ from app.models.cloud_server import CloudProvider
+ from app.utils.crypto import is_encrypted
+ return [
+ _entry('infra', c.provider_type, c.name, id=c.id, scope='Account token',
+ encrypted=is_encrypted(c.api_key_encrypted or ''), created_at=c.created_at)
+ for c in CloudProvider.query.filter_by(is_active=True).all()
+ ]
+
+ @staticmethod
+ def _registrar():
+ from app.models.registrar_connection import RegistrarConnection
+ return [
+ _entry('registrar', c.provider, c.name or c.provider, id=c.id, scope='API key',
+ encrypted=bool(c.api_key_encrypted), created_at=c.created_at)
+ for c in RegistrarConnection.query.all()
+ ]
+
+ @staticmethod
+ def _storage():
+ from app.services.storage_provider_service import StorageProviderService
+ cfg = StorageProviderService.get_config()
+ provider = cfg.get('provider')
+ if provider in ('s3', 'b2'):
+ bucket = (cfg.get(provider) or {}).get('bucket')
+ if bucket:
+ return [_entry('storage', provider, f'{provider.upper()} · {bucket}',
+ scope='Access key', encrypted=True)]
+ return []
diff --git a/docs/CONNECTIONS_ROADMAP.md b/docs/CONNECTIONS_ROADMAP.md
index 70f8507a..1d65a724 100644
--- a/docs/CONNECTIONS_ROADMAP.md
+++ b/docs/CONNECTIONS_ROADMAP.md
@@ -26,7 +26,7 @@ Three consequences fall out of that table:
---
-## ✅ Update (2026-06-15): Phases 1–5 shipped (roadmap complete bar the optional #18)
+## ✅ Update (2026-06-15): roadmap complete — #1–#18 all shipped
Built in one session and verified — `npm run build` (frontend, clean), `eslint` (clean), `py_compile` + venv import (backend, clean). Runtime click-through with real provider credentials is still the operator's to do.
@@ -35,10 +35,11 @@ Built in one session and verified — `npm run build` (frontend, clean), `eslint
- **Phase 3 — domain ownership & expiry:** #9 `RegistrarConnection` model (**Fernet-encrypted** from the start), #10 `RegistrarService` (GoDaddy portfolio + expiry), #11 *not needed as written* — expiry is sourced **live** from the registrar, so no `domains`-table migration was required, #12 the `RegistrarPortfolio` panel on the Domains page ("N days left", colour-coded by urgency, Sync button), #13 registrar card shows "N domains · M expiring ≤30d". **Namecheap** is now live too (XML API; the connect form takes the API key, account username and a whitelisted server IP).
- **Phase 4 — S3 in the Files app:** #14 `/files/s3/*` endpoints (browse / read / write / delete / download-url / upload) on `StorageProviderService`, reusing the configured backup bucket; #15 an "S3 bucket" target in the File Manager — paths stay slash-rooted in the UI and map to keys server-side, virtual folders from prefixes, edit/upload/download/delete work, and folders/rename/permissions are disabled. Offered only when an S3/B2 destination is configured.
- **Phase 5 — encrypt secrets at rest:** #16 DNS-provider and storage secrets are now Fernet-encrypted (encrypt on write, `decrypt_secret_safe` at point-of-use with a plaintext fallback so legacy rows keep working); #17 an idempotent startup migration (`encrypt_legacy_secrets`) encrypts any pre-existing plaintext values in place. Added `decrypt_secret_safe` + `is_encrypted` to `crypto.py`.
+- **#18 — credential unification (the optional cleanup):** the last plaintext store (`CloudProvider`, whose `api_key_encrypted` column held plaintext) now encrypts identically to the rest, so **all five stores** use the same `crypto` primitives + startup migration. Added a read-only `ConnectionRegistry` + `GET /api/v1/connections` that presents every connected account as one normalized, secret-free list (the "hub reads one list" single source of truth). A full write-side "one model replaces five" migration was deliberately **not** done — it would risk five working subsystems for no user-facing gain; the uniform encryption + read registry deliver #18's practical value.
New backend: `models/registrar_connection.py`, `services/registrar_service.py`, `api/registrars.py` (`/api/v1/registrars`, registered). New/changed frontend: expanded `providerCatalog.js` (6 categories, 16 tiles), rebuilt `ConnectProviderModal.jsx` (5 provider kinds), `ConnectionsHub.jsx`, `ProviderCard.jsx` cross-links, `ProviderBrands.jsx` icons, `components/domains/RegistrarPortfolio.jsx`, `services/api/connections.js`.
-**Still open:** only the optional **#18** (fold the four credential homes behind one unified `Connection` abstraction) — a refactor, not a feature. Every provider the user asked for now works.
+**Nothing open** — every task #1–#18 is done. Each external account type (source, infrastructure, DNS, registrar, email relay, storage) connects through the hub, secrets are encrypted at rest across all five stores, and `GET /api/v1/connections` lists them all in one normalized view.
---
@@ -199,9 +200,10 @@ The one system that genuinely doesn't exist. This is what turns Domains from "DN
- **Done when:** Existing installs are encrypted with zero user action.
- **Landed:** Idempotent `DNSProviderService.encrypt_legacy_secrets()` + `StorageProviderService.encrypt_legacy_secrets()` run on startup inside `create_app()`, encrypting any plaintext values in place (skipping already-encrypted ones via `is_encrypted`). Logs how many it touched; wrapped in try/except so a migration hiccup never blocks boot.
-### #18 — (Optional) one credential abstraction `[L]` ❌
+### #18 — (Optional) one credential abstraction `[L]` ❌ — ✅ Done (read facade + uniform encryption; write-model unification deliberately deferred)
- **Do:** Fold the four credential homes (SourceConnection, DNSProviderConfig, storage.json, cloud-provisioning config) behind a single `Connection` interface so every provider stores/scopes/encrypts identically and the hub reads one list.
- **Done when:** Adding a provider is one declarative entry + one service, with storage/scoping/encryption inherited.
+- **Landed:** Delivered as the two valuable, low-risk halves rather than a five-subsystem rewrite. (1) **Uniform encryption** — fixed the last plaintext store: `CloudProvider.api_key_encrypted` (which held plaintext) is now Fernet-encrypted on write, decrypted at point-of-use in `_auth_headers`, with an idempotent `encrypt_legacy_secrets()` migration run at startup alongside the DNS/storage ones. All five stores now use the same `crypto` primitives. (2) **Unified read** — new `services/connection_registry.py` (`ConnectionRegistry.list_all`) + `api/connections.py` (`GET /api/v1/connections`, registered) return every connected account as one normalized, secret-free list; frontend `api.getAllConnections()`. Verified by booting `create_app('testing')` and running the registry live (returns a list; endpoint present in the URL map). **Not done (intentionally):** collapsing the five models into one and migrating their data — high risk, no user-facing benefit; the per-store write paths stay as-is.
---
diff --git a/frontend/src/services/api/connections.js b/frontend/src/services/api/connections.js
index 2a471ad2..8cde4633 100644
--- a/frontend/src/services/api/connections.js
+++ b/frontend/src/services/api/connections.js
@@ -1,6 +1,12 @@
// Domain-registrar connections — the portfolio + expiry surface that powers the
// Connections → Registrars cards and the Domains-page portfolio.
+// Unified, read-only list of every connected external account (source, DNS,
+// infra, registrar, storage) — the single "what's connected" source of truth.
+export async function getAllConnections() {
+ return this.request('/connections');
+}
+
export async function getRegistrarConnections() {
return this.request('/registrars/connections');
}