mirror of
https://github.com/Dvorinka/Containr.git
synced 2026-10-28 07:41:27 +00:00
Compare commits
164
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
86567a1ae8 | ||
|
|
fc82c30765 | ||
|
|
24b9b1267e | ||
|
|
a05d225016 | ||
|
|
61a33a066f | ||
|
|
f88b4f1bf3 | ||
|
|
0dc5857b5f | ||
|
|
66301bb68a | ||
|
|
6bcbc1369e | ||
|
|
81a4ca28d8 | ||
|
|
269e031761 | ||
|
|
ce71b5af24 | ||
|
|
cca20f7eae | ||
|
|
202fdd7b5a | ||
|
|
687555fc78 | ||
|
|
302c595380 | ||
|
|
ca8eab4a65 | ||
|
|
a6b8397dd8 | ||
|
|
c4307034ec | ||
|
|
985d22798c | ||
|
|
26f495ac8e | ||
|
|
9243d020cc | ||
|
|
60145f7b21 | ||
|
|
dd7b735b38 | ||
|
|
d5909172ae | ||
|
|
1409186a1f | ||
|
|
a4bb7ad559 | ||
|
|
4071ed2991 | ||
|
|
c5562109bf | ||
|
|
604319ee06 | ||
|
|
3cc84553f4 | ||
|
|
d57ac72aa5 | ||
|
|
59dfd35583 | ||
|
|
4028639239 | ||
|
|
c3a35081d0 | ||
|
|
4458de56b0 | ||
|
|
9de586f177 | ||
|
|
dd5ce80ce2 | ||
|
|
bc93c83cf9 | ||
|
|
401daf69aa | ||
|
|
ae7e6b4991 | ||
|
|
ee43064c5c | ||
|
|
e054457156 | ||
|
|
3f066b3b1e | ||
|
|
7547375cc2 | ||
|
|
d4a4cde1fd | ||
|
|
8cfe2ca826 | ||
|
|
612520bbff | ||
|
|
5eac4a382b | ||
|
|
00e69b3733 | ||
|
|
50e39fe140 | ||
|
|
b47ced0135 | ||
|
|
9408464f12 | ||
|
|
954a1d1f80 | ||
|
|
cf7afd2ba7 | ||
|
|
f524d95894 | ||
|
|
f499678964 | ||
|
|
64cbf82dd8 | ||
|
|
5c2f2861bd | ||
|
|
dca8aaa7b3 | ||
|
|
6946357a7b | ||
|
|
a0952faf25 | ||
|
|
999d719377 | ||
|
|
8d682583ff | ||
|
|
c80fbc766d | ||
|
|
99ee4e79cf | ||
|
|
f729ef2c82 | ||
|
|
f90fc5f5ad | ||
|
|
f68225a97d | ||
|
|
7536443610 | ||
|
|
68da4236fa | ||
|
|
b9441f3e11 | ||
|
|
605a43455d | ||
|
|
c7256954c2 | ||
|
|
a50260e857 | ||
|
|
3441acbc37 | ||
|
|
c5e0561add | ||
|
|
1ef5ea5f7f | ||
|
|
d26452d97e | ||
|
|
164a39c9c6 | ||
|
|
63f65c78cc | ||
|
|
d5960fdfff | ||
|
|
31c04ad51d | ||
|
|
03f6dcc80d | ||
|
|
1a4ee99f5b | ||
|
|
264f619be5 | ||
|
|
11525194b0 | ||
|
|
9e3994e937 | ||
|
|
355eb2c9ab | ||
|
|
b5a1a8cc21 | ||
|
|
91f40a652d | ||
|
|
2be1715015 | ||
|
|
b32e63368a | ||
|
|
f1a0f2189d | ||
|
|
e3676bd89a | ||
|
|
71c7d2b9f0 | ||
|
|
e67a39436c | ||
|
|
2c3d9fc123 | ||
|
|
4734be0d9d | ||
|
|
4e8d364911 | ||
|
|
f53695be4f | ||
|
|
925199633b | ||
|
|
1054679ca2 | ||
|
|
57f8b4f56e | ||
|
|
ace68da188 | ||
|
|
2d626f8ee8 | ||
|
|
6d3aff6d58 | ||
|
|
c60c31693b | ||
|
|
9fd34482e3 | ||
|
|
541a4719d4 | ||
|
|
dd1bbaf5a2 | ||
|
|
f22a464ad0 | ||
|
|
58424ed0ab | ||
|
|
25fa8c47e6 | ||
|
|
b3da014d26 | ||
|
|
f5f1ff34a0 | ||
|
|
bac317e0e6 | ||
|
|
26d585ffb7 | ||
|
|
f0df61b52e | ||
|
|
ffe66d7b3d | ||
|
|
121b33f36a | ||
|
|
8a8b903cdf | ||
|
|
f83ad81f73 | ||
|
|
2049087ccd | ||
|
|
01edb677e5 | ||
|
|
b22ac401c8 | ||
|
|
ea96929435 | ||
|
|
971773f80c | ||
|
|
7e99e41f8d | ||
|
|
6c5ff7be41 | ||
|
|
7b93506e2d | ||
|
|
f34fba165c | ||
|
|
be83467cc9 | ||
|
|
c99b2df419 | ||
|
|
a26c103eb4 | ||
|
|
6949c01464 | ||
|
|
f22ae22375 | ||
|
|
a4b6ea4bd2 | ||
|
|
9f8eaed386 | ||
|
|
3360eedc29 | ||
|
|
79fb73afc8 | ||
|
|
99a25cb10e | ||
|
|
d135e5fd4b | ||
|
|
dbc87bf34c | ||
|
|
b4baa853b2 | ||
|
|
15045b8dd3 | ||
|
|
109f79268f | ||
|
|
6f43bbf8f6 | ||
|
|
b7131965b1 | ||
|
|
5dc9b5f115 | ||
|
|
3664eaed1a | ||
|
|
912f6f5142 | ||
|
|
e995c98d05 | ||
|
|
2f8f595d6e | ||
|
|
747b714a02 | ||
|
|
7839dc48a3 | ||
|
|
84d58cf808 | ||
|
|
18f78f00a3 | ||
|
|
485e41a817 | ||
|
|
e51b20b2d4 | ||
|
|
17e429b86b | ||
|
|
e881747388 | ||
|
|
f9ae5b962c | ||
|
|
15b5b0e6e7 |
No files matched your search
@@ -39,10 +39,24 @@ REDIS_URL=redis://:your_secure_redis_password@localhost:6379/0
|
||||
# Application Configuration
|
||||
# In production this must be a strong value with at least 32 characters.
|
||||
JWT_SECRET=SuperSecretJWTKey123456789!@#$%^&*()
|
||||
# Encrypts secret env vars at rest. Defaults to JWT_SECRET when unset;
|
||||
# setting it explicitly survives a JWT_SECRET rotation.
|
||||
SECRETS_KEY=
|
||||
|
||||
# Shared secret for node-agent registration/heartbeat auth.
|
||||
CONTAINR_AGENT_AUTH_TOKEN=replace_with_strong_agent_secret
|
||||
# Optional rotation list (comma-separated). When set, this takes precedence.
|
||||
# CONTAINR_AGENT_AUTH_TOKENS=current_secret,next_secret
|
||||
# Bundled agent binaries dir served at /api/agents/download/:platform.
|
||||
# CONTAINR_AGENT_BIN_DIR=/agents
|
||||
# Directory Traefik's file provider watches. The backend writes one
|
||||
# config per remote service here so domains route to node agents. The
|
||||
# infra compose wires a shared volume — set only for custom layouts.
|
||||
# TRAEFIK_DYNAMIC_DIR=/traefik-dynamic
|
||||
# Optional remote template catalog (another Containr instance). Its
|
||||
# templates merge into GET /templates as source:"registry" and
|
||||
# materialize locally on deploy. The bundled catalog works without it.
|
||||
# CONTAINR_TEMPLATE_REGISTRY_URL=https://templates.example.com
|
||||
CORS_ORIGINS=http://localhost:3000,http://localhost:8080
|
||||
# Optional build-time API URL overrides. Leave empty for the default
|
||||
# same-origin setup - the frontend nginx proxies /api to the backend, which
|
||||
@@ -155,3 +169,6 @@ DOCKER_GID=0
|
||||
|
||||
# Optional: trust reverse proxy CIDR (default local Docker bridge used by Traefik)
|
||||
TRUSTED_PROXY_CIDR=172.20.0.0/16
|
||||
|
||||
# Optional: audit log retention in days (default 90; 0 disables pruning)
|
||||
# AUDIT_RETENTION_DAYS=90
|
||||
@@ -0,0 +1 @@
|
||||
github: Dvorinka
|
||||
@@ -0,0 +1,18 @@
|
||||
# CI-only compose overlay: pull and build through mirror.gcr.io so shared
|
||||
# runner egress IPs never touch docker.io (anonymous pulls get 429'd).
|
||||
# Production refs in infra/docker-compose.yml stay unchanged.
|
||||
services:
|
||||
postgres:
|
||||
image: mirror.gcr.io/library/postgres:15-alpine
|
||||
traefik:
|
||||
image: mirror.gcr.io/library/traefik:v3.2
|
||||
backend:
|
||||
build:
|
||||
args:
|
||||
GO_IMAGE: mirror.gcr.io/library/golang:1.26-alpine
|
||||
NODE_IMAGE: mirror.gcr.io/library/node:22-alpine
|
||||
frontend:
|
||||
build:
|
||||
args:
|
||||
NODE_IMAGE: mirror.gcr.io/library/node:22-alpine
|
||||
NGINX_IMAGE: mirror.gcr.io/library/nginx:alpine
|
||||
@@ -17,6 +17,30 @@ permissions:
|
||||
contents: read
|
||||
|
||||
jobs:
|
||||
# Cheap gate so docs-only/roadmap PRs skip the heavyweight jobs. Skipped
|
||||
# required checks still satisfy branch protection ("skipped" = success).
|
||||
changes:
|
||||
name: Changed paths
|
||||
runs-on: ubuntu-latest
|
||||
outputs:
|
||||
code: ${{ steps.filter.outputs.code }}
|
||||
mobile: ${{ steps.filter.outputs.mobile }}
|
||||
steps:
|
||||
- uses: actions/checkout@v4
|
||||
- uses: dorny/paths-filter@v3
|
||||
id: filter
|
||||
with:
|
||||
filters: |
|
||||
code:
|
||||
- 'app/**'
|
||||
- 'infra/**'
|
||||
- 'docker-compose.yml'
|
||||
- 'install.sh'
|
||||
- '.github/workflows/**'
|
||||
- 'docs/api/**'
|
||||
mobile:
|
||||
- 'app/mobile/**'
|
||||
|
||||
infra:
|
||||
name: Infra (Compose + Scripts)
|
||||
runs-on: ubuntu-latest
|
||||
@@ -48,6 +72,8 @@ jobs:
|
||||
backend:
|
||||
name: Backend (Go)
|
||||
runs-on: ubuntu-latest
|
||||
needs: changes
|
||||
if: needs.changes.outputs.code == 'true' || github.event_name != 'pull_request'
|
||||
defaults:
|
||||
run:
|
||||
working-directory: app/backend
|
||||
@@ -70,6 +96,8 @@ jobs:
|
||||
frontend:
|
||||
name: Frontend (React)
|
||||
runs-on: ubuntu-latest
|
||||
needs: changes
|
||||
if: needs.changes.outputs.code == 'true' || github.event_name != 'pull_request'
|
||||
defaults:
|
||||
run:
|
||||
working-directory: app/frontend
|
||||
@@ -96,20 +124,52 @@ jobs:
|
||||
- name: Test
|
||||
run: npm test
|
||||
|
||||
mobile:
|
||||
name: Mobile (Expo)
|
||||
runs-on: ubuntu-latest
|
||||
needs: changes
|
||||
if: needs.changes.outputs.mobile == 'true' || github.event_name != 'pull_request'
|
||||
defaults:
|
||||
run:
|
||||
working-directory: app/mobile
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Node
|
||||
uses: actions/setup-node@v4
|
||||
with:
|
||||
node-version: "22"
|
||||
cache: npm
|
||||
cache-dependency-path: app/mobile/package-lock.json
|
||||
|
||||
- name: Install dependencies
|
||||
run: npm ci
|
||||
|
||||
- name: Typecheck
|
||||
run: npx tsc --noEmit
|
||||
|
||||
- name: Lint
|
||||
run: npx expo lint
|
||||
|
||||
smoke:
|
||||
name: Compose boot smoke
|
||||
runs-on: ubuntu-latest
|
||||
timeout-minutes: 20
|
||||
needs: changes
|
||||
if: needs.changes.outputs.code == 'true' || github.event_name != 'pull_request'
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Boot stack
|
||||
# .github/compose.ci.yml swaps every docker.io ref for
|
||||
# mirror.gcr.io — anonymous Hub pulls 429 on shared CI IPs.
|
||||
run: |
|
||||
BETTER_AUTH_SECRET=$(openssl rand -hex 32)
|
||||
BETTER_AUTH_INTERNAL_TOKEN=$(openssl rand -hex 32)
|
||||
export BETTER_AUTH_SECRET BETTER_AUTH_INTERNAL_TOKEN
|
||||
docker compose -f infra/docker-compose.yml up -d --build postgres dragonfly backend frontend traefik
|
||||
docker compose -f infra/docker-compose.yml -f .github/compose.ci.yml up -d --build postgres dragonfly backend frontend traefik
|
||||
|
||||
- name: Wait for backend health
|
||||
run: |
|
||||
@@ -137,6 +197,7 @@ jobs:
|
||||
name: Build & publish images
|
||||
runs-on: ubuntu-latest
|
||||
needs: [backend, frontend, smoke]
|
||||
if: needs.backend.result == 'success' && needs.frontend.result == 'success' && needs.smoke.result == 'success'
|
||||
permissions:
|
||||
contents: read
|
||||
packages: write
|
||||
@@ -145,7 +206,13 @@ jobs:
|
||||
uses: actions/checkout@v4
|
||||
|
||||
- name: Setup Docker Buildx
|
||||
# Mirror docker.io through gcr — shared CI IPs hit anonymous
|
||||
# rate limits on base-image metadata resolution otherwise.
|
||||
uses: docker/setup-buildx-action@v3
|
||||
with:
|
||||
config-inline: |
|
||||
[registry."docker.io"]
|
||||
mirrors = ["mirror.gcr.io"]
|
||||
|
||||
- name: Login to GHCR
|
||||
if: github.event_name != 'pull_request'
|
||||
@@ -189,6 +256,10 @@ jobs:
|
||||
push: ${{ github.event_name != 'pull_request' }}
|
||||
tags: ${{ steps.meta-backend.outputs.tags }}
|
||||
labels: ${{ steps.meta-backend.outputs.labels }}
|
||||
# Stamp the release tag into the binary so the in-app upgrade
|
||||
# check knows what it's running ("dev" disables it).
|
||||
build-args: |
|
||||
VERSION=${{ github.ref_type == 'tag' && github.ref_name || 'dev' }}
|
||||
cache-from: type=gha,scope=backend
|
||||
cache-to: type=gha,mode=max,scope=backend
|
||||
|
||||
@@ -227,7 +298,7 @@ jobs:
|
||||
name: Release
|
||||
runs-on: ubuntu-latest
|
||||
needs: [docker]
|
||||
if: github.event_name != 'pull_request'
|
||||
if: github.event_name != 'pull_request' && needs.docker.result == 'success'
|
||||
permissions:
|
||||
contents: write
|
||||
packages: write
|
||||
|
||||
@@ -124,3 +124,6 @@ ref/
|
||||
.playwright-mcp/
|
||||
canvas-*.png
|
||||
.vercel
|
||||
|
||||
# Local design previews (not shipped)
|
||||
design/
|
||||
@@ -0,0 +1,37 @@
|
||||
# AGENTS.md — Containr
|
||||
|
||||
Containr is a self-hosted Docker PaaS (Railway-style). Backend: Go + Gin + PostgreSQL (sqlc) + Redis-compatible cache (Dragonfly) + Docker API. Frontend: React + Vite + TypeScript + React Query. API contract: `docs/api/openapi.yaml` (generated types: `app/frontend/src/generated/api-types.ts`).
|
||||
|
||||
This file is for **coding agents working on the repo**. For operating Containr as a deployed product, see `llms.txt`.
|
||||
|
||||
## Layout
|
||||
|
||||
- `app/backend` — Go monolith. Entrypoints: `cmd/server` (API), `cmd/cli` (`containr` CLI), `cmd/mcp` (`containr-mcp` MCP server).
|
||||
- `app/backend/internal/api` — Gin handlers + route groups (`routes.go`: public / `read` OptionalAuth / `authed` / `admin`).
|
||||
- `app/backend/internal/middleware` — `Auth` (JWT + Better Auth session + PAT), `RequireAdmin`, `Idempotency`.
|
||||
- `app/backend/internal/cli/commands` — all CLI commands; shared `Client`, `Output`, `confirm` helpers live here.
|
||||
- `app/backend/internal/mcpserver` — declarative MCP tool registry mirroring the API.
|
||||
- `app/backend/sqlc` — `schema.sql` + `queries/*.sql`; generate via `sqlc generate` into `internal/database/sqlcdb`.
|
||||
- `app/backend/migrations_goose` — goose migrations (`YYYYMMDDHHMMSS_name.sql`).
|
||||
- `app/frontend` — React app. Pages under `src/features/`, API calls via `src/lib/api-client.ts`.
|
||||
|
||||
## Build / test / verify
|
||||
|
||||
```sh
|
||||
cd app/backend && go build ./... && go vet ./... && go test ./...
|
||||
cd app/frontend && npx tsc --noEmit && npm run lint && npm run build
|
||||
cd app/frontend && npm run generate:api # after any openapi.yaml change
|
||||
```
|
||||
|
||||
Dev stack: Postgres on `:15432` (`containr-postgres-1`), Dragonfly on `:16379` (`containr-dragonfly-1`). Server needs `DATABASE_URL`, `REDIS_URL`, `JWT_SECRET`, `PORT=8080`.
|
||||
|
||||
## Conventions that matter
|
||||
|
||||
- New endpoints go in the right route group; mutations on `authed`/`admin` get idempotency for free.
|
||||
- Deploy work goes through `deploy_queue` (`internal/deployqueue`) — per-service FIFO; enqueue jobs instead of `go`-ing deployment/reconcile goroutines so they never race a build.
|
||||
- New list endpoints use `pageWindow`/`paginationMeta` (`internal/api/pagination.go`) — cursor + page, `next_cursor` in the envelope.
|
||||
- New errors use stable codes via `respondError(c, status, code, msg)` in `internal/api/errors.go`.
|
||||
- New CLI command → also register an MCP tool in `internal/mcpserver/server.go` and document in OpenAPI. All three surfaces ship together.
|
||||
- Destructive CLI commands must gate on `--yes`/`confirm` via `commands.confirm`; destructive MCP tools require `confirm: true`.
|
||||
- sqlc: edit `schema.sql` + `queries/*.sql`, run `sqlc generate`, add the goose migration.
|
||||
- Don't commit secrets. PATs (`cnp_…`) and agent tokens (`cagt_…`) are sha256-hashed at rest; raw values are shown once.
|
||||
@@ -49,14 +49,15 @@ dedicated console.
|
||||
- **Public project catalog** — approved projects are browsable anonymously; new projects stay private until an admin approves them
|
||||
- **Admin console** (`/admin`) — platform stats, project approval/edit/delete, user admin management
|
||||
- **Private networking** — every project gets an isolated Docker network; services reach each other by name (`web:3000`) and share variables via `${{service.KEY}}` references
|
||||
- **Deployments & builds** — Docker deploys with history, image rollback, live build status
|
||||
- **Deployments & builds** — Docker deploys with history, image rollback, live build status; git-sourced services build directly on remote node agents (no registry required)
|
||||
- **Runtime** — replicas, published ports, domains, health checks, restart policies; start/stop/restart/redeploy from canvas or service page
|
||||
- **Git integration** — GitHub, GitLab, Bitbucket, Gitea; webhooks, GitHub App
|
||||
- **Managed databases** — create PostgreSQL, MySQL, MariaDB, MongoDB, Redis, Dragonfly, or ClickHouse directly from `/databases`, with backup/restore
|
||||
- **Template catalog** — 30 official templates (NocoDB, Plausible, MinIO, n8n, …) plus user-defined templates with JSON upload and owner-scoped edit/delete
|
||||
- **Metrics** — per-service Docker stats plus host CPU/memory/disk telemetry
|
||||
- **HA & scaling** — failover policies, node agents, autoscaling rules
|
||||
- **Auth & security** — Better Auth sessions (email/password self-hosted), audit logs, vulnerability scans
|
||||
- **Auth & security** — Better Auth sessions (email/password self-hosted), personal access tokens with `read`/`write`/`admin` scopes for CLI and agents, audit logs, vulnerability scans
|
||||
- **CLI** — `containr` covers the full API: projects, services, deployments, logs, exec, variables, databases, cron, templates, nodes, scaling, HA, security, gateway, admin — with `--json`, `--yes`, named profiles, and `containr up` for current-directory deploys
|
||||
- **Integrated docs** — searchable documentation rendered in-app on the landing page and `/docs`; syncs from GitHub, falls back to a bundled snapshot offline
|
||||
- **Networking** — Traefik reverse proxy, optional Cloudflare Tunnel
|
||||
|
||||
|
||||
+610
@@ -0,0 +1,610 @@
|
||||
# Containr — Roadmap 2: The dflow Convergence
|
||||
|
||||
Last verified: **2026-10-10** — deep pass over the full dflow codebase
|
||||
(`~/Desktop/PROG+HTML/dflow`, community fork of dflow-sh/dflow, frozen for
|
||||
preservation) against Containr `main` (`15b5b0e`).
|
||||
|
||||
Goal: make Containr the superior successor — absorb everything worth having
|
||||
from dflow, then go past it and past Railway. Same power for humans and
|
||||
agents: the UI is the friendly face, the CLI and MCP are the machine face,
|
||||
all three sit on the same API. **Branding stays Containr/Vertice** —
|
||||
white-labeling is an admin option, not an identity change.
|
||||
|
||||
---
|
||||
|
||||
## 1. What dflow actually is (verified)
|
||||
|
||||
Next.js 15 + PayloadCMS 3 + MongoDB + Redis/BullMQ. Runtime model: every
|
||||
host is a remote server driven **over SSH** with Dokku auto-installed as the
|
||||
per-app container manager. Async work = 43 BullMQ queue processors
|
||||
(`src/queues/**`), live logs via Redis pub/sub → SSE endpoint, persisted to
|
||||
deployment records. ~30 collections, ~180 server actions, 11 dashboard
|
||||
routes per service (general, deployments, logs, environment, domains,
|
||||
volumes, proxy, scaling, backups, settings).
|
||||
|
||||
### Full capability matrix — dflow → Containr
|
||||
|
||||
Legend: ✅ done · 🟡 partial/different · ❌ missing · ➖ not applicable (dokku/SSH-specific)
|
||||
|
||||
#### Deployment pipeline
|
||||
| dflow capability | Containr status | Action |
|
||||
|---|---|---|
|
||||
| Queued deployments, serialized per server, cancellable while queued | ❌ goroutine fire-and-forget; `CancelDeployment` exists in engine but is **never routed** | Add deploy queue + cancel route |
|
||||
| Deployment states `queued → building → success/failed`, live SSE logs + persisted record | 🟡 states exist (`building→running`); WS streams builds; no queued state | Extend status + stream |
|
||||
| Redeploy cache modes (`ps:rebuild` vs fresh pull); webhook pushes force `no-cache` | ❌ always rebuilds | Add `cache` flag on deploy/redeploy |
|
||||
| Cancel queued deployment from UI | ❌ (engine fn unrouted) | Route it |
|
||||
| Per-service job ordering | ❌ concurrent deploys of one service can race | Serialize per service |
|
||||
|
||||
#### Service configuration
|
||||
| dflow capability | Containr status | Action |
|
||||
|---|---|---|
|
||||
| Multi-domain per service + default flag + cert type | ❌ single `services.domain` | `service_domains` table |
|
||||
| Cert types: letsencrypt / none / Cloudflare Origin / **CF-for-SaaS custom hostnames** | 🟡 Traefik ACME only | LE default; CF custom hostnames deferred (only needed if hosting tenant domains) |
|
||||
| DNS preflight check (`checkDNSConfigAction`) | ❌ | `GET /services/:id/domains/check` |
|
||||
| Volumes per service + dangling-volume browser/attach/delete | ❌ engine supports `VolumeMounts`; no API/UI | `services.volumes` jsonb + Volumes tab |
|
||||
| Resource **limits and reservations**, per-server defaults | 🟡 `cpu_limit`/`memory_limit` int columns (unclear units) | explicit string units + reserves + instance defaults |
|
||||
| Pre-deploy capacity check (dokku binary interceptor) | ❌ | Node capacity check pre-deploy/scale |
|
||||
| Maintenance mode toggle | ❌ | Traefik middleware label |
|
||||
| Basic-auth gate per service (`toggleHttpAuth`) | ❌ | Traefik basic-auth middleware |
|
||||
| Raw proxy config override (`setServiceNginxConfig`) | ❌ | Allowlisted `traefik_labels` jsonb |
|
||||
| Service clone (full config copy) | ❌ | `POST /services/:id/clone` |
|
||||
| Move service between projects (`SwitchServiceProjectDialog`) | ❌ | `PUT /services/:id {project_id}` |
|
||||
| Env var save → hot-apply + optional restart (`noRestart`) | ❌ rows rewritten; containers keep stale env until manual redeploy | Apply→redeploy flow |
|
||||
| Static-site builder; Heroku buildpacks | 🟡 railpack/nixpacks/dockerfile auto-detect, no user override | `services.builder` enum + static builder |
|
||||
| Docker registry accounts (private registries) | ❌ | `registry_credentials` per service/instance |
|
||||
|
||||
#### Databases
|
||||
| dflow capability | Containr status | Action |
|
||||
|---|---|---|
|
||||
| Managed DBs (pg/mysql/mariadb/mongo/redis/clickhouse via dokku plugins) | ✅ plus dragonfly | — |
|
||||
| **External DB registration** (Neon/Atlas/RDS conn details + test) | ❌ | `provider=external` + test endpoint |
|
||||
| Internal backups + restore (same-type enforced) | ✅ | — |
|
||||
| Scheduled backups | ✅ cron field | — |
|
||||
| **External S3 backups** (auth/schedule/unschedule ops) | ❌ | `backup_targets` + shipper |
|
||||
| **Cross-server DB migration** (export → sftp hop → import) | ❌ | `POST /databases/:id/migrate` |
|
||||
| Public DB port expose/unexpose | ❌ loopback only | opt-in TCP expose |
|
||||
| DB → service env linking (`dokku link`) | ✅ Bind action injects `DATABASE_URL` | — |
|
||||
|
||||
#### Servers / infrastructure
|
||||
| dflow capability | Containr status | Action |
|
||||
|---|---|---|
|
||||
| Attach server = paste IP + SSH key; Dokku auto-installs | 🟡 agents exist, manual install | **SSH bootstrap**: one SSH session installs agent, never needed again |
|
||||
| Tailscale/Headscale, NetBird, ZeroTier enroll + mesh connect | ❌ | Agent mesh-IP detection first; auth-key enroll optional |
|
||||
| AWS EC2 + Hetzner provisioning w/ cloud-init | ❌ | IaC (ROADMAP §2.2): Terraform Proxmox/AWS + Hetzner module |
|
||||
| Security groups (AWS SG rules, sync) | ❌ | Under IaC provider modules |
|
||||
| Server-level global/wildcard domain (`updateServerDomain`) | ❌ | `nodes.default_domain` |
|
||||
| Scheduled server cleanup (dokku cleanup → docker prune) | ❌ | Agent `prune` command + schedule |
|
||||
| Queue flush/inspect per server (`flushServerQueues`, `getServerQueues`) | ➖ no queue yet | Ops page shows jobs once queue exists |
|
||||
| Arbitrary SSH command on server (`executeCommand`) | ➖ exec is container-scoped | Node terminal via agent (PTY) |
|
||||
| Server onboarding wizard, reset, cleanup-on-delete | 🟡 token issue/revoke only | Onboarding flow + `nodes add` UX |
|
||||
| Ansible playbooks (stored, runnable per server, exec history) | ➖ | **Defer** — security surface; SSH bootstrap covers provisioning |
|
||||
| Per-node resource dashboards | 🟡 heartbeat telemetry exists | Node detail page |
|
||||
|
||||
#### Platform / org
|
||||
| dflow capability | Containr status | Action |
|
||||
|---|---|---|
|
||||
| Multi-tenant organisations + custom RBAC (per-resource CRUD, create-limits, read-scope) | 🟡 `is_admin` + `project_members` | Defer full RBAC; design PAT scopes role-compatible |
|
||||
| Team invite links/emails | 🟡 manual user create only | `POST /invites` → link join |
|
||||
| User API keys → full REST API | ❌ **the big gap** — session/JWT only | PATs (Phase A) |
|
||||
| Outbound webhooks (collection events, HMAC, custom headers) | ✅ `outbound_webhooks` + `webhook_deliveries`, HMAC `X-Containr-Signature`, wildcard events, 3-retry, UI page | — |
|
||||
| Banners (global + per-tenant, dismissible) | ❌ | `banners` table + top bar |
|
||||
| White-label (logo/favicon/title/OG/theme colors) | ❌ | `app_settings.branding` — **Containr remains the default brand** |
|
||||
| Activity feed (icon/severity/category/metadata, 90-day TTL job) | ✅ `/activity` + project view | — |
|
||||
| Admin metrics dashboard (users/servers/queued/failed counts) | 🟡 `/admin` exists | Extend |
|
||||
| **Impersonate user** | ✅ admin token + `impersonated_by` claim (browser swap pending) | Done (CLI/API/MCP) |
|
||||
| In-app docs (content-collections markdown) | ✅ `/docs` | — |
|
||||
| Dynamic client-side filter framework (535-line `filter.utils`) | ✅ `dynamic-filter.ts` + URL state; audit/builds/activity | Done |
|
||||
| Ops "bubble" (queue monitor, terminal, sync) | ✅ `/operations` + interactive terminal | Done |
|
||||
|
||||
#### Agent surface
|
||||
| dflow capability | Containr status | Action |
|
||||
|---|---|---|
|
||||
| Payload REST/GraphQL auto-API + per-user API keys | ❌ | PATs + OpenAPI (better: typed, documented) |
|
||||
| CLI | ❌ | none in dflow — **Containr CLI is a genuine leap past it** |
|
||||
| MCP | ❌ | none — same leap |
|
||||
| Railway migrator script | ❌ | `containr migrate railway` |
|
||||
| GitHub App **manifest flow** (instance self-provisions the App) | ✅ manifest + convert + App webhook | Done |
|
||||
| fork-sync webhook deploy trigger | ✅ sync/repository_dispatch/workflow_run | Done |
|
||||
| Azure DevOps provider | ❌ | Optional (dflow does token-clone only) |
|
||||
|
||||
---
|
||||
|
||||
## 2. The spine: agent-first platform
|
||||
|
||||
*Agent and user on the same level.* Concretely:
|
||||
|
||||
- Every operation reachable by a **personal access token**, not just
|
||||
sessions.
|
||||
- Every operation in the **CLI** with `--json`, stable exit codes, `--yes`.
|
||||
- Every operation in an **MCP server** (stdio first) so Devin/Claude Code/
|
||||
Cursor/Codex drive Containr natively.
|
||||
- `openapi.yaml` stays the single source of truth; UI, CLI, MCP are thin
|
||||
clients.
|
||||
|
||||
### Phase A — Agent surface (do this FIRST; everything after inherits it) ✅ SHIPPED (PRs #14–#18)
|
||||
|
||||
- [x] **Personal access tokens.** `user_tokens` table — `id`, `user_id`,
|
||||
`name`, `key_prefix` (`cnp_…`), `key_hash` (sha256), `scopes`
|
||||
(`read`/`write`/`admin`, later resource-scoped), `expires_at`,
|
||||
`last_used_at`. Model on `agent_auth_tokens` (raw shown once, hash stored).
|
||||
`POST/GET/DELETE /api/v1/user/tokens`; `middleware.Auth` accepts
|
||||
`Bearer cnp_…`; Settings → Access tokens UI with one-time display +
|
||||
copyable CLI/MCP config snippets.
|
||||
- [x] **CLI v2.** Current `cmd/cli` is auth + projects only. Full client:
|
||||
- `auth login [--token] [--url] [--profile]` (multi-instance profiles).
|
||||
- Resources: `projects services deploy logs exec variables databases
|
||||
cron templates nodes scaling ha security gateway notifications
|
||||
webhooks domains volumes backups admin`.
|
||||
- `containr up` / `deploy` — cwd→service create-or-update→watch
|
||||
(the `railway up` equivalent; single most important command).
|
||||
- `containr link` (bind cwd→service), `containr open`, `containr run`,
|
||||
`containr shell` (PTY once exec upgrade lands), `completion`.
|
||||
- Universal `--json`, `-o yaml`, `--watch`, `--yes`; exit codes
|
||||
0/1/2/3/4 (ok/err/not-found/unauthorized/conflict).
|
||||
- GoReleaser → GH releases + `install.sh`; `version` command.
|
||||
- Conventions from docsync/envdiff/cix/repolint.
|
||||
- [x] **MCP server** (`cmd/mcp`, same module, stdio). Tools mirror the
|
||||
CLI: `list_projects create_service deploy get_logs set_variable
|
||||
restart rollback create_database trigger_cron deploy_template exec
|
||||
get_metrics security_scan …`. Auth: `CONTAINR_URL` + `CONTAINR_TOKEN`
|
||||
or shared `~/.containr/config`. `containr mcp serve` + install snippets
|
||||
for Devin/Claude/Cursor.
|
||||
- [x] **API agent ergonomics.** `Idempotency-Key` on mutating endpoints;
|
||||
uniform `{error, code, details}` envelope (audit bare-string returns);
|
||||
cursor pagination on growing lists; name-or-id lookup everywhere.
|
||||
- [x] **Agent docs.** Root `AGENTS.md`, `/docs` serves `llms.txt` +
|
||||
`llms-full.txt`, MCP tool list generated from OpenAPI where possible.
|
||||
|
||||
**Gate**: an agent holding only a PAT registers a project, deploys from a
|
||||
git repo, streams logs, sets vars, rolls back — zero UI. Same via CLI
|
||||
for a human.
|
||||
|
||||
---
|
||||
|
||||
## 3. Phase B — Deployment pipeline maturity ✅ SHIPPED (PR #19)
|
||||
|
||||
dflow's one real architectural edge. Containr deploys are fire-and-forget
|
||||
goroutines; this fixes correctness *and* unlocks the ops surface.
|
||||
|
||||
- [x] **Deploy job queue.** Lightweight internal queue — per-service
|
||||
FIFO in `internal/deployqueue`; status gains `queued`.
|
||||
- [x] **Cancel deployments.** `POST /deployments/:id/cancel` — queued
|
||||
jobs cancel immediately, active jobs cancel context; `cancelled` status.
|
||||
- [x] **Cache modes.** `no_cache` on deploys; webhook-triggered deploys
|
||||
default to no-cache (dflow convention — prevents stale-layer surprises).
|
||||
- [x] **Queued→building→running status + persisted step logs.** `queued`
|
||||
state lands; streaming over existing log surface.
|
||||
- [x] **Env var apply → redeploy.** `--redeploy` flag on variable writes;
|
||||
backend requeues honoring running state.
|
||||
- [x] **Encrypt variables at rest.** `is_secret` values encrypted
|
||||
AES-GCM `enc:v1:` (key from `SECRETS_KEY`); masked-echo preserves
|
||||
ciphertext.
|
||||
|
||||
## 4. Phase C — Service configuration parity ✅ SHIPPED (PRs #20–#23)
|
||||
|
||||
- [x] **Volumes on services** — `services.volumes` jsonb
|
||||
`[{volume|host_path, container_path, read_only}]`; Volumes tab +
|
||||
admin volume inventory.
|
||||
- [x] **Multi-domain** — `service_domains(service_id, domain, is_default,
|
||||
cert_type, status, last_checked_at)`. Domains UI: add/remove/
|
||||
set-default/DNS-status badge. `services.domain` stays derived default.
|
||||
- [x] **DNS preflight** — `GET /services/:id/domains/check` resolves and
|
||||
compares vs edge IP → `ok|wrong-target|pending`.
|
||||
- [x] **Maintenance mode + basic-auth gate** — Traefik middleware labels;
|
||||
no proxy code needed.
|
||||
- [x] **Routing overrides** — `services.traefik_labels` jsonb with
|
||||
allowlist validation. Keys `middlewares.<name>.<type>[.<field>]` scope
|
||||
to the service router (`svc-<id>-<name>`), auto-attach after builtins;
|
||||
router/service/provider-level keys rejected. API + CLI
|
||||
(`--traefik-label`) + MCP + service page Access editor.
|
||||
- [x] **Builder override** — `services.builder` enum
|
||||
`auto|railpack|nixpacks|dockerfile|static`; static builder = build +
|
||||
nginx serve. Fixed hardcoded `nixpacks` + `BuildImage` stream drain bug.
|
||||
- [x] **Resource limits + reserves** — `cpu`/`memory` caps +
|
||||
`cpu_reserve`/`memory_reserve` → NanoCPUs/Memory/MemoryReservation;
|
||||
instance defaults in `app_settings`.
|
||||
- [x] **Pre-deploy capacity check** — reservations vs node capacity;
|
||||
`app_settings.capacity_policy=block` rejects, default warns.
|
||||
- [x] **Service clone** + **move between projects** — copies volumes,
|
||||
domains, access gates, vars (ciphertext verbatim); audit-logged.
|
||||
- [x] **Private registries** — `registry_credentials`
|
||||
(server/username/secret, encrypted); pull auth resolved at deploy;
|
||||
per-service registry link.
|
||||
- [x] **Service detail tabs to match dflow's surface**: Domains,
|
||||
Volumes, access-controls sections added to the service page.
|
||||
|
||||
## 5. Phase D — Data layer maturity
|
||||
|
||||
- [x] **External DB registration** — `provider='managed'|'external'`,
|
||||
conn fields, `POST /databases/test-connection`, same bind flow. (PR #24)
|
||||
- [x] **Public DB port expose** — `public_port` opt-in `0.0.0.0` host
|
||||
bind; default stays loopback. (PR #25)
|
||||
- [x] **Offsite backups** — `backup_targets` (S3-compatible:
|
||||
endpoint/bucket/keys encrypted); per-DB destination; archives shipped
|
||||
via holder container + minio-go; restore-from-S3 fallback. (PR #26)
|
||||
- [ ] **Cross-node DB migration** — `POST /databases/:id/migrate
|
||||
{target_node_id}`: dump → stream transfer → provision → restore →
|
||||
re-point bound services. Deferred until Phase F lands real second
|
||||
nodes — nothing to migrate between today.
|
||||
- [x] **Backup import** — `POST /databases/:id/backups/import` accepts a
|
||||
`.tar.gz` (multipart `file` or raw body, 2 GiB cap, gzip-magic
|
||||
validated), stores it in the backups volume via a holder container,
|
||||
registers a completed restore point, and ships offsite when a backup
|
||||
target is configured. `api/backuptargets.go`. (pairs with
|
||||
migrators).
|
||||
|
||||
## 6. Phase E — Templates v2 (absorbs compose-catalog spec + dflow graph templates)
|
||||
|
||||
- [x] **Template = ordered service graph** — `services[]` entries with
|
||||
`{name, type, source, build, ports, volumes, variables, depends_on}`;
|
||||
deploy order from dependency refs. `templategraph.go`,
|
||||
`topoSortServices`.
|
||||
- [x] **Expressions** — `{{service.KEY.prop}}` (host/port/user/password/
|
||||
database/url), `{{secret}}`/`{{secret(N)}}`, `{{random:N}}`,
|
||||
`{{VAR:-default}}` (incl. explicitly-empty defaults); `POST
|
||||
/templates/:id/plan` returns the resolved graph without deploying.
|
||||
- [x] **Compose import** — `POST /templates/import/compose` (yaml →
|
||||
graph, known DB images become managed databases), `POST
|
||||
/templates/import/git` (compose file fetched from any git repo — clone
|
||||
URL, ssh remote, or owner/repo via connected providers, `ref` selects
|
||||
branch/commit); `templates import-compose|import-git` CLI, MCP tools,
|
||||
paste-or-git toggle in the template UI.
|
||||
- [x] **Graph deploy pipeline** — creates services in dependency order,
|
||||
resolves vars/secrets, triggers builds. `deployTemplateGraph`,
|
||||
`POST /templates/deploy` (inline), `POST /projects/:id/import-compose`.
|
||||
- [x] **Visual template builder** — canvas mode in the template editor:
|
||||
web/worker/cron/database nodes, handles wire `depends_on`, inspector
|
||||
edits image/repo/engine/port/env/volumes, live validation, applies
|
||||
back to the version-2 graph JSON. Positions persist under
|
||||
`config.canvas`; flat legacy configs open as a single node.
|
||||
`builder-model.ts` + `TemplateBuilder.tsx`.
|
||||
- [x] **Catalog growth** — seeded graph stacks for n8n, Gitea,
|
||||
Nextcloud, Vaultwarden, Umami, Immich (Plausible already shipped).
|
||||
`TestSeedTemplatesResolveCleanly` keeps every seed parseable,
|
||||
topo-sortable, and fully resolved. Remaining single-service guides can
|
||||
convert incrementally via the same endpoint.
|
||||
|
||||
**Gate**: paste n8n compose → 3-service project with generated secrets
|
||||
and wired refs, one click or one CLI call.
|
||||
|
||||
## 7. Phase F — Nodes & infrastructure
|
||||
|
||||
- [x] **SSH bootstrap install** — `containr nodes add <user@host>` mints an
|
||||
enroll token, SSHes once (agent/key/default keys), runs
|
||||
`GET /api/agents/install.sh | bash` remotely, closes the session.
|
||||
`--print` emits the one-liner for manual runs; the Usage page shows the
|
||||
same command. Agent binaries ship in the image under `/agents` and are
|
||||
served at `/api/agents/download/{linux-amd64,linux-arm64}` (install
|
||||
script falls back to GH releases). `cli/commands/nodes_add.go`,
|
||||
`api/assets/install-agent.sh`, `agents.go:ServeInstallScript`/`ServeAgentBinary`.
|
||||
- [x] **Mesh awareness** — agent reports tailscale/netbird/wireguard/
|
||||
zerotier interface IPs at register (`mesh` field, authoritative on
|
||||
re-register), stored in `node_agents.metadata.mesh`, shown on the node
|
||||
detail page. `TAILSCALE_AUTH_KEY` enroll-at-provision still open.
|
||||
- [x] **Node detail page** — `/nodes/:id` (admin): heartbeat telemetry
|
||||
(cpu/mem sparklines over 1h/24h/7d via `GET /agents/:id/metrics`),
|
||||
containers, command history, rename, auto-prune toggle, prune-now,
|
||||
remove. Drain/cordon and self-upgrade deferred — they need
|
||||
`services.node_id` routing which lands with multi-node scheduling.
|
||||
- [x] **Node housekeeping** — agent `prune` + `system_df` commands
|
||||
(`docker system prune -af`, volumes excluded unless requested, optional
|
||||
`--until`). `node_agents.auto_prune` + the health sweep enqueue a prune
|
||||
at most once per 24h per node. Surfaces: `POST /agents/:id/prune`,
|
||||
`nodes update --auto-prune` / `nodes prune [--volumes --until]` /
|
||||
`nodes commands`, MCP `containr_nodes_update|prune|commands`
|
||||
(confirm-gated), Usage-page per-node Prune button + auto toggle.
|
||||
`cmd/agent/main.go`, `notification_producers.go:scheduleAutoPrune`.
|
||||
- [🟡] **Multi-node scheduling** — phase 1 shipped: `services.node_id`
|
||||
pin (explicit / `auto` = least-loaded online / `local` = clear),
|
||||
`NodeRunner` seam in the deploy engine routes reconcile to the agent's
|
||||
command queue (`create_container` with image/env/cmd/ports/volumes/
|
||||
labels/limits), `container_instances` inventory per (agent, service,
|
||||
replica), remote stop/start/restart/delete fan-out, `GET
|
||||
/nodes/options`, `--node` on services create/update + MCP + OpenAPI +
|
||||
service-detail Placement section. Drain/cordon shipped:
|
||||
`node_agents.schedulable`, `POST /agents/:id/cordon|uncordon|drain`
|
||||
(drain evicts remote containers via the agent queue + unpins services
|
||||
so they redeploy locally), `nodes cordon|uncordon|drain` CLI +
|
||||
MCP (drain confirm-gated) + node-detail page controls; `auto` and
|
||||
explicit pins skip cordoned nodes. Spread placement shipped:
|
||||
`services.spread` distributes replicas deterministically across every
|
||||
online schedulable agent (replica i → agents[i mod n]), mutually
|
||||
exclusive with a pin; `auto` is now resource-aware — least memory
|
||||
utilisation, then cpu, then container count, with uninstrumented
|
||||
agents sorting last. Lifecycle and `GET /runtime` are
|
||||
inventory-driven, so remote replicas show up and tear down wherever
|
||||
they run. Remote ingress shipped: the agent reports assigned host
|
||||
ports in the `create_container` result, the backend renders a
|
||||
Traefik file-provider config per remote service into
|
||||
`TRAEFIK_DYNAMIC_DIR` (shared volume, `watch=true`) pointing the
|
||||
loadbalancer at each replica's mesh IP + published port; route files
|
||||
are rewritten on every remote reconcile and removed on teardown or
|
||||
local placement. Agents report managed-container states at every
|
||||
heartbeat — exits, OOMs, and crash loops land in inventory between
|
||||
reconciles (tombstones never resurrect). Remote sleep/wake shipped:
|
||||
heartbeats carry per-container net counters, idle remote services
|
||||
lose replicas via agent remove + tombstones, and the route file
|
||||
repoints at `/internal/wake-page/:id` on the backend — first hit
|
||||
retriggers the wake reconcile, which recreates replicas and restores
|
||||
real upstreams. Remote nodes run
|
||||
registry-pulled images only — git builds are the remaining
|
||||
documented ceiling. Tag affinity shipped: `node_agents.tags`
|
||||
(operator-set via `PUT /agents/:id` / `nodes update --tags`) +
|
||||
`services.placement_tags` — `auto` and `spread` only consider
|
||||
online schedulable agents carrying every required tag, tags are
|
||||
re-evaluated each reconcile (a tag change re-spreads replicas),
|
||||
unsatisfiable requirements fail with a clear error, pin + tags
|
||||
and local + tags are rejected, empty array clears. Surfaces:
|
||||
`--placement-tags` on services create/update, `--tags` on
|
||||
`nodes update`, MCP args, OpenAPI, node-detail tag editor +
|
||||
service-detail placement-tags input. Build-on-node and remote
|
||||
sleep/wake both shipped (see below). Remaining: spread weighting
|
||||
by real capacity.
|
||||
- [x] **Agent self-upgrade** — `POST /agents/:id/upgrade` enqueues a
|
||||
`self_upgrade` command carrying platform + sha256 + server version;
|
||||
the agent downloads its replacement from `GET /agents/download/
|
||||
:platform`, verifies the checksum, sanity-checks `--version`,
|
||||
atomically swaps its own executable, reports the result, then
|
||||
re-execs (`syscall.Exec`) — no supervisor needed. Agents now stamp
|
||||
`main.version` via ldflags at image build and report it at
|
||||
register/heartbeat (`node_agents.version` refreshes on re-register;
|
||||
older agents keep their stored value). Surfaces: `nodes upgrade`
|
||||
CLI, `containr_nodes_upgrade` MCP (confirm-gated), node-detail page
|
||||
button (online-gated). Ceilings: linux amd64/arm64 only; agents on
|
||||
other platforms get a `NO_BINARY` 409.
|
||||
- [x] **Build-on-node** — private registry pulls + real builds shipped.
|
||||
Git sources materialize via `internal/source` checkout (branch/commit,
|
||||
`GIT_ASKPASS` credential injection, dumb-transport fallback), package
|
||||
through `BuildManager.PackageContext`, ship as agent-token artifacts,
|
||||
and `docker build` on the node (`build_image` command). Remote rollbacks
|
||||
`docker save` → artifact → `load_image` on the node. Verified end-to-end
|
||||
on a live agent (PR #60).
|
||||
- [ ] **IaC provisioning (ROADMAP §2.2, unchanged)** — Terraform/
|
||||
OpenTofu, `infra_connections`, Proxmox + AWS modules, + Hetzner module
|
||||
(dflow's second provider), cloud-init → auto-enroll.
|
||||
- [x] **Global/wildcard domain per node** (`node_agents.default_domain`) —
|
||||
`PUT /agents/:id {default_domain}`; domainless services placed on the
|
||||
node get `<name>.<default_domain>` (pinned → node's base; spread →
|
||||
union of eligible nodes). `serviceDomainNames` resolves them so
|
||||
routing, wake placeholders, and `public_url` all pick them up.
|
||||
CLI `nodes update --domain`, MCP arg, node-detail editor.
|
||||
|
||||
## 8. Phase G — Platform polish
|
||||
|
||||
- [x] **Outbound webhooks** — `outbound_webhooks(url, secret, events[],
|
||||
headers, enabled)` + `webhook_deliveries` log; HMAC
|
||||
`X-Containr-Signature`; wildcard events on every audited action;
|
||||
CLI + MCP + `/settings/webhooks` page with delivery log + test ping.
|
||||
- [x] **Activity feed** — `audit_logs` enriched with `severity`
|
||||
(`info|success|warning|error`), `category`, human `label`; derived at
|
||||
write time, classified on read for legacy rows, boot-time backfill.
|
||||
`GET /activity` (global, visibility-scoped, filters + `since`) and
|
||||
`GET /projects/:id/activity` (owner/member/approved/admin). Frontend:
|
||||
`/activity` page with severity chips + project workspace `Activity`
|
||||
view. `containr activity` + `containr_activity_list`/`containr_project_activity`
|
||||
MCP tools. Retention worker prunes > `AUDIT_RETENTION_DAYS` (default 90).
|
||||
- [x] **Banners** — `banners` table with title/body/level/active/
|
||||
dismissible/schedule window. `GET /banners/active` for signed-in
|
||||
users; admin CRUD at `/admin/banners`. `BannerBar` renders above the
|
||||
header (per-banner dismiss via localStorage); admin console manages
|
||||
them; `containr banners` CLI + `auth status` surfaces active ones.
|
||||
- [x] **White-label** — `app_settings` `branding.*` keys: product name,
|
||||
logo/favicon URL, accent color, docs/support links. Public
|
||||
`GET /api/v1/branding` (defaults = Containr); admin writes via
|
||||
`PUT /settings`. `useBranding()` applies title/accent/favicon;
|
||||
`BrandWordmark` replaces hardcoded marks; admin Platform section
|
||||
edits all fields. MCP `containr_branding_get` + `settings_update`.
|
||||
- [x] **Operations page** — `GET /operations` aggregates active
|
||||
deployments, in-memory deploy-queue depth (`Queue.Snapshot()`),
|
||||
24h failures, cron runs and backups scoped to the caller's projects.
|
||||
UI at `/operations` (Operate nav) with live refetch; `containr
|
||||
operations` CLI + `containr_operations_get` MCP. Remaining: agent
|
||||
commands once node fleet lands; flush/stuck-job tools.
|
||||
- [x] **Interactive terminal** — `GET /services/:id/terminal` upgrades
|
||||
to a WebSocket bridged to a `docker exec` PTY (bash→sh fallback,
|
||||
`TERM=xterm-256color`, resize frames). xterm.js Console section on the
|
||||
service page; `containr shell <id>` bridges a local TTY; distroless
|
||||
images get a clean "no shell" error. Remaining: `nodes/:id/terminal`
|
||||
via agent.
|
||||
- [x] **Team invites** — `POST /admin/invites` returns a one-time
|
||||
link (sha256-hashed token at rest, optional email binding, TTL);
|
||||
public `GET /auth/invites/:token` validates and
|
||||
`POST /auth/accept-invite` registers — works while public signup is
|
||||
closed. `/auth/accept-invite` page, admin manage/revoke UI,
|
||||
`containr invites` CLI + MCP tools.
|
||||
- [x] **Impersonate user** — `POST /admin/users/:id/impersonate`
|
||||
mints a 15-minute bearer token (`impersonated_by` claim),
|
||||
audit-logged; admin accounts are not impersonatable. CLI
|
||||
`admin impersonate`, MCP tool, admin UI token modal. Remaining:
|
||||
in-browser session swap needs a session bridge.
|
||||
- [x] **GitHub App manifest flow** — `POST /admin/git/github-app/manifest`
|
||||
returns manifest + submission URL; the callback page exchanges
|
||||
`code` via `POST /admin/git/github-app/convert` and credentials
|
||||
persist in `app_settings` (overriding `GITHUB_APP_*` env). New
|
||||
public `POST /api/git/github-app/webhook` receives App-level push
|
||||
events (HMAC-verified) and enqueues matching services. Settings→env
|
||||
fallback wired into install-URL + installation-token paths.
|
||||
`containr admin github-app` CLI + MCP + OpenAPI.
|
||||
- [x] **fork-sync trigger** — webhook receiver accepts `sync`,
|
||||
`repository_dispatch`, and `workflow_run` events (no `ref`) alongside
|
||||
pushes: branch falls back to the webhook filter, then the service's
|
||||
own `git_branch`; repo-only matching when no branch is known.
|
||||
Also fixed a latent bug where the webhook's deployment insert omitted
|
||||
the NOT NULL `version` column — push-triggered deploys silently
|
||||
failed for every service.
|
||||
- [x] **Dynamic filter framework** — ported dflow's `filter.utils`
|
||||
pattern as `lib/dynamic-filter.ts` + `use-dynamic-filter.ts`
|
||||
(`FilterConfig` schema → `FilterEngine` → `FilterBar`; state lives in
|
||||
`?f.<key>` search params so filtered views are shareable links).
|
||||
`useFilterState` exposes URL state pre-fetch for server-backed
|
||||
filters. Applied: audit logs (server-backed), builds (server + client
|
||||
date-range), activity (severity server + client category/search).
|
||||
Notifications table has no list surface yet — apply when one lands.
|
||||
- [x] **Notification producers round-out** — all shipped: deployments
|
||||
(deployments.go), backups (databases.go `createBackupProcess` → owner),
|
||||
agent offline/online sweep (`reconcileAgentHealth` → admins),
|
||||
upgrade-available (`checkForUpgrade`, tag-persisted) and security-scan
|
||||
findings (`scanner.OnScanComplete` → project owner).
|
||||
- [x] **Sleep-on-idle / wake-on-traffic** (serverless-lite) —
|
||||
`services.sleep_enabled` + `sleep_idle_minutes` (PR #29). Sweeper
|
||||
polls container NetworkIO deltas; idle past threshold → workload
|
||||
removed, status `sleeping`. Wake-on-traffic via per-service
|
||||
placeholder container on the edge network carrying the same Traefik
|
||||
host rule at higher priority — serves a waking page, calls
|
||||
`/internal/wake/:id`, exits; Traefik falls back once the app is up.
|
||||
Verified `running → sleeping → waking → running` end to end.
|
||||
- [x] **Env doctor** — `GET /services/:id/env-check` surfaces
|
||||
unresolved `${{…}}` refs, empty values, and unreadable
|
||||
(rotated-key) secrets; template `plan` endpoint reports missing
|
||||
required vars before deploy (PR #30). Frontend Variables section
|
||||
shows the banner.
|
||||
- [x] **Shared project variables** — `project_variables` table,
|
||||
`GET/PUT /projects/:id/variables`, `${{shared.KEY}}` refs resolved
|
||||
at deploy/reconcile, secrets `enc:v1:` at rest and masked (PR #32).
|
||||
- [x] **One-click install** — `install.sh` (curl|sh): detects docker,
|
||||
writes compose + .env with generated secrets incl. `SECRETS_KEY`,
|
||||
pulls images, runs migrations, prints admin URL. GHCR images mean
|
||||
installs never build from source.
|
||||
- [x] **CI/CD v2** — required checks (Go test/vet, frontend
|
||||
lint/build/test, compose boot smoke), `dorny/paths-filter` gate so
|
||||
docs-only PRs skip heavyweight jobs, and release on main/tag:
|
||||
conventional-commit version bump → tag → GHCR images (backend,
|
||||
frontend) → cross-platform CLI + MCP binaries → SHA-256 checksums
|
||||
→ GitHub Release → Trivy scans (PR #33).
|
||||
- [ ] **Custom roles/RBAC** — defer until demanded; PAT scopes designed
|
||||
forward-compatible now.
|
||||
|
||||
## 9. Phase H — Migration & ecosystem
|
||||
|
||||
- [x] **Railway migrator** — `containr migrate railway --token --project
|
||||
--environment --into --dry-run`: Railway GraphQL (backboard) →
|
||||
services (git-repo/image + command + replicas), variables, custom +
|
||||
service domains → recreates via the Containr API. Project tokens and
|
||||
account tokens both supported. `cli/commands/migrate.go`.
|
||||
- [x] **dflow importer** — `containr migrate dflow --url --api-key
|
||||
--project --into --dry-run`: pages the Payload REST collections
|
||||
(users API-Key auth) → services (app→git/web, docker→image,
|
||||
database→typed image + DATABASE_URL for external providers),
|
||||
variables (empty encrypted values flagged), bind volumes, domains.
|
||||
Soft-deleted services excluded. `cli/commands/migrate_dflow.go`.
|
||||
- [x] **`containr import compose`** — `POST /projects/:id/import-compose`
|
||||
deploys a compose file straight into a project; CLI `import compose`,
|
||||
MCP tool, workspace import modal; project delete cascades to
|
||||
graph-provisioned managed databases (`database_services.project_id`).
|
||||
- [x] **Template registry** — `CONTAINR_TEMPLATE_REGISTRY_URL` merges a
|
||||
remote Containr catalog into `GET /templates` as `source:"registry"`
|
||||
(60s cache, dead registry never blocks local). `GET /templates/:id`
|
||||
falls through to the registry; deploy materializes a deterministic
|
||||
`reg-<hash>` private copy for the deployer so re-deploys don't dup.
|
||||
`api/templateregistry.go`, `.env.example`.
|
||||
|
||||
## 9b. Phase I — Companion surfaces
|
||||
|
||||
- [x] **Phone app (v1)** — `app/mobile`, Expo/React Native thin client
|
||||
over the agent-shaped API; dark UI mirroring the design system. PAT
|
||||
auth against any instance URL, stored in platform secure storage.
|
||||
- **Dashboard** — service status counts (running/sleeping/failed/
|
||||
stopped), per-project service health dots, recent deployments feed.
|
||||
- **Operate** — start/stop/restart/redeploy, sleep/wake, rollback to
|
||||
a prior deployment, database start/stop/restart/backup-now.
|
||||
- **Observe** — polling log tail (300 lines, 5s refresh), per-service
|
||||
deployment history, env-doctor issues surfaced on the service view.
|
||||
- [x] **env var edit + domain management + replica scaling** (PR #61 —
|
||||
mobile v1.5: variables editor, domains screen, scale + sleep
|
||||
settings, notifications tab, cron jobs screen with run-now and
|
||||
execution history).
|
||||
- [x] **Push notifications** — per-user `notification_channels`
|
||||
(ntfy + Gotify): every `insertUserNotification` fans out best-effort
|
||||
to enabled channels; CRUD + live test-ping via web settings, CLI
|
||||
`notifications channels`, and MCP. FCM/APNs remain optional.
|
||||
- [x] **Offline resilience** — AsyncStorage-persisted query cache
|
||||
(7d, high-churn keys excluded) + banner when the instance is
|
||||
unreachable (`/health` ping + expo-network).
|
||||
- [x] **Status/offline resilience** — web persists the query cache to
|
||||
localStorage (24h) and PlatformShell shows a "last-known data" banner
|
||||
on offline/unreachable; CLI exits `ExitUnavailable` with a clean
|
||||
"cannot reach" message (no stack traces).
|
||||
|
||||
## 10. Carried-over debt (ROADMAP.md items still open)
|
||||
|
||||
- [x] **Gateway traffic path** — `/g/:slug/*` mounted → api_key auth →
|
||||
rpm + monthly quota → `gateway/proxy.go` → usage_counters /
|
||||
metrics_timeseries / incident_events.
|
||||
- [x] **Preview environments runtime** — previews are real service clones:
|
||||
branch override, queued deployment, node-domain hostnames, TTL sweeper,
|
||||
promote = redeploy of the source at the preview branch.
|
||||
- [x] **Environments** — CRUD (`GET/POST /projects/:id/environments`,
|
||||
`DELETE /environments/:id`), switcher chips with service counts in the
|
||||
workspace, CLI `environments` + MCP parity. Per-env vars/domains ride the
|
||||
service's own scope (one env per service); promote ships via preview
|
||||
promote.
|
||||
- [x] **sqlc port** — all static `database/sql` call sites → `sqlc/queries/` + generated `sqlcdb`. Merged: metrics + domains (#75), service clone/move (#76), preview environments (#77), deployments + rollback (#78), runtime ops (#79), cron (#80), sleep/wake sweeper (#81), git providers/repos/webhooks (#82), auth + security (#83), operations/settings/audit/activity/builds/misc + apwhy gateway (#84). Raw SQL remains only for runtime-assembled statements (filtered lists, sparse updates) and Better Auth `auth_users` probes.
|
||||
- [ ] **Tests** — canvas/wizard frontend coverage, deployment/template/
|
||||
webhook table tests, Playwright e2e golden path.
|
||||
- [x] **UI/UX audit** — browser-driven pass over the whole app (PR #85):
|
||||
fixed the stuck connectivity banner (`pingServer` returned
|
||||
`undefined`), stale persisted sessions, fabricated observability
|
||||
metrics (now real `getServiceMetrics` aggregation), mobile overflow,
|
||||
Security blank state, deep-linkable service tabs, and a dozen
|
||||
polish items.
|
||||
- [x] **Design previews** — gitignored `/design` holds three on-brand
|
||||
HTML concepts (console, service detail, landing) on the product's
|
||||
token system with a live white-label accent switcher.
|
||||
- [x] **First-run wizard** — public `GET /setup/status` + authed
|
||||
`POST /setup/complete`; `/setup` page steps admin account → GitHub App
|
||||
manifest flow (callback returns to the wizard via a session marker) →
|
||||
optional Cloudflare tunnel token → done. Shell redirects signed-in
|
||||
users to `/setup` while `needs_setup`; installs with existing projects
|
||||
auto-complete so nobody gets nagged retroactively.
|
||||
- [x] **Self-upgrade loop verification** — live-verified: admin
|
||||
`POST /agents/:id/upgrade` → `self_upgrade` command → agent downloads
|
||||
`/api/agents/download/linux-amd64` → sha256 verify → `--version`
|
||||
sanity → atomic rename → result posts → `syscall.Exec` re-exec. Agent
|
||||
ran `v0.0.1-old`, came back `v9.9.9-test`, command row `completed`.
|
||||
- [ ] **Docs freshness, v1.0.0 release.**
|
||||
|
||||
## 11. Deliberately not imported from dflow
|
||||
|
||||
- Dokku-over-SSH runtime — our Docker API + agent model is strictly
|
||||
better.
|
||||
- Per-server-named BullMQ queues — our queue stays a single ordered
|
||||
table.
|
||||
- Payload/Mongo/Next.js stack, multi-tenant plugin machinery.
|
||||
- Ansible playbook runner — arbitrary code-on-node is a security
|
||||
surface; SSH bootstrap + agent commands cover the real needs.
|
||||
Revisit only with a concrete demand.
|
||||
- `isAdmin`-only collection access — our owner/admin/public model is
|
||||
richer already.
|
||||
- Netdata install flow — our agent telemetry + optional Beszel covers
|
||||
monitoring; don't ship a third dashboard stack.
|
||||
|
||||
## 12. Recommended order
|
||||
|
||||
```
|
||||
A (agent surface) ──► B (deploy pipeline) ──► C/D (service+data depth)
|
||||
│ │
|
||||
▼ ▼
|
||||
CLI/MCP dogfood every feature E (templates v2)
|
||||
│
|
||||
F (nodes/infra) ──► H (migrators)
|
||||
│
|
||||
G (polish) + debt
|
||||
```
|
||||
|
||||
Why this order: PATs+CLI+MCP first = every later feature ships
|
||||
user-and-agent-visible at once. The queue (B) is prerequisite for
|
||||
the ops surface and correct concurrency. Templates v2 needs volumes,
|
||||
multi-domain, clone. Migrators last — they're launch marketing, not
|
||||
foundation.
|
||||
|
||||
## 13. Design rules
|
||||
|
||||
- **API is the contract.** UI, CLI, MCP are thin clients over
|
||||
`openapi.yaml`. A feature isn't done until all three reach it.
|
||||
- **Everything scriptable.** JSON in/out, `--json`/`--yes`, uniform
|
||||
pagination, name-or-id addressing.
|
||||
- **Same powers, same position.** Nothing user-facing that isn't
|
||||
API-facing; agent-visible state (tokens, jobs) visible to users too.
|
||||
- **dflow is a donor, not a dependency.** Port semantics, not code.
|
||||
- **Containr branding is the default**; white-label is instance config.
|
||||
- **Self-hosted first** — no required external service, no phone-home.
|
||||
+14
-5
@@ -1,5 +1,7 @@
|
||||
# Build Go API binary
|
||||
FROM golang:1.26-alpine AS go-builder
|
||||
ARG GO_IMAGE=golang:1.26-alpine
|
||||
ARG NODE_IMAGE=node:22-alpine
|
||||
FROM ${GO_IMAGE} AS go-builder
|
||||
|
||||
RUN apk add --no-cache git ca-certificates tzdata
|
||||
|
||||
@@ -9,10 +11,16 @@ COPY go.mod go.sum ./
|
||||
RUN go mod download
|
||||
|
||||
COPY . .
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo -o main cmd/server/main.go
|
||||
ARG VERSION=dev
|
||||
RUN CGO_ENABLED=0 GOOS=linux go build -a -installsuffix cgo \
|
||||
-ldflags "-X containr/internal/api.Version=${VERSION}" -o main cmd/server/main.go
|
||||
|
||||
# Node-agent binaries served by the API for SSH/bootstrap installs.
|
||||
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -ldflags "-X main.version=${VERSION}" -o /agents/containr-agent-linux-amd64 ./cmd/agent && \
|
||||
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 go build -ldflags "-X main.version=${VERSION}" -o /agents/containr-agent-linux-arm64 ./cmd/agent
|
||||
|
||||
# Build embedded Better Auth runtime dependencies
|
||||
FROM node:22-alpine AS auth-builder
|
||||
FROM ${NODE_IMAGE} AS auth-builder
|
||||
|
||||
WORKDIR /auth
|
||||
|
||||
@@ -22,9 +30,9 @@ RUN npm ci --omit=dev
|
||||
COPY auth/src ./src
|
||||
|
||||
# Final runtime image (Go API + Better Auth sidecar in one container)
|
||||
FROM node:22-alpine
|
||||
FROM ${NODE_IMAGE}
|
||||
|
||||
RUN apk --no-cache add ca-certificates tzdata wget
|
||||
RUN apk --no-cache add ca-certificates tzdata wget git
|
||||
|
||||
RUN addgroup -g 1001 -S appgroup && \
|
||||
adduser -u 1001 -S appuser -G appgroup
|
||||
@@ -33,6 +41,7 @@ WORKDIR /app
|
||||
|
||||
COPY --from=go-builder /app/main ./main
|
||||
COPY --from=go-builder /app/migrations_goose ./migrations_goose
|
||||
COPY --from=go-builder /agents /agents
|
||||
COPY --from=auth-builder /auth ./auth
|
||||
|
||||
RUN chown -R appuser:appgroup /app
|
||||
|
||||
@@ -3,14 +3,18 @@ package main
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"os"
|
||||
"os/exec"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"strconv"
|
||||
"strings"
|
||||
@@ -104,7 +108,15 @@ type registerResponse struct {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
// version is stamped at build time via -ldflags "-X main.version=…".
|
||||
// The server reports it on upgrades so agents can show what they run.
|
||||
var version = "dev"
|
||||
|
||||
func main() {
|
||||
if len(os.Args) > 1 && (os.Args[1] == "--version" || os.Args[1] == "version") {
|
||||
fmt.Printf("containr-agent %s\n", version)
|
||||
return
|
||||
}
|
||||
cfg, err := loadConfig()
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
@@ -167,6 +179,9 @@ func register(ctx context.Context, client *http.Client, cfg agentConfig) (string
|
||||
"ip_address": cfg.IP,
|
||||
"port": cfg.Port,
|
||||
"capabilities": detectCapabilities(),
|
||||
"version": firstEnv("CONTAINR_AGENT_VERSION", version),
|
||||
// Always send mesh so a removed interface clears stored state.
|
||||
"mesh": detectMeshInterfaces(),
|
||||
}
|
||||
var response registerResponse
|
||||
if err := postJSON(ctx, client, cfg, "/api/agents/register", payload, &response); err != nil {
|
||||
@@ -188,11 +203,93 @@ func sendHeartbeat(ctx context.Context, client *http.Client, cfg agentConfig, ag
|
||||
"container_count": dockerContainerCount(),
|
||||
"system_load": readSystemLoad(),
|
||||
"uptime": readSystemUptime(),
|
||||
"version": firstEnv("CONTAINR_AGENT_VERSION", "dev"),
|
||||
"version": firstEnv("CONTAINR_AGENT_VERSION", version),
|
||||
"containers": managedContainerStates(),
|
||||
}
|
||||
return postJSON(ctx, client, cfg, "/api/agents/heartbeat", payload, nil)
|
||||
}
|
||||
|
||||
// managedContainerStates lists every containr-managed container's docker
|
||||
// state plus cumulative network counters — state keeps runtime honest,
|
||||
// net bytes let the sleeper detect idle remote services.
|
||||
func managedContainerStates() []map[string]interface{} {
|
||||
out, err := runDocker("ps", "-a",
|
||||
"--filter", "label=containr.managed=true",
|
||||
"--format", "{{.Names}}\t{{.State}}")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
netIO := containerNetIO()
|
||||
var states []map[string]interface{}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
parts := strings.SplitN(line, "\t", 2)
|
||||
if len(parts) != 2 || parts[0] == "" {
|
||||
continue
|
||||
}
|
||||
entry := map[string]interface{}{"name": parts[0], "state": parts[1]}
|
||||
if io, ok := netIO[parts[0]]; ok {
|
||||
entry["rx_bytes"], entry["tx_bytes"] = io[0], io[1]
|
||||
}
|
||||
states = append(states, entry)
|
||||
}
|
||||
return states
|
||||
}
|
||||
|
||||
// containerNetIO snapshots cumulative rx/tx per running container. Stats
|
||||
// only covers running containers — stopped replicas report state without
|
||||
// counters, which is all the sleeper needs.
|
||||
func containerNetIO() map[string][2]int64 {
|
||||
out, err := runDocker("stats", "--no-stream",
|
||||
"--format", "{{.Name}}\t{{.NetIO}}")
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
result := map[string][2]int64{}
|
||||
for _, line := range strings.Split(out, "\n") {
|
||||
parts := strings.SplitN(line, "\t", 2)
|
||||
if len(parts) != 2 {
|
||||
continue
|
||||
}
|
||||
io := strings.Split(parts[1], "/")
|
||||
if len(io) != 2 {
|
||||
continue
|
||||
}
|
||||
result[parts[0]] = [2]int64{parseHumanBytes(io[0]), parseHumanBytes(io[1])}
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
// parseHumanBytes converts docker's human units ("1.2kB", "3.4MB") to bytes.
|
||||
func parseHumanBytes(s string) int64 {
|
||||
s = strings.TrimSpace(s)
|
||||
var num float64
|
||||
var unit string
|
||||
if _, err := fmt.Sscanf(s, "%f%s", &num, &unit); err != nil {
|
||||
return 0
|
||||
}
|
||||
switch strings.ToUpper(unit) {
|
||||
case "B":
|
||||
return int64(num)
|
||||
case "KB":
|
||||
return int64(num * 1e3)
|
||||
case "MB":
|
||||
return int64(num * 1e6)
|
||||
case "GB":
|
||||
return int64(num * 1e9)
|
||||
case "TB":
|
||||
return int64(num * 1e12)
|
||||
case "KIB":
|
||||
return int64(num * 1024)
|
||||
case "MIB":
|
||||
return int64(num * 1024 * 1024)
|
||||
case "GIB":
|
||||
return int64(num * 1024 * 1024 * 1024)
|
||||
case "TIB":
|
||||
return int64(num * 1024 * 1024 * 1024 * 1024)
|
||||
}
|
||||
return int64(num)
|
||||
}
|
||||
|
||||
func processCommands(ctx context.Context, client *http.Client, cfg agentConfig, agentID string) error {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, fmt.Sprintf("%s/api/agents/%s/commands", cfg.APIURL, agentID), nil)
|
||||
if err != nil {
|
||||
@@ -214,7 +311,13 @@ func processCommands(ctx context.Context, client *http.Client, cfg agentConfig,
|
||||
return err
|
||||
}
|
||||
for _, item := range list.Commands {
|
||||
result, runErr := executeCommand(item)
|
||||
var result, reexecPath string
|
||||
var runErr error
|
||||
if item.Type == "self_upgrade" {
|
||||
result, reexecPath, runErr = selfUpgrade(cfg, item.Payload)
|
||||
} else {
|
||||
result, runErr = executeCommand(cfg, item)
|
||||
}
|
||||
status := "completed"
|
||||
errText := ""
|
||||
if runErr != nil {
|
||||
@@ -230,11 +333,17 @@ func processCommands(ctx context.Context, client *http.Client, cfg agentConfig,
|
||||
if err := postJSON(ctx, client, cfg, path, payload, nil); err != nil {
|
||||
log.Printf("report command result failed: command=%s err=%v", item.ID, err)
|
||||
}
|
||||
// Re-exec AFTER the result posts — the new binary picks up the loop.
|
||||
if reexecPath != "" {
|
||||
if err := syscall.Exec(reexecPath, os.Args, os.Environ()); err != nil {
|
||||
log.Fatalf("re-exec after upgrade failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func executeCommand(item command) (string, error) {
|
||||
func executeCommand(cfg agentConfig, item command) (string, error) {
|
||||
switch item.Type {
|
||||
case "create_container":
|
||||
return createContainer(item.Payload)
|
||||
@@ -245,12 +354,100 @@ func executeCommand(item command) (string, error) {
|
||||
case "restart_container":
|
||||
return runDocker("restart", commandTarget(item.Payload))
|
||||
case "remove_container":
|
||||
return runDocker("rm", "-f", commandTarget(item.Payload))
|
||||
out, err := runDocker("rm", "-f", commandTarget(item.Payload))
|
||||
// A missing container is the desired end state — report success.
|
||||
if err != nil && strings.Contains(out, "No such container") {
|
||||
return out, nil
|
||||
}
|
||||
return out, err
|
||||
case "build_image":
|
||||
return buildImage(cfg, item.Payload)
|
||||
case "load_image":
|
||||
return loadImage(cfg, item.Payload)
|
||||
case "prune":
|
||||
return dockerPrune(item.Payload)
|
||||
case "system_df":
|
||||
return runDocker("system", "df", "--format", "json")
|
||||
default:
|
||||
return "", fmt.Errorf("unsupported command type %q", item.Type)
|
||||
}
|
||||
}
|
||||
|
||||
// selfUpgrade downloads the agent binary the server bundles for this
|
||||
// platform, verifies it, swaps it in atomically, and returns the path to
|
||||
// re-exec. On any failure the running binary is left untouched.
|
||||
func selfUpgrade(cfg agentConfig, payload map[string]interface{}) (string, string, error) {
|
||||
platform := firstString(stringValue(payload["platform"]), runtime.GOOS+"-"+runtime.GOARCH)
|
||||
|
||||
exe, err := os.Executable()
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("resolve own path: %w", err)
|
||||
}
|
||||
if resolved, err := filepath.EvalSymlinks(exe); err == nil {
|
||||
exe = resolved
|
||||
}
|
||||
|
||||
url := fmt.Sprintf("%s/api/agents/download/%s", cfg.APIURL, platform)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return "", "", err
|
||||
}
|
||||
resp, err := (&http.Client{Timeout: 2 * time.Minute}).Do(req)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("download %s: %w", platform, err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return "", "", fmt.Errorf("download %s: server returned %d", platform, resp.StatusCode)
|
||||
}
|
||||
|
||||
newPath := exe + ".new"
|
||||
out, err := os.OpenFile(newPath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0o755)
|
||||
if err != nil {
|
||||
return "", "", fmt.Errorf("write %s: %w", newPath, err)
|
||||
}
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(io.MultiWriter(out, h), resp.Body); err != nil {
|
||||
out.Close()
|
||||
os.Remove(newPath)
|
||||
return "", "", fmt.Errorf("download write: %w", err)
|
||||
}
|
||||
out.Close()
|
||||
|
||||
if want := stringValue(payload["sha256"]); want != "" {
|
||||
if got := hex.EncodeToString(h.Sum(nil)); got != want {
|
||||
os.Remove(newPath)
|
||||
return "", "", fmt.Errorf("sha256 mismatch: got %s want %s", got, want)
|
||||
}
|
||||
}
|
||||
|
||||
// Sanity: the new binary must at least report its version.
|
||||
if verOut, err := exec.Command(newPath, "--version").CombinedOutput(); err != nil {
|
||||
os.Remove(newPath)
|
||||
return "", "", fmt.Errorf("new binary failed sanity check: %s", strings.TrimSpace(string(verOut)))
|
||||
}
|
||||
|
||||
if err := os.Rename(newPath, exe); err != nil {
|
||||
os.Remove(newPath)
|
||||
return "", "", fmt.Errorf("swap binary: %w", err)
|
||||
}
|
||||
return fmt.Sprintf("upgraded to %s (%s)", firstString(stringValue(payload["version"]), "latest"), platform), exe, nil
|
||||
}
|
||||
|
||||
// dockerPrune runs a bounded docker system prune. Volumes are excluded
|
||||
// unless the payload explicitly opts in — a stray volume prune destroys
|
||||
// database data.
|
||||
func dockerPrune(payload map[string]interface{}) (string, error) {
|
||||
args := []string{"system", "prune", "-af"}
|
||||
if boolValue(payload["volumes"]) {
|
||||
args = append(args, "--volumes")
|
||||
}
|
||||
if until := stringValue(payload["until"]); until != "" {
|
||||
args = append(args, "--filter", fmt.Sprintf("until=%s", until))
|
||||
}
|
||||
return runDocker(args...)
|
||||
}
|
||||
|
||||
func createContainer(payload map[string]interface{}) (string, error) {
|
||||
container, ok := payload["container"].(map[string]interface{})
|
||||
if !ok {
|
||||
@@ -263,7 +460,38 @@ func createContainer(payload map[string]interface{}) (string, error) {
|
||||
return "", errors.New("container name and image are required")
|
||||
}
|
||||
|
||||
args := []string{"run", "-d", "--name", name, "--restart", "unless-stopped"}
|
||||
// Private registry: log in for the pull, then log back out so the
|
||||
// credential doesn't linger in the node's docker config.
|
||||
if reg, ok := container["registry"].(map[string]interface{}); ok {
|
||||
if user := stringValue(reg["username"]); user != "" {
|
||||
server := firstString(stringValue(reg["server"]), registryHostOf(image))
|
||||
login := exec.Command("docker", "login", server, "-u", user, "--password-stdin")
|
||||
login.Stdin = strings.NewReader(stringValue(reg["password"]))
|
||||
if out, err := login.CombinedOutput(); err != nil {
|
||||
return "", fmt.Errorf("registry login %s: %s", server, strings.TrimSpace(string(out)))
|
||||
}
|
||||
defer func() { _, _ = runDocker("logout", server) }()
|
||||
}
|
||||
}
|
||||
|
||||
// Redeploys reuse the deterministic name — clear the stale container
|
||||
// first so `run` never fails on a name conflict.
|
||||
_, _ = runDocker("rm", "-f", name)
|
||||
|
||||
args := []string{"run", "-d", "--name", name}
|
||||
restart := firstString(stringValue(container["restart_policy"]), "unless-stopped")
|
||||
args = append(args, "--restart", restart)
|
||||
if labels, ok := container["labels"].(map[string]interface{}); ok {
|
||||
for key, value := range labels {
|
||||
args = append(args, "--label", fmt.Sprintf("%s=%v", key, value))
|
||||
}
|
||||
}
|
||||
if mem := int64Value(container["memory"]); mem > 0 {
|
||||
args = append(args, "--memory", fmt.Sprintf("%d", mem))
|
||||
}
|
||||
if cpus := floatValue(container["cpus"]); cpus > 0 {
|
||||
args = append(args, "--cpus", fmt.Sprintf("%g", cpus))
|
||||
}
|
||||
if env, ok := container["environment"].(map[string]interface{}); ok {
|
||||
for key, value := range env {
|
||||
args = append(args, "-e", fmt.Sprintf("%s=%v", key, value))
|
||||
@@ -278,8 +506,14 @@ func createContainer(payload map[string]interface{}) (string, error) {
|
||||
hostPort := intValue(port["host_port"])
|
||||
containerPort := intValue(port["container_port"])
|
||||
protocol := firstString(stringValue(port["protocol"]), "tcp")
|
||||
if hostPort > 0 && containerPort > 0 {
|
||||
if containerPort <= 0 {
|
||||
continue
|
||||
}
|
||||
if hostPort > 0 {
|
||||
args = append(args, "-p", fmt.Sprintf("%d:%d/%s", hostPort, containerPort, protocol))
|
||||
} else {
|
||||
// Ephemeral host port — docker picks the binding.
|
||||
args = append(args, "-p", fmt.Sprintf("%d/%s", containerPort, protocol))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -301,7 +535,165 @@ func createContainer(payload map[string]interface{}) (string, error) {
|
||||
}
|
||||
}
|
||||
args = append(args, image)
|
||||
return runDocker(args...)
|
||||
if cmd, ok := container["command"].([]interface{}); ok {
|
||||
for _, arg := range cmd {
|
||||
args = append(args, stringValue(arg))
|
||||
}
|
||||
}
|
||||
if _, err := runDocker(args...); err != nil {
|
||||
return "", err
|
||||
}
|
||||
// docker run -d returns before the entrypoint fails — a crash-looping
|
||||
// service would be reported healthy. Give it a moment, then verify.
|
||||
time.Sleep(1500 * time.Millisecond)
|
||||
state, _ := runDocker("inspect", "-f", "{{.State.Status}}", name)
|
||||
if state != "running" {
|
||||
logs, _ := runDocker("logs", "--tail", "5", name)
|
||||
return "", fmt.Errorf("container %s is %q after start: %s", name, state, logs)
|
||||
}
|
||||
// Report the real host port bindings — ephemeral publishes pick a port
|
||||
// the server can't guess, and remote ingress needs the actual mapping.
|
||||
hostPorts := map[string]string{}
|
||||
portsJSON, _ := runDocker("inspect", "-f", "{{json .NetworkSettings.Ports}}", name)
|
||||
var bindings map[string][]struct {
|
||||
HostIP string `json:"HostIp"`
|
||||
HostPort string `json:"HostPort"`
|
||||
}
|
||||
if json.Unmarshal([]byte(portsJSON), &bindings) == nil {
|
||||
for cport, addrs := range bindings {
|
||||
for _, a := range addrs {
|
||||
if a.HostPort != "" && a.HostPort != "0" {
|
||||
hostPorts[cport] = a.HostPort
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
out, _ := json.Marshal(map[string]interface{}{"name": name, "host_ports": hostPorts})
|
||||
return string(out), nil
|
||||
}
|
||||
|
||||
// fetchArtifact downloads a server-side deploy artifact (build context or
|
||||
// docker-save tarball) into a temp file the docker CLI consumes.
|
||||
func fetchArtifact(cfg agentConfig, artifactID string) (*os.File, error) {
|
||||
if len(artifactID) != 32 {
|
||||
return nil, fmt.Errorf("invalid artifact id")
|
||||
}
|
||||
url := fmt.Sprintf("%s/api/agents/artifacts/%s", cfg.APIURL, artifactID)
|
||||
req, err := http.NewRequest(http.MethodGet, url, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("X-Containr-Agent-Token", cfg.Token)
|
||||
resp, err := (&http.Client{Timeout: 10 * time.Minute}).Do(req)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("download artifact: %w", err)
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("download artifact: server returned %d", resp.StatusCode)
|
||||
}
|
||||
f, err := os.CreateTemp("", "containr-artifact-*.tar")
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if _, err := io.Copy(f, resp.Body); err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
return nil, fmt.Errorf("write artifact: %w", err)
|
||||
}
|
||||
if _, err := f.Seek(0, io.SeekStart); err != nil {
|
||||
f.Close()
|
||||
os.Remove(f.Name())
|
||||
return nil, err
|
||||
}
|
||||
return f, nil
|
||||
}
|
||||
|
||||
// buildImage docker-builds a packaged context tar on this node. The context
|
||||
// already carries its Dockerfile at the tar root, so `docker build -` can
|
||||
// stream it straight from stdin. Result reports the image digest so the
|
||||
// server records what each node actually produced.
|
||||
func buildImage(cfg agentConfig, payload map[string]interface{}) (string, error) {
|
||||
image := stringValue(payload["image"])
|
||||
artifactID := stringValue(payload["artifact_id"])
|
||||
if image == "" || artifactID == "" {
|
||||
return "", errors.New("build_image requires image and artifact_id")
|
||||
}
|
||||
f, err := fetchArtifact(cfg, artifactID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
|
||||
args := []string{"build", "-t", image}
|
||||
if boolValue(payload["no_cache"]) {
|
||||
args = append(args, "--no-cache")
|
||||
}
|
||||
// Bare --build-arg keys read their values from the process env — keeps
|
||||
// token-bearing args out of the node's process list.
|
||||
env := os.Environ()
|
||||
if buildArgs, ok := payload["build_args"].(map[string]interface{}); ok {
|
||||
for k, v := range buildArgs {
|
||||
args = append(args, "--build-arg", k)
|
||||
env = append(env, fmt.Sprintf("%s=%v", k, v))
|
||||
}
|
||||
}
|
||||
args = append(args, "-")
|
||||
cmd := exec.Command("docker", args...)
|
||||
cmd.Stdin = f
|
||||
cmd.Env = env
|
||||
outBytes, err := cmd.CombinedOutput()
|
||||
out := strings.TrimSpace(string(outBytes))
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("docker build failed: %s", tailOutput(out, 4096))
|
||||
}
|
||||
digest, _ := runDocker("inspect", "-f", "{{.Id}}", image)
|
||||
size, _ := runDocker("inspect", "-f", "{{.Size}}", image)
|
||||
result, _ := json.Marshal(map[string]string{
|
||||
"image": image, "id": digest, "size": size,
|
||||
"log_tail": tailOutput(out, 4096),
|
||||
})
|
||||
return string(result), nil
|
||||
}
|
||||
|
||||
// loadImage imports a docker-save tarball — rollbacks of locally-built tags
|
||||
// reach remote nodes this way since no registry serves containr-* images.
|
||||
func loadImage(cfg agentConfig, payload map[string]interface{}) (string, error) {
|
||||
artifactID := stringValue(payload["artifact_id"])
|
||||
if artifactID == "" {
|
||||
return "", errors.New("load_image requires artifact_id")
|
||||
}
|
||||
f, err := fetchArtifact(cfg, artifactID)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
defer os.Remove(f.Name())
|
||||
out, err := runDockerStdin(f, "load")
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("docker load failed: %s", tailOutput(out, 4096))
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// tailOutput bounds command results — full docker build logs would swamp
|
||||
// the agent_commands row.
|
||||
func tailOutput(s string, max int) string {
|
||||
if len(s) <= max {
|
||||
return s
|
||||
}
|
||||
return s[len(s)-max:]
|
||||
}
|
||||
|
||||
// registryHostOf mirrors the server's registryHost: first path segment with
|
||||
// a "." or ":" is a registry host; bare names mean Docker Hub.
|
||||
func registryHostOf(imageRef string) string {
|
||||
parts := strings.Split(imageRef, "/")
|
||||
if len(parts) > 1 && (strings.Contains(parts[0], ".") || strings.Contains(parts[0], ":")) {
|
||||
return parts[0]
|
||||
}
|
||||
return "docker.io"
|
||||
}
|
||||
|
||||
func commandTarget(payload map[string]interface{}) string {
|
||||
@@ -354,6 +746,42 @@ func detectCapabilities() capabilities {
|
||||
}
|
||||
}
|
||||
|
||||
// detectMeshInterfaces reports overlay-network interface IPs (tailscale,
|
||||
// netbird, zerotier, wireguard) so the backend can display reachable
|
||||
// mesh addresses alongside the primary IP.
|
||||
func detectMeshInterfaces() map[string]string {
|
||||
ifaces, err := net.Interfaces()
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
mesh := map[string]string{}
|
||||
for _, iface := range ifaces {
|
||||
name := strings.ToLower(iface.Name)
|
||||
isMesh := strings.HasPrefix(name, "tailscale") || strings.HasPrefix(name, "ts.") ||
|
||||
strings.HasPrefix(name, "netbird") || strings.HasPrefix(name, "wt") ||
|
||||
strings.HasPrefix(name, "zt") || strings.HasPrefix(name, "wg")
|
||||
if !isMesh || iface.Flags&net.FlagUp == 0 {
|
||||
continue
|
||||
}
|
||||
addrs, err := iface.Addrs()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
for _, addr := range addrs {
|
||||
ip, _, err := net.ParseCIDR(addr.String())
|
||||
if err != nil || ip.IsLoopback() {
|
||||
continue
|
||||
}
|
||||
if v4 := ip.To4(); v4 != nil {
|
||||
mesh[iface.Name] = v4.String()
|
||||
} else if _, exists := mesh[iface.Name]; !exists {
|
||||
mesh[iface.Name] = ip.String()
|
||||
}
|
||||
}
|
||||
}
|
||||
return mesh
|
||||
}
|
||||
|
||||
func collectResources(ip string) nodeResources {
|
||||
mem := readMemory()
|
||||
storage := readStorage("/")
|
||||
@@ -469,6 +897,15 @@ func runDocker(args ...string) (string, error) {
|
||||
return strings.TrimSpace(string(output)), err
|
||||
}
|
||||
|
||||
// runDockerStdin pipes stdin into docker — build contexts and saved image
|
||||
// tarballs stream in without a second copy on disk.
|
||||
func runDockerStdin(stdin io.Reader, args ...string) (string, error) {
|
||||
cmd := exec.Command("docker", args...)
|
||||
cmd.Stdin = stdin
|
||||
output, err := cmd.CombinedOutput()
|
||||
return strings.TrimSpace(string(output)), err
|
||||
}
|
||||
|
||||
func dockerAvailable() bool {
|
||||
_, err := exec.LookPath("docker")
|
||||
if err != nil {
|
||||
@@ -564,3 +1001,35 @@ func boolValue(value interface{}) bool {
|
||||
typed, _ := value.(bool)
|
||||
return typed
|
||||
}
|
||||
|
||||
func int64Value(value interface{}) int64 {
|
||||
switch typed := value.(type) {
|
||||
case int64:
|
||||
return typed
|
||||
case int:
|
||||
return int64(typed)
|
||||
case float64:
|
||||
return int64(typed)
|
||||
case json.Number:
|
||||
out, _ := typed.Int64()
|
||||
return out
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
|
||||
func floatValue(value interface{}) float64 {
|
||||
switch typed := value.(type) {
|
||||
case float64:
|
||||
return typed
|
||||
case int:
|
||||
return float64(typed)
|
||||
case int64:
|
||||
return float64(typed)
|
||||
case json.Number:
|
||||
out, _ := typed.Float64()
|
||||
return out
|
||||
default:
|
||||
return 0
|
||||
}
|
||||
}
|
||||
@@ -15,8 +15,5 @@ var (
|
||||
|
||||
func main() {
|
||||
cli.SetVersion(fmt.Sprintf("%s (%s, %s)", Version, GitCommit, BuildTime))
|
||||
if err := cli.Run(); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "Error: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
os.Exit(cli.Execute())
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"os/signal"
|
||||
"strings"
|
||||
"syscall"
|
||||
|
||||
"containr/internal/mcpserver"
|
||||
)
|
||||
|
||||
// Injected at build time via -ldflags.
|
||||
var Version = "dev"
|
||||
|
||||
// containr-mcp exposes the whole Containr API as MCP tools over stdio.
|
||||
// Configure it in an MCP client with:
|
||||
//
|
||||
// command: containr-mcp
|
||||
// env: CONTAINR_API_URL, CONTAINR_TOKEN (a cnp_ personal access token)
|
||||
func main() {
|
||||
apiURL := os.Getenv("CONTAINR_API_URL")
|
||||
if apiURL == "" {
|
||||
apiURL = "http://localhost:8080/api/v1"
|
||||
}
|
||||
apiURL = strings.TrimSuffix(apiURL, "/")
|
||||
if !strings.HasSuffix(apiURL, "/api/v1") {
|
||||
apiURL += "/api/v1"
|
||||
}
|
||||
|
||||
ctx, stop := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
||||
defer stop()
|
||||
|
||||
if err := mcpserver.Run(ctx, apiURL, os.Getenv("CONTAINR_TOKEN"), Version); err != nil {
|
||||
fmt.Fprintf(os.Stderr, "containr-mcp: %v\n", err)
|
||||
os.Exit(1)
|
||||
}
|
||||
}
|
||||
+21
-5
@@ -8,11 +8,13 @@ require (
|
||||
github.com/gin-gonic/gin v1.9.1
|
||||
github.com/go-playground/validator/v10 v10.30.1
|
||||
github.com/go-redis/redis/v8 v8.11.5
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/gorilla/websocket v1.5.3
|
||||
github.com/lib/pq v1.10.9
|
||||
github.com/miekg/dns v1.1.72
|
||||
github.com/minio/minio-go/v7 v7.0.95
|
||||
github.com/modelcontextprotocol/go-sdk v1.7.0
|
||||
github.com/pressly/goose/v3 v3.24.1
|
||||
github.com/robfig/cron/v3 v3.0.1
|
||||
github.com/rs/cors v1.10.1
|
||||
@@ -21,6 +23,8 @@ require (
|
||||
github.com/sqlc-dev/pqtype v0.3.0
|
||||
github.com/stretchr/testify v1.11.1
|
||||
golang.org/x/crypto v0.57.0
|
||||
golang.org/x/term v0.46.0
|
||||
gopkg.in/yaml.v3 v3.0.1
|
||||
)
|
||||
|
||||
require (
|
||||
@@ -35,22 +39,28 @@ require (
|
||||
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
|
||||
github.com/distribution/reference v0.6.0 // indirect
|
||||
github.com/docker/go-units v0.5.0 // indirect
|
||||
github.com/dustin/go-humanize v1.0.1 // indirect
|
||||
github.com/felixge/httpsnoop v1.0.4 // indirect
|
||||
github.com/fsnotify/fsnotify v1.9.0 // indirect
|
||||
github.com/gabriel-vasile/mimetype v1.4.12 // indirect
|
||||
github.com/gin-contrib/sse v0.1.0 // indirect
|
||||
github.com/go-ini/ini v1.67.0 // indirect
|
||||
github.com/go-logr/logr v1.4.3 // indirect
|
||||
github.com/go-logr/stdr v1.2.2 // indirect
|
||||
github.com/go-playground/locales v0.14.1 // indirect
|
||||
github.com/go-playground/universal-translator v0.18.1 // indirect
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 // indirect
|
||||
github.com/goccy/go-json v0.10.2 // indirect
|
||||
github.com/goccy/go-json v0.10.5 // indirect
|
||||
github.com/google/jsonschema-go v0.4.3 // indirect
|
||||
github.com/inconshreveable/mousetrap v1.1.0 // indirect
|
||||
github.com/json-iterator/go v1.1.12 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
|
||||
github.com/klauspost/compress v1.18.0 // indirect
|
||||
github.com/klauspost/cpuid/v2 v2.2.11 // indirect
|
||||
github.com/leodido/go-urn v1.4.0 // indirect
|
||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||
github.com/mfridman/interpolate v0.0.2 // indirect
|
||||
github.com/minio/crc64nvme v1.0.2 // indirect
|
||||
github.com/minio/md5-simd v1.1.2 // indirect
|
||||
github.com/moby/docker-image-spec v1.3.1 // indirect
|
||||
github.com/moby/sys/atomicwriter v0.1.0 // indirect
|
||||
github.com/moby/term v0.5.2 // indirect
|
||||
@@ -60,17 +70,23 @@ require (
|
||||
github.com/opencontainers/go-digest v1.0.0 // indirect
|
||||
github.com/opencontainers/image-spec v1.1.1 // indirect
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 // indirect
|
||||
github.com/philhofer/fwd v1.2.0 // indirect
|
||||
github.com/pkg/errors v0.9.1 // indirect
|
||||
github.com/pmezard/go-difflib v1.0.0 // indirect
|
||||
github.com/rs/xid v1.6.0 // indirect
|
||||
github.com/sagikazarmark/locafero v0.11.0 // indirect
|
||||
github.com/segmentio/asm v1.2.0 // indirect
|
||||
github.com/segmentio/encoding v0.5.4 // indirect
|
||||
github.com/sethvargo/go-retry v0.3.0 // indirect
|
||||
github.com/sourcegraph/conc v0.3.1-0.20240121214520-5f936abd7ae8 // indirect
|
||||
github.com/spf13/afero v1.15.0 // indirect
|
||||
github.com/spf13/cast v1.10.0 // indirect
|
||||
github.com/spf13/pflag v1.0.10 // indirect
|
||||
github.com/subosito/gotenv v1.6.0 // indirect
|
||||
github.com/tinylib/msgp v1.3.0 // indirect
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
|
||||
github.com/ugorji/go/codec v1.2.11 // indirect
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 // indirect
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 // indirect
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 // indirect
|
||||
go.opentelemetry.io/otel v1.41.0 // indirect
|
||||
@@ -84,13 +100,13 @@ require (
|
||||
golang.org/x/arch v0.3.0 // indirect
|
||||
golang.org/x/mod v0.41.0 // indirect
|
||||
golang.org/x/net v0.59.0 // indirect
|
||||
golang.org/x/oauth2 v0.35.0 // indirect
|
||||
golang.org/x/sync v0.23.0 // indirect
|
||||
golang.org/x/sys v0.48.0 // indirect
|
||||
golang.org/x/text v0.42.0 // indirect
|
||||
golang.org/x/time v0.11.0 // indirect
|
||||
golang.org/x/time v0.15.0 // indirect
|
||||
golang.org/x/tools v0.49.0 // indirect
|
||||
google.golang.org/protobuf v1.36.11 // indirect
|
||||
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||
gotest.tools/v3 v3.5.2 // indirect
|
||||
modernc.org/sqlite v1.39.0 // indirect
|
||||
)
|
||||
+39
-9
@@ -46,6 +46,8 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
|
||||
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
|
||||
github.com/gin-gonic/gin v1.9.1 h1:4idEAncQnU5cB7BeOkPtxjfCSye0AAm1R0RVIqJ+Jmg=
|
||||
github.com/gin-gonic/gin v1.9.1/go.mod h1:hPrL7YrpYKXt5YId3A/Tnip5kqbEAP+KLuI3SUcPTeU=
|
||||
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
||||
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
|
||||
github.com/go-logr/logr v1.4.3 h1:CjnDlHq8ikf6E492q6eKboGOC0T8CDaOvkHCIg8idEI=
|
||||
github.com/go-logr/logr v1.4.3/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY=
|
||||
@@ -63,13 +65,15 @@ github.com/go-redis/redis/v8 v8.11.5 h1:AcZZR7igkdvfVmQTPnu9WE37LRrO/YrBH5zWyjDC
|
||||
github.com/go-redis/redis/v8 v8.11.5/go.mod h1:gREzHqY1hg6oD9ngVRbLStwAWKhA0FEgq8Jd4h5lpwo=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0 h1:EBsztssimR/CONLSZZ04E8qAkxNYq4Qp9LvH92wZUgs=
|
||||
github.com/go-viper/mapstructure/v2 v2.4.0/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||
github.com/goccy/go-json v0.10.2 h1:CrxCmQqYDkv1z7lO7Wbh2HN93uovUHgrECaO5ZrCXAU=
|
||||
github.com/goccy/go-json v0.10.2/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2 h1:Rl4B7itRWVtYIHFrSNd7vhTiz9UpLdi6gZhZ3wEeDy8=
|
||||
github.com/golang-jwt/jwt/v5 v5.2.2/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
|
||||
github.com/goccy/go-json v0.10.5 h1:Fq85nIqj+gXn/S5ahsiTlK3TmC85qgirsdTP/+DeaC4=
|
||||
github.com/goccy/go-json v0.10.5/go.mod h1:oq7eo15ShAhp70Anwd5lgX2pLfOS3QCiwU/PULtXL6M=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1 h1:kYf81DTWFe7t+1VvL7eS+jKFVWaUnK9cB1qbwn63YCY=
|
||||
github.com/golang-jwt/jwt/v5 v5.3.1/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/jsonschema-go v0.4.3 h1:/DBOLZTfDow7pe2GmaJNhltueGTtDKICi8V8p+DQPd0=
|
||||
github.com/google/jsonschema-go v0.4.3/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
|
||||
@@ -80,9 +84,12 @@ github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2
|
||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
|
||||
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
|
||||
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
|
||||
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
|
||||
github.com/klauspost/cpuid/v2 v2.0.1/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
|
||||
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
|
||||
github.com/klauspost/cpuid/v2 v2.2.11 h1:0OwqZRYI2rFrjS4kvkDnqJkKHdHaRnCm68/DY4OxRzU=
|
||||
github.com/klauspost/cpuid/v2 v2.2.11/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
|
||||
github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
|
||||
github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk=
|
||||
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||
@@ -97,6 +104,12 @@ github.com/mfridman/interpolate v0.0.2 h1:pnuTK7MQIxxFz1Gr+rjSIx9u7qVjf5VOoM/u6B
|
||||
github.com/mfridman/interpolate v0.0.2/go.mod h1:p+7uk6oE07mpE/Ik1b8EckO0O4ZXiGAfshKBWLUM9Xg=
|
||||
github.com/miekg/dns v1.1.72 h1:vhmr+TF2A3tuoGNkLDFK9zi36F2LS+hKTRW0Uf8kbzI=
|
||||
github.com/miekg/dns v1.1.72/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs=
|
||||
github.com/minio/crc64nvme v1.0.2 h1:6uO1UxGAD+kwqWWp7mBFsi5gAse66C4NXO8cmcVculg=
|
||||
github.com/minio/crc64nvme v1.0.2/go.mod h1:eVfm2fAzLlxMdUGc0EEBGSMmPwmXD5XiNRpnu9J3bvg=
|
||||
github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34=
|
||||
github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM=
|
||||
github.com/minio/minio-go/v7 v7.0.95 h1:ywOUPg+PebTMTzn9VDsoFJy32ZuARN9zhB+K3IYEvYU=
|
||||
github.com/minio/minio-go/v7 v7.0.95/go.mod h1:wOOX3uxS334vImCNRVyIDdXX9OsXDm89ToynKgqUKlo=
|
||||
github.com/moby/docker-image-spec v1.3.1 h1:jMKff3w6PgbfSa69GfNg+zN/XLhfXJGnEx3Nl2EsFP0=
|
||||
github.com/moby/docker-image-spec v1.3.1/go.mod h1:eKmb5VW8vQEh/BAr2yvVNvuiJuY6UIocYsFu/DxxRpo=
|
||||
github.com/moby/sys/atomicwriter v0.1.0 h1:kw5D/EqkBwsBFi0ss9v1VG3wIkVhzGvLklJ+w3A14Sw=
|
||||
@@ -105,6 +118,8 @@ github.com/moby/sys/sequential v0.6.0 h1:qrx7XFUd/5DxtqcoH1h438hF5TmOvzC/lspjy7z
|
||||
github.com/moby/sys/sequential v0.6.0/go.mod h1:uyv8EUTrca5PnDsdMGXhZe6CCe8U/UiTWd+lL+7b/Ko=
|
||||
github.com/moby/term v0.5.2 h1:6qk3FJAFDs6i/q3W/pQ97SX192qKfZgGjCQqfCJkgzQ=
|
||||
github.com/moby/term v0.5.2/go.mod h1:d3djjFCrjnB+fl8NJux+EJzu0msscUP+f8it8hPkFLc=
|
||||
github.com/modelcontextprotocol/go-sdk v1.7.0 h1:yqjY2dsbKAC0LSuWZVBMrHgiG8ukXv6NRo0JiALay44=
|
||||
github.com/modelcontextprotocol/go-sdk v1.7.0/go.mod h1:dL7u98E/zjJTGzEq+j30jQ8K2k1mb6LeAH4inEcSGts=
|
||||
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
|
||||
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
|
||||
@@ -126,6 +141,8 @@ github.com/opencontainers/image-spec v1.1.1 h1:y0fUlFfIZhPF1W537XOLg0/fcx6zcHCJw
|
||||
github.com/opencontainers/image-spec v1.1.1/go.mod h1:qpqAh3Dmcf36wStyyWU+kCeDgrGnAve2nCC8+7h8Q0M=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
|
||||
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
|
||||
github.com/philhofer/fwd v1.2.0 h1:e6DnBTl7vGY+Gz322/ASL4Gyp1FspeMvx1RNDoToZuM=
|
||||
github.com/philhofer/fwd v1.2.0/go.mod h1:RqIHx9QI14HlwKwm98g9Re5prTQ6LdeRQn+gXJFxsJM=
|
||||
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
|
||||
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
|
||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
@@ -140,9 +157,15 @@ github.com/rogpeppe/go-internal v1.14.1 h1:UQB4HGPB6osV0SQTLymcB4TgvyWu6ZyliaW0t
|
||||
github.com/rogpeppe/go-internal v1.14.1/go.mod h1:MaRKkUm5W0goXpeCfT7UZI6fk/L7L7so1lCWt35ZSgc=
|
||||
github.com/rs/cors v1.10.1 h1:L0uuZVXIKlI1SShY2nhFfo44TYvDPQ1w4oFkUJNfhyo=
|
||||
github.com/rs/cors v1.10.1/go.mod h1:XyqrcTp5zjWr1wsJ8PIRZssZ8b/WMcMf71DJnit4EMU=
|
||||
github.com/rs/xid v1.6.0 h1:fV591PaemRlL6JfRxGDEPl69wICngIQ3shQtzfy2gxU=
|
||||
github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0=
|
||||
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
|
||||
github.com/sagikazarmark/locafero v0.11.0 h1:1iurJgmM9G3PA/I+wWYIOw/5SyBtxapeHDcg+AAIFXc=
|
||||
github.com/sagikazarmark/locafero v0.11.0/go.mod h1:nVIGvgyzw595SUSUE6tvCp3YYTeHs15MvlmU87WwIik=
|
||||
github.com/segmentio/asm v1.2.0 h1:9BQrFxC+YOHJlTlHGkTrFWf59nbL3XnCoFLTwDCI7ys=
|
||||
github.com/segmentio/asm v1.2.0/go.mod h1:BqMnlJP91P8d+4ibuonYZw9mfnzI9HfxselHZr5aAcs=
|
||||
github.com/segmentio/encoding v0.5.4 h1:OW1VRern8Nw6ITAtwSZ7Idrl3MXCFwXHPgqESYfvNt0=
|
||||
github.com/segmentio/encoding v0.5.4/go.mod h1:HS1ZKa3kSN32ZHVZ7ZLPLXWvOVIiZtyJnO1gPH1sKt0=
|
||||
github.com/sethvargo/go-retry v0.3.0 h1:EEt31A35QhrcRZtrYFDTBg91cqZVnFL2navjDrah2SE=
|
||||
github.com/sethvargo/go-retry v0.3.0/go.mod h1:mNX17F0C/HguQMyMyJxcnU471gOZGxCLyYaFyAZraas=
|
||||
github.com/sirupsen/logrus v1.9.3 h1:dueUQJ1C2q9oE3F7wvmSGAaVtTmUizReu6fjN8uqzbQ=
|
||||
@@ -174,10 +197,14 @@ github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu
|
||||
github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U=
|
||||
github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8=
|
||||
github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU=
|
||||
github.com/tinylib/msgp v1.3.0 h1:ULuf7GPooDaIlbyvgAxBV/FI7ynli6LZ1/nVUNu+0ww=
|
||||
github.com/tinylib/msgp v1.3.0/go.mod h1:ykjzy2wzgrlvpDCRc4LA8UXy6D8bzMSuAF3WD57Gok0=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS4MhqMhdFk5YI=
|
||||
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
|
||||
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
|
||||
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=
|
||||
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1 h1:jXsnJ4Lmnqd11kwkBV2LgLoFMZKizbCi5fNZ/ipaZ64=
|
||||
go.opentelemetry.io/auto/sdk v1.2.1/go.mod h1:KRTj+aOaElaLi+wW1kO/DZRXwkF4C5xPbEe3ZiIhN7Y=
|
||||
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.60.0 h1:sbiXRNDSWJOTobXh5HyQKjq6wUC5tNybqjIqDpAY4CU=
|
||||
@@ -213,16 +240,19 @@ golang.org/x/mod v0.41.0 h1:qJmnOUb4YB+FsEuM3HcWucdZASCPGhsX6uljO6pog0c=
|
||||
golang.org/x/mod v0.41.0/go.mod h1:Ek9pY8RKWXwsWvd3rQiHYtMqkjSUV+s1Rj7j4H5Ur6o=
|
||||
golang.org/x/net v0.59.0 h1:5zfYln+w5XCxwrnMMJPufRgNoXEaGxl0wo5GqPXyues=
|
||||
golang.org/x/net v0.59.0/go.mod h1:2DA/G1UfVbCpQPeWTmMPGY7Cs2PkBkwu743bVX5PIVg=
|
||||
golang.org/x/oauth2 v0.35.0 h1:Mv2mzuHuZuY2+bkyWXIHMfhNdJAdwW3FuWeCPYN5GVQ=
|
||||
golang.org/x/oauth2 v0.35.0/go.mod h1:lzm5WQJQwKZ3nwavOZ3IS5Aulzxi68dUSgRHujetwEA=
|
||||
golang.org/x/sync v0.23.0 h1:KameEIfc1IkluZyXWLn39Wd4tURc6GbCiISGiZm2bQk=
|
||||
golang.org/x/sync v0.23.0/go.mod h1:sUUOizhqBxiL6pEWpqNLUiaJn1ShEbZ6BBqskPbjZm0=
|
||||
golang.org/x/sys v0.0.0-20220704084225-05e143d24a9e/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.48.0 h1:bbX/i/6MgT9BVLM9RT1thmxL04yeTAhbEz4SyadbXoo=
|
||||
golang.org/x/sys v0.48.0/go.mod h1:hNLxWAXmnKAxqDtdwIYC4bM9oQPEecfsnNMuSxOs3og=
|
||||
golang.org/x/term v0.46.0 h1:3+OXuTbaKDgwk8jTi3aSLHRlmWqHEUDUtxnbFigO4YE=
|
||||
golang.org/x/term v0.46.0/go.mod h1:+K02xbkittuwc0Am4abfA3Fc+XRGXkvBXNO88NCXPoc=
|
||||
golang.org/x/text v0.42.0 h1:JbOZXgfeCPU9gacVtYliJqOhD+zhrEqK4LfdpmlUZqI=
|
||||
golang.org/x/text v0.42.0/go.mod h1:ojzP1Z+2QtioaF8DTtO8K5q7JWVVYwZKenzujK0Zd0E=
|
||||
golang.org/x/time v0.11.0 h1:/bpjEDfN9tkoN/ryeYHnv5hcMlc8ncjMcM4XBk5NWV0=
|
||||
golang.org/x/time v0.11.0/go.mod h1:CDIdPxbZBQxdj6cxyCIdrNogrJKMJ7pr37NYpMcMDSg=
|
||||
golang.org/x/time v0.15.0 h1:bbrp8t3bGUeFOx08pvsMYRTCVSMk89u4tKbNOZbp88U=
|
||||
golang.org/x/time v0.15.0/go.mod h1:Y4YMaQmXwGQZoFaVFk4YpCt4FLQMYKZe9oeV/f4MSno=
|
||||
golang.org/x/tools v0.49.0 h1:3NI7VXzL9+1WZD52Dx2ttoPwD5DWrFGpl9mFZDlmisI=
|
||||
golang.org/x/tools v0.49.0/go.mod h1:SJNXV9DBKT0UbdttsQjbfJlAE/q+y36++zo3uL3N0Oo=
|
||||
google.golang.org/genproto/googleapis/api v0.0.0-20260128011058-8636f8732409 h1:merA0rdPeUV3YIIfHHcH4qBkiQAc1nfCKSI7lB4cV2M=
|
||||
|
||||
@@ -0,0 +1,291 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Activity feed — enriched audit_logs rows (severity/category/label) served
|
||||
// as a global feed and per-project timelines. Visibility mirrors the
|
||||
// deployment/operations scoping: own actions, plus events on resources
|
||||
// inside visible projects (approved / owned / member; admin sees all).
|
||||
|
||||
const activityVisibility = `(
|
||||
a.user_id = $1::uuid OR $2::bool
|
||||
OR (a.resource = 'project' AND a.resource_id IN (
|
||||
SELECT id FROM projects p WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||
OR (a.resource = 'service' AND a.resource_id IN (
|
||||
SELECT s.id FROM services s JOIN projects p ON p.id = s.project_id
|
||||
WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||
OR (a.resource = 'deployment' AND a.resource_id IN (
|
||||
SELECT d.id FROM deployments d
|
||||
JOIN services s ON s.id = d.service_id
|
||||
JOIN projects p ON p.id = s.project_id
|
||||
WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||
OR (a.resource = 'database' AND a.resource_id IN (
|
||||
SELECT ds.id::uuid FROM database_services ds JOIN projects p ON p.id = ds.project_id
|
||||
WHERE p.is_approved OR p.owner_id = $1::uuid
|
||||
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $1::uuid)))
|
||||
)`
|
||||
|
||||
const activitySelect = `SELECT
|
||||
a.id, COALESCE(a.user_id::text, ''), COALESCE(u.email, ''),
|
||||
a.resource, COALESCE(a.resource_id::text, ''), a.action,
|
||||
COALESCE(a.details::text, '{}'),
|
||||
COALESCE(a.ip_address::text, ''), COALESCE(a.user_agent, ''),
|
||||
COALESCE(a.severity, 'info'), COALESCE(a.category, ''), COALESCE(a.label, ''),
|
||||
a.created_at
|
||||
FROM audit_logs a
|
||||
LEFT JOIN users u ON u.id = a.user_id`
|
||||
|
||||
func scanActivityRows(rows *sql.Rows) []AuditLog {
|
||||
var logs []AuditLog
|
||||
for rows.Next() {
|
||||
var l AuditLog
|
||||
if err := rows.Scan(&l.ID, &l.UserID, &l.UserEmail, &l.Resource, &l.ResourceID,
|
||||
&l.Action, &l.Details, &l.IPAddress, &l.UserAgent,
|
||||
&l.Severity, &l.Category, &l.Label, &l.CreatedAt); err != nil {
|
||||
continue
|
||||
}
|
||||
// Rows written before enrichment carry empty metadata — classify on read.
|
||||
if l.Label == "" || l.Category == "" {
|
||||
sev, cat, label := classifyAuditEvent(l.Resource, l.Action)
|
||||
if l.Severity == "" || l.Severity == "info" {
|
||||
l.Severity = sev
|
||||
}
|
||||
if l.Category == "" {
|
||||
l.Category = cat
|
||||
}
|
||||
if l.Label == "" {
|
||||
l.Label = label
|
||||
}
|
||||
}
|
||||
logs = append(logs, l)
|
||||
}
|
||||
return logs
|
||||
}
|
||||
|
||||
// GET /activity — global feed. Filters: severity, category, resource,
|
||||
// project_id, since, page, limit.
|
||||
func handleGetActivity(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
isAdmin := contextIsAdmin(c)
|
||||
|
||||
page := parsePositiveInt(c.DefaultQuery("page", "1"), 1)
|
||||
limit := parsePositiveInt(c.DefaultQuery("limit", "50"), 50)
|
||||
if limit > 200 {
|
||||
limit = 200
|
||||
}
|
||||
offset := (page - 1) * limit
|
||||
|
||||
conditions := []string{activityVisibility}
|
||||
args := []interface{}{userID, isAdmin}
|
||||
nextArg := 3
|
||||
|
||||
if v := strings.TrimSpace(c.Query("severity")); v != "" {
|
||||
conditions = append(conditions, fmt.Sprintf("a.severity = $%d", nextArg))
|
||||
args = append(args, v)
|
||||
nextArg++
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("category")); v != "" {
|
||||
conditions = append(conditions, fmt.Sprintf("a.category = $%d", nextArg))
|
||||
args = append(args, v)
|
||||
nextArg++
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("resource")); v != "" {
|
||||
conditions = append(conditions, fmt.Sprintf("a.resource = $%d", nextArg))
|
||||
args = append(args, v)
|
||||
nextArg++
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("project_id")); v != "" {
|
||||
if _, err := uuid.Parse(v); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "invalid project_id")
|
||||
return
|
||||
}
|
||||
conditions = append(conditions, fmt.Sprintf(`(
|
||||
(a.resource = 'project' AND a.resource_id = $%d::uuid)
|
||||
OR (a.resource = 'service' AND a.resource_id IN (SELECT id FROM services WHERE project_id = $%d::uuid))
|
||||
OR (a.resource = 'deployment' AND a.resource_id IN (
|
||||
SELECT d.id FROM deployments d JOIN services s ON s.id = d.service_id WHERE s.project_id = $%d::uuid))
|
||||
OR (a.resource = 'database' AND a.resource_id IN (SELECT id::uuid FROM database_services WHERE project_id = $%d::uuid))
|
||||
)`, nextArg, nextArg, nextArg, nextArg))
|
||||
args = append(args, v)
|
||||
nextArg++
|
||||
}
|
||||
if v := strings.TrimSpace(c.Query("since")); v != "" {
|
||||
sinceTime, err := time.Parse(time.RFC3339, v)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "invalid since timestamp, expected RFC3339")
|
||||
return
|
||||
}
|
||||
conditions = append(conditions, fmt.Sprintf("a.created_at >= $%d", nextArg))
|
||||
args = append(args, sinceTime)
|
||||
nextArg++
|
||||
}
|
||||
|
||||
query := fmt.Sprintf(`%s WHERE %s ORDER BY a.created_at DESC LIMIT $%d OFFSET $%d`,
|
||||
activitySelect, strings.Join(conditions, " AND "), nextArg, nextArg+1)
|
||||
args = append(args, limit, offset)
|
||||
|
||||
rows, err := db.Query(query, args...)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to fetch activity")
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
logs := scanActivityRows(rows)
|
||||
if logs == nil {
|
||||
logs = []AuditLog{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"activity": logs,
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"has_more": len(logs) == limit,
|
||||
})
|
||||
}
|
||||
|
||||
// GET /projects/:id/activity — per-project timeline. Resource correlation
|
||||
// mirrors the global feed, pinned to this project.
|
||||
func handleGetProjectActivity(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
projectID := strings.TrimSpace(c.Param("id"))
|
||||
if _, err := uuid.Parse(projectID); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid project id")
|
||||
return
|
||||
}
|
||||
|
||||
// Access check: approved / owner / member / admin.
|
||||
userUUID, _ := uuid.Parse(userID)
|
||||
visible, err := sqlcdb.New(db.DB).CheckSecurityProjectAccess(context.Background(), sqlcdb.CheckSecurityProjectAccessParams{
|
||||
ID: uuid.MustParse(projectID),
|
||||
OwnerID: userUUID,
|
||||
Column3: contextIsAdmin(c),
|
||||
})
|
||||
if err != nil || !visible {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "project not found")
|
||||
return
|
||||
}
|
||||
|
||||
limit := parsePositiveInt(c.DefaultQuery("limit", "50"), 50)
|
||||
if limit > 200 {
|
||||
limit = 200
|
||||
}
|
||||
page := parsePositiveInt(c.DefaultQuery("page", "1"), 1)
|
||||
|
||||
rows, err := db.Query(fmt.Sprintf(`%s WHERE (
|
||||
(a.resource = 'project' AND a.resource_id = $1::uuid)
|
||||
OR (a.resource = 'service' AND a.resource_id IN (SELECT id FROM services WHERE project_id = $1::uuid))
|
||||
OR (a.resource = 'deployment' AND a.resource_id IN (
|
||||
SELECT d.id FROM deployments d JOIN services s ON s.id = d.service_id WHERE s.project_id = $1::uuid))
|
||||
OR (a.resource = 'database' AND a.resource_id IN (SELECT id::uuid FROM database_services WHERE project_id = $1::uuid))
|
||||
) ORDER BY a.created_at DESC LIMIT $2 OFFSET $3`,
|
||||
activitySelect), projectID, limit, (page-1)*limit)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to fetch activity")
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
logs := scanActivityRows(rows)
|
||||
if logs == nil {
|
||||
logs = []AuditLog{}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"activity": logs})
|
||||
}
|
||||
|
||||
// StartAuditRetention prunes audit rows past the retention window once at
|
||||
// boot and then daily. AUDIT_RETENTION_DAYS overrides the 90-day default;
|
||||
// 0 disables pruning entirely.
|
||||
func StartAuditRetention(ctx context.Context, db *database.DB) {
|
||||
days := 90
|
||||
if raw := strings.TrimSpace(os.Getenv("AUDIT_RETENTION_DAYS")); raw != "" {
|
||||
if parsed, err := strconv.Atoi(raw); err == nil {
|
||||
days = parsed
|
||||
}
|
||||
}
|
||||
// Backfill enrichment for rows written before the classifier existed,
|
||||
// batched so a large audit table doesn't hold a giant transaction.
|
||||
go backfillAuditMetadata(db)
|
||||
|
||||
if days <= 0 {
|
||||
return
|
||||
}
|
||||
|
||||
prune := func() {
|
||||
n, err := sqlcdb.New(db.DB).PruneAuditLogs(context.Background(), int32(days))
|
||||
if err != nil {
|
||||
log.Printf("audit retention prune failed: %v", err)
|
||||
return
|
||||
}
|
||||
if n > 0 {
|
||||
log.Printf("audit retention: pruned %d rows older than %d days", n, days)
|
||||
}
|
||||
}
|
||||
prune()
|
||||
|
||||
go func() {
|
||||
ticker := time.NewTicker(24 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
prune()
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// backfillAuditMetadata classifies rows written before severity/category/label
|
||||
// existed so filters and the feed see uniform metadata.
|
||||
func backfillAuditMetadata(db *database.DB) {
|
||||
q := sqlcdb.New(db.DB)
|
||||
cursor := uuid.Nil
|
||||
for {
|
||||
rows, err := q.ListAuditBackfillBatch(context.Background(), cursor)
|
||||
if err != nil {
|
||||
log.Printf("audit backfill scan failed: %v", err)
|
||||
return
|
||||
}
|
||||
if len(rows) == 0 {
|
||||
return
|
||||
}
|
||||
for _, r := range rows {
|
||||
id, _ := uuid.Parse(r.ID)
|
||||
cursor = id
|
||||
sev, cat, label := classifyAuditEvent(r.Resource, r.Action)
|
||||
if err := q.SetAuditLogMetadata(context.Background(), sqlcdb.SetAuditLogMetadataParams{
|
||||
Severity: sev, Category: cat, Label: label, Column4: id,
|
||||
}); err != nil {
|
||||
log.Printf("audit backfill update failed: %v", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
package api
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestClassifyAuditEvent(t *testing.T) {
|
||||
cases := []struct {
|
||||
resource, action string
|
||||
wantSev, wantCat, wantLab string
|
||||
}{
|
||||
{"service", "service.create", "success", "service", "Service created"},
|
||||
{"service", "service.delete", "warning", "service", "Service deleted"},
|
||||
{"deployment", "deploy", "success", "deployment", "Deployment deployed"},
|
||||
{"project", "project.create", "success", "project", "Project created"},
|
||||
{"user_invite", "user_invite.create", "success", "system", "User invite created"},
|
||||
{"user_token", "user_token.revoke", "warning", "security", "User token revoked"},
|
||||
{"service", "deploy.fail", "error", "service", "Service fail"},
|
||||
{"user", "impersonate", "warning", "user", "User impersonated"},
|
||||
{"webhook", "update", "info", "webhook", "Webhook updated"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
sev, cat, lab := classifyAuditEvent(tc.resource, tc.action)
|
||||
if sev != tc.wantSev {
|
||||
t.Errorf("%s/%s: severity = %q, want %q", tc.resource, tc.action, sev, tc.wantSev)
|
||||
}
|
||||
if cat != tc.wantCat {
|
||||
t.Errorf("%s/%s: category = %q, want %q", tc.resource, tc.action, cat, tc.wantCat)
|
||||
}
|
||||
if lab != tc.wantLab {
|
||||
t.Errorf("%s/%s: label = %q, want %q", tc.resource, tc.action, lab, tc.wantLab)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -52,15 +52,11 @@ func EnsureAdminAccount(ctx context.Context, db *database.DB, cfg *config.Config
|
||||
return
|
||||
}
|
||||
|
||||
var userID string
|
||||
err = db.QueryRowContext(ctx, `
|
||||
INSERT INTO users (email, password_hash, name, is_admin)
|
||||
VALUES ($1, $2, $3, true)
|
||||
ON CONFLICT (email) DO UPDATE
|
||||
SET is_admin = true, updated_at = NOW()
|
||||
RETURNING id
|
||||
`, email, string(hashed), name).Scan(&userID)
|
||||
if err != nil {
|
||||
if _, err := sqlcdb.New(db.DB).UpsertAdminUser(ctx, sqlcdb.UpsertAdminUserParams{
|
||||
Email: email,
|
||||
PasswordHash: string(hashed),
|
||||
Name: name,
|
||||
}); err != nil {
|
||||
log.Printf("Admin bootstrap failed to upsert user %s: %v", email, err)
|
||||
return
|
||||
}
|
||||
@@ -185,24 +181,18 @@ func handleAdminOverview(c *gin.Context) {
|
||||
func handleAdminListUsers(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
|
||||
rows, err := db.QueryContext(c.Request.Context(), `
|
||||
SELECT id, email, name, COALESCE(avatar_url, ''), is_admin, COALESCE(to_char(created_at, 'YYYY-MM-DD"T"HH24:MI:SS"Z"'), '')
|
||||
FROM users
|
||||
ORDER BY created_at ASC
|
||||
`)
|
||||
rows, err := sqlcdb.New(db.DB).ListAdminUsers(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list users"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
users := make([]adminUserRow, 0)
|
||||
for rows.Next() {
|
||||
var u adminUserRow
|
||||
if err := rows.Scan(&u.ID, &u.Email, &u.Name, &u.AvatarURL, &u.IsAdmin, &u.CreatedAt); err != nil {
|
||||
continue
|
||||
}
|
||||
users = append(users, u)
|
||||
users := make([]adminUserRow, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
users = append(users, adminUserRow{
|
||||
ID: r.ID.String(), Email: r.Email, Name: r.Name,
|
||||
AvatarURL: r.AvatarUrl, IsAdmin: r.IsAdmin, CreatedAt: r.CreatedAt,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"users": users})
|
||||
}
|
||||
@@ -229,15 +219,16 @@ func handleAdminSetUserAdmin(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := db.ExecContext(c.Request.Context(),
|
||||
`UPDATE users SET is_admin = $1, updated_at = NOW() WHERE id = $2`,
|
||||
req.IsAdmin, targetID,
|
||||
)
|
||||
targetUUID, _ := uuid.Parse(targetID)
|
||||
n, err := sqlcdb.New(db.DB).SetUserAdmin(c.Request.Context(), sqlcdb.SetUserAdminParams{
|
||||
IsAdmin: req.IsAdmin,
|
||||
ID: targetUUID,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update user"})
|
||||
return
|
||||
}
|
||||
if n, _ := result.RowsAffected(); n == 0 {
|
||||
if n == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
||||
return
|
||||
}
|
||||
@@ -246,6 +237,55 @@ func handleAdminSetUserAdmin(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{"message": "User updated"})
|
||||
}
|
||||
|
||||
// POST /admin/users/:id/impersonate — mints a 15-minute bearer token acting
|
||||
// as the target user. Admin-only; admin accounts cannot be impersonated
|
||||
// (would blur audit attribution). Every use is audit-logged at mint time;
|
||||
// token carries `impersonated_by` for downstream attribution.
|
||||
func handleAdminImpersonateUser(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
targetID := strings.TrimSpace(c.Param("id"))
|
||||
if _, err := uuid.Parse(targetID); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid user id")
|
||||
return
|
||||
}
|
||||
|
||||
userUUID, _ := uuid.Parse(targetID)
|
||||
info, err := sqlcdb.New(db.DB).GetAdminUserInfo(c.Request.Context(), userUUID)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "user not found")
|
||||
return
|
||||
}
|
||||
email, name, isAdmin := info.Email, info.Name, info.IsAdmin
|
||||
if isAdmin {
|
||||
respondError(c, http.StatusForbidden, "FORBIDDEN", "admin accounts cannot be impersonated")
|
||||
return
|
||||
}
|
||||
actorID := optionalUserID(c)
|
||||
if actorID == targetID {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "cannot impersonate yourself")
|
||||
return
|
||||
}
|
||||
|
||||
expiresAt := time.Now().UTC().Add(15 * time.Minute)
|
||||
jwtSecret := c.MustGet("jwt_secret").(string)
|
||||
token, err := generateImpersonationJWT(targetID, email, actorID, jwtSecret, expiresAt)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate token")
|
||||
return
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "user", targetID, "impersonate", map[string]interface{}{
|
||||
"actor_id": actorID,
|
||||
"target_email": email,
|
||||
"expires_at": expiresAt.Format(time.RFC3339),
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"token": token,
|
||||
"expires_at": expiresAt.Format(time.RFC3339),
|
||||
"user": gin.H{"id": targetID, "email": email, "name": name},
|
||||
})
|
||||
}
|
||||
|
||||
func handleAdminSetProjectApproval(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
queries := sqlcdb.New(db.DB)
|
||||
|
||||
@@ -6,13 +6,17 @@ import (
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"database/sql"
|
||||
_ "embed"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"math"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -26,16 +30,24 @@ import (
|
||||
|
||||
// NodeAgent represents a container orchestration agent
|
||||
type NodeAgent struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Hostname string `json:"hostname"`
|
||||
IPAddress string `json:"ip_address"`
|
||||
Port int `json:"port"`
|
||||
Status string `json:"status"`
|
||||
Version string `json:"version"`
|
||||
Capabilities AgentCapabilities `json:"capabilities"`
|
||||
Resources NodeResources `json:"resources"`
|
||||
LastHeartbeat time.Time `json:"last_heartbeat"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Hostname string `json:"hostname"`
|
||||
IPAddress string `json:"ip_address"`
|
||||
Port int `json:"port"`
|
||||
Status string `json:"status"`
|
||||
Version string `json:"version"`
|
||||
Capabilities AgentCapabilities `json:"capabilities"`
|
||||
Resources NodeResources `json:"resources"`
|
||||
LastHeartbeat time.Time `json:"last_heartbeat"`
|
||||
AutoPrune bool `json:"auto_prune"`
|
||||
Schedulable bool `json:"schedulable"`
|
||||
// Tags are operator-set placement labels; services with placement_tags
|
||||
// only schedule onto agents carrying every required tag.
|
||||
Tags []string `json:"tags"`
|
||||
// DefaultDomain is this node's base domain — services placed here with
|
||||
// no explicit domains get <name>.<default_domain> automatically.
|
||||
DefaultDomain string `json:"default_domain"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
Metadata map[string]interface{} `json:"metadata"`
|
||||
@@ -189,6 +201,17 @@ type AgentHeartbeat struct {
|
||||
SystemLoad SystemLoad `json:"system_load"`
|
||||
Uptime int64 `json:"uptime"`
|
||||
Version string `json:"version"`
|
||||
// Containers reports managed-container liveness — {name, state} per
|
||||
// containr.managed container on the node — so remote replica status
|
||||
// reflects reality between reconciles (exits, OOMs, crash loops).
|
||||
Containers []AgentContainerState `json:"containers,omitempty"`
|
||||
}
|
||||
|
||||
type AgentContainerState struct {
|
||||
Name string `json:"name"`
|
||||
State string `json:"state"`
|
||||
RxBytes int64 `json:"rx_bytes,omitempty"`
|
||||
TxBytes int64 `json:"tx_bytes,omitempty"`
|
||||
}
|
||||
|
||||
type SystemLoad struct {
|
||||
@@ -225,16 +248,45 @@ func unmarshalRaw(raw pqtype.NullRawMessage, dst interface{}) {
|
||||
}
|
||||
}
|
||||
|
||||
// ListNodeOptions returns the minimal id/name/status list used by the service
|
||||
// placement picker — available to any authenticated user (no hostnames, IPs,
|
||||
// or telemetry).
|
||||
func (h *NodeAgentHandler) ListNodeOptions(c *gin.Context) {
|
||||
rows, err := h.q.ListAgents(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list nodes"})
|
||||
return
|
||||
}
|
||||
options := make([]gin.H, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
var tags []string
|
||||
if len(row.Tags) > 0 {
|
||||
_ = json.Unmarshal(row.Tags, &tags)
|
||||
}
|
||||
options = append(options, gin.H{
|
||||
"id": row.ID,
|
||||
"name": row.Name,
|
||||
"status": row.Status.String,
|
||||
"tags": tags,
|
||||
})
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"nodes": options})
|
||||
}
|
||||
|
||||
func agentFromRow(row sqlcdb.NodeAgent) NodeAgent {
|
||||
agent := NodeAgent{
|
||||
ID: row.ID,
|
||||
Name: row.Name,
|
||||
Hostname: row.Hostname,
|
||||
IPAddress: row.IpAddress,
|
||||
Port: int(row.Port),
|
||||
Status: row.Status.String,
|
||||
Version: row.Version.String,
|
||||
Metadata: map[string]interface{}{},
|
||||
ID: row.ID,
|
||||
Name: row.Name,
|
||||
Hostname: row.Hostname,
|
||||
IPAddress: row.IpAddress,
|
||||
Port: int(row.Port),
|
||||
Status: row.Status.String,
|
||||
Version: row.Version.String,
|
||||
AutoPrune: row.AutoPrune,
|
||||
Schedulable: row.Schedulable,
|
||||
Tags: []string{},
|
||||
DefaultDomain: row.DefaultDomain,
|
||||
Metadata: map[string]interface{}{},
|
||||
}
|
||||
if row.LastHeartbeat.Valid {
|
||||
agent.LastHeartbeat = row.LastHeartbeat.Time
|
||||
@@ -248,6 +300,9 @@ func agentFromRow(row sqlcdb.NodeAgent) NodeAgent {
|
||||
unmarshalRaw(row.Capabilities, &agent.Capabilities)
|
||||
unmarshalRaw(row.Resources, &agent.Resources)
|
||||
unmarshalRaw(row.Metadata, &agent.Metadata)
|
||||
if len(row.Tags) > 0 {
|
||||
_ = json.Unmarshal(row.Tags, &agent.Tags)
|
||||
}
|
||||
return agent
|
||||
}
|
||||
|
||||
@@ -326,7 +381,9 @@ func (h *NodeAgentHandler) RegisterAgent(c *gin.Context) {
|
||||
IPAddress string `json:"ip_address" binding:"required"`
|
||||
Port int `json:"port" binding:"required"`
|
||||
Capabilities AgentCapabilities `json:"capabilities" binding:"required"`
|
||||
Version string `json:"version"`
|
||||
AuthToken string `json:"auth_token"`
|
||||
Mesh map[string]string `json:"mesh"`
|
||||
}
|
||||
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
@@ -341,24 +398,43 @@ func (h *NodeAgentHandler) RegisterAgent(c *gin.Context) {
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
metadata := map[string]interface{}{}
|
||||
// req.Mesh is authoritative when present (newer agents always send it,
|
||||
// possibly empty); nil means an older agent — keep any stored value.
|
||||
if req.Mesh != nil {
|
||||
metadata["mesh"] = req.Mesh
|
||||
}
|
||||
|
||||
// Re-registering an existing host updates it in place.
|
||||
if existing, err := h.q.GetAgentByHostAndIP(ctx, sqlcdb.GetAgentByHostAndIPParams{
|
||||
Hostname: req.Hostname,
|
||||
IpAddress: req.IPAddress,
|
||||
}); err == nil {
|
||||
if existing.Metadata.Valid && len(existing.Metadata.RawMessage) > 0 {
|
||||
var prev map[string]interface{}
|
||||
if err := json.Unmarshal(existing.Metadata.RawMessage, &prev); err == nil {
|
||||
for k, v := range prev {
|
||||
if _, ok := metadata[k]; !ok {
|
||||
metadata[k] = v
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
updated, err := h.q.UpdateAgent(ctx, sqlcdb.UpdateAgentParams{
|
||||
ID: existing.ID,
|
||||
Name: req.Name,
|
||||
Hostname: existing.Hostname,
|
||||
IpAddress: existing.IpAddress,
|
||||
Port: int32(req.Port),
|
||||
Status: sql.NullString{String: "connecting", Valid: true},
|
||||
Version: existing.Version,
|
||||
ID: existing.ID,
|
||||
Name: req.Name,
|
||||
Hostname: existing.Hostname,
|
||||
IpAddress: existing.IpAddress,
|
||||
Port: int32(req.Port),
|
||||
Status: sql.NullString{String: "connecting", Valid: true},
|
||||
// Older agents don't report a version — keep whatever's stored.
|
||||
Version: sql.NullString{String: firstNonEmpty(req.Version, existing.Version.String), Valid: true},
|
||||
Capabilities: rawJSON(req.Capabilities),
|
||||
Resources: existing.Resources,
|
||||
LastHeartbeat: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
Metadata: existing.Metadata,
|
||||
Metadata: rawJSON(metadata),
|
||||
AutoPrune: existing.AutoPrune,
|
||||
Schedulable: existing.Schedulable,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update agent"})
|
||||
@@ -404,11 +480,11 @@ func (h *NodeAgentHandler) RegisterAgent(c *gin.Context) {
|
||||
IpAddress: req.IPAddress,
|
||||
Port: int32(req.Port),
|
||||
Status: sql.NullString{String: "connecting", Valid: true},
|
||||
Version: sql.NullString{},
|
||||
Version: sql.NullString{String: req.Version, Valid: req.Version != ""},
|
||||
Capabilities: rawJSON(req.Capabilities),
|
||||
Resources: rawJSON(resources),
|
||||
LastHeartbeat: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
Metadata: rawJSON(map[string]interface{}{}),
|
||||
Metadata: rawJSON(metadata),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create agent"})
|
||||
@@ -489,13 +565,46 @@ func (h *NodeAgentHandler) UpdateAgent(c *gin.Context) {
|
||||
Resources: rawJSON(agent.Resources),
|
||||
LastHeartbeat: sql.NullTime{Time: agent.LastHeartbeat, Valid: !agent.LastHeartbeat.IsZero()},
|
||||
Metadata: rawJSON(agent.Metadata),
|
||||
AutoPrune: agent.AutoPrune,
|
||||
Schedulable: agent.Schedulable,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update agent"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"agent": agentFromRow(updated)})
|
||||
// Tags persist through their own column write — UpdateAgent keeps the
|
||||
// register-path signature untouched.
|
||||
resp := agentFromRow(updated)
|
||||
if raw, ok := updates["default_domain"]; ok {
|
||||
domain := strings.ToLower(strings.TrimSpace(fmt.Sprintf("%v", raw)))
|
||||
if domain != "" && !validHostname(domain) {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "default_domain must be a valid hostname", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if err := h.q.SetAgentDefaultDomain(ctx, sqlcdb.SetAgentDefaultDomainParams{ID: id, DefaultDomain: domain}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update default domain"})
|
||||
return
|
||||
}
|
||||
resp.DefaultDomain = domain
|
||||
}
|
||||
if raw, ok := updates["tags"]; ok {
|
||||
var tags []string
|
||||
if b, mErr := json.Marshal(raw); mErr == nil {
|
||||
_ = json.Unmarshal(b, &tags)
|
||||
}
|
||||
tags = normalizePlacementTags(tags)
|
||||
if err := h.q.SetAgentTags(ctx, sqlcdb.SetAgentTagsParams{
|
||||
ID: id,
|
||||
Column2: json.RawMessage(tagsJSON(tags)),
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update agent tags"})
|
||||
return
|
||||
}
|
||||
resp.Tags = tags
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"agent": resp})
|
||||
}
|
||||
|
||||
// applyAgentUpdates merges whitelisted keys from an arbitrary update map.
|
||||
@@ -516,6 +625,8 @@ func applyAgentUpdates(agent *NodeAgent, updates map[string]interface{}) {
|
||||
remarshal("capabilities", &agent.Capabilities)
|
||||
remarshal("resources", &agent.Resources)
|
||||
remarshal("metadata", &agent.Metadata)
|
||||
remarshal("auto_prune", &agent.AutoPrune)
|
||||
remarshal("schedulable", &agent.Schedulable)
|
||||
}
|
||||
|
||||
// DeleteAgent removes an agent
|
||||
@@ -581,6 +692,22 @@ func (h *NodeAgentHandler) SendHeartbeat(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Reconcile remote container liveness — exits and crash loops surface
|
||||
// in inventory between reconciles. Best-effort, never fatal.
|
||||
for _, rc := range heartbeat.Containers {
|
||||
if rc.Name == "" || rc.State == "" {
|
||||
continue
|
||||
}
|
||||
stateJSON, _ := json.Marshal(rc.State)
|
||||
bytesJSON, _ := json.Marshal(rc.RxBytes + rc.TxBytes)
|
||||
_ = h.q.UpdateContainerStateByName(ctx, sqlcdb.UpdateContainerStateByNameParams{
|
||||
Name: rc.Name,
|
||||
NodeAgentID: heartbeat.NodeAgentID,
|
||||
Column3: stateJSON,
|
||||
Column4: bytesJSON,
|
||||
})
|
||||
}
|
||||
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
@@ -650,9 +777,51 @@ func (h *NodeAgentHandler) CompleteCommand(c *gin.Context) {
|
||||
h.updateContainerStatusAfterCommand(ctx, out)
|
||||
}
|
||||
|
||||
// Command payloads can carry registry credentials — scrub them once the
|
||||
// command is settled so secrets don't linger in the queue rows.
|
||||
scrubCommandSecrets(ctx, h.q, commandID, command.Payload)
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"command": out})
|
||||
}
|
||||
|
||||
// scrubCommandSecrets rewrites a completed command's payload with any
|
||||
// credential-bearing fields blanked — registry passwords and build-time
|
||||
// args (tokens like NPM_TOKEN ride along as build args). Best-effort — a
|
||||
// failed scrub leaves the row.
|
||||
func scrubCommandSecrets(ctx context.Context, q *sqlcdb.Queries, commandID string, payload pqtype.NullRawMessage) {
|
||||
if !payload.Valid || len(payload.RawMessage) == 0 {
|
||||
return
|
||||
}
|
||||
var doc map[string]interface{}
|
||||
if err := json.Unmarshal(payload.RawMessage, &doc); err != nil {
|
||||
return
|
||||
}
|
||||
dirty := false
|
||||
if container, ok := doc["container"].(map[string]interface{}); ok {
|
||||
if reg, ok := container["registry"].(map[string]interface{}); ok && reg["password"] != nil {
|
||||
reg["password"] = "***"
|
||||
dirty = true
|
||||
}
|
||||
}
|
||||
for _, key := range []string{"build_args", "environment"} {
|
||||
if args, ok := doc[key].(map[string]interface{}); ok && len(args) > 0 {
|
||||
for k := range args {
|
||||
args[k] = "***"
|
||||
}
|
||||
dirty = true
|
||||
}
|
||||
}
|
||||
if !dirty {
|
||||
return
|
||||
}
|
||||
if err := q.ScrubCommandPayload(ctx, sqlcdb.ScrubCommandPayloadParams{
|
||||
ID: commandID,
|
||||
Payload: rawJSON(doc),
|
||||
}); err != nil {
|
||||
log.Printf("agents: scrub command %s payload: %v", commandID, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *NodeAgentHandler) updateContainerStatusAfterCommand(ctx context.Context, command AgentCommand) {
|
||||
row, err := h.q.GetContainer(ctx, *command.ContainerID)
|
||||
if err != nil {
|
||||
@@ -803,6 +972,103 @@ func (h *NodeAgentHandler) CreateContainer(c *gin.Context) {
|
||||
c.JSON(http.StatusCreated, gin.H{"container": container})
|
||||
}
|
||||
|
||||
// PruneAgent enqueues a bounded `docker system prune -af` command. Volumes
|
||||
// stay unless the caller explicitly opts in.
|
||||
func (h *NodeAgentHandler) PruneAgent(c *gin.Context) {
|
||||
agentID := c.Param("id")
|
||||
if _, err := h.q.GetAgent(c.Request.Context(), agentID); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Agent not found"})
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Until string `json:"until"` // e.g. "168h" — only prune objects older than this
|
||||
Volumes bool `json:"volumes"` // destructive — off by default
|
||||
}
|
||||
_ = c.ShouldBindJSON(&req)
|
||||
|
||||
command, err := h.q.CreateCommand(c.Request.Context(), sqlcdb.CreateCommandParams{
|
||||
ID: uuid.New().String(),
|
||||
Type: "prune",
|
||||
NodeAgentID: agentID,
|
||||
Payload: rawJSON(map[string]interface{}{"until": req.Until, "volumes": req.Volumes}),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to enqueue prune"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusAccepted, gin.H{"command": command})
|
||||
}
|
||||
|
||||
// UpgradeAgent enqueues a self_upgrade command: the agent downloads its
|
||||
// replacement binary from this server's bundled /download endpoint,
|
||||
// verifies the sha256, swaps it in, and re-execs. Fails fast when no
|
||||
// bundled binary exists for the agent's architecture.
|
||||
func (h *NodeAgentHandler) UpgradeAgent(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id := c.Param("id")
|
||||
agent, err := h.q.GetAgent(ctx, id)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Agent not found")
|
||||
return
|
||||
}
|
||||
if agent.Status.String != "online" {
|
||||
respondError(c, http.StatusConflict, "NODE_OFFLINE", "node is offline — reconnect it before upgrading")
|
||||
return
|
||||
}
|
||||
|
||||
arch := "amd64"
|
||||
var caps struct {
|
||||
SupportedArchitectures []string `json:"supported_architectures"`
|
||||
}
|
||||
unmarshalRaw(agent.Capabilities, &caps)
|
||||
if len(caps.SupportedArchitectures) > 0 && caps.SupportedArchitectures[0] != "" {
|
||||
arch = caps.SupportedArchitectures[0]
|
||||
}
|
||||
platform := "linux-" + arch // agents only ship for linux today
|
||||
|
||||
dir := os.Getenv("CONTAINR_AGENT_BIN_DIR")
|
||||
if dir == "" {
|
||||
dir = "/agents"
|
||||
}
|
||||
path := filepath.Join(dir, "containr-agent-"+platform)
|
||||
sum, err := sha256File(path)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusConflict, "NO_BINARY",
|
||||
fmt.Sprintf("no bundled agent binary for %s on this server", platform))
|
||||
return
|
||||
}
|
||||
|
||||
command, err := h.q.CreateCommand(ctx, sqlcdb.CreateCommandParams{
|
||||
ID: uuid.New().String(),
|
||||
Type: "self_upgrade",
|
||||
NodeAgentID: id,
|
||||
Payload: rawJSON(map[string]interface{}{
|
||||
"platform": platform,
|
||||
"sha256": sum,
|
||||
"version": Version,
|
||||
}),
|
||||
})
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to enqueue upgrade")
|
||||
return
|
||||
}
|
||||
LogAuditWithRequest(c, "node", id, "upgrade", map[string]interface{}{"platform": platform, "version": Version})
|
||||
c.JSON(http.StatusAccepted, gin.H{"command": command, "platform": platform, "version": Version})
|
||||
}
|
||||
|
||||
func sha256File(path string) (string, error) {
|
||||
f, err := os.Open(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
defer f.Close()
|
||||
h := sha256.New()
|
||||
if _, err := io.Copy(h, f); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(h.Sum(nil)), nil
|
||||
}
|
||||
|
||||
// ExecuteCommand executes a command on an agent
|
||||
func (h *NodeAgentHandler) ExecuteCommand(c *gin.Context) {
|
||||
agentID := c.Param("id")
|
||||
@@ -1214,9 +1480,49 @@ func (h *NodeAgentHandler) SetupPublicRoutes(router *gin.RouterGroup) {
|
||||
agents.POST("/heartbeat", h.SendHeartbeat)
|
||||
agents.GET("/:id/commands", h.GetPendingCommandsForAgent)
|
||||
agents.POST("/:id/commands/:commandId/result", h.CompleteCommand)
|
||||
|
||||
// Bootstrap surface — the install script and agent binaries are
|
||||
// public; the enroll token is the actual secret.
|
||||
agents.GET("/install.sh", h.ServeInstallScript)
|
||||
agents.GET("/download/:platform", h.ServeAgentBinary)
|
||||
|
||||
// Deploy artifacts (build contexts, image tarballs) — token-gated;
|
||||
// the blobs are per-deployment source code, not public bootstrap.
|
||||
agents.GET("/artifacts/:id", h.ServeArtifact)
|
||||
}
|
||||
}
|
||||
|
||||
//go:embed assets/install-agent.sh
|
||||
var agentInstallScript string
|
||||
|
||||
// ServeInstallScript returns the agent bootstrap script. The script
|
||||
// downloads the binary from this instance first, GH releases second.
|
||||
func (h *NodeAgentHandler) ServeInstallScript(c *gin.Context) {
|
||||
c.Data(http.StatusOK, "text/x-sh", []byte(agentInstallScript))
|
||||
}
|
||||
|
||||
// ServeAgentBinary streams a bundled agent binary. Images ship the
|
||||
// linux binaries under CONTAINR_AGENT_BIN_DIR (default /agents).
|
||||
func (h *NodeAgentHandler) ServeAgentBinary(c *gin.Context) {
|
||||
platform := c.Param("platform")
|
||||
if platform != "linux-amd64" && platform != "linux-arm64" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "unsupported platform"})
|
||||
return
|
||||
}
|
||||
dir := os.Getenv("CONTAINR_AGENT_BIN_DIR")
|
||||
if dir == "" {
|
||||
dir = "/agents"
|
||||
}
|
||||
path := filepath.Join(dir, "containr-agent-"+platform)
|
||||
info, err := os.Stat(path)
|
||||
if err != nil || info.IsDir() {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "no bundled binary for " + platform})
|
||||
return
|
||||
}
|
||||
c.Header("Content-Disposition", `attachment; filename="containr-agent"`)
|
||||
c.File(path)
|
||||
}
|
||||
|
||||
// SetupAdminRoutes registers dashboard/control agent routes. Command
|
||||
// history and container details can leak sensitive output, so these stay
|
||||
// admin-only rather than joining the public read group.
|
||||
@@ -1243,8 +1549,95 @@ func (h *NodeAgentHandler) SetupAdminRoutes(router *gin.RouterGroup) {
|
||||
agents.DELETE("/:id/containers/:containerId", h.ContainerAction)
|
||||
|
||||
agents.GET("/:id/metrics", h.GetAgentMetrics)
|
||||
agents.POST("/:id/cordon", h.CordonAgent)
|
||||
agents.POST("/:id/uncordon", h.UncordonAgent)
|
||||
agents.POST("/:id/drain", h.DrainAgent)
|
||||
agents.POST("/:id/upgrade", h.UpgradeAgent)
|
||||
agents.POST("/:id/prune", h.PruneAgent)
|
||||
agents.POST("/:id/commands", h.ExecuteCommand)
|
||||
agents.GET("/:id/commands", h.GetAgentCommands)
|
||||
agents.GET("/:id/commands/:commandId", h.GetCommandStatus)
|
||||
}
|
||||
}
|
||||
|
||||
// setSchedulable flips the node's scheduling gate. Cordoned nodes keep
|
||||
// running their placed replicas but take no new pins or `auto` placements.
|
||||
func (h *NodeAgentHandler) setSchedulable(c *gin.Context, schedulable bool) {
|
||||
id := c.Param("id")
|
||||
if _, err := h.q.GetAgent(c.Request.Context(), id); err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Agent not found")
|
||||
return
|
||||
}
|
||||
if err := h.q.SetAgentSchedulable(c.Request.Context(), sqlcdb.SetAgentSchedulableParams{
|
||||
ID: id, Schedulable: schedulable,
|
||||
}); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to update node")
|
||||
return
|
||||
}
|
||||
LogAuditWithRequest(c, "node", id, map[bool]string{true: "uncordon", false: "cordon"}[schedulable], nil)
|
||||
c.JSON(http.StatusOK, gin.H{"id": id, "schedulable": schedulable})
|
||||
}
|
||||
|
||||
// CordonAgent blocks new placements on the node (explicit pins and `auto`).
|
||||
func (h *NodeAgentHandler) CordonAgent(c *gin.Context) { h.setSchedulable(c, false) }
|
||||
|
||||
// UncordonAgent re-opens the node for placement.
|
||||
func (h *NodeAgentHandler) UncordonAgent(c *gin.Context) { h.setSchedulable(c, true) }
|
||||
|
||||
// DrainAgent cordons the node, removes every service container via the agent
|
||||
// command queue, and clears the affected services' pins — they fall back to
|
||||
// the local host on next deploy and can be re-pinned to another node.
|
||||
// Requires the node to be online; an offline node can't acknowledge the
|
||||
// removals.
|
||||
func (h *NodeAgentHandler) DrainAgent(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
id := c.Param("id")
|
||||
agent, err := h.q.GetAgent(ctx, id)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Agent not found")
|
||||
return
|
||||
}
|
||||
if agent.Status.String != "online" {
|
||||
respondError(c, http.StatusConflict, "NODE_OFFLINE",
|
||||
"node is not online — reconnect it, or remove the containers manually")
|
||||
return
|
||||
}
|
||||
if err := h.q.SetAgentSchedulable(ctx, sqlcdb.SetAgentSchedulableParams{
|
||||
ID: id, Schedulable: false,
|
||||
}); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to cordon node")
|
||||
return
|
||||
}
|
||||
|
||||
runner := &agentNodeRunner{q: h.q}
|
||||
rows, err := h.q.ListContainersForAgent(ctx, id)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to list node containers")
|
||||
return
|
||||
}
|
||||
services := map[string]bool{}
|
||||
for _, row := range rows {
|
||||
services[row.ServiceID] = true
|
||||
}
|
||||
var drained int
|
||||
var firstErr error
|
||||
for serviceID := range services {
|
||||
if err := runner.RemoveService(ctx, serviceID, id); err != nil {
|
||||
if firstErr == nil {
|
||||
firstErr = err
|
||||
}
|
||||
continue
|
||||
}
|
||||
drained += 1
|
||||
}
|
||||
unpinned, _ := h.q.ClearServiceNodePins(ctx, sql.NullString{String: id, Valid: true})
|
||||
if firstErr != nil {
|
||||
respondError(c, http.StatusBadGateway, "DRAIN_PARTIAL",
|
||||
fmt.Sprintf("drained %d service(s), unpinned %d — one failed: %v", drained, unpinned, firstErr))
|
||||
return
|
||||
}
|
||||
LogAuditWithRequest(c, "node", id, "drain", map[string]interface{}{
|
||||
"services": drained, "unpinned": unpinned,
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"id": id, "schedulable": false, "services_drained": drained, "services_unpinned": unpinned})
|
||||
}
|
||||
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
@@ -14,6 +15,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/google/uuid"
|
||||
"github.com/lib/pq"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
@@ -160,21 +162,22 @@ func handleAPwhyServiceValidate(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
ctx := c.Request.Context()
|
||||
|
||||
var upstreamURL, healthPath string
|
||||
var authHeader, authValue sql.NullString
|
||||
var timeoutMs sql.NullInt64
|
||||
err := db.QueryRowContext(ctx, `
|
||||
SELECT upstream_url, health_path, upstream_auth_header, upstream_auth_value, request_timeout_ms
|
||||
FROM api_services WHERE id = $1
|
||||
`, serviceID).Scan(&upstreamURL, &healthPath, &authHeader, &authValue, &timeoutMs)
|
||||
if err != nil {
|
||||
svcUUID, parseErr := uuid.Parse(serviceID)
|
||||
var svc sqlcdb.GetAPServiceForValidateRow
|
||||
var err error
|
||||
if parseErr == nil {
|
||||
svc, err = sqlcdb.New(db.DB).GetAPServiceForValidate(ctx, svcUUID)
|
||||
}
|
||||
if parseErr != nil || err != nil {
|
||||
sendErrorResponse(c, http.StatusNotFound, "SERVICE_NOT_FOUND", "Service not found", nil)
|
||||
return
|
||||
}
|
||||
upstreamURL, healthPath := svc.UpstreamUrl, svc.HealthPath
|
||||
authHeader, authValue := svc.UpstreamAuthHeader, svc.UpstreamAuthValue
|
||||
|
||||
timeout := 8000
|
||||
if timeoutMs.Valid && timeoutMs.Int64 > 0 {
|
||||
timeout = int(timeoutMs.Int64)
|
||||
if svc.RequestTimeoutMs.Valid && svc.RequestTimeoutMs.Int32 > 0 {
|
||||
timeout = int(svc.RequestTimeoutMs.Int32)
|
||||
}
|
||||
target := strings.TrimRight(upstreamURL, "/") + "/" + strings.TrimLeft(healthPath, "/")
|
||||
|
||||
@@ -203,17 +206,19 @@ func handleAPwhyServiceValidate(c *gin.Context) {
|
||||
validationStatus = "healthy"
|
||||
}
|
||||
|
||||
_, _ = db.ExecContext(ctx, `
|
||||
UPDATE api_services
|
||||
SET last_validation_at = NOW(), last_validation_status = $1, last_validation_message = $2, updated_at = NOW()
|
||||
WHERE id = $3
|
||||
`, validationStatus, message, serviceID)
|
||||
q := sqlcdb.New(db.DB)
|
||||
_ = q.SetAPServiceValidation(ctx, sqlcdb.SetAPServiceValidationParams{
|
||||
LastValidationStatus: sql.NullString{String: validationStatus, Valid: true},
|
||||
LastValidationMessage: sql.NullString{String: message, Valid: true},
|
||||
ID: svcUUID,
|
||||
})
|
||||
|
||||
if !ok {
|
||||
_, _ = db.ExecContext(ctx, `
|
||||
INSERT INTO incident_events (service_id, code, message, severity, http_status, occurred_at)
|
||||
VALUES ($1, 'SERVICE_VALIDATION_FAILED', $2, 'medium', $3, NOW())
|
||||
`, serviceID, message, status)
|
||||
_ = q.InsertServiceIncident(ctx, sqlcdb.InsertServiceIncidentParams{
|
||||
ServiceID: uuid.NullUUID{UUID: svcUUID, Valid: true},
|
||||
Message: message,
|
||||
HttpStatus: sql.NullInt32{Int32: int32(status), Valid: true},
|
||||
})
|
||||
}
|
||||
|
||||
sendSuccessResponse(c, http.StatusOK, map[string]interface{}{
|
||||
@@ -231,36 +236,24 @@ func handleAPwhyServicesList(c *gin.Context) {
|
||||
ctx := context.Background()
|
||||
|
||||
// Query services from database
|
||||
rows, err := db.QueryContext(ctx, `
|
||||
SELECT id, name, slug, upstream_url, route_prefix, enabled, created_at, updated_at
|
||||
FROM api_services
|
||||
ORDER BY created_at DESC
|
||||
`)
|
||||
rows, err := sqlcdb.New(db.DB).ListAPServices(ctx)
|
||||
if err != nil {
|
||||
sendErrorResponse(c, http.StatusInternalServerError, "DATABASE_ERROR",
|
||||
"Failed to query services", err.Error())
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
services := make([]map[string]interface{}, 0)
|
||||
for rows.Next() {
|
||||
var id, name, slug, upstreamURL, routePrefix, createdAt, updatedAt string
|
||||
var enabled int
|
||||
err := rows.Scan(&id, &name, &slug, &upstreamURL, &routePrefix, &enabled, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
continue // Skip malformed rows
|
||||
}
|
||||
|
||||
services := make([]map[string]interface{}, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
services = append(services, map[string]interface{}{
|
||||
"id": id,
|
||||
"name": name,
|
||||
"slug": slug,
|
||||
"upstreamUrl": upstreamURL,
|
||||
"routePrefix": routePrefix,
|
||||
"enabled": enabled != 0,
|
||||
"createdAt": createdAt,
|
||||
"updatedAt": updatedAt,
|
||||
"id": r.ID.String(),
|
||||
"name": r.Name,
|
||||
"slug": r.Slug,
|
||||
"upstreamUrl": r.UpstreamUrl,
|
||||
"routePrefix": r.RoutePrefix,
|
||||
"enabled": r.Enabled != 0,
|
||||
"createdAt": r.CreatedAt,
|
||||
"updatedAt": r.UpdatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -299,14 +292,14 @@ func handleAPwhyServicesCreate(c *gin.Context) {
|
||||
}
|
||||
|
||||
// id defaults to gen_random_uuid(); enabled defaults to 1.
|
||||
var id string
|
||||
err := db.QueryRowContext(ctx, `
|
||||
INSERT INTO api_services (
|
||||
name, slug, upstream_url, route_prefix, health_path,
|
||||
rpm_limit, monthly_quota, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $4, '/health', $5, $6, NOW(), NOW())
|
||||
RETURNING id
|
||||
`, req.Name, slug, req.UpstreamURL, req.RoutePrefix, rpmLimit, monthlyQuota).Scan(&id)
|
||||
svcID, err := sqlcdb.New(db.DB).InsertAPService(ctx, sqlcdb.InsertAPServiceParams{
|
||||
Name: req.Name,
|
||||
Slug: slug,
|
||||
UpstreamUrl: req.UpstreamURL,
|
||||
RoutePrefix: req.RoutePrefix,
|
||||
RpmLimit: sql.NullInt32{Int32: int32(rpmLimit), Valid: true},
|
||||
MonthlyQuota: sql.NullInt32{Int32: int32(monthlyQuota), Valid: true},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
if isUniqueViolation(err) {
|
||||
@@ -320,7 +313,7 @@ func handleAPwhyServicesCreate(c *gin.Context) {
|
||||
}
|
||||
|
||||
serviceData := map[string]interface{}{
|
||||
"id": id,
|
||||
"id": svcID.String(),
|
||||
"name": req.Name,
|
||||
"slug": slug,
|
||||
"upstreamUrl": req.UpstreamURL,
|
||||
@@ -356,13 +349,15 @@ func handleAPwhyServicesPatch(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
|
||||
if input.Enabled != nil {
|
||||
enabled := 0
|
||||
enabled := int32(0)
|
||||
if *input.Enabled {
|
||||
enabled = 1
|
||||
}
|
||||
_, err := db.ExecContext(context.Background(),
|
||||
"UPDATE api_services SET enabled = $1, updated_at = NOW() WHERE id = $2",
|
||||
enabled, serviceID)
|
||||
svcUUID, _ := uuid.Parse(serviceID)
|
||||
err := sqlcdb.New(db.DB).SetAPServiceEnabled(context.Background(), sqlcdb.SetAPServiceEnabledParams{
|
||||
Enabled: enabled,
|
||||
ID: svcUUID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
@@ -383,11 +378,7 @@ func handleAPwhyServicesPatch(c *gin.Context) {
|
||||
func handleAPwhyKeysList(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
|
||||
rows, err := db.QueryContext(context.Background(), `
|
||||
SELECT id, name, key_prefix, plan, enabled, rpm_limit, monthly_quota, created_at, updated_at
|
||||
FROM api_keys
|
||||
ORDER BY created_at DESC
|
||||
`)
|
||||
rows, err := sqlcdb.New(db.DB).ListAPKeys(context.Background())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"ok": true,
|
||||
@@ -395,34 +386,24 @@ func handleAPwhyKeysList(c *gin.Context) {
|
||||
})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
keys := make([]map[string]interface{}, 0)
|
||||
for rows.Next() {
|
||||
var id, name, keyPrefix, plan, createdAt, updatedAt string
|
||||
var enabled int
|
||||
var rpmLimit, monthlyQuota sql.NullInt64
|
||||
|
||||
err := rows.Scan(&id, &name, &keyPrefix, &plan, &enabled, &rpmLimit, &monthlyQuota, &createdAt, &updatedAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
keys := make([]map[string]interface{}, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
key := map[string]interface{}{
|
||||
"id": id,
|
||||
"name": name,
|
||||
"keyPrefix": keyPrefix,
|
||||
"plan": plan,
|
||||
"enabled": enabled != 0,
|
||||
"createdAt": createdAt,
|
||||
"updatedAt": updatedAt,
|
||||
"id": r.ID.String(),
|
||||
"name": r.Name,
|
||||
"keyPrefix": r.KeyPrefix,
|
||||
"plan": r.Plan,
|
||||
"enabled": r.Enabled != 0,
|
||||
"createdAt": r.CreatedAt,
|
||||
"updatedAt": r.UpdatedAt,
|
||||
}
|
||||
|
||||
if rpmLimit.Valid {
|
||||
key["rpmLimit"] = rpmLimit.Int64
|
||||
if r.RpmLimit.Valid {
|
||||
key["rpmLimit"] = int64(r.RpmLimit.Int32)
|
||||
}
|
||||
if monthlyQuota.Valid {
|
||||
key["monthlyQuota"] = monthlyQuota.Int64
|
||||
if r.MonthlyQuota.Valid {
|
||||
key["monthlyQuota"] = int64(r.MonthlyQuota.Int32)
|
||||
}
|
||||
|
||||
keys = append(keys, key)
|
||||
@@ -481,14 +462,14 @@ func handleAPwhyKeysCreate(c *gin.Context) {
|
||||
}
|
||||
|
||||
// id defaults to gen_random_uuid(); enabled defaults to 1.
|
||||
var id string
|
||||
err = db.QueryRowContext(context.Background(), `
|
||||
INSERT INTO api_keys (
|
||||
name, key_hash, key_prefix, plan, allowed_service_ids,
|
||||
rpm_limit, monthly_quota, created_at, updated_at
|
||||
) VALUES ($1, $2, $3, $4, '[]', $5, $6, NOW(), NOW())
|
||||
RETURNING id
|
||||
`, input.Name, keyHash, keyPrefix, plan, rpmLimit, monthlyQuota).Scan(&id)
|
||||
keyID, err := sqlcdb.New(db.DB).InsertAPKey(context.Background(), sqlcdb.InsertAPKeyParams{
|
||||
Name: input.Name,
|
||||
KeyHash: keyHash,
|
||||
KeyPrefix: keyPrefix,
|
||||
Plan: plan,
|
||||
RpmLimit: sql.NullInt32{Int32: int32(rpmLimit), Valid: true},
|
||||
MonthlyQuota: sql.NullInt32{Int32: int32(monthlyQuota), Valid: true},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
@@ -501,7 +482,7 @@ func handleAPwhyKeysCreate(c *gin.Context) {
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"ok": true,
|
||||
"data": gin.H{
|
||||
"id": id,
|
||||
"id": keyID.String(),
|
||||
"name": input.Name,
|
||||
"plan": plan,
|
||||
"key": apiKey, // Only return the actual key once
|
||||
@@ -532,13 +513,15 @@ func handleAPwhyKeysPatch(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
|
||||
if input.Enabled != nil {
|
||||
enabled := 0
|
||||
enabled := int32(0)
|
||||
if *input.Enabled {
|
||||
enabled = 1
|
||||
}
|
||||
_, err := db.ExecContext(context.Background(),
|
||||
"UPDATE api_keys SET enabled = $1, updated_at = NOW() WHERE id = $2",
|
||||
enabled, keyID)
|
||||
keyUUID, _ := uuid.Parse(keyID)
|
||||
err := sqlcdb.New(db.DB).SetAPKeyEnabled(context.Background(), sqlcdb.SetAPKeyEnabledParams{
|
||||
Enabled: enabled,
|
||||
ID: keyUUID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
@@ -560,23 +543,27 @@ func handleAPwhyAnalyticsOps(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
|
||||
// Get counts from database
|
||||
var totalServices, totalKeys, totalUsers int
|
||||
q := sqlcdb.New(db.DB)
|
||||
ctx := context.Background()
|
||||
|
||||
if err := db.QueryRowContext(context.Background(), "SELECT COUNT(*) FROM api_services").Scan(&totalServices); err != nil {
|
||||
totalServices, err := q.CountAPServices(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"ok": false,
|
||||
"error": "Failed to count services: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
if err := db.QueryRowContext(context.Background(), "SELECT COUNT(*) FROM api_keys").Scan(&totalKeys); err != nil {
|
||||
totalKeys, err := q.CountAPKeys(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"ok": false,
|
||||
"error": "Failed to count api keys: " + err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
if err := db.QueryRowContext(context.Background(), "SELECT COUNT(*) FROM users").Scan(&totalUsers); err != nil {
|
||||
totalUsers, err := q.CountUsers(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"ok": false,
|
||||
"error": "Failed to count users: " + err.Error(),
|
||||
@@ -584,8 +571,8 @@ func handleAPwhyAnalyticsOps(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var totalRequests int
|
||||
if err := db.QueryRowContext(context.Background(), "SELECT COALESCE(SUM(request_count), 0) FROM usage_counters").Scan(&totalRequests); err != nil {
|
||||
totalRequests, err := q.SumUsageCounters(ctx)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"ok": false,
|
||||
"error": "Failed to aggregate request counters: " + err.Error(),
|
||||
@@ -593,12 +580,8 @@ func handleAPwhyAnalyticsOps(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var requestsToday int
|
||||
if err := db.QueryRowContext(context.Background(), `
|
||||
SELECT COALESCE(SUM(value), 0)::int
|
||||
FROM metrics_timeseries
|
||||
WHERE metric = 'request_total' AND occurred_at >= DATE_TRUNC('day', NOW())
|
||||
`).Scan(&requestsToday); err != nil {
|
||||
requestsToday, err := q.SumRequestMetricSince(ctx, "day")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"ok": false,
|
||||
"error": "Failed to aggregate today's requests: " + err.Error(),
|
||||
@@ -606,12 +589,8 @@ func handleAPwhyAnalyticsOps(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var requestsThisMonth int
|
||||
if err := db.QueryRowContext(context.Background(), `
|
||||
SELECT COALESCE(SUM(value), 0)::int
|
||||
FROM metrics_timeseries
|
||||
WHERE metric = 'request_total' AND occurred_at >= DATE_TRUNC('month', NOW())
|
||||
`).Scan(&requestsThisMonth); err != nil {
|
||||
requestsThisMonth, err := q.SumRequestMetricSince(ctx, "month")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{
|
||||
"ok": false,
|
||||
"error": "Failed to aggregate monthly requests: " + err.Error(),
|
||||
@@ -636,94 +615,47 @@ func handleAPwhyAnalyticsOps(c *gin.Context) {
|
||||
func handleAPwhyAnalyticsTraffic(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
|
||||
q := sqlcdb.New(db.DB)
|
||||
ctx := context.Background()
|
||||
|
||||
topServices := make([]map[string]interface{}, 0)
|
||||
serviceRows, err := db.QueryContext(context.Background(), `
|
||||
SELECT s.id, s.name, COALESCE(SUM(u.request_count), 0) AS total_requests
|
||||
FROM api_services s
|
||||
LEFT JOIN usage_counters u ON u.service_id = s.id
|
||||
GROUP BY s.id, s.name
|
||||
ORDER BY total_requests DESC, s.name ASC
|
||||
LIMIT 10
|
||||
`)
|
||||
if err == nil {
|
||||
defer serviceRows.Close()
|
||||
for serviceRows.Next() {
|
||||
var serviceID, serviceName string
|
||||
var totalRequests int
|
||||
if scanErr := serviceRows.Scan(&serviceID, &serviceName, &totalRequests); scanErr == nil {
|
||||
topServices = append(topServices, map[string]interface{}{
|
||||
"service_id": serviceID,
|
||||
"name": serviceName,
|
||||
"requests": totalRequests,
|
||||
})
|
||||
}
|
||||
if serviceRows, err := q.ListTopAPServices(ctx); err == nil {
|
||||
for _, r := range serviceRows {
|
||||
topServices = append(topServices, map[string]interface{}{
|
||||
"service_id": r.ID.String(),
|
||||
"name": r.Name,
|
||||
"requests": r.TotalRequests,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
requestsByDay := make([]map[string]interface{}, 0)
|
||||
trafficRows, err := db.QueryContext(context.Background(), `
|
||||
SELECT TO_CHAR(DATE_TRUNC('day', occurred_at), 'YYYY-MM-DD') AS day_bucket, COUNT(*) AS total
|
||||
FROM metrics_timeseries
|
||||
WHERE metric = 'request_total' AND occurred_at >= NOW() - INTERVAL '7 days'
|
||||
GROUP BY DATE_TRUNC('day', occurred_at)
|
||||
ORDER BY DATE_TRUNC('day', occurred_at) ASC
|
||||
`)
|
||||
if err == nil {
|
||||
defer trafficRows.Close()
|
||||
for trafficRows.Next() {
|
||||
var day string
|
||||
var count int
|
||||
if scanErr := trafficRows.Scan(&day, &count); scanErr == nil {
|
||||
requestsByDay = append(requestsByDay, map[string]interface{}{
|
||||
"day": day,
|
||||
"requests": count,
|
||||
})
|
||||
}
|
||||
if trafficRows, err := q.ListRequestsByDay(ctx); err == nil {
|
||||
for _, r := range trafficRows {
|
||||
requestsByDay = append(requestsByDay, map[string]interface{}{
|
||||
"day": r.DayBucket,
|
||||
"requests": r.Total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
statusCodes := make([]map[string]interface{}, 0)
|
||||
statusRows, err := db.QueryContext(context.Background(), `
|
||||
SELECT COALESCE(http_status, 0) AS status_code, COALESCE(SUM(count), 0) AS total
|
||||
FROM incident_events
|
||||
WHERE occurred_at >= NOW() - INTERVAL '7 days'
|
||||
GROUP BY COALESCE(http_status, 0)
|
||||
ORDER BY total DESC, status_code ASC
|
||||
`)
|
||||
if err == nil {
|
||||
defer statusRows.Close()
|
||||
for statusRows.Next() {
|
||||
var code int
|
||||
var total int
|
||||
if scanErr := statusRows.Scan(&code, &total); scanErr == nil {
|
||||
statusCodes = append(statusCodes, map[string]interface{}{
|
||||
"status_code": code,
|
||||
"count": total,
|
||||
})
|
||||
}
|
||||
if statusRows, err := q.ListIncidentStatusCodes(ctx); err == nil {
|
||||
for _, r := range statusRows {
|
||||
statusCodes = append(statusCodes, map[string]interface{}{
|
||||
"status_code": r.StatusCode,
|
||||
"count": r.Total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
clientEvents := make([]map[string]interface{}, 0)
|
||||
eventRows, err := db.QueryContext(context.Background(), `
|
||||
SELECT COALESCE((labels_json::jsonb ->> 'path'), 'unknown') AS path, COUNT(*) AS total
|
||||
FROM metrics_timeseries
|
||||
WHERE metric = 'client_event' AND occurred_at >= NOW() - INTERVAL '7 days'
|
||||
GROUP BY COALESCE((labels_json::jsonb ->> 'path'), 'unknown')
|
||||
ORDER BY total DESC, path ASC
|
||||
LIMIT 20
|
||||
`)
|
||||
if err == nil {
|
||||
defer eventRows.Close()
|
||||
for eventRows.Next() {
|
||||
var path string
|
||||
var total int
|
||||
if scanErr := eventRows.Scan(&path, &total); scanErr == nil {
|
||||
clientEvents = append(clientEvents, map[string]interface{}{
|
||||
"path": path,
|
||||
"count": total,
|
||||
})
|
||||
}
|
||||
if eventRows, err := q.ListClientEventPaths(ctx); err == nil {
|
||||
for _, r := range eventRows {
|
||||
clientEvents = append(clientEvents, map[string]interface{}{
|
||||
"path": r.Path,
|
||||
"count": r.Total,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"encoding/hex"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// containrWorkDir is the build workspace root — same dir the BuildManager
|
||||
// was constructed with; checkouts and agent artifacts live beneath it.
|
||||
const containrWorkDir = "/tmp/containr-builds"
|
||||
|
||||
// artifacts is a one-shot blob store for node agents: build contexts and
|
||||
// docker-save tarballs the server produced on behalf of a deployment. Files
|
||||
// live under workdir/artifacts/<id> and are removed when the deployment
|
||||
// finishes or after maxArtifactAge.
|
||||
var artifacts = &artifactStore{files: map[string]string{}}
|
||||
|
||||
type artifactStore struct {
|
||||
mu sync.Mutex
|
||||
dir string
|
||||
files map[string]string
|
||||
}
|
||||
|
||||
const maxArtifactAge = 2 // hours — a deployment can never outlive this
|
||||
|
||||
// initArtifacts points the store at dir (created on demand).
|
||||
func initArtifacts(dir string) {
|
||||
artifacts.mu.Lock()
|
||||
defer artifacts.mu.Unlock()
|
||||
artifacts.dir = dir
|
||||
_ = os.MkdirAll(dir, 0o755)
|
||||
}
|
||||
|
||||
// artifactDir returns the store root, defaulting under the build workdir.
|
||||
func artifactDir() string {
|
||||
artifacts.mu.Lock()
|
||||
defer artifacts.mu.Unlock()
|
||||
if artifacts.dir != "" {
|
||||
return artifacts.dir
|
||||
}
|
||||
return filepath.Join(containrWorkDir, "artifacts")
|
||||
}
|
||||
|
||||
// putArtifact streams rc into the store and returns the artifact id.
|
||||
func putArtifact(rc io.Reader) (string, error) {
|
||||
buf := make([]byte, 16)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
id := hex.EncodeToString(buf)
|
||||
dir := artifactDir()
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
path := filepath.Join(dir, id+".tar")
|
||||
f, err := os.Create(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if _, err := io.Copy(f, rc); err != nil {
|
||||
f.Close()
|
||||
os.Remove(path)
|
||||
return "", err
|
||||
}
|
||||
f.Close()
|
||||
|
||||
artifacts.mu.Lock()
|
||||
artifacts.files[id] = path
|
||||
artifacts.mu.Unlock()
|
||||
return id, nil
|
||||
}
|
||||
|
||||
// removeArtifact deletes the file and forgets the id. Safe on missing ids.
|
||||
func removeArtifact(id string) {
|
||||
artifacts.mu.Lock()
|
||||
path, ok := artifacts.files[id]
|
||||
delete(artifacts.files, id)
|
||||
artifacts.mu.Unlock()
|
||||
if ok {
|
||||
_ = os.Remove(path)
|
||||
return
|
||||
}
|
||||
// Registered before a restart — remove best-effort by name.
|
||||
if id != "" {
|
||||
_ = os.Remove(filepath.Join(artifactDir(), id+".tar"))
|
||||
}
|
||||
}
|
||||
|
||||
// ServeArtifact streams a stored blob to a token-authenticated agent. Files
|
||||
// persist across downloads — spread builds share one context — and are
|
||||
// deleted by the deployment that created them.
|
||||
func (h *NodeAgentHandler) ServeArtifact(c *gin.Context) {
|
||||
if !h.isValidAgentAuthToken(c.Request.Context(), agentAuthTokenFromRequest(c)) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid auth token"})
|
||||
return
|
||||
}
|
||||
id := c.Param("id")
|
||||
raw, err := hex.DecodeString(id)
|
||||
if len(id) != 32 || err != nil || len(raw) != 16 {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Artifact not found"})
|
||||
return
|
||||
}
|
||||
path := filepath.Join(artifactDir(), id+".tar")
|
||||
info, err := os.Stat(path)
|
||||
if err != nil || info.IsDir() {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Artifact not found"})
|
||||
return
|
||||
}
|
||||
c.Header("Content-Type", "application/x-tar")
|
||||
c.File(path)
|
||||
}
|
||||
|
||||
// purgeStaleArtifacts drops files older than maxArtifactAge. Called on boot.
|
||||
func purgeStaleArtifacts() {
|
||||
entries, err := os.ReadDir(artifactDir())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, e := range entries {
|
||||
info, err := e.Info()
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if info.ModTime().Before(time.Now().Add(-maxArtifactAge * time.Hour)) {
|
||||
_ = os.Remove(filepath.Join(artifactDir(), e.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,82 @@
|
||||
#!/usr/bin/env bash
|
||||
# Containr node agent installer.
|
||||
# curl -fsSL <api>/api/agents/install.sh | bash -s -- --url <api> --token <enroll-token>
|
||||
#
|
||||
# One-shot: downloads the agent binary from the Containr instance (or GH
|
||||
# releases as fallback), writes a systemd unit, starts it. SSH is never
|
||||
# needed again — the agent heartbeats and polls commands outbound.
|
||||
set -euo pipefail
|
||||
|
||||
API_URL=""
|
||||
TOKEN=""
|
||||
AGENT_NAME=""
|
||||
VERSION=""
|
||||
|
||||
while [ $# -gt 0 ]; do
|
||||
case "$1" in
|
||||
--url) API_URL="$2"; shift 2 ;;
|
||||
--token) TOKEN="$2"; shift 2 ;;
|
||||
--name) AGENT_NAME="$2"; shift 2 ;;
|
||||
--version) VERSION="$2"; shift 2 ;;
|
||||
*) echo "unknown flag: $1" >&2; exit 2 ;;
|
||||
esac
|
||||
done
|
||||
|
||||
[ -n "$API_URL" ] || { echo "--url is required" >&2; exit 2; }
|
||||
[ -n "$TOKEN" ] || { echo "--token is required (create one: containr nodes tokens create)" >&2; exit 2; }
|
||||
API_URL="${API_URL%/}"
|
||||
API_URL="${API_URL%/api/v1}"
|
||||
API_URL="${API_URL%/api}"
|
||||
|
||||
info() { printf '\033[1;34m==>\033[0m %s\n' "$*"; }
|
||||
err() { printf '\033[1;31mError:\033[0m %s\n' "$*" >&2; }
|
||||
|
||||
[ "$(id -u)" = "0" ] || { err "run as root (sudo bash -s -- --url … --token …)"; exit 1; }
|
||||
command -v docker >/dev/null 2>&1 || { err "docker not found"; exit 1; }
|
||||
command -v curl >/dev/null 2>&1 || { err "curl not found"; exit 1; }
|
||||
|
||||
ARCH="$(uname -m)"
|
||||
case "$ARCH" in
|
||||
x86_64|amd64) ARCH=amd64 ;;
|
||||
aarch64|arm64) ARCH=arm64 ;;
|
||||
*) err "unsupported architecture: $ARCH"; exit 1 ;;
|
||||
esac
|
||||
|
||||
BIN=/usr/local/bin/containr-agent
|
||||
info "downloading containr-agent (linux/$ARCH)"
|
||||
if ! curl -fsSL "$API_URL/api/agents/download/linux-$ARCH" -o "$BIN" 2>/dev/null; then
|
||||
info "instance has no bundled binary — trying GitHub releases"
|
||||
REF="${VERSION:-latest}"
|
||||
if [ "$REF" = "latest" ]; then
|
||||
DL="https://github.com/Dvorinka/Containr/releases/latest/download/containr-agent-linux-$ARCH"
|
||||
else
|
||||
DL="https://github.com/Dvorinka/Containr/releases/download/$REF/containr-agent-linux-$ARCH"
|
||||
fi
|
||||
curl -fsSL "$DL" -o "$BIN" || { err "agent download failed"; exit 1; }
|
||||
fi
|
||||
chmod +x "$BIN"
|
||||
|
||||
UNIT=/etc/systemd/system/containr-agent.service
|
||||
info "writing systemd unit"
|
||||
cat > "$UNIT" <<UNIT
|
||||
[Unit]
|
||||
Description=Containr node agent
|
||||
After=docker.service network-online.target
|
||||
Wants=network-online.target
|
||||
Requires=docker.service
|
||||
|
||||
[Service]
|
||||
Environment=CONTAINR_API_URL=$API_URL
|
||||
Environment=CONTAINR_AGENT_AUTH_TOKEN=$TOKEN
|
||||
Environment=CONTAINR_AGENT_NAME=${AGENT_NAME:-$(hostname)}
|
||||
Restart=always
|
||||
RestartSec=5
|
||||
ExecStart=$BIN
|
||||
|
||||
[Install]
|
||||
WantedBy=multi-user.target
|
||||
UNIT
|
||||
|
||||
systemctl daemon-reload
|
||||
systemctl enable --now containr-agent
|
||||
info "agent installed and started — it should appear in Containr within ~15s"
|
||||
@@ -2,6 +2,9 @@ package api
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -11,6 +14,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sqlc-dev/pqtype"
|
||||
)
|
||||
|
||||
type AuditLog struct {
|
||||
@@ -23,9 +27,67 @@ type AuditLog struct {
|
||||
Details string `json:"details" db:"details"`
|
||||
IPAddress string `json:"ip_address" db:"ip_address"`
|
||||
UserAgent string `json:"user_agent" db:"user_agent"`
|
||||
Severity string `json:"severity" db:"severity"`
|
||||
Category string `json:"category" db:"category"`
|
||||
Label string `json:"label" db:"label"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
}
|
||||
|
||||
// auditVerbs maps action verbs to past-tense words for feed labels.
|
||||
var auditVerbs = map[string]string{
|
||||
"create": "created", "delete": "deleted", "update": "updated",
|
||||
"deploy": "deployed", "clone": "cloned", "move": "moved",
|
||||
"backup": "backed up", "restore": "restored", "revoke": "revoked",
|
||||
"impersonate": "impersonated", "provision": "provisioned",
|
||||
"accept": "accepted", "login": "logged in", "scale": "scaled",
|
||||
"sleep": "put to sleep", "wake": "woke", "approve": "approved",
|
||||
"set_admin": "admin flag changed", "provisioned": "provisioned",
|
||||
}
|
||||
|
||||
// classifyAuditEvent derives severity/category/label for the activity feed.
|
||||
// Actions may already carry the resource prefix ("service.clone") — it is
|
||||
// stripped before matching.
|
||||
func classifyAuditEvent(resource, action string) (severity, category, label string) {
|
||||
verb := action
|
||||
if idx := strings.LastIndex(verb, "."); idx >= 0 {
|
||||
verb = verb[idx+1:]
|
||||
}
|
||||
|
||||
category = resource
|
||||
switch resource {
|
||||
case "user_invite", "invite", "github_app", "banner", "settings":
|
||||
category = "system"
|
||||
case "user_token", "agent_token", "registry_credential":
|
||||
category = "security"
|
||||
}
|
||||
|
||||
severity = "info"
|
||||
switch {
|
||||
case strings.Contains(verb, "fail"), strings.Contains(verb, "error"), strings.Contains(verb, "denied"):
|
||||
severity = "error"
|
||||
case strings.Contains(verb, "delete"), strings.Contains(verb, "revoke"),
|
||||
strings.Contains(verb, "remove"), strings.Contains(verb, "disable"),
|
||||
strings.Contains(verb, "impersonate"):
|
||||
severity = "warning"
|
||||
case strings.Contains(verb, "create"), strings.Contains(verb, "success"),
|
||||
strings.Contains(verb, "complete"), strings.Contains(verb, "accept"),
|
||||
strings.Contains(verb, "provision"), strings.Contains(verb, "clone"),
|
||||
strings.Contains(verb, "deploy"), strings.Contains(verb, "approve"):
|
||||
severity = "success"
|
||||
}
|
||||
|
||||
past := verb
|
||||
if v, ok := auditVerbs[verb]; ok {
|
||||
past = v
|
||||
}
|
||||
resName := strings.ReplaceAll(resource, "_", " ")
|
||||
if resName != "" {
|
||||
resName = strings.ToUpper(resName[:1]) + resName[1:]
|
||||
}
|
||||
label = resName + " " + past
|
||||
return severity, category, label
|
||||
}
|
||||
|
||||
type AuditLogDetail struct {
|
||||
OldValue interface{} `json:"old_value,omitempty"`
|
||||
NewValue interface{} `json:"new_value,omitempty"`
|
||||
@@ -40,23 +102,33 @@ func LogAudit(userID, resource, resourceID, action string, details map[string]in
|
||||
}
|
||||
|
||||
detailsJSON, _ := json.Marshal(details)
|
||||
resourceUUID := parseUUIDOrNil(resourceID)
|
||||
userUUID := parseUUIDOrNil(userID)
|
||||
severity, category, label := classifyAuditEvent(resource, action)
|
||||
|
||||
auditID := uuid.New().String()
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO audit_logs (id, user_id, resource, resource_id, action, details, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
||||
auditID, userUUID, resource, resourceUUID, action, string(detailsJSON), time.Now().UTC(),
|
||||
)
|
||||
err := sqlcdb.New(db.DB).InsertAuditLog(context.Background(), sqlcdb.InsertAuditLogParams{
|
||||
ID: uuid.New(),
|
||||
UserID: nullUUIDOrNil(userID),
|
||||
Resource: resource,
|
||||
ResourceID: nullUUIDOrNil(resourceID),
|
||||
Action: action,
|
||||
Details: pqtype.NullRawMessage{RawMessage: detailsJSON, Valid: true},
|
||||
Severity: severity,
|
||||
Category: category,
|
||||
Label: label,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
emitWebhookEvent(userID, resource, resourceID, action, details)
|
||||
}
|
||||
|
||||
func LogAuditWithRequest(c *gin.Context, resource, resourceID, action string, details map[string]interface{}) {
|
||||
userID, _ := c.Get("user_id")
|
||||
|
||||
if details == nil {
|
||||
details = map[string]interface{}{}
|
||||
}
|
||||
details["ip_address"] = c.ClientIP()
|
||||
details["user_agent"] = c.GetHeader("User-Agent")
|
||||
|
||||
@@ -67,18 +139,29 @@ func LogAuditWithRequest(c *gin.Context, resource, resourceID, action string, de
|
||||
if uid, ok := userID.(string); ok {
|
||||
userIDStr = uid
|
||||
}
|
||||
userUUID := parseUUIDOrNil(userIDStr)
|
||||
resourceUUID := parseUUIDOrNil(resourceID)
|
||||
severity, category, label := classifyAuditEvent(resource, action)
|
||||
|
||||
auditID := uuid.New().String()
|
||||
_, err := db.Exec(
|
||||
`INSERT INTO audit_logs (id, user_id, resource, resource_id, action, details, ip_address, user_agent, created_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6::jsonb, $7, $8, $9)`,
|
||||
auditID, userUUID, resource, resourceUUID, action, string(detailsJSON), c.ClientIP(), c.GetHeader("User-Agent"), time.Now().UTC(),
|
||||
)
|
||||
var inet pqtype.Inet
|
||||
_ = inet.Scan(c.ClientIP())
|
||||
err := sqlcdb.New(db.DB).InsertAuditLogWithRequest(context.Background(), sqlcdb.InsertAuditLogWithRequestParams{
|
||||
ID: uuid.New(),
|
||||
UserID: nullUUIDOrNil(userIDStr),
|
||||
Resource: resource,
|
||||
ResourceID: nullUUIDOrNil(resourceID),
|
||||
Action: action,
|
||||
Column6: json.RawMessage(detailsJSON),
|
||||
Column7: inet,
|
||||
UserAgent: sql.NullString{String: c.GetHeader("User-Agent"), Valid: true},
|
||||
Severity: severity,
|
||||
Category: category,
|
||||
Label: label,
|
||||
CreatedAt: time.Now().UTC(),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
}
|
||||
|
||||
emitWebhookEvent(userIDStr, resource, resourceID, action, details)
|
||||
}
|
||||
|
||||
var auditDB *database.DB
|
||||
@@ -151,6 +234,9 @@ func handleGetAuditLogs(c *gin.Context) {
|
||||
COALESCE(a.details::text, '{}'),
|
||||
COALESCE(a.ip_address::text, ''),
|
||||
COALESCE(a.user_agent, ''),
|
||||
COALESCE(a.severity, 'info'),
|
||||
COALESCE(a.category, ''),
|
||||
COALESCE(a.label, ''),
|
||||
a.created_at
|
||||
FROM audit_logs a
|
||||
LEFT JOIN users u ON u.id = a.user_id
|
||||
@@ -169,7 +255,7 @@ func handleGetAuditLogs(c *gin.Context) {
|
||||
var logs []AuditLog
|
||||
for rows.Next() {
|
||||
var log AuditLog
|
||||
err := rows.Scan(&log.ID, &log.UserID, &log.UserEmail, &log.Resource, &log.ResourceID, &log.Action, &log.Details, &log.IPAddress, &log.UserAgent, &log.CreatedAt)
|
||||
err := rows.Scan(&log.ID, &log.UserID, &log.UserEmail, &log.Resource, &log.ResourceID, &log.Action, &log.Details, &log.IPAddress, &log.UserAgent, &log.Severity, &log.Category, &log.Label, &log.CreatedAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -185,30 +271,29 @@ func handleGetResourceAuditLogs(c *gin.Context) {
|
||||
resource := c.Param("resource")
|
||||
resourceID := c.Param("id")
|
||||
|
||||
rows, err := db.Query(
|
||||
`SELECT id, COALESCE(user_id::text, ''), resource, COALESCE(resource_id::text, ''), action, COALESCE(details::text, '{}'),
|
||||
COALESCE(ip_address::text, ''), COALESCE(user_agent, ''), created_at
|
||||
FROM audit_logs
|
||||
WHERE user_id::text = $1 AND resource = $2 AND resource_id::text = $3
|
||||
ORDER BY created_at DESC
|
||||
LIMIT 100`,
|
||||
userID, resource, resourceID,
|
||||
)
|
||||
|
||||
rows, err := sqlcdb.New(db.DB).ListResourceAuditLogs(context.Background(), sqlcdb.ListResourceAuditLogsParams{
|
||||
UserID: nullUUIDOrNil(userID),
|
||||
Resource: resource,
|
||||
ResourceID: nullUUIDOrNil(resourceID),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch audit logs"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var logs []AuditLog
|
||||
for rows.Next() {
|
||||
var log AuditLog
|
||||
err := rows.Scan(&log.ID, &log.UserID, &log.Resource, &log.ResourceID, &log.Action, &log.Details, &log.IPAddress, &log.UserAgent, &log.CreatedAt)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
logs = append(logs, log)
|
||||
for _, row := range rows {
|
||||
logs = append(logs, AuditLog{
|
||||
ID: row.ID.String(),
|
||||
UserID: row.UserID,
|
||||
Resource: row.Resource,
|
||||
ResourceID: row.ResourceID,
|
||||
Action: row.Action,
|
||||
Details: row.Details,
|
||||
IPAddress: row.IpAddress,
|
||||
UserAgent: row.UserAgent,
|
||||
CreatedAt: row.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"audit_logs": logs})
|
||||
@@ -232,3 +317,8 @@ func parseUUIDOrNil(raw string) interface{} {
|
||||
}
|
||||
return trimmed
|
||||
}
|
||||
|
||||
func nullUUIDOrNil(raw string) uuid.NullUUID {
|
||||
parsed, err := uuid.Parse(strings.TrimSpace(raw))
|
||||
return uuid.NullUUID{UUID: parsed, Valid: err == nil}
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package api
|
||||
import (
|
||||
"bytes"
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
@@ -63,11 +65,16 @@ func handleLogin(c *gin.Context) {
|
||||
// Find user by email
|
||||
var user User
|
||||
var hashedPassword string
|
||||
err := db.QueryRow(`
|
||||
SELECT id, email, password_hash, name, COALESCE(avatar_url, ''), is_admin, created_at
|
||||
FROM users
|
||||
WHERE email = $1
|
||||
`, req.Email).Scan(&user.ID, &user.Email, &hashedPassword, &user.Name, &user.AvatarURL, &user.IsAdmin, &user.CreatedAt)
|
||||
urow, err := sqlcdb.New(db.DB).GetUserByEmailForAuth(context.Background(), req.Email)
|
||||
user = User{
|
||||
ID: urow.ID.String(),
|
||||
Email: urow.Email,
|
||||
Name: urow.Name,
|
||||
AvatarURL: urow.AvatarUrl,
|
||||
IsAdmin: urow.IsAdmin,
|
||||
CreatedAt: urow.CreatedAt.Time.String(),
|
||||
}
|
||||
hashedPassword = urow.PasswordHash
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
authUser, ok := verifyBetterAuthCredentials(c, req.Email, req.Password)
|
||||
@@ -257,25 +264,30 @@ func createLocalUserFromBetterAuth(db *database.DB, authUser betterAuthLoginUser
|
||||
}
|
||||
|
||||
// A mirrored first account owns the platform, same as handleRegister.
|
||||
var total int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM users").Scan(&total); err != nil {
|
||||
q := sqlcdb.New(db.DB)
|
||||
total64, err := q.CountUsers(context.Background())
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
|
||||
var user User
|
||||
err = db.QueryRow(`
|
||||
INSERT INTO users (email, password_hash, name, avatar_url, is_admin)
|
||||
VALUES ($1, $2, $3, NULLIF($4, ''), $5)
|
||||
ON CONFLICT (email) DO UPDATE
|
||||
SET name = EXCLUDED.name,
|
||||
avatar_url = COALESCE(EXCLUDED.avatar_url, users.avatar_url),
|
||||
updated_at = NOW()
|
||||
RETURNING id, email, name, COALESCE(avatar_url, ''), is_admin, created_at
|
||||
`, email, string(hashedPassword), name, strings.TrimSpace(authUser.Image), total == 0).
|
||||
Scan(&user.ID, &user.Email, &user.Name, &user.AvatarURL, &user.IsAdmin, &user.CreatedAt)
|
||||
urow, err := q.UpsertLocalUser(context.Background(), sqlcdb.UpsertLocalUserParams{
|
||||
Email: email,
|
||||
PasswordHash: string(hashedPassword),
|
||||
Name: name,
|
||||
Column4: strings.TrimSpace(authUser.Image),
|
||||
IsAdmin: total64 == 0,
|
||||
})
|
||||
if err != nil {
|
||||
return User{}, err
|
||||
}
|
||||
user := User{
|
||||
ID: urow.ID.String(),
|
||||
Email: urow.Email,
|
||||
Name: urow.Name,
|
||||
AvatarURL: urow.AvatarUrl,
|
||||
IsAdmin: urow.IsAdmin,
|
||||
CreatedAt: urow.CreatedAt.Time.String(),
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
@@ -302,8 +314,8 @@ func handleRegister(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var existing int
|
||||
err = db.QueryRow("SELECT COUNT(*) FROM users WHERE email = $1", strings.ToLower(strings.TrimSpace(req.Email))).Scan(&existing)
|
||||
q := sqlcdb.New(db.DB)
|
||||
existing, err := q.CountUsersByEmail(context.Background(), strings.ToLower(strings.TrimSpace(req.Email)))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
@@ -322,12 +334,20 @@ func handleRegister(c *gin.Context) {
|
||||
}
|
||||
|
||||
// First account owns the platform.
|
||||
var user User
|
||||
err = db.QueryRow(`
|
||||
INSERT INTO users (email, password_hash, name, is_admin)
|
||||
VALUES ($1, $2, $3, $4)
|
||||
RETURNING id, email, name, COALESCE(avatar_url, ''), is_admin, created_at
|
||||
`, req.Email, string(hashedPassword), req.Name, total == 0).Scan(&user.ID, &user.Email, &user.Name, &user.AvatarURL, &user.IsAdmin, &user.CreatedAt)
|
||||
urow, err := q.InsertUserAdmin(context.Background(), sqlcdb.InsertUserAdminParams{
|
||||
Email: req.Email,
|
||||
PasswordHash: string(hashedPassword),
|
||||
Name: req.Name,
|
||||
IsAdmin: total == 0,
|
||||
})
|
||||
user := User{
|
||||
ID: urow.ID.String(),
|
||||
Email: urow.Email,
|
||||
Name: urow.Name,
|
||||
AvatarURL: urow.AvatarUrl,
|
||||
IsAdmin: urow.IsAdmin,
|
||||
CreatedAt: urow.CreatedAt.Time.String(),
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create user"})
|
||||
@@ -365,8 +385,8 @@ func handleCreateUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var existing int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM users WHERE email = $1", req.Email).Scan(&existing); err != nil {
|
||||
existing, err := sqlcdb.New(db.DB).CountUsersByEmail(context.Background(), req.Email)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
@@ -390,12 +410,19 @@ func handleCreateUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var user User
|
||||
err = db.QueryRow(`
|
||||
INSERT INTO users (email, password_hash, name)
|
||||
VALUES ($1, $2, $3)
|
||||
RETURNING id, email, name, COALESCE(avatar_url, ''), is_admin, created_at
|
||||
`, req.Email, string(hashedPassword), req.Name).Scan(&user.ID, &user.Email, &user.Name, &user.AvatarURL, &user.IsAdmin, &user.CreatedAt)
|
||||
urow, err := sqlcdb.New(db.DB).InsertUser(context.Background(), sqlcdb.InsertUserParams{
|
||||
Email: req.Email,
|
||||
PasswordHash: string(hashedPassword),
|
||||
Name: req.Name,
|
||||
})
|
||||
user := User{
|
||||
ID: urow.ID.String(),
|
||||
Email: urow.Email,
|
||||
Name: urow.Name,
|
||||
AvatarURL: urow.AvatarUrl,
|
||||
IsAdmin: urow.IsAdmin,
|
||||
CreatedAt: urow.CreatedAt.Time.String(),
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "User already exists"})
|
||||
return
|
||||
@@ -458,10 +485,11 @@ func createBetterAuthUser(c *gin.Context, name string, email string, password st
|
||||
}
|
||||
|
||||
func countLocalUsers(db *database.DB) (int, error) {
|
||||
var count int
|
||||
if err := db.QueryRow("SELECT COUNT(*) FROM users").Scan(&count); err != nil {
|
||||
count64, err := sqlcdb.New(db.DB).CountUsers(context.Background())
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
count := int(count64)
|
||||
// Better Auth writes to auth_users; a row there only mirrors into users on
|
||||
// the first authenticated call. Count it too or a second public sign-up
|
||||
// slips through the bootstrap window. UNION by email avoids double-counting
|
||||
@@ -489,11 +517,15 @@ func handleGetProfile(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
|
||||
var user User
|
||||
err := db.QueryRow(`
|
||||
SELECT id, email, name, COALESCE(avatar_url, ''), is_admin, created_at
|
||||
FROM users
|
||||
WHERE id = $1
|
||||
`, userID).Scan(&user.ID, &user.Email, &user.Name, &user.AvatarURL, &user.IsAdmin, &user.CreatedAt)
|
||||
urow, err := sqlcdb.New(db.DB).GetUserByID(context.Background(), uuid.MustParse(userID))
|
||||
user = User{
|
||||
ID: urow.ID.String(),
|
||||
Email: urow.Email,
|
||||
Name: urow.Name,
|
||||
AvatarURL: urow.AvatarUrl,
|
||||
IsAdmin: urow.IsAdmin,
|
||||
CreatedAt: urow.CreatedAt.Time.String(),
|
||||
}
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "User not found"})
|
||||
@@ -521,11 +553,11 @@ func handleUpdateProfile(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Update user profile
|
||||
_, err := db.Exec(`
|
||||
UPDATE users
|
||||
SET name = COALESCE($1, name), avatar_url = COALESCE($2, avatar_url)
|
||||
WHERE id = $3
|
||||
`, req.Name, req.AvatarURL, userID)
|
||||
err := sqlcdb.New(db.DB).UpdateUserProfile(context.Background(), sqlcdb.UpdateUserProfileParams{
|
||||
Column1: req.Name,
|
||||
Column2: req.AvatarURL,
|
||||
ID: uuid.MustParse(userID),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update profile"})
|
||||
@@ -546,6 +578,19 @@ func generateJWT(userID, email, secret string) (string, error) {
|
||||
return token.SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
// generateImpersonationJWT mints a short-lived token acting as `userID`,
|
||||
// tagged with the admin who minted it so downstream callers can attribute
|
||||
// actions taken while impersonating.
|
||||
func generateImpersonationJWT(userID, email, actorID, secret string, expiresAt time.Time) (string, error) {
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
|
||||
"user_id": userID,
|
||||
"email": email,
|
||||
"impersonated_by": actorID,
|
||||
"exp": expiresAt.Unix(),
|
||||
})
|
||||
return token.SignedString([]byte(secret))
|
||||
}
|
||||
|
||||
// ValidateJWT validates a JWT token and returns the claims
|
||||
func ValidateJWT(tokenString, secret string) (jwt.MapClaims, error) {
|
||||
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
|
||||
|
||||
@@ -0,0 +1,646 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/docker"
|
||||
"containr/internal/secrets"
|
||||
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
// BackupTarget is an S3-compatible archive destination. Secret keys are
|
||||
// stored encrypted and never serialized — has_credentials signals presence.
|
||||
type BackupTarget struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Bucket string `json:"bucket"`
|
||||
Region string `json:"region,omitempty"`
|
||||
Prefix string `json:"prefix,omitempty"`
|
||||
HasCredentials bool `json:"has_credentials"`
|
||||
UseTLS bool `json:"use_tls"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
|
||||
accessKey string `json:"-"`
|
||||
secretKey string `json:"-"`
|
||||
}
|
||||
|
||||
type backupTargetRequest struct {
|
||||
Name string `json:"name"`
|
||||
Endpoint string `json:"endpoint" binding:"required"`
|
||||
Bucket string `json:"bucket" binding:"required"`
|
||||
Region string `json:"region"`
|
||||
Prefix string `json:"prefix"`
|
||||
AccessKey string `json:"access_key"`
|
||||
SecretKey string `json:"secret_key"`
|
||||
UseTLS *bool `json:"use_tls"`
|
||||
}
|
||||
|
||||
func mapBackupTargetRow(row sqlcdb.BackupTarget) BackupTarget {
|
||||
return BackupTarget{
|
||||
ID: row.ID,
|
||||
Name: row.Name,
|
||||
Endpoint: row.Endpoint,
|
||||
Bucket: row.Bucket,
|
||||
Region: row.Region,
|
||||
Prefix: row.Prefix,
|
||||
HasCredentials: row.SecretKey != "",
|
||||
UseTLS: row.UseTls,
|
||||
CreatedAt: databaseNullTime(row.CreatedAt),
|
||||
UpdatedAt: databaseNullTime(row.UpdatedAt),
|
||||
accessKey: secrets.Decrypt(row.AccessKey),
|
||||
secretKey: secrets.Decrypt(row.SecretKey),
|
||||
}
|
||||
}
|
||||
|
||||
// s3Client builds a client for the target. Endpoint is a bare host[:port] —
|
||||
// scheme comes from use_tls, matching MinIO/Garage/AWS conventions.
|
||||
func (t BackupTarget) s3Client() (*minio.Client, error) {
|
||||
opts := &minio.Options{Secure: t.UseTLS, Region: t.Region}
|
||||
if t.accessKey != "" || t.secretKey != "" {
|
||||
opts.Creds = credentials.NewStaticV4(t.accessKey, t.secretKey, "")
|
||||
} else {
|
||||
opts.Creds = credentials.NewIAM("")
|
||||
}
|
||||
return minio.New(t.Endpoint, opts)
|
||||
}
|
||||
|
||||
func normalizeBackupTargetRequest(req *backupTargetRequest) error {
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
req.Endpoint = strings.TrimSpace(req.Endpoint)
|
||||
req.Endpoint = strings.TrimPrefix(strings.TrimPrefix(req.Endpoint, "https://"), "http://")
|
||||
req.Endpoint = strings.TrimSuffix(req.Endpoint, "/")
|
||||
req.Bucket = strings.TrimSpace(req.Bucket)
|
||||
req.Prefix = strings.Trim(strings.TrimSpace(req.Prefix), "/")
|
||||
if req.Name == "" {
|
||||
return errors.New("name is required")
|
||||
}
|
||||
if req.Endpoint == "" || req.Bucket == "" {
|
||||
return errors.New("endpoint and bucket are required")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// probeBackupTarget verifies credentials and bucket access.
|
||||
func probeBackupTarget(ctx context.Context, t BackupTarget) (time.Duration, error) {
|
||||
client, err := t.s3Client()
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
start := time.Now()
|
||||
exists, err := client.BucketExists(ctx, t.Bucket)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
if !exists {
|
||||
return 0, fmt.Errorf("bucket %q not found or not accessible", t.Bucket)
|
||||
}
|
||||
return time.Since(start), nil
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) ListBackupTargets(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
rows, err := h.queries.ListBackupTargetsByUser(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list backup targets"})
|
||||
return
|
||||
}
|
||||
targets := make([]BackupTarget, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
t := mapBackupTargetRow(row)
|
||||
t.accessKey, t.secretKey = "", ""
|
||||
targets = append(targets, t)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"backup_targets": targets})
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) CreateBackupTarget(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req backupTargetRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if err := normalizeBackupTargetRequest(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
useTLS := true
|
||||
if req.UseTLS != nil {
|
||||
useTLS = *req.UseTLS
|
||||
}
|
||||
target := BackupTarget{
|
||||
ID: "bt_" + strings.ReplaceAll(uuid.NewString(), "-", "")[:12],
|
||||
Name: req.Name,
|
||||
Endpoint: req.Endpoint,
|
||||
Bucket: req.Bucket,
|
||||
Region: req.Region,
|
||||
Prefix: req.Prefix,
|
||||
UseTLS: useTLS,
|
||||
accessKey: req.AccessKey,
|
||||
secretKey: req.SecretKey,
|
||||
}
|
||||
|
||||
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, err := probeBackupTarget(probeCtx, target); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Connection check failed: %v", err), "code": "DEPENDENCY_UNAVAILABLE"})
|
||||
return
|
||||
}
|
||||
|
||||
enc := func(v string) string {
|
||||
if v == "" {
|
||||
return ""
|
||||
}
|
||||
return secrets.Encrypt(v)
|
||||
}
|
||||
now := sql.NullTime{Time: time.Now(), Valid: true}
|
||||
if err := h.queries.CreateBackupTarget(c.Request.Context(), sqlcdb.CreateBackupTargetParams{
|
||||
ID: target.ID,
|
||||
UserID: userID,
|
||||
Name: target.Name,
|
||||
Endpoint: target.Endpoint,
|
||||
Bucket: target.Bucket,
|
||||
Region: target.Region,
|
||||
Prefix: target.Prefix,
|
||||
AccessKey: enc(req.AccessKey),
|
||||
SecretKey: enc(req.SecretKey),
|
||||
UseTls: useTLS,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create backup target"})
|
||||
return
|
||||
}
|
||||
LogAudit(userID, "backup_target", target.ID, "create", map[string]interface{}{"name": req.Name, "endpoint": req.Endpoint, "bucket": req.Bucket})
|
||||
target.accessKey, target.secretKey = "", ""
|
||||
target.HasCredentials = req.SecretKey != ""
|
||||
target.CreatedAt, target.UpdatedAt = now.Time, now.Time
|
||||
c.JSON(http.StatusCreated, gin.H{"backup_target": target})
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) UpdateBackupTarget(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
targetID := c.Param("id")
|
||||
row, err := h.queries.GetBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.GetBackupTargetByIDAndUserParams{ID: targetID, UserID: userID})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Backup target not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
var req backupTargetRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if err := normalizeBackupTargetRequest(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
useTLS := row.UseTls
|
||||
if req.UseTLS != nil {
|
||||
useTLS = *req.UseTLS
|
||||
}
|
||||
// Empty credentials keep the stored pair.
|
||||
accessKey := row.AccessKey
|
||||
secretKey := row.SecretKey
|
||||
if req.AccessKey != "" || req.SecretKey != "" {
|
||||
accessKey = secrets.Encrypt(req.AccessKey)
|
||||
secretKey = secrets.Encrypt(req.SecretKey)
|
||||
}
|
||||
|
||||
target := BackupTarget{
|
||||
Endpoint: req.Endpoint,
|
||||
Bucket: req.Bucket,
|
||||
Region: req.Region,
|
||||
Prefix: req.Prefix,
|
||||
UseTLS: useTLS,
|
||||
accessKey: secrets.Decrypt(accessKey),
|
||||
secretKey: secrets.Decrypt(secretKey),
|
||||
}
|
||||
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, err := probeBackupTarget(probeCtx, target); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("Connection check failed: %v", err), "code": "DEPENDENCY_UNAVAILABLE"})
|
||||
return
|
||||
}
|
||||
|
||||
if err := h.queries.UpdateBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.UpdateBackupTargetByIDAndUserParams{
|
||||
Name: req.Name,
|
||||
Endpoint: req.Endpoint,
|
||||
Bucket: req.Bucket,
|
||||
Region: req.Region,
|
||||
Prefix: req.Prefix,
|
||||
AccessKey: accessKey,
|
||||
SecretKey: secretKey,
|
||||
UseTls: useTLS,
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: targetID,
|
||||
UserID: userID,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update backup target"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Backup target updated"})
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) DeleteBackupTarget(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
targetID := c.Param("id")
|
||||
count, err := h.queries.CountDatabasesUsingBackupTarget(c.Request.Context(), sql.NullString{String: targetID, Valid: true})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check target usage"})
|
||||
return
|
||||
}
|
||||
if count > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": fmt.Sprintf("%d database(s) still back up to this target", count), "code": "CONFLICT"})
|
||||
return
|
||||
}
|
||||
if err := h.queries.DeleteBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.DeleteBackupTargetByIDAndUserParams{ID: targetID, UserID: userID}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete backup target"})
|
||||
return
|
||||
}
|
||||
LogAudit(userID, "backup_target", targetID, "delete", nil)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Backup target deleted"})
|
||||
}
|
||||
|
||||
// ImportBackup accepts a `.tar.gz` archive (multipart `file` field or raw
|
||||
// body) and stores it as a restore point — the counterpart to offsite
|
||||
// export, and the entry path for migrators.
|
||||
func (h *DatabaseHandler) ImportBackup(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
databaseID := c.Param("id")
|
||||
userID = h.effectiveUserID(c, userID, databaseID)
|
||||
|
||||
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||
ID: databaseID, UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
if row.Provider == "external" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "backup import applies to managed databases — external restores are not supported yet", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
// Buffer to a temp file — tar headers need the size up front, and a
|
||||
// streamed upload can't be rewound for the archive write.
|
||||
src, _, err := openUploadStream(c)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
defer src.Close()
|
||||
|
||||
tmp, err := os.CreateTemp("", "containr-backup-import-*")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to stage upload"})
|
||||
return
|
||||
}
|
||||
defer func() { tmp.Close(); os.Remove(tmp.Name()) }()
|
||||
|
||||
const maxImport = 2 << 30 // 2 GiB ceiling for staged archives
|
||||
size, err := io.Copy(tmp, http.MaxBytesReader(c.Writer, src, maxImport))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusRequestEntityTooLarge, gin.H{"error": "archive too large or unreadable", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read staged upload"})
|
||||
return
|
||||
}
|
||||
magic := make([]byte, 2)
|
||||
if _, err := tmp.Read(magic); err != nil || magic[0] != 0x1f || magic[1] != 0x8b {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "archive must be gzip-compressed (.tar.gz)", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if _, err := tmp.Seek(0, io.SeekStart); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read staged upload"})
|
||||
return
|
||||
}
|
||||
|
||||
backupID := generateDatabaseBackupID(databaseID)
|
||||
archivePath := sanitizeBackupArchivePath(managedDatabaseBackupArchivePath(backupID))
|
||||
|
||||
if _, err := h.dockerClient.CreateVolume(c.Request.Context(), docker.VolumeConfig{Name: managedDatabaseBackupVolume}); err != nil && !strings.Contains(strings.ToLower(err.Error()), "already exists") {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to prepare backup volume"})
|
||||
return
|
||||
}
|
||||
holderID, err := h.createBackupVolumeHolder(c.Request.Context())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to open backup volume: %v", err)})
|
||||
return
|
||||
}
|
||||
defer func() { _ = h.dockerClient.RemoveContainer(context.Background(), holderID, true) }()
|
||||
|
||||
pr, pw := io.Pipe()
|
||||
tw := tar.NewWriter(pw)
|
||||
go func() {
|
||||
err := tw.WriteHeader(&tar.Header{Name: archivePath, Mode: 0o644, Size: size})
|
||||
if err == nil {
|
||||
_, err = io.Copy(tw, tmp)
|
||||
}
|
||||
if cerr := tw.Close(); err == nil {
|
||||
err = cerr
|
||||
}
|
||||
_ = pw.CloseWithError(err)
|
||||
}()
|
||||
if err := h.dockerClient.CopyToContainer(c.Request.Context(), holderID, "/backup", pr); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": fmt.Sprintf("Failed to store archive: %v", err)})
|
||||
return
|
||||
}
|
||||
|
||||
now := sql.NullTime{Time: time.Now(), Valid: true}
|
||||
if err := h.queries.CreateDatabaseBackup(c.Request.Context(), sqlcdb.CreateDatabaseBackupParams{
|
||||
ID: backupID,
|
||||
DatabaseID: databaseID,
|
||||
Size: humanReadableBytes(size),
|
||||
Status: "completed",
|
||||
BackupPath: sql.NullString{String: archivePath, Valid: true},
|
||||
CreatedAt: now,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to register backup"})
|
||||
return
|
||||
}
|
||||
_ = h.queries.SetDatabaseBackupStatusByID(c.Request.Context(), sqlcdb.SetDatabaseBackupStatusByIDParams{
|
||||
Status: "completed",
|
||||
Size: humanReadableBytes(size),
|
||||
CompletedAt: now,
|
||||
ID: backupID,
|
||||
})
|
||||
|
||||
// Imported archives ship offsite too when a target is configured —
|
||||
// the import should be a first-class restore point.
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
||||
defer cancel()
|
||||
if key, err := h.uploadBackupToTarget(ctx, row, archivePath, backupID); err != nil {
|
||||
log.Printf("containr: imported backup %s remote upload failed: %v (local archive kept)", backupID, err)
|
||||
} else if key != "" {
|
||||
log.Printf("containr: imported backup %s shipped to %s", backupID, key)
|
||||
}
|
||||
}()
|
||||
|
||||
LogAudit(userID, "database", databaseID, "backup_import", map[string]interface{}{"backup_id": backupID, "size": size})
|
||||
c.JSON(http.StatusCreated, gin.H{"backup_id": backupID, "size": humanReadableBytes(size), "status": "completed"})
|
||||
}
|
||||
|
||||
// openUploadStream resolves the request body: multipart `file` field when
|
||||
// present, otherwise the raw body (e.g. CLI pipe of a .tar.gz).
|
||||
func openUploadStream(c *gin.Context) (io.ReadCloser, string, error) {
|
||||
if fh, err := c.FormFile("file"); err == nil {
|
||||
src, err := fh.Open()
|
||||
return src, fh.Filename, err
|
||||
}
|
||||
if c.Request.Body == nil || c.Request.ContentLength == 0 {
|
||||
return nil, "", errors.New("no archive uploaded — send multipart field 'file' or a raw .tar.gz body")
|
||||
}
|
||||
return c.Request.Body, "archive.tar.gz", nil
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) TestBackupTarget(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
targetID := c.Param("id")
|
||||
row, err := h.queries.GetBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.GetBackupTargetByIDAndUserParams{ID: targetID, UserID: userID})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Backup target not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
target := mapBackupTargetRow(row)
|
||||
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
latency, probeErr := probeBackupTarget(probeCtx, target)
|
||||
if probeErr != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": false, "error": probeErr.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "latency_ms": latency.Milliseconds()})
|
||||
}
|
||||
|
||||
// uploadBackupToTarget copies the archive out of the backup volume and puts
|
||||
// it in the configured S3 target. Returns the object key, or "" when no
|
||||
// target is configured.
|
||||
func (h *DatabaseHandler) uploadBackupToTarget(ctx context.Context, db sqlcdb.DatabaseService, archivePath, backupID string) (string, error) {
|
||||
if !db.BackupTargetID.Valid || db.BackupTargetID.String == "" {
|
||||
return "", nil
|
||||
}
|
||||
targetRow, err := h.queries.GetBackupTargetByID(ctx, db.BackupTargetID.String)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("backup target lookup failed: %w", err)
|
||||
}
|
||||
target := mapBackupTargetRow(targetRow)
|
||||
|
||||
archive, err := h.readBackupArchive(ctx, archivePath)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("read archive: %w", err)
|
||||
}
|
||||
defer func() {
|
||||
_, _ = io.Copy(io.Discard, archive)
|
||||
_ = archive.Close()
|
||||
}()
|
||||
|
||||
client, err := target.s3Client()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
key := target.Prefix
|
||||
if key != "" {
|
||||
key += "/"
|
||||
}
|
||||
key += "databases/" + db.ID + "/" + sanitizeBackupArchivePath(archivePath)
|
||||
_, err = client.PutObject(ctx, target.Bucket, key, archive, -1, minio.PutObjectOptions{ContentType: "application/gzip"})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("s3 upload: %w", err)
|
||||
}
|
||||
_ = h.queries.SetDatabaseBackupRemoteKeyByID(ctx, sqlcdb.SetDatabaseBackupRemoteKeyByIDParams{
|
||||
RemoteKey: sql.NullString{String: key, Valid: true},
|
||||
ID: backupID,
|
||||
})
|
||||
return key, nil
|
||||
}
|
||||
|
||||
// readBackupArchive streams an archive out of the shared backup volume via a
|
||||
// stopped holder container — the volume has no direct host path.
|
||||
func (h *DatabaseHandler) readBackupArchive(ctx context.Context, archivePath string) (io.ReadCloser, error) {
|
||||
holderID, err := h.createBackupVolumeHolder(ctx)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
reader, err := h.dockerClient.CopyFromContainer(ctx, holderID, "/backup/"+sanitizeBackupArchivePath(archivePath))
|
||||
if err != nil {
|
||||
_ = h.dockerClient.RemoveContainer(context.Background(), holderID, true)
|
||||
return nil, err
|
||||
}
|
||||
// The holder must outlive the stream — deleting the source container while
|
||||
// dockerd serves the archive stalls the read without an error.
|
||||
return &archiveReader{tar: reader, cleanup: func() {
|
||||
_ = h.dockerClient.RemoveContainer(context.Background(), holderID, true)
|
||||
}}, nil
|
||||
}
|
||||
|
||||
// downloadBackupToVolume fetches a remote archive back into the backup volume
|
||||
// so the normal restore path can run unchanged.
|
||||
func (h *DatabaseHandler) downloadBackupToVolume(ctx context.Context, db sqlcdb.DatabaseService, remoteKey, archivePath string) error {
|
||||
targetRow, err := h.queries.GetBackupTargetByID(ctx, db.BackupTargetID.String)
|
||||
if err != nil {
|
||||
return fmt.Errorf("backup target lookup failed: %w", err)
|
||||
}
|
||||
target := mapBackupTargetRow(targetRow)
|
||||
client, err := target.s3Client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
obj, err := client.GetObject(ctx, target.Bucket, remoteKey, minio.GetObjectOptions{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer obj.Close()
|
||||
|
||||
archive := sanitizeBackupArchivePath(archivePath)
|
||||
if archive == "" {
|
||||
return errors.New("backup path is required")
|
||||
}
|
||||
|
||||
var buf bytes.Buffer
|
||||
tw := tar.NewWriter(&buf)
|
||||
data, err := io.ReadAll(obj)
|
||||
if err != nil {
|
||||
return fmt.Errorf("s3 download: %w", err)
|
||||
}
|
||||
if err := tw.WriteHeader(&tar.Header{Name: archive, Mode: 0o644, Size: int64(len(data))}); err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err := tw.Write(data); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := tw.Close(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
holderID, err := h.createBackupVolumeHolder(ctx)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer func() { _ = h.dockerClient.RemoveContainer(context.Background(), holderID, true) }()
|
||||
return h.dockerClient.CopyToContainer(ctx, holderID, "/backup", &buf)
|
||||
}
|
||||
|
||||
// backupArchiveExists reports whether the archive is still in the local
|
||||
// backup volume — a copy attempt on a holder container is the cheapest check.
|
||||
func (h *DatabaseHandler) backupArchiveExists(ctx context.Context, archivePath string) (bool, error) {
|
||||
holderID, err := h.createBackupVolumeHolder(ctx)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
defer func() { _ = h.dockerClient.RemoveContainer(context.Background(), holderID, true) }()
|
||||
reader, err := h.dockerClient.CopyFromContainer(ctx, holderID, "/backup/"+sanitizeBackupArchivePath(archivePath))
|
||||
if err != nil {
|
||||
return false, nil
|
||||
}
|
||||
_ = reader.Close()
|
||||
return true, nil
|
||||
}
|
||||
|
||||
// createBackupVolumeHolder creates a container mounting the backup volume so
|
||||
// archives can be copied in and out. It must be started — docker cp/archive
|
||||
// only resolves volume mounts on containers that have run at least once.
|
||||
func (h *DatabaseHandler) createBackupVolumeHolder(ctx context.Context) (string, error) {
|
||||
if err := h.ensureImage(ctx, managedDatabaseBackupImage); err != nil {
|
||||
return "", err
|
||||
}
|
||||
holderID, err := h.dockerClient.CreateContainer(ctx, docker.ContainerConfig{
|
||||
Image: managedDatabaseBackupImage,
|
||||
Cmd: []string{"true"},
|
||||
RestartPolicy: "no",
|
||||
Mounts: []mount.Mount{{Type: mount.TypeVolume, Source: managedDatabaseBackupVolume, Target: "/backup"}},
|
||||
Labels: map[string]string{"containr.managed": "true", "containr.utility": "backup-io"},
|
||||
NetworkMode: "none",
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
if err := h.dockerClient.StartContainer(ctx, holderID); err != nil {
|
||||
_ = h.dockerClient.RemoveContainer(context.Background(), holderID, true)
|
||||
return "", fmt.Errorf("failed to start backup holder: %w", err)
|
||||
}
|
||||
return holderID, nil
|
||||
}
|
||||
|
||||
// archiveReader unwraps the tar stream CopyFromContainer returns.
|
||||
type archiveReader struct {
|
||||
tar io.ReadCloser
|
||||
tr *tar.Reader
|
||||
err error
|
||||
cleanup func()
|
||||
}
|
||||
|
||||
func (r *archiveReader) Read(p []byte) (int, error) {
|
||||
if r.err != nil {
|
||||
return 0, r.err
|
||||
}
|
||||
if r.tr == nil {
|
||||
r.tr = tar.NewReader(r.tar)
|
||||
// Skip to the first regular file entry.
|
||||
for {
|
||||
hdr, err := r.tr.Next()
|
||||
if err != nil {
|
||||
r.err = err
|
||||
return 0, err
|
||||
}
|
||||
if hdr.Typeflag == tar.TypeReg {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
n, err := r.tr.Read(p)
|
||||
if err != nil {
|
||||
r.err = err
|
||||
}
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (r *archiveReader) Close() error {
|
||||
err := r.tar.Close()
|
||||
if r.cleanup != nil {
|
||||
r.cleanup()
|
||||
r.cleanup = nil
|
||||
}
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"bytes"
|
||||
"io"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeBackupTargetRequest(t *testing.T) {
|
||||
req := backupTargetRequest{
|
||||
Name: " garage ",
|
||||
Endpoint: "https://garage.local:3900/",
|
||||
Bucket: " backups ",
|
||||
Prefix: " /prod/dbs/ ",
|
||||
}
|
||||
if err := normalizeBackupTargetRequest(&req); err != nil {
|
||||
t.Fatalf("normalize failed: %v", err)
|
||||
}
|
||||
if req.Endpoint != "garage.local:3900" {
|
||||
t.Fatalf("endpoint not normalized: %q", req.Endpoint)
|
||||
}
|
||||
if req.Bucket != "backups" || req.Prefix != "prod/dbs" {
|
||||
t.Fatalf("bucket/prefix not trimmed: %q %q", req.Bucket, req.Prefix)
|
||||
}
|
||||
|
||||
for i, bad := range []backupTargetRequest{
|
||||
{Endpoint: "e", Bucket: "b"}, // no name
|
||||
{Name: "n", Bucket: "b"}, // no endpoint
|
||||
{Name: "n", Endpoint: "e"}, // no bucket
|
||||
} {
|
||||
if err := normalizeBackupTargetRequest(&bad); err == nil {
|
||||
t.Fatalf("case %d: expected error", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestArchiveReaderUnwrapsTar(t *testing.T) {
|
||||
var buf bytes.Buffer
|
||||
tw := tar.NewWriter(&buf)
|
||||
payload := []byte("backup-bytes")
|
||||
if err := tw.WriteHeader(&tar.Header{Name: "b.tar.gz", Mode: 0o644, Size: int64(len(payload))}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if _, err := tw.Write(payload); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := tw.Close(); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
r := &archiveReader{tar: io.NopCloser(&buf)}
|
||||
got, err := io.ReadAll(r)
|
||||
if err != nil {
|
||||
t.Fatalf("read: %v", err)
|
||||
}
|
||||
if !bytes.Equal(got, payload) {
|
||||
t.Fatalf("payload mismatch: %q", got)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Instance-wide announcement banners. Admins manage them under
|
||||
// /admin/banners; every signed-in user reads the active set at
|
||||
// /banners/active so the UI (and CLI) can surface announcements.
|
||||
|
||||
var bannerLevels = map[string]bool{"info": true, "success": true, "warning": true, "error": true}
|
||||
|
||||
// bannerJSON flattens sqlc nullable wrappers into plain API values.
|
||||
type bannerJSON struct {
|
||||
ID string `json:"id"`
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Level string `json:"level"`
|
||||
Active bool `json:"active"`
|
||||
Dismissible bool `json:"dismissible"`
|
||||
StartsAt *time.Time `json:"starts_at,omitempty"`
|
||||
EndsAt *time.Time `json:"ends_at,omitempty"`
|
||||
CreatedAt *time.Time `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
func toBannerJSON(b sqlcdb.Banner) bannerJSON {
|
||||
out := bannerJSON{
|
||||
ID: b.ID.String(),
|
||||
Title: b.Title,
|
||||
Body: b.Body,
|
||||
Level: b.Level,
|
||||
Active: b.Active,
|
||||
Dismissible: b.Dismissible,
|
||||
}
|
||||
if b.StartsAt.Valid {
|
||||
out.StartsAt = &b.StartsAt.Time
|
||||
}
|
||||
if b.EndsAt.Valid {
|
||||
out.EndsAt = &b.EndsAt.Time
|
||||
}
|
||||
if b.CreatedAt.Valid {
|
||||
out.CreatedAt = &b.CreatedAt.Time
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func toBannerList(banners []sqlcdb.Banner) []bannerJSON {
|
||||
out := make([]bannerJSON, 0, len(banners))
|
||||
for _, b := range banners {
|
||||
out = append(out, toBannerJSON(b))
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
type bannerRequest struct {
|
||||
Title *string `json:"title"`
|
||||
Body *string `json:"body"`
|
||||
Level *string `json:"level"`
|
||||
Active *bool `json:"active"`
|
||||
Dismissible *bool `json:"dismissible"`
|
||||
StartsAt *time.Time `json:"starts_at"`
|
||||
EndsAt *time.Time `json:"ends_at"`
|
||||
}
|
||||
|
||||
func bannerQueries(c *gin.Context) *sqlcdb.Queries {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
return sqlcdb.New(db.DB)
|
||||
}
|
||||
|
||||
func parseBannerID(c *gin.Context) (uuid.UUID, bool) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid banner id")
|
||||
return uuid.Nil, false
|
||||
}
|
||||
return id, true
|
||||
}
|
||||
|
||||
func validBannerLevel(level string) bool { return bannerLevels[level] }
|
||||
|
||||
func handleListActiveBanners(c *gin.Context) {
|
||||
banners, err := bannerQueries(c).ListActiveBanners(c.Request.Context())
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to list banners")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"banners": toBannerList(banners)})
|
||||
}
|
||||
|
||||
func handleAdminListBanners(c *gin.Context) {
|
||||
banners, err := bannerQueries(c).ListBanners(c.Request.Context())
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to list banners")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"banners": toBannerList(banners)})
|
||||
}
|
||||
|
||||
func handleAdminCreateBanner(c *gin.Context) {
|
||||
var req bannerRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_BODY", "invalid request body")
|
||||
return
|
||||
}
|
||||
if req.Title == nil || *req.Title == "" {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "title is required")
|
||||
return
|
||||
}
|
||||
level := "info"
|
||||
if req.Level != nil {
|
||||
if !validBannerLevel(*req.Level) {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "level must be info, success, warning or error")
|
||||
return
|
||||
}
|
||||
level = *req.Level
|
||||
}
|
||||
userID, _ := requireAuthenticatedUserUUID(c)
|
||||
params := sqlcdb.CreateBannerParams{
|
||||
Title: *req.Title,
|
||||
Level: level,
|
||||
Active: true,
|
||||
Dismissible: true,
|
||||
CreatedBy: uuid.NullUUID{UUID: userID, Valid: true},
|
||||
}
|
||||
if req.Body != nil {
|
||||
params.Body = *req.Body
|
||||
}
|
||||
if req.Active != nil {
|
||||
params.Active = *req.Active
|
||||
}
|
||||
if req.Dismissible != nil {
|
||||
params.Dismissible = *req.Dismissible
|
||||
}
|
||||
if req.StartsAt != nil {
|
||||
params.StartsAt = sql.NullTime{Time: *req.StartsAt, Valid: true}
|
||||
}
|
||||
if req.EndsAt != nil {
|
||||
params.EndsAt = sql.NullTime{Time: *req.EndsAt, Valid: true}
|
||||
}
|
||||
banner, err := bannerQueries(c).CreateBanner(c.Request.Context(), params)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to create banner")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, toBannerJSON(banner))
|
||||
}
|
||||
|
||||
func handleAdminUpdateBanner(c *gin.Context) {
|
||||
id, ok := parseBannerID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
q := bannerQueries(c)
|
||||
banner, err := q.GetBannerByID(c.Request.Context(), id)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "banner not found")
|
||||
return
|
||||
}
|
||||
var req bannerRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_BODY", "invalid request body")
|
||||
return
|
||||
}
|
||||
params := sqlcdb.UpdateBannerParams{
|
||||
ID: id,
|
||||
Title: banner.Title,
|
||||
Body: banner.Body,
|
||||
Level: banner.Level,
|
||||
Active: banner.Active,
|
||||
Dismissible: banner.Dismissible,
|
||||
StartsAt: banner.StartsAt,
|
||||
EndsAt: banner.EndsAt,
|
||||
}
|
||||
if req.Title != nil {
|
||||
params.Title = *req.Title
|
||||
}
|
||||
if req.Body != nil {
|
||||
params.Body = *req.Body
|
||||
}
|
||||
if req.Level != nil {
|
||||
if !validBannerLevel(*req.Level) {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "level must be info, success, warning or error")
|
||||
return
|
||||
}
|
||||
params.Level = *req.Level
|
||||
}
|
||||
if req.Active != nil {
|
||||
params.Active = *req.Active
|
||||
}
|
||||
if req.Dismissible != nil {
|
||||
params.Dismissible = *req.Dismissible
|
||||
}
|
||||
if req.StartsAt != nil {
|
||||
params.StartsAt = sql.NullTime{Time: *req.StartsAt, Valid: true}
|
||||
}
|
||||
if req.EndsAt != nil {
|
||||
params.EndsAt = sql.NullTime{Time: *req.EndsAt, Valid: true}
|
||||
}
|
||||
updated, err := q.UpdateBanner(c.Request.Context(), params)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to update banner")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, toBannerJSON(updated))
|
||||
}
|
||||
|
||||
func handleAdminDeleteBanner(c *gin.Context) {
|
||||
id, ok := parseBannerID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := bannerQueries(c).DeleteBanner(c.Request.Context(), id); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to delete banner")
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"containr/internal/build"
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/docker"
|
||||
"containr/internal/types"
|
||||
|
||||
@@ -669,39 +670,22 @@ func (h *BuildHandler) upsertBuild(status *BuildStatusResponse) error {
|
||||
}
|
||||
|
||||
now := time.Now().UTC()
|
||||
_, err := h.db.Exec(
|
||||
`INSERT INTO builds
|
||||
(id, project_id, service_id, status, progress, started_at, completed_at, image_name, image_tag, size, error, log, metadata, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13::jsonb, $14, $14)
|
||||
ON CONFLICT (id) DO UPDATE SET
|
||||
project_id = EXCLUDED.project_id,
|
||||
service_id = EXCLUDED.service_id,
|
||||
status = EXCLUDED.status,
|
||||
progress = EXCLUDED.progress,
|
||||
started_at = EXCLUDED.started_at,
|
||||
completed_at = EXCLUDED.completed_at,
|
||||
image_name = EXCLUDED.image_name,
|
||||
image_tag = EXCLUDED.image_tag,
|
||||
size = EXCLUDED.size,
|
||||
error = EXCLUDED.error,
|
||||
log = EXCLUDED.log,
|
||||
metadata = EXCLUDED.metadata,
|
||||
updated_at = EXCLUDED.updated_at`,
|
||||
status.ID,
|
||||
nullIfEmptyString(status.ProjectID),
|
||||
nullIfEmptyString(status.ServiceID),
|
||||
status.Status,
|
||||
status.Progress,
|
||||
status.StartedAt,
|
||||
status.CompletedAt,
|
||||
status.ImageName,
|
||||
status.ImageTag,
|
||||
status.Size,
|
||||
nullIfEmptyString(status.Error),
|
||||
status.Log,
|
||||
jsonOrEmptyObject(metadataRaw),
|
||||
now,
|
||||
)
|
||||
err := sqlcdb.New(h.db.DB).UpsertBuild(context.Background(), sqlcdb.UpsertBuildParams{
|
||||
ID: status.ID,
|
||||
ProjectID: nsNullIfEmpty(status.ProjectID),
|
||||
ServiceID: nsNullIfEmpty(status.ServiceID),
|
||||
Status: status.Status,
|
||||
Progress: int32(status.Progress),
|
||||
StartedAt: status.StartedAt,
|
||||
CompletedAt: ntPtr(status.CompletedAt),
|
||||
ImageName: status.ImageName,
|
||||
ImageTag: status.ImageTag,
|
||||
Size: status.Size,
|
||||
Error: nsNullIfEmpty(status.Error),
|
||||
Log: status.Log,
|
||||
Column13: json.RawMessage(jsonOrEmptyObject(metadataRaw)),
|
||||
CreatedAt: now,
|
||||
})
|
||||
if err != nil && h.isMissingBuildsTable(err) {
|
||||
return nil
|
||||
}
|
||||
@@ -713,18 +697,37 @@ func (h *BuildHandler) getBuildFromDB(buildID string) (BuildStatusResponse, bool
|
||||
return BuildStatusResponse{}, false, nil
|
||||
}
|
||||
|
||||
row := h.db.QueryRow(
|
||||
`SELECT id, project_id, service_id, status, progress, started_at, completed_at, image_name, image_tag, size, error, log, metadata
|
||||
FROM builds
|
||||
WHERE id = $1`,
|
||||
buildID,
|
||||
)
|
||||
|
||||
build, found, err := scanBuildRow(row.Scan)
|
||||
if err != nil && h.isMissingBuildsTable(err) {
|
||||
return BuildStatusResponse{}, false, nil
|
||||
row, err := sqlcdb.New(h.db.DB).GetBuild(context.Background(), buildID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) || h.isMissingBuildsTable(err) {
|
||||
return BuildStatusResponse{}, false, nil
|
||||
}
|
||||
return BuildStatusResponse{}, false, err
|
||||
}
|
||||
return build, found, err
|
||||
build := BuildStatusResponse{
|
||||
ID: row.ID,
|
||||
ProjectID: row.ProjectID.String,
|
||||
ServiceID: row.ServiceID.String,
|
||||
Status: row.Status,
|
||||
Progress: int(row.Progress),
|
||||
StartedAt: row.StartedAt.UTC(),
|
||||
ImageName: row.ImageName,
|
||||
ImageTag: row.ImageTag,
|
||||
Size: row.Size,
|
||||
Error: row.Error.String,
|
||||
Log: row.Log,
|
||||
}
|
||||
if t := timePtrNT(row.CompletedAt); t != nil {
|
||||
u := t.UTC()
|
||||
build.CompletedAt = &u
|
||||
}
|
||||
if len(row.Metadata) > 0 {
|
||||
metadata := map[string]string{}
|
||||
if err := json.Unmarshal(row.Metadata, &metadata); err == nil {
|
||||
build.Metadata = metadata
|
||||
}
|
||||
}
|
||||
return build, true, nil
|
||||
}
|
||||
|
||||
func (h *BuildHandler) listBuildsFromDB(projectID, serviceID, status string, page, limit int) ([]BuildStatusResponse, int, error) {
|
||||
@@ -808,19 +811,11 @@ func (h *BuildHandler) cancelBuildInDB(buildID string) bool {
|
||||
return false
|
||||
}
|
||||
now := time.Now().UTC()
|
||||
result, err := h.db.Exec(
|
||||
`UPDATE builds
|
||||
SET status = 'cancelled',
|
||||
progress = 100,
|
||||
completed_at = $1,
|
||||
log = COALESCE(log, '') || $2,
|
||||
updated_at = $1
|
||||
WHERE id = $3
|
||||
AND status NOT IN ('success', 'failed', 'cancelled')`,
|
||||
now,
|
||||
h.formatLogLine("Cancellation requested")+"\n",
|
||||
buildID,
|
||||
)
|
||||
affected, err := sqlcdb.New(h.db.DB).CancelBuild(context.Background(), sqlcdb.CancelBuildParams{
|
||||
CompletedAt: sql.NullTime{Time: now, Valid: true},
|
||||
Log: h.formatLogLine("Cancellation requested") + "\n",
|
||||
ID: buildID,
|
||||
})
|
||||
if err != nil {
|
||||
if h.isMissingBuildsTable(err) {
|
||||
return false
|
||||
@@ -828,9 +823,7 @@ func (h *BuildHandler) cancelBuildInDB(buildID string) bool {
|
||||
log.Printf("failed to cancel build %s in database: %v", buildID, err)
|
||||
return false
|
||||
}
|
||||
|
||||
affected, err := result.RowsAffected()
|
||||
if err != nil || affected == 0 {
|
||||
if affected == 0 {
|
||||
return false
|
||||
}
|
||||
|
||||
@@ -909,12 +902,9 @@ func scanBuildRow(scan func(dest ...interface{}) error) (BuildStatusResponse, bo
|
||||
return parsed, true, nil
|
||||
}
|
||||
|
||||
func nullIfEmptyString(value string) interface{} {
|
||||
func nsNullIfEmpty(value string) sql.NullString {
|
||||
trimmed := strings.TrimSpace(value)
|
||||
if trimmed == "" {
|
||||
return nil
|
||||
}
|
||||
return trimmed
|
||||
return sql.NullString{String: trimmed, Valid: trimmed != ""}
|
||||
}
|
||||
|
||||
func jsonOrEmptyObject(raw []byte) string {
|
||||
|
||||
@@ -1,9 +1,11 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -57,15 +59,12 @@ func contextIsAdmin(c *gin.Context) bool {
|
||||
// public; unapproved ones are visible to the owner, project members, and
|
||||
// platform admins only.
|
||||
func projectReadAccess(c *gin.Context, db *database.DB, projectID uuid.UUID) (bool, bool) {
|
||||
var isApproved bool
|
||||
var ownerID string
|
||||
err := db.QueryRow(
|
||||
`SELECT is_approved, owner_id::text FROM projects WHERE id = $1`,
|
||||
projectID,
|
||||
).Scan(&isApproved, &ownerID)
|
||||
q := sqlcdb.New(db.DB)
|
||||
row, err := q.GetProjectReadAccess(context.Background(), projectID)
|
||||
if err != nil {
|
||||
return false, false
|
||||
}
|
||||
isApproved, ownerID := row.IsApproved, row.OwnerID
|
||||
if isApproved || contextIsAdmin(c) {
|
||||
return true, true
|
||||
}
|
||||
@@ -76,11 +75,11 @@ func projectReadAccess(c *gin.Context, db *database.DB, projectID uuid.UUID) (bo
|
||||
if ownerID == userID {
|
||||
return true, true
|
||||
}
|
||||
var member bool
|
||||
if err := db.QueryRow(
|
||||
`SELECT EXISTS(SELECT 1 FROM project_members WHERE project_id = $1 AND user_id = $2)`,
|
||||
projectID, userID,
|
||||
).Scan(&member); err == nil && member {
|
||||
userUUID, _ := uuid.Parse(userID)
|
||||
if member, err := q.ProjectMemberExists(context.Background(), sqlcdb.ProjectMemberExistsParams{
|
||||
ProjectID: projectID,
|
||||
UserID: userUUID,
|
||||
}); err == nil && member {
|
||||
return true, true
|
||||
}
|
||||
return true, false
|
||||
@@ -91,11 +90,8 @@ func projectReadAccess(c *gin.Context, db *database.DB, projectID uuid.UUID) (bo
|
||||
// project members, and platform admins may reach secret-adjacent data
|
||||
// such as variables and runtime logs.
|
||||
func projectManageAccess(c *gin.Context, db *database.DB, projectID uuid.UUID) (bool, bool) {
|
||||
var ownerID string
|
||||
err := db.QueryRow(
|
||||
`SELECT owner_id::text FROM projects WHERE id = $1`,
|
||||
projectID,
|
||||
).Scan(&ownerID)
|
||||
q := sqlcdb.New(db.DB)
|
||||
ownerID, err := q.GetProjectOwnerText(context.Background(), projectID)
|
||||
if err != nil {
|
||||
return false, false
|
||||
}
|
||||
@@ -109,11 +105,11 @@ func projectManageAccess(c *gin.Context, db *database.DB, projectID uuid.UUID) (
|
||||
if ownerID == userID {
|
||||
return true, true
|
||||
}
|
||||
var member bool
|
||||
if err := db.QueryRow(
|
||||
`SELECT EXISTS(SELECT 1 FROM project_members WHERE project_id = $1 AND user_id = $2)`,
|
||||
projectID, userID,
|
||||
).Scan(&member); err == nil && member {
|
||||
userUUID, _ := uuid.Parse(userID)
|
||||
if member, err := q.ProjectMemberExists(context.Background(), sqlcdb.ProjectMemberExistsParams{
|
||||
ProjectID: projectID,
|
||||
UserID: userUUID,
|
||||
}); err == nil && member {
|
||||
return true, true
|
||||
}
|
||||
return true, false
|
||||
@@ -121,8 +117,7 @@ func projectManageAccess(c *gin.Context, db *database.DB, projectID uuid.UUID) (
|
||||
|
||||
// serviceProjectID resolves the project that owns a service.
|
||||
func serviceProjectID(db *database.DB, serviceID uuid.UUID) (uuid.UUID, bool) {
|
||||
var projectID uuid.UUID
|
||||
err := db.QueryRow(`SELECT project_id FROM services WHERE id = $1`, serviceID).Scan(&projectID)
|
||||
projectID, err := sqlcdb.New(db.DB).GetServiceProjectID(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
|
||||
+143
-119
@@ -65,13 +65,47 @@ type UpdateCronJobRequest struct {
|
||||
Retention int `json:"retention"`
|
||||
}
|
||||
|
||||
// cronJobFrom maps the sqlc row onto the string-ID API model.
|
||||
func cronJobFrom(j sqlcdb.CronJob) CronJob {
|
||||
return CronJob{
|
||||
ID: j.ID.String(),
|
||||
ProjectID: j.ProjectID.String(),
|
||||
ServiceID: j.ServiceID.String(),
|
||||
Name: j.Name,
|
||||
Schedule: j.Schedule,
|
||||
Command: j.Command,
|
||||
Timezone: j.Timezone.String,
|
||||
Enabled: j.Enabled.Bool,
|
||||
LastRunAt: timePtrNT(j.LastRunAt),
|
||||
NextRunAt: timePtrNT(j.NextRunAt),
|
||||
LastStatus: j.LastStatus.String,
|
||||
LastOutput: j.LastOutput.String,
|
||||
Retention: int(j.Retention.Int32),
|
||||
CreatedAt: j.CreatedAt.Time,
|
||||
UpdatedAt: j.UpdatedAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
// cronExecutionFrom maps the sqlc row onto the string-ID API model.
|
||||
func cronExecutionFrom(e sqlcdb.CronExecution) CronExecution {
|
||||
return CronExecution{
|
||||
ID: e.ID.String(),
|
||||
CronJobID: e.CronJobID.String(),
|
||||
StartedAt: e.StartedAt,
|
||||
FinishedAt: timePtrNT(e.FinishedAt),
|
||||
Status: e.Status.String,
|
||||
Output: e.Output.String,
|
||||
Error: e.Error.String,
|
||||
}
|
||||
}
|
||||
|
||||
// cronJobProjectID resolves the project a cron job belongs to.
|
||||
func cronJobProjectID(db *database.DB, jobID string) (uuid.UUID, bool) {
|
||||
var projectID uuid.UUID
|
||||
err := db.QueryRow(
|
||||
`SELECT cj.project_id FROM cron_jobs cj WHERE cj.id = $1`,
|
||||
jobID,
|
||||
).Scan(&projectID)
|
||||
id, err := uuid.Parse(jobID)
|
||||
if err != nil {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
projectID, err := sqlcdb.New(db.DB).GetCronJobProjectID(context.Background(), id)
|
||||
if err != nil {
|
||||
return uuid.Nil, false
|
||||
}
|
||||
@@ -137,15 +171,14 @@ func handleCreateCronJob(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var ownerCheck string
|
||||
err := db.QueryRow(
|
||||
`SELECT p.owner_id FROM projects p
|
||||
JOIN services s ON s.project_id = p.id
|
||||
WHERE s.id = $1`,
|
||||
req.ServiceID,
|
||||
).Scan(&ownerCheck)
|
||||
svcID, err := uuid.Parse(req.ServiceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
ownerCheck, err := sqlcdb.New(db.DB).GetCronOwnerViaService(context.Background(), svcID)
|
||||
|
||||
if err != nil || (ownerCheck != userID && !contextIsAdmin(c)) {
|
||||
if err != nil || (ownerCheck.String() != userID && !contextIsAdmin(c)) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
@@ -178,11 +211,20 @@ func handleCreateCronJob(c *gin.Context) {
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO cron_jobs (id, project_id, service_id, name, schedule, command, timezone, enabled, next_run_at, retention, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12)`,
|
||||
job.ID, job.ProjectID, job.ServiceID, job.Name, job.Schedule, job.Command, job.Timezone, job.Enabled, job.NextRunAt, job.Retention, job.CreatedAt, job.UpdatedAt,
|
||||
)
|
||||
err = sqlcdb.New(db.DB).InsertCronJob(context.Background(), sqlcdb.InsertCronJobParams{
|
||||
ID: uuid.MustParse(job.ID),
|
||||
ProjectID: uuid.MustParse(job.ProjectID),
|
||||
ServiceID: uuid.MustParse(job.ServiceID),
|
||||
Name: job.Name,
|
||||
Schedule: job.Schedule,
|
||||
Command: job.Command,
|
||||
Timezone: sql.NullString{String: job.Timezone, Valid: true},
|
||||
Enabled: sql.NullBool{Bool: job.Enabled, Valid: true},
|
||||
NextRunAt: ntPtr(job.NextRunAt),
|
||||
Retention: sql.NullInt32{Int32: int32(job.Retention), Valid: true},
|
||||
CreatedAt: sql.NullTime{Time: job.CreatedAt, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: job.UpdatedAt, Valid: true},
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create cron job"})
|
||||
@@ -201,22 +243,17 @@ func handleGetCronJob(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
jobID := c.Param("id")
|
||||
|
||||
var job CronJob
|
||||
err := db.QueryRow(
|
||||
`SELECT cj.id, cj.project_id, cj.service_id, cj.name, cj.schedule, cj.timezone,
|
||||
cj.enabled, cj.last_run_at, cj.next_run_at, cj.last_status, cj.last_output,
|
||||
cj.retention, cj.created_at, cj.updated_at
|
||||
FROM cron_jobs cj
|
||||
WHERE cj.id = $1`,
|
||||
jobID,
|
||||
).Scan(&job.ID, &job.ProjectID, &job.ServiceID, &job.Name, &job.Schedule, &job.Timezone,
|
||||
&job.Enabled, &job.LastRunAt, &job.NextRunAt, &job.LastStatus, &job.LastOutput,
|
||||
&job.Retention, &job.CreatedAt, &job.UpdatedAt)
|
||||
|
||||
jid, err := uuid.Parse(jobID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Cron job not found"})
|
||||
return
|
||||
}
|
||||
row, err := sqlcdb.New(db.DB).GetCronJob(context.Background(), jid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Cron job not found"})
|
||||
return
|
||||
}
|
||||
job := cronJobFrom(row)
|
||||
|
||||
projectID, found := cronJobProjectID(db, jobID)
|
||||
if !found {
|
||||
@@ -242,15 +279,14 @@ func handleUpdateCronJob(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var ownerCheck string
|
||||
err := db.QueryRow(
|
||||
`SELECT p.owner_id FROM cron_jobs cj
|
||||
JOIN projects p ON cj.project_id = p.id
|
||||
WHERE cj.id = $1`,
|
||||
jobID,
|
||||
).Scan(&ownerCheck)
|
||||
jid, err := uuid.Parse(jobID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
ownerCheck, err := sqlcdb.New(db.DB).GetCronJobOwner(context.Background(), jid)
|
||||
|
||||
if err != nil || (ownerCheck != userID && !contextIsAdmin(c)) {
|
||||
if err != nil || (ownerCheck.String() != userID && !contextIsAdmin(c)) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
@@ -312,21 +348,19 @@ func handleDeleteCronJob(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
jobID := c.Param("id")
|
||||
|
||||
var ownerCheck string
|
||||
err := db.QueryRow(
|
||||
`SELECT p.owner_id FROM cron_jobs cj
|
||||
JOIN projects p ON cj.project_id = p.id
|
||||
WHERE cj.id = $1`,
|
||||
jobID,
|
||||
).Scan(&ownerCheck)
|
||||
jid, err := uuid.Parse(jobID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
ownerCheck, err := sqlcdb.New(db.DB).GetCronJobOwner(context.Background(), jid)
|
||||
|
||||
if err != nil || (ownerCheck != userID && !contextIsAdmin(c)) {
|
||||
if err != nil || (ownerCheck.String() != userID && !contextIsAdmin(c)) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
|
||||
_, err = db.Exec("DELETE FROM cron_jobs WHERE id = $1", jobID)
|
||||
if err != nil {
|
||||
if err := sqlcdb.New(db.DB).DeleteCronJob(context.Background(), jid); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete cron job"})
|
||||
return
|
||||
}
|
||||
@@ -350,29 +384,20 @@ func handleGetCronExecutions(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := db.Query(
|
||||
`SELECT id, cron_job_id, started_at, finished_at, status, output, error
|
||||
FROM cron_executions
|
||||
WHERE cron_job_id = $1
|
||||
ORDER BY started_at DESC
|
||||
LIMIT 100`,
|
||||
jobID,
|
||||
)
|
||||
|
||||
jid, err := uuid.Parse(jobID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch executions"})
|
||||
return
|
||||
}
|
||||
rows, err := sqlcdb.New(db.DB).ListCronExecutions(context.Background(), jid)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch executions"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var executions []CronExecution
|
||||
for rows.Next() {
|
||||
var exec CronExecution
|
||||
err := rows.Scan(&exec.ID, &exec.CronJobID, &exec.StartedAt, &exec.FinishedAt, &exec.Status, &exec.Output, &exec.Error)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
executions = append(executions, exec)
|
||||
for _, e := range rows {
|
||||
executions = append(executions, cronExecutionFrom(e))
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"executions": executions})
|
||||
@@ -383,29 +408,33 @@ func handleTriggerCronJob(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
jobID := c.Param("id")
|
||||
|
||||
var job CronJob
|
||||
var ownerCheck string
|
||||
err := db.QueryRow(
|
||||
`SELECT cj.service_id, cj.command, cj.schedule, cj.timezone, cj.retention, p.owner_id
|
||||
FROM cron_jobs cj
|
||||
JOIN projects p ON cj.project_id = p.id
|
||||
WHERE cj.id = $1`,
|
||||
jobID,
|
||||
).Scan(&job.ServiceID, &job.Command, &job.Schedule, &job.Timezone, &job.Retention, &ownerCheck)
|
||||
|
||||
if err != nil || (ownerCheck != userID && !contextIsAdmin(c)) {
|
||||
jid, err := uuid.Parse(jobID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
trig, err := sqlcdb.New(db.DB).GetCronJobForTrigger(context.Background(), jid)
|
||||
if err != nil || (trig.OwnerID.String() != userID && !contextIsAdmin(c)) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
job := CronJob{
|
||||
ServiceID: trig.ServiceID.String(),
|
||||
Command: trig.Command,
|
||||
Schedule: trig.Schedule,
|
||||
Timezone: trig.Timezone.String,
|
||||
Retention: int(trig.Retention.Int32),
|
||||
}
|
||||
|
||||
execID := uuid.New().String()
|
||||
now := time.Now()
|
||||
|
||||
_, err = db.Exec(
|
||||
`INSERT INTO cron_executions (id, cron_job_id, started_at, status)
|
||||
VALUES ($1, $2, $3, $4)`,
|
||||
execID, jobID, now, "running",
|
||||
)
|
||||
_ = sqlcdb.New(db.DB).InsertCronExecution(context.Background(), sqlcdb.InsertCronExecutionParams{
|
||||
ID: uuid.MustParse(execID),
|
||||
CronJobID: jid,
|
||||
StartedAt: now,
|
||||
Status: sql.NullString{String: "running", Valid: true},
|
||||
})
|
||||
|
||||
dockerClient, _ := c.Get("docker_client")
|
||||
dockerCli, _ := dockerClient.(*docker.Client)
|
||||
@@ -458,39 +487,25 @@ func StartCronScheduler(ctx context.Context, db *database.DB, dockerClient *dock
|
||||
}
|
||||
|
||||
func runDueCronJobs(db *database.DB, dockerClient *docker.Client) {
|
||||
rows, err := db.Query(
|
||||
`SELECT id, project_id, service_id, name, schedule, command, timezone, enabled, retention
|
||||
FROM cron_jobs
|
||||
WHERE enabled = TRUE AND next_run_at IS NOT NULL AND next_run_at <= NOW()`)
|
||||
q := sqlcdb.New(db.DB)
|
||||
jobs, err := q.ListDueCronJobs(context.Background())
|
||||
if err != nil {
|
||||
log.Printf("cron scheduler: failed to list due jobs: %v", err)
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
type dueJob struct {
|
||||
CronJob
|
||||
Command string
|
||||
}
|
||||
var jobs []dueJob
|
||||
for rows.Next() {
|
||||
var j dueJob
|
||||
if err := rows.Scan(&j.ID, &j.ProjectID, &j.ServiceID, &j.Name, &j.Schedule,
|
||||
&j.Command, &j.Timezone, &j.Enabled, &j.Retention); err != nil {
|
||||
continue
|
||||
}
|
||||
jobs = append(jobs, j)
|
||||
}
|
||||
|
||||
for _, j := range jobs {
|
||||
execID := uuid.New().String()
|
||||
if _, err := db.Exec(
|
||||
`INSERT INTO cron_executions (id, cron_job_id, started_at, status) VALUES ($1, $2, $3, $4)`,
|
||||
execID, j.ID, time.Now(), "running"); err != nil {
|
||||
if err := q.InsertCronExecution(context.Background(), sqlcdb.InsertCronExecutionParams{
|
||||
ID: uuid.MustParse(execID),
|
||||
CronJobID: j.ID,
|
||||
StartedAt: time.Now(),
|
||||
Status: sql.NullString{String: "running", Valid: true},
|
||||
}); err != nil {
|
||||
log.Printf("cron scheduler: failed to record execution for %s: %v", j.ID, err)
|
||||
continue
|
||||
}
|
||||
go executeCronJob(db, dockerClient, j.ID, j.ServiceID, execID, j.Command, j.Schedule, j.Timezone, j.Retention)
|
||||
go executeCronJob(db, dockerClient, j.ID.String(), j.ServiceID.String(), execID, j.Command, j.Schedule, j.Timezone.String, int(j.Retention.Int32))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -584,23 +599,32 @@ func executeCronJob(db *database.DB, dockerClient *docker.Client, jobID, service
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
db.Exec(
|
||||
`UPDATE cron_executions SET finished_at = $1, status = $2, output = $3, error = $4 WHERE id = $5`,
|
||||
now, status, output, errText, execID,
|
||||
)
|
||||
q := sqlcdb.New(db.DB)
|
||||
if eid, err := uuid.Parse(execID); err == nil {
|
||||
_ = q.FinishCronExecution(context.Background(), sqlcdb.FinishCronExecutionParams{
|
||||
FinishedAt: sql.NullTime{Time: now, Valid: true},
|
||||
Status: sql.NullString{String: status, Valid: true},
|
||||
Output: sql.NullString{String: output, Valid: true},
|
||||
Error: sql.NullString{String: errText, Valid: true},
|
||||
ID: eid,
|
||||
})
|
||||
}
|
||||
|
||||
next, _ := calculateNextRun(schedule, timezone)
|
||||
db.Exec(
|
||||
`UPDATE cron_jobs SET last_run_at = $1, last_status = $2, last_output = $3, next_run_at = $4 WHERE id = $5`,
|
||||
now, status, output, next, jobID,
|
||||
)
|
||||
if jid, err := uuid.Parse(jobID); err == nil {
|
||||
_ = q.UpdateCronJobRun(context.Background(), sqlcdb.UpdateCronJobRunParams{
|
||||
LastRunAt: sql.NullTime{Time: now, Valid: true},
|
||||
LastStatus: sql.NullString{String: status, Valid: true},
|
||||
LastOutput: sql.NullString{String: output, Valid: true},
|
||||
NextRunAt: ntPtr(next),
|
||||
ID: jid,
|
||||
})
|
||||
|
||||
if retention > 0 {
|
||||
db.Exec(
|
||||
`DELETE FROM cron_executions WHERE cron_job_id = $1 AND id NOT IN (
|
||||
SELECT id FROM cron_executions WHERE cron_job_id = $1
|
||||
ORDER BY started_at DESC LIMIT $2)`,
|
||||
jobID, retention,
|
||||
)
|
||||
if retention > 0 {
|
||||
_ = q.TrimCronExecutions(context.Background(), sqlcdb.TrimCronExecutionsParams{
|
||||
CronJobID: jid,
|
||||
Limit: int32(retention),
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,193 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestCalculateNextRun(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
schedule string
|
||||
timezone string
|
||||
wantErr bool
|
||||
}{
|
||||
{"every minute", "* * * * *", "", false},
|
||||
{"daily at 4am", "0 4 * * *", "UTC", false},
|
||||
{"weekday run", "0 9 * * 1-5", "Europe/Prague", false},
|
||||
{"whitespace tolerated", " */15 * * * * ", "", false},
|
||||
{"empty schedule", "", "", true},
|
||||
{"bad field count", "* * *", "", true},
|
||||
{"garbage", "not a cron", "", true},
|
||||
{"bad timezone", "0 4 * * *", "Mars/Olympus", true},
|
||||
}
|
||||
now := time.Now()
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
next, err := calculateNextRun(tc.schedule, tc.timezone)
|
||||
if tc.wantErr {
|
||||
if err == nil {
|
||||
t.Fatalf("expected error, got next=%v", next)
|
||||
}
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
if next == nil {
|
||||
t.Fatal("expected non-nil next run")
|
||||
}
|
||||
if !next.After(now) {
|
||||
t.Errorf("next run %v is not after %v", next, now)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCalculateNextRunTimezone(t *testing.T) {
|
||||
// CRON_TZ must change the computed instant: 04:00 in a fixed-offset zone
|
||||
// lands at a different UTC hour than 04:00 UTC.
|
||||
utcNext, err := calculateNextRun("0 4 * * *", "UTC")
|
||||
if err != nil {
|
||||
t.Fatalf("utc: %v", err)
|
||||
}
|
||||
tokyoNext, err := calculateNextRun("0 4 * * *", "Asia/Tokyo")
|
||||
if err != nil {
|
||||
t.Fatalf("tokyo: %v", err)
|
||||
}
|
||||
if utcNext.Equal(*tokyoNext) {
|
||||
t.Fatalf("timezone had no effect: both %v", utcNext)
|
||||
}
|
||||
if !strings.EqualFold(utcNext.Location().String(), "Local") {
|
||||
// UTC-scheduled runs still land in server-local time representation.
|
||||
t.Logf("utc next: %v / tokyo next: %v", utcNext, tokyoNext)
|
||||
}
|
||||
}
|
||||
|
||||
// TestCronLiveInvariants exercises the cron.sql queries on live Postgres:
|
||||
// insert/read/owner join, execution lifecycle, run bookkeeping, retention trim.
|
||||
func TestCronLiveInvariants(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
serviceID := uuid.New()
|
||||
jobID := uuid.New()
|
||||
suffix := strings.ReplaceAll(jobID.String()[:8], "-", "")
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test')`,
|
||||
userID, "cron-"+suffix+"@test.local")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, true)`,
|
||||
projectID, "cron-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type)
|
||||
VALUES ($1, $2, $3, $4, 'web', 'image')`,
|
||||
serviceID, "cron-"+suffix, projectID, envID)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE id = $1`, userID) }()
|
||||
|
||||
next, err := calculateNextRun("0 4 * * *", "UTC")
|
||||
if err != nil {
|
||||
t.Fatalf("schedule: %v", err)
|
||||
}
|
||||
must(q.InsertCronJob(ctx, sqlcdb.InsertCronJobParams{
|
||||
ID: jobID, ProjectID: projectID, ServiceID: serviceID,
|
||||
Name: "job-" + suffix, Schedule: "0 4 * * *", Command: "echo hi",
|
||||
Timezone: sql.NullString{String: "UTC", Valid: true},
|
||||
Enabled: sql.NullBool{Bool: true, Valid: true},
|
||||
NextRunAt: ntPtr(next),
|
||||
}))
|
||||
job, err := q.GetCronJob(ctx, jobID)
|
||||
if err != nil || job.Name != "job-"+suffix || job.Command != "echo hi" {
|
||||
t.Fatalf("job: %v %+v", err, job)
|
||||
}
|
||||
owner, err := q.GetCronJobOwner(ctx, jobID)
|
||||
if err != nil || owner != userID {
|
||||
t.Fatalf("owner: %v %v", err, owner)
|
||||
}
|
||||
owner2, err := q.GetCronOwnerViaService(ctx, serviceID)
|
||||
if err != nil || owner2 != userID {
|
||||
t.Fatalf("owner via service: %v %v", err, owner2)
|
||||
}
|
||||
pid, err := q.GetCronJobProjectID(ctx, jobID)
|
||||
if err != nil || pid != projectID {
|
||||
t.Fatalf("project: %v %v", err, pid)
|
||||
}
|
||||
|
||||
// Trigger-shaped read.
|
||||
trig, err := q.GetCronJobForTrigger(ctx, jobID)
|
||||
if err != nil || trig.ServiceID != serviceID || trig.OwnerID != userID {
|
||||
t.Fatalf("trigger: %v %+v", err, trig)
|
||||
}
|
||||
|
||||
// Execution lifecycle + retention trim.
|
||||
for i := 0; i < 3; i++ {
|
||||
execID := uuid.New()
|
||||
must(q.InsertCronExecution(ctx, sqlcdb.InsertCronExecutionParams{
|
||||
ID: execID, CronJobID: jobID,
|
||||
StartedAt: time.Now().Add(time.Duration(i) * time.Second),
|
||||
Status: sql.NullString{String: "running", Valid: true},
|
||||
}))
|
||||
must(q.FinishCronExecution(ctx, sqlcdb.FinishCronExecutionParams{
|
||||
FinishedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
Status: sql.NullString{String: "success", Valid: true},
|
||||
Output: sql.NullString{String: "ok", Valid: true},
|
||||
Error: sql.NullString{String: "", Valid: true},
|
||||
ID: execID,
|
||||
}))
|
||||
}
|
||||
execs, err := q.ListCronExecutions(ctx, jobID)
|
||||
if err != nil || len(execs) != 3 {
|
||||
t.Fatalf("execs: %v %d", err, len(execs))
|
||||
}
|
||||
if execs[0].Status.String != "success" || !execs[0].FinishedAt.Valid {
|
||||
t.Fatalf("exec row: %+v", execs[0])
|
||||
}
|
||||
must(q.TrimCronExecutions(ctx, sqlcdb.TrimCronExecutionsParams{CronJobID: jobID, Limit: 1}))
|
||||
execs, _ = q.ListCronExecutions(ctx, jobID)
|
||||
if len(execs) != 1 {
|
||||
t.Fatalf("trim kept %d, want 1", len(execs))
|
||||
}
|
||||
|
||||
must(q.UpdateCronJobRun(ctx, sqlcdb.UpdateCronJobRunParams{
|
||||
LastRunAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
LastStatus: sql.NullString{String: "success", Valid: true},
|
||||
LastOutput: sql.NullString{String: "ok", Valid: true},
|
||||
NextRunAt: ntPtr(next),
|
||||
ID: jobID,
|
||||
}))
|
||||
job, _ = q.GetCronJob(ctx, jobID)
|
||||
if job.LastStatus.String != "success" || !job.LastRunAt.Valid {
|
||||
t.Fatalf("post-run job: %+v", job)
|
||||
}
|
||||
|
||||
must(q.DeleteCronJob(ctx, jobID))
|
||||
if _, err := q.GetCronJob(ctx, jobID); err == nil {
|
||||
t.Fatal("deleted job still readable")
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,11 @@ package api
|
||||
|
||||
import (
|
||||
"archive/tar"
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/docker"
|
||||
"containr/internal/secrets"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"database/sql"
|
||||
@@ -21,6 +24,8 @@ import (
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
dockercontainer "github.com/docker/docker/api/types/container"
|
||||
"github.com/docker/docker/api/types/mount"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
@@ -51,12 +56,32 @@ type DatabaseService struct {
|
||||
Region string `json:"region" db:"region"`
|
||||
BackupSchedule string `json:"backup_schedule,omitempty"`
|
||||
NextBackupAt *time.Time `json:"next_backup_at,omitempty"`
|
||||
Provider string `json:"provider"` // managed | external
|
||||
PublicPort bool `json:"public_port" db:"public_port"`
|
||||
BackupTargetID string `json:"backup_target_id,omitempty"` // S3-compatible archive destination
|
||||
External *DatabaseExternalRef `json:"external,omitempty"` // connection target for external DBs
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
ConnectionURL string `json:"connection_url"`
|
||||
Metrics DatabaseMetrics `json:"metrics"`
|
||||
Backups DatabaseBackupConfig `json:"backups"`
|
||||
Settings DatabaseSettings `json:"settings"`
|
||||
|
||||
// externalPasswordEnc is the stored ciphertext — needed to compose the
|
||||
// connection URL on privileged reads, never serialized.
|
||||
externalPasswordEnc string `json:"-"`
|
||||
}
|
||||
|
||||
// DatabaseExternalRef describes a registered external database's connection
|
||||
// target. The password is stored encrypted and never returned — has_password
|
||||
// signals presence.
|
||||
type DatabaseExternalRef struct {
|
||||
Host string `json:"host"`
|
||||
Port int `json:"port"`
|
||||
Database string `json:"database"`
|
||||
Username string `json:"username"`
|
||||
HasPassword bool `json:"has_password"`
|
||||
SSL bool `json:"ssl"`
|
||||
}
|
||||
|
||||
// DatabaseMetrics represents database performance metrics
|
||||
@@ -86,6 +111,7 @@ type DatabaseBackup struct {
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Size string `json:"size"`
|
||||
Status string `json:"status"` // completed, failed, in_progress
|
||||
Remote bool `json:"remote"` // archive also exists on the configured backup target
|
||||
}
|
||||
|
||||
// DatabaseSettings represents database configuration
|
||||
@@ -98,10 +124,11 @@ type DatabaseSettings struct {
|
||||
|
||||
// DatabaseCreateRequest represents a request to create a new database
|
||||
type DatabaseCreateRequest struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
Plan string `json:"plan" binding:"required,oneof=hobby starter standard business"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
Plan string `json:"plan" binding:"required,oneof=hobby starter standard business"`
|
||||
Region string `json:"region" binding:"required"`
|
||||
PublicPort bool `json:"public_port"`
|
||||
}
|
||||
|
||||
// DatabaseUpdateRequest represents a request to update a database
|
||||
@@ -109,6 +136,8 @@ type DatabaseUpdateRequest struct {
|
||||
Name string `json:"name,omitempty"`
|
||||
Plan string `json:"plan,omitempty"`
|
||||
BackupSchedule *string `json:"backup_schedule,omitempty"`
|
||||
PublicPort *bool `json:"public_port,omitempty"`
|
||||
BackupTargetID *string `json:"backup_target_id,omitempty"` // empty string clears
|
||||
}
|
||||
|
||||
// DatabaseActionRequest represents a request to perform database actions
|
||||
@@ -272,10 +301,11 @@ func (h *DatabaseHandler) CreateDatabase(c *gin.Context) {
|
||||
}
|
||||
|
||||
databaseID, dbName, dbType, err := h.createManagedDatabase(c.Request.Context(), userID, managedDatabaseCreateRequest{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Plan: req.Plan,
|
||||
Region: req.Region,
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Plan: req.Plan,
|
||||
Region: req.Region,
|
||||
PublicPort: req.PublicPort,
|
||||
})
|
||||
if err != nil {
|
||||
switch {
|
||||
@@ -303,7 +333,13 @@ type managedDatabaseCreateRequest struct {
|
||||
Type string
|
||||
Plan string
|
||||
Region string
|
||||
PublicPort bool
|
||||
RuntimeVariables map[string]string
|
||||
// ProjectID + NetworkAlias attach the container to a project network
|
||||
// under Alias — set by graph-template deploys so sibling services can
|
||||
// resolve the database by name.
|
||||
ProjectID string
|
||||
NetworkAlias string
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) createManagedDatabase(ctx context.Context, userID string, req managedDatabaseCreateRequest) (databaseID string, name string, dbType string, err error) {
|
||||
@@ -345,17 +381,26 @@ func (h *DatabaseHandler) createManagedDatabase(ctx context.Context, userID stri
|
||||
now := time.Now()
|
||||
version := h.getDefaultVersion(dbType)
|
||||
|
||||
var projectID uuid.NullUUID
|
||||
if req.ProjectID != "" {
|
||||
if parsed, parseErr := uuid.Parse(req.ProjectID); parseErr == nil {
|
||||
projectID = uuid.NullUUID{UUID: parsed, Valid: true}
|
||||
}
|
||||
}
|
||||
|
||||
err = h.queries.CreateDatabaseService(ctx, sqlcdb.CreateDatabaseServiceParams{
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
Name: name,
|
||||
Type: dbType,
|
||||
Status: "building",
|
||||
Version: version,
|
||||
Plan: plan,
|
||||
Region: region,
|
||||
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
Name: name,
|
||||
Type: dbType,
|
||||
Status: "building",
|
||||
Version: version,
|
||||
Plan: plan,
|
||||
Region: region,
|
||||
PublicPort: req.PublicPort,
|
||||
ProjectID: projectID,
|
||||
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
return "", "", "", fmt.Errorf("failed to create database service: %w", err)
|
||||
@@ -370,10 +415,17 @@ func (h *DatabaseHandler) createManagedDatabaseAndProvision(ctx context.Context,
|
||||
return "", err
|
||||
}
|
||||
|
||||
go h.provisionDatabaseWithVariables(databaseID, name, dbType, req.RuntimeVariables)
|
||||
go h.provisionDatabaseWithVariables(databaseID, name, dbType, req.RuntimeVariables, dbNetworkAttach{ProjectID: req.ProjectID, Alias: req.NetworkAlias})
|
||||
return databaseID, nil
|
||||
}
|
||||
|
||||
// dbNetworkAttach carries the optional project-network membership for a
|
||||
// managed database provisioned by a graph template.
|
||||
type dbNetworkAttach struct {
|
||||
ProjectID string
|
||||
Alias string
|
||||
}
|
||||
|
||||
// UpdateDatabase updates a database service
|
||||
func (h *DatabaseHandler) UpdateDatabase(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
@@ -392,11 +444,64 @@ func (h *DatabaseHandler) UpdateDatabase(c *gin.Context) {
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
req.Plan = strings.TrimSpace(req.Plan)
|
||||
|
||||
if req.Name == "" && req.Plan == "" && req.BackupSchedule == nil {
|
||||
if req.Name == "" && req.Plan == "" && req.BackupSchedule == nil && req.PublicPort == nil && req.BackupTargetID == nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No fields to update"})
|
||||
return
|
||||
}
|
||||
|
||||
if req.PublicPort != nil || req.BackupTargetID != nil {
|
||||
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
if req.PublicPort != nil {
|
||||
if row.Provider == "external" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "public_port applies to managed databases — external databases are already reachable", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if err := h.queries.SetDatabaseServicePublicPortByIDAndUser(c.Request.Context(), sqlcdb.SetDatabaseServicePublicPortByIDAndUserParams{
|
||||
PublicPort: *req.PublicPort,
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update public port"})
|
||||
return
|
||||
}
|
||||
// Port bindings are baked into the container — recreate it so the
|
||||
// new bind scope takes effect. The data volume survives.
|
||||
if *req.PublicPort != row.PublicPort {
|
||||
go h.reprovisionManagedDatabase(databaseID, row.Name, row.Type)
|
||||
}
|
||||
}
|
||||
if req.BackupTargetID != nil {
|
||||
if row.Provider == "external" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "backup targets apply to managed databases — external backups are not supported yet", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
targetID := strings.TrimSpace(*req.BackupTargetID)
|
||||
if targetID != "" {
|
||||
if _, err := h.queries.GetBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.GetBackupTargetByIDAndUserParams{ID: targetID, UserID: userID}); err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Backup target not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
}
|
||||
if err := h.queries.SetDatabaseBackupTargetByIDAndUser(c.Request.Context(), sqlcdb.SetDatabaseBackupTargetByIDAndUserParams{
|
||||
BackupTargetID: sql.NullString{String: targetID, Valid: targetID != ""},
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update backup target"})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if req.BackupSchedule != nil {
|
||||
var schedule sql.NullString
|
||||
var nextBackup sql.NullTime
|
||||
@@ -467,21 +572,20 @@ func (h *DatabaseHandler) DeleteDatabase(c *gin.Context) {
|
||||
databaseID := c.Param("id")
|
||||
userID = h.effectiveUserID(c, userID, databaseID)
|
||||
|
||||
exists, err := h.queries.DatabaseServiceExistsByIDAndUser(c.Request.Context(), sqlcdb.DatabaseServiceExistsByIDAndUserParams{
|
||||
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
||||
return
|
||||
}
|
||||
|
||||
if !exists {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||
return
|
||||
}
|
||||
|
||||
if h.dockerClient != nil {
|
||||
if row.Provider != "external" && h.dockerClient != nil {
|
||||
if err := h.deleteManagedDatabaseRuntime(databaseID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete runtime database"})
|
||||
return
|
||||
@@ -515,17 +619,24 @@ func (h *DatabaseHandler) PerformDatabaseAction(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
exists, err := h.queries.DatabaseServiceExistsByIDAndUser(c.Request.Context(), sqlcdb.DatabaseServiceExistsByIDAndUserParams{
|
||||
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
||||
return
|
||||
}
|
||||
|
||||
if !exists {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||
if row.Provider == "external" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Lifecycle actions apply to managed databases only — external databases are run elsewhere",
|
||||
"code": "VALIDATION",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -619,6 +730,13 @@ func (h *DatabaseHandler) CreateBackup(c *gin.Context) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Database is still provisioning"})
|
||||
return
|
||||
}
|
||||
if row.Provider == "external" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Backups run against the managed dump pipeline — external database backups are not supported yet",
|
||||
"code": "VALIDATION",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
backupID := generateDatabaseBackupID(databaseID)
|
||||
archivePath := sanitizeBackupArchivePath(managedDatabaseBackupArchivePath(backupID))
|
||||
@@ -659,17 +777,23 @@ func (h *DatabaseHandler) RestoreBackup(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
exists, err := h.queries.DatabaseServiceExistsByIDAndUser(c.Request.Context(), sqlcdb.DatabaseServiceExistsByIDAndUserParams{
|
||||
dbRow, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check database"})
|
||||
return
|
||||
}
|
||||
|
||||
if !exists {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found"})
|
||||
if dbRow.Provider == "external" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": "Restores run against the managed dump pipeline — external database restores are not supported yet",
|
||||
"code": "VALIDATION",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -787,7 +911,7 @@ func (h *DatabaseHandler) DownloadBackup(c *gin.Context) {
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) resolveDatabaseMetrics(ctx context.Context, db DatabaseService) DatabaseMetrics {
|
||||
if h.dockerClient == nil {
|
||||
if h.dockerClient == nil || db.Provider == "external" {
|
||||
return h.generateMockMetrics()
|
||||
}
|
||||
|
||||
@@ -918,6 +1042,7 @@ func (h *DatabaseHandler) resolveBackupConfig(ctx context.Context, userID string
|
||||
CreatedAt: createdAt,
|
||||
Size: row.Size,
|
||||
Status: row.Status,
|
||||
Remote: row.RemoteKey.Valid && row.RemoteKey.String != "",
|
||||
})
|
||||
if lastBackup == nil && row.Status == "completed" {
|
||||
t := createdAt
|
||||
@@ -945,7 +1070,11 @@ func (h *DatabaseHandler) generateMockSettings() DatabaseSettings {
|
||||
|
||||
// resolveConnectionURL returns the stored connection URL for privileged
|
||||
// viewers, or "" when the runtime has not produced one (not provisioned yet).
|
||||
// External databases compose it from the registered fields on demand.
|
||||
func (h *DatabaseHandler) resolveConnectionURL(db DatabaseService) string {
|
||||
if db.Provider == "external" && db.External != nil {
|
||||
return externalConnectionURL(db.Type, db.External, secrets.Decrypt(db.externalPasswordEnc))
|
||||
}
|
||||
return strings.TrimSpace(db.ConnectionURL)
|
||||
}
|
||||
|
||||
@@ -987,7 +1116,7 @@ func rewriteLoopbackURL(rawURL, host string) string {
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) reconcileManagedDatabaseState(ctx context.Context, db DatabaseService) DatabaseService {
|
||||
if h.dockerClient == nil {
|
||||
if h.dockerClient == nil || db.Provider == "external" {
|
||||
return db
|
||||
}
|
||||
|
||||
@@ -1054,7 +1183,7 @@ func (h *DatabaseHandler) resolveManagedRuntimeStatus(ctx context.Context, db Da
|
||||
}
|
||||
|
||||
func mapDatabaseServiceRow(row sqlcdb.DatabaseService) DatabaseService {
|
||||
return DatabaseService{
|
||||
svc := DatabaseService{
|
||||
ID: row.ID,
|
||||
OwnerID: row.UserID,
|
||||
Name: row.Name,
|
||||
@@ -1065,10 +1194,25 @@ func mapDatabaseServiceRow(row sqlcdb.DatabaseService) DatabaseService {
|
||||
Region: row.Region,
|
||||
BackupSchedule: databaseNullString(row.BackupSchedule),
|
||||
NextBackupAt: databaseNullTimePtr(row.NextBackupAt),
|
||||
Provider: row.Provider,
|
||||
PublicPort: row.PublicPort,
|
||||
BackupTargetID: databaseNullString(row.BackupTargetID),
|
||||
ConnectionURL: databaseNullString(row.ConnectionUrl),
|
||||
CreatedAt: databaseNullTime(row.CreatedAt),
|
||||
UpdatedAt: databaseNullTime(row.UpdatedAt),
|
||||
}
|
||||
if svc.Provider == "external" {
|
||||
svc.External = &DatabaseExternalRef{
|
||||
Host: databaseNullString(row.ExternalHost),
|
||||
Port: int(row.ExternalPort.Int32),
|
||||
Database: databaseNullString(row.ExternalName),
|
||||
Username: databaseNullString(row.ExternalUsername),
|
||||
HasPassword: row.ExternalPassword != "",
|
||||
SSL: row.ExternalSsl,
|
||||
}
|
||||
svc.externalPasswordEnc = row.ExternalPassword
|
||||
}
|
||||
return svc
|
||||
}
|
||||
|
||||
func databaseNullTime(value sql.NullTime) time.Time {
|
||||
@@ -1230,10 +1374,21 @@ type databaseRuntimePlan struct {
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) provisionDatabase(databaseID, databaseName, dbType string) {
|
||||
h.provisionDatabaseWithVariables(databaseID, databaseName, dbType, nil)
|
||||
h.provisionDatabaseWithVariables(databaseID, databaseName, dbType, nil, dbNetworkAttach{})
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) provisionDatabaseWithVariables(databaseID, databaseName, dbType string, runtimeVariables map[string]string) {
|
||||
// reprovisionManagedDatabase recreates a managed container so changed
|
||||
// runtime settings (public_port) take effect. The data volume persists.
|
||||
func (h *DatabaseHandler) reprovisionManagedDatabase(databaseID, databaseName, dbType string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
|
||||
defer cancel()
|
||||
if h.dockerClient != nil {
|
||||
_ = h.dockerClient.RemoveContainer(ctx, managedDatabaseContainerName(databaseID), true)
|
||||
}
|
||||
h.provisionDatabase(databaseID, databaseName, dbType)
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) provisionDatabaseWithVariables(databaseID, databaseName, dbType string, runtimeVariables map[string]string, attach dbNetworkAttach) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
@@ -1242,7 +1397,7 @@ func (h *DatabaseHandler) provisionDatabaseWithVariables(databaseID, databaseNam
|
||||
return
|
||||
}
|
||||
|
||||
connectionURL, err := h.provisionDatabaseRuntime(ctx, databaseID, databaseName, dbType, runtimeVariables)
|
||||
connectionURL, err := h.provisionDatabaseRuntime(ctx, databaseID, databaseName, dbType, runtimeVariables, attach)
|
||||
if err != nil {
|
||||
log.Printf("containr: managed database %s (%s) provisioning failed: %v", databaseID, dbType, err)
|
||||
_ = h.setDatabaseStatusAndConnection(databaseID, "error", sql.NullString{})
|
||||
@@ -1264,7 +1419,7 @@ func (h *DatabaseHandler) setDatabaseStatusAndConnection(databaseID, status stri
|
||||
})
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, databaseID, databaseName, dbType string, runtimeVariables map[string]string) (string, error) {
|
||||
func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, databaseID, databaseName, dbType string, runtimeVariables map[string]string, attach dbNetworkAttach) (string, error) {
|
||||
plan, err := buildDatabaseRuntimePlan(dbType, databaseName, runtimeVariables)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -1289,8 +1444,12 @@ func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, database
|
||||
}
|
||||
}
|
||||
|
||||
hostIP := "127.0.0.1"
|
||||
if row, err := h.queries.GetDatabaseServiceByID(ctx, databaseID); err == nil && row.PublicPort {
|
||||
hostIP = "0.0.0.0"
|
||||
}
|
||||
portBindings := nat.PortMap{
|
||||
plan.Port: []nat.PortBinding{{HostIP: "127.0.0.1", HostPort: ""}},
|
||||
plan.Port: []nat.PortBinding{{HostIP: hostIP, HostPort: ""}},
|
||||
}
|
||||
|
||||
exposedPorts := nat.PortSet{plan.Port: struct{}{}}
|
||||
@@ -1348,6 +1507,17 @@ func (h *DatabaseHandler) provisionDatabaseRuntime(ctx context.Context, database
|
||||
return "", fmt.Errorf("failed to start container: %w", err)
|
||||
}
|
||||
|
||||
// Graph-template deploys attach the database to the project network so
|
||||
// sibling services resolve it by alias ({{service.<key>.host}}).
|
||||
if attach.ProjectID != "" && attach.Alias != "" {
|
||||
projectNet := deployment.ProjectNetworkName(attach.ProjectID)
|
||||
if err := h.dockerClient.ConnectNetwork(ctx, projectNet, containerID, network.EndpointSettings{
|
||||
Aliases: []string{attach.Alias},
|
||||
}); err != nil {
|
||||
log.Printf("containr: database %s attach to %s failed: %v", databaseID, projectNet, err)
|
||||
}
|
||||
}
|
||||
|
||||
hostPort, err := h.resolvePublishedHostPort(ctx, containerID, plan.Port)
|
||||
if err != nil {
|
||||
return "", err
|
||||
@@ -1699,20 +1869,36 @@ func (h *DatabaseHandler) createBackupProcess(databaseID, backupID, backupPath s
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
notify := func(title, body string) {
|
||||
if row, err := h.queries.GetDatabaseServiceByID(context.Background(), databaseID); err == nil && row.UserID != "" {
|
||||
insertUserNotification(&database.DB{DB: h.db}, row.UserID, "backup", title, body, "database", databaseID)
|
||||
}
|
||||
}
|
||||
|
||||
if h.dockerClient == nil {
|
||||
_ = h.setDatabaseBackupStatus(backupID, "failed", "0 B", false)
|
||||
notify("Database backup failed", "Backup could not start: docker unavailable.")
|
||||
return
|
||||
}
|
||||
|
||||
sizeLabel, err := h.snapshotDatabaseVolume(ctx, databaseID, backupPath)
|
||||
if err != nil {
|
||||
_ = h.setDatabaseBackupStatus(backupID, "failed", "0 B", false)
|
||||
notify("Database backup failed", fmt.Sprintf("Snapshot failed: %v", err))
|
||||
return
|
||||
}
|
||||
if dbRow, err := h.queries.GetDatabaseServiceByID(ctx, databaseID); err == nil {
|
||||
if key, err := h.uploadBackupToTarget(ctx, dbRow, backupPath, backupID); err != nil {
|
||||
log.Printf("containr: backup %s remote upload failed: %v (local archive kept)", backupID, err)
|
||||
} else if key != "" {
|
||||
log.Printf("containr: backup %s shipped to %s", backupID, key)
|
||||
}
|
||||
}
|
||||
_ = h.setDatabaseBackupStatus(backupID, "completed", sizeLabel, true)
|
||||
notify("Database backup completed", fmt.Sprintf("Backup archived (%s).", sizeLabel))
|
||||
}
|
||||
|
||||
func (h *DatabaseHandler) restoreBackupProcess(databaseID, _ string, backupPath string) {
|
||||
func (h *DatabaseHandler) restoreBackupProcess(databaseID, backupID string, backupPath string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Minute)
|
||||
defer cancel()
|
||||
|
||||
@@ -1721,6 +1907,22 @@ func (h *DatabaseHandler) restoreBackupProcess(databaseID, _ string, backupPath
|
||||
return
|
||||
}
|
||||
|
||||
// Restore the archive from the configured S3 target when the local copy
|
||||
// is gone (fresh node, pruned volume) — local takes precedence.
|
||||
if dbRow, err := h.queries.GetDatabaseServiceByID(ctx, databaseID); err == nil && dbRow.BackupTargetID.Valid && dbRow.BackupTargetID.String != "" {
|
||||
if ok, _ := h.backupArchiveExists(ctx, backupPath); !ok {
|
||||
if b, err := h.queries.GetDatabaseBackupByIDAndDatabaseAndUser(ctx, sqlcdb.GetDatabaseBackupByIDAndDatabaseAndUserParams{
|
||||
ID: backupID, DatabaseID: databaseID, UserID: dbRow.UserID,
|
||||
}); err == nil && b.RemoteKey.Valid && b.RemoteKey.String != "" {
|
||||
if err := h.downloadBackupToVolume(ctx, dbRow, b.RemoteKey.String, backupPath); err != nil {
|
||||
log.Printf("containr: remote restore fetch failed for backup %s: %v", backupID, err)
|
||||
_ = h.setDatabaseStatus(databaseID, "error")
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
_ = h.setDatabaseStatus(databaseID, "building")
|
||||
_ = h.stopManagedDatabase(databaseID)
|
||||
|
||||
|
||||
@@ -0,0 +1,319 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/tls"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/secrets"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/go-redis/redis/v8"
|
||||
)
|
||||
|
||||
// externalDatabaseRequest is the shared payload for registering an external
|
||||
// database and for ad-hoc connection tests.
|
||||
type externalDatabaseRequest struct {
|
||||
Name string `json:"name"`
|
||||
Type string `json:"type" binding:"required"`
|
||||
Host string `json:"host" binding:"required"`
|
||||
Port int `json:"port"`
|
||||
Database string `json:"database"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
SSL bool `json:"ssl"`
|
||||
}
|
||||
|
||||
var externalDefaultPorts = map[string]int{
|
||||
"postgresql": 5432,
|
||||
"mysql": 3306,
|
||||
"mariadb": 3306,
|
||||
"mongodb": 27017,
|
||||
"redis": 6379,
|
||||
"dragonfly": 6379,
|
||||
"clickhouse": 9000,
|
||||
}
|
||||
|
||||
// externalConnectionURL composes a DSN for display/bind use. The password is
|
||||
// embedded only on privileged reads — the value is decrypted at that point.
|
||||
func externalConnectionURL(dbType string, ext *DatabaseExternalRef, password string) string {
|
||||
host := net.JoinHostPort(ext.Host, fmt.Sprintf("%d", ext.Port))
|
||||
creds := ""
|
||||
if ext.Username != "" {
|
||||
creds = url.QueryEscape(ext.Username)
|
||||
if password != "" {
|
||||
creds += ":" + url.QueryEscape(password)
|
||||
}
|
||||
creds += "@"
|
||||
}
|
||||
switch dbType {
|
||||
case "postgresql":
|
||||
u := fmt.Sprintf("postgres://%s%s/%s", creds, host, ext.Database)
|
||||
if ext.SSL {
|
||||
u += "?sslmode=require"
|
||||
}
|
||||
return u
|
||||
case "mysql", "mariadb":
|
||||
return fmt.Sprintf("mysql://%s%s/%s", creds, host, ext.Database)
|
||||
case "mongodb":
|
||||
u := fmt.Sprintf("mongodb://%s%s/%s", creds, host, ext.Database)
|
||||
if ext.SSL {
|
||||
u += "?tls=true"
|
||||
}
|
||||
return u
|
||||
case "redis", "dragonfly":
|
||||
scheme := "redis"
|
||||
if ext.SSL {
|
||||
scheme = "rediss"
|
||||
}
|
||||
// Redis auth is password-only; username support is ACL-era.
|
||||
if password != "" {
|
||||
creds = ":" + url.QueryEscape(password) + "@"
|
||||
if ext.Username != "" {
|
||||
creds = url.QueryEscape(ext.Username) + creds
|
||||
}
|
||||
}
|
||||
return fmt.Sprintf("%s://%s%s", scheme, creds, host)
|
||||
case "clickhouse":
|
||||
return fmt.Sprintf("clickhouse://%s%s/%s", creds, host, ext.Database)
|
||||
}
|
||||
return fmt.Sprintf("%s://%s%s", dbType, creds, host)
|
||||
}
|
||||
|
||||
// probeExternalDatabase verifies reachability. postgres and redis-family get
|
||||
// real protocol checks via the vendored drivers; everything else gets a TCP
|
||||
// dial — enough to catch typos and firewall issues without new dependencies.
|
||||
func probeExternalDatabase(ctx context.Context, req externalDatabaseRequest) (time.Duration, error) {
|
||||
addr := net.JoinHostPort(req.Host, fmt.Sprintf("%d", req.Port))
|
||||
dialer := &net.Dialer{Timeout: 5 * time.Second}
|
||||
start := time.Now()
|
||||
|
||||
switch req.Type {
|
||||
case "postgresql":
|
||||
sslmode := "disable"
|
||||
if req.SSL {
|
||||
sslmode = "require"
|
||||
}
|
||||
dsn := fmt.Sprintf("postgres://%s:%s@%s/%s?sslmode=%s&connect_timeout=5",
|
||||
url.QueryEscape(req.Username), url.QueryEscape(req.Password), addr,
|
||||
url.QueryEscape(req.Database), sslmode)
|
||||
conn, err := sql.Open("postgres", dsn)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
defer conn.Close()
|
||||
if err := conn.PingContext(ctx); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
case "redis", "dragonfly":
|
||||
client := redis.NewClient(&redis.Options{
|
||||
Addr: addr,
|
||||
Username: req.Username,
|
||||
Password: req.Password,
|
||||
DialTimeout: 5 * time.Second,
|
||||
ReadTimeout: 5 * time.Second,
|
||||
TLSConfig: redisTLSConfig(req.SSL, req.Host),
|
||||
})
|
||||
defer client.Close()
|
||||
if err := client.Ping(ctx).Err(); err != nil {
|
||||
return 0, err
|
||||
}
|
||||
default:
|
||||
conn, err := dialer.DialContext(ctx, "tcp", addr)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
conn.Close()
|
||||
}
|
||||
return time.Since(start), nil
|
||||
}
|
||||
|
||||
func redisTLSConfig(enabled bool, host string) *tls.Config {
|
||||
if !enabled {
|
||||
return nil
|
||||
}
|
||||
return &tls.Config{ServerName: host, MinVersion: tls.VersionTLS12}
|
||||
}
|
||||
|
||||
// handleTestDatabaseConnection probes arbitrary connection parameters — used
|
||||
// by the register form's "test" button and by agents before registering.
|
||||
func (h *DatabaseHandler) TestDatabaseConnection(c *gin.Context) {
|
||||
if _, ok := requireAuthenticatedUserID(c); !ok {
|
||||
return
|
||||
}
|
||||
var req externalDatabaseRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if err := normalizeExternalRequest(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
latency, err := probeExternalDatabase(ctx, req)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": false, "error": err.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "latency_ms": latency.Milliseconds()})
|
||||
}
|
||||
|
||||
// TestDatabaseConnectionByID re-probes a registered external database using
|
||||
// its stored (decrypted) credentials.
|
||||
func (h *DatabaseHandler) TestDatabaseConnectionByID(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
databaseID := c.Param("id")
|
||||
userID = h.effectiveUserID(c, userID, databaseID)
|
||||
|
||||
row, err := h.queries.GetDatabaseServiceByIDAndUser(c.Request.Context(), sqlcdb.GetDatabaseServiceByIDAndUserParams{
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Database not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch database"})
|
||||
return
|
||||
}
|
||||
if row.Provider != "external" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "test-connection applies to external databases", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
req := externalDatabaseRequest{
|
||||
Type: row.Type,
|
||||
Host: databaseNullString(row.ExternalHost),
|
||||
Port: int(row.ExternalPort.Int32),
|
||||
Database: databaseNullString(row.ExternalName),
|
||||
Username: databaseNullString(row.ExternalUsername),
|
||||
Password: secrets.Decrypt(row.ExternalPassword),
|
||||
SSL: row.ExternalSsl,
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
latency, probeErr := probeExternalDatabase(ctx, req)
|
||||
if probeErr != nil {
|
||||
c.JSON(http.StatusOK, gin.H{"ok": false, "error": probeErr.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"ok": true, "latency_ms": latency.Milliseconds()})
|
||||
}
|
||||
|
||||
func normalizeExternalRequest(req *externalDatabaseRequest) error {
|
||||
req.Type = normalizeDatabaseType(req.Type)
|
||||
if _, ok := supportedDatabaseTypes[req.Type]; !ok {
|
||||
return fmt.Errorf("%w: %s", errUnsupportedDatabaseType, req.Type)
|
||||
}
|
||||
req.Host = strings.TrimSpace(req.Host)
|
||||
if req.Host == "" {
|
||||
return errors.New("host is required")
|
||||
}
|
||||
if req.Port <= 0 {
|
||||
req.Port = externalDefaultPorts[req.Type]
|
||||
}
|
||||
if req.Port <= 0 || req.Port > 65535 {
|
||||
return errors.New("port must be 1-65535")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RegisterExternalDatabase records a database hosted elsewhere — same list,
|
||||
// same bind surface, no container. The connection is probed first so typos
|
||||
// fail at registration, not at the first service bind.
|
||||
func (h *DatabaseHandler) RegisterExternalDatabase(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req externalDatabaseRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
if req.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if err := normalizeExternalRequest(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
probeCtx, cancel := context.WithTimeout(c.Request.Context(), 10*time.Second)
|
||||
defer cancel()
|
||||
if _, err := probeExternalDatabase(probeCtx, req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{
|
||||
"error": fmt.Sprintf("Connection check failed: %v", err),
|
||||
"code": "DEPENDENCY_UNAVAILABLE",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
count, err := h.queries.CountDatabaseServicesByUserAndName(c.Request.Context(), sqlcdb.CountDatabaseServicesByUserAndNameParams{
|
||||
UserID: userID,
|
||||
Name: req.Name,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to validate database name"})
|
||||
return
|
||||
}
|
||||
if count > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "database name already exists", "code": "CONFLICT"})
|
||||
return
|
||||
}
|
||||
|
||||
databaseID := generateDatabaseID(req.Name)
|
||||
now := sql.NullTime{Time: time.Now(), Valid: true}
|
||||
encPassword := ""
|
||||
if req.Password != "" {
|
||||
encPassword = secrets.Encrypt(req.Password)
|
||||
}
|
||||
if err := h.queries.CreateExternalDatabaseService(c.Request.Context(), sqlcdb.CreateExternalDatabaseServiceParams{
|
||||
ID: databaseID,
|
||||
UserID: userID,
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Version: "",
|
||||
Plan: "external",
|
||||
Region: "external",
|
||||
ExternalHost: sql.NullString{String: req.Host, Valid: true},
|
||||
ExternalPort: sql.NullInt32{Int32: int32(req.Port), Valid: true},
|
||||
ExternalName: sql.NullString{String: req.Database, Valid: req.Database != ""},
|
||||
ExternalUsername: sql.NullString{String: req.Username, Valid: req.Username != ""},
|
||||
ExternalPassword: encPassword,
|
||||
ExternalSsl: req.SSL,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to register database"})
|
||||
return
|
||||
}
|
||||
|
||||
LogAudit(userID, "database", databaseID, "register-external", map[string]interface{}{
|
||||
"name": req.Name,
|
||||
"type": req.Type,
|
||||
"host": req.Host,
|
||||
})
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"id": databaseID,
|
||||
"message": "External database registered",
|
||||
"status": "running",
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeExternalRequestDefaults(t *testing.T) {
|
||||
req := externalDatabaseRequest{Type: "PostgreSQL", Host: " db.example.com ", Port: 0}
|
||||
if err := normalizeExternalRequest(&req); err != nil {
|
||||
t.Fatalf("normalize failed: %v", err)
|
||||
}
|
||||
if req.Type != "postgresql" {
|
||||
t.Fatalf("type not normalized: %q", req.Type)
|
||||
}
|
||||
if req.Host != "db.example.com" {
|
||||
t.Fatalf("host not trimmed: %q", req.Host)
|
||||
}
|
||||
if req.Port != 5432 {
|
||||
t.Fatalf("default port = %d, want 5432", req.Port)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeExternalRequestRejects(t *testing.T) {
|
||||
cases := []externalDatabaseRequest{
|
||||
{Type: "sqlite", Host: "x"}, // unsupported type
|
||||
{Type: "postgresql", Host: " "}, // empty host
|
||||
{Type: "postgresql", Host: "h", Port: 70000}, // out of range
|
||||
}
|
||||
for i, req := range cases {
|
||||
if err := normalizeExternalRequest(&req); err == nil {
|
||||
t.Fatalf("case %d: expected error", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestExternalConnectionURLMasking(t *testing.T) {
|
||||
ext := &DatabaseExternalRef{
|
||||
Host: "db.example.com",
|
||||
Port: 5432,
|
||||
Database: "appdb",
|
||||
Username: "svc",
|
||||
SSL: true,
|
||||
}
|
||||
u := externalConnectionURL("postgresql", ext, "s3cret")
|
||||
if u != "postgres://svc:[email protected]:5432/appdb?sslmode=require" {
|
||||
t.Fatalf("unexpected pg url: %s", u)
|
||||
}
|
||||
u = externalConnectionURL("postgresql", ext, "")
|
||||
if u != "postgres://[email protected]:5432/appdb?sslmode=require" {
|
||||
t.Fatalf("unexpected passwordless url: %s", u)
|
||||
}
|
||||
|
||||
r := &DatabaseExternalRef{Host: "cache.internal", Port: 6379, SSL: true}
|
||||
u = externalConnectionURL("redis", r, "pw")
|
||||
if u != "rediss://:[email protected]:6379" {
|
||||
t.Fatalf("unexpected redis url: %s", u)
|
||||
}
|
||||
}
|
||||
|
||||
func TestProbeExternalDatabaseTCP(t *testing.T) {
|
||||
ln, err := net.Listen("tcp", "127.0.0.1:0")
|
||||
if err != nil {
|
||||
t.Fatalf("listen: %v", err)
|
||||
}
|
||||
defer ln.Close()
|
||||
port := ln.Addr().(*net.TCPAddr).Port
|
||||
|
||||
// TCP fallback path — mysql uses a plain dial.
|
||||
latency, err := probeExternalDatabase(context.Background(), externalDatabaseRequest{
|
||||
Type: "mysql", Host: "127.0.0.1", Port: port,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("probe should succeed against open listener: %v", err)
|
||||
}
|
||||
if latency <= 0 {
|
||||
t.Fatalf("latency not recorded")
|
||||
}
|
||||
|
||||
// Closed port must fail fast.
|
||||
if _, err := probeExternalDatabase(context.Background(), externalDatabaseRequest{
|
||||
Type: "mysql", Host: "127.0.0.1", Port: 1,
|
||||
}); err == nil {
|
||||
t.Fatalf("probe should fail against closed port")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedisTLSConfig(t *testing.T) {
|
||||
if redisTLSConfig(false, "h") != nil {
|
||||
t.Fatalf("disabled ssl must produce nil tls config")
|
||||
}
|
||||
cfg := redisTLSConfig(true, "db.example.com")
|
||||
if cfg == nil || cfg.ServerName != "db.example.com" {
|
||||
t.Fatalf("expected ServerName passthrough, got %+v", cfg)
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,46 @@
|
||||
package api
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSplitImageReference(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
image string
|
||||
fallback string
|
||||
wantImage string
|
||||
wantTag string
|
||||
}{
|
||||
{"empty uses fallback tag", "", "latest", "", "latest"},
|
||||
{"bare image", "nginx", "latest", "nginx", "latest"},
|
||||
{"tagged image", "nginx:1.27", "latest", "nginx", "1.27"},
|
||||
{"registry with port untagged", "registry.local:5000/app", "latest", "registry.local:5000/app", "latest"},
|
||||
{"registry with port tagged", "registry.local:5000/app:v2", "latest", "registry.local:5000/app", "v2"},
|
||||
{"digest stays whole", "nginx@sha256:abc123", "latest", "nginx@sha256:abc123", "latest"},
|
||||
{"tag and digest", "nginx:1.27@sha256:abc", "latest", "nginx:1.27@sha256:abc", "latest"},
|
||||
{"namespaced tagged", "ghcr.io/owner/repo:dev", "latest", "ghcr.io/owner/repo", "dev"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
img, tag := splitImageReference(tc.image, tc.fallback)
|
||||
if img != tc.wantImage || tag != tc.wantTag {
|
||||
t.Fatalf("splitImageReference(%q, %q) = (%q, %q), want (%q, %q)",
|
||||
tc.image, tc.fallback, img, tag, tc.wantImage, tc.wantTag)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMapEngineStatusToDBStatus(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"running": "deployed",
|
||||
"building": "building",
|
||||
"failed": "failed",
|
||||
"cancelled": "cancelled",
|
||||
"queued": "queued",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := mapEngineStatusToDBStatus(in); got != want {
|
||||
t.Fatalf("mapEngineStatusToDBStatus(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,457 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// ServiceDomain is one hostname routed to a service. services.domain stays
|
||||
// the derived default; this table is the source of truth for the full set.
|
||||
type ServiceDomain struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
ServiceID uuid.UUID `json:"service_id"`
|
||||
Domain string `json:"domain"`
|
||||
IsDefault bool `json:"is_default"`
|
||||
CertType string `json:"cert_type"`
|
||||
CertStatus string `json:"cert_status"`
|
||||
LastCheckedAt *time.Time `json:"last_checked_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
// BasicAuthCred is a plaintext credential the server encodes to htpasswd.
|
||||
type BasicAuthCred struct {
|
||||
Username string `json:"username" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
|
||||
var hostnameRE = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?(\.[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?)*$`)
|
||||
|
||||
func validHostname(d string) bool {
|
||||
return len(d) <= 253 && hostnameRE.MatchString(d)
|
||||
}
|
||||
|
||||
func loadServiceDomains(db *database.DB, serviceID uuid.UUID) []ServiceDomain {
|
||||
rows, err := sqlcdb.New(db.DB).ListServiceDomains(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]ServiceDomain, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
d := ServiceDomain{
|
||||
ID: r.ID,
|
||||
ServiceID: r.ServiceID,
|
||||
Domain: r.Domain,
|
||||
IsDefault: r.IsDefault,
|
||||
CertType: r.CertType,
|
||||
CertStatus: r.CertStatus,
|
||||
CreatedAt: r.CreatedAt,
|
||||
}
|
||||
if r.LastCheckedAt.Valid {
|
||||
t := r.LastCheckedAt.Time
|
||||
d.LastCheckedAt = &t
|
||||
}
|
||||
out = append(out, d)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func serviceDomainNames(db *database.DB, serviceID uuid.UUID, fallback string) []string {
|
||||
domains := loadServiceDomains(db, serviceID)
|
||||
out := make([]string, 0, len(domains))
|
||||
for _, d := range domains {
|
||||
out = append(out, d.Domain)
|
||||
}
|
||||
if len(out) == 0 && fallback != "" {
|
||||
out = []string{fallback}
|
||||
}
|
||||
if len(out) == 0 {
|
||||
out = serviceAutoDomains(db, serviceID)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// dnsLabel normalizes a service name into a DNS label: lowercase alnum
|
||||
// plus single dashes, never leading/trailing, ≤63 chars.
|
||||
func dnsLabel(name string) string {
|
||||
var b strings.Builder
|
||||
lastWasSep := false
|
||||
for _, r := range strings.ToLower(strings.TrimSpace(name)) {
|
||||
if (r >= 'a' && r <= 'z') || (r >= '0' && r <= '9') {
|
||||
b.WriteRune(r)
|
||||
lastWasSep = false
|
||||
continue
|
||||
}
|
||||
if !lastWasSep && b.Len() > 0 {
|
||||
b.WriteByte('-')
|
||||
lastWasSep = true
|
||||
}
|
||||
}
|
||||
out := strings.Trim(b.String(), "-")
|
||||
if len(out) > 63 {
|
||||
out = strings.Trim(out[:63], "-")
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// serviceAutoDomains generates <service>.<node-default-domain> hostnames
|
||||
// for services with no explicit domains. Pinned services take their node's
|
||||
// base domain; spread services union every eligible node's base domain.
|
||||
// Local services get nothing — there is no instance-level base domain yet.
|
||||
func serviceAutoDomains(db *database.DB, serviceID uuid.UUID) []string {
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
brief, err := q.GetServicePlacementBrief(ctx, serviceID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
label := dnsLabel(brief.Name)
|
||||
if label == "" {
|
||||
return nil
|
||||
}
|
||||
|
||||
var bases []string
|
||||
nodeID := brief.NodeID
|
||||
spread := brief.Spread
|
||||
switch {
|
||||
case nodeID.Valid && nodeID.String != "":
|
||||
if a, err := q.GetAgent(ctx, nodeID.String); err == nil && a.DefaultDomain != "" {
|
||||
bases = []string{a.DefaultDomain}
|
||||
}
|
||||
case spread:
|
||||
var err error
|
||||
bases, err = q.ListSchedulableAgentDomains(ctx, json.RawMessage(tagsJSON(servicePlacementTags(db, serviceID))))
|
||||
if err != nil {
|
||||
bases = nil
|
||||
}
|
||||
}
|
||||
out := make([]string, 0, len(bases))
|
||||
for _, base := range bases {
|
||||
out = append(out, label+"."+base)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// syncDefaultDomain writes the is_default row back to services.domain so
|
||||
// legacy reads (CLI tables, public_url) keep working.
|
||||
func syncDefaultDomain(db *database.DB, serviceID uuid.UUID) {
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
domain, err := q.GetServiceDomainDefault(ctx, serviceID)
|
||||
if err == sql.ErrNoRows {
|
||||
domain, err = q.GetServiceDomainFirst(ctx, serviceID)
|
||||
}
|
||||
if err == sql.ErrNoRows {
|
||||
domain = ""
|
||||
}
|
||||
_ = q.SetServiceLegacyDomain(ctx, sqlcdb.SetServiceLegacyDomainParams{Domain: domain, ID: serviceID})
|
||||
}
|
||||
|
||||
// serviceAccess loads the maintenance/basic-auth columns for the runtime spec.
|
||||
func serviceAccess(db *database.DB, serviceID uuid.UUID) (maintenance bool, basicAuth string) {
|
||||
row, err := sqlcdb.New(db.DB).GetServiceAccess(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
return false, ""
|
||||
}
|
||||
return row.MaintenanceMode, row.BasicAuthUsers
|
||||
}
|
||||
|
||||
// maintenanceURL is the redirect target for maintenance mode — the public
|
||||
// base URL of this API plus the maintenance page path.
|
||||
func maintenanceURL(db *database.DB) string {
|
||||
base := strings.TrimRight(settingValue(db, "base_url", "BASE_URL", ""), "/")
|
||||
if base == "" {
|
||||
return ""
|
||||
}
|
||||
return base + "/api/v1/maintenance"
|
||||
}
|
||||
|
||||
// encodeBasicAuth turns plaintext creds into Traefik's htpasswd format
|
||||
// (user:bcrypt hash, comma-separated). Bcrypt is what Traefik expects.
|
||||
func encodeBasicAuth(creds []BasicAuthCred) (string, error) {
|
||||
pairs := make([]string, 0, len(creds))
|
||||
for _, cred := range creds {
|
||||
if cred.Username == "" || cred.Password == "" {
|
||||
continue
|
||||
}
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(cred.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
pairs = append(pairs, cred.Username+":"+string(hash))
|
||||
}
|
||||
return strings.Join(pairs, ","), nil
|
||||
}
|
||||
|
||||
// basicAuthUsernames returns just the usernames for API responses — never
|
||||
// the hashes.
|
||||
func basicAuthUsernames(encoded string) []string {
|
||||
var out []string
|
||||
for _, pair := range strings.Split(encoded, ",") {
|
||||
if u, _, ok := strings.Cut(pair, ":"); ok && u != "" {
|
||||
out = append(out, u)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// serviceWriteAccess mirrors the service update handler's access check.
|
||||
func serviceWriteAccess(c *gin.Context, db *database.DB, serviceID uuid.UUID) bool {
|
||||
userID, _ := c.Get("user_id")
|
||||
uid, _ := uuid.Parse(fmt.Sprint(userID))
|
||||
ok, err := sqlcdb.New(db.DB).ServiceWriteAccess(context.Background(), sqlcdb.ServiceWriteAccessParams{
|
||||
ID: serviceID,
|
||||
OwnerID: uid,
|
||||
Column3: contextIsAdmin(c),
|
||||
})
|
||||
return err == nil && ok
|
||||
}
|
||||
|
||||
func handleListServiceDomains(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Database connection not available")
|
||||
return
|
||||
}
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid service ID")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"domains": loadServiceDomains(db.(*database.DB), serviceID)})
|
||||
}
|
||||
|
||||
func handleAddServiceDomain(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Database connection not available")
|
||||
return
|
||||
}
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid service ID")
|
||||
return
|
||||
}
|
||||
if !serviceWriteAccess(c, db.(*database.DB), serviceID) {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Service not found")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Domain string `json:"domain" binding:"required"`
|
||||
IsDefault bool `json:"is_default"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", err.Error())
|
||||
return
|
||||
}
|
||||
domain := strings.ToLower(strings.TrimSpace(req.Domain))
|
||||
if !validHostname(domain) {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid hostname")
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
q := sqlcdb.New(db.(*database.DB).DB)
|
||||
|
||||
// First domain on a service becomes the default automatically.
|
||||
count, _ := q.CountServiceDomains(ctx, serviceID)
|
||||
makeDefault := req.IsDefault || count == 0
|
||||
|
||||
tx, err := db.(*database.DB).Begin()
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Failed to add domain")
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
qtx := q.WithTx(tx)
|
||||
if makeDefault {
|
||||
if err := qtx.ClearServiceDomainDefault(ctx, serviceID); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Failed to add domain")
|
||||
return
|
||||
}
|
||||
}
|
||||
row, err := qtx.CreateServiceDomain(ctx, sqlcdb.CreateServiceDomainParams{
|
||||
ServiceID: serviceID,
|
||||
Domain: domain,
|
||||
IsDefault: makeDefault,
|
||||
})
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "duplicate") || strings.Contains(err.Error(), "unique") {
|
||||
respondError(c, http.StatusConflict, "CONFLICT", "Domain already attached to this service")
|
||||
return
|
||||
}
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Failed to add domain")
|
||||
return
|
||||
}
|
||||
if err := tx.Commit(); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Failed to add domain")
|
||||
return
|
||||
}
|
||||
syncDefaultDomain(db.(*database.DB), serviceID)
|
||||
c.JSON(http.StatusCreated, gin.H{"domain": ServiceDomain{
|
||||
ID: row.ID,
|
||||
ServiceID: row.ServiceID,
|
||||
Domain: row.Domain,
|
||||
IsDefault: row.IsDefault,
|
||||
CertType: row.CertType,
|
||||
CertStatus: row.CertStatus,
|
||||
CreatedAt: row.CreatedAt,
|
||||
}})
|
||||
}
|
||||
|
||||
func handleDeleteServiceDomain(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Database connection not available")
|
||||
return
|
||||
}
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid service ID")
|
||||
return
|
||||
}
|
||||
if !serviceWriteAccess(c, db.(*database.DB), serviceID) {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Service not found")
|
||||
return
|
||||
}
|
||||
domainID, err := uuid.Parse(c.Param("domain_id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid domain ID")
|
||||
return
|
||||
}
|
||||
affected, err := sqlcdb.New(db.(*database.DB).DB).DeleteServiceDomain(
|
||||
c.Request.Context(), sqlcdb.DeleteServiceDomainParams{ID: domainID, ServiceID: serviceID})
|
||||
if err != nil || affected == 0 {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Domain not found")
|
||||
return
|
||||
}
|
||||
syncDefaultDomain(db.(*database.DB), serviceID)
|
||||
c.JSON(http.StatusOK, gin.H{"deleted": domainID})
|
||||
}
|
||||
|
||||
func handleSetDefaultDomain(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Database connection not available")
|
||||
return
|
||||
}
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid service ID")
|
||||
return
|
||||
}
|
||||
if !serviceWriteAccess(c, db.(*database.DB), serviceID) {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Service not found")
|
||||
return
|
||||
}
|
||||
domainID, err := uuid.Parse(c.Param("domain_id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid domain ID")
|
||||
return
|
||||
}
|
||||
affected, err := sqlcdb.New(db.(*database.DB).DB).SetServiceDomainDefault(
|
||||
c.Request.Context(), sqlcdb.SetServiceDomainDefaultParams{ID: domainID, ServiceID: serviceID})
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Failed to set default domain")
|
||||
return
|
||||
}
|
||||
if affected == 0 {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Domain not found")
|
||||
return
|
||||
}
|
||||
syncDefaultDomain(db.(*database.DB), serviceID)
|
||||
c.JSON(http.StatusOK, gin.H{"domains": loadServiceDomains(db.(*database.DB), serviceID)})
|
||||
}
|
||||
|
||||
// handleCheckServiceDomains resolves each domain's DNS and compares it to
|
||||
// the configured expected target (app_settings.public_ip / PUBLIC_IP, or the
|
||||
// base_url host). Statuses: ok | wrong-target | pending.
|
||||
func handleCheckServiceDomains(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Database connection not available")
|
||||
return
|
||||
}
|
||||
dbh := db.(*database.DB)
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid service ID")
|
||||
return
|
||||
}
|
||||
|
||||
expectedIP := settingValue(dbh, "public_ip", "PUBLIC_IP", "")
|
||||
expectedHost := ""
|
||||
if base := settingValue(dbh, "base_url", "BASE_URL", ""); base != "" {
|
||||
if u := strings.TrimPrefix(strings.TrimPrefix(base, "https://"), "http://"); u != "" {
|
||||
expectedHost, _, _ = strings.Cut(u, "/")
|
||||
expectedHost, _, _ = strings.Cut(expectedHost, ":")
|
||||
}
|
||||
}
|
||||
|
||||
type result struct {
|
||||
Domain string `json:"domain"`
|
||||
Status string `json:"status"` // ok | wrong-target | pending
|
||||
Resolved []string `json:"resolved"`
|
||||
}
|
||||
results := []result{}
|
||||
resolver := net.DefaultResolver
|
||||
for _, d := range loadServiceDomains(dbh, serviceID) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 4*time.Second)
|
||||
addrs, _ := resolver.LookupHost(ctx, d.Domain)
|
||||
cname, _ := resolver.LookupCNAME(ctx, d.Domain)
|
||||
cancel()
|
||||
|
||||
r := result{Domain: d.Domain, Resolved: addrs}
|
||||
cname = strings.TrimSuffix(cname, ".")
|
||||
switch {
|
||||
case len(addrs) == 0:
|
||||
r.Status = "pending"
|
||||
case expectedIP != "":
|
||||
r.Status = "wrong-target"
|
||||
for _, a := range addrs {
|
||||
if a == expectedIP {
|
||||
r.Status = "ok"
|
||||
}
|
||||
}
|
||||
case expectedHost != "":
|
||||
r.Status = "ok" // resolves; CNAME match is advisory only
|
||||
if cname != "" && cname != d.Domain && cname != expectedHost {
|
||||
r.Status = "wrong-target"
|
||||
}
|
||||
default:
|
||||
r.Status = "ok"
|
||||
}
|
||||
results = append(results, r)
|
||||
_ = sqlcdb.New(dbh.DB).TouchServiceDomainChecked(c.Request.Context(), d.ID)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"domains": results})
|
||||
}
|
||||
|
||||
// handleMaintenancePage serves the maintenance-mode landing page. Public:
|
||||
// browsers hit it via the Traefik redirect before any auth.
|
||||
func handleMaintenancePage(c *gin.Context) {
|
||||
c.Data(http.StatusOK, "text/html; charset=utf-8", []byte(`<!doctype html>
|
||||
<html lang="en"><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1">
|
||||
<title>Maintenance</title><style>
|
||||
body{margin:0;min-height:100vh;display:flex;align-items:center;justify-content:center;background:#0b0e14;color:#e6e8ee;font-family:system-ui,sans-serif}
|
||||
.card{text-align:center;padding:2rem}.title{font-size:1.4rem;font-weight:600;margin-bottom:.5rem}
|
||||
.sub{color:#9aa3b2;font-size:.95rem}.brand{margin-top:2rem;color:#5b6472;font-size:.75rem;letter-spacing:.12em;text-transform:uppercase}
|
||||
</style></head><body><div class="card">
|
||||
<div class="title">This service is under maintenance</div>
|
||||
<div class="sub">Please check back shortly.</div>
|
||||
<div class="brand">Containr</div>
|
||||
</div></body></html>`))
|
||||
}
|
||||
@@ -0,0 +1,601 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"os"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func testDSNString(t *testing.T) string {
|
||||
t.Helper()
|
||||
dsn := os.Getenv("METRICS_TEST_DSN")
|
||||
if dsn == "" {
|
||||
dsn = os.Getenv("DATABASE_URL")
|
||||
}
|
||||
if dsn == "" {
|
||||
t.Skip("METRICS_TEST_DSN not set")
|
||||
}
|
||||
return dsn
|
||||
}
|
||||
|
||||
func TestDNSLabel(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"my-app": "my-app",
|
||||
"My App_2": "my-app-2",
|
||||
" web ": "web",
|
||||
"__weird__name": "weird-name",
|
||||
"---": "",
|
||||
"": "",
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := dnsLabel(in); got != want {
|
||||
t.Errorf("dnsLabel(%q) = %q, want %q", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestDNSLabelLengthCap(t *testing.T) {
|
||||
long := "a" + string(make([]byte, 0)) + "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"
|
||||
if got := dnsLabel(long); len(got) > 63 {
|
||||
t.Fatalf("label over 63 chars: %d", len(got))
|
||||
}
|
||||
}
|
||||
|
||||
// Live invariant test against real Postgres — set METRICS_TEST_DSN (or
|
||||
// DATABASE_URL). Verifies the generated domain queries preserve the
|
||||
// default-domain and legacy services.domain invariants.
|
||||
func TestServiceDomainsLiveInvariants(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
serviceID := uuid.New()
|
||||
suffix := strings.ReplaceAll(serviceID.String()[:8], "-", "")
|
||||
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test')`,
|
||||
userID, "domtest-"+suffix+"@test.local")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, true)`,
|
||||
projectID, "domtest-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type)
|
||||
VALUES ($1, 'domtest-`+suffix+`', $2, $3, 'web', 'image')`,
|
||||
serviceID, projectID, envID)
|
||||
must(err)
|
||||
defer func() {
|
||||
_, _ = db.Exec(`DELETE FROM users WHERE id = $1`, userID)
|
||||
}()
|
||||
|
||||
d1, err := q.CreateServiceDomain(ctx, sqlcdb.CreateServiceDomainParams{
|
||||
ServiceID: serviceID, Domain: "a-" + suffix + ".example.com", IsDefault: true})
|
||||
if err != nil {
|
||||
t.Fatalf("create d1: %v", err)
|
||||
}
|
||||
d2, err := q.CreateServiceDomain(ctx, sqlcdb.CreateServiceDomainParams{
|
||||
ServiceID: serviceID, Domain: "b-" + suffix + ".example.com", IsDefault: false})
|
||||
if err != nil {
|
||||
t.Fatalf("create d2: %v", err)
|
||||
}
|
||||
|
||||
syncDefaultDomain(db, serviceID)
|
||||
var legacy string
|
||||
if err := db.QueryRow(`SELECT domain FROM services WHERE id = $1`, serviceID).Scan(&legacy); err != nil {
|
||||
t.Fatalf("read legacy: %v", err)
|
||||
}
|
||||
if legacy != d1.Domain {
|
||||
t.Fatalf("legacy domain = %q, want %q", legacy, d1.Domain)
|
||||
}
|
||||
|
||||
// Switch default: exactly one row stays default, legacy follows.
|
||||
if err := q.ClearServiceDomainDefault(ctx, serviceID); err != nil {
|
||||
t.Fatalf("clear: %v", err)
|
||||
}
|
||||
if _, err := q.SetServiceDomainDefault(ctx, sqlcdb.SetServiceDomainDefaultParams{
|
||||
ID: d2.ID, ServiceID: serviceID}); err != nil {
|
||||
t.Fatalf("set default: %v", err)
|
||||
}
|
||||
syncDefaultDomain(db, serviceID)
|
||||
if err := db.QueryRow(`SELECT domain FROM services WHERE id = $1`, serviceID).Scan(&legacy); err != nil {
|
||||
t.Fatalf("read legacy2: %v", err)
|
||||
}
|
||||
if legacy != d2.Domain {
|
||||
t.Fatalf("legacy after switch = %q, want %q", legacy, d2.Domain)
|
||||
}
|
||||
|
||||
doms := loadServiceDomains(db, serviceID)
|
||||
if len(doms) != 2 || !doms[0].IsDefault || doms[0].Domain != d2.Domain {
|
||||
t.Fatalf("list order wrong: %+v", doms)
|
||||
}
|
||||
|
||||
// Deleting the default falls back to the remaining domain.
|
||||
if _, err := q.DeleteServiceDomain(ctx, sqlcdb.DeleteServiceDomainParams{
|
||||
ID: d2.ID, ServiceID: serviceID}); err != nil {
|
||||
t.Fatalf("delete d2: %v", err)
|
||||
}
|
||||
syncDefaultDomain(db, serviceID)
|
||||
if err := db.QueryRow(`SELECT domain FROM services WHERE id = $1`, serviceID).Scan(&legacy); err != nil {
|
||||
t.Fatalf("read legacy3: %v", err)
|
||||
}
|
||||
if legacy != d1.Domain {
|
||||
t.Fatalf("legacy after delete = %q, want %q", legacy, d1.Domain)
|
||||
}
|
||||
}
|
||||
|
||||
// Live clone test: cloneServiceRow must copy config, domains, and variables
|
||||
// verbatim, start stopped, and land in the target project/environment.
|
||||
func TestCloneServiceRowLive(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
serviceID := uuid.New()
|
||||
suffix := strings.ReplaceAll(serviceID.String()[:8], "-", "")
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test')`,
|
||||
userID, "clone-"+suffix+"@test.local")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, true)`,
|
||||
projectID, "clone-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type,
|
||||
image_name, port, volumes, traefik_labels, maintenance_mode)
|
||||
VALUES ($1, $2, $3, $4, 'web', 'image', 'traefik/whoami:latest', 80,
|
||||
'[{"mount":"/data"}]'::jsonb,
|
||||
'{"middlewares.rl.ratelimit.average":"100"}'::jsonb, true)`,
|
||||
serviceID, "clonesrc-"+suffix, projectID, envID)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE id = $1`, userID) }()
|
||||
|
||||
if _, err := q.CreateServiceDomain(ctx, sqlcdb.CreateServiceDomainParams{
|
||||
ServiceID: serviceID, Domain: "src-" + suffix + ".example.com", IsDefault: true}); err != nil {
|
||||
t.Fatalf("seed domain: %v", err)
|
||||
}
|
||||
if _, err := db.Exec(`INSERT INTO environment_variables (id, service_id, key, value, is_secret)
|
||||
VALUES ($1, $2, 'FOO', 'bar', false)`, uuid.New(), serviceID); err != nil {
|
||||
t.Fatalf("seed var: %v", err)
|
||||
}
|
||||
|
||||
cloneID, err := cloneServiceRow(db, serviceID, projectID, "clone-"+suffix, "")
|
||||
if err != nil {
|
||||
t.Fatalf("clone: %v", err)
|
||||
}
|
||||
|
||||
var status, imageName, env string
|
||||
var maintenance bool
|
||||
var volumes, labels []byte
|
||||
if err := db.QueryRow(`SELECT status, image_name, COALESCE(environment,''), maintenance_mode,
|
||||
volumes, traefik_labels FROM services WHERE id = $1`, cloneID).
|
||||
Scan(&status, &imageName, &env, &maintenance, &volumes, &labels); err != nil {
|
||||
t.Fatalf("read clone: %v", err)
|
||||
}
|
||||
if status != "stopped" || imageName != "traefik/whoami:latest" || env != "production" {
|
||||
t.Fatalf("clone mismatch: %v %v %v", status, imageName, env)
|
||||
}
|
||||
if !maintenance || string(volumes) == "[]" || string(labels) == "{}" {
|
||||
t.Fatalf("clone dropped config: maint=%v vol=%s labels=%s", maintenance, volumes, labels)
|
||||
}
|
||||
doms := loadServiceDomains(db, cloneID)
|
||||
if len(doms) != 1 || doms[0].Domain != "src-"+suffix+".example.com" || !doms[0].IsDefault {
|
||||
t.Fatalf("domains not cloned: %+v", doms)
|
||||
}
|
||||
var varVal string
|
||||
if err := db.QueryRow(`SELECT value FROM environment_variables WHERE service_id = $1 AND key = 'FOO'`,
|
||||
cloneID).Scan(&varVal); err != nil || varVal != "bar" {
|
||||
t.Fatalf("vars not cloned: %v %q", err, varVal)
|
||||
}
|
||||
}
|
||||
|
||||
// Live preview-environment test: generated queries must preserve the
|
||||
// access-check join, the status-sync CASE, and the sweep listing.
|
||||
func TestPreviewEnvironmentsLiveInvariants(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
srcID := uuid.New()
|
||||
cloneID := uuid.New()
|
||||
previewID := uuid.New()
|
||||
suffix := strings.ReplaceAll(previewID.String()[:8], "-", "")
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test')`,
|
||||
userID, "prev-"+suffix+"@test.local")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, true)`,
|
||||
projectID, "prev-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
for _, p := range [][3]interface{}{{srcID, "src-" + suffix, "running"}, {cloneID, "clone-" + suffix, "running"}} {
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type, status)
|
||||
VALUES ($1, $2, $3, $4, 'web', 'image', $5)`,
|
||||
p[0], p[1], projectID, envID, p[2])
|
||||
must(err)
|
||||
}
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE id = $1`, userID) }()
|
||||
|
||||
must(q.CreatePreviewEnvironment(ctx, sqlcdb.CreatePreviewEnvironmentParams{
|
||||
ID: previewID,
|
||||
ProjectID: projectID,
|
||||
ServiceID: srcID,
|
||||
PreviewServiceID: uuid.NullUUID{UUID: cloneID, Valid: true},
|
||||
BranchName: "feat-" + suffix,
|
||||
Environment: "preview-feat-" + suffix,
|
||||
Status: "building",
|
||||
ExpiresAt: time.Now().Add(-time.Hour), // already past TTL → sweepable
|
||||
}))
|
||||
|
||||
// List + service join.
|
||||
rows, err := q.ListPreviewEnvironmentsForProject(ctx, projectID)
|
||||
if err != nil || len(rows) != 1 {
|
||||
t.Fatalf("list: %v %d", err, len(rows))
|
||||
}
|
||||
if rows[0].ID != previewID || !rows[0].SvcID.Valid || rows[0].ServiceName.String != "src-"+suffix {
|
||||
t.Fatalf("list row: %+v", rows[0])
|
||||
}
|
||||
|
||||
// Access-check join: owner reads; a stranger does not.
|
||||
if _, err := q.GetPreviewEnvironment(ctx, sqlcdb.GetPreviewEnvironmentParams{
|
||||
ID: previewID, OwnerID: userID}); err != nil {
|
||||
t.Fatalf("owner read: %v", err)
|
||||
}
|
||||
if _, err := q.GetPreviewEnvironment(ctx, sqlcdb.GetPreviewEnvironmentParams{
|
||||
ID: previewID, OwnerID: uuid.New()}); err != nil {
|
||||
t.Fatalf("approved project should be public-read: %v", err)
|
||||
}
|
||||
|
||||
// Status sync folds clone's 'running' into the preview row.
|
||||
if err := q.SyncPreviewStatuses(ctx); err != nil {
|
||||
t.Fatalf("sync: %v", err)
|
||||
}
|
||||
var status string
|
||||
if err := db.QueryRow(`SELECT status FROM preview_environments WHERE id = $1`, previewID).Scan(&status); err != nil {
|
||||
t.Fatalf("read status: %v", err)
|
||||
}
|
||||
if status != "running" {
|
||||
t.Fatalf("status after sync = %q, want running", status)
|
||||
}
|
||||
|
||||
// TTL sweep picks it up (expired at insert), marks expired.
|
||||
sweep, err := q.ListSweepablePreviews(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("sweep list: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, s := range sweep {
|
||||
if s.ID == previewID && s.PreviewServiceID.UUID == cloneID {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("sweepable list missing preview: %+v", sweep)
|
||||
}
|
||||
must(q.MarkPreviewEnvironmentStatus(ctx, sqlcdb.MarkPreviewEnvironmentStatusParams{Status: "expired", ID: previewID}))
|
||||
sweep, _ = q.ListSweepablePreviews(ctx)
|
||||
for _, s := range sweep {
|
||||
if s.ID == previewID {
|
||||
t.Fatal("expired preview still sweepable")
|
||||
}
|
||||
}
|
||||
|
||||
// Owner lookup and delete.
|
||||
owner, err := q.GetPreviewEnvironmentOwner(ctx, previewID)
|
||||
if err != nil || owner != userID {
|
||||
t.Fatalf("owner: %v %v", owner, err)
|
||||
}
|
||||
must(q.DeletePreviewEnvironment(ctx, previewID))
|
||||
}
|
||||
|
||||
// Live deployment lifecycle: generated queries preserve list ordering,
|
||||
// the access join, and the status-transition writes.
|
||||
func TestDeploymentsLiveInvariants(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
serviceID := uuid.New()
|
||||
deployID := uuid.New()
|
||||
suffix := strings.ReplaceAll(deployID.String()[:8], "-", "")
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test')`,
|
||||
userID, "dep-"+suffix+"@test.local")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, true)`,
|
||||
projectID, "dep-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type, status)
|
||||
VALUES ($1, $2, $3, $4, 'web', 'image', 'running')`,
|
||||
serviceID, "dep-"+suffix, projectID, envID)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE id = $1`, userID) }()
|
||||
|
||||
must(q.InsertDeployment(ctx, sqlcdb.InsertDeploymentParams{
|
||||
ID: deployID,
|
||||
ServiceID: serviceID,
|
||||
Version: "v1-" + suffix,
|
||||
Status: sql.NullString{String: "pending", Valid: true},
|
||||
}))
|
||||
total, err := q.CountDeploymentsForService(ctx, serviceID)
|
||||
if err != nil || total != 1 {
|
||||
t.Fatalf("count: %v %d", err, total)
|
||||
}
|
||||
rows, err := q.ListDeploymentsForService(ctx, sqlcdb.ListDeploymentsForServiceParams{
|
||||
ServiceID: serviceID, Limit: 10, Offset: 0})
|
||||
if err != nil || len(rows) != 1 || rows[0].ID != deployID {
|
||||
t.Fatalf("list: %v %+v", err, rows)
|
||||
}
|
||||
|
||||
// Access join returns owner.
|
||||
access, err := q.GetDeploymentAccess(ctx, deployID)
|
||||
if err != nil || access.OwnerID != userID || access.ServiceID != serviceID {
|
||||
t.Fatalf("access: %v %+v", err, access)
|
||||
}
|
||||
|
||||
// Recent feed honors the access predicate.
|
||||
feed, err := q.ListRecentAccessibleDeployments(ctx, sqlcdb.ListRecentAccessibleDeploymentsParams{
|
||||
OwnerID: userID, Limit: 10, Offset: 0})
|
||||
if err != nil {
|
||||
t.Fatalf("feed: %v", err)
|
||||
}
|
||||
found := false
|
||||
for _, d := range feed {
|
||||
if d.ID == deployID && d.ServiceName == "dep-"+suffix {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("feed missing deployment: %+v", feed)
|
||||
}
|
||||
|
||||
// Status transitions.
|
||||
must(q.SetDeploymentStatus(ctx, sqlcdb.SetDeploymentStatusParams{
|
||||
Status: sql.NullString{String: "queued", Valid: true}, UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true}, ID: deployID}))
|
||||
must(q.SyncDeploymentProgress(ctx, sqlcdb.SyncDeploymentProgressParams{
|
||||
Status: sql.NullString{String: "deployed", Valid: true},
|
||||
ImageName: sql.NullString{String: "nginx", Valid: true},
|
||||
ImageTag: sql.NullString{String: "alpine", Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: deployID,
|
||||
}))
|
||||
r, err := q.GetDeploymentWithProject(ctx, deployID)
|
||||
if err != nil {
|
||||
t.Fatalf("get: %v", err)
|
||||
}
|
||||
if r.Status.String != "deployed" || r.ImageName.String != "nginx" || r.ProjectID != projectID {
|
||||
t.Fatalf("post-sync row: %+v", r)
|
||||
}
|
||||
must(q.FailDeployment(ctx, sqlcdb.FailDeploymentParams{
|
||||
Error: sql.NullString{String: "boom", Valid: true},
|
||||
CompletedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: deployID,
|
||||
}))
|
||||
r, _ = q.GetDeploymentWithProject(ctx, deployID)
|
||||
if r.Status.String != "failed" || r.Error.String != "boom" {
|
||||
t.Fatalf("post-fail row: %+v", r)
|
||||
}
|
||||
}
|
||||
|
||||
// TestRuntimeLiveInvariants exercises the runtime.sql queries: the
|
||||
// legacy-column COALESCE chain in GetServiceRuntimeWithOwner, the
|
||||
// deployed-image concat, and the conditional published-port update.
|
||||
func TestRuntimeLiveInvariants(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
serviceID := uuid.New()
|
||||
suffix := strings.ReplaceAll(serviceID.String()[:8], "-", "")
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test')`,
|
||||
userID, "rt-"+suffix+"@test.local")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, true)`,
|
||||
projectID, "rt-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
// Only legacy columns populated — the COALESCE chain must surface them.
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type,
|
||||
source_url, image_name, start_command, port) VALUES ($1, $2, $3, $4, 'worker', 'git', $5, $6, 'run.sh', 8080)`,
|
||||
serviceID, "rt-"+suffix, projectID, envID, "https://git.example/x.git", "repo/img:latest")
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE id = $1`, userID) }()
|
||||
|
||||
row, err := q.GetServiceRuntimeWithOwner(ctx, serviceID)
|
||||
if err != nil {
|
||||
t.Fatalf("runtime row: %v", err)
|
||||
}
|
||||
if row.Type != "worker" || row.Image != "repo/img:latest" || row.Command != "run.sh" ||
|
||||
row.GitRepo != "https://git.example/x.git" || row.OwnerID != userID {
|
||||
t.Fatalf("COALESCE chain lost legacy columns: %+v", row)
|
||||
}
|
||||
svc := serviceFromRuntimeRow(row)
|
||||
if svc.Type != "worker" || svc.Image != "repo/img:latest" || svc.Port != 8080 {
|
||||
t.Fatalf("mapped service wrong: %+v", svc)
|
||||
}
|
||||
|
||||
// Sibling refs + port lookup.
|
||||
refs, err := q.ListProjectServiceRefs(ctx, projectID)
|
||||
if err != nil || len(refs) != 1 || refs[0].Name != "rt-"+suffix {
|
||||
t.Fatalf("refs: %v %+v", err, refs)
|
||||
}
|
||||
port, err := q.GetServicePort(ctx, serviceID)
|
||||
if err != nil || port != 8080 {
|
||||
t.Fatalf("port: %v %d", err, port)
|
||||
}
|
||||
|
||||
// Variables round-trip.
|
||||
_, err = db.Exec(`INSERT INTO environment_variables (id, service_id, key, value, is_secret)
|
||||
VALUES (gen_random_uuid(), $1, 'API_KEY', 'shh', true), (gen_random_uuid(), $1, 'PLAIN', 'p', false)`,
|
||||
serviceID)
|
||||
must(err)
|
||||
vars, err := q.ListServiceVariablesWithSecret(ctx, serviceID)
|
||||
if err != nil || len(vars) != 2 {
|
||||
t.Fatalf("vars: %v %+v", err, vars)
|
||||
}
|
||||
vals, err := q.ListServiceVariableValues(ctx, serviceID)
|
||||
if err != nil || len(vals) != 2 {
|
||||
t.Fatalf("vals: %v %+v", err, vals)
|
||||
}
|
||||
got, err := q.GetServiceVariableValue(ctx, sqlcdb.GetServiceVariableValueParams{ServiceID: serviceID, Key: "API_KEY"})
|
||||
if err != nil || got != "shh" {
|
||||
t.Fatalf("var value: %v %q", err, got)
|
||||
}
|
||||
|
||||
// Node agent listing through container_instances.
|
||||
_, err = db.Exec(`INSERT INTO node_agents (id, name, hostname, ip_address, port) VALUES ($1, $2, 'h', '127.0.0.1', 9999)`,
|
||||
"agent-"+suffix, "agent-"+suffix)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO container_instances (id, name, image, project_id, service_id, node_agent_id)
|
||||
VALUES ($1, 'c', 'img', $2, $3, $4)`,
|
||||
"ci-"+suffix, projectID.String(), serviceID.String(), "agent-"+suffix)
|
||||
must(err)
|
||||
agents, err := q.ListNodeAgentsForService(ctx, serviceID.String())
|
||||
if err != nil || len(agents) != 1 || agents[0] != "agent-"+suffix {
|
||||
t.Fatalf("agents: %v %+v", err, agents)
|
||||
}
|
||||
|
||||
// Last deployed image: only 'deployed' rows, latest first.
|
||||
d1, d2 := uuid.New(), uuid.New()
|
||||
must(q.InsertDeployment(ctx, sqlcdb.InsertDeploymentParams{
|
||||
ID: d1, ServiceID: serviceID, Version: "v1",
|
||||
Status: sql.NullString{String: "failed", Valid: true},
|
||||
}))
|
||||
must(q.InsertDeployment(ctx, sqlcdb.InsertDeploymentParams{
|
||||
ID: d2, ServiceID: serviceID, Version: "v2",
|
||||
Status: sql.NullString{String: "deployed", Valid: true},
|
||||
}))
|
||||
must(q.SyncDeploymentProgress(ctx, sqlcdb.SyncDeploymentProgressParams{
|
||||
Status: sql.NullString{String: "deployed", Valid: true},
|
||||
ImageName: sql.NullString{String: "app", Valid: true},
|
||||
ImageTag: sql.NullString{String: "v2", Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: d2,
|
||||
}))
|
||||
img, err := q.GetLastDeployedImage(ctx, serviceID)
|
||||
if err != nil || img != "app:v2" {
|
||||
t.Fatalf("last image: %v %q", err, img)
|
||||
}
|
||||
|
||||
// Conditional published-port update: writes once, no-ops on repeat.
|
||||
must(q.SetServicePublishedPort(ctx, sqlcdb.SetServicePublishedPortParams{PublishedPort: 30001, ID: serviceID}))
|
||||
p, err := q.GetServicePublishedPort(ctx, serviceID)
|
||||
if err != nil || p != 30001 {
|
||||
t.Fatalf("published port: %v %d", err, p)
|
||||
}
|
||||
must(q.SetServiceReplicas(ctx, sqlcdb.SetServiceReplicasParams{
|
||||
Replicas: 3, UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true}, ID: serviceID}))
|
||||
row, _ = q.GetServiceRuntimeWithOwner(ctx, serviceID)
|
||||
if row.Replicas != 3 {
|
||||
t.Fatalf("replicas: %+v", row)
|
||||
}
|
||||
|
||||
// Sleeper reads: candidate sweep + domain/status text lookups.
|
||||
_, err = db.Exec(`UPDATE services SET sleep_enabled = true, domain = 'rt.example.com', status = 'running' WHERE id = $1`, serviceID)
|
||||
must(err)
|
||||
cands, err := q.ListSleepCandidates(ctx)
|
||||
if err != nil {
|
||||
t.Fatalf("sleep candidates: %v", err)
|
||||
}
|
||||
var mine *sqlcdb.ListSleepCandidatesRow
|
||||
for i := range cands {
|
||||
if cands[i].ID == serviceID {
|
||||
mine = &cands[i]
|
||||
}
|
||||
}
|
||||
if mine == nil || mine.SleepIdleMinutes != 15 {
|
||||
t.Fatalf("candidate missing/idle wrong: %+v", mine)
|
||||
}
|
||||
dom, err := q.GetServiceDomainText(ctx, serviceID)
|
||||
if err != nil || dom != "rt.example.com" {
|
||||
t.Fatalf("domain text: %v %q", err, dom)
|
||||
}
|
||||
st, err := q.GetServiceStatusText(ctx, serviceID)
|
||||
if err != nil || st != "running" {
|
||||
t.Fatalf("status text: %v %q", err, st)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,140 @@
|
||||
package api
|
||||
|
||||
// Project environments — the named lanes services deploy into
|
||||
// (production/development/preview are seeded; custom names allowed).
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
var envNamePattern = regexp.MustCompile(`^[a-z0-9]([a-z0-9-]{0,48}[a-z0-9])?$`)
|
||||
|
||||
type environmentDTO struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
ServiceCount int64 `json:"service_count"`
|
||||
}
|
||||
|
||||
func toEnvironmentDTO(id uuid.UUID, name string, projectID uuid.UUID, created, updated time.Time, count int64) environmentDTO {
|
||||
return environmentDTO{ID: id, Name: name, ProjectID: projectID, CreatedAt: created, UpdatedAt: updated, ServiceCount: count}
|
||||
}
|
||||
|
||||
// handleListEnvironments returns the project's environments with live
|
||||
// service counts — the data behind the env switcher.
|
||||
func handleListEnvironments(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
projectID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid project ID")
|
||||
return
|
||||
}
|
||||
if exists, allowed := projectReadAccess(c, db, projectID); !exists || !allowed {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Project not found")
|
||||
return
|
||||
}
|
||||
rows, err := sqlcdb.New(db.DB).ListProjectEnvironments(context.Background(), projectID)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to list environments")
|
||||
return
|
||||
}
|
||||
envs := make([]environmentDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
envs = append(envs, toEnvironmentDTO(r.ID, r.Name, r.ProjectID, r.CreatedAt.Time, r.UpdatedAt.Time, r.ServiceCount))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"environments": envs})
|
||||
}
|
||||
|
||||
// handleCreateEnvironment adds a named environment lane to the project.
|
||||
func handleCreateEnvironment(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
projectID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid project ID")
|
||||
return
|
||||
}
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists || !ownsProject(db, projectID, userID.(string), contextIsAdmin(c)) {
|
||||
respondError(c, http.StatusForbidden, "FORBIDDEN", "Access denied")
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Name string `json:"name" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "name is required")
|
||||
return
|
||||
}
|
||||
name := strings.ToLower(strings.TrimSpace(req.Name))
|
||||
if !envNamePattern.MatchString(name) {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Name must be lowercase alphanumeric with dashes, 1-50 chars")
|
||||
return
|
||||
}
|
||||
q := sqlcdb.New(db.DB)
|
||||
if _, err := q.GetProjectEnvironmentByName(context.Background(), sqlcdb.GetProjectEnvironmentByNameParams{
|
||||
ProjectID: projectID, Name: name,
|
||||
}); err == nil {
|
||||
respondError(c, http.StatusConflict, "CONFLICT", "Environment already exists")
|
||||
return
|
||||
}
|
||||
if err := q.InsertProjectEnvironment(context.Background(), sqlcdb.InsertProjectEnvironmentParams{
|
||||
Name: name, ProjectID: projectID,
|
||||
}); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to create environment")
|
||||
return
|
||||
}
|
||||
env, _ := q.GetProjectEnvironmentByName(context.Background(), sqlcdb.GetProjectEnvironmentByNameParams{
|
||||
ProjectID: projectID, Name: name,
|
||||
})
|
||||
LogAuditWithRequest(c, "project", projectID.String(), "environment.create", map[string]interface{}{"name": name})
|
||||
c.JSON(http.StatusCreated, gin.H{"environment": toEnvironmentDTO(env.ID, env.Name, env.ProjectID, env.CreatedAt.Time, env.UpdatedAt.Time, 0)})
|
||||
}
|
||||
|
||||
// handleDeleteEnvironment removes an environment that holds no services.
|
||||
func handleDeleteEnvironment(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
envID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid environment ID")
|
||||
return
|
||||
}
|
||||
userID, exists := c.Get("user_id")
|
||||
if !exists {
|
||||
respondError(c, http.StatusUnauthorized, "UNAUTHORIZED", "User not authenticated")
|
||||
return
|
||||
}
|
||||
var projectID uuid.UUID
|
||||
if projectID, err = sqlcdb.New(db.DB).GetEnvironmentProjectID(context.Background(), envID); err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Environment not found")
|
||||
return
|
||||
}
|
||||
if !ownsProject(db, projectID, userID.(string), contextIsAdmin(c)) {
|
||||
respondError(c, http.StatusForbidden, "FORBIDDEN", "Access denied")
|
||||
return
|
||||
}
|
||||
q := sqlcdb.New(db.DB)
|
||||
if n, err := q.CountEnvironmentServices(context.Background(), envID); err == nil && n > 0 {
|
||||
respondError(c, http.StatusConflict, "CONFLICT", "Environment still contains services — move them first")
|
||||
return
|
||||
}
|
||||
if err := q.DeleteProjectEnvironment(context.Background(), sqlcdb.DeleteProjectEnvironmentParams{
|
||||
ID: envID, ProjectID: projectID,
|
||||
}); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to delete environment")
|
||||
return
|
||||
}
|
||||
LogAuditWithRequest(c, "project", projectID.String(), "environment.delete", map[string]interface{}{"environment_id": envID.String()})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Environment deleted"})
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Stable machine-readable error codes for agent clients. The "error" field
|
||||
// stays human-readable; "code" is the contract the CLI/MCP rely on.
|
||||
const (
|
||||
CodeUnauthorized = "UNAUTHENTICATED"
|
||||
CodeInvalidToken = "INVALID_TOKEN"
|
||||
CodeForbidden = "FORBIDDEN"
|
||||
CodeAdminRequired = "ADMIN_REQUIRED"
|
||||
CodeScopeInsufficient = "SCOPE_INSUFFICIENT"
|
||||
CodeNotFound = "NOT_FOUND"
|
||||
CodeValidation = "VALIDATION"
|
||||
CodeConflict = "CONFLICT"
|
||||
CodeInternal = "INTERNAL"
|
||||
CodeUnavailable = "DEPENDENCY_UNAVAILABLE"
|
||||
CodeIdempotencyRunning = "IDEMPOTENCY_IN_PROGRESS"
|
||||
)
|
||||
|
||||
// respondError emits the standard error envelope. Existing handlers return
|
||||
// bare {"error": msg} — this adds the stable code for new and
|
||||
// agent-facing paths.
|
||||
func respondError(c *gin.Context, status int, code, message string) {
|
||||
c.JSON(status, gin.H{"error": message, "code": code})
|
||||
c.Abort()
|
||||
}
|
||||
@@ -0,0 +1,324 @@
|
||||
package api
|
||||
|
||||
// Data plane for the public API gateway: /g/:slug/* proxies to the
|
||||
// upstream_url of the matching api_services row. Callers authenticate with
|
||||
// api_keys (X-API-Key or Authorization: Bearer), pass the rpm + monthly
|
||||
// quota gates, and every request lands in usage_counters /
|
||||
// metrics_timeseries / incident_events for the /admin/gateway analytics
|
||||
// surfaces.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/gateway"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// jarvis: ceiling — a single shared transport; per-request timeouts come
|
||||
// from api_services.request_timeout_ms on the request context.
|
||||
var gatewayHTTPClient = &http.Client{
|
||||
Transport: &http.Transport{
|
||||
MaxIdleConns: 100,
|
||||
MaxIdleConnsPerHost: 10,
|
||||
IdleConnTimeout: 90 * time.Second,
|
||||
},
|
||||
}
|
||||
|
||||
// gatewayRPM is a process-local per-minute counter map. Containr runs a
|
||||
// single API process, so an in-memory bucket is sufficient — a multi-node
|
||||
// control plane would need a Redis-backed limiter instead.
|
||||
var gatewayRPM = struct {
|
||||
sync.Mutex
|
||||
buckets map[string]*rpmBucket
|
||||
}{buckets: map[string]*rpmBucket{}}
|
||||
|
||||
type rpmBucket struct {
|
||||
window time.Time
|
||||
count int
|
||||
}
|
||||
|
||||
// gatewayRateAllow returns false when the bucket for the current minute has
|
||||
// already reached limit. A limit <= 0 disables the check. `now` is injected
|
||||
// so tests can freeze the window.
|
||||
func gatewayRateAllow(bucketKey string, limit int, now time.Time) bool {
|
||||
if limit <= 0 {
|
||||
return true
|
||||
}
|
||||
window := now.Truncate(time.Minute)
|
||||
gatewayRPM.Lock()
|
||||
defer gatewayRPM.Unlock()
|
||||
b := gatewayRPM.buckets[bucketKey]
|
||||
if b == nil || !b.window.Equal(window) {
|
||||
b = &rpmBucket{window: window}
|
||||
gatewayRPM.buckets[bucketKey] = b
|
||||
// Best-effort sweep so the map cannot grow without bound.
|
||||
for k, old := range gatewayRPM.buckets {
|
||||
if old.window.Before(window.Add(-time.Minute)) {
|
||||
delete(gatewayRPM.buckets, k)
|
||||
}
|
||||
}
|
||||
}
|
||||
b.count++
|
||||
return b.count <= limit
|
||||
}
|
||||
|
||||
type gatewayService struct {
|
||||
ID string
|
||||
UpstreamURL string
|
||||
RoutePrefix string
|
||||
UpstreamAuthHeader sql.NullString
|
||||
UpstreamAuthValue sql.NullString
|
||||
Enabled bool
|
||||
RPMLimit sql.NullInt64
|
||||
MonthlyQuota sql.NullInt64
|
||||
RequestTimeoutMS int
|
||||
}
|
||||
|
||||
type gatewayKey struct {
|
||||
ID string
|
||||
Hash string
|
||||
Enabled bool
|
||||
RPMLimit sql.NullInt64
|
||||
MonthlyQuota sql.NullInt64
|
||||
AllowedServiceIDs string
|
||||
}
|
||||
|
||||
// extractGatewayKey reads the caller's API key from X-API-Key first, then
|
||||
// Authorization: Bearer.
|
||||
func extractGatewayKey(c *gin.Context) string {
|
||||
if k := strings.TrimSpace(c.GetHeader("X-API-Key")); k != "" {
|
||||
return k
|
||||
}
|
||||
auth := strings.TrimSpace(c.GetHeader("Authorization"))
|
||||
if len(auth) > 7 && strings.EqualFold(auth[:7], "Bearer ") {
|
||||
return strings.TrimSpace(auth[7:])
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// lookupGatewayService resolves the slug to an enabled service row.
|
||||
func lookupGatewayService(ctx context.Context, db *database.DB, slug string) (*gatewayService, error) {
|
||||
r, err := sqlcdb.New(db.DB).GetAPServiceBySlug(ctx, slug)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
s := gatewayService{
|
||||
ID: r.ID.String(),
|
||||
UpstreamURL: r.UpstreamUrl,
|
||||
RoutePrefix: r.RoutePrefix,
|
||||
UpstreamAuthHeader: r.UpstreamAuthHeader,
|
||||
UpstreamAuthValue: r.UpstreamAuthValue,
|
||||
Enabled: r.Enabled != 0,
|
||||
RPMLimit: nullInt32To64(r.RpmLimit),
|
||||
MonthlyQuota: nullInt32To64(r.MonthlyQuota),
|
||||
RequestTimeoutMS: int(r.RequestTimeoutMs.Int32),
|
||||
}
|
||||
return &s, nil
|
||||
}
|
||||
|
||||
func nullInt32To64(v sql.NullInt32) sql.NullInt64 {
|
||||
return sql.NullInt64{Int64: int64(v.Int32), Valid: v.Valid}
|
||||
}
|
||||
|
||||
// authenticateGatewayKey resolves a presented key by its indexed prefix and
|
||||
// bcrypt-verifies candidates. Returns nil when nothing matches.
|
||||
func authenticateGatewayKey(ctx context.Context, db *database.DB, presented string) (*gatewayKey, error) {
|
||||
prefix := presented
|
||||
if len(prefix) > 8 {
|
||||
prefix = prefix[:8]
|
||||
}
|
||||
rows, err := sqlcdb.New(db.DB).ListAPKeysByPrefix(ctx, prefix)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
for _, r := range rows {
|
||||
k := gatewayKey{
|
||||
ID: r.ID.String(),
|
||||
Hash: r.KeyHash,
|
||||
Enabled: r.Enabled != 0,
|
||||
RPMLimit: nullInt32To64(r.RpmLimit),
|
||||
MonthlyQuota: nullInt32To64(r.MonthlyQuota),
|
||||
AllowedServiceIDs: r.AllowedServiceIds,
|
||||
}
|
||||
if bcrypt.CompareHashAndPassword([]byte(k.Hash), []byte(presented)) == nil {
|
||||
return &k, nil
|
||||
}
|
||||
}
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// keyAllowsService applies the allowed_service_ids JSON allowlist. An empty
|
||||
// list means the key may call every service.
|
||||
func keyAllowsService(k *gatewayKey, serviceID string) bool {
|
||||
var allowed []string
|
||||
if err := json.Unmarshal([]byte(k.AllowedServiceIDs), &allowed); err != nil || len(allowed) == 0 {
|
||||
return true
|
||||
}
|
||||
for _, id := range allowed {
|
||||
if id == serviceID {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// monthlyKeyUsage / monthlyServiceUsage sum a counter dimension for the
|
||||
// current calendar month.
|
||||
func monthlyKeyUsage(ctx context.Context, db *database.DB, id, period string) (int64, error) {
|
||||
keyUUID, _ := uuid.Parse(id)
|
||||
return sqlcdb.New(db.DB).SumUsageByKey(ctx, sqlcdb.SumUsageByKeyParams{
|
||||
ApiKeyID: keyUUID, PeriodMonth: period,
|
||||
})
|
||||
}
|
||||
|
||||
func monthlyServiceUsage(ctx context.Context, db *database.DB, id, period string) (int64, error) {
|
||||
svcUUID, _ := uuid.Parse(id)
|
||||
return sqlcdb.New(db.DB).SumUsageByService(ctx, sqlcdb.SumUsageByServiceParams{
|
||||
ServiceID: svcUUID, PeriodMonth: period,
|
||||
})
|
||||
}
|
||||
|
||||
// recordGatewayRequest persists usage, metrics, and failure incidents for a
|
||||
// proxied call. Runs in the request path but is three small writes.
|
||||
func recordGatewayRequest(ctx context.Context, db *database.DB, svc *gatewayService, k *gatewayKey, status int, period string) {
|
||||
q := sqlcdb.New(db.DB)
|
||||
keyUUID, _ := uuid.Parse(k.ID)
|
||||
svcUUID, _ := uuid.Parse(svc.ID)
|
||||
_ = q.UpsertUsageCounter(ctx, sqlcdb.UpsertUsageCounterParams{
|
||||
ApiKeyID: keyUUID, ServiceID: svcUUID, PeriodMonth: period,
|
||||
})
|
||||
_ = q.TouchAPKeyLastUsed(ctx, keyUUID)
|
||||
labels, _ := json.Marshal(map[string]interface{}{
|
||||
"service_id": svc.ID,
|
||||
"key_id": k.ID,
|
||||
"status": status,
|
||||
})
|
||||
_ = q.InsertMetricPoint(ctx, sql.NullString{String: string(labels), Valid: true})
|
||||
}
|
||||
|
||||
func recordGatewayIncident(ctx context.Context, db *database.DB, svcID string, keyID *string, code, msg, severity string, status int) {
|
||||
var kid uuid.NullUUID
|
||||
if keyID != nil {
|
||||
if u, err := uuid.Parse(*keyID); err == nil {
|
||||
kid = uuid.NullUUID{UUID: u, Valid: true}
|
||||
}
|
||||
}
|
||||
svcUUID, _ := uuid.Parse(svcID)
|
||||
_ = sqlcdb.New(db.DB).InsertGatewayIncident(ctx, sqlcdb.InsertGatewayIncidentParams{
|
||||
ServiceID: uuid.NullUUID{UUID: svcUUID, Valid: svcUUID != uuid.Nil},
|
||||
ApiKeyID: kid,
|
||||
Code: code,
|
||||
Message: msg,
|
||||
Severity: severity,
|
||||
HttpStatus: sql.NullInt32{Int32: int32(status), Valid: true},
|
||||
})
|
||||
}
|
||||
|
||||
// handleGatewayProxy is mounted at /g/:slug and /g/:slug/*path on the root
|
||||
// router — outside /api/v1 so route_prefix stays clean.
|
||||
func handleGatewayProxy(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
ctx := c.Request.Context()
|
||||
slug := c.Param("slug")
|
||||
|
||||
svc, err := lookupGatewayService(ctx, db, slug)
|
||||
if err == sql.ErrNoRows {
|
||||
respondError(c, http.StatusNotFound, "SERVICE_NOT_FOUND", "Gateway service not found")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to resolve gateway service")
|
||||
return
|
||||
}
|
||||
if !svc.Enabled {
|
||||
respondError(c, http.StatusServiceUnavailable, "SERVICE_DISABLED", "Gateway service is disabled")
|
||||
return
|
||||
}
|
||||
|
||||
presented := extractGatewayKey(c)
|
||||
if presented == "" {
|
||||
recordGatewayIncident(ctx, db, svc.ID, nil, "MISSING_API_KEY", "Request without API key", "low", http.StatusUnauthorized)
|
||||
respondError(c, http.StatusUnauthorized, "API_KEY_REQUIRED", "An API key is required for this service")
|
||||
return
|
||||
}
|
||||
key, err := authenticateGatewayKey(ctx, db, presented)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to authenticate API key")
|
||||
return
|
||||
}
|
||||
// Disabled and unknown keys collapse to the same 401 — a disabled key
|
||||
// must not leak its existence.
|
||||
if key == nil || !key.Enabled {
|
||||
recordGatewayIncident(ctx, db, svc.ID, nil, "INVALID_API_KEY", "Request with invalid or disabled API key", "low", http.StatusUnauthorized)
|
||||
respondError(c, http.StatusUnauthorized, "INVALID_API_KEY", "Invalid API key")
|
||||
return
|
||||
}
|
||||
if !keyAllowsService(key, svc.ID) {
|
||||
recordGatewayIncident(ctx, db, svc.ID, &key.ID, "KEY_SCOPE_DENIED", "API key not allowed on this service", "medium", http.StatusForbidden)
|
||||
respondError(c, http.StatusForbidden, "KEY_SCOPE_DENIED", "API key is not permitted for this service")
|
||||
return
|
||||
}
|
||||
|
||||
now := time.Now()
|
||||
if !gatewayRateAllow("key:"+key.ID, int(key.RPMLimit.Int64), now) ||
|
||||
!gatewayRateAllow("svc:"+svc.ID, int(svc.RPMLimit.Int64), now) {
|
||||
recordGatewayIncident(ctx, db, svc.ID, &key.ID, "RATE_LIMITED", "Per-minute rate limit exceeded", "medium", http.StatusTooManyRequests)
|
||||
respondError(c, http.StatusTooManyRequests, "RATE_LIMITED", "Rate limit exceeded")
|
||||
return
|
||||
}
|
||||
|
||||
period := now.Format("2006-01")
|
||||
if key.MonthlyQuota.Valid && key.MonthlyQuota.Int64 > 0 {
|
||||
if used, err := monthlyKeyUsage(ctx, db, key.ID, period); err == nil && used >= key.MonthlyQuota.Int64 {
|
||||
respondError(c, http.StatusTooManyRequests, "QUOTA_EXCEEDED", "Monthly request quota exceeded")
|
||||
return
|
||||
}
|
||||
}
|
||||
if svc.MonthlyQuota.Valid && svc.MonthlyQuota.Int64 > 0 {
|
||||
if used, err := monthlyServiceUsage(ctx, db, svc.ID, period); err == nil && used >= svc.MonthlyQuota.Int64 {
|
||||
respondError(c, http.StatusTooManyRequests, "QUOTA_EXCEEDED", "Service monthly quota exceeded")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
target, err := gateway.BuildTargetURL(svc.UpstreamURL, svc.RoutePrefix, c.Request.URL.Path, c.Request.URL.RawQuery)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadGateway, "UPSTREAM_URL_INVALID", "Gateway upstream is misconfigured")
|
||||
return
|
||||
}
|
||||
headers := map[string]string{}
|
||||
if svc.UpstreamAuthHeader.Valid && svc.UpstreamAuthHeader.String != "" {
|
||||
headers[svc.UpstreamAuthHeader.String] = svc.UpstreamAuthValue.String
|
||||
}
|
||||
|
||||
timeout := time.Duration(svc.RequestTimeoutMS) * time.Millisecond
|
||||
if timeout <= 0 {
|
||||
timeout = 8 * time.Second
|
||||
}
|
||||
reqCtx, cancel := context.WithTimeout(ctx, timeout)
|
||||
defer cancel()
|
||||
|
||||
status, _, err := gateway.ProxyRequest(gatewayHTTPClient, c.Writer, c.Request.WithContext(reqCtx), target, headers)
|
||||
if err != nil {
|
||||
recordGatewayIncident(reqCtx, db, svc.ID, &key.ID, "UPSTREAM_ERROR", err.Error(), "high", http.StatusBadGateway)
|
||||
recordGatewayRequest(ctx, db, svc, key, http.StatusBadGateway, period)
|
||||
respondError(c, http.StatusBadGateway, "UPSTREAM_ERROR", "Upstream request failed")
|
||||
return
|
||||
}
|
||||
|
||||
recordGatewayRequest(ctx, db, svc, key, status, period)
|
||||
if status >= 500 {
|
||||
recordGatewayIncident(ctx, db, svc.ID, &key.ID, "UPSTREAM_5XX", "Upstream returned "+http.StatusText(status), "high", status)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func TestGatewayRateAllow(t *testing.T) {
|
||||
now := time.Date(2026, 10, 15, 12, 0, 30, 0, time.UTC)
|
||||
|
||||
for i := 0; i < 3; i++ {
|
||||
if !gatewayRateAllow("test:a", 3, now) {
|
||||
t.Fatalf("call %d within limit rejected", i+1)
|
||||
}
|
||||
}
|
||||
if gatewayRateAllow("test:a", 3, now) {
|
||||
t.Fatal("call beyond limit allowed")
|
||||
}
|
||||
if !gatewayRateAllow("test:a", 3, now.Add(time.Minute)) {
|
||||
t.Fatal("new minute window still rejected")
|
||||
}
|
||||
if !gatewayRateAllow("test:unlimited", 0, now) {
|
||||
t.Fatal("non-positive limit should disable the check")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractGatewayKey(t *testing.T) {
|
||||
mkctx := func(headers map[string]string) *gin.Context {
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("GET", "/g/x", nil)
|
||||
for k, v := range headers {
|
||||
c.Request.Header.Set(k, v)
|
||||
}
|
||||
return c
|
||||
}
|
||||
|
||||
if got := extractGatewayKey(mkctx(map[string]string{"X-API-Key": "ap_abc"})); got != "ap_abc" {
|
||||
t.Fatalf("X-API-Key not extracted: %q", got)
|
||||
}
|
||||
if got := extractGatewayKey(mkctx(map[string]string{"Authorization": "Bearer ap_def"})); got != "ap_def" {
|
||||
t.Fatalf("Bearer not extracted: %q", got)
|
||||
}
|
||||
// X-API-Key wins when both are present.
|
||||
if got := extractGatewayKey(mkctx(map[string]string{
|
||||
"X-API-Key": "ap_first",
|
||||
"Authorization": "Bearer ap_second",
|
||||
})); got != "ap_first" {
|
||||
t.Fatalf("X-API-Key should take precedence: %q", got)
|
||||
}
|
||||
if got := extractGatewayKey(mkctx(map[string]string{})); got != "" {
|
||||
t.Fatalf("expected empty key, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestKeyAllowsService(t *testing.T) {
|
||||
svcID := "11111111-2222-3333-4444-555555555555"
|
||||
other := "aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"
|
||||
|
||||
cases := []struct {
|
||||
name string
|
||||
allowed string
|
||||
want bool
|
||||
}{
|
||||
{"empty list permits all", "[]", true},
|
||||
{"blank permits all", "", true},
|
||||
{"invalid json permits all", "not-json", true},
|
||||
{"listed service allowed", `["` + svcID + `"]`, true},
|
||||
{"unlisted service denied", `["` + other + `"]`, false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
k := &gatewayKey{AllowedServiceIDs: tc.allowed}
|
||||
if got := keyAllowsService(k, svcID); got != tc.want {
|
||||
t.Fatalf("keyAllowsService(%q) = %v, want %v", tc.allowed, got, tc.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
+202
-224
@@ -2,6 +2,8 @@ package api
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"context"
|
||||
"crypto/rand"
|
||||
"crypto/rsa"
|
||||
"crypto/x509"
|
||||
@@ -92,48 +94,82 @@ type CreateWebhookRequest struct {
|
||||
Branch string `json:"branch"`
|
||||
}
|
||||
|
||||
func gitRepoFrom(r sqlcdb.ListGitRepositoriesRow) GitRepository {
|
||||
return GitRepository{
|
||||
ID: r.ID.String(),
|
||||
ProviderID: r.ProviderID.String(),
|
||||
Name: r.Name,
|
||||
FullName: r.FullName,
|
||||
Description: r.Description,
|
||||
CloneURL: r.CloneUrl,
|
||||
WebhookURL: r.WebhookUrl,
|
||||
DefaultBranch: r.DefaultBranch.String,
|
||||
IsPrivate: r.IsPrivate.Bool,
|
||||
UserID: r.UserID.String(),
|
||||
CreatedAt: r.CreatedAt.Time,
|
||||
UpdatedAt: r.UpdatedAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
func gitRepoFromFullNameRow(r sqlcdb.GetGitRepositoryByFullNameRow) GitRepository {
|
||||
return GitRepository{
|
||||
ID: r.ID.String(),
|
||||
ProviderID: r.ProviderID.String(),
|
||||
Name: r.Name,
|
||||
FullName: r.FullName,
|
||||
Description: r.Description,
|
||||
CloneURL: r.CloneUrl,
|
||||
WebhookURL: r.WebhookUrl,
|
||||
DefaultBranch: r.DefaultBranch.String,
|
||||
IsPrivate: r.IsPrivate.Bool,
|
||||
UserID: r.UserID.String(),
|
||||
CreatedAt: r.CreatedAt.Time,
|
||||
UpdatedAt: r.UpdatedAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
func gitProviderFrom(p sqlcdb.ListGitProvidersRow) GitProvider {
|
||||
return GitProvider{
|
||||
ID: p.ID.String(),
|
||||
Name: p.Name,
|
||||
DisplayName: p.DisplayName,
|
||||
APIUrl: p.ApiUrl,
|
||||
WebhookUrl: p.WebhookUrl,
|
||||
UserID: p.UserID.String(),
|
||||
CreatedAt: p.CreatedAt.Time,
|
||||
UpdatedAt: p.UpdatedAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
func handleGetGitProviders(c *gin.Context) {
|
||||
ctx, ok := requireGitRequestContext(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := ctx.db.Query(`
|
||||
SELECT id, name, display_name, api_url, webhook_url, user_id, created_at, updated_at
|
||||
FROM git_providers
|
||||
WHERE user_id = $1
|
||||
ORDER BY created_at DESC
|
||||
`, ctx.userID)
|
||||
userID, err := uuid.Parse(ctx.userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
rows, err := sqlcdb.New(ctx.db.DB).ListGitProviders(context.Background(), userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
providers := make([]GitProvider, 0)
|
||||
for rows.Next() {
|
||||
var provider GitProvider
|
||||
if err := rows.Scan(
|
||||
&provider.ID,
|
||||
&provider.Name,
|
||||
&provider.DisplayName,
|
||||
&provider.APIUrl,
|
||||
&provider.WebhookUrl,
|
||||
&provider.UserID,
|
||||
&provider.CreatedAt,
|
||||
&provider.UpdatedAt,
|
||||
); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
providers = append(providers, provider)
|
||||
providers := make([]GitProvider, 0, len(rows))
|
||||
for _, p := range rows {
|
||||
providers = append(providers, gitProviderFrom(p))
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"providers": providers})
|
||||
}
|
||||
|
||||
func handleGetGitHubAppInstallURL(c *gin.Context) {
|
||||
slug := strings.TrimSpace(os.Getenv("GITHUB_APP_SLUG"))
|
||||
db, _ := c.Get("db")
|
||||
d, _ := db.(*database.DB)
|
||||
slug := strings.TrimSpace(settingValue(d, settingGitHubAppSlug, "GITHUB_APP_SLUG", ""))
|
||||
if slug == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "GITHUB_APP_SLUG is not configured"})
|
||||
return
|
||||
@@ -171,21 +207,22 @@ func handleConnectGitHubApp(c *gin.Context) {
|
||||
UserID: ctx.userID,
|
||||
}
|
||||
|
||||
if err := ctx.db.QueryRow(`
|
||||
INSERT INTO git_providers (id, name, display_name, api_url, webhook_url, access_token, user_id, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), NOW())
|
||||
ON CONFLICT (name, user_id)
|
||||
DO UPDATE SET
|
||||
display_name = EXCLUDED.display_name,
|
||||
api_url = EXCLUDED.api_url,
|
||||
webhook_url = EXCLUDED.webhook_url,
|
||||
access_token = EXCLUDED.access_token,
|
||||
updated_at = NOW()
|
||||
RETURNING id, created_at, updated_at
|
||||
`, provider.ID, provider.Name, provider.DisplayName, provider.APIUrl, provider.WebhookUrl, provider.AccessToken, provider.UserID).Scan(&provider.ID, &provider.CreatedAt, &provider.UpdatedAt); err != nil {
|
||||
prow, err := sqlcdb.New(ctx.db.DB).UpsertGitHubAppProvider(context.Background(), sqlcdb.UpsertGitHubAppProviderParams{
|
||||
ID: uuid.MustParse(provider.ID),
|
||||
Name: provider.Name,
|
||||
DisplayName: provider.DisplayName,
|
||||
ApiUrl: provider.APIUrl,
|
||||
WebhookUrl: provider.WebhookUrl,
|
||||
AccessToken: provider.AccessToken,
|
||||
UserID: uuid.MustParse(provider.UserID),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to connect GitHub App installation"})
|
||||
return
|
||||
}
|
||||
provider.ID = prow.ID.String()
|
||||
provider.CreatedAt = prow.CreatedAt.Time
|
||||
provider.UpdatedAt = prow.UpdatedAt.Time
|
||||
|
||||
provider.AccessToken = ""
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
@@ -257,10 +294,16 @@ func handleCreateGitProvider(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := ctx.db.Exec(`
|
||||
INSERT INTO git_providers (id, name, display_name, api_url, webhook_url, access_token, user_id, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), NOW())
|
||||
`, provider.ID, provider.Name, provider.DisplayName, provider.APIUrl, provider.WebhookUrl, provider.AccessToken, provider.UserID); err != nil {
|
||||
q := sqlcdb.New(ctx.db.DB)
|
||||
if err := q.InsertGitProvider(context.Background(), sqlcdb.InsertGitProviderParams{
|
||||
ID: uuid.MustParse(provider.ID),
|
||||
Name: provider.Name,
|
||||
DisplayName: provider.DisplayName,
|
||||
ApiUrl: provider.APIUrl,
|
||||
WebhookUrl: provider.WebhookUrl,
|
||||
AccessToken: provider.AccessToken,
|
||||
UserID: uuid.MustParse(provider.UserID),
|
||||
}); err != nil {
|
||||
if isUniqueConstraintError(err) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Provider already connected for this account"})
|
||||
return
|
||||
@@ -269,13 +312,12 @@ func handleCreateGitProvider(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if err := ctx.db.QueryRow(`
|
||||
SELECT created_at, updated_at
|
||||
FROM git_providers
|
||||
WHERE id = $1
|
||||
`, provider.ID).Scan(&provider.CreatedAt, &provider.UpdatedAt); err != nil {
|
||||
if ts, err := q.GetGitProviderTimestamps(context.Background(), uuid.MustParse(provider.ID)); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to read created provider"})
|
||||
return
|
||||
} else {
|
||||
provider.CreatedAt = ts.CreatedAt.Time
|
||||
provider.UpdatedAt = ts.UpdatedAt.Time
|
||||
}
|
||||
|
||||
provider.AccessToken = ""
|
||||
@@ -294,12 +336,11 @@ func handleGetGitRepositories(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var providerName, providerDisplayName, providerAccessToken, providerAPIURL string
|
||||
err := ctx.db.QueryRow(`
|
||||
SELECT name, display_name, access_token, api_url
|
||||
FROM git_providers
|
||||
WHERE id = $1 AND user_id = $2
|
||||
`, providerID, ctx.userID).Scan(&providerName, &providerDisplayName, &providerAccessToken, &providerAPIURL)
|
||||
q := sqlcdb.New(ctx.db.DB)
|
||||
provRow, err := q.GetGitProviderForRepos(context.Background(), sqlcdb.GetGitProviderForReposParams{
|
||||
ID: uuid.MustParse(providerID),
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Provider not found"})
|
||||
@@ -308,6 +349,10 @@ func handleGetGitRepositories(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
providerName := provRow.Name
|
||||
providerDisplayName := provRow.DisplayName
|
||||
providerAccessToken := provRow.AccessToken
|
||||
providerAPIURL := provRow.ApiUrl
|
||||
|
||||
if c.DefaultQuery("sync", "1") != "0" {
|
||||
if syncErr := syncProviderRepositories(ctx.db, providerID, ctx.userID, providerName, providerAPIURL, providerAccessToken); syncErr != nil {
|
||||
@@ -322,53 +367,30 @@ func handleGetGitRepositories(c *gin.Context) {
|
||||
}
|
||||
offset := (page - 1) * limit
|
||||
search := strings.TrimSpace(c.Query("search"))
|
||||
searchNullable := sql.NullString{String: search, Valid: search != ""}
|
||||
|
||||
rows, err := ctx.db.Query(`
|
||||
SELECT id, provider_id, name, full_name, COALESCE(description, ''), clone_url, COALESCE(webhook_url, ''),
|
||||
default_branch, is_private, user_id, created_at, updated_at
|
||||
FROM git_repositories
|
||||
WHERE provider_id = $1 AND user_id = $2
|
||||
AND ($3::text IS NULL OR full_name ILIKE ('%' || $3::text || '%') OR name ILIKE ('%' || $3::text || '%'))
|
||||
ORDER BY updated_at DESC
|
||||
LIMIT $4 OFFSET $5
|
||||
`, providerID, ctx.userID, searchNullable, limit, offset)
|
||||
repoRows, err := q.ListGitRepositories(context.Background(), sqlcdb.ListGitRepositoriesParams{
|
||||
ProviderID: uuid.MustParse(providerID),
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
Column3: search,
|
||||
Limit: int32(limit),
|
||||
Offset: int32(offset),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
repositories := make([]GitRepository, 0)
|
||||
for rows.Next() {
|
||||
var repo GitRepository
|
||||
if err := rows.Scan(
|
||||
&repo.ID,
|
||||
&repo.ProviderID,
|
||||
&repo.Name,
|
||||
&repo.FullName,
|
||||
&repo.Description,
|
||||
&repo.CloneURL,
|
||||
&repo.WebhookURL,
|
||||
&repo.DefaultBranch,
|
||||
&repo.IsPrivate,
|
||||
&repo.UserID,
|
||||
&repo.CreatedAt,
|
||||
&repo.UpdatedAt,
|
||||
); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
repositories = append(repositories, repo)
|
||||
repositories := make([]GitRepository, 0, len(repoRows))
|
||||
for _, r := range repoRows {
|
||||
repositories = append(repositories, gitRepoFrom(r))
|
||||
}
|
||||
|
||||
var total int
|
||||
if err := ctx.db.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM git_repositories
|
||||
WHERE provider_id = $1 AND user_id = $2
|
||||
AND ($3::text IS NULL OR full_name ILIKE ('%' || $3::text || '%') OR name ILIKE ('%' || $3::text || '%'))
|
||||
`, providerID, ctx.userID, searchNullable).Scan(&total); err != nil {
|
||||
total, err := q.CountGitRepositories(context.Background(), sqlcdb.CountGitRepositoriesParams{
|
||||
ProviderID: uuid.MustParse(providerID),
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
Column3: search,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
@@ -406,12 +428,11 @@ func handleConnectGitRepository(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var providerName string
|
||||
err := ctx.db.QueryRow(`
|
||||
SELECT name
|
||||
FROM git_providers
|
||||
WHERE id = $1 AND user_id = $2
|
||||
`, providerID, ctx.userID).Scan(&providerName)
|
||||
q := sqlcdb.New(ctx.db.DB)
|
||||
providerName, err := q.GetGitProviderName(context.Background(), sqlcdb.GetGitProviderNameParams{
|
||||
ID: uuid.MustParse(providerID),
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Provider not found"})
|
||||
@@ -427,31 +448,16 @@ func handleConnectGitRepository(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var existing GitRepository
|
||||
err = ctx.db.QueryRow(`
|
||||
SELECT id, provider_id, name, full_name, COALESCE(description, ''), clone_url, COALESCE(webhook_url, ''),
|
||||
default_branch, is_private, user_id, created_at, updated_at
|
||||
FROM git_repositories
|
||||
WHERE provider_id = $1 AND full_name = $2 AND user_id = $3
|
||||
`, providerID, fullName, ctx.userID).Scan(
|
||||
&existing.ID,
|
||||
&existing.ProviderID,
|
||||
&existing.Name,
|
||||
&existing.FullName,
|
||||
&existing.Description,
|
||||
&existing.CloneURL,
|
||||
&existing.WebhookURL,
|
||||
&existing.DefaultBranch,
|
||||
&existing.IsPrivate,
|
||||
&existing.UserID,
|
||||
&existing.CreatedAt,
|
||||
&existing.UpdatedAt,
|
||||
)
|
||||
existingRow, err := q.GetGitRepositoryByFullName(context.Background(), sqlcdb.GetGitRepositoryByFullNameParams{
|
||||
ProviderID: uuid.MustParse(providerID),
|
||||
FullName: fullName,
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
})
|
||||
switch {
|
||||
case err == nil:
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"message": "Repository already connected",
|
||||
"repository": existing,
|
||||
"repository": gitRepoFromFullNameRow(existingRow),
|
||||
})
|
||||
return
|
||||
case !errors.Is(err, sql.ErrNoRows):
|
||||
@@ -471,13 +477,18 @@ func handleConnectGitRepository(c *gin.Context) {
|
||||
UserID: ctx.userID,
|
||||
}
|
||||
|
||||
if err := ctx.db.QueryRow(`
|
||||
INSERT INTO git_repositories (
|
||||
id, provider_id, name, full_name, description, clone_url, default_branch, is_private, user_id, created_at, updated_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW())
|
||||
RETURNING created_at, updated_at
|
||||
`, repo.ID, repo.ProviderID, repo.Name, repo.FullName, repo.Description, repo.CloneURL, repo.DefaultBranch, repo.IsPrivate, repo.UserID).Scan(&repo.CreatedAt, &repo.UpdatedAt); err != nil {
|
||||
ins, err := q.InsertGitRepository(context.Background(), sqlcdb.InsertGitRepositoryParams{
|
||||
ID: uuid.MustParse(repo.ID),
|
||||
ProviderID: uuid.MustParse(repo.ProviderID),
|
||||
Name: repo.Name,
|
||||
FullName: repo.FullName,
|
||||
Description: sql.NullString{String: repo.Description, Valid: true},
|
||||
CloneUrl: repo.CloneURL,
|
||||
DefaultBranch: sql.NullString{String: repo.DefaultBranch, Valid: true},
|
||||
IsPrivate: sql.NullBool{Bool: repo.IsPrivate, Valid: true},
|
||||
UserID: uuid.MustParse(repo.UserID),
|
||||
})
|
||||
if err != nil {
|
||||
if isUniqueConstraintError(err) {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Repository is already connected"})
|
||||
return
|
||||
@@ -485,6 +496,8 @@ func handleConnectGitRepository(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to connect repository"})
|
||||
return
|
||||
}
|
||||
repo.CreatedAt = ins.CreatedAt.Time
|
||||
repo.UpdatedAt = ins.UpdatedAt.Time
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"message": "Repository connected successfully",
|
||||
@@ -514,12 +527,10 @@ func handleCreateWebhook(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var providerID string
|
||||
err := ctx.db.QueryRow(`
|
||||
SELECT provider_id
|
||||
FROM git_repositories
|
||||
WHERE id = $1 AND user_id = $2
|
||||
`, repoID, ctx.userID).Scan(&providerID)
|
||||
providerUUID, err := sqlcdb.New(ctx.db.DB).GetGitRepositoryProviderID(context.Background(), sqlcdb.GetGitRepositoryProviderIDParams{
|
||||
ID: uuid.MustParse(repoID),
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Repository not found"})
|
||||
@@ -528,6 +539,7 @@ func handleCreateWebhook(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
providerID := providerUUID.String()
|
||||
|
||||
eventsJSON, err := json.Marshal(req.Events)
|
||||
if err != nil {
|
||||
@@ -550,23 +562,22 @@ func handleCreateWebhook(c *gin.Context) {
|
||||
Active: true,
|
||||
}
|
||||
|
||||
if err := ctx.db.QueryRow(`
|
||||
INSERT INTO git_webhooks (
|
||||
id, repo_id, provider_id, events, webhook_secret, active, branch_filter, created_at, updated_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, TRUE, $6, NOW(), NOW())
|
||||
ON CONFLICT (repo_id, provider_id)
|
||||
DO UPDATE SET
|
||||
events = EXCLUDED.events,
|
||||
webhook_secret = EXCLUDED.webhook_secret,
|
||||
active = TRUE,
|
||||
branch_filter = EXCLUDED.branch_filter,
|
||||
updated_at = NOW()
|
||||
RETURNING id, active, created_at, updated_at
|
||||
`, webhook.ID, webhook.RepoID, webhook.ProviderID, webhook.Events, webhook.Secret, strings.TrimSpace(req.Branch)).Scan(&webhook.ID, &webhook.Active, &webhook.CreatedAt, &webhook.UpdatedAt); err != nil {
|
||||
wrow, err := sqlcdb.New(ctx.db.DB).UpsertGitWebhook(context.Background(), sqlcdb.UpsertGitWebhookParams{
|
||||
ID: uuid.MustParse(webhook.ID),
|
||||
RepoID: uuid.MustParse(webhook.RepoID),
|
||||
ProviderID: uuid.MustParse(webhook.ProviderID),
|
||||
Events: webhook.Events,
|
||||
WebhookSecret: webhook.Secret,
|
||||
BranchFilter: sql.NullString{String: strings.TrimSpace(req.Branch), Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to configure webhook"})
|
||||
return
|
||||
}
|
||||
webhook.ID = wrow.ID.String()
|
||||
webhook.Active = wrow.Active.Bool
|
||||
webhook.CreatedAt = wrow.CreatedAt.Time
|
||||
webhook.UpdatedAt = wrow.UpdatedAt.Time
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"message": "Webhook configured successfully",
|
||||
@@ -596,69 +607,39 @@ func handleGetConnectedRepositories(c *gin.Context) {
|
||||
}
|
||||
offset := (page - 1) * limit
|
||||
|
||||
rows, err := ctx.db.Query(`
|
||||
SELECT r.id, r.provider_id, r.name, r.full_name, COALESCE(r.description, ''), r.clone_url,
|
||||
r.default_branch, r.is_private, r.user_id, r.created_at, r.updated_at,
|
||||
p.name as provider_name, p.display_name
|
||||
FROM git_repositories r
|
||||
JOIN git_providers p ON r.provider_id = p.id
|
||||
WHERE r.user_id = $1
|
||||
ORDER BY r.updated_at DESC
|
||||
LIMIT $2 OFFSET $3
|
||||
`, ctx.userID, limit, offset)
|
||||
q := sqlcdb.New(ctx.db.DB)
|
||||
repoRows, err := q.ListConnectedRepositories(context.Background(), sqlcdb.ListConnectedRepositoriesParams{
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
Limit: int32(limit),
|
||||
Offset: int32(offset),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
repositories := make([]map[string]interface{}, 0)
|
||||
for rows.Next() {
|
||||
var repo GitRepository
|
||||
var providerName, providerDisplayName string
|
||||
if err := rows.Scan(
|
||||
&repo.ID,
|
||||
&repo.ProviderID,
|
||||
&repo.Name,
|
||||
&repo.FullName,
|
||||
&repo.Description,
|
||||
&repo.CloneURL,
|
||||
&repo.DefaultBranch,
|
||||
&repo.IsPrivate,
|
||||
&repo.UserID,
|
||||
&repo.CreatedAt,
|
||||
&repo.UpdatedAt,
|
||||
&providerName,
|
||||
&providerDisplayName,
|
||||
); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
|
||||
repositories := make([]map[string]interface{}, 0, len(repoRows))
|
||||
for _, r := range repoRows {
|
||||
repositories = append(repositories, map[string]interface{}{
|
||||
"id": repo.ID,
|
||||
"provider_id": repo.ProviderID,
|
||||
"name": repo.Name,
|
||||
"full_name": repo.FullName,
|
||||
"description": repo.Description,
|
||||
"clone_url": repo.CloneURL,
|
||||
"default_branch": repo.DefaultBranch,
|
||||
"is_private": repo.IsPrivate,
|
||||
"created_at": repo.CreatedAt,
|
||||
"updated_at": repo.UpdatedAt,
|
||||
"id": r.ID.String(),
|
||||
"provider_id": r.ProviderID.String(),
|
||||
"name": r.Name,
|
||||
"full_name": r.FullName,
|
||||
"description": r.Description,
|
||||
"clone_url": r.CloneUrl,
|
||||
"default_branch": r.DefaultBranch.String,
|
||||
"is_private": r.IsPrivate.Bool,
|
||||
"created_at": r.CreatedAt.Time,
|
||||
"updated_at": r.UpdatedAt.Time,
|
||||
"provider": map[string]string{
|
||||
"name": providerName,
|
||||
"display_name": providerDisplayName,
|
||||
"name": r.ProviderName,
|
||||
"display_name": r.DisplayName,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
var total int
|
||||
if err := ctx.db.QueryRow(`
|
||||
SELECT COUNT(*)
|
||||
FROM git_repositories
|
||||
WHERE user_id = $1
|
||||
`, ctx.userID).Scan(&total); err != nil {
|
||||
total, err := q.CountUserRepositories(context.Background(), uuid.MustParse(ctx.userID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
@@ -703,20 +684,17 @@ func syncProviderRepositories(db *database.DB, providerID, userID, providerName,
|
||||
defaultBranch = "main"
|
||||
}
|
||||
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO git_repositories (
|
||||
id, provider_id, name, full_name, description, clone_url, default_branch, is_private, user_id, created_at, updated_at
|
||||
)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, NOW(), NOW())
|
||||
ON CONFLICT (provider_id, full_name)
|
||||
DO UPDATE SET
|
||||
name = EXCLUDED.name,
|
||||
description = EXCLUDED.description,
|
||||
clone_url = EXCLUDED.clone_url,
|
||||
default_branch = EXCLUDED.default_branch,
|
||||
is_private = EXCLUDED.is_private,
|
||||
updated_at = NOW()
|
||||
`, uuid.NewString(), providerID, repository.Name, repository.FullName, repository.Description, cloneURL, defaultBranch, repository.IsPrivate, userID)
|
||||
err := sqlcdb.New(db.DB).UpsertGitRepository(context.Background(), sqlcdb.UpsertGitRepositoryParams{
|
||||
ID: uuid.New(),
|
||||
ProviderID: uuid.MustParse(providerID),
|
||||
Name: repository.Name,
|
||||
FullName: repository.FullName,
|
||||
Description: sql.NullString{String: repository.Description, Valid: true},
|
||||
CloneUrl: cloneURL,
|
||||
DefaultBranch: sql.NullString{String: defaultBranch, Valid: true},
|
||||
IsPrivate: sql.NullBool{Bool: repository.IsPrivate, Valid: true},
|
||||
UserID: uuid.MustParse(userID),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@@ -1073,8 +1051,8 @@ func fetchBitbucketRepositories(providerAPIURL, accessToken string) ([]remoteGit
|
||||
}
|
||||
|
||||
func getGitHubAppInstallationToken(baseURL string, installationID int64) (string, error) {
|
||||
appID := strings.TrimSpace(os.Getenv("GITHUB_APP_ID"))
|
||||
privateKeyPEM := strings.TrimSpace(os.Getenv("GITHUB_APP_PRIVATE_KEY"))
|
||||
appID := platformSetting(settingGitHubAppID, "GITHUB_APP_ID")
|
||||
privateKeyPEM := platformSetting(settingGitHubAppPrivateKey, "GITHUB_APP_PRIVATE_KEY")
|
||||
if appID == "" || privateKeyPEM == "" {
|
||||
return "", fmt.Errorf("GITHUB_APP_ID and GITHUB_APP_PRIVATE_KEY must be configured")
|
||||
}
|
||||
@@ -1321,16 +1299,15 @@ func handleDeleteGitProvider(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
result, err := ctx.db.Exec(
|
||||
"DELETE FROM git_providers WHERE id = $1 AND user_id = $2",
|
||||
providerID, ctx.userID,
|
||||
)
|
||||
affected, err := sqlcdb.New(ctx.db.DB).DeleteGitProvider(context.Background(), sqlcdb.DeleteGitProviderParams{
|
||||
ID: uuid.MustParse(providerID),
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete provider"})
|
||||
return
|
||||
}
|
||||
|
||||
affected, _ := result.RowsAffected()
|
||||
if affected == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Provider not found"})
|
||||
return
|
||||
@@ -1360,12 +1337,10 @@ func handleGetGitRepositoryBranches(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var providerName, providerAPIURL, providerAccessToken string
|
||||
err := ctx.db.QueryRow(`
|
||||
SELECT name, api_url, access_token
|
||||
FROM git_providers
|
||||
WHERE id = $1 AND user_id = $2
|
||||
`, providerID, ctx.userID).Scan(&providerName, &providerAPIURL, &providerAccessToken)
|
||||
pf, err := sqlcdb.New(ctx.db.DB).GetGitProviderForFetch(context.Background(), sqlcdb.GetGitProviderForFetchParams{
|
||||
ID: uuid.MustParse(providerID),
|
||||
UserID: uuid.MustParse(ctx.userID),
|
||||
})
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Provider not found"})
|
||||
@@ -1374,6 +1349,9 @@ func handleGetGitRepositoryBranches(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database error"})
|
||||
return
|
||||
}
|
||||
providerName := pf.Name
|
||||
providerAPIURL := pf.ApiUrl
|
||||
providerAccessToken := pf.AccessToken
|
||||
|
||||
branches, err := fetchRepositoryBranches(providerName, providerAPIURL, providerAccessToken, owner, repo)
|
||||
if err != nil {
|
||||
|
||||
@@ -5,15 +5,19 @@ import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/deployqueue"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
@@ -36,22 +40,15 @@ func handleGitWebhookPush(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var (
|
||||
secret string
|
||||
repoID string
|
||||
providerID string
|
||||
branchFilter string
|
||||
active bool
|
||||
)
|
||||
err := db.QueryRow(`
|
||||
SELECT webhook_secret, repo_id, provider_id, COALESCE(branch_filter, ''), active
|
||||
FROM git_webhooks WHERE id = $1`, webhookID).
|
||||
Scan(&secret, &repoID, &providerID, &branchFilter, &active)
|
||||
q := sqlcdb.New(db.DB)
|
||||
hook, err := q.GetGitWebhookForPush(context.Background(), uuid.MustParse(webhookID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Webhook not found"})
|
||||
return
|
||||
}
|
||||
if !active {
|
||||
secret := hook.WebhookSecret
|
||||
branchFilter := hook.BranchFilter
|
||||
if !hook.Active.Bool {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Webhook is disabled"})
|
||||
return
|
||||
}
|
||||
@@ -62,8 +59,7 @@ func handleGitWebhookPush(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var providerName string
|
||||
_ = db.QueryRow(`SELECT name FROM git_providers WHERE id = $1`, providerID).Scan(&providerName)
|
||||
providerName, _ := q.GetGitProviderNameByID(context.Background(), hook.ProviderID)
|
||||
|
||||
if !verifyGitWebhookSignature(providerName, secret, c.Request, body) {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid signature"})
|
||||
@@ -89,11 +85,24 @@ func handleGitWebhookPush(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if !strings.HasPrefix(payload.Ref, "refs/heads/") {
|
||||
// Non-push events that still mean "the repo changed": fork-sync bots
|
||||
// (sync), repository_dispatch, workflow_run completions. They carry no
|
||||
// `ref`, so fall back to the webhook's branch filter (or the service's
|
||||
// own git_branch via the repo match below).
|
||||
event := c.Request.Header.Get("X-GitHub-Event")
|
||||
if event == "" {
|
||||
event = c.Request.Header.Get("X-Gitea-Event")
|
||||
}
|
||||
syncEvent := event == "sync" || event == "repository_dispatch" || event == "workflow_run"
|
||||
|
||||
if !strings.HasPrefix(payload.Ref, "refs/heads/") && !syncEvent {
|
||||
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "not a branch push"})
|
||||
return
|
||||
}
|
||||
branch := strings.TrimPrefix(payload.Ref, "refs/heads/")
|
||||
if branch == "" {
|
||||
branch = branchFilter
|
||||
}
|
||||
if branchFilter != "" && branchFilter != branch {
|
||||
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "branch not watched"})
|
||||
return
|
||||
@@ -103,44 +112,65 @@ func handleGitWebhookPush(c *gin.Context) {
|
||||
commit = payload.After
|
||||
}
|
||||
|
||||
var cloneURL, fullName, repoUserID string
|
||||
err = db.QueryRow(
|
||||
`SELECT clone_url, full_name, user_id FROM git_repositories WHERE id = $1`, repoID,
|
||||
).Scan(&cloneURL, &fullName, &repoUserID)
|
||||
repo, err := q.GetGitRepoForPush(context.Background(), hook.RepoID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Repository not found"})
|
||||
return
|
||||
}
|
||||
cloneURL, fullName, repoUserID := repo.CloneUrl, repo.FullName, repo.UserID.String()
|
||||
|
||||
rows, err := db.Query(`
|
||||
SELECT s.id, s.project_id, s.name, s.type, s.status, s.image, s.command,
|
||||
s.environment, s.git_repo, s.git_branch, s.build_path, s.cpu, s.memory,
|
||||
s.created_at, s.updated_at
|
||||
FROM services s
|
||||
WHERE s.git_branch = $1 AND (s.git_repo = $2 OR s.git_repo = $3)`,
|
||||
branch, cloneURL, fullName)
|
||||
engineValue, _ := c.Get("deployment_engine")
|
||||
engine, _ := engineValue.(*deployment.DeploymentEngine)
|
||||
|
||||
enqueued, err := dispatchPushToServices(c, db, engine, cloneURL, fullName, branch, commit, repoUserID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to match services"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var services []Service
|
||||
for rows.Next() {
|
||||
var s Service
|
||||
if err := rows.Scan(
|
||||
&s.ID, &s.ProjectID, &s.Name, &s.Type, &s.Status, &s.Image, &s.Command,
|
||||
&s.Environment, &s.GitRepo, &s.GitBranch, &s.BuildPath, &s.CPU, &s.Memory,
|
||||
&s.CreatedAt, &s.UpdatedAt,
|
||||
); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan service"})
|
||||
return
|
||||
}
|
||||
services = append(services, s)
|
||||
c.JSON(http.StatusAccepted, gin.H{
|
||||
"received": true,
|
||||
"branch": branch,
|
||||
"deployments": enqueued,
|
||||
})
|
||||
}
|
||||
|
||||
// dispatchPushToServices enqueues a deployment for every service tracking
|
||||
// the pushed repo+branch. Shared by the per-repo webhook receiver and the
|
||||
// GitHub App webhook endpoint.
|
||||
func dispatchPushToServices(c *gin.Context, db *database.DB, engine *deployment.DeploymentEngine, cloneURL, fullName, branch, commit, repoUserID string) (int, error) {
|
||||
q := sqlcdb.New(db.DB)
|
||||
ctx := context.Background()
|
||||
// Sync-style events may carry no branch — empty branch matches all.
|
||||
rows, err := q.ListServicesForPush(ctx, sqlcdb.ListServicesForPushParams{
|
||||
GitBranch: sql.NullString{String: branch, Valid: true},
|
||||
GitRepo: sql.NullString{String: cloneURL, Valid: true},
|
||||
GitRepo_2: sql.NullString{String: fullName, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
|
||||
engineValue, _ := c.Get("deployment_engine")
|
||||
engine, _ := engineValue.(*deployment.DeploymentEngine)
|
||||
var services []Service
|
||||
for _, row := range rows {
|
||||
services = append(services, Service{
|
||||
ID: row.ID,
|
||||
ProjectID: row.ProjectID,
|
||||
Name: row.Name,
|
||||
Type: row.Type,
|
||||
Status: row.Status,
|
||||
Image: row.Image,
|
||||
Command: row.Command,
|
||||
Environment: row.Environment,
|
||||
GitRepo: row.GitRepo,
|
||||
GitBranch: row.GitBranch,
|
||||
BuildPath: row.BuildPath,
|
||||
CPU: row.Cpu,
|
||||
Memory: row.Memory,
|
||||
CreatedAt: row.CreatedAt.Time,
|
||||
UpdatedAt: row.UpdatedAt.Time,
|
||||
})
|
||||
}
|
||||
|
||||
enqueued := 0
|
||||
for _, service := range services {
|
||||
@@ -157,40 +187,62 @@ func handleGitWebhookPush(c *gin.Context) {
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}
|
||||
if _, err := db.Exec(
|
||||
`INSERT INTO deployments
|
||||
(id, service_id, commit_hash, status, image_name, image_tag, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8)`,
|
||||
d.ID, d.ServiceID, d.CommitHash, d.Status, d.ImageName, d.ImageTag, d.CreatedAt, d.UpdatedAt,
|
||||
); err != nil {
|
||||
if err := q.InsertDeployment(ctx, sqlcdb.InsertDeploymentParams{
|
||||
ID: d.ID,
|
||||
ServiceID: d.ServiceID,
|
||||
Version: fmt.Sprintf("v%d", now.Unix()),
|
||||
CommitHash: sql.NullString{String: ptrStr(d.CommitHash), Valid: d.CommitHash != nil},
|
||||
Status: sql.NullString{String: d.Status, Valid: true},
|
||||
ImageName: sql.NullString{String: d.ImageName, Valid: true},
|
||||
ImageTag: sql.NullString{String: d.ImageTag, Valid: true},
|
||||
CreatedAt: sql.NullTime{Time: d.CreatedAt, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: d.UpdatedAt, Valid: true},
|
||||
}); err != nil {
|
||||
continue
|
||||
}
|
||||
if engine == nil {
|
||||
failedAt := time.Now()
|
||||
failure := "Deployment engine unavailable. Docker may not be configured on this server."
|
||||
_, _ = db.Exec(
|
||||
`UPDATE deployments SET status = 'failed', error = $1, completed_at = $2, updated_at = $2 WHERE id = $3`,
|
||||
failure, failedAt, d.ID,
|
||||
)
|
||||
failedAt := time.Now()
|
||||
_ = q.FailDeployment(ctx, sqlcdb.FailDeploymentParams{
|
||||
Error: sql.NullString{String: failure, Valid: true},
|
||||
CompletedAt: sql.NullTime{Time: failedAt, Valid: true},
|
||||
ID: d.ID,
|
||||
})
|
||||
continue
|
||||
}
|
||||
_, _ = db.Exec(
|
||||
`UPDATE services SET status = 'building', updated_at = $1 WHERE id = $2`,
|
||||
time.Now(), service.ID,
|
||||
)
|
||||
go runDeploymentAndSync(
|
||||
context.Background(), db, engine, &d, service,
|
||||
CreateDeploymentRequest{CommitHash: commit, Branch: branch, Trigger: "webhook"},
|
||||
repoUserID,
|
||||
)
|
||||
_ = q.SetServiceStatus(ctx, sqlcdb.SetServiceStatusParams{
|
||||
Status: sql.NullString{String: "building", Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: service.ID,
|
||||
})
|
||||
// Webhook pushes default to a clean build — stale layers are the
|
||||
// classic silent-rollback failure (dflow convention).
|
||||
branchForBuild := branch
|
||||
if branchForBuild == "" {
|
||||
branchForBuild = service.GitBranch
|
||||
}
|
||||
webhookReq := CreateDeploymentRequest{CommitHash: commit, Branch: branchForBuild, Trigger: "webhook", NoCache: true}
|
||||
if pos := getDeployQueue(c).Enqueue(service.ID, deployqueue.Job{
|
||||
DeploymentID: d.ID,
|
||||
Run: func(jctx context.Context) {
|
||||
runDeploymentAndSync(jctx, db, engine, &d, service, webhookReq, repoUserID)
|
||||
},
|
||||
}); pos > 0 {
|
||||
now := sql.NullTime{Time: time.Now(), Valid: true}
|
||||
_ = q.SetDeploymentStatus(ctx, sqlcdb.SetDeploymentStatusParams{
|
||||
Status: sql.NullString{String: "queued", Valid: true},
|
||||
UpdatedAt: now,
|
||||
ID: d.ID,
|
||||
})
|
||||
_ = q.SetServiceStatus(ctx, sqlcdb.SetServiceStatusParams{
|
||||
Status: sql.NullString{String: "queued", Valid: true},
|
||||
UpdatedAt: now,
|
||||
ID: service.ID,
|
||||
})
|
||||
}
|
||||
enqueued++
|
||||
}
|
||||
|
||||
c.JSON(http.StatusAccepted, gin.H{
|
||||
"received": true,
|
||||
"branch": branch,
|
||||
"deployments": enqueued,
|
||||
})
|
||||
return enqueued, nil
|
||||
}
|
||||
|
||||
// verifyGitWebhookSignature checks the push signature per provider:
|
||||
|
||||
@@ -1,6 +1,16 @@
|
||||
package api
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestNormalizeRepositoryFullName(t *testing.T) {
|
||||
cases := []struct {
|
||||
@@ -83,3 +93,195 @@ func TestDeriveCloneURL(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
// Live invariant test against real Postgres — set METRICS_TEST_DSN (or
|
||||
// DATABASE_URL). Covers the generated git provider/repo/webhook queries plus
|
||||
// the push-dispatch service matcher (empty branch must match all branches).
|
||||
func TestGitLiveInvariants(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
providerID := uuid.New()
|
||||
repoID := uuid.New()
|
||||
webhookID := uuid.New()
|
||||
svcMain := uuid.New()
|
||||
svcDev := uuid.New()
|
||||
suffix := strings.ReplaceAll(userID.String()[:8], "-", "")
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test')`,
|
||||
userID, "git-"+suffix+"@test.local")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, true)`,
|
||||
projectID, "git-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE id = $1`, userID) }()
|
||||
|
||||
// Provider lifecycle.
|
||||
must(q.InsertGitProvider(ctx, sqlcdb.InsertGitProviderParams{
|
||||
ID: providerID, Name: "github", DisplayName: "GitHub",
|
||||
ApiUrl: "https://api.github.com", WebhookUrl: "https://hooks.test",
|
||||
AccessToken: "tok", UserID: userID,
|
||||
}))
|
||||
providers, err := q.ListGitProviders(ctx, userID)
|
||||
if err != nil || len(providers) == 0 {
|
||||
t.Fatalf("providers: %v %v", err, providers)
|
||||
}
|
||||
name, err := q.GetGitProviderNameByID(ctx, providerID)
|
||||
if err != nil || name != "github" {
|
||||
t.Fatalf("provider name: %v %q", err, name)
|
||||
}
|
||||
fetch, err := q.GetGitProviderForFetch(ctx, sqlcdb.GetGitProviderForFetchParams{ID: providerID, UserID: userID})
|
||||
if err != nil || fetch.AccessToken != "tok" || fetch.ApiUrl != "https://api.github.com" {
|
||||
t.Fatalf("fetch: %v %+v", err, fetch)
|
||||
}
|
||||
|
||||
// Repository insert + search.
|
||||
cloneURL := "https://github.com/acme/svc-" + suffix + ".git"
|
||||
fullName := "acme/svc-" + suffix
|
||||
_, err = q.InsertGitRepository(ctx, sqlcdb.InsertGitRepositoryParams{
|
||||
ID: repoID, ProviderID: providerID, Name: "svc-" + suffix, FullName: fullName,
|
||||
Description: sql.NullString{String: "", Valid: true},
|
||||
CloneUrl: cloneURL,
|
||||
DefaultBranch: sql.NullString{String: "main", Valid: true},
|
||||
IsPrivate: sql.NullBool{Bool: false, Valid: true},
|
||||
UserID: userID,
|
||||
})
|
||||
must(err)
|
||||
byName, err := q.GetGitRepositoryByFullName(ctx, sqlcdb.GetGitRepositoryByFullNameParams{
|
||||
ProviderID: providerID, FullName: fullName, UserID: userID,
|
||||
})
|
||||
if err != nil || byName.ID != repoID || byName.CloneUrl != cloneURL {
|
||||
t.Fatalf("repo by full name: %v %+v", err, byName)
|
||||
}
|
||||
all, err := q.ListGitRepositories(ctx, sqlcdb.ListGitRepositoriesParams{
|
||||
ProviderID: providerID, UserID: userID, Column3: "", Limit: 50, Offset: 0,
|
||||
})
|
||||
if err != nil || len(all) == 0 {
|
||||
t.Fatalf("repo list (empty search): %v %v", err, all)
|
||||
}
|
||||
hit, err := q.ListGitRepositories(ctx, sqlcdb.ListGitRepositoriesParams{
|
||||
ProviderID: providerID, UserID: userID, Column3: "svc-" + suffix, Limit: 50, Offset: 0,
|
||||
})
|
||||
if err != nil || len(hit) != 1 || hit[0].ID != repoID {
|
||||
t.Fatalf("repo list (matching search): %v %v", err, hit)
|
||||
}
|
||||
miss, err := q.ListGitRepositories(ctx, sqlcdb.ListGitRepositoriesParams{
|
||||
ProviderID: providerID, UserID: userID, Column3: "zzz-no-match", Limit: 50, Offset: 0,
|
||||
})
|
||||
if err != nil || len(miss) != 0 {
|
||||
t.Fatalf("repo list (non-matching search): %v %v", err, miss)
|
||||
}
|
||||
count, err := q.CountGitRepositories(ctx, sqlcdb.CountGitRepositoriesParams{
|
||||
ProviderID: providerID, UserID: userID, Column3: "",
|
||||
})
|
||||
if err != nil || count < 1 {
|
||||
t.Fatalf("repo count: %v %d", err, count)
|
||||
}
|
||||
// Upsert on (provider_id, full_name) conflict — updates rather than errors.
|
||||
must(q.UpsertGitRepository(ctx, sqlcdb.UpsertGitRepositoryParams{
|
||||
ID: uuid.New(), ProviderID: providerID, Name: "renamed-" + suffix, FullName: fullName,
|
||||
Description: sql.NullString{String: "d", Valid: true},
|
||||
CloneUrl: cloneURL,
|
||||
DefaultBranch: sql.NullString{String: "main", Valid: true},
|
||||
IsPrivate: sql.NullBool{Bool: true, Valid: true},
|
||||
UserID: userID,
|
||||
}))
|
||||
byName, err = q.GetGitRepositoryByFullName(ctx, sqlcdb.GetGitRepositoryByFullNameParams{
|
||||
ProviderID: providerID, FullName: fullName, UserID: userID,
|
||||
})
|
||||
if err != nil || byName.Name != "renamed-"+suffix {
|
||||
t.Fatalf("repo after upsert: %v %+v", err, byName)
|
||||
}
|
||||
|
||||
// Connected-repos join + webhook lifecycle.
|
||||
connected, err := q.ListConnectedRepositories(ctx, sqlcdb.ListConnectedRepositoriesParams{
|
||||
UserID: userID, Limit: 50, Offset: 0,
|
||||
})
|
||||
if err != nil || len(connected) == 0 {
|
||||
t.Fatalf("connected repos: %v %v", err, connected)
|
||||
}
|
||||
wh, err := q.UpsertGitWebhook(ctx, sqlcdb.UpsertGitWebhookParams{
|
||||
ID: webhookID, RepoID: repoID, ProviderID: providerID,
|
||||
Events: "push", WebhookSecret: "sec-" + suffix,
|
||||
BranchFilter: sql.NullString{String: "", Valid: true},
|
||||
})
|
||||
must(err)
|
||||
if !wh.Active.Bool {
|
||||
t.Fatalf("webhook inactive after upsert: %+v", wh)
|
||||
}
|
||||
push, err := q.GetGitWebhookForPush(ctx, webhookID)
|
||||
if err != nil || push.WebhookSecret != "sec-"+suffix || push.ProviderID != providerID {
|
||||
t.Fatalf("webhook for push: %v %+v", err, push)
|
||||
}
|
||||
pushRepo, err := q.GetGitRepoForPush(ctx, repoID)
|
||||
if err != nil || pushRepo.CloneUrl != cloneURL || pushRepo.FullName != fullName || pushRepo.UserID != userID {
|
||||
t.Fatalf("repo for push: %v %+v", err, pushRepo)
|
||||
}
|
||||
|
||||
// Push-dispatch service matcher: branch filter and empty-branch match-all.
|
||||
for id, branch := range map[uuid.UUID]string{svcMain: "main", svcDev: "dev"} {
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type, git_repo, git_branch)
|
||||
VALUES ($1, $2, $3, $4, 'web', 'git', $5, $6)`,
|
||||
id, "svc-"+suffix+"-"+branch, projectID, envID, cloneURL, branch)
|
||||
must(err)
|
||||
}
|
||||
servicesFor := func(branch string) []sqlcdb.ListServicesForPushRow {
|
||||
rows, err := q.ListServicesForPush(ctx, sqlcdb.ListServicesForPushParams{
|
||||
GitBranch: sql.NullString{String: branch, Valid: true},
|
||||
GitRepo: sql.NullString{String: cloneURL, Valid: true},
|
||||
GitRepo_2: sql.NullString{String: fullName, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("services for push %q: %v", branch, err)
|
||||
}
|
||||
return rows
|
||||
}
|
||||
mainRows := servicesFor("main")
|
||||
if len(mainRows) != 1 || mainRows[0].ID != svcMain || mainRows[0].GitBranch != "main" {
|
||||
t.Fatalf("main branch dispatch: %+v", mainRows)
|
||||
}
|
||||
devRows := servicesFor("dev")
|
||||
if len(devRows) != 1 || devRows[0].ID != svcDev {
|
||||
t.Fatalf("dev branch dispatch: %+v", devRows)
|
||||
}
|
||||
allRows := servicesFor("")
|
||||
if len(allRows) != 2 {
|
||||
t.Fatalf("empty-branch dispatch should match all: %+v", allRows)
|
||||
}
|
||||
// Repo may also be referenced by its full_name.
|
||||
fullRows, err := q.ListServicesForPush(ctx, sqlcdb.ListServicesForPushParams{
|
||||
GitBranch: sql.NullString{String: "main", Valid: true},
|
||||
GitRepo: sql.NullString{String: "[email protected]:other/none.git", Valid: true},
|
||||
GitRepo_2: sql.NullString{String: cloneURL, Valid: true},
|
||||
})
|
||||
if err != nil || len(fullRows) != 1 {
|
||||
t.Fatalf("full-name repo match: %v %+v", err, fullRows)
|
||||
}
|
||||
|
||||
// Provider delete is owner-scoped.
|
||||
other, err := q.DeleteGitProvider(ctx, sqlcdb.DeleteGitProviderParams{ID: providerID, UserID: uuid.New()})
|
||||
if err != nil || other != 0 {
|
||||
t.Fatalf("foreign delete affected %d rows: %v", other, err)
|
||||
}
|
||||
deleted, err := q.DeleteGitProvider(ctx, sqlcdb.DeleteGitProviderParams{ID: providerID, UserID: userID})
|
||||
if err != nil || deleted != 1 {
|
||||
t.Fatalf("owner delete affected %d rows: %v", deleted, err)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,93 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/source"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// resolveGitCloneSpec turns a service's git_repo (clone URL, ssh remote, or
|
||||
// owner/repo) into a source.Spec with the project owner's provider token.
|
||||
// Public repos get no token — the clone proceeds anonymously. github_app
|
||||
// providers store the installation id, not a usable token; a fresh
|
||||
// installation token is minted per deploy.
|
||||
func resolveGitCloneSpec(db *database.DB, ownerID, gitRepo, branch, commit, buildPath string) (source.Spec, error) {
|
||||
raw := strings.TrimSpace(gitRepo)
|
||||
cloneURL, err := source.NormalizeCloneURL(raw)
|
||||
if err != nil {
|
||||
return source.Spec{}, err
|
||||
}
|
||||
|
||||
spec := source.Spec{Branch: branch, Commit: commit, BuildPath: buildPath}
|
||||
q := sqlcdb.New(db.DB)
|
||||
ctx := context.Background()
|
||||
|
||||
ownerUUID, _ := uuid.Parse(ownerID)
|
||||
var providerID uuid.UUID
|
||||
if !strings.Contains(cloneURL, "://") {
|
||||
// Bare owner/repo — the connected repository row knows the host.
|
||||
row, err := q.GetGitRepoCloneByFullName(ctx, sqlcdb.GetGitRepoCloneByFullNameParams{
|
||||
FullName: cloneURL,
|
||||
UserID: ownerUUID,
|
||||
})
|
||||
if err != nil {
|
||||
return source.Spec{}, fmt.Errorf("repository %s is not connected — connect it under Git settings or store a clone URL", raw)
|
||||
}
|
||||
cloneURL = row.CloneUrl
|
||||
providerID = row.ProviderID
|
||||
} else {
|
||||
// URL form — match a connected repo for its provider when possible;
|
||||
// an unmatched public URL still clones, just without credentials.
|
||||
if pid, err := q.GetGitRepoProviderByCloneURL(ctx, sqlcdb.GetGitRepoProviderByCloneURLParams{
|
||||
CloneUrl: cloneURL,
|
||||
UserID: ownerUUID,
|
||||
}); err == nil {
|
||||
providerID = pid
|
||||
}
|
||||
}
|
||||
|
||||
if err := source.ValidateRemoteURL(cloneURL); err != nil {
|
||||
return source.Spec{}, err
|
||||
}
|
||||
spec.CloneURL = cloneURL
|
||||
|
||||
if providerID == uuid.Nil {
|
||||
return spec, nil
|
||||
}
|
||||
prov, err := q.GetGitProviderForFetch(ctx, sqlcdb.GetGitProviderForFetchParams{
|
||||
ID: providerID,
|
||||
UserID: ownerUUID,
|
||||
})
|
||||
if err != nil {
|
||||
return spec, nil // provider row gone — anonymous clone still works for public repos
|
||||
}
|
||||
name, apiURL, token := prov.Name, prov.ApiUrl, prov.AccessToken
|
||||
|
||||
switch strings.ToLower(name) {
|
||||
case "github_app":
|
||||
installationID, err := strconv.ParseInt(strings.TrimSpace(token), 10, 64)
|
||||
if err == nil && installationID > 0 {
|
||||
if minted, err := getGitHubAppInstallationToken(apiURL, installationID); err == nil {
|
||||
spec.Token = minted
|
||||
spec.User = "x-access-token"
|
||||
}
|
||||
}
|
||||
case "gitlab":
|
||||
spec.Token = token
|
||||
spec.User = "oauth2"
|
||||
case "bitbucket":
|
||||
spec.Token = token
|
||||
spec.User = "x-token-auth"
|
||||
default:
|
||||
spec.Token = token
|
||||
spec.User = "x-access-token"
|
||||
}
|
||||
return spec, nil
|
||||
}
|
||||
@@ -0,0 +1,310 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"crypto/subtle"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// GitHub App self-provisioning (manifest flow): an admin POSTs the manifest
|
||||
// to github.com/settings/apps/new, GitHub redirects back with `code`, and the
|
||||
// code is exchanged at /app-manifests/{code}/conversions for real credentials
|
||||
// stored in app_settings. Values stored there take precedence over the
|
||||
// GITHUB_APP_* env vars, so the flow removes the manual env-setup barrier.
|
||||
|
||||
const (
|
||||
settingGitHubAppID = "github_app.id"
|
||||
settingGitHubAppSlug = "github_app.slug"
|
||||
settingGitHubAppName = "github_app.name"
|
||||
settingGitHubAppClientID = "github_app.client_id"
|
||||
settingGitHubAppClientSecret = "github_app.client_secret"
|
||||
settingGitHubAppPrivateKey = "github_app.private_key"
|
||||
settingGitHubAppWebhookSecret = "github_app.webhook_secret"
|
||||
)
|
||||
|
||||
// platformSetting resolves a setting row first, then env, then empty.
|
||||
func platformSetting(key, envKey string) string {
|
||||
return strings.TrimSpace(settingValue(GetAuditDB(), key, envKey, ""))
|
||||
}
|
||||
|
||||
func githubAppConfigured(db *database.DB) bool {
|
||||
return strings.TrimSpace(settingValue(db, settingGitHubAppID, "GITHUB_APP_ID", "")) != "" &&
|
||||
strings.TrimSpace(settingValue(db, settingGitHubAppPrivateKey, "GITHUB_APP_PRIVATE_KEY", "")) != ""
|
||||
}
|
||||
|
||||
// POST /admin/git/github-app/manifest — builds the GitHub App manifest the
|
||||
// browser POSTs to github.com. Response carries the manifest as a JSON string
|
||||
// (the form submits it verbatim) plus the destination URL.
|
||||
func handleAdminGitHubAppManifest(c *gin.Context) {
|
||||
var req struct {
|
||||
BaseURL string `json:"base_url" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
Organization string `json:"organization"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_BODY", "base_url is required")
|
||||
return
|
||||
}
|
||||
base := strings.TrimSuffix(strings.TrimSpace(req.BaseURL), "/")
|
||||
if !strings.HasPrefix(base, "https://") &&
|
||||
!strings.HasPrefix(base, "http://localhost") &&
|
||||
!strings.HasPrefix(base, "http://127.0.0.1") {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "base_url must be an https URL (localhost http allowed)")
|
||||
return
|
||||
}
|
||||
|
||||
stateBuf := make([]byte, 16)
|
||||
if _, err := rand.Read(stateBuf); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate state")
|
||||
return
|
||||
}
|
||||
state := hex.EncodeToString(stateBuf)
|
||||
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
db, _ := c.Get("db")
|
||||
if d, ok := db.(*database.DB); ok {
|
||||
name = strings.TrimSpace(settingValue(d, settingBrandProductName, "BRAND_NAME", ""))
|
||||
}
|
||||
if name == "" {
|
||||
name = "Containr"
|
||||
}
|
||||
name = fmt.Sprintf("%s-%s", name, time.Now().UTC().Format("2006-01-02"))
|
||||
}
|
||||
|
||||
callbackURL := base + "/git/github-app/callback"
|
||||
manifest, err := json.Marshal(map[string]interface{}{
|
||||
"name": name,
|
||||
"url": base,
|
||||
"hook_attributes": map[string]interface{}{
|
||||
"url": base + "/api/git/github-app/webhook",
|
||||
"active": true,
|
||||
},
|
||||
"redirect_url": callbackURL,
|
||||
"callback_urls": []string{callbackURL},
|
||||
"public": false,
|
||||
"default_permissions": map[string]string{
|
||||
"contents": "read",
|
||||
"metadata": "read",
|
||||
"pull_requests": "write",
|
||||
},
|
||||
"default_events": []string{"push", "pull_request"},
|
||||
})
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to build manifest")
|
||||
return
|
||||
}
|
||||
|
||||
org := strings.TrimSpace(req.Organization)
|
||||
createURL := fmt.Sprintf("https://github.com/settings/apps/new?state=%s", state)
|
||||
if org != "" {
|
||||
createURL = fmt.Sprintf("https://github.com/organizations/%s/settings/apps/new?state=%s", org, state)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"url": createURL,
|
||||
"manifest": string(manifest),
|
||||
"state": state,
|
||||
})
|
||||
}
|
||||
|
||||
// POST /admin/git/github-app/convert — exchanges the `code` GitHub appends to
|
||||
// the manifest redirect for the real app credentials, then stores them.
|
||||
func handleAdminGitHubAppConvert(c *gin.Context) {
|
||||
var req struct {
|
||||
Code string `json:"code" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_BODY", "code is required")
|
||||
return
|
||||
}
|
||||
|
||||
apiBase := strings.TrimSuffix(strings.TrimSpace(os.Getenv("GITHUB_APP_BASE_URL")), "/")
|
||||
if apiBase == "" {
|
||||
apiBase = "https://api.github.com"
|
||||
}
|
||||
request, err := http.NewRequest(http.MethodPost,
|
||||
fmt.Sprintf("%s/app-manifests/%s/conversions", apiBase, strings.TrimSpace(req.Code)), nil)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to build conversion request")
|
||||
return
|
||||
}
|
||||
request.Header.Set("Accept", "application/vnd.github+json")
|
||||
request.Header.Set("X-GitHub-Api-Version", "2022-11-28")
|
||||
|
||||
client := &http.Client{Timeout: 15 * time.Second}
|
||||
resp, err := client.Do(request)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadGateway, "UPSTREAM", "failed to reach GitHub")
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if resp.StatusCode != http.StatusCreated && resp.StatusCode != http.StatusOK {
|
||||
respondError(c, http.StatusBadGateway, "UPSTREAM",
|
||||
fmt.Sprintf("GitHub conversion failed (%d)", resp.StatusCode))
|
||||
return
|
||||
}
|
||||
|
||||
var app struct {
|
||||
ID int64 `json:"id"`
|
||||
Slug string `json:"slug"`
|
||||
Name string `json:"name"`
|
||||
ClientID string `json:"client_id"`
|
||||
ClientSecret string `json:"client_secret"`
|
||||
PEM string `json:"pem"`
|
||||
WebhookSecret string `json:"webhook_secret"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &app); err != nil || app.ID == 0 {
|
||||
respondError(c, http.StatusBadGateway, "UPSTREAM", "invalid conversion response")
|
||||
return
|
||||
}
|
||||
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
for _, kv := range []struct {
|
||||
key string
|
||||
value string
|
||||
secret bool
|
||||
}{
|
||||
{settingGitHubAppID, fmt.Sprintf("%d", app.ID), false},
|
||||
{settingGitHubAppSlug, app.Slug, false},
|
||||
{settingGitHubAppName, app.Name, false},
|
||||
{settingGitHubAppClientID, app.ClientID, false},
|
||||
{settingGitHubAppClientSecret, app.ClientSecret, true},
|
||||
{settingGitHubAppPrivateKey, app.PEM, true},
|
||||
{settingGitHubAppWebhookSecret, app.WebhookSecret, true},
|
||||
} {
|
||||
if err := setSetting(db, kv.key, kv.value, kv.secret); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to store app credentials")
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "github_app", fmt.Sprintf("%d", app.ID), "provision",
|
||||
map[string]interface{}{"name": app.Name, "slug": app.Slug})
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"app_id": app.ID,
|
||||
"slug": app.Slug,
|
||||
"name": app.Name,
|
||||
})
|
||||
}
|
||||
|
||||
// GET /admin/git/github-app — provisioning status for the settings UI.
|
||||
func handleAdminGetGitHubApp(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
source := "none"
|
||||
if v, err := sqlcdb.New(db.DB).GetAppSetting(c.Request.Context(), settingGitHubAppID); err == nil && strings.TrimSpace(v) != "" {
|
||||
source = "settings"
|
||||
} else if strings.TrimSpace(os.Getenv("GITHUB_APP_ID")) != "" {
|
||||
source = "env"
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"configured": githubAppConfigured(db),
|
||||
"source": source,
|
||||
"app_id": settingValue(db, settingGitHubAppID, "GITHUB_APP_ID", ""),
|
||||
"slug": settingValue(db, settingGitHubAppSlug, "GITHUB_APP_SLUG", ""),
|
||||
"name": settingValue(db, settingGitHubAppName, "", ""),
|
||||
})
|
||||
}
|
||||
|
||||
// POST /api/git/github-app/webhook — the single App-level webhook all
|
||||
// installed repos deliver to. Verified against the provisioned webhook
|
||||
// secret, then dispatched to matching services.
|
||||
func handleGitHubAppWebhook(c *gin.Context) {
|
||||
dbValue, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
db := dbValue.(*database.DB)
|
||||
|
||||
secret := strings.TrimSpace(settingValue(db, settingGitHubAppWebhookSecret, "GITHUB_APP_WEBHOOK_SECRET", ""))
|
||||
if secret == "" {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "GitHub App webhook not configured"})
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(io.LimitReader(c.Request.Body, 4<<20))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Failed to read payload"})
|
||||
return
|
||||
}
|
||||
|
||||
mac := hmac.New(sha256.New, []byte(secret))
|
||||
mac.Write(body)
|
||||
expected := mac.Sum(nil)
|
||||
sig := strings.TrimPrefix(c.GetHeader("X-Hub-Signature-256"), "sha256=")
|
||||
decoded, err := hex.DecodeString(sig)
|
||||
if err != nil || len(decoded) == 0 || subtle.ConstantTimeCompare(decoded, expected) != 1 {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid signature"})
|
||||
return
|
||||
}
|
||||
|
||||
event := c.GetHeader("X-GitHub-Event")
|
||||
if event != "push" {
|
||||
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "event " + event})
|
||||
return
|
||||
}
|
||||
|
||||
var payload struct {
|
||||
Ref string `json:"ref"`
|
||||
After string `json:"after"`
|
||||
Repository struct {
|
||||
FullName string `json:"full_name"`
|
||||
CloneURL string `json:"clone_url"`
|
||||
} `json:"repository"`
|
||||
HeadCommit struct {
|
||||
ID string `json:"id"`
|
||||
} `json:"head_commit"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &payload); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid payload"})
|
||||
return
|
||||
}
|
||||
if !strings.HasPrefix(payload.Ref, "refs/heads/") {
|
||||
c.JSON(http.StatusAccepted, gin.H{"received": true, "ignored": "not a branch push"})
|
||||
return
|
||||
}
|
||||
branch := strings.TrimPrefix(payload.Ref, "refs/heads/")
|
||||
commit := payload.HeadCommit.ID
|
||||
if commit == "" {
|
||||
commit = payload.After
|
||||
}
|
||||
|
||||
cloneURL := payload.Repository.CloneURL
|
||||
fullName := payload.Repository.FullName
|
||||
if cloneURL == "" {
|
||||
cloneURL = fmt.Sprintf("https://github.com/%s.git", fullName)
|
||||
}
|
||||
|
||||
// Services tracking an App-connected repo store either the clone URL or
|
||||
// owner/name — match both like the per-repo receiver does.
|
||||
engineValue, _ := c.Get("deployment_engine")
|
||||
engine, _ := engineValue.(*deployment.DeploymentEngine)
|
||||
|
||||
enqueued, err := dispatchPushToServices(c, db, engine, cloneURL, fullName, branch, commit, "")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to match services"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusAccepted, gin.H{
|
||||
"received": true,
|
||||
"branch": branch,
|
||||
"deployments": enqueued,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sort"
|
||||
"strings"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
)
|
||||
|
||||
// Remote ingress: replicas on node agents aren't on the hub's
|
||||
// containr-network, so Traefik's Docker provider can't see them. Instead
|
||||
// the backend renders one file-provider config per service into
|
||||
// TRAEFIK_DYNAMIC_DIR (a directory Traefik watches), pointing the
|
||||
// loadbalancer at the node's reachable IP + the ephemeral host port the
|
||||
// agent reported back after `create_container`.
|
||||
//
|
||||
// Empty dir → feature off (bare-metal dev). Files are best-effort: a
|
||||
// failed write never fails a reconcile.
|
||||
|
||||
func ingressDir() string { return os.Getenv("TRAEFIK_DYNAMIC_DIR") }
|
||||
|
||||
func ingressFile(serviceID string) string {
|
||||
return filepath.Join(ingressDir(), "containr-"+serviceID+".yml")
|
||||
}
|
||||
|
||||
// jsonInt coerces JSON-ish numbers (int/int32/int64/float64) to int —
|
||||
// spec fields arrive as int32, agent payloads unmarshal as float64.
|
||||
func jsonInt(v interface{}) int {
|
||||
switch n := v.(type) {
|
||||
case int:
|
||||
return n
|
||||
case int32:
|
||||
return int(n)
|
||||
case int64:
|
||||
return int(n)
|
||||
case float64:
|
||||
return int(n)
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// reportedHostPorts parses the agent's create_container result —
|
||||
// {"name": ..., "host_ports": {"80/tcp": "32768"}}. Older agents return a
|
||||
// bare name string; JSON parse failure means no port info.
|
||||
func reportedHostPorts(result string) map[string]string {
|
||||
var out struct {
|
||||
HostPorts map[string]string `json:"host_ports"`
|
||||
}
|
||||
if json.Unmarshal([]byte(result), &out) != nil {
|
||||
return nil
|
||||
}
|
||||
return out.HostPorts
|
||||
}
|
||||
|
||||
// nodeIngressIP picks the address the hub can reach the node on. Mesh
|
||||
// overlays first — the agent operator joined a mesh precisely so the hub
|
||||
// can reach it — then the reported primary IP as a LAN fallback.
|
||||
func nodeIngressIP(agent sqlcdb.NodeAgent) string {
|
||||
var meta struct {
|
||||
Mesh map[string]string `json:"mesh"`
|
||||
}
|
||||
_ = json.Unmarshal(agent.Metadata.RawMessage, &meta)
|
||||
for _, prefix := range []string{"tailscale", "netbird", "wg", "zt"} {
|
||||
var keys []string
|
||||
for name := range meta.Mesh {
|
||||
keys = append(keys, name)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, name := range keys {
|
||||
if strings.HasPrefix(name, prefix) && meta.Mesh[name] != "" {
|
||||
return meta.Mesh[name]
|
||||
}
|
||||
}
|
||||
}
|
||||
return agent.IpAddress
|
||||
}
|
||||
|
||||
// syncRemoteIngress rewrites the service's file-provider config from the
|
||||
// live remote replica set. Called after every remote reconcile so replica
|
||||
// adds/removals and node retargets converge the upstream list.
|
||||
func syncRemoteIngress(ctx context.Context, db *database.DB, spec deployment.RuntimeSpec) {
|
||||
if ingressDir() == "" {
|
||||
return
|
||||
}
|
||||
domains := spec.Domains
|
||||
if len(domains) == 0 && spec.Domain != "" {
|
||||
domains = []string{spec.Domain}
|
||||
}
|
||||
if len(domains) == 0 || spec.Port <= 0 {
|
||||
removeServiceIngress(spec.ServiceID)
|
||||
return
|
||||
}
|
||||
|
||||
q := sqlcdb.New(db.DB)
|
||||
rows, err := q.ListServiceContainers(ctx, spec.ServiceID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
type upstream struct{ ip, port string }
|
||||
var servers []upstream
|
||||
for _, row := range rows {
|
||||
var st struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
_ = json.Unmarshal(row.Status.RawMessage, &st)
|
||||
if st.State != "running" {
|
||||
continue
|
||||
}
|
||||
var port string
|
||||
var plist []map[string]interface{}
|
||||
if err := json.Unmarshal(row.Ports.RawMessage, &plist); err == nil {
|
||||
for _, p := range plist {
|
||||
if hp, ok := p["host_port"].(float64); ok && hp > 0 {
|
||||
port = fmt.Sprintf("%d", int(hp))
|
||||
}
|
||||
}
|
||||
}
|
||||
if port == "" {
|
||||
continue
|
||||
}
|
||||
agent, err := q.GetAgent(ctx, row.NodeAgentID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if ip := nodeIngressIP(agent); ip != "" {
|
||||
servers = append(servers, upstream{ip, port})
|
||||
}
|
||||
}
|
||||
if len(servers) == 0 {
|
||||
removeServiceIngress(spec.ServiceID)
|
||||
return
|
||||
}
|
||||
|
||||
router := "svc-" + spec.ServiceID[:8]
|
||||
rules := make([]string, 0, len(domains))
|
||||
for _, d := range domains {
|
||||
rules = append(rules, "Host(`"+d+"`)")
|
||||
}
|
||||
var b strings.Builder
|
||||
b.WriteString("http:\n routers:\n " + router + ":\n")
|
||||
b.WriteString(" rule: \"" + strings.Join(rules, " || ") + "\"\n")
|
||||
b.WriteString(" entrypoints: [web]\n")
|
||||
b.WriteString(" service: " + router + "\n")
|
||||
b.WriteString(" services:\n " + router + ":\n loadBalancer:\n servers:\n")
|
||||
for _, s := range servers {
|
||||
b.WriteString(" - url: \"http://" + s.ip + ":" + s.port + "\"\n")
|
||||
}
|
||||
// Atomic-ish: write temp then rename so Traefik's watcher never reads a
|
||||
// truncated file.
|
||||
tmp := ingressFile(spec.ServiceID) + ".tmp"
|
||||
if err := os.WriteFile(tmp, []byte(b.String()), 0o644); err != nil {
|
||||
return
|
||||
}
|
||||
_ = os.Rename(tmp, ingressFile(spec.ServiceID))
|
||||
}
|
||||
|
||||
// removeServiceIngress deletes the service's remote route. Called on
|
||||
// remote teardown and when a service returns to local placement.
|
||||
func removeServiceIngress(serviceID string) {
|
||||
if ingressDir() == "" {
|
||||
return
|
||||
}
|
||||
_ = os.Remove(ingressFile(serviceID))
|
||||
}
|
||||
|
||||
// wakeUpstreamURL is where a sleeping remote service's route points —
|
||||
// the backend's wake-page endpoint. Defaults to the compose service name;
|
||||
// TRAEFIK_BACKEND_URL overrides for custom layouts.
|
||||
func wakeUpstreamURL(apiPort int) string {
|
||||
if v := strings.TrimSpace(os.Getenv("TRAEFIK_BACKEND_URL")); v != "" {
|
||||
return strings.TrimRight(v, "/")
|
||||
}
|
||||
return fmt.Sprintf("http://containr-backend:%d", apiPort)
|
||||
}
|
||||
|
||||
// writeWakeIngress routes the service's domains at the wake-page endpoint —
|
||||
// first hit triggers the wake reconcile and serves a reloading page.
|
||||
func writeWakeIngress(serviceID string, domains []string, apiPort int) {
|
||||
if ingressDir() == "" {
|
||||
return
|
||||
}
|
||||
sort.Strings(domains)
|
||||
router := "svc-" + serviceID[:8] + "-wake"
|
||||
rules := make([]string, 0, len(domains))
|
||||
for _, d := range domains {
|
||||
rules = append(rules, "Host(`"+d+"`)")
|
||||
}
|
||||
var b strings.Builder
|
||||
mw := router + "-rw"
|
||||
b.WriteString("http:\n routers:\n " + router + ":\n")
|
||||
b.WriteString(" rule: \"" + strings.Join(rules, " || ") + "\"\n")
|
||||
b.WriteString(" entrypoints: [web]\n")
|
||||
b.WriteString(" service: " + router + "\n")
|
||||
b.WriteString(" priority: 100\n")
|
||||
b.WriteString(" middlewares: [" + mw + "]\n")
|
||||
b.WriteString(" middlewares:\n " + mw + ":\n")
|
||||
b.WriteString(" replacePath:\n path: \"/api/v1/internal/wake-page/" + serviceID + "\"\n")
|
||||
b.WriteString(" services:\n " + router + ":\n loadBalancer:\n servers:\n")
|
||||
b.WriteString(" - url: \"" + wakeUpstreamURL(apiPort) + "\"\n")
|
||||
tmp := ingressFile(serviceID) + ".tmp"
|
||||
if err := os.WriteFile(tmp, []byte(b.String()), 0o644); err != nil {
|
||||
return
|
||||
}
|
||||
_ = os.Rename(tmp, ingressFile(serviceID))
|
||||
}
|
||||
@@ -0,0 +1,251 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// Team invites: an admin creates a single-use link; the invitee registers
|
||||
// through it even while public signup is closed. Tokens are random 32-byte
|
||||
// values shared once — only their sha256 hash is stored.
|
||||
|
||||
type inviteJSON struct {
|
||||
ID string `json:"id"`
|
||||
Email *string `json:"email,omitempty"`
|
||||
ExpiresAt time.Time `json:"expires_at"`
|
||||
UsedAt *time.Time `json:"used_at,omitempty"`
|
||||
CreatedAt *time.Time `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
func toInviteJSON(i sqlcdb.UserInvite) inviteJSON {
|
||||
out := inviteJSON{ID: i.ID.String(), ExpiresAt: i.ExpiresAt}
|
||||
if i.Email.Valid && i.Email.String != "" {
|
||||
out.Email = &i.Email.String
|
||||
}
|
||||
if i.UsedAt.Valid {
|
||||
out.UsedAt = &i.UsedAt.Time
|
||||
}
|
||||
if i.CreatedAt.Valid {
|
||||
out.CreatedAt = &i.CreatedAt.Time
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func inviteTokenHash(token string) string {
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
func generateInviteToken() (string, error) {
|
||||
buf := make([]byte, 32)
|
||||
if _, err := rand.Read(buf); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString(buf), nil
|
||||
}
|
||||
|
||||
// POST /admin/invites — {email?, expires_in_hours?} → {invite, token, url}
|
||||
func handleAdminCreateInvite(c *gin.Context) {
|
||||
var req struct {
|
||||
Email string `json:"email"`
|
||||
ExpiresInHours *int `json:"expires_in_hours"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_BODY", "invalid request body")
|
||||
return
|
||||
}
|
||||
ttl := 24 * 7
|
||||
if req.ExpiresInHours != nil {
|
||||
if *req.ExpiresInHours < 1 || *req.ExpiresInHours > 24*90 {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "expires_in_hours must be 1–2160")
|
||||
return
|
||||
}
|
||||
ttl = *req.ExpiresInHours
|
||||
}
|
||||
token, err := generateInviteToken()
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate token")
|
||||
return
|
||||
}
|
||||
userID, _ := requireAuthenticatedUserUUID(c)
|
||||
params := sqlcdb.CreateUserInviteParams{
|
||||
TokenHash: inviteTokenHash(token),
|
||||
CreatedBy: uuid.NullUUID{UUID: userID, Valid: true},
|
||||
ExpiresAt: time.Now().UTC().Add(time.Duration(ttl) * time.Hour),
|
||||
}
|
||||
if email := strings.ToLower(strings.TrimSpace(req.Email)); email != "" {
|
||||
params.Email = sql.NullString{String: email, Valid: true}
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
invite, err := sqlcdb.New(db.DB).CreateUserInvite(c.Request.Context(), params)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to create invite")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"invite": toInviteJSON(invite),
|
||||
"token": token,
|
||||
"url": "/auth/accept-invite?token=" + token,
|
||||
})
|
||||
}
|
||||
|
||||
// GET /admin/invites
|
||||
func handleAdminListInvites(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
invites, err := sqlcdb.New(db.DB).ListUserInvites(c.Request.Context())
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to list invites")
|
||||
return
|
||||
}
|
||||
out := make([]inviteJSON, 0, len(invites))
|
||||
for _, i := range invites {
|
||||
out = append(out, toInviteJSON(i))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"invites": out})
|
||||
}
|
||||
|
||||
// DELETE /admin/invites/:id — revoke an unused invite.
|
||||
func handleAdminDeleteInvite(c *gin.Context) {
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid invite id")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
if err := sqlcdb.New(db.DB).DeleteUserInvite(c.Request.Context(), id); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to delete invite")
|
||||
return
|
||||
}
|
||||
c.Status(http.StatusNoContent)
|
||||
}
|
||||
|
||||
// GET /auth/invites/:token — public. Lets the accept page validate the link
|
||||
// and pre-fill a bound email before the user commits to registering.
|
||||
func handleGetInvite(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
invite, err := sqlcdb.New(db.DB).GetUserInviteByTokenHash(c.Request.Context(), inviteTokenHash(c.Param("token")))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "invite not found")
|
||||
return
|
||||
}
|
||||
expired := time.Now().After(invite.ExpiresAt)
|
||||
var email *string
|
||||
if invite.Email.Valid && invite.Email.String != "" {
|
||||
email = &invite.Email.String
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"valid": !invite.UsedAt.Valid && !expired,
|
||||
"used": invite.UsedAt.Valid,
|
||||
"expired": expired,
|
||||
"email": email,
|
||||
"expires_at": invite.ExpiresAt,
|
||||
})
|
||||
}
|
||||
|
||||
// POST /auth/accept-invite — {token, name, email, password}
|
||||
func handleAcceptInvite(c *gin.Context) {
|
||||
var req struct {
|
||||
Token string `json:"token" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Email string `json:"email" binding:"required"`
|
||||
Password string `json:"password" binding:"required"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_BODY", "token, name, email and password are required")
|
||||
return
|
||||
}
|
||||
if len(req.Password) < 8 {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "password must be at least 8 characters")
|
||||
return
|
||||
}
|
||||
req.Email = strings.ToLower(strings.TrimSpace(req.Email))
|
||||
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
q := sqlcdb.New(db.DB)
|
||||
invite, err := q.GetUserInviteByTokenHash(c.Request.Context(), inviteTokenHash(req.Token))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "invite not found")
|
||||
return
|
||||
}
|
||||
if invite.UsedAt.Valid {
|
||||
respondError(c, http.StatusGone, "INVITE_USED", "invite already used")
|
||||
return
|
||||
}
|
||||
if time.Now().After(invite.ExpiresAt) {
|
||||
respondError(c, http.StatusGone, "INVITE_EXPIRED", "invite expired")
|
||||
return
|
||||
}
|
||||
if invite.Email.Valid && invite.Email.String != "" && invite.Email.String != req.Email {
|
||||
respondError(c, http.StatusForbidden, "INVITE_EMAIL_MISMATCH", "invite is bound to a different email")
|
||||
return
|
||||
}
|
||||
|
||||
existing, err := q.CountUsersByEmail(c.Request.Context(), req.Email)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "database error")
|
||||
return
|
||||
}
|
||||
if existing > 0 {
|
||||
respondError(c, http.StatusConflict, "CONFLICT", "user already exists")
|
||||
return
|
||||
}
|
||||
|
||||
if err := createBetterAuthUser(c, strings.TrimSpace(req.Name), req.Email, req.Password); err != nil && !errors.Is(err, errBetterAuthUserExists) {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to hash password")
|
||||
return
|
||||
}
|
||||
|
||||
row, err := q.InsertUser(c.Request.Context(), sqlcdb.InsertUserParams{
|
||||
Email: req.Email,
|
||||
PasswordHash: string(hashedPassword),
|
||||
Name: strings.TrimSpace(req.Name),
|
||||
})
|
||||
if err != nil {
|
||||
respondError(c, http.StatusConflict, "CONFLICT", "user already exists")
|
||||
return
|
||||
}
|
||||
user := User{
|
||||
ID: row.ID.String(),
|
||||
Email: row.Email,
|
||||
Name: row.Name,
|
||||
AvatarURL: row.AvatarUrl,
|
||||
IsAdmin: row.IsAdmin,
|
||||
CreatedAt: row.CreatedAt.Time.String(),
|
||||
}
|
||||
|
||||
userUUID, _ := uuid.Parse(user.ID)
|
||||
if err := q.MarkUserInviteUsed(c.Request.Context(), sqlcdb.MarkUserInviteUsedParams{
|
||||
ID: invite.ID,
|
||||
UsedBy: uuid.NullUUID{UUID: userUUID, Valid: userUUID != uuid.Nil},
|
||||
}); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to consume invite")
|
||||
return
|
||||
}
|
||||
|
||||
jwtSecret := c.MustGet("jwt_secret").(string)
|
||||
token, err := generateJWT(user.ID, user.Email, jwtSecret)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "failed to generate token")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, AuthResponse{Token: token, User: user})
|
||||
}
|
||||
@@ -9,6 +9,7 @@ import (
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/docker"
|
||||
|
||||
containertypes "github.com/docker/docker/api/types/container"
|
||||
@@ -60,17 +61,12 @@ func handleGetServiceMetrics(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var projectID uuid.UUID
|
||||
var status string
|
||||
err = db.QueryRow(
|
||||
`SELECT s.project_id, COALESCE(s.status, '')
|
||||
FROM services s WHERE s.id = $1`,
|
||||
serviceID,
|
||||
).Scan(&projectID, &status)
|
||||
svc, err := sqlcdb.New(db.DB).GetServiceProjectAndStatus(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
||||
return
|
||||
}
|
||||
projectID := svc.ProjectID
|
||||
if _, allowed := projectReadAccess(c, db, projectID); !allowed {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
||||
return
|
||||
|
||||
@@ -0,0 +1,495 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
)
|
||||
|
||||
// agentNodeRunner implements deployment.NodeRunner by dispatching container
|
||||
// lifecycle commands to a node agent's command queue and waiting for the
|
||||
// reported result. Domains route through the file provider —
|
||||
// syncRemoteIngress renders per-service configs from live inventory.
|
||||
type agentNodeRunner struct {
|
||||
q *sqlcdb.Queries
|
||||
db *database.DB
|
||||
}
|
||||
|
||||
func newAgentNodeRunner(db *database.DB) *agentNodeRunner {
|
||||
return &agentNodeRunner{q: sqlcdb.New(db.DB), db: db}
|
||||
}
|
||||
|
||||
// remoteContainerID is deterministic per (agent, service, replica) so redeploys
|
||||
// update the same inventory row instead of accumulating stale ones.
|
||||
func remoteContainerID(agentID, serviceID string, replica int) string {
|
||||
sum := sha256.Sum256([]byte(agentID + "|" + serviceID + "|" + fmt.Sprint(replica)))
|
||||
return fmt.Sprintf("%x-%x-%x-%x-%x", sum[0:4], sum[4:6], sum[6:8], sum[8:10], sum[10:16])
|
||||
}
|
||||
|
||||
func remoteContainerName(serviceID string, replica int) string {
|
||||
return fmt.Sprintf("containr-%s-%d", serviceID, replica)
|
||||
}
|
||||
|
||||
func (r *agentNodeRunner) ReconcileOnNode(ctx context.Context, spec deployment.RuntimeSpec) (*deployment.RuntimeState, error) {
|
||||
agent, err := r.q.GetAgent(ctx, spec.NodeID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("node %s not found", spec.NodeID)
|
||||
}
|
||||
if !agent.Status.Valid || agent.Status.String != "online" {
|
||||
return nil, fmt.Errorf("node %s is %s — heartbeats resume scheduling when it reconnects", agent.Name, agent.Status.String)
|
||||
}
|
||||
return r.reconcileOnAgents(ctx, spec, func(int) string { return spec.NodeID })
|
||||
}
|
||||
|
||||
// ReconcileSpread distributes replicas deterministically across online,
|
||||
// schedulable agents: replica i lands on agents[i % len(agents)], agents
|
||||
// sorted by id. Replicas wrap when they outnumber nodes. The local host is
|
||||
// not a spread target — pin "local" or leave unset for local execution.
|
||||
func (r *agentNodeRunner) ReconcileSpread(ctx context.Context, spec deployment.RuntimeSpec) (*deployment.RuntimeState, error) {
|
||||
tagsRaw, _ := json.Marshal(spec.PlacementTags)
|
||||
if len(spec.PlacementTags) == 0 {
|
||||
tagsRaw = []byte("[]")
|
||||
}
|
||||
agents, err := r.q.ListSchedulableAgentsMatching(ctx, json.RawMessage(tagsRaw))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("list schedulable nodes: %w", err)
|
||||
}
|
||||
if len(agents) == 0 {
|
||||
if len(spec.PlacementTags) > 0 {
|
||||
return nil, fmt.Errorf("no online schedulable nodes carry placement tags %v — tag a node or relax the requirement", spec.PlacementTags)
|
||||
}
|
||||
return nil, fmt.Errorf("no online schedulable nodes — connect an agent or unset spread")
|
||||
}
|
||||
return r.reconcileOnAgents(ctx, spec, func(i int) string {
|
||||
return agents[i%len(agents)].ID
|
||||
})
|
||||
}
|
||||
|
||||
// ReconcileAuto resolves the least-loaded online agent matching the
|
||||
// service's placement tags and reconciles every replica onto it. Stale
|
||||
// replicas on other agents retire through reconcileOnAgents — a service
|
||||
// follows its tags when the matching set changes.
|
||||
func (r *agentNodeRunner) ReconcileAuto(ctx context.Context, spec deployment.RuntimeSpec) (*deployment.RuntimeState, error) {
|
||||
tagsRaw, _ := json.Marshal(spec.PlacementTags)
|
||||
if len(spec.PlacementTags) == 0 {
|
||||
tagsRaw = []byte("[]")
|
||||
}
|
||||
agentID, err := r.q.PickLeastLoadedAgentMatching(ctx, json.RawMessage(tagsRaw))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("no online nodes carry placement tags %v", spec.PlacementTags)
|
||||
}
|
||||
return r.reconcileOnAgents(ctx, spec, func(int) string { return agentID })
|
||||
}
|
||||
|
||||
// RemoteRuntimeState reads the service's remote replicas from inventory.
|
||||
// Rows only carry the last dispatched state — the agent doesn't stream
|
||||
// container events — so "running" means "last create succeeded".
|
||||
func (r *agentNodeRunner) RemoteRuntimeState(ctx context.Context, serviceID string) (*deployment.RuntimeState, error) {
|
||||
rows, err := r.q.ListServiceContainers(ctx, serviceID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state := &deployment.RuntimeState{Status: "stopped"}
|
||||
running := 0
|
||||
for _, row := range rows {
|
||||
var status struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
unmarshalRaw(row.Status, &status)
|
||||
if status.State == "" || status.State == "removed" {
|
||||
continue
|
||||
}
|
||||
replica := -1
|
||||
if idx, err := strconv.Atoi(strings.TrimPrefix(row.Name, fmt.Sprintf("containr-%s-", serviceID))); err == nil {
|
||||
replica = idx
|
||||
}
|
||||
state.Containers = append(state.Containers, deployment.RuntimeContainer{
|
||||
ID: row.ID, Name: row.Name, State: status.State, Replica: replica,
|
||||
})
|
||||
if status.State == "running" {
|
||||
running++
|
||||
}
|
||||
}
|
||||
if running > 0 {
|
||||
state.Status = "running"
|
||||
}
|
||||
return state, nil
|
||||
}
|
||||
|
||||
// reconcileOnAgents creates the service's replicas on the agents chosen by
|
||||
// targetFor and retires stale replicas on any agent — including leftovers
|
||||
// from a previous pin or a different spread assignment.
|
||||
func (r *agentNodeRunner) reconcileOnAgents(ctx context.Context, spec deployment.RuntimeSpec, targetFor func(replica int) string) (*deployment.RuntimeState, error) {
|
||||
replicas := spec.Replicas
|
||||
if replicas < 1 {
|
||||
replicas = 1
|
||||
}
|
||||
|
||||
prefix := fmt.Sprintf("containr-%s-", spec.ServiceID)
|
||||
existing, _ := r.q.ListServiceContainers(ctx, spec.ServiceID)
|
||||
for _, row := range existing {
|
||||
if !strings.HasPrefix(row.Name, prefix) {
|
||||
continue
|
||||
}
|
||||
replica, err := strconv.Atoi(strings.TrimPrefix(row.Name, prefix))
|
||||
keep := err == nil && replica < replicas && row.NodeAgentID == targetFor(replica)
|
||||
if keep {
|
||||
continue
|
||||
}
|
||||
if _, cmdErr := r.enqueueAndWait(ctx, row.NodeAgentID, "", "remove_container",
|
||||
map[string]interface{}{"container_name": row.Name}); cmdErr != nil {
|
||||
return nil, fmt.Errorf("remove stale replica %s on node %s: %w", row.Name, row.NodeAgentID, cmdErr)
|
||||
}
|
||||
_ = r.q.UpdateContainerStatus(ctx, sqlcdb.UpdateContainerStatusParams{
|
||||
ID: row.ID,
|
||||
Status: rawJSON(map[string]interface{}{"state": "removed"}),
|
||||
})
|
||||
}
|
||||
|
||||
// Ship the image to every distinct target agent before replicas start.
|
||||
// RemoteBuild nodes docker-build from the packaged context artifact;
|
||||
// RemoteLoad nodes docker-load a saved tarball (local-tag rollbacks).
|
||||
// Registry-pullable images skip this — docker run pulls on demand.
|
||||
if spec.RemoteBuild != nil || spec.RemoteLoad != nil {
|
||||
seen := map[string]bool{}
|
||||
for i := 0; i < replicas; i++ {
|
||||
agentID := targetFor(i)
|
||||
if seen[agentID] {
|
||||
continue
|
||||
}
|
||||
seen[agentID] = true
|
||||
if spec.RemoteBuild != nil {
|
||||
buildArgs := map[string]interface{}{}
|
||||
for k, v := range spec.RemoteBuild.BuildArgs {
|
||||
buildArgs[k] = v
|
||||
}
|
||||
_, err := r.enqueueAndWaitTimeout(ctx, agentID, "", "build_image",
|
||||
map[string]interface{}{
|
||||
"artifact_id": spec.RemoteBuild.ArtifactID,
|
||||
"image": spec.Image,
|
||||
"build_args": buildArgs,
|
||||
"no_cache": spec.RemoteBuild.NoCache,
|
||||
}, 25*time.Minute)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("build image on node %s: %w", agentID, err)
|
||||
}
|
||||
} else if spec.RemoteLoad != nil {
|
||||
_, err := r.enqueueAndWaitTimeout(ctx, agentID, "", "load_image",
|
||||
map[string]interface{}{
|
||||
"artifact_id": spec.RemoteLoad.ArtifactID,
|
||||
}, 10*time.Minute)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("load image on node %s: %w", agentID, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
state := &deployment.RuntimeState{Desired: replicas, Status: "running"}
|
||||
for i := 0; i < replicas; i++ {
|
||||
agentID := targetFor(i)
|
||||
name := remoteContainerName(spec.ServiceID, i)
|
||||
containerID := remoteContainerID(agentID, spec.ServiceID, i)
|
||||
container, env, ports, volumes, restart := r.replicaPayload(spec, agentID, i, name)
|
||||
|
||||
_ = r.q.UpsertServiceContainer(ctx, sqlcdb.UpsertServiceContainerParams{
|
||||
ID: containerID,
|
||||
Name: name,
|
||||
Image: spec.Image,
|
||||
ProjectID: spec.ProjectID,
|
||||
ServiceID: spec.ServiceID,
|
||||
NodeAgentID: agentID,
|
||||
Status: rawJSON(map[string]interface{}{"state": "created", "health": "none"}),
|
||||
Resources: rawJSON(map[string]interface{}{"memory": spec.MemoryBytes, "cpus": float64(spec.NanoCPUs) / 1e9}),
|
||||
Ports: rawJSON(ports),
|
||||
Environment: rawJSON(env),
|
||||
Volumes: rawJSON(volumes),
|
||||
Networks: rawJSON([]string{}),
|
||||
RestartPolicy: rawJSON(map[string]interface{}{"name": restart}),
|
||||
HealthCheck: rawJSON(map[string]interface{}{"path": spec.HealthPath}),
|
||||
})
|
||||
|
||||
result, err := r.enqueueAndWait(ctx, agentID, containerID, "create_container",
|
||||
map[string]interface{}{"container": container})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("replica %d on node %s: %w", i, agentID, err)
|
||||
}
|
||||
if assigned := reportedHostPorts(result); len(assigned) > 0 {
|
||||
for _, p := range ports {
|
||||
pm, ok := p.(map[string]interface{})
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
cport := jsonInt(pm["container_port"])
|
||||
proto, _ := pm["protocol"].(string)
|
||||
if hp, ok := assigned[fmt.Sprintf("%d/%s", cport, proto)]; ok {
|
||||
if n, err := strconv.Atoi(hp); err == nil {
|
||||
pm["host_port"] = n
|
||||
}
|
||||
}
|
||||
}
|
||||
_ = r.q.UpdateContainerPorts(ctx, sqlcdb.UpdateContainerPortsParams{
|
||||
ID: containerID,
|
||||
Status: rawJSON(map[string]interface{}{"state": "running", "health": "none"}),
|
||||
Ports: rawJSON(ports),
|
||||
})
|
||||
} else {
|
||||
_ = r.q.UpdateContainerStatus(ctx, sqlcdb.UpdateContainerStatusParams{
|
||||
ID: containerID,
|
||||
Status: rawJSON(map[string]interface{}{"state": "running", "health": "none"}),
|
||||
})
|
||||
}
|
||||
state.Containers = append(state.Containers, deployment.RuntimeContainer{
|
||||
ID: containerID, Name: name, State: "running", Replica: i,
|
||||
})
|
||||
}
|
||||
syncRemoteIngress(ctx, r.db, spec)
|
||||
return state, nil
|
||||
}
|
||||
|
||||
// replicaPayload builds the agent create_container payload for one replica.
|
||||
func (r *agentNodeRunner) replicaPayload(spec deployment.RuntimeSpec, agentID string, i int, name string) (map[string]interface{}, map[string]interface{}, []interface{}, []interface{}, string) {
|
||||
ports := []interface{}{}
|
||||
if spec.Port > 0 {
|
||||
hostPort := spec.PublishedPort // 0 → ephemeral on the node
|
||||
if i > 0 {
|
||||
hostPort = 0 // only replica 0 claims the preferred host port
|
||||
}
|
||||
ports = append(ports, map[string]interface{}{
|
||||
"published": true,
|
||||
"host_port": hostPort,
|
||||
"container_port": spec.Port,
|
||||
"protocol": "tcp",
|
||||
})
|
||||
}
|
||||
volumes := make([]interface{}, 0, len(spec.Volumes))
|
||||
for _, v := range spec.Volumes {
|
||||
volumes = append(volumes, map[string]interface{}{
|
||||
"source": v.Source, "target": v.Destination, "read_only": v.ReadOnly,
|
||||
})
|
||||
}
|
||||
env := map[string]interface{}{}
|
||||
for k, v := range spec.Env {
|
||||
env[k] = v
|
||||
}
|
||||
restart := spec.RestartPolicy
|
||||
if restart == "" {
|
||||
restart = "unless-stopped"
|
||||
}
|
||||
cmd := make([]interface{}, 0, len(spec.Command))
|
||||
for _, arg := range spec.Command {
|
||||
cmd = append(cmd, arg)
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"name": name,
|
||||
"image": spec.Image,
|
||||
"command": cmd,
|
||||
"environment": env,
|
||||
"registry": r.pullCredentials(spec.ProjectID, spec.Image),
|
||||
"ports": ports,
|
||||
"volumes": volumes,
|
||||
"restart_policy": restart,
|
||||
"memory": spec.MemoryBytes,
|
||||
"cpus": float64(spec.NanoCPUs) / 1e9,
|
||||
"labels": map[string]interface{}{
|
||||
"containr.managed": "true",
|
||||
"containr.project": spec.ProjectID,
|
||||
"containr.service": spec.ServiceID,
|
||||
"containr.replica": fmt.Sprint(i),
|
||||
"containr.node": agentID,
|
||||
},
|
||||
}, env, ports, volumes, restart
|
||||
}
|
||||
|
||||
// pullCredentials resolves the project owner's registry auth for the image
|
||||
// host so private images pull on remote nodes. Empty map → anonymous pull.
|
||||
func (r *agentNodeRunner) pullCredentials(projectID, imageRef string) map[string]interface{} {
|
||||
pid, _ := uuid.Parse(projectID)
|
||||
ownerID, err := sqlcdb.New(r.db.DB).GetProjectOwnerText(context.Background(), pid)
|
||||
if err != nil {
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
auth := registryAuthFor(r.db, ownerID, imageRef)
|
||||
if auth.Username == "" {
|
||||
return map[string]interface{}{}
|
||||
}
|
||||
return map[string]interface{}{
|
||||
"server": auth.ServerAddress,
|
||||
"username": auth.Username,
|
||||
"password": auth.Password,
|
||||
}
|
||||
}
|
||||
|
||||
// ControlService fans a lifecycle action out to every replica row the
|
||||
// service has on the agent. Actions map onto the agent's container commands;
|
||||
// "remove" also tombstones the inventory row.
|
||||
func (r *agentNodeRunner) ControlService(ctx context.Context, serviceID, agentID, action string) error {
|
||||
cmdType := map[string]string{
|
||||
"start": "start_container",
|
||||
"stop": "stop_container",
|
||||
"restart": "restart_container",
|
||||
"remove": "remove_container",
|
||||
}[action]
|
||||
if cmdType == "" {
|
||||
return fmt.Errorf("unsupported action %q", action)
|
||||
}
|
||||
rows, err := r.q.ListContainersForAgent(ctx, agentID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
removed := map[string]interface{}{"state": "removed"}
|
||||
for _, row := range rows {
|
||||
if row.ServiceID != serviceID {
|
||||
continue
|
||||
}
|
||||
// Tombstoned replicas are already gone remotely — skip the dispatch
|
||||
// or a fresh docker rm on a missing name reports a false failure.
|
||||
if action == "remove" {
|
||||
var st struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
if err := json.Unmarshal(row.Status.RawMessage, &st); err == nil && st.State == "removed" {
|
||||
continue
|
||||
}
|
||||
}
|
||||
if _, err := r.enqueueAndWait(ctx, agentID, row.ID, cmdType,
|
||||
map[string]interface{}{"container_name": row.Name}); err != nil {
|
||||
return fmt.Errorf("%s %s on node: %w", action, row.Name, err)
|
||||
}
|
||||
if action == "remove" {
|
||||
_ = r.q.UpdateContainerStatus(ctx, sqlcdb.UpdateContainerStatusParams{
|
||||
ID: row.ID, Status: rawJSON(removed)})
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// RemoveService tears down every replica of the service on the agent and
|
||||
// clears the inventory rows.
|
||||
func (r *agentNodeRunner) RemoveService(ctx context.Context, serviceID, agentID string) error {
|
||||
if err := r.ControlService(ctx, serviceID, agentID, "remove"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := r.q.DeleteServiceContainersOnAgent(ctx, sqlcdb.DeleteServiceContainersOnAgentParams{
|
||||
NodeAgentID: agentID,
|
||||
ServiceID: serviceID,
|
||||
}); err != nil {
|
||||
return err
|
||||
}
|
||||
removeServiceIngress(serviceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// RetireService removes the service's remote replicas wherever they run —
|
||||
// called when the service converges to local placement so a cleared pin or
|
||||
// removed tag requirement doesn't leave containers behind.
|
||||
func (r *agentNodeRunner) RetireService(ctx context.Context, serviceID string) error {
|
||||
agentIDs, err := r.q.ListServiceAgents(ctx, serviceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, agentID := range agentIDs {
|
||||
if err := r.RemoveService(ctx, serviceID, agentID); err != nil {
|
||||
return fmt.Errorf("retire on node %s: %w", agentID, err)
|
||||
}
|
||||
}
|
||||
removeServiceIngress(serviceID)
|
||||
return nil
|
||||
}
|
||||
|
||||
// enqueueAndWait pushes a command to the agent's queue and polls for the
|
||||
// reported result, returning the agent's result string on success. Agents
|
||||
// poll on their own interval (≥5s), so this blocks — it runs inside the
|
||||
// deploy queue worker, never on a request path.
|
||||
func (r *agentNodeRunner) enqueueAndWait(ctx context.Context, agentID, containerID, cmdType string, payload map[string]interface{}) (string, error) {
|
||||
return r.enqueueAndWaitTimeout(ctx, agentID, containerID, cmdType, payload, 8*time.Minute)
|
||||
}
|
||||
|
||||
func (r *agentNodeRunner) enqueueAndWaitTimeout(ctx context.Context, agentID, containerID, cmdType string, payload map[string]interface{}, timeout time.Duration) (string, error) {
|
||||
var cid sql.NullString
|
||||
if containerID != "" {
|
||||
cid = sql.NullString{String: containerID, Valid: true}
|
||||
}
|
||||
cmd, err := r.q.CreateCommand(ctx, sqlcdb.CreateCommandParams{
|
||||
ID: uuid.New().String(),
|
||||
Type: cmdType,
|
||||
NodeAgentID: agentID,
|
||||
ContainerID: cid,
|
||||
Payload: rawJSON(payload),
|
||||
})
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
deadline := time.Now().Add(timeout) // image pulls/builds on a cold node
|
||||
for {
|
||||
row, err := r.q.GetCommandForAgent(ctx, sqlcdb.GetCommandForAgentParams{
|
||||
ID: cmd.ID,
|
||||
NodeAgentID: agentID,
|
||||
})
|
||||
if err == nil {
|
||||
switch row.Status.String {
|
||||
case "completed":
|
||||
return row.Result.String, nil
|
||||
case "failed":
|
||||
if row.Error.Valid && row.Error.String != "" {
|
||||
return "", fmt.Errorf("agent: %s", row.Error.String)
|
||||
}
|
||||
return "", fmt.Errorf("agent reported failure")
|
||||
}
|
||||
}
|
||||
if time.Now().After(deadline) {
|
||||
return "", fmt.Errorf("command %s timed out waiting for the agent", cmdType)
|
||||
}
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return "", ctx.Err()
|
||||
case <-time.After(2 * time.Second):
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// resolveNodePin validates or resolves a node_id request value. "auto" picks
|
||||
// the least-loaded online agent that carries every required placement tag;
|
||||
// "" / "local" clears the pin. An explicit pin bypasses tag filtering —
|
||||
// the operator is overriding placement on purpose.
|
||||
func resolveNodePin(ctx context.Context, db *database.DB, requested string, requiredTags []string) (string, error) {
|
||||
requested = strings.TrimSpace(requested)
|
||||
if requested == "" || requested == "local" {
|
||||
return "", nil
|
||||
}
|
||||
q := sqlcdb.New(db.DB)
|
||||
if requested == "auto" {
|
||||
tagsRaw, _ := json.Marshal(requiredTags)
|
||||
if len(requiredTags) == 0 {
|
||||
tagsRaw = []byte("[]")
|
||||
}
|
||||
id, err := q.PickLeastLoadedAgentMatching(ctx, json.RawMessage(tagsRaw))
|
||||
if err != nil {
|
||||
if len(requiredTags) > 0 {
|
||||
return "", fmt.Errorf("no online nodes carry placement tags %v", requiredTags)
|
||||
}
|
||||
return "", fmt.Errorf("no online nodes available")
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
agent, err := q.GetAgent(ctx, requested)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("node not found")
|
||||
}
|
||||
if !agent.Schedulable {
|
||||
return "", fmt.Errorf("node %s is cordoned — uncordon it before pinning", agent.Name)
|
||||
}
|
||||
return requested, nil
|
||||
}
|
||||
@@ -0,0 +1,225 @@
|
||||
package api
|
||||
|
||||
// Push notification channels — self-hosted ntfy and Gotify endpoints users
|
||||
// attach to their account. insertUserNotification fans out best-effort.
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// jarvis: ceiling — one shared client; no retry/backoff, delivery is
|
||||
// best-effort and failures are silent by design (notifications must never
|
||||
// break the producing flow).
|
||||
var pushHTTPClient = &http.Client{Timeout: 5 * time.Second}
|
||||
|
||||
type notificationChannelDTO struct {
|
||||
ID string `json:"id"`
|
||||
Kind string `json:"kind"`
|
||||
Endpoint string `json:"endpoint"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt string `json:"created_at"`
|
||||
}
|
||||
|
||||
func mapChannel(row sqlcdb.NotificationChannel) notificationChannelDTO {
|
||||
return notificationChannelDTO{
|
||||
ID: row.ID.String(),
|
||||
Kind: row.Kind,
|
||||
Endpoint: row.Endpoint,
|
||||
Enabled: row.Enabled,
|
||||
CreatedAt: row.CreatedAt.Time.Format(time.RFC3339),
|
||||
}
|
||||
}
|
||||
|
||||
func handleListNotificationChannels(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
uid, _ := uuid.Parse(userID)
|
||||
rows, err := sqlcdb.New(db.DB).ListAllNotificationChannelsByUser(c.Request.Context(), uid)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to list channels")
|
||||
return
|
||||
}
|
||||
out := make([]notificationChannelDTO, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
out = append(out, mapChannel(r))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"channels": out})
|
||||
}
|
||||
|
||||
func handleCreateNotificationChannel(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req struct {
|
||||
Kind string `json:"kind" binding:"required,oneof=ntfy gotify"`
|
||||
Endpoint string `json:"endpoint" binding:"required"`
|
||||
Token string `json:"token"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "kind (ntfy|gotify) and endpoint are required")
|
||||
return
|
||||
}
|
||||
endpoint := strings.TrimRight(strings.TrimSpace(req.Endpoint), "/")
|
||||
u, err := url.Parse(endpoint)
|
||||
if err != nil || u.Scheme == "" || u.Host == "" || (u.Scheme != "http" && u.Scheme != "https") {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "endpoint must be a valid http(s) URL")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
uid, _ := uuid.Parse(userID)
|
||||
row, err := sqlcdb.New(db.DB).InsertNotificationChannel(c.Request.Context(), sqlcdb.InsertNotificationChannelParams{
|
||||
UserID: uid,
|
||||
Kind: req.Kind,
|
||||
Endpoint: endpoint,
|
||||
Token: sql.NullString{String: req.Token, Valid: req.Token != ""},
|
||||
})
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to create channel")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"channel": mapChannel(row)})
|
||||
}
|
||||
|
||||
func handleDeleteNotificationChannel(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid channel id")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
uid, _ := uuid.Parse(userID)
|
||||
n, err := sqlcdb.New(db.DB).DeleteNotificationChannel(c.Request.Context(), sqlcdb.DeleteNotificationChannelParams{
|
||||
ID: id, UserID: uid,
|
||||
})
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to delete channel")
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Channel not found")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Channel deleted"})
|
||||
}
|
||||
|
||||
// handleTestNotificationChannel pushes a probe notification through the
|
||||
// channel — surfaces misconfiguration before real alerts depend on it.
|
||||
func handleTestNotificationChannel(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid channel id")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
uid, _ := uuid.Parse(userID)
|
||||
rows, err := sqlcdb.New(db.DB).ListAllNotificationChannelsByUser(c.Request.Context(), uid)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to load channel")
|
||||
return
|
||||
}
|
||||
var target *sqlcdb.NotificationChannel
|
||||
for i := range rows {
|
||||
if rows[i].ID == id {
|
||||
target = &rows[i]
|
||||
break
|
||||
}
|
||||
}
|
||||
if target == nil {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Channel not found")
|
||||
return
|
||||
}
|
||||
if err := deliverPush(c.Request.Context(), *target, "test", "Containr test notification", "Your push channel is configured correctly."); err != nil {
|
||||
respondError(c, http.StatusBadGateway, "DELIVERY_FAILED", fmt.Sprintf("Push failed: %v", err))
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Test notification sent"})
|
||||
}
|
||||
|
||||
// deliverPush sends one notification through a channel. ntfy expects the
|
||||
// body at POST {endpoint} with Title/Priority headers; Gotify takes JSON at
|
||||
// POST {endpoint}/message?token=...
|
||||
func deliverPush(ctx context.Context, ch sqlcdb.NotificationChannel, kind, title, body string) error {
|
||||
var req *http.Request
|
||||
var err error
|
||||
switch ch.Kind {
|
||||
case "ntfy":
|
||||
req, err = http.NewRequestWithContext(ctx, http.MethodPost, ch.Endpoint, strings.NewReader(body))
|
||||
if err == nil {
|
||||
req.Header.Set("Title", title)
|
||||
req.Header.Set("Tags", "containr,"+kind)
|
||||
if ch.Token.Valid && ch.Token.String != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+ch.Token.String)
|
||||
}
|
||||
}
|
||||
case "gotify":
|
||||
payload, _ := json.Marshal(map[string]interface{}{
|
||||
"title": title,
|
||||
"message": body,
|
||||
"priority": 5,
|
||||
})
|
||||
endpoint := ch.Endpoint + "/message"
|
||||
if ch.Token.Valid && ch.Token.String != "" {
|
||||
endpoint += "?token=" + url.QueryEscape(ch.Token.String)
|
||||
}
|
||||
req, err = http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewReader(payload))
|
||||
if err == nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
default:
|
||||
return fmt.Errorf("unsupported channel kind %q", ch.Kind)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
res, err := pushHTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer res.Body.Close()
|
||||
if res.StatusCode >= 400 {
|
||||
return fmt.Errorf("push endpoint returned %d", res.StatusCode)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// fanoutUserPush delivers a notification to every enabled channel the user
|
||||
// configured. Errors are swallowed — the in-app row already landed.
|
||||
func fanoutUserPush(db *database.DB, userID, kind, title, body string) {
|
||||
uid, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
rows, err := sqlcdb.New(db.DB).ListNotificationChannelsByUser(context.Background(), uid)
|
||||
if err != nil || len(rows) == 0 {
|
||||
return
|
||||
}
|
||||
for _, ch := range rows {
|
||||
_ = deliverPush(context.Background(), ch, kind, title, body)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,80 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"containr/internal/database/sqlcdb"
|
||||
)
|
||||
|
||||
func TestDeliverPushNtfy(t *testing.T) {
|
||||
var gotAuth, gotTitle, gotTags, gotBody string
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotAuth = r.Header.Get("Authorization")
|
||||
gotTitle = r.Header.Get("Title")
|
||||
gotTags = r.Header.Get("Tags")
|
||||
b, _ := io.ReadAll(r.Body)
|
||||
gotBody = string(b)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
ch := sqlcdb.NotificationChannel{
|
||||
Kind: "ntfy",
|
||||
Endpoint: srv.URL + "/alerts",
|
||||
Token: sql.NullString{String: "tk_abc", Valid: true},
|
||||
}
|
||||
if err := deliverPush(context.Background(), ch, "deployment", "Deploy done", "svc is live"); err != nil {
|
||||
t.Fatalf("deliverPush: %v", err)
|
||||
}
|
||||
if gotAuth != "Bearer tk_abc" {
|
||||
t.Fatalf("auth header = %q", gotAuth)
|
||||
}
|
||||
if gotTitle != "Deploy done" || gotTags != "containr,deployment" || gotBody != "svc is live" {
|
||||
t.Fatalf("title=%q tags=%q body=%q", gotTitle, gotTags, gotBody)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverPushGotify(t *testing.T) {
|
||||
var gotPath, gotToken string
|
||||
var payload map[string]interface{}
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
gotPath = r.URL.Path
|
||||
gotToken = r.URL.Query().Get("token")
|
||||
_ = json.NewDecoder(r.Body).Decode(&payload)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
ch := sqlcdb.NotificationChannel{
|
||||
Kind: "gotify",
|
||||
Endpoint: srv.URL,
|
||||
Token: sql.NullString{String: "apptoken", Valid: true},
|
||||
}
|
||||
if err := deliverPush(context.Background(), ch, "backup", "Backup done", "db backup ok"); err != nil {
|
||||
t.Fatalf("deliverPush: %v", err)
|
||||
}
|
||||
if gotPath != "/message" || gotToken != "apptoken" {
|
||||
t.Fatalf("path=%q token=%q", gotPath, gotToken)
|
||||
}
|
||||
if payload["title"] != "Backup done" || payload["message"] != "db backup ok" {
|
||||
t.Fatalf("payload = %v", payload)
|
||||
}
|
||||
}
|
||||
|
||||
func TestDeliverPushFailure(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
}))
|
||||
defer srv.Close()
|
||||
|
||||
ch := sqlcdb.NotificationChannel{Kind: "ntfy", Endpoint: srv.URL}
|
||||
if err := deliverPush(context.Background(), ch, "k", "t", "b"); err == nil {
|
||||
t.Fatal("expected error on 500")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,235 @@
|
||||
package api
|
||||
|
||||
// Notification producers — the emitters behind `insertUserNotification`.
|
||||
// Deployment success/failure lives in deployments.go; this file covers
|
||||
// the remaining producers from the roadmap: admin fan-out helper, the
|
||||
// agent offline/online sweep, and the upgrade-available check.
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Version is stamped at image build time via -ldflags "-X
|
||||
// containr/internal/api.Version=…". "dev" disables the upgrade check.
|
||||
var Version = "dev"
|
||||
|
||||
// notifyAdmins inserts a notification for every admin user. Best-effort.
|
||||
func notifyAdmins(db *database.DB, kind, title, body, resourceType, resourceID string) {
|
||||
if db == nil {
|
||||
return
|
||||
}
|
||||
adminIDs, err := sqlcdb.New(db.DB).ListAdminUserIDs(context.Background())
|
||||
if err != nil {
|
||||
log.Printf("containr: notify admins lookup failed: %v", err)
|
||||
return
|
||||
}
|
||||
for _, id := range adminIDs {
|
||||
{
|
||||
insertUserNotification(db, id.String(), kind, title, body, resourceType, resourceID)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// StartAgentHealthSweep marks agents offline when their heartbeat goes
|
||||
// stale (and back online when it resumes), notifying admins on each
|
||||
// transition. Heartbeats keep the row fresh via UpdateAgentHeartbeat.
|
||||
func StartAgentHealthSweep(ctx context.Context, db *database.DB) {
|
||||
const staleAfter = 3 * time.Minute
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
reconcileAgentHealth(db, staleAfter)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
type agentHealthRow struct {
|
||||
id string
|
||||
name string
|
||||
status string
|
||||
stale bool
|
||||
autoPrune bool
|
||||
}
|
||||
|
||||
func reconcileAgentHealth(db *database.DB, staleAfter time.Duration) {
|
||||
cutoff := time.Now().Add(-staleAfter)
|
||||
var agents []agentHealthRow
|
||||
q := sqlcdb.New(db.DB)
|
||||
agentRows, err := q.ListAgentHealthRows(context.Background(), sql.NullTime{Time: cutoff, Valid: true})
|
||||
if err != nil {
|
||||
log.Printf("containr: agent health sweep: %v", err)
|
||||
return
|
||||
}
|
||||
for _, r := range agentRows {
|
||||
agents = append(agents, agentHealthRow{
|
||||
id: r.ID, name: r.Name, status: r.Status, stale: r.Stale, autoPrune: r.AutoPrune,
|
||||
})
|
||||
}
|
||||
|
||||
for _, a := range agents {
|
||||
switch {
|
||||
case a.stale && a.status != "offline":
|
||||
if err := q.SetAgentStatus(context.Background(), sqlcdb.SetAgentStatusParams{
|
||||
Status: sql.NullString{String: "offline", Valid: true}, ID: a.id,
|
||||
}); err != nil {
|
||||
continue
|
||||
}
|
||||
notifyAdmins(db, "agent", "Node agent offline",
|
||||
fmt.Sprintf("Agent %s missed its heartbeat for over %s.", a.name, staleAfter), "agent", a.id)
|
||||
case !a.stale && a.status == "offline":
|
||||
if err := q.SetAgentStatus(context.Background(), sqlcdb.SetAgentStatusParams{
|
||||
Status: sql.NullString{String: "online", Valid: true}, ID: a.id,
|
||||
}); err != nil {
|
||||
continue
|
||||
}
|
||||
notifyAdmins(db, "agent", "Node agent back online",
|
||||
fmt.Sprintf("Agent %s resumed heartbeating.", a.name), "agent", a.id)
|
||||
}
|
||||
}
|
||||
|
||||
scheduleAutoPrune(db, agents)
|
||||
}
|
||||
|
||||
// scheduleAutoPrune enqueues a `prune` command once per day for agents
|
||||
// with auto_prune enabled. Disk pressure is the most common small-VPS
|
||||
// failure — this is the dflow setServerAutoCleanup analog.
|
||||
func scheduleAutoPrune(db *database.DB, agents []agentHealthRow) {
|
||||
q := sqlcdb.New(db.DB)
|
||||
for _, a := range agents {
|
||||
if !a.autoPrune || a.stale {
|
||||
continue
|
||||
}
|
||||
last, err := q.GetLastAgentCommandByType(context.Background(),
|
||||
sqlcdb.GetLastAgentCommandByTypeParams{NodeAgentID: a.id, Type: "prune"})
|
||||
if err == nil && last.CreatedAt.Valid && time.Since(last.CreatedAt.Time) < 24*time.Hour {
|
||||
continue
|
||||
}
|
||||
if _, err := q.CreateCommand(context.Background(), sqlcdb.CreateCommandParams{
|
||||
ID: uuid.New().String(),
|
||||
Type: "prune",
|
||||
NodeAgentID: a.id,
|
||||
}); err != nil {
|
||||
log.Printf("containr: auto-prune enqueue failed for agent %s: %v", a.id, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// StartUpgradeCheck polls GitHub for the latest release once a day and
|
||||
// notifies admins when it is newer than the running version. The last
|
||||
// notified tag is persisted in app_settings so restarts don't re-fire.
|
||||
func StartUpgradeCheck(ctx context.Context, db *database.DB, currentVersion string) {
|
||||
if currentVersion == "" || currentVersion == "dev" {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
checkForUpgrade(db, currentVersion)
|
||||
ticker := time.NewTicker(24 * time.Hour)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
checkForUpgrade(db, currentVersion)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
func checkForUpgrade(db *database.DB, currentVersion string) {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet,
|
||||
"https://api.github.com/repos/Dvorinka/Containr/releases/latest", nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
req.Header.Set("Accept", "application/vnd.github+json")
|
||||
req.Header.Set("User-Agent", "containr-upgrade-check")
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return
|
||||
}
|
||||
var release struct {
|
||||
TagName string `json:"tag_name"`
|
||||
HTMLURL string `json:"html_url"`
|
||||
}
|
||||
if err := json.NewDecoder(resp.Body).Decode(&release); err != nil || release.TagName == "" {
|
||||
return
|
||||
}
|
||||
if !semverNewer(release.TagName, currentVersion) {
|
||||
return
|
||||
}
|
||||
|
||||
lastNotified, _ := sqlcdb.New(db.DB).GetAppSetting(context.Background(), "upgrade_notified_tag")
|
||||
if lastNotified == release.TagName {
|
||||
return
|
||||
}
|
||||
notifyAdmins(db, "upgrade", "Containr update available",
|
||||
fmt.Sprintf("Release %s is available (running %s). %s", release.TagName, currentVersion, release.HTMLURL),
|
||||
"release", release.TagName)
|
||||
_ = sqlcdb.New(db.DB).UpsertAppSettingValue(context.Background(), sqlcdb.UpsertAppSettingValueParams{
|
||||
Key: "upgrade_notified_tag",
|
||||
Value: release.TagName,
|
||||
})
|
||||
}
|
||||
|
||||
// semverNewer reports whether candidate is a higher semver than current.
|
||||
// Non-semver input (e.g. "dev", commit hashes) compares false.
|
||||
func semverNewer(candidate, current string) bool {
|
||||
parse := func(s string) ([]int, bool) {
|
||||
s = strings.TrimPrefix(strings.TrimSpace(s), "v")
|
||||
// Prerelease/build metadata sorts after the same base version
|
||||
// upstream — for "is a newer release available" purposes the base
|
||||
// comparison is what matters.
|
||||
if j := strings.IndexAny(s, "-+"); j >= 0 {
|
||||
s = s[:j]
|
||||
}
|
||||
parts := strings.Split(s, ".")
|
||||
if len(parts) == 0 || len(parts) > 3 {
|
||||
return nil, false
|
||||
}
|
||||
out := make([]int, 3)
|
||||
for i, p := range parts {
|
||||
n, err := strconv.Atoi(p)
|
||||
if err != nil {
|
||||
return nil, false
|
||||
}
|
||||
out[i] = n
|
||||
}
|
||||
return out, true
|
||||
}
|
||||
c, ok1 := parse(candidate)
|
||||
cur, ok2 := parse(current)
|
||||
if !ok1 || !ok2 {
|
||||
return false
|
||||
}
|
||||
for i := 0; i < 3; i++ {
|
||||
if c[i] != cur[i] {
|
||||
return c[i] > cur[i]
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
package api
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestSemverNewer(t *testing.T) {
|
||||
cases := []struct {
|
||||
candidate, current string
|
||||
want bool
|
||||
}{
|
||||
{"v1.2.3", "v1.2.2", true},
|
||||
{"1.3.0", "v1.2.9", true},
|
||||
{"v2.0.0", "v1.9.9", true},
|
||||
{"v1.2.3", "v1.2.3", false},
|
||||
{"v1.2.0", "v1.2.3", false},
|
||||
{"v1.10.0", "v1.9.9", true}, // numeric, not lexicographic
|
||||
{"dev", "v1.0.0", false},
|
||||
{"v1.0.0", "main", false},
|
||||
{"v1.2.3-rc.1", "v1.2.2", true}, // prerelease suffix tolerated
|
||||
{"", "v1.0.0", false},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
if got := semverNewer(tc.candidate, tc.current); got != tc.want {
|
||||
t.Errorf("semverNewer(%q, %q) = %v, want %v", tc.candidate, tc.current, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@ func insertUserNotification(db *database.DB, userID, kind, title, body, resource
|
||||
ResourceID: sql.NullString{String: resourceID, Valid: resourceID != ""},
|
||||
CreatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
})
|
||||
go fanoutUserPush(db, userID, kind, title, body)
|
||||
}
|
||||
|
||||
func mapNotification(row sqlcdb.Notification) NotificationItem {
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployqueue"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// GET /operations — a single cross-cutting view of platform work: active
|
||||
// and recent deployments, the per-service deploy queue, recent cron runs,
|
||||
// and recent backups. Scoped to the caller's projects (admins see all).
|
||||
|
||||
type operationDeployment struct {
|
||||
ID string `json:"id"`
|
||||
ServiceID string `json:"service_id"`
|
||||
ServiceName string `json:"service_name"`
|
||||
ProjectName string `json:"project_name"`
|
||||
Status string `json:"status"`
|
||||
Image string `json:"image"`
|
||||
Error *string `json:"error,omitempty"`
|
||||
StartedAt *time.Time `json:"started_at,omitempty"`
|
||||
CompletedAt *time.Time `json:"completed_at,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
}
|
||||
|
||||
type operationCronRun struct {
|
||||
ID string `json:"id"`
|
||||
JobName string `json:"job_name"`
|
||||
Schedule string `json:"schedule"`
|
||||
Status string `json:"status"`
|
||||
StartedAt time.Time `json:"started_at"`
|
||||
FinishedAt *time.Time `json:"finished_at,omitempty"`
|
||||
Error *string `json:"error,omitempty"`
|
||||
}
|
||||
|
||||
type operationBackup struct {
|
||||
ID string `json:"id"`
|
||||
DatabaseID string `json:"database_id"`
|
||||
DBName string `json:"database_name"`
|
||||
Status string `json:"status"`
|
||||
Size string `json:"size"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
Completed *time.Time `json:"completed_at,omitempty"`
|
||||
}
|
||||
|
||||
func handleGetOperations(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
isAdmin := contextIsAdmin(c)
|
||||
|
||||
userUUID, _ := uuid.Parse(userID)
|
||||
q := sqlcdb.New(db.DB)
|
||||
visibility := sqlcdb.ListActiveOperationDeploymentsParams{OwnerID: userUUID, Column2: isAdmin}
|
||||
|
||||
active := []operationDeployment{}
|
||||
if rows, err := q.ListActiveOperationDeployments(context.Background(), visibility); err == nil {
|
||||
for _, r := range rows {
|
||||
active = append(active, operationDeployment{
|
||||
ID: r.ID.String(), ServiceID: r.ServiceID.String(), ServiceName: r.ServiceName,
|
||||
ProjectName: r.ProjectName, Status: r.Status.String, Image: r.Image,
|
||||
Error: strPtrNS(r.Error), StartedAt: timePtrNT(r.StartedAt),
|
||||
CompletedAt: timePtrNT(r.CompletedAt), CreatedAt: r.CreatedAt.Time,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
recentFailed := []operationDeployment{}
|
||||
if rows, err := q.ListRecentFailedDeployments(context.Background(), sqlcdb.ListRecentFailedDeploymentsParams{
|
||||
OwnerID: userUUID, Column2: isAdmin,
|
||||
}); err == nil {
|
||||
for _, r := range rows {
|
||||
recentFailed = append(recentFailed, operationDeployment{
|
||||
ID: r.ID.String(), ServiceID: r.ServiceID.String(), ServiceName: r.ServiceName,
|
||||
ProjectName: r.ProjectName, Status: r.Status.String, Image: r.Image,
|
||||
Error: strPtrNS(r.Error), StartedAt: timePtrNT(r.StartedAt),
|
||||
CompletedAt: timePtrNT(r.CompletedAt), CreatedAt: r.CreatedAt.Time,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
cronRuns := []operationCronRun{}
|
||||
if rows, err := q.ListRecentCronRuns(context.Background(), sqlcdb.ListRecentCronRunsParams{
|
||||
OwnerID: userUUID, Column2: isAdmin,
|
||||
}); err == nil {
|
||||
for _, r := range rows {
|
||||
cronRuns = append(cronRuns, operationCronRun{
|
||||
ID: r.ID.String(), JobName: r.JobName, Schedule: r.Schedule, Status: r.Status.String,
|
||||
StartedAt: r.StartedAt, FinishedAt: timePtrNT(r.FinishedAt), Error: strPtrNS(r.Error),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
backups := []operationBackup{}
|
||||
if rows, err := q.ListRecentOperationBackups(context.Background(), sqlcdb.ListRecentOperationBackupsParams{
|
||||
UserID: userID, Column2: isAdmin,
|
||||
}); err == nil {
|
||||
for _, r := range rows {
|
||||
backups = append(backups, operationBackup{
|
||||
ID: r.ID, DatabaseID: r.DatabaseID, DBName: r.DbName, Status: r.Status,
|
||||
Size: r.Size, CreatedAt: r.CreatedAt.Time, Completed: timePtrNT(r.CompletedAt),
|
||||
})
|
||||
}
|
||||
}
|
||||
var queue []deployqueue.ServiceQueueState
|
||||
if queueValue, exists := c.Get("deploy_queue"); exists && queueValue != nil {
|
||||
queue = queueValue.(*deployqueue.Queue).Snapshot()
|
||||
}
|
||||
if queue == nil {
|
||||
queue = []deployqueue.ServiceQueueState{}
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"active_deployments": active,
|
||||
"recent_failures": recentFailed,
|
||||
"cron_runs": cronRuns,
|
||||
"backups": backups,
|
||||
"deploy_queue": queue,
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// pageWindow parses pagination from either ?page=&limit= (legacy) or
|
||||
// ?cursor=&limit= (agent-friendly). The cursor is an opaque base64 offset —
|
||||
// cheap to implement over the existing offset queries while the public
|
||||
// contract stays cursor-shaped for clients.
|
||||
func pageWindow(c *gin.Context, defaultLimit, maxLimit int) (offset, limit int) {
|
||||
limit = defaultLimit
|
||||
if v := c.Query("limit"); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil && n > 0 && n <= maxLimit {
|
||||
limit = n
|
||||
}
|
||||
}
|
||||
if cur := c.Query("cursor"); cur != "" {
|
||||
if raw, err := base64.RawURLEncoding.DecodeString(strings.TrimPrefix(cur, "o_")); err == nil {
|
||||
if n, err := strconv.Atoi(string(raw)); err == nil && n >= 0 {
|
||||
return n, limit
|
||||
}
|
||||
}
|
||||
return 0, limit // invalid cursor → first page, fail soft
|
||||
}
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
return (page - 1) * limit, limit
|
||||
}
|
||||
|
||||
// paginationMeta renders the standard envelope: legacy page fields plus
|
||||
// next_cursor when another page exists.
|
||||
func paginationMeta(offset, limit, total int) gin.H {
|
||||
page := offset/limit + 1
|
||||
meta := gin.H{
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": total,
|
||||
"pages": (total + limit - 1) / limit,
|
||||
}
|
||||
if offset+limit < total {
|
||||
meta["next_cursor"] = "o_" + base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprint(offset+limit)))
|
||||
}
|
||||
return meta
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func ctxWithQuery(q string) *gin.Context {
|
||||
gin.SetMode(gin.TestMode)
|
||||
c, _ := gin.CreateTestContext(httptest.NewRecorder())
|
||||
c.Request = httptest.NewRequest("GET", "/x?"+q, nil)
|
||||
return c
|
||||
}
|
||||
|
||||
func TestPageWindowDefaults(t *testing.T) {
|
||||
offset, limit := pageWindow(ctxWithQuery(""), 10, 100)
|
||||
if offset != 0 || limit != 10 {
|
||||
t.Fatalf("expected 0/10, got %d/%d", offset, limit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageWindowLegacyPage(t *testing.T) {
|
||||
offset, limit := pageWindow(ctxWithQuery("page=3&limit=20"), 10, 100)
|
||||
if offset != 40 || limit != 20 {
|
||||
t.Fatalf("expected 40/20, got %d/%d", offset, limit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageWindowCursor(t *testing.T) {
|
||||
// cursor for offset 50 produced by paginationMeta
|
||||
meta := paginationMeta(0, 50, 200)
|
||||
cursor := meta["next_cursor"].(string)
|
||||
offset, limit := pageWindow(ctxWithQuery("cursor="+cursor+"&limit=50"), 10, 100)
|
||||
if offset != 50 || limit != 50 {
|
||||
t.Fatalf("expected 50/50, got %d/%d", offset, limit)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPageWindowInvalidCursorFailsSoft(t *testing.T) {
|
||||
offset, _ := pageWindow(ctxWithQuery("cursor=garbage"), 10, 100)
|
||||
if offset != 0 {
|
||||
t.Fatalf("expected offset 0 on invalid cursor, got %d", offset)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaginationMetaNextCursorOnlyWhenMore(t *testing.T) {
|
||||
if _, ok := paginationMeta(0, 10, 10)["next_cursor"]; ok {
|
||||
t.Fatal("no next page should omit next_cursor")
|
||||
}
|
||||
if _, ok := paginationMeta(0, 10, 25)["next_cursor"]; !ok {
|
||||
t.Fatal("expected next_cursor when more pages exist")
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,12 @@ package api
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/deployqueue"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -26,11 +31,77 @@ type PreviewEnvironment struct {
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
|
||||
// PreviewServiceID is the cloned service this preview actually deploys;
|
||||
// its status is the source of truth while the preview is active.
|
||||
PreviewServiceID *uuid.UUID `json:"preview_service_id,omitempty" db:"preview_service_id"`
|
||||
|
||||
// Related data
|
||||
Service *Service `json:"service,omitempty"`
|
||||
DeploymentID *uuid.UUID `json:"deployment_id,omitempty"`
|
||||
}
|
||||
|
||||
// previewRowShape is the common column set the generated preview rows
|
||||
// carry — ListPreviewEnvironmentsForProjectRow and GetPreviewEnvironmentRow
|
||||
// share it.
|
||||
type previewRowShape struct {
|
||||
ID uuid.UUID
|
||||
ProjectID uuid.UUID
|
||||
ServiceID uuid.UUID
|
||||
PreviewServiceID uuid.NullUUID
|
||||
BranchName string
|
||||
PrNumber sql.NullInt32
|
||||
Environment string
|
||||
Status string
|
||||
Url sql.NullString
|
||||
ExpiresAt time.Time
|
||||
CreatedAt sql.NullTime
|
||||
UpdatedAt sql.NullTime
|
||||
SvcID uuid.NullUUID
|
||||
ServiceName sql.NullString
|
||||
ServiceType sql.NullString
|
||||
}
|
||||
|
||||
func previewFromRow(r previewRowShape) PreviewEnvironment {
|
||||
env := PreviewEnvironment{
|
||||
ID: r.ID,
|
||||
ProjectID: r.ProjectID,
|
||||
ServiceID: r.ServiceID,
|
||||
BranchName: r.BranchName,
|
||||
Environment: r.Environment,
|
||||
Status: r.Status,
|
||||
URL: r.Url.String,
|
||||
CreatedAt: r.CreatedAt.Time,
|
||||
UpdatedAt: r.UpdatedAt.Time,
|
||||
}
|
||||
if r.PreviewServiceID.Valid {
|
||||
id := r.PreviewServiceID.UUID
|
||||
env.PreviewServiceID = &id
|
||||
}
|
||||
if r.PrNumber.Valid {
|
||||
n := int(r.PrNumber.Int32)
|
||||
env.PRNumber = &n
|
||||
}
|
||||
if !r.ExpiresAt.IsZero() {
|
||||
t := r.ExpiresAt
|
||||
env.ExpiresAt = &t
|
||||
}
|
||||
if r.SvcID.Valid {
|
||||
env.Service = &Service{
|
||||
ID: r.SvcID.UUID,
|
||||
Name: r.ServiceName.String,
|
||||
Type: r.ServiceType.String,
|
||||
}
|
||||
}
|
||||
return env
|
||||
}
|
||||
|
||||
func prNumberSQL(n *int) sql.NullInt32 {
|
||||
if n == nil {
|
||||
return sql.NullInt32{}
|
||||
}
|
||||
return sql.NullInt32{Int32: int32(*n), Valid: true}
|
||||
}
|
||||
|
||||
// CreatePreviewEnvironmentRequest represents a request to create a preview environment
|
||||
type CreatePreviewEnvironmentRequest struct {
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
@@ -77,51 +148,21 @@ func handleGetPreviewEnvironments(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Get preview environments for the project with service info
|
||||
rows, err := db.(*database.DB).Query(
|
||||
`SELECT pe.id, pe.project_id, pe.service_id, pe.branch_name, pe.pr_number,
|
||||
pe.environment, pe.status, pe.url, pe.expires_at, pe.created_at, pe.updated_at,
|
||||
s.id as service_id, s.name as service_name, s.type as service_type
|
||||
FROM preview_environments pe
|
||||
LEFT JOIN services s ON pe.service_id = s.id
|
||||
WHERE pe.project_id = $1
|
||||
ORDER BY pe.created_at DESC`,
|
||||
projectID,
|
||||
)
|
||||
rows, err := sqlcdb.New(db.(*database.DB).DB).ListPreviewEnvironmentsForProject(c.Request.Context(), projectID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve preview environments"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var environments []PreviewEnvironment
|
||||
for rows.Next() {
|
||||
var env PreviewEnvironment
|
||||
var serviceID sql.NullString
|
||||
var serviceName sql.NullString
|
||||
var serviceType sql.NullString
|
||||
|
||||
err := rows.Scan(
|
||||
&env.ID, &env.ProjectID, &env.ServiceID, &env.BranchName, &env.PRNumber,
|
||||
&env.Environment, &env.Status, &env.URL, &env.ExpiresAt, &env.CreatedAt, &env.UpdatedAt,
|
||||
&serviceID, &serviceName, &serviceType,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan preview environment"})
|
||||
return
|
||||
}
|
||||
|
||||
if serviceID.Valid {
|
||||
parsedServiceID, parseErr := uuid.Parse(serviceID.String)
|
||||
if parseErr == nil {
|
||||
env.Service = &Service{
|
||||
ID: parsedServiceID,
|
||||
Name: serviceName.String,
|
||||
Type: serviceType.String,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
environments = append(environments, env)
|
||||
environments := make([]PreviewEnvironment, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
environments = append(environments, previewFromRow(previewRowShape{
|
||||
ID: r.ID, ProjectID: r.ProjectID, ServiceID: r.ServiceID,
|
||||
PreviewServiceID: r.PreviewServiceID, BranchName: r.BranchName,
|
||||
PrNumber: r.PrNumber, Environment: r.Environment, Status: r.Status,
|
||||
Url: r.Url, ExpiresAt: r.ExpiresAt, CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
|
||||
SvcID: r.SvcID, ServiceName: r.ServiceName, ServiceType: r.ServiceType,
|
||||
}))
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"preview_environments": environments})
|
||||
@@ -162,48 +203,37 @@ func handleCreatePreviewEnvironment(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if project exists and user has access
|
||||
var project Project
|
||||
err = db.(*database.DB).QueryRow(
|
||||
"SELECT id, name, owner_id FROM projects WHERE id = $1",
|
||||
req.ProjectID,
|
||||
).Scan(&project.ID, &project.Name, &project.OwnerID)
|
||||
ctx := c.Request.Context()
|
||||
q := sqlcdb.New(db.(*database.DB).DB)
|
||||
|
||||
// Check if project exists and user has access
|
||||
project, err := q.GetProjectBrief(ctx, req.ProjectID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Project not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// Check if user owns the project
|
||||
if project.OwnerID != userID.(string) && !contextIsAdmin(c) {
|
||||
if project.OwnerID.String() != userID.(string) && !contextIsAdmin(c) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
|
||||
// Check if service exists and belongs to the project
|
||||
var service Service
|
||||
err = db.(*database.DB).QueryRow(
|
||||
"SELECT id, name, COALESCE(type, service_type, '') FROM services WHERE id = $1 AND project_id = $2",
|
||||
req.ServiceID, req.ProjectID,
|
||||
).Scan(&service.ID, &service.Name, &service.Type)
|
||||
|
||||
service, err := q.GetServiceTypeBrief(ctx, sqlcdb.GetServiceTypeBriefParams{
|
||||
ID: req.ServiceID, ProjectID: req.ProjectID})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found or doesn't belong to this project"})
|
||||
return
|
||||
}
|
||||
|
||||
// Check if preview environment already exists for this branch and service
|
||||
var count int
|
||||
err = db.(*database.DB).QueryRow(
|
||||
"SELECT COUNT(*) FROM preview_environments WHERE service_id = $1 AND branch_name = $2 AND status NOT IN ('expired', 'stopped')",
|
||||
req.ServiceID, req.BranchName,
|
||||
).Scan(&count)
|
||||
|
||||
count, err := q.CountActivePreviewsForBranch(ctx, sqlcdb.CountActivePreviewsForBranchParams{
|
||||
ServiceID: req.ServiceID, BranchName: req.BranchName})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to check existing preview environment"})
|
||||
return
|
||||
}
|
||||
|
||||
if count > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Preview environment already exists for this branch and service"})
|
||||
return
|
||||
@@ -215,87 +245,111 @@ func handleCreatePreviewEnvironment(c *gin.Context) {
|
||||
ttlHours = 24 // Default 24 hours
|
||||
}
|
||||
|
||||
// Create preview environment
|
||||
// A preview is a real service: clone the source, pin it to the
|
||||
// requested branch, swap its domains for a preview hostname, and run a
|
||||
// normal deployment through the queue. Expiry sweeps the clone away.
|
||||
envName := generatePreviewEnvironmentName(req.BranchName)
|
||||
cloneName := previewServiceName(service.Name, req.BranchName)
|
||||
cloneID, err := cloneServiceRow(db.(*database.DB), req.ServiceID, req.ProjectID, cloneName, "")
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clone service for preview"})
|
||||
return
|
||||
}
|
||||
|
||||
// The clone's branch override is what makes it a preview — everything
|
||||
// else (env vars, volumes, builder, resources) stays identical.
|
||||
if err := q.UpdateServicePreviewBranch(ctx, sqlcdb.UpdateServicePreviewBranchParams{
|
||||
GitBranch: sql.NullString{String: req.BranchName, Valid: true}, ID: cloneID}); err != nil {
|
||||
_ = q.DeleteServiceByID(ctx, cloneID)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to configure preview service"})
|
||||
return
|
||||
}
|
||||
// Cloned service_domains would collide with the parent's Traefik
|
||||
// router — previews get their own hostname or none.
|
||||
if err := q.DeleteAllServiceDomains(ctx, cloneID); err != nil {
|
||||
_ = q.DeleteServiceByID(ctx, cloneID)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to configure preview domains"})
|
||||
return
|
||||
}
|
||||
previewHost := resolvePreviewHost(db.(*database.DB), cloneID, envName)
|
||||
if previewHost != "" {
|
||||
_ = q.CreatePreviewDomain(ctx, sqlcdb.CreatePreviewDomainParams{
|
||||
ServiceID: cloneID, Domain: previewHost})
|
||||
}
|
||||
|
||||
expiresAt := time.Now().Add(time.Duration(ttlHours) * time.Hour)
|
||||
env := PreviewEnvironment{
|
||||
ID: uuid.New(),
|
||||
ProjectID: req.ProjectID,
|
||||
ServiceID: req.ServiceID,
|
||||
BranchName: req.BranchName,
|
||||
PRNumber: req.PRNumber,
|
||||
Environment: generatePreviewEnvironmentName(req.BranchName),
|
||||
Environment: envName,
|
||||
Status: "building",
|
||||
ExpiresAt: &[]time.Time{time.Now().Add(time.Duration(ttlHours) * time.Hour)}[0],
|
||||
ExpiresAt: &expiresAt,
|
||||
CreatedAt: time.Now(),
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
if previewHost != "" {
|
||||
env.URL = "https://" + previewHost
|
||||
}
|
||||
|
||||
// Insert preview environment into database
|
||||
_, err = db.(*database.DB).Exec(
|
||||
`INSERT INTO preview_environments
|
||||
(id, project_id, service_id, branch_name, pr_number, environment,
|
||||
status, url, expires_at, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`,
|
||||
env.ID, env.ProjectID, env.ServiceID, env.BranchName, env.PRNumber,
|
||||
env.Environment, env.Status, env.URL, env.ExpiresAt, env.CreatedAt, env.UpdatedAt,
|
||||
)
|
||||
|
||||
err = q.CreatePreviewEnvironment(ctx, sqlcdb.CreatePreviewEnvironmentParams{
|
||||
ID: env.ID,
|
||||
ProjectID: env.ProjectID,
|
||||
ServiceID: env.ServiceID,
|
||||
PreviewServiceID: uuid.NullUUID{UUID: cloneID, Valid: true},
|
||||
BranchName: env.BranchName,
|
||||
PrNumber: prNumberSQL(env.PRNumber),
|
||||
Environment: env.Environment,
|
||||
Status: env.Status,
|
||||
Url: sql.NullString{String: env.URL, Valid: env.URL != ""},
|
||||
ExpiresAt: *env.ExpiresAt,
|
||||
CreatedAt: sql.NullTime{Time: env.CreatedAt, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: env.UpdatedAt, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
_ = q.DeleteServiceByID(ctx, cloneID)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create preview environment"})
|
||||
return
|
||||
}
|
||||
|
||||
deploymentID := uuid.New()
|
||||
sanitizedBranch := strings.ReplaceAll(req.BranchName, "/", "-")
|
||||
sanitizedBranch = strings.ReplaceAll(sanitizedBranch, "_", "-")
|
||||
version := fmt.Sprintf("preview-%s-%d", sanitizedBranch, time.Now().Unix())
|
||||
startedAt := time.Now().UTC()
|
||||
completedAt := startedAt
|
||||
|
||||
_, deployErr := db.(*database.DB).Exec(
|
||||
`INSERT INTO deployments
|
||||
(id, service_id, version, commit_hash, status, started_at, completed_at, deployment_log, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, NOW(), NOW())`,
|
||||
deploymentID,
|
||||
req.ServiceID,
|
||||
version,
|
||||
req.BranchName,
|
||||
"running",
|
||||
startedAt,
|
||||
completedAt,
|
||||
fmt.Sprintf("Preview environment %s activated for branch %s", env.Environment, req.BranchName),
|
||||
)
|
||||
if deployErr != nil {
|
||||
_, _ = db.(*database.DB).Exec(
|
||||
`UPDATE preview_environments
|
||||
SET status = 'failed', updated_at = NOW()
|
||||
WHERE id = $1`,
|
||||
env.ID,
|
||||
)
|
||||
err = q.InsertPendingDeployment(ctx, sqlcdb.InsertPendingDeploymentParams{
|
||||
ID: deploymentID,
|
||||
ServiceID: cloneID,
|
||||
Version: "preview-" + env.Environment,
|
||||
CommitHash: sql.NullString{String: req.BranchName, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
_ = q.DeletePreviewEnvironment(ctx, env.ID)
|
||||
_ = q.DeleteServiceByID(ctx, cloneID)
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to provision preview deployment"})
|
||||
return
|
||||
}
|
||||
|
||||
if strings.TrimSpace(env.URL) == "" {
|
||||
env.URL = fmt.Sprintf("https://%s.preview.containr.local", env.Environment)
|
||||
}
|
||||
env.Status = "running"
|
||||
env.UpdatedAt = time.Now().UTC()
|
||||
env.DeploymentID = &deploymentID
|
||||
|
||||
_, err = db.(*database.DB).Exec(
|
||||
`UPDATE preview_environments
|
||||
SET status = $1, url = $2, updated_at = $3
|
||||
WHERE id = $4`,
|
||||
env.Status,
|
||||
env.URL,
|
||||
env.UpdatedAt,
|
||||
env.ID,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to finalize preview environment"})
|
||||
return
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
if engine, ok := engineValue.(*deployment.DeploymentEngine); ok {
|
||||
cloneService, loadErr := loadServiceForDeploy(db.(*database.DB), cloneID)
|
||||
if loadErr == nil {
|
||||
engineInstance := engine
|
||||
d := DeploymentModel{ID: deploymentID, ServiceID: cloneID, Status: "pending", CreatedAt: time.Now(), UpdatedAt: time.Now()}
|
||||
getDeployQueue(c).Enqueue(cloneID, deployqueue.Job{
|
||||
DeploymentID: deploymentID,
|
||||
Run: func(jctx context.Context) {
|
||||
runDeploymentAndSync(jctx, db.(*database.DB), engineInstance, &d, cloneService, CreateDeploymentRequest{Branch: req.BranchName, Trigger: "preview"}, userID.(string))
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "service", req.ServiceID.String(), "preview.create", map[string]interface{}{
|
||||
"preview_environment_id": env.ID.String(),
|
||||
"preview_service_id": cloneID.String(),
|
||||
"branch": req.BranchName,
|
||||
})
|
||||
c.JSON(http.StatusCreated, gin.H{"preview_environment": env})
|
||||
}
|
||||
|
||||
@@ -316,43 +370,20 @@ func handleGetPreviewEnvironment(c *gin.Context) {
|
||||
|
||||
// Public when the parent project is approved; otherwise owner/member/admin.
|
||||
userID := optionalUserUUID(c)
|
||||
var env PreviewEnvironment
|
||||
var serviceID sql.NullString
|
||||
var serviceName sql.NullString
|
||||
var serviceType sql.NullString
|
||||
err = db.(*database.DB).QueryRow(
|
||||
`SELECT pe.id, pe.project_id, pe.service_id, pe.branch_name, pe.pr_number,
|
||||
pe.environment, pe.status, pe.url, pe.expires_at, pe.created_at, pe.updated_at,
|
||||
s.id as service_id, s.name as service_name, s.type as service_type
|
||||
FROM preview_environments pe
|
||||
LEFT JOIN services s ON pe.service_id = s.id
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1 AND (p.is_approved OR p.owner_id = $2 OR $3::bool
|
||||
OR EXISTS (SELECT 1 FROM project_members pm WHERE pm.project_id = p.id AND pm.user_id = $2))`,
|
||||
envID, userID, contextIsAdmin(c),
|
||||
).Scan(
|
||||
&env.ID, &env.ProjectID, &env.ServiceID, &env.BranchName, &env.PRNumber,
|
||||
&env.Environment, &env.Status, &env.URL, &env.ExpiresAt, &env.CreatedAt, &env.UpdatedAt,
|
||||
&serviceID, &serviceName, &serviceType,
|
||||
)
|
||||
|
||||
r, err := sqlcdb.New(db.(*database.DB).DB).GetPreviewEnvironment(
|
||||
c.Request.Context(), sqlcdb.GetPreviewEnvironmentParams{
|
||||
ID: envID, OwnerID: userID, Column3: contextIsAdmin(c)})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Preview environment not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// Populate service info if available
|
||||
if serviceID.Valid {
|
||||
parsedServiceID, parseErr := uuid.Parse(serviceID.String)
|
||||
if parseErr == nil {
|
||||
env.Service = &Service{
|
||||
ID: parsedServiceID,
|
||||
Name: serviceName.String,
|
||||
Type: serviceType.String,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
env := previewFromRow(previewRowShape{
|
||||
ID: r.ID, ProjectID: r.ProjectID, ServiceID: r.ServiceID,
|
||||
PreviewServiceID: r.PreviewServiceID, BranchName: r.BranchName,
|
||||
PrNumber: r.PrNumber, Environment: r.Environment, Status: r.Status,
|
||||
Url: r.Url, ExpiresAt: r.ExpiresAt, CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
|
||||
SvcID: r.SvcID, ServiceName: r.ServiceName, ServiceType: r.ServiceType,
|
||||
})
|
||||
c.JSON(http.StatusOK, gin.H{"preview_environment": env})
|
||||
}
|
||||
|
||||
@@ -385,24 +416,21 @@ func handleUpdatePreviewEnvironment(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Check if preview environment exists and user has access
|
||||
var existingEnv PreviewEnvironment
|
||||
err = db.(*database.DB).QueryRow(
|
||||
`SELECT pe.id, pe.project_id, pe.service_id, pe.branch_name, pe.pr_number,
|
||||
pe.environment, pe.status, pe.url, pe.expires_at, pe.created_at, pe.updated_at
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1 AND (p.owner_id = $2 OR $3::bool)`,
|
||||
envID, userID, contextIsAdmin(c),
|
||||
).Scan(
|
||||
&existingEnv.ID, &existingEnv.ProjectID, &existingEnv.ServiceID, &existingEnv.BranchName,
|
||||
&existingEnv.PRNumber, &existingEnv.Environment, &existingEnv.Status, &existingEnv.URL,
|
||||
&existingEnv.ExpiresAt, &existingEnv.CreatedAt, &existingEnv.UpdatedAt,
|
||||
)
|
||||
|
||||
uid, _ := uuid.Parse(fmt.Sprint(userID))
|
||||
q := sqlcdb.New(db.(*database.DB).DB)
|
||||
r, err := q.GetPreviewEnvironmentForWrite(
|
||||
c.Request.Context(), sqlcdb.GetPreviewEnvironmentForWriteParams{
|
||||
ID: envID, OwnerID: uid, Column3: contextIsAdmin(c)})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Preview environment not found"})
|
||||
return
|
||||
}
|
||||
existingEnv := previewFromRow(previewRowShape{
|
||||
ID: r.ID, ProjectID: r.ProjectID, ServiceID: r.ServiceID,
|
||||
BranchName: r.BranchName, PrNumber: r.PrNumber, Environment: r.Environment,
|
||||
Status: r.Status, Url: r.Url, ExpiresAt: r.ExpiresAt,
|
||||
CreatedAt: r.CreatedAt, UpdatedAt: r.UpdatedAt,
|
||||
})
|
||||
|
||||
// Update fields if provided
|
||||
if req.Status != "" {
|
||||
@@ -422,12 +450,17 @@ func handleUpdatePreviewEnvironment(c *gin.Context) {
|
||||
existingEnv.UpdatedAt = time.Now()
|
||||
|
||||
// Update preview environment in database
|
||||
_, err = db.(*database.DB).Exec(
|
||||
`UPDATE preview_environments
|
||||
SET status = $1, url = $2, expires_at = $3, updated_at = $4
|
||||
WHERE id = $5`,
|
||||
existingEnv.Status, existingEnv.URL, existingEnv.ExpiresAt, existingEnv.UpdatedAt, existingEnv.ID,
|
||||
)
|
||||
expiresAt := time.Time{}
|
||||
if existingEnv.ExpiresAt != nil {
|
||||
expiresAt = *existingEnv.ExpiresAt
|
||||
}
|
||||
err = q.UpdatePreviewEnvironment(c.Request.Context(), sqlcdb.UpdatePreviewEnvironmentParams{
|
||||
Status: existingEnv.Status,
|
||||
Url: sql.NullString{String: existingEnv.URL, Valid: existingEnv.URL != ""},
|
||||
ExpiresAt: expiresAt,
|
||||
UpdatedAt: sql.NullTime{Time: existingEnv.UpdatedAt, Valid: true},
|
||||
ID: existingEnv.ID,
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update preview environment"})
|
||||
@@ -459,49 +492,34 @@ func handleDeletePreviewEnvironment(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Check if preview environment exists and user has access
|
||||
var projectOwnerID string
|
||||
err = db.(*database.DB).QueryRow(
|
||||
`SELECT p.owner_id
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1`,
|
||||
envID,
|
||||
).Scan(&projectOwnerID)
|
||||
ctx := c.Request.Context()
|
||||
q := sqlcdb.New(db.(*database.DB).DB)
|
||||
|
||||
// Check if preview environment exists and user has access
|
||||
projectOwnerID, err := q.GetPreviewEnvironmentOwner(ctx, envID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Preview environment not found"})
|
||||
return
|
||||
}
|
||||
|
||||
// Check if user owns the project
|
||||
if projectOwnerID != userID.(string) && !contextIsAdmin(c) {
|
||||
if projectOwnerID.String() != userID.(string) && !contextIsAdmin(c) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
|
||||
cleanupID := uuid.New()
|
||||
cleanupStartedAt := time.Now().UTC()
|
||||
cleanupCompletedAt := cleanupStartedAt
|
||||
|
||||
_, _ = db.(*database.DB).Exec(
|
||||
`INSERT INTO deployments
|
||||
(id, service_id, version, status, started_at, completed_at, deployment_log, created_at, updated_at)
|
||||
VALUES ($1, (SELECT service_id FROM preview_environments WHERE id = $2), $3, $4, $5, $6, $7, NOW(), NOW())`,
|
||||
cleanupID,
|
||||
envID,
|
||||
"preview-cleanup",
|
||||
"rolled_back",
|
||||
cleanupStartedAt,
|
||||
cleanupCompletedAt,
|
||||
"Preview environment resources cleaned up",
|
||||
)
|
||||
// Tear down the clone's runtime and row — the preview is the clone.
|
||||
if cloneID, err := q.GetPreviewServiceID(ctx, envID); err == nil && cloneID.Valid {
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
if engine, ok := engineValue.(*deployment.DeploymentEngine); ok {
|
||||
removeServiceRuntime(ctx, db.(*database.DB), engine, cloneID.UUID)
|
||||
}
|
||||
}
|
||||
_ = q.DeleteServiceByID(ctx, cloneID.UUID)
|
||||
}
|
||||
|
||||
// Delete preview environment
|
||||
_, err = db.(*database.DB).Exec(
|
||||
"DELETE FROM preview_environments WHERE id = $1",
|
||||
envID,
|
||||
)
|
||||
err = q.DeletePreviewEnvironment(ctx, envID)
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete preview environment"})
|
||||
@@ -539,22 +557,30 @@ func handlePromotePreviewEnvironment(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Get preview environment details
|
||||
var env PreviewEnvironment
|
||||
err = db.(*database.DB).QueryRow(
|
||||
`SELECT pe.id, pe.project_id, pe.service_id, pe.branch_name, pe.environment, pe.status
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE pe.id = $1 AND (p.owner_id = $2 OR $3::bool)`,
|
||||
envID, userID, contextIsAdmin(c),
|
||||
).Scan(
|
||||
&env.ID, &env.ProjectID, &env.ServiceID, &env.BranchName, &env.Environment, &env.Status,
|
||||
)
|
||||
ctx := c.Request.Context()
|
||||
q := sqlcdb.New(db.(*database.DB).DB)
|
||||
|
||||
// Get preview environment details
|
||||
uid, _ := uuid.Parse(fmt.Sprint(userID))
|
||||
r, err := q.GetPreviewEnvironmentForPromote(
|
||||
ctx, sqlcdb.GetPreviewEnvironmentForPromoteParams{
|
||||
ID: envID, OwnerID: uid, Column3: contextIsAdmin(c)})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Preview environment not found"})
|
||||
return
|
||||
}
|
||||
env := PreviewEnvironment{
|
||||
ID: r.ID,
|
||||
ProjectID: r.ProjectID,
|
||||
ServiceID: r.ServiceID,
|
||||
BranchName: r.BranchName,
|
||||
Environment: r.Environment,
|
||||
Status: r.Status,
|
||||
}
|
||||
if r.PreviewServiceID.Valid {
|
||||
id := r.PreviewServiceID.UUID
|
||||
env.PreviewServiceID = &id
|
||||
}
|
||||
|
||||
// Check if preview environment is in a state that can be promoted
|
||||
if env.Status != "running" {
|
||||
@@ -562,39 +588,51 @@ func handlePromotePreviewEnvironment(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Promote is a real redeploy: the source service builds the preview's
|
||||
// branch onto its own deployment track.
|
||||
deploymentID := uuid.New()
|
||||
promotionVersion := fmt.Sprintf("promote-%s-%d", strings.ReplaceAll(env.BranchName, "/", "-"), time.Now().Unix())
|
||||
_, err = db.(*database.DB).Exec(
|
||||
`INSERT INTO deployments (id, service_id, version, status, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, NOW(), NOW())`,
|
||||
deploymentID, env.ServiceID, promotionVersion, "pending",
|
||||
)
|
||||
err = q.InsertPendingDeployment(ctx, sqlcdb.InsertPendingDeploymentParams{
|
||||
ID: deploymentID,
|
||||
ServiceID: env.ServiceID,
|
||||
Version: promotionVersion,
|
||||
CommitHash: sql.NullString{String: env.BranchName, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create promotion deployment"})
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := db.(*database.DB).Exec(
|
||||
`UPDATE services
|
||||
SET environment = $1, updated_at = NOW()
|
||||
WHERE id = $2`,
|
||||
req.TargetEnvironment, env.ServiceID,
|
||||
); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update service target environment"})
|
||||
sourceService, loadErr := loadServiceForDeploy(db.(*database.DB), env.ServiceID)
|
||||
if loadErr != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load promotion target service"})
|
||||
return
|
||||
}
|
||||
|
||||
previewStatus := "stopped"
|
||||
if req.CreateBackup {
|
||||
previewStatus = "expired"
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
if engine, ok := engineValue.(*deployment.DeploymentEngine); ok {
|
||||
engineInstance := engine
|
||||
d := DeploymentModel{ID: deploymentID, ServiceID: env.ServiceID, Status: "pending", CreatedAt: time.Now(), UpdatedAt: time.Now()}
|
||||
getDeployQueue(c).Enqueue(env.ServiceID, deployqueue.Job{
|
||||
DeploymentID: deploymentID,
|
||||
Run: func(jctx context.Context) {
|
||||
runDeploymentAndSync(jctx, db.(*database.DB), engineInstance, &d, sourceService, CreateDeploymentRequest{Branch: env.BranchName, Trigger: "promote"}, userID.(string))
|
||||
},
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := db.(*database.DB).Exec(
|
||||
`UPDATE preview_environments
|
||||
SET status = $1, updated_at = NOW()
|
||||
WHERE id = $2`,
|
||||
previewStatus, env.ID,
|
||||
); err != nil {
|
||||
// Promotion retires the preview — the clone's containers and row go.
|
||||
if env.PreviewServiceID != nil {
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
if engine, ok := engineValue.(*deployment.DeploymentEngine); ok {
|
||||
removeServiceRuntime(ctx, db.(*database.DB), engine, *env.PreviewServiceID)
|
||||
}
|
||||
}
|
||||
_ = q.DeleteServiceByID(ctx, *env.PreviewServiceID)
|
||||
}
|
||||
|
||||
if err := q.MarkPreviewEnvironmentStatus(ctx, sqlcdb.MarkPreviewEnvironmentStatusParams{
|
||||
Status: "stopped", ID: env.ID}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update preview environment status"})
|
||||
return
|
||||
}
|
||||
@@ -608,7 +646,7 @@ func handlePromotePreviewEnvironment(c *gin.Context) {
|
||||
"create_backup": req.CreateBackup,
|
||||
"deployment_id": deploymentID,
|
||||
"status": "queued",
|
||||
"preview_status": previewStatus,
|
||||
"preview_status": "stopped",
|
||||
},
|
||||
})
|
||||
}
|
||||
@@ -637,44 +675,46 @@ func handleCleanupExpiredPreviewEnvironments(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
q := sqlcdb.New(db.(*database.DB).DB)
|
||||
uid, _ := uuid.Parse(fmt.Sprint(userID))
|
||||
|
||||
// Find expired preview environments for user's projects
|
||||
rows, err := db.(*database.DB).Query(
|
||||
`SELECT pe.id, pe.project_id, pe.service_id, pe.branch_name, pe.environment
|
||||
FROM preview_environments pe
|
||||
JOIN projects p ON pe.project_id = p.id
|
||||
WHERE (p.owner_id = $1 OR $2::bool) AND pe.expires_at < NOW() AND pe.status != 'expired'`,
|
||||
userID, contextIsAdmin(c),
|
||||
)
|
||||
rows, err := q.ListExpiredPreviewsForUser(ctx, sqlcdb.ListExpiredPreviewsForUserParams{
|
||||
OwnerID: uid, Column2: contextIsAdmin(c)})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to find expired preview environments"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var expiredEnvs []PreviewEnvironment
|
||||
for rows.Next() {
|
||||
var env PreviewEnvironment
|
||||
err := rows.Scan(
|
||||
&env.ID, &env.ProjectID, &env.ServiceID, &env.BranchName, &env.Environment,
|
||||
)
|
||||
if err != nil {
|
||||
continue
|
||||
expiredEnvs := make([]PreviewEnvironment, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
env := PreviewEnvironment{
|
||||
ID: r.ID, ProjectID: r.ProjectID, ServiceID: r.ServiceID,
|
||||
BranchName: r.BranchName, Environment: r.Environment,
|
||||
}
|
||||
if r.PreviewServiceID.Valid {
|
||||
id := r.PreviewServiceID.UUID
|
||||
env.PreviewServiceID = &id
|
||||
}
|
||||
expiredEnvs = append(expiredEnvs, env)
|
||||
}
|
||||
|
||||
// Mark expired environments as expired and trigger cleanup
|
||||
// Mark expired environments as expired and tear down their clones
|
||||
cleanupCount := 0
|
||||
for _, env := range expiredEnvs {
|
||||
// Update status to expired
|
||||
_, err := db.(*database.DB).Exec(
|
||||
"UPDATE preview_environments SET status = 'expired', updated_at = NOW() WHERE id = $1",
|
||||
env.ID,
|
||||
)
|
||||
if err != nil {
|
||||
if err := q.MarkPreviewEnvironmentStatus(ctx, sqlcdb.MarkPreviewEnvironmentStatusParams{
|
||||
Status: "expired", ID: env.ID}); err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
if env.PreviewServiceID != nil {
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
if engine, ok := engineValue.(*deployment.DeploymentEngine); ok {
|
||||
removeServiceRuntime(ctx, db.(*database.DB), engine, *env.PreviewServiceID)
|
||||
}
|
||||
}
|
||||
_ = q.DeleteServiceByID(ctx, *env.PreviewServiceID)
|
||||
}
|
||||
cleanupCount++
|
||||
}
|
||||
|
||||
@@ -684,3 +724,124 @@ func handleCleanupExpiredPreviewEnvironments(c *gin.Context) {
|
||||
"expired_environments": expiredEnvs,
|
||||
})
|
||||
}
|
||||
|
||||
// previewServiceName builds the clone's service name — deterministic enough
|
||||
// to spot in lists, short enough for dnsLabel-derived hostnames.
|
||||
func previewServiceName(serviceName, branch string) string {
|
||||
label := dnsLabel(serviceName + "-" + branch)
|
||||
if label == "" {
|
||||
return "preview-" + uuid.NewString()[:8]
|
||||
}
|
||||
return label + "-preview"
|
||||
}
|
||||
|
||||
// resolvePreviewHost picks a preview hostname under the clone's node default
|
||||
// domain — same source as serviceAutoDomains. Returns "" when no node
|
||||
// carries a default domain; the preview still runs, reachable internally.
|
||||
func resolvePreviewHost(db *database.DB, cloneID uuid.UUID, envName string) string {
|
||||
label := dnsLabel(envName)
|
||||
if label == "" {
|
||||
return ""
|
||||
}
|
||||
q := sqlcdb.New(db.DB)
|
||||
ctx := context.Background()
|
||||
brief, err := q.GetServicePlacementBrief(ctx, cloneID)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
nodeID, spread := brief.NodeID, brief.Spread
|
||||
var base string
|
||||
switch {
|
||||
case nodeID.Valid && nodeID.String != "":
|
||||
if a, err := q.GetAgent(ctx, nodeID.String); err == nil {
|
||||
base = a.DefaultDomain
|
||||
}
|
||||
case spread:
|
||||
if bases, err := q.ListSchedulableAgentDomains(ctx, json.RawMessage(tagsJSON(servicePlacementTags(db, cloneID)))); err == nil && len(bases) > 0 {
|
||||
base = bases[0]
|
||||
}
|
||||
default:
|
||||
// Local preview: borrow the first node default domain if any —
|
||||
// still better than no hostname for a Traefik-fronted local host.
|
||||
if bases, err := q.ListSchedulableAgentDomains(ctx, json.RawMessage("[]")); err == nil && len(bases) > 0 {
|
||||
base = bases[0]
|
||||
}
|
||||
}
|
||||
if base == "" {
|
||||
return ""
|
||||
}
|
||||
return label + "." + base
|
||||
}
|
||||
|
||||
// loadServiceForDeploy hydrates the Service fields runDeploymentAndSync
|
||||
// needs — mirrors the loader in handleDeployService.
|
||||
func serviceForDeployFromRow(r sqlcdb.GetServiceForDeployRow) Service {
|
||||
return Service{
|
||||
ID: r.ID, ProjectID: r.ProjectID, Name: r.Name, Type: r.Type.String,
|
||||
Status: r.Status.String, Image: r.Image.String, Command: r.Command.String,
|
||||
Environment: r.Environment.String, GitRepo: r.GitRepo.String,
|
||||
GitBranch: r.GitBranch.String, BuildPath: r.BuildPath.String,
|
||||
CPU: r.Cpu.String, Memory: r.Memory.String,
|
||||
Replicas: int(r.Replicas), Port: int(r.Port), Domain: r.Domain,
|
||||
HealthCheckPath: r.HealthcheckPath, RestartPolicy: r.RestartPolicy,
|
||||
Builder: r.Builder, CPUReserve: r.CpuReserve, MemoryReserve: r.MemoryReserve,
|
||||
StaticBuildCmd: r.StaticBuildCmd, StaticDir: r.StaticDir,
|
||||
CreatedAt: r.CreatedAt.Time, UpdatedAt: r.UpdatedAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
func loadServiceForDeploy(db *database.DB, serviceID uuid.UUID) (Service, error) {
|
||||
r, err := sqlcdb.New(db.DB).GetServiceForDeploy(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
return Service{}, err
|
||||
}
|
||||
return serviceForDeployFromRow(r), nil
|
||||
}
|
||||
|
||||
// StartPreviewSweeper expires preview environments past their TTL — tears
|
||||
// down each clone's runtime and row, marks the preview expired.
|
||||
func StartPreviewSweeper(ctx context.Context, db *database.DB, engine *deployment.DeploymentEngine) {
|
||||
if db == nil || engine == nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(time.Minute)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
sweepExpiredPreviews(ctx, db, engine)
|
||||
syncPreviewStatuses(ctx, db)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
||||
// sweepExpiredPreviews retires previews whose TTL elapsed.
|
||||
func sweepExpiredPreviews(ctx context.Context, db *database.DB, engine *deployment.DeploymentEngine) {
|
||||
q := sqlcdb.New(db.DB)
|
||||
rows, err := q.ListSweepablePreviews(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var ids []uuid.UUID
|
||||
for _, r := range rows {
|
||||
ids = append(ids, r.ID)
|
||||
if r.PreviewServiceID.Valid {
|
||||
removeServiceRuntime(ctx, db, engine, r.PreviewServiceID.UUID)
|
||||
_ = q.DeleteServiceByID(ctx, r.PreviewServiceID.UUID)
|
||||
}
|
||||
}
|
||||
for _, id := range ids {
|
||||
_ = q.MarkPreviewEnvironmentStatus(ctx, sqlcdb.MarkPreviewEnvironmentStatusParams{
|
||||
Status: "expired", ID: id})
|
||||
}
|
||||
}
|
||||
|
||||
// syncPreviewStatuses folds the clone's live service status into the
|
||||
// preview row so list/detail endpoints reflect the real deploy state.
|
||||
func syncPreviewStatuses(ctx context.Context, db *database.DB) {
|
||||
_ = sqlcdb.New(db.DB).SyncPreviewStatuses(ctx)
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestPreviewServiceName(t *testing.T) {
|
||||
cases := []struct {
|
||||
svc, branch string
|
||||
want string
|
||||
}{
|
||||
{"whoami-web", "feature-x", "whoami-web-feature-x-preview"},
|
||||
{"My App", "feat/JARV-123_foo", "my-app-feat-jarv-123-foo-preview"},
|
||||
{"svc", "", "svc-preview"},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
got := previewServiceName(tc.svc, tc.branch)
|
||||
if got != tc.want {
|
||||
t.Fatalf("previewServiceName(%q,%q) = %q, want %q", tc.svc, tc.branch, got, tc.want)
|
||||
}
|
||||
if len(got) > 63 {
|
||||
t.Fatalf("name exceeds DNS label limit: %q", got)
|
||||
}
|
||||
}
|
||||
// A name that sanitizes to nothing must still be usable.
|
||||
if got := previewServiceName("!!!", "%%%"); !strings.HasPrefix(got, "preview-") {
|
||||
t.Fatalf("expected fallback name, got %q", got)
|
||||
}
|
||||
}
|
||||
@@ -7,8 +7,8 @@ import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -55,18 +55,9 @@ func handleGetProjects(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
queries := sqlcdb.New(db.DB)
|
||||
|
||||
page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
|
||||
limit, _ := strconv.Atoi(c.DefaultQuery("limit", "10"))
|
||||
offset, limit := pageWindow(c, 10, 100)
|
||||
search := strings.TrimSpace(c.DefaultQuery("search", ""))
|
||||
|
||||
if page < 1 {
|
||||
page = 1
|
||||
}
|
||||
if limit > 100 || limit < 1 {
|
||||
limit = 10
|
||||
}
|
||||
offset := (page - 1) * limit
|
||||
|
||||
searchParam := sql.NullString{}
|
||||
if search != "" {
|
||||
searchParam = sql.NullString{String: search, Valid: true}
|
||||
@@ -99,15 +90,9 @@ func handleGetProjects(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
totalCount := int(total)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"projects": projects,
|
||||
"pagination": gin.H{
|
||||
"page": page,
|
||||
"limit": limit,
|
||||
"total": totalCount,
|
||||
"pages": (totalCount + limit - 1) / limit,
|
||||
},
|
||||
"projects": projects,
|
||||
"pagination": paginationMeta(offset, limit, int(total)),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -168,6 +153,7 @@ func handleCreateProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "project", projectRow.ID.String(), "project.create", map[string]interface{}{"name": req.Name})
|
||||
c.JSON(http.StatusCreated, mapSQLCProject(projectRow))
|
||||
}
|
||||
|
||||
@@ -313,6 +299,28 @@ func handleDeleteProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Managed databases provisioned into this project (graph templates,
|
||||
// compose imports) carry project_id and must be torn down first — their
|
||||
// containers are not labelled containr.project and hold references to the
|
||||
// project network.
|
||||
if dbHandlerValue, exists := c.Get("database_handler"); exists && dbHandlerValue != nil {
|
||||
if dbHandler, ok := dbHandlerValue.(*DatabaseHandler); ok {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 30*time.Second)
|
||||
projectDBs, listErr := dbHandler.queries.ListDatabaseServicesByProject(ctx, uuid.NullUUID{UUID: projectID, Valid: true})
|
||||
if listErr == nil {
|
||||
for _, projectDB := range projectDBs {
|
||||
if projectDB.Provider != "external" {
|
||||
if err := dbHandler.deleteManagedDatabaseRuntime(projectDB.ID); err != nil {
|
||||
log.Printf("delete project %s: database %s runtime removal failed: %v", projectID, projectDB.ID, err)
|
||||
}
|
||||
}
|
||||
_ = dbHandler.queries.DeleteDatabaseServiceByID(ctx, projectDB.ID)
|
||||
}
|
||||
}
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
|
||||
// Remove live containers and the project network before dropping rows.
|
||||
// Best-effort: Docker may be unavailable; DB cleanup proceeds regardless.
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
@@ -333,6 +341,7 @@ func handleDeleteProject(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "project", projectID.String(), "project.delete", nil)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Project deleted successfully"})
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,186 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/secrets"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Project-level shared variables — referenced from service env values as
|
||||
// ${{shared.KEY}} and resolved at runtime in resolveServiceEnv.
|
||||
|
||||
type ProjectVariable struct {
|
||||
ID uuid.UUID `json:"id" db:"id"`
|
||||
ProjectID uuid.UUID `json:"project_id" db:"project_id"`
|
||||
Key string `json:"key" db:"key"`
|
||||
Value string `json:"value" db:"value"`
|
||||
IsSecret bool `json:"is_secret" db:"is_secret"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
}
|
||||
|
||||
func handleGetProjectVariables(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
projectID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"})
|
||||
return
|
||||
}
|
||||
if _, allowed := projectManageAccess(c, db.(*database.DB), projectID); !allowed {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := db.(*database.DB).Query(
|
||||
`SELECT id, project_id, key, value, is_secret, created_at, updated_at
|
||||
FROM project_variables WHERE project_id = $1 ORDER BY key ASC`,
|
||||
projectID,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve variables"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
variables := []ProjectVariable{}
|
||||
for rows.Next() {
|
||||
var v ProjectVariable
|
||||
if err := rows.Scan(&v.ID, &v.ProjectID, &v.Key, &v.Value, &v.IsSecret, &v.CreatedAt, &v.UpdatedAt); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan variable"})
|
||||
return
|
||||
}
|
||||
if v.IsSecret {
|
||||
v.Value = maskedSecretValue
|
||||
}
|
||||
variables = append(variables, v)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"variables": variables})
|
||||
}
|
||||
|
||||
func handleUpdateProjectVariables(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
projectID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid project ID"})
|
||||
return
|
||||
}
|
||||
if _, allowed := projectManageAccess(c, db.(*database.DB), projectID); !allowed {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return
|
||||
}
|
||||
|
||||
var req UpdateVariablesRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
storedSecrets := map[string]string{}
|
||||
secretRows, err := db.(*database.DB).Query(
|
||||
`SELECT key, value FROM project_variables WHERE project_id = $1 AND is_secret = TRUE`,
|
||||
projectID,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load existing variables"})
|
||||
return
|
||||
}
|
||||
for secretRows.Next() {
|
||||
var k, v string
|
||||
if err := secretRows.Scan(&k, &v); err == nil {
|
||||
storedSecrets[k] = v
|
||||
}
|
||||
}
|
||||
secretRows.Close()
|
||||
|
||||
tx, err := db.(*database.DB).Begin()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to begin transaction"})
|
||||
return
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
txq := sqlcdb.New(tx)
|
||||
if err = txq.DeleteProjectVariables(c.Request.Context(), projectID); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clear existing variables"})
|
||||
return
|
||||
}
|
||||
now := time.Now()
|
||||
for _, v := range req.Variables {
|
||||
value := v.Value
|
||||
if v.IsSecret && value == maskedSecretValue {
|
||||
if stored, ok := storedSecrets[v.Key]; ok {
|
||||
value = stored
|
||||
}
|
||||
}
|
||||
if v.IsSecret {
|
||||
value = secrets.Encrypt(value)
|
||||
}
|
||||
if err = txq.InsertProjectVariable(c.Request.Context(), sqlcdb.InsertProjectVariableParams{
|
||||
ID: uuid.New(),
|
||||
ProjectID: projectID,
|
||||
Key: v.Key,
|
||||
Value: value,
|
||||
IsSecret: v.IsSecret,
|
||||
CreatedAt: now,
|
||||
UpdatedAt: now,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to insert variable: " + v.Key})
|
||||
return
|
||||
}
|
||||
}
|
||||
if err = tx.Commit(); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to commit transaction"})
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := db.(*database.DB).Query(
|
||||
`SELECT id, project_id, key, value, is_secret, created_at, updated_at
|
||||
FROM project_variables WHERE project_id = $1 ORDER BY key ASC`,
|
||||
projectID,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to retrieve variables"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
variables := []ProjectVariable{}
|
||||
for rows.Next() {
|
||||
var v ProjectVariable
|
||||
if err := rows.Scan(&v.ID, &v.ProjectID, &v.Key, &v.Value, &v.IsSecret, &v.CreatedAt, &v.UpdatedAt); err != nil {
|
||||
continue
|
||||
}
|
||||
if v.IsSecret {
|
||||
v.Value = maskedSecretValue
|
||||
}
|
||||
variables = append(variables, v)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"variables": variables, "message": "Project variables updated successfully"})
|
||||
}
|
||||
|
||||
// resolveSharedVar returns the decrypted value of a project variable.
|
||||
func resolveSharedVar(db *database.DB, projectID uuid.UUID, key string) (string, bool) {
|
||||
value, err := sqlcdb.New(db.DB).GetProjectVariableValue(context.Background(), sqlcdb.GetProjectVariableValueParams{
|
||||
ProjectID: projectID,
|
||||
Key: key,
|
||||
})
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return secrets.Decrypt(value), true
|
||||
}
|
||||
@@ -0,0 +1,195 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/secrets"
|
||||
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Registry is a stored credential for pulling private images. Password is
|
||||
// encrypted at rest and never returned — `has_password` signals presence.
|
||||
type Registry struct {
|
||||
ID uuid.UUID `json:"id"`
|
||||
Name string `json:"name"`
|
||||
Host string `json:"host"` // e.g. ghcr.io, registry.example.com; docker.io for Docker Hub
|
||||
Username string `json:"username"`
|
||||
HasPassword bool `json:"has_password"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
type registryRequest struct {
|
||||
Name string `json:"name" binding:"required,min=1,max=255"`
|
||||
Host string `json:"host" binding:"required,min=1,max=255"`
|
||||
Username string `json:"username"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
func handleListRegistries(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
userID, _ := c.Get("user_id")
|
||||
rows, err := db.(*database.DB).Query(
|
||||
`SELECT id, name, host, username, password <> '', created_at, updated_at
|
||||
FROM registries WHERE owner_id = $1 ORDER BY created_at DESC`,
|
||||
userID.(string),
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list registries"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
registries := []Registry{}
|
||||
for rows.Next() {
|
||||
var r Registry
|
||||
if err := rows.Scan(&r.ID, &r.Name, &r.Host, &r.Username, &r.HasPassword, &r.CreatedAt, &r.UpdatedAt); err == nil {
|
||||
registries = append(registries, r)
|
||||
}
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"registries": registries})
|
||||
}
|
||||
|
||||
func handleCreateRegistry(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
userID, _ := c.Get("user_id")
|
||||
|
||||
var req registryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
req.Host = strings.ToLower(strings.TrimSpace(req.Host))
|
||||
req.Host = strings.TrimPrefix(strings.TrimPrefix(req.Host, "https://"), "http://")
|
||||
req.Host = strings.TrimSuffix(req.Host, "/")
|
||||
|
||||
// Encrypt only a real credential — encrypting "" would still satisfy
|
||||
// `password <> ''` and report has_password for anonymous registries.
|
||||
password := ""
|
||||
if req.Password != "" {
|
||||
password = secrets.Encrypt(req.Password)
|
||||
}
|
||||
var r Registry
|
||||
err := db.(*database.DB).QueryRow(
|
||||
`INSERT INTO registries (owner_id, name, host, username, password)
|
||||
VALUES ($1, $2, $3, $4, $5)
|
||||
RETURNING id, name, host, username, password <> '', created_at, updated_at`,
|
||||
userID.(string), req.Name, req.Host, req.Username, password,
|
||||
).Scan(&r.ID, &r.Name, &r.Host, &r.Username, &r.HasPassword, &r.CreatedAt, &r.UpdatedAt)
|
||||
if err != nil {
|
||||
if strings.Contains(err.Error(), "unique") {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "A registry for this host already exists", "code": "CONFLICT"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create registry"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{"registry": r})
|
||||
}
|
||||
|
||||
func handleUpdateRegistry(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
userID, _ := c.Get("user_id")
|
||||
registryID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid registry ID", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
var req registryRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
req.Host = strings.ToLower(strings.TrimSpace(req.Host))
|
||||
req.Host = strings.TrimPrefix(strings.TrimPrefix(req.Host, "https://"), "http://")
|
||||
req.Host = strings.TrimSuffix(req.Host, "/")
|
||||
|
||||
// Empty password keeps the stored credential — there is no "clear"
|
||||
// operation; delete + recreate to remove a password.
|
||||
var passwordArg interface{}
|
||||
if req.Password != "" {
|
||||
passwordArg = secrets.Encrypt(req.Password)
|
||||
}
|
||||
|
||||
var r Registry
|
||||
err = db.(*database.DB).QueryRow(
|
||||
`UPDATE registries SET name = $1, host = $2, username = $3,
|
||||
password = COALESCE($4, password), updated_at = now()
|
||||
WHERE id = $5 AND owner_id = $6
|
||||
RETURNING id, name, host, username, password <> '', created_at, updated_at`,
|
||||
req.Name, req.Host, req.Username, passwordArg, registryID, userID.(string),
|
||||
).Scan(&r.ID, &r.Name, &r.Host, &r.Username, &r.HasPassword, &r.CreatedAt, &r.UpdatedAt)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Registry not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"registry": r})
|
||||
}
|
||||
|
||||
func handleDeleteRegistry(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
userID, _ := c.Get("user_id")
|
||||
registryID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid registry ID", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
res, err := db.(*database.DB).Exec(
|
||||
`DELETE FROM registries WHERE id = $1 AND owner_id = $2`,
|
||||
registryID, userID.(string),
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete registry"})
|
||||
return
|
||||
}
|
||||
if n, _ := res.RowsAffected(); n == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Registry not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Registry deleted"})
|
||||
}
|
||||
|
||||
// registryAuthFor resolves pull credentials for an image reference: the
|
||||
// caller's registry whose host matches, else anonymous. docker.io covers
|
||||
// bare image names (nginx:latest).
|
||||
func registryAuthFor(db *database.DB, ownerID, imageRef string) registry.AuthConfig {
|
||||
host := registryHost(imageRef)
|
||||
ownerUUID, _ := uuid.Parse(ownerID)
|
||||
row, err := sqlcdb.New(db.DB).GetRegistryAuth(context.Background(), sqlcdb.GetRegistryAuthParams{
|
||||
OwnerID: ownerUUID,
|
||||
Host: host,
|
||||
})
|
||||
if err != nil {
|
||||
return registry.AuthConfig{}
|
||||
}
|
||||
return registry.AuthConfig{
|
||||
Username: row.Username,
|
||||
Password: secrets.Decrypt(row.Password),
|
||||
ServerAddress: host,
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package api
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestParseCPUReserveShares(t *testing.T) {
|
||||
cases := map[string]int64{
|
||||
"": 0,
|
||||
"0": 0,
|
||||
"-1": 0,
|
||||
"abc": 0,
|
||||
"0.25": 256,
|
||||
"1": 1024,
|
||||
"2.5": 2560,
|
||||
" 0.5 ": 512,
|
||||
}
|
||||
for in, want := range cases {
|
||||
if got := parseCPUReserveShares(in); got != want {
|
||||
t.Errorf("parseCPUReserveShares(%q) = %d, want %d", in, got, want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateResourceSpecs(t *testing.T) {
|
||||
if err := validateCPUSpec(""); err != nil {
|
||||
t.Error("empty cpu should pass")
|
||||
}
|
||||
if err := validateCPUSpec("0.5"); err != nil {
|
||||
t.Error("0.5 should be valid cpu")
|
||||
}
|
||||
if err := validateCPUSpec("512mb"); err == nil {
|
||||
t.Error("512mb should fail cpu validation")
|
||||
}
|
||||
if err := validateMemorySpec(""); err != nil {
|
||||
t.Error("empty memory should pass")
|
||||
}
|
||||
if err := validateMemorySpec("512Mi"); err != nil {
|
||||
t.Error("512Mi should be valid memory")
|
||||
}
|
||||
if err := validateMemorySpec("1g"); err != nil {
|
||||
t.Error("1g should be valid memory")
|
||||
}
|
||||
if err := validateMemorySpec("0.5"); err == nil {
|
||||
t.Error("0.5 should fail memory validation (parses to <1 byte)")
|
||||
}
|
||||
if err := validateMemorySpec("lots"); err == nil {
|
||||
t.Error("'lots' should fail memory validation")
|
||||
}
|
||||
}
|
||||
@@ -4,22 +4,33 @@ import (
|
||||
"context"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"containr/internal/build"
|
||||
"containr/internal/config"
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/deployqueue"
|
||||
"containr/internal/docker"
|
||||
"containr/internal/ha"
|
||||
"containr/internal/metrics"
|
||||
"containr/internal/middleware"
|
||||
"containr/internal/scaling"
|
||||
"containr/internal/secrets"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg *config.Config) {
|
||||
// Audit + outbound webhook delivery need a package-level handle for
|
||||
// call sites that don't carry a gin context.
|
||||
SetAuditDB(db)
|
||||
StartWebhookDispatcher()
|
||||
|
||||
// Expose Better Auth through backend so frontend can use a single backend origin.
|
||||
setupAuthProxyRoutes(router, cfg, db)
|
||||
|
||||
@@ -35,6 +46,11 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
dockerClient = client
|
||||
buildManager = build.NewBuildManager("/tmp/containr-builds", dockerClient)
|
||||
deploymentEngine = deployment.NewDeploymentEngine(buildManager, dockerClient)
|
||||
initArtifacts(filepath.Join("/tmp/containr-builds", "artifacts"))
|
||||
purgeStaleArtifacts()
|
||||
// Node-pinned services dispatch container lifecycle to the agent's
|
||||
// command queue instead of the local Docker socket.
|
||||
deploymentEngine.SetNodeRunner(newAgentNodeRunner(db))
|
||||
// Restore managed sidecars (e.g. cloudflared) from saved settings.
|
||||
syncManagedContainers(dockerClient, db)
|
||||
}
|
||||
@@ -57,6 +73,32 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
}
|
||||
}
|
||||
haManager := ha.NewHighAvailabilityManager(scheduler, metricsCollector)
|
||||
if db != nil && db.DB != nil {
|
||||
haManager.WithPersistence(db.DB)
|
||||
if err := haManager.LoadHealthChecks(context.Background()); err != nil {
|
||||
log.Printf("Failed to restore health checks: %v", err)
|
||||
}
|
||||
}
|
||||
if dockerClient != nil && db != nil {
|
||||
// Service-bound checks probe the service DNS name on the project
|
||||
// network — the backend container joins it on demand. The old
|
||||
// 127.0.0.1 fallback stays for node-bound checks.
|
||||
docker := dockerClient
|
||||
haManager.WithServiceResolver(func(ctx context.Context, serviceID string) (string, int, error) {
|
||||
sid, err := uuid.Parse(serviceID)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
svc, err := sqlcdb.New(db.DB).GetServiceNameProjectPort(ctx, sid)
|
||||
if err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
if err := docker.ConnectSelfToNetwork(ctx, deployment.ProjectNetworkName(svc.ProjectID)); err != nil {
|
||||
return "", 0, err
|
||||
}
|
||||
return svc.Name, int(svc.Port), nil
|
||||
})
|
||||
}
|
||||
haAPIManager := NewHAManager(haManager)
|
||||
|
||||
// Initialize scaling handler
|
||||
@@ -68,9 +110,56 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
// Start the cron scheduler — executes due cron_jobs via docker exec.
|
||||
StartCronScheduler(context.Background(), db, dockerClient)
|
||||
|
||||
// Sleep sweeper — scales idle services to zero and prunes wake containers.
|
||||
StartSleepSweeper(context.Background(), db, dockerClient, deploymentEngine, cfg.Port)
|
||||
StartPreviewSweeper(context.Background(), db, deploymentEngine)
|
||||
|
||||
// Audit retention — prunes rows past AUDIT_RETENTION_DAYS (default 90).
|
||||
StartAuditRetention(context.Background(), db)
|
||||
|
||||
// Agent health — marks stale heartbeats offline, notifies admins on
|
||||
// offline/recovery transitions.
|
||||
StartAgentHealthSweep(context.Background(), db)
|
||||
|
||||
// Daily upgrade check — notifies admins when a newer release exists.
|
||||
StartUpgradeCheck(context.Background(), db, Version)
|
||||
|
||||
// Initialize database handler
|
||||
databaseHandler := NewDatabaseHandler(db.DB, dockerClient)
|
||||
|
||||
// Per-service serialized deploy queue — one active deployment per
|
||||
// service, the rest wait as 'queued'.
|
||||
deployQueue := deployqueue.New()
|
||||
|
||||
// Encrypt-at-rest for secret env vars. SECRETS_KEY wins, JWT_SECRET is
|
||||
// the fallback so existing installs need no new config.
|
||||
secrets.Configure(os.Getenv("SECRETS_KEY"), cfg.JWTSecret)
|
||||
|
||||
// Boot sweep: in-memory engine state dies on restart, so anything that
|
||||
// looked in-flight is actually orphaned — mark it failed rather than
|
||||
// leaving phantom 'building'/'queued' rows.
|
||||
bootQ := sqlcdb.New(db.DB)
|
||||
_ = bootQ.FailInterruptedDeployments(context.Background())
|
||||
|
||||
// Boot sweep: encrypt any plaintext secret values left over from before
|
||||
// encryption-at-rest landed. Transparent and idempotent.
|
||||
if encRows, err := bootQ.ListSecretEnvVars(context.Background()); err == nil {
|
||||
toEncrypt := 0
|
||||
for _, r := range encRows {
|
||||
if secrets.IsEncrypted(r.Value) {
|
||||
continue
|
||||
}
|
||||
if err := bootQ.UpdateEnvVarValue(context.Background(), sqlcdb.UpdateEnvVarValueParams{
|
||||
Value: secrets.Encrypt(r.Value), ID: r.ID,
|
||||
}); err == nil {
|
||||
toEncrypt++
|
||||
}
|
||||
}
|
||||
if toEncrypt > 0 {
|
||||
log.Printf("secrets: encrypted %d plaintext secret variable(s)", toEncrypt)
|
||||
}
|
||||
}
|
||||
|
||||
// Initialize security handler
|
||||
securityHandler := NewSecurityHandler(db, cfg.JWTSecret)
|
||||
|
||||
@@ -88,6 +177,8 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
if deploymentEngine != nil {
|
||||
c.Set("deployment_engine", deploymentEngine)
|
||||
}
|
||||
c.Set("deploy_queue", deployQueue)
|
||||
c.Set("api_port", cfg.Port)
|
||||
c.Set("scheduler", scheduler)
|
||||
c.Set("metrics_collector", metricsCollector)
|
||||
c.Set("auto_scaler", autoScaler)
|
||||
@@ -170,6 +261,16 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
// replaces session auth. Providers POST to /api/git/webhooks/:id.
|
||||
router.POST("/api/git/webhooks/:id", handleGitWebhookPush)
|
||||
|
||||
// GitHub App-level webhook: installed repos deliver to this single
|
||||
// endpoint, verified against the provisioned webhook secret.
|
||||
router.POST("/api/git/github-app/webhook", handleGitHubAppWebhook)
|
||||
|
||||
// Public API gateway data plane. Lives outside /api/v1 so each service's
|
||||
// route_prefix (/g/<slug>) maps cleanly; auth is the caller's api_key,
|
||||
// not a session.
|
||||
router.Any("/g/:slug", handleGatewayProxy)
|
||||
router.Any("/g/:slug/*path", handleGatewayProxy)
|
||||
|
||||
v1 := router.Group("/api/v1")
|
||||
{
|
||||
// Public routes (no authentication required)
|
||||
@@ -178,6 +279,19 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
public.GET("/auth/bootstrap", handleAuthBootstrap)
|
||||
public.POST("/auth/login", handleLogin)
|
||||
public.POST("/auth/register", handleRegister)
|
||||
public.GET("/maintenance", handleMaintenancePage)
|
||||
public.GET("/branding", handleGetBranding)
|
||||
public.GET("/setup/status", handleSetupStatus)
|
||||
|
||||
// Invite accept flow — validates the link, then registers
|
||||
// even while public signup is closed.
|
||||
public.GET("/auth/invites/:token", handleGetInvite)
|
||||
public.POST("/auth/accept-invite", handleAcceptInvite)
|
||||
|
||||
// Wake callback for sleep-mode placeholders. Unauthenticated but
|
||||
// non-destructive: it only reconciles the service back to running.
|
||||
public.Any("/internal/wake/:id", handleServiceWake)
|
||||
public.Any("/internal/wake-page/:id", handleServiceWakePage)
|
||||
}
|
||||
|
||||
// Read routes — public browsing. OptionalAuth resolves a session when
|
||||
@@ -189,6 +303,7 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
read.GET("/projects", handleGetProjects)
|
||||
read.GET("/projects/:id", handleGetProject)
|
||||
read.GET("/projects/:id/services", handleGetServices)
|
||||
read.GET("/projects/:id/environments", handleListEnvironments)
|
||||
read.GET("/projects/:id/preview-environments", handleGetPreviewEnvironments)
|
||||
read.GET("/projects/:id/security/history", securityHandler.GetProjectSecurityHistory)
|
||||
read.GET("/projects/:id/vulnerabilities", securityHandler.GetVulnerabilities)
|
||||
@@ -198,6 +313,9 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
read.GET("/services/:id/metrics", handleGetServiceMetrics)
|
||||
read.GET("/services/:id/runtime", handleGetServiceRuntime)
|
||||
read.GET("/services/:id/deployments", handleGetDeployments)
|
||||
read.GET("/services/:id/domains", handleListServiceDomains)
|
||||
read.GET("/services/:id/domains/check", handleCheckServiceDomains)
|
||||
read.GET("/services/:id/env-check", handleServiceEnvCheck)
|
||||
read.GET("/deployments", handleGetRecentDeployments)
|
||||
read.GET("/deployments/:id", handleGetDeployment)
|
||||
|
||||
@@ -229,38 +347,82 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
// enforce ownership; platform admins additionally pass every check.
|
||||
authed := v1.Group("/")
|
||||
authed.Use(middleware.Auth(cfg.JWTSecret))
|
||||
authed.Use(middleware.Idempotency())
|
||||
{
|
||||
authed.GET("/user/profile", handleGetProfile)
|
||||
authed.PUT("/user/profile", handleUpdateProfile)
|
||||
authed.POST("/setup/complete", handleSetupComplete)
|
||||
|
||||
// Personal access tokens for CLI/MCP/agent auth.
|
||||
authed.GET("/user/tokens", handleListUserTokens)
|
||||
authed.POST("/user/tokens", handleCreateUserToken)
|
||||
authed.DELETE("/user/tokens/:id", handleDeleteUserToken)
|
||||
|
||||
authed.GET("/notifications", handleListNotifications)
|
||||
authed.POST("/notifications/:id/read", handleMarkNotificationRead)
|
||||
authed.POST("/notifications/read-all", handleMarkAllNotificationsRead)
|
||||
authed.GET("/notifications/channels", handleListNotificationChannels)
|
||||
authed.POST("/notifications/channels", handleCreateNotificationChannel)
|
||||
authed.POST("/notifications/channels/:id/test", handleTestNotificationChannel)
|
||||
authed.DELETE("/notifications/channels/:id", handleDeleteNotificationChannel)
|
||||
|
||||
// WebSocket endpoint
|
||||
authed.GET("/ws", handleWebSocket)
|
||||
|
||||
// Cross-cutting ops view — active/queued/failed work
|
||||
authed.GET("/operations", handleGetOperations)
|
||||
authed.GET("/nodes/options", agentHandler.ListNodeOptions)
|
||||
authed.GET("/activity", handleGetActivity)
|
||||
authed.GET("/projects/:id/activity", handleGetProjectActivity)
|
||||
|
||||
// Instance announcements shown to all signed-in users
|
||||
authed.GET("/banners/active", handleListActiveBanners)
|
||||
|
||||
// Outbound webhooks — signed event delivery to user HTTP endpoints
|
||||
authed.GET("/webhooks", handleListOutboundWebhooks)
|
||||
authed.POST("/webhooks", handleCreateOutboundWebhook)
|
||||
authed.PATCH("/webhooks/:id", handleUpdateOutboundWebhook)
|
||||
authed.DELETE("/webhooks/:id", handleDeleteOutboundWebhook)
|
||||
authed.GET("/webhooks/:id/deliveries", handleListOutboundWebhookDeliveries)
|
||||
authed.POST("/webhooks/:id/test", handleTestOutboundWebhook)
|
||||
|
||||
// Owned-resource mutations — handlers scope to the caller and
|
||||
// admit admins. New projects land unapproved until an admin
|
||||
// publishes them.
|
||||
authed.POST("/projects", handleCreateProject)
|
||||
authed.POST("/projects/:id/services", handleCreateService)
|
||||
authed.POST("/projects/:id/import-compose", handleImportProjectCompose)
|
||||
authed.PUT("/projects/:id", handleUpdateProject)
|
||||
authed.DELETE("/projects/:id", handleDeleteProject)
|
||||
|
||||
authed.PUT("/services/:id", handleUpdateService)
|
||||
authed.DELETE("/services/:id", handleDeleteService)
|
||||
authed.POST("/services/:id/domains", handleAddServiceDomain)
|
||||
authed.DELETE("/services/:id/domains/:domain_id", handleDeleteServiceDomain)
|
||||
authed.POST("/services/:id/domains/:domain_id/default", handleSetDefaultDomain)
|
||||
authed.POST("/services/:id/start", handleServiceStart)
|
||||
authed.POST("/services/:id/stop", handleServiceStop)
|
||||
authed.POST("/services/:id/restart", handleServiceRestart)
|
||||
authed.POST("/services/:id/sleep", handleServiceSleep)
|
||||
authed.POST("/services/:id/wake", handleServiceWakeNow)
|
||||
authed.POST("/services/:id/redeploy", handleServiceRedeploy)
|
||||
authed.POST("/services/:id/clone", handleCloneService)
|
||||
authed.POST("/services/:id/move", handleMoveService)
|
||||
authed.POST("/services/:id/deployments", handleCreateDeployment)
|
||||
authed.POST("/deployments/:id/rollback", handleRollbackDeployment)
|
||||
authed.POST("/deployments/:id/cancel", handleCancelDeployment)
|
||||
|
||||
// Interactive terminal — WebSocket → docker exec pty.
|
||||
authed.GET("/services/:id/terminal", handleServiceTerminal)
|
||||
|
||||
// Environment variables — secrets, owner/admin only.
|
||||
authed.GET("/services/:id/variables", handleGetVariables)
|
||||
authed.PUT("/services/:id/variables", handleUpdateVariables)
|
||||
|
||||
// Project-level shared variables — ${{shared.KEY}} refs.
|
||||
authed.GET("/projects/:id/variables", handleGetProjectVariables)
|
||||
authed.PUT("/projects/:id/variables", handleUpdateProjectVariables)
|
||||
|
||||
// Runtime logs — container stdout routinely echoes secrets,
|
||||
// so these stay behind authentication like build logs.
|
||||
authed.GET("/services/:id/logs", handleGetLogs)
|
||||
@@ -269,6 +431,19 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
// One-off exec console (docker exec, 30s ceiling)
|
||||
authed.POST("/services/:id/exec", handleExecInService)
|
||||
|
||||
// S3-compatible backup targets — owner-scoped, keys encrypted at rest.
|
||||
authed.GET("/backup-targets", databaseHandler.ListBackupTargets)
|
||||
authed.POST("/backup-targets", databaseHandler.CreateBackupTarget)
|
||||
authed.PUT("/backup-targets/:id", databaseHandler.UpdateBackupTarget)
|
||||
authed.DELETE("/backup-targets/:id", databaseHandler.DeleteBackupTarget)
|
||||
authed.POST("/backup-targets/:id/test", databaseHandler.TestBackupTarget)
|
||||
|
||||
// Private-image registries — owner-scoped, credentials encrypted at rest.
|
||||
authed.GET("/registries", handleListRegistries)
|
||||
authed.POST("/registries", handleCreateRegistry)
|
||||
authed.PUT("/registries/:id", handleUpdateRegistry)
|
||||
authed.DELETE("/registries/:id", handleDeleteRegistry)
|
||||
|
||||
// Git integration — providers carry per-user credentials.
|
||||
authed.GET("/git/github-app/install-url", handleGetGitHubAppInstallURL)
|
||||
authed.POST("/git/github-app/connect", handleConnectGitHubApp)
|
||||
@@ -292,13 +467,19 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
authed.POST("/builds/plan", buildHandler.GetBuildPlan)
|
||||
|
||||
authed.POST("/databases", databaseHandler.CreateDatabase)
|
||||
authed.POST("/databases/register-external", databaseHandler.RegisterExternalDatabase)
|
||||
authed.POST("/databases/test-connection", databaseHandler.TestDatabaseConnection)
|
||||
authed.POST("/databases/:id/test-connection", databaseHandler.TestDatabaseConnectionByID)
|
||||
authed.PUT("/databases/:id", databaseHandler.UpdateDatabase)
|
||||
authed.DELETE("/databases/:id", databaseHandler.DeleteDatabase)
|
||||
authed.POST("/databases/:id/action", databaseHandler.PerformDatabaseAction)
|
||||
authed.POST("/databases/:id/backup", databaseHandler.CreateBackup)
|
||||
authed.POST("/databases/:id/restore", databaseHandler.RestoreBackup)
|
||||
authed.POST("/databases/:id/backups/import", databaseHandler.ImportBackup)
|
||||
authed.GET("/databases/:id/backups/:bid/download", databaseHandler.DownloadBackup)
|
||||
|
||||
authed.POST("/projects/:id/environments", handleCreateEnvironment)
|
||||
authed.DELETE("/environments/:id", handleDeleteEnvironment)
|
||||
authed.POST("/projects/:id/preview-environments", handleCreatePreviewEnvironment)
|
||||
authed.PUT("/preview-environments/:id", handleUpdatePreviewEnvironment)
|
||||
authed.DELETE("/preview-environments/:id", handleDeletePreviewEnvironment)
|
||||
@@ -315,6 +496,10 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
authed.PUT("/templates/:id", handleUpdateTemplate)
|
||||
authed.DELETE("/templates/:id", handleDeleteTemplate)
|
||||
authed.POST("/templates/:id/deploy", handleCreateFromTemplate)
|
||||
authed.POST("/templates/:id/plan", handlePlanTemplate)
|
||||
authed.POST("/templates/import/compose", handleImportCompose)
|
||||
authed.POST("/templates/import/git", handleImportGitCompose)
|
||||
authed.POST("/templates/deploy", handleDeployInlineGraph)
|
||||
|
||||
authed.POST("/cron-jobs", handleCreateCronJob)
|
||||
authed.PUT("/cron-jobs/:id", handleUpdateCronJob)
|
||||
@@ -325,11 +510,13 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
// Admin routes — platform-wide controls and cross-user management.
|
||||
admin := v1.Group("/")
|
||||
admin.Use(middleware.Auth(cfg.JWTSecret))
|
||||
admin.Use(middleware.Idempotency())
|
||||
admin.Use(middleware.RequireAdmin())
|
||||
{
|
||||
admin.GET("/admin/overview", handleAdminOverview)
|
||||
admin.GET("/admin/users", handleAdminListUsers)
|
||||
admin.PATCH("/admin/users/:id", handleAdminSetUserAdmin)
|
||||
admin.POST("/admin/users/:id/impersonate", handleAdminImpersonateUser)
|
||||
admin.PATCH("/admin/projects/:id", handleAdminSetProjectApproval)
|
||||
|
||||
admin.POST("/users", handleCreateUser)
|
||||
@@ -345,6 +532,24 @@ func SetupRoutes(router *gin.Engine, db *database.DB, redis *database.Redis, cfg
|
||||
scalingHandler.RegisterAdminRoutes(admin)
|
||||
agentHandler.SetupAdminRoutes(admin)
|
||||
|
||||
admin.GET("/admin/volumes", handleAdminListVolumes)
|
||||
admin.DELETE("/admin/volumes/:name", handleAdminDeleteVolume)
|
||||
|
||||
// Announcement banners
|
||||
admin.GET("/admin/banners", handleAdminListBanners)
|
||||
admin.POST("/admin/banners", handleAdminCreateBanner)
|
||||
admin.PATCH("/admin/banners/:id", handleAdminUpdateBanner)
|
||||
admin.DELETE("/admin/banners/:id", handleAdminDeleteBanner)
|
||||
|
||||
// GitHub App self-provisioning (manifest flow)
|
||||
admin.GET("/admin/git/github-app", handleAdminGetGitHubApp)
|
||||
admin.POST("/admin/git/github-app/manifest", handleAdminGitHubAppManifest)
|
||||
admin.POST("/admin/git/github-app/convert", handleAdminGitHubAppConvert)
|
||||
|
||||
// Team invites — single-use registration links
|
||||
admin.GET("/admin/invites", handleAdminListInvites)
|
||||
admin.POST("/admin/invites", handleAdminCreateInvite)
|
||||
admin.DELETE("/admin/invites/:id", handleAdminDeleteInvite)
|
||||
admin.GET("/audit-logs", handleGetAuditLogs)
|
||||
admin.GET("/audit-logs/:resource/:id", handleGetResourceAuditLogs)
|
||||
|
||||
|
||||
+375
-136
@@ -2,6 +2,7 @@ package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
@@ -10,12 +11,65 @@ import (
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/deployqueue"
|
||||
"containr/internal/secrets"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// remoteAgentsForService lists every agent holding inventory rows for the
|
||||
// service — covers pinned placements, spread replicas, and leftovers from a
|
||||
// since-cleared pin.
|
||||
func remoteAgentsForService(ctx context.Context, db *database.DB, serviceID uuid.UUID) []string {
|
||||
ids, err := sqlcdb.New(db.DB).ListNodeAgentsForService(ctx, serviceID.String())
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]string, 0, len(ids))
|
||||
for _, id := range ids {
|
||||
if id != "" {
|
||||
out = append(out, id)
|
||||
}
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// serviceLifecycle routes a start/stop/restart action to every agent holding
|
||||
// the service's replicas, then to the local Docker engine. Both run — a
|
||||
// service can't be pinned and local at once, but stale containers from a
|
||||
// cleared pin shouldn't survive a stop.
|
||||
func serviceLifecycle(ctx context.Context, db *database.DB, engine *deployment.DeploymentEngine, serviceID uuid.UUID, action string) error {
|
||||
for _, agentID := range remoteAgentsForService(ctx, db, serviceID) {
|
||||
if err := engine.ControlServiceOnNode(ctx, serviceID.String(), agentID, action); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
switch action {
|
||||
case "start":
|
||||
return engine.StartService(ctx, serviceID.String())
|
||||
case "stop":
|
||||
return engine.StopService(ctx, serviceID.String())
|
||||
case "restart":
|
||||
if err := engine.StopService(ctx, serviceID.String()); err != nil {
|
||||
return err
|
||||
}
|
||||
return engine.StartService(ctx, serviceID.String())
|
||||
}
|
||||
return fmt.Errorf("unsupported lifecycle action %q", action)
|
||||
}
|
||||
|
||||
// removeServiceRuntime tears down replicas wherever they run — remote nodes
|
||||
// (pinned or spread) and the local Docker host.
|
||||
func removeServiceRuntime(ctx context.Context, db *database.DB, engine *deployment.DeploymentEngine, serviceID uuid.UUID) {
|
||||
for _, agentID := range remoteAgentsForService(ctx, db, serviceID) {
|
||||
_ = engine.RemoveServiceContainersOnNode(ctx, serviceID.String(), agentID)
|
||||
}
|
||||
_ = engine.RemoveServiceContainers(ctx, serviceID.String())
|
||||
}
|
||||
|
||||
// serviceRuntimeSpec builds the container spec for a service: stored env vars
|
||||
// merged with ${{Service.KEY}} references resolved across the project.
|
||||
func serviceRuntimeSpec(db *database.DB, service Service) (deployment.RuntimeSpec, error) {
|
||||
@@ -35,11 +89,16 @@ func serviceRuntimeSpec(db *database.DB, service Service) (deployment.RuntimeSpe
|
||||
Domain: service.Domain,
|
||||
HealthPath: service.HealthCheckPath,
|
||||
RestartPolicy: service.RestartPolicy,
|
||||
NodeID: serviceNodeID(db, service.ID),
|
||||
Spread: serviceSpread(db, service.ID),
|
||||
PlacementTags: servicePlacementTags(db, service.ID),
|
||||
}
|
||||
// Best effort: reuse the host port from the last live deployment so the
|
||||
// public URL survives restarts. Column exists post-migration; older DBs
|
||||
// silently get ephemeral ports.
|
||||
_ = db.QueryRow(`SELECT published_port FROM services WHERE id = $1`, service.ID).Scan(&spec.PublishedPort)
|
||||
if p, err := sqlcdb.New(db.DB).GetServicePublishedPort(context.Background(), service.ID); err == nil {
|
||||
spec.PublishedPort = p
|
||||
}
|
||||
if cmd := strings.TrimSpace(service.Command); cmd != "" {
|
||||
spec.Command = strings.Fields(cmd)
|
||||
}
|
||||
@@ -51,6 +110,20 @@ func serviceRuntimeSpec(db *database.DB, service Service) (deployment.RuntimeSpe
|
||||
}
|
||||
spec.NanoCPUs = parseCPULimit(service.CPU)
|
||||
spec.MemoryBytes = parseMemoryLimit(service.Memory)
|
||||
spec.MemoryReserve = parseMemoryLimit(service.MemoryReserve)
|
||||
spec.CPUShares = parseCPUReserveShares(service.CPUReserve)
|
||||
if service.Builder == "static" && spec.Port == 0 {
|
||||
// The static builder's image is nginx; expose its default port so
|
||||
// Traefik has something to route to without extra config.
|
||||
spec.Port = 80
|
||||
}
|
||||
spec.Volumes = loadServiceVolumes(db, service.ID)
|
||||
spec.Domains = serviceDomainNames(db, service.ID, service.Domain)
|
||||
spec.Maintenance, spec.BasicAuthUsers = serviceAccess(db, service.ID)
|
||||
spec.TraefikLabels = serviceTraefikLabels(db, service.ID)
|
||||
if spec.Maintenance {
|
||||
spec.MaintenanceURL = maintenanceURL(db)
|
||||
}
|
||||
if spec.Port > 0 {
|
||||
env["PORT"] = strconv.Itoa(service.Port)
|
||||
}
|
||||
@@ -63,47 +136,31 @@ var refVarPattern = regexp.MustCompile(`\$\{\{\s*([A-Za-z0-9_-]+)\.([A-Za-z_][A-
|
||||
// secrets) and expands ${{service.KEY}} references. KEY may be a variable on
|
||||
// the referenced service, or the builtins HOST (service name alias) and PORT.
|
||||
func resolveServiceEnv(db *database.DB, service Service) (map[string]string, error) {
|
||||
rows, err := db.Query(
|
||||
`SELECT key, value FROM environment_variables WHERE service_id = $1`,
|
||||
service.ID,
|
||||
)
|
||||
q := sqlcdb.New(db.DB)
|
||||
vars, err := q.ListServiceVariableValues(context.Background(), service.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
env := map[string]string{}
|
||||
for rows.Next() {
|
||||
var k, v string
|
||||
if err := rows.Scan(&k, &v); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
env[k] = v
|
||||
for _, v := range vars {
|
||||
env[v.Key] = secrets.Decrypt(v.Value)
|
||||
}
|
||||
|
||||
var refServices []struct{ ID, Name string }
|
||||
svcRows, err := db.Query(
|
||||
`SELECT id, name FROM services WHERE project_id = $1`,
|
||||
service.ProjectID,
|
||||
)
|
||||
refServices, err := q.ListProjectServiceRefs(context.Background(), service.ProjectID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer svcRows.Close()
|
||||
for svcRows.Next() {
|
||||
var s struct{ ID, Name string }
|
||||
if err := svcRows.Scan(&s.ID, &s.Name); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
refServices = append(refServices, s)
|
||||
}
|
||||
|
||||
byName := map[string]string{}
|
||||
byName := map[string]uuid.UUID{}
|
||||
for _, s := range refServices {
|
||||
byName[strings.ToLower(s.Name)] = s.ID
|
||||
}
|
||||
|
||||
resolve := func(serviceName, key string) (string, bool) {
|
||||
if strings.EqualFold(serviceName, "shared") {
|
||||
return resolveSharedVar(db, service.ProjectID, key)
|
||||
}
|
||||
id, ok := byName[strings.ToLower(serviceName)]
|
||||
if !ok {
|
||||
return "", false
|
||||
@@ -112,21 +169,19 @@ func resolveServiceEnv(db *database.DB, service Service) (map[string]string, err
|
||||
case "HOST":
|
||||
return serviceName, true
|
||||
case "PORT":
|
||||
var port int
|
||||
if err := db.QueryRow(`SELECT COALESCE(port, 0) FROM services WHERE id = $1`, id).Scan(&port); err == nil && port > 0 {
|
||||
return strconv.Itoa(port), true
|
||||
if port, err := q.GetServicePort(context.Background(), id); err == nil && port > 0 {
|
||||
return strconv.Itoa(int(port)), true
|
||||
}
|
||||
return "", false
|
||||
}
|
||||
var value string
|
||||
err := db.QueryRow(
|
||||
`SELECT value FROM environment_variables WHERE service_id = $1 AND key = $2`,
|
||||
id, key,
|
||||
).Scan(&value)
|
||||
value, err := q.GetServiceVariableValue(context.Background(), sqlcdb.GetServiceVariableValueParams{
|
||||
ServiceID: id,
|
||||
Key: key,
|
||||
})
|
||||
if err != nil {
|
||||
return "", false
|
||||
}
|
||||
return value, true
|
||||
return secrets.Decrypt(value), true
|
||||
}
|
||||
|
||||
for k, v := range env {
|
||||
@@ -152,6 +207,32 @@ func parseCPULimit(cpu string) int64 {
|
||||
return int64(v * 1e9)
|
||||
}
|
||||
|
||||
// parseCPUReserveShares maps a cpu fraction to Docker's relative share
|
||||
// weight: 1024 shares ≈ one CPU.
|
||||
func parseCPUReserveShares(cpu string) int64 {
|
||||
v, err := strconv.ParseFloat(strings.TrimSpace(cpu), 64)
|
||||
if err != nil || v <= 0 {
|
||||
return 0
|
||||
}
|
||||
return int64(v * 1024)
|
||||
}
|
||||
|
||||
// validateCPUSpec and validateMemorySpec reject values that silently parse
|
||||
// to zero — a typo like "512mb" must fail loudly instead of dropping the limit.
|
||||
func validateCPUSpec(v string) error {
|
||||
if s := strings.TrimSpace(v); s != "" && parseCPULimit(s) <= 0 {
|
||||
return fmt.Errorf("must be a positive cpu fraction (e.g. 0.5, 2)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validateMemorySpec(v string) error {
|
||||
if s := strings.TrimSpace(v); s != "" && parseMemoryLimit(s) <= 0 {
|
||||
return fmt.Errorf("must be bytes with unit (e.g. 256Mi, 1g)")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
var memUnits = map[string]int64{"k": 1e3, "m": 1e6, "g": 1e9, "ki": 1 << 10, "mi": 1 << 20, "gi": 1 << 30}
|
||||
|
||||
func parseMemoryLimit(mem string) int64 {
|
||||
@@ -169,6 +250,15 @@ func parseMemoryLimit(mem string) int64 {
|
||||
return 0
|
||||
}
|
||||
|
||||
// setServiceStatusNow writes the stored status with a fresh updated_at.
|
||||
func setServiceStatusNow(db *database.DB, id uuid.UUID, status string) {
|
||||
_ = sqlcdb.New(db.DB).SetServiceStatus(context.Background(), sqlcdb.SetServiceStatusParams{
|
||||
Status: sql.NullString{String: status, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: id,
|
||||
})
|
||||
}
|
||||
|
||||
func getRuntimeEngine(c *gin.Context) (*deployment.DeploymentEngine, *database.DB, bool) {
|
||||
dbValue, exists := c.Get("db")
|
||||
if !exists {
|
||||
@@ -183,6 +273,22 @@ func getRuntimeEngine(c *gin.Context) (*deployment.DeploymentEngine, *database.D
|
||||
return engineValue.(*deployment.DeploymentEngine), dbValue.(*database.DB), true
|
||||
}
|
||||
|
||||
// serviceFromRuntimeRow maps the sqlc runtime row (with legacy-column
|
||||
// fallbacks applied in SQL) onto the API Service model.
|
||||
func serviceFromRuntimeRow(r sqlcdb.GetServiceRuntimeWithOwnerRow) Service {
|
||||
return Service{
|
||||
ID: r.ID, ProjectID: r.ProjectID, Name: r.Name, Type: r.Type,
|
||||
Status: r.Status, Image: r.Image, Command: r.Command,
|
||||
Environment: r.Environment, GitRepo: r.GitRepo, GitBranch: r.GitBranch,
|
||||
BuildPath: r.BuildPath, CPU: r.Cpu, Memory: r.Memory,
|
||||
Replicas: int(r.Replicas), Port: int(r.Port), Domain: r.Domain,
|
||||
HealthCheckPath: r.HealthcheckPath, RestartPolicy: r.RestartPolicy,
|
||||
Builder: r.Builder, CPUReserve: r.CpuReserve,
|
||||
MemoryReserve: r.MemoryReserve, StaticBuildCmd: r.StaticBuildCmd,
|
||||
StaticDir: r.StaticDir, CreatedAt: r.CreatedAt.Time, UpdatedAt: r.UpdatedAt.Time,
|
||||
}
|
||||
}
|
||||
|
||||
func loadOwnedService(c *gin.Context, db *database.DB) (Service, bool) {
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
@@ -195,38 +301,16 @@ func loadOwnedService(c *gin.Context, db *database.DB) (Service, bool) {
|
||||
return Service{}, false
|
||||
}
|
||||
|
||||
var service Service
|
||||
var owner string
|
||||
err = db.QueryRow(
|
||||
`SELECT s.id, s.project_id, s.name,
|
||||
COALESCE(s.type, s.service_type, ''), COALESCE(s.status, ''),
|
||||
COALESCE(s.image, s.image_name, ''), COALESCE(s.command, s.start_command, ''),
|
||||
COALESCE(s.environment, ''), COALESCE(s.git_repo, s.source_url, ''),
|
||||
COALESCE(s.git_branch, ''), COALESCE(s.build_path, ''),
|
||||
COALESCE(s.cpu, ''), COALESCE(s.memory, ''),
|
||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||
s.created_at, s.updated_at, p.owner_id
|
||||
FROM services s JOIN projects p ON s.project_id = p.id
|
||||
WHERE s.id = $1`,
|
||||
serviceID,
|
||||
).Scan(
|
||||
&service.ID, &service.ProjectID, &service.Name, &service.Type, &service.Status,
|
||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt, &owner,
|
||||
)
|
||||
row, err := sqlcdb.New(db.DB).GetServiceRuntimeWithOwner(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
||||
return Service{}, false
|
||||
}
|
||||
if owner != userID.(string) && !isAdminUser(db, userID.(string)) {
|
||||
if row.OwnerID.String() != userID.(string) && !isAdminUser(db, userID.(string)) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied"})
|
||||
return Service{}, false
|
||||
}
|
||||
return service, true
|
||||
return serviceFromRuntimeRow(row), true
|
||||
}
|
||||
|
||||
// loadReadableService loads a service when the caller may read its project:
|
||||
@@ -238,34 +322,13 @@ func loadReadableService(c *gin.Context, db *database.DB) (Service, bool) {
|
||||
return Service{}, false
|
||||
}
|
||||
|
||||
var service Service
|
||||
var projectID uuid.UUID
|
||||
err = db.QueryRow(
|
||||
`SELECT s.id, s.project_id, s.name,
|
||||
COALESCE(s.type, s.service_type, ''), COALESCE(s.status, ''),
|
||||
COALESCE(s.image, s.image_name, ''), COALESCE(s.command, s.start_command, ''),
|
||||
COALESCE(s.environment, ''), COALESCE(s.git_repo, s.source_url, ''),
|
||||
COALESCE(s.git_branch, ''), COALESCE(s.build_path, ''),
|
||||
COALESCE(s.cpu, ''), COALESCE(s.memory, ''),
|
||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||
s.created_at, s.updated_at, s.project_id
|
||||
FROM services s
|
||||
WHERE s.id = $1`,
|
||||
serviceID,
|
||||
).Scan(
|
||||
&service.ID, &service.ProjectID, &service.Name, &service.Type, &service.Status,
|
||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt, &projectID,
|
||||
)
|
||||
row, err := sqlcdb.New(db.DB).GetServiceRuntimeWithOwner(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
||||
return Service{}, false
|
||||
}
|
||||
if _, allowed := projectReadAccess(c, db, projectID); !allowed {
|
||||
service := serviceFromRuntimeRow(row)
|
||||
if _, allowed := projectReadAccess(c, db, service.ProjectID); !allowed {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found"})
|
||||
return Service{}, false
|
||||
}
|
||||
@@ -287,6 +350,7 @@ func handleGetServiceRuntime(c *gin.Context) {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
mergeRemoteRuntime(c.Request.Context(), engine, service.ID.String(), state)
|
||||
engine.ProbeServiceHealth(c.Request.Context(), state, service.ProjectID.String(), service.Name, service.Port, service.HealthCheckPath)
|
||||
persistPublishedPort(db, service.ID, state.Ports)
|
||||
if host := requestHostname(c); host != "" {
|
||||
@@ -315,7 +379,7 @@ func reconcileNow(c *gin.Context, engine *deployment.DeploymentEngine, db *datab
|
||||
status = state.Status
|
||||
persistPublishedPort(db, service.ID, state.Ports)
|
||||
}
|
||||
_, _ = db.Exec(`UPDATE services SET status = $1, updated_at = $2 WHERE id = $3`, status, time.Now(), service.ID)
|
||||
setServiceStatusNow(db, service.ID, status)
|
||||
c.JSON(http.StatusOK, gin.H{"runtime": state})
|
||||
return true
|
||||
}
|
||||
@@ -330,11 +394,11 @@ func handleServiceStart(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := engine.StartService(c.Request.Context(), service.ID.String()); err != nil {
|
||||
if err := serviceLifecycle(c.Request.Context(), db, engine, service.ID, "start"); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
_, _ = db.Exec(`UPDATE services SET status = 'running', updated_at = $1 WHERE id = $2`, time.Now(), service.ID)
|
||||
setServiceStatusNow(db, service.ID, "running")
|
||||
c.JSON(http.StatusOK, gin.H{"status": "running"})
|
||||
}
|
||||
|
||||
@@ -348,11 +412,11 @@ func handleServiceStop(c *gin.Context) {
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if err := engine.StopService(c.Request.Context(), service.ID.String()); err != nil {
|
||||
if err := serviceLifecycle(c.Request.Context(), db, engine, service.ID, "stop"); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
_, _ = db.Exec(`UPDATE services SET status = 'stopped', updated_at = $1 WHERE id = $2`, time.Now(), service.ID)
|
||||
setServiceStatusNow(db, service.ID, "stopped")
|
||||
c.JSON(http.StatusOK, gin.H{"status": "stopped"})
|
||||
}
|
||||
|
||||
@@ -367,20 +431,17 @@ func handleServiceRestart(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
if err := engine.StopService(ctx, service.ID.String()); err != nil {
|
||||
if err := serviceLifecycle(ctx, db, engine, service.ID, "restart"); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
if err := engine.StartService(ctx, service.ID.String()); err != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
_, _ = db.Exec(`UPDATE services SET status = 'running', updated_at = $1 WHERE id = $2`, time.Now(), service.ID)
|
||||
setServiceStatusNow(db, service.ID, "running")
|
||||
c.JSON(http.StatusOK, gin.H{"status": "running"})
|
||||
}
|
||||
|
||||
// handleServiceRedeploy re-applies the current spec: pulls the image for
|
||||
// image-sourced services and reconciles containers (env, ports, replicas).
|
||||
// Runs through the per-service deploy queue so it never overlaps a build.
|
||||
func handleServiceRedeploy(c *gin.Context) {
|
||||
engine, db, ok := getRuntimeEngine(c)
|
||||
if !ok {
|
||||
@@ -394,19 +455,114 @@ func handleServiceRedeploy(c *gin.Context) {
|
||||
image := service.Image
|
||||
if image == "" {
|
||||
// Git-sourced service: reuse the last successful deployment image.
|
||||
err := db.QueryRow(
|
||||
`SELECT image_name || ':' || image_tag FROM deployments
|
||||
WHERE service_id = $1 AND status = 'deployed' AND image_name <> ''
|
||||
ORDER BY created_at DESC LIMIT 1`,
|
||||
service.ID,
|
||||
).Scan(&image)
|
||||
var err error
|
||||
image, err = sqlcdb.New(db.DB).GetLastDeployedImage(context.Background(), service.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "No deployable image yet — run a deployment first"})
|
||||
return
|
||||
}
|
||||
}
|
||||
service.Image = image
|
||||
reconcileNow(c, engine, db, service)
|
||||
|
||||
finished := make(chan struct{})
|
||||
var recErr error
|
||||
var state *deployment.RuntimeState
|
||||
getDeployQueue(c).Enqueue(service.ID, deployqueue.Job{Run: func(jctx context.Context) {
|
||||
defer close(finished)
|
||||
state, recErr = reconcileServiceJob(jctx, db, engine, service)
|
||||
}})
|
||||
|
||||
select {
|
||||
case <-finished:
|
||||
if recErr != nil {
|
||||
c.JSON(http.StatusBadGateway, gin.H{"error": recErr.Error()})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"runtime": state})
|
||||
case <-c.Request.Context().Done():
|
||||
c.JSON(http.StatusAccepted, gin.H{"status": "queued"})
|
||||
}
|
||||
}
|
||||
|
||||
// reconcileServiceJob is the shared queue-job body for redeploy paths:
|
||||
// build the spec from current DB state, reconcile containers, persist the
|
||||
// resulting status and published port.
|
||||
func reconcileServiceJob(ctx context.Context, db *database.DB, engine *deployment.DeploymentEngine, service Service) (*deployment.RuntimeState, error) {
|
||||
spec, err := serviceRuntimeSpec(db, service)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
state, err := engine.ReconcileService(ctx, spec)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if state != nil {
|
||||
persistPublishedPort(db, service.ID, state.Ports)
|
||||
status := "stopped"
|
||||
if state.Status != "" {
|
||||
status = state.Status
|
||||
}
|
||||
setServiceStatusNow(db, service.ID, status)
|
||||
}
|
||||
return state, nil
|
||||
}
|
||||
|
||||
// enqueueServiceRedeploy resolves a deployable image (service.Image, else
|
||||
// the last successful deployment's tag) and queues a reconcile. Returns
|
||||
// false when no image is available or the engine is missing.
|
||||
func enqueueServiceRedeploy(c *gin.Context, db *database.DB, service Service) bool {
|
||||
engineValue, exists := c.Get("deployment_engine")
|
||||
if !exists || engineValue == nil {
|
||||
return false
|
||||
}
|
||||
engine, ok := engineValue.(*deployment.DeploymentEngine)
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
image := service.Image
|
||||
if image == "" {
|
||||
var err error
|
||||
image, err = sqlcdb.New(db.DB).GetLastDeployedImage(context.Background(), service.ID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
}
|
||||
service.Image = image
|
||||
getDeployQueue(c).Enqueue(service.ID, deployqueue.Job{Run: func(jctx context.Context) {
|
||||
_, _ = reconcileServiceJob(jctx, db, engine, service)
|
||||
}})
|
||||
return true
|
||||
}
|
||||
|
||||
// mergeRemoteRuntime folds inventory-backed remote replica state into a
|
||||
// local runtime view so pinned and spread services report their containers.
|
||||
// Remote rows carry deterministic UUIDs, local ones Docker IDs — no overlap.
|
||||
func mergeRemoteRuntime(ctx context.Context, engine *deployment.DeploymentEngine, serviceID string, state *deployment.RuntimeState) {
|
||||
remote, err := engine.RemoteRuntimeState(ctx, serviceID)
|
||||
if err != nil || remote == nil {
|
||||
return
|
||||
}
|
||||
running := 0
|
||||
for _, c := range state.Containers {
|
||||
if c.State == "running" {
|
||||
running++
|
||||
}
|
||||
}
|
||||
for _, rc := range remote.Containers {
|
||||
state.Containers = append(state.Containers, rc)
|
||||
if rc.State == "running" {
|
||||
running++
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case len(state.Containers) == 0:
|
||||
case running == 0:
|
||||
state.Status = "stopped"
|
||||
case running < state.Desired:
|
||||
state.Status = "degraded"
|
||||
default:
|
||||
state.Status = "running"
|
||||
}
|
||||
}
|
||||
|
||||
// persistPublishedPort records the host port Docker actually bound so later
|
||||
@@ -415,10 +571,10 @@ func persistPublishedPort(db *database.DB, serviceID uuid.UUID, ports []uint16)
|
||||
if len(ports) == 0 {
|
||||
return
|
||||
}
|
||||
_, _ = db.Exec(
|
||||
`UPDATE services SET published_port = $1 WHERE id = $2 AND published_port <> $1`,
|
||||
int(ports[0]), serviceID,
|
||||
)
|
||||
_ = sqlcdb.New(db.DB).SetServicePublishedPort(context.Background(), sqlcdb.SetServicePublishedPortParams{
|
||||
PublishedPort: int32(ports[0]),
|
||||
ID: serviceID,
|
||||
})
|
||||
}
|
||||
|
||||
// liveServiceStatus reconciles the stored status with real container state.
|
||||
@@ -432,16 +588,22 @@ func liveServiceStatus(c *gin.Context, db *database.DB, service *Service) {
|
||||
ctx, cancel := context.WithTimeout(c.Request.Context(), 3*time.Second)
|
||||
defer cancel()
|
||||
state, err := engine.RuntimeState(ctx, service.ID.String(), service.Replicas)
|
||||
if err != nil || state == nil || len(state.Containers) == 0 {
|
||||
if err != nil || state == nil {
|
||||
return
|
||||
}
|
||||
mergeRemoteRuntime(ctx, engine, service.ID.String(), state)
|
||||
if len(state.Containers) == 0 {
|
||||
return
|
||||
}
|
||||
if state.Status != service.Status {
|
||||
service.Status = state.Status
|
||||
_, _ = db.Exec(`UPDATE services SET status = $1, updated_at = $2 WHERE id = $3`, state.Status, time.Now(), service.ID)
|
||||
setServiceStatusNow(db, service.ID, state.Status)
|
||||
}
|
||||
persistPublishedPort(db, service.ID, state.Ports)
|
||||
if service.Domain != "" {
|
||||
service.PublicURL = "https://" + service.Domain
|
||||
} else if auto := serviceAutoDomains(db, service.ID); len(auto) > 0 {
|
||||
service.PublicURL = "https://" + auto[0]
|
||||
} else if len(state.URLs) > 0 {
|
||||
service.PublicURL = rewriteLoopbackURL(state.URLs[0], requestHostname(c))
|
||||
}
|
||||
@@ -457,39 +619,22 @@ func runtimeScaleService(c *gin.Context, serviceID string, replicas int) error {
|
||||
return fmt.Errorf("runtime unavailable")
|
||||
}
|
||||
|
||||
var service Service
|
||||
var owner string
|
||||
err := db.QueryRow(
|
||||
`SELECT s.id, s.project_id, s.name,
|
||||
COALESCE(s.type, s.service_type, ''), COALESCE(s.status, ''),
|
||||
COALESCE(s.image, s.image_name, ''), COALESCE(s.command, s.start_command, ''),
|
||||
COALESCE(s.environment, ''), COALESCE(s.git_repo, s.source_url, ''),
|
||||
COALESCE(s.git_branch, ''), COALESCE(s.build_path, ''),
|
||||
COALESCE(s.cpu, ''), COALESCE(s.memory, ''),
|
||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||
s.created_at, s.updated_at, p.owner_id
|
||||
FROM services s JOIN projects p ON s.project_id = p.id
|
||||
WHERE s.id = $1`, serviceID,
|
||||
).Scan(
|
||||
&service.ID, &service.ProjectID, &service.Name, &service.Type, &service.Status,
|
||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt, &owner,
|
||||
)
|
||||
q := sqlcdb.New(db.DB)
|
||||
sid, err := uuid.Parse(serviceID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
row, err := q.GetServiceRuntimeWithOwner(context.Background(), sid)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
service := serviceFromRuntimeRow(row)
|
||||
if service.Image == "" {
|
||||
if err := db.QueryRow(
|
||||
`SELECT image_name || ':' || image_tag FROM deployments
|
||||
WHERE service_id = $1 AND status = 'deployed' AND image_name <> ''
|
||||
ORDER BY created_at DESC LIMIT 1`, service.ID,
|
||||
).Scan(&service.Image); err != nil {
|
||||
image, err := q.GetLastDeployedImage(context.Background(), service.ID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("no deployed image to scale")
|
||||
}
|
||||
service.Image = image
|
||||
}
|
||||
|
||||
spec, err := serviceRuntimeSpec(db, service)
|
||||
@@ -507,6 +652,100 @@ func runtimeScaleService(c *gin.Context, serviceID string, replicas int) error {
|
||||
if state != nil {
|
||||
persistPublishedPort(db, service.ID, state.Ports)
|
||||
}
|
||||
_, _ = db.Exec(`UPDATE services SET replicas = $1, updated_at = $2 WHERE id = $3`, replicas, time.Now(), service.ID)
|
||||
_ = q.SetServiceReplicas(context.Background(), sqlcdb.SetServiceReplicasParams{
|
||||
Replicas: int32(replicas),
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: service.ID,
|
||||
})
|
||||
return nil
|
||||
}
|
||||
|
||||
// handleServiceEnvCheck reports environment problems without exposing
|
||||
// values: empty vars, ${{service.KEY}} refs that fail to resolve, and
|
||||
// secrets whose ciphertext can't be decrypted (key rotation).
|
||||
func handleServiceEnvCheck(c *gin.Context) {
|
||||
dbVal, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
db := dbVal.(*database.DB)
|
||||
service, ok := loadReadableService(c, db)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
q := sqlcdb.New(db.DB)
|
||||
vars, err := q.ListServiceVariablesWithSecret(context.Background(), service.ID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
var unresolved, empty, unreadable []string
|
||||
var raw []struct{ key, val string }
|
||||
for _, v := range vars {
|
||||
decrypted := secrets.Decrypt(v.Value)
|
||||
if v.IsSecret && secrets.IsEncrypted(v.Value) && decrypted == v.Value {
|
||||
// Ciphertext failed to decrypt — Decrypt returns the input.
|
||||
unreadable = append(unreadable, v.Key)
|
||||
continue
|
||||
}
|
||||
if strings.TrimSpace(decrypted) == "" {
|
||||
empty = append(empty, v.Key)
|
||||
}
|
||||
raw = append(raw, struct{ key, val string }{v.Key, decrypted})
|
||||
}
|
||||
|
||||
// Project siblings for ${{name.KEY}} resolution.
|
||||
siblingIDs := map[string]uuid.UUID{}
|
||||
if refs, err := q.ListProjectServiceRefs(context.Background(), service.ProjectID); err == nil {
|
||||
for _, s := range refs {
|
||||
siblingIDs[strings.ToLower(s.Name)] = s.ID
|
||||
}
|
||||
}
|
||||
|
||||
resolvable := func(serviceName, key string) bool {
|
||||
id, ok := siblingIDs[strings.ToLower(serviceName)]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
if strings.EqualFold(key, "HOST") {
|
||||
return true
|
||||
}
|
||||
if strings.EqualFold(key, "PORT") {
|
||||
port, err := q.GetServicePort(context.Background(), id)
|
||||
return err == nil && port > 0
|
||||
}
|
||||
_, err := q.GetServiceVariableValue(context.Background(), sqlcdb.GetServiceVariableValueParams{
|
||||
ServiceID: id,
|
||||
Key: key,
|
||||
})
|
||||
return err == nil
|
||||
}
|
||||
|
||||
for _, kv := range raw {
|
||||
for _, match := range refVarPattern.FindAllStringSubmatch(kv.val, -1) {
|
||||
if len(match) == 3 && !resolvable(match[1], match[2]) {
|
||||
unresolved = append(unresolved, fmt.Sprintf("%s → %s", kv.key, match[0]))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if unresolved == nil {
|
||||
unresolved = []string{}
|
||||
}
|
||||
if empty == nil {
|
||||
empty = []string{}
|
||||
}
|
||||
if unreadable == nil {
|
||||
unreadable = []string{}
|
||||
}
|
||||
okResult := len(unresolved) == 0 && len(empty) == 0 && len(unreadable) == 0
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"ok": okResult,
|
||||
"unresolved": unresolved,
|
||||
"empty": empty,
|
||||
"unreadable": unreadable,
|
||||
})
|
||||
}
|
||||
@@ -2,7 +2,9 @@ package api
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/security"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
@@ -29,9 +31,27 @@ type SecurityHandler struct {
|
||||
func NewSecurityHandler(db *database.DB, encryptionKey string) *SecurityHandler {
|
||||
encryptionManager, _ := security.NewEncryptionManager(encryptionKey)
|
||||
|
||||
scanner := security.NewScanner(db)
|
||||
// Notify the project owner when a background scan completes — criticals
|
||||
// earn a distinct title so they stand out in the notification list.
|
||||
scanner.OnScanComplete = func(scan *security.SecurityScan) {
|
||||
ownerID, err := sqlcdb.New(db.DB).GetProjectOwnerText(context.Background(), uuid.MustParse(scan.ProjectID))
|
||||
if err != nil || ownerID == "" {
|
||||
return
|
||||
}
|
||||
title := "Security scan completed"
|
||||
if scan.Summary.Critical > 0 || scan.Summary.High > 0 {
|
||||
title = "Security scan found issues"
|
||||
}
|
||||
insertUserNotification(db, ownerID, "security", title,
|
||||
fmt.Sprintf("%s scan finished: %d critical, %d high, %d total findings (score %d/100).",
|
||||
scan.ScanType, scan.Summary.Critical, scan.Summary.High, scan.Summary.Total, scan.Summary.Score),
|
||||
"project", scan.ProjectID)
|
||||
}
|
||||
|
||||
return &SecurityHandler{
|
||||
db: db,
|
||||
scanner: security.NewScanner(db),
|
||||
scanner: scanner,
|
||||
complianceManager: security.NewComplianceManager(db),
|
||||
encryptionManager: encryptionManager,
|
||||
dataRetentionManager: security.NewDataRetentionManager(encryptionManager),
|
||||
@@ -63,14 +83,10 @@ func (sh *SecurityHandler) StartSecurityScan(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var serviceExists bool
|
||||
err := sh.db.QueryRow(
|
||||
`SELECT EXISTS(
|
||||
SELECT 1 FROM services WHERE id = $1 AND project_id = $2
|
||||
)`,
|
||||
req.ServiceID,
|
||||
req.ProjectID,
|
||||
).Scan(&serviceExists)
|
||||
serviceExists, err := sqlcdb.New(sh.db.DB).ServiceExistsInProject(context.Background(), sqlcdb.ServiceExistsInProjectParams{
|
||||
ID: uuid.MustParse(req.ServiceID),
|
||||
ProjectID: uuid.MustParse(req.ProjectID),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to validate service"})
|
||||
return
|
||||
@@ -146,40 +162,30 @@ func (sh *SecurityHandler) GetVulnerabilities(c *gin.Context) {
|
||||
}
|
||||
|
||||
// Query vulnerabilities
|
||||
rows, err := sh.db.Query(`
|
||||
SELECT id, type, severity, title, description, service_id, status, found_at, resolved_at
|
||||
FROM vulnerabilities
|
||||
WHERE project_id = $1
|
||||
ORDER BY
|
||||
CASE severity
|
||||
WHEN 'critical' THEN 1
|
||||
WHEN 'high' THEN 2
|
||||
WHEN 'medium' THEN 3
|
||||
WHEN 'low' THEN 4
|
||||
END,
|
||||
found_at DESC
|
||||
`, projectID)
|
||||
|
||||
rows, err := sqlcdb.New(sh.db.DB).ListVulnerabilities(context.Background(), uuid.MustParse(projectID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get vulnerabilities"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var vulnerabilities []security.Vulnerability
|
||||
for rows.Next() {
|
||||
var vuln security.Vulnerability
|
||||
var resolvedAt *time.Time
|
||||
|
||||
err := rows.Scan(&vuln.ID, &vuln.Type, &vuln.Severity, &vuln.Title, &vuln.Description,
|
||||
&vuln.ServiceID, &vuln.Status, &vuln.FoundAt, &resolvedAt)
|
||||
|
||||
if err != nil {
|
||||
for _, row := range rows {
|
||||
// NULL description/service_id failed the original scan and dropped
|
||||
// the row — keep the same filter.
|
||||
if !row.Description.Valid || !row.ServiceID.Valid {
|
||||
continue
|
||||
}
|
||||
|
||||
vuln.ResolvedAt = resolvedAt
|
||||
vulnerabilities = append(vulnerabilities, vuln)
|
||||
vulnerabilities = append(vulnerabilities, security.Vulnerability{
|
||||
ID: row.ID.String(),
|
||||
Type: row.Type,
|
||||
Severity: row.Severity,
|
||||
Title: row.Title,
|
||||
Description: row.Description.String,
|
||||
ServiceID: row.ServiceID.UUID.String(),
|
||||
Status: row.Status,
|
||||
FoundAt: row.FoundAt,
|
||||
ResolvedAt: timePtrNT(row.ResolvedAt),
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"vulnerabilities": vulnerabilities})
|
||||
@@ -209,11 +215,11 @@ func (sh *SecurityHandler) UpdateVulnerability(c *gin.Context) {
|
||||
resolvedAt = &now
|
||||
}
|
||||
|
||||
_, err := sh.db.Exec(`
|
||||
UPDATE vulnerabilities
|
||||
SET status = $1, resolved_at = $2
|
||||
WHERE id = $3
|
||||
`, req.Status, resolvedAt, vulnID)
|
||||
err := sqlcdb.New(sh.db.DB).UpdateVulnerabilityStatus(context.Background(), sqlcdb.UpdateVulnerabilityStatusParams{
|
||||
Status: req.Status,
|
||||
ResolvedAt: ntPtr(resolvedAt),
|
||||
ID: uuid.MustParse(vulnID),
|
||||
})
|
||||
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update vulnerability"})
|
||||
@@ -253,13 +259,7 @@ func (sh *SecurityHandler) StartComplianceAssessment(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var frameworkExists bool
|
||||
err := sh.db.QueryRow(
|
||||
`SELECT EXISTS(
|
||||
SELECT 1 FROM compliance_frameworks WHERE id = $1
|
||||
)`,
|
||||
req.FrameworkID,
|
||||
).Scan(&frameworkExists)
|
||||
frameworkExists, err := sqlcdb.New(sh.db.DB).ComplianceFrameworkExists(context.Background(), uuid.MustParse(req.FrameworkID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to validate framework"})
|
||||
return
|
||||
@@ -303,30 +303,25 @@ func (sh *SecurityHandler) GetComplianceReport(c *gin.Context) {
|
||||
|
||||
// GetComplianceFrameworks retrieves available compliance frameworks
|
||||
func (sh *SecurityHandler) GetComplianceFrameworks(c *gin.Context) {
|
||||
rows, err := sh.db.Query(`
|
||||
SELECT id, name, description, version, enabled, created_at
|
||||
FROM compliance_frameworks
|
||||
WHERE enabled = true
|
||||
ORDER BY name
|
||||
`)
|
||||
|
||||
rows, err := sqlcdb.New(sh.db.DB).ListComplianceFrameworks(context.Background())
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get compliance frameworks"})
|
||||
return
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var frameworks []security.ComplianceFramework
|
||||
for rows.Next() {
|
||||
var framework security.ComplianceFramework
|
||||
err := rows.Scan(&framework.ID, &framework.Name, &framework.Description,
|
||||
&framework.Version, &framework.Enabled, &framework.CreatedAt)
|
||||
|
||||
if err != nil {
|
||||
for _, row := range rows {
|
||||
if !row.Description.Valid {
|
||||
continue
|
||||
}
|
||||
|
||||
frameworks = append(frameworks, framework)
|
||||
frameworks = append(frameworks, security.ComplianceFramework{
|
||||
ID: row.ID.String(),
|
||||
Name: row.Name,
|
||||
Description: row.Description.String,
|
||||
Version: row.Version,
|
||||
Enabled: row.Enabled,
|
||||
CreatedAt: row.CreatedAt,
|
||||
})
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"frameworks": frameworks})
|
||||
@@ -367,23 +362,19 @@ func (sh *SecurityHandler) GetSecurityMetrics(c *gin.Context) {
|
||||
Resolved int `json:"resolved"`
|
||||
}
|
||||
|
||||
err := sh.db.QueryRow(`
|
||||
SELECT
|
||||
COUNT(*) as total,
|
||||
COUNT(*) FILTER (WHERE severity = 'critical') as critical,
|
||||
COUNT(*) FILTER (WHERE severity = 'high') as high,
|
||||
COUNT(*) FILTER (WHERE severity = 'medium') as medium,
|
||||
COUNT(*) FILTER (WHERE severity = 'low') as low,
|
||||
COUNT(*) FILTER (WHERE status = 'open') as open,
|
||||
COUNT(*) FILTER (WHERE status = 'resolved') as resolved
|
||||
FROM vulnerabilities
|
||||
WHERE project_id = $1
|
||||
`, projectID).Scan(&vulnMetrics.Total, &vulnMetrics.Critical, &vulnMetrics.High,
|
||||
&vulnMetrics.Medium, &vulnMetrics.Low, &vulnMetrics.Open, &vulnMetrics.Resolved)
|
||||
q := sqlcdb.New(sh.db.DB)
|
||||
mrow, err := q.GetVulnerabilityMetrics(context.Background(), uuid.MustParse(projectID))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to get vulnerability metrics"})
|
||||
return
|
||||
}
|
||||
vulnMetrics.Total = int(mrow.Total)
|
||||
vulnMetrics.Critical = int(mrow.Critical)
|
||||
vulnMetrics.High = int(mrow.High)
|
||||
vulnMetrics.Medium = int(mrow.Medium)
|
||||
vulnMetrics.Low = int(mrow.Low)
|
||||
vulnMetrics.Open = int(mrow.Open)
|
||||
vulnMetrics.Resolved = int(mrow.Resolved)
|
||||
|
||||
// Get latest scan
|
||||
var latestScan struct {
|
||||
@@ -393,13 +384,11 @@ func (sh *SecurityHandler) GetSecurityMetrics(c *gin.Context) {
|
||||
Status string `json:"status"`
|
||||
}
|
||||
|
||||
err = sh.db.QueryRow(`
|
||||
SELECT id, COALESCE((summary->>'score')::int, 0), started_at as scanned_at, status
|
||||
FROM security_scans
|
||||
WHERE project_id = $1
|
||||
ORDER BY started_at DESC
|
||||
LIMIT 1
|
||||
`, projectID).Scan(&latestScan.ID, &latestScan.Score, &latestScan.ScannedAt, &latestScan.Status)
|
||||
srow, err := q.GetLatestSecurityScan(context.Background(), uuid.MustParse(projectID))
|
||||
latestScan.ID = srow.ID.String()
|
||||
latestScan.Score = int(srow.Score)
|
||||
latestScan.ScannedAt = srow.ScannedAt
|
||||
latestScan.Status = srow.Status
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
latestScan = struct {
|
||||
@@ -420,13 +409,10 @@ func (sh *SecurityHandler) GetSecurityMetrics(c *gin.Context) {
|
||||
LastAssessed *time.Time `json:"last_assessed"`
|
||||
}
|
||||
|
||||
err = sh.db.QueryRow(`
|
||||
SELECT overall_status, score, assessment_date
|
||||
FROM compliance_reports
|
||||
WHERE project_id = $1
|
||||
ORDER BY assessment_date DESC
|
||||
LIMIT 1
|
||||
`, projectID).Scan(&complianceStatus.OverallStatus, &complianceStatus.Score, &complianceStatus.LastAssessed)
|
||||
crow, err := q.GetLatestComplianceReport(context.Background(), uuid.MustParse(projectID))
|
||||
complianceStatus.OverallStatus = crow.OverallStatus
|
||||
complianceStatus.Score = int(crow.Score)
|
||||
complianceStatus.LastAssessed = &crow.AssessmentDate
|
||||
|
||||
if err == sql.ErrNoRows {
|
||||
complianceStatus = struct {
|
||||
@@ -596,21 +582,11 @@ func (sh *SecurityHandler) requireProjectAccess(c *gin.Context, projectID string
|
||||
return "", false
|
||||
}
|
||||
|
||||
var hasAccess bool
|
||||
err := sh.db.QueryRow(
|
||||
`SELECT EXISTS (
|
||||
SELECT 1
|
||||
FROM projects p
|
||||
WHERE p.id = $1
|
||||
AND (p.is_approved
|
||||
OR $3::bool
|
||||
OR p.owner_id = $2 OR EXISTS (
|
||||
SELECT 1 FROM project_members pm
|
||||
WHERE pm.project_id = p.id AND pm.user_id = $2
|
||||
))
|
||||
)`,
|
||||
projectID, optionalUserUUID(c), contextIsAdmin(c),
|
||||
).Scan(&hasAccess)
|
||||
hasAccess, err := sqlcdb.New(sh.db.DB).CheckSecurityProjectAccess(context.Background(), sqlcdb.CheckSecurityProjectAccessParams{
|
||||
ID: uuid.MustParse(projectID),
|
||||
OwnerID: optionalUserUUID(c),
|
||||
Column3: contextIsAdmin(c),
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to verify project access"})
|
||||
return "", false
|
||||
@@ -631,7 +607,8 @@ func (sh *SecurityHandler) requireSecurityScanAccess(c *gin.Context, scanID stri
|
||||
}
|
||||
|
||||
var projectID string
|
||||
err := sh.db.QueryRow("SELECT project_id FROM security_scans WHERE id = $1", scanID).Scan(&projectID)
|
||||
pid, err := sqlcdb.New(sh.db.DB).GetSecurityScanProjectID(context.Background(), uuid.MustParse(scanID))
|
||||
projectID = pid.String()
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Security scan not found"})
|
||||
return false
|
||||
@@ -652,7 +629,8 @@ func (sh *SecurityHandler) requireComplianceReportAccess(c *gin.Context, reportI
|
||||
}
|
||||
|
||||
var projectID string
|
||||
err := sh.db.QueryRow("SELECT project_id FROM compliance_reports WHERE id = $1", reportID).Scan(&projectID)
|
||||
pid, err := sqlcdb.New(sh.db.DB).GetComplianceReportProjectID(context.Background(), uuid.MustParse(reportID))
|
||||
projectID = pid.String()
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Compliance report not found"})
|
||||
return false
|
||||
@@ -673,7 +651,8 @@ func (sh *SecurityHandler) requireVulnerabilityAccess(c *gin.Context, vulnID str
|
||||
}
|
||||
|
||||
var projectID string
|
||||
err := sh.db.QueryRow("SELECT project_id FROM vulnerabilities WHERE id = $1", vulnID).Scan(&projectID)
|
||||
pid, err := sqlcdb.New(sh.db.DB).GetVulnerabilityProjectID(context.Background(), uuid.MustParse(vulnID))
|
||||
projectID = pid.String()
|
||||
if err == sql.ErrNoRows {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Vulnerability not found"})
|
||||
return "", false
|
||||
|
||||
@@ -0,0 +1,212 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Live invariant test against real Postgres — set METRICS_TEST_DSN (or
|
||||
// DATABASE_URL). Covers the generated auth/security queries: user upsert
|
||||
// conflict path, project-access EXISTS predicate, vulnerability ordering and
|
||||
// status update, latest-scan/report lookups, and the framework list.
|
||||
func TestSecurityLiveInvariants(t *testing.T) {
|
||||
dsn := testDSNString(t)
|
||||
db, err := database.NewConnection(dsn)
|
||||
if err != nil {
|
||||
t.Fatalf("connect: %v", err)
|
||||
}
|
||||
defer db.Close()
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
userID := uuid.New()
|
||||
otherID := uuid.New()
|
||||
projectID := uuid.New()
|
||||
envID := uuid.New()
|
||||
serviceID := uuid.New()
|
||||
scanID := uuid.New()
|
||||
reportID := uuid.New()
|
||||
frameworkID := uuid.New()
|
||||
vulnCrit := uuid.New()
|
||||
vulnLow := uuid.New()
|
||||
suffix := strings.ReplaceAll(userID.String()[:8], "-", "")
|
||||
must := func(err error) {
|
||||
t.Helper()
|
||||
if err != nil {
|
||||
t.Fatalf("setup: %v", err)
|
||||
}
|
||||
}
|
||||
_, err = db.Exec(`INSERT INTO users (id, email, password_hash, name) VALUES ($1, $2, 'x', 'test'), ($3, $4, 'x', 'other')`,
|
||||
userID, "sec-"+suffix+"@test.local", otherID, "sec-"+suffix+"[email protected]")
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO projects (id, name, owner_id, is_approved) VALUES ($1, $2, $3, false)`,
|
||||
projectID, "sec-"+suffix, userID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO environments (id, name, project_id) VALUES ($1, 'production', $2)`,
|
||||
envID, projectID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO services (id, name, project_id, environment_id, service_type, source_type)
|
||||
VALUES ($1, $2, $3, $4, 'web', 'image')`,
|
||||
serviceID, "sec-"+suffix, projectID, envID)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE id IN ($1, $2)`, userID, otherID) }()
|
||||
|
||||
// Auth queries: upsert conflicts update rather than error, counts see rows.
|
||||
_, err = q.InsertUserAdmin(ctx, sqlcdb.InsertUserAdminParams{
|
||||
Email: "ins-" + suffix + "@test.local", PasswordHash: "x", Name: "ins", IsAdmin: false,
|
||||
})
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM users WHERE email = $1`, "ins-"+suffix+"@test.local") }()
|
||||
urow, err := q.UpsertLocalUser(ctx, sqlcdb.UpsertLocalUserParams{
|
||||
Email: "ins-" + suffix + "@test.local", PasswordHash: "y", Name: "renamed",
|
||||
Column4: "http://img", IsAdmin: false,
|
||||
})
|
||||
must(err)
|
||||
if urow.Name != "renamed" || urow.AvatarUrl != "http://img" {
|
||||
t.Fatalf("upsert: %+v", urow)
|
||||
}
|
||||
byEmail, err := q.GetUserByEmailForAuth(ctx, "ins-"+suffix+"@test.local")
|
||||
must(err)
|
||||
if byEmail.ID != urow.ID || byEmail.PasswordHash != "x" {
|
||||
t.Fatalf("by email: %+v", byEmail)
|
||||
}
|
||||
count, err := q.CountUsersByEmail(ctx, "ins-"+suffix+"@test.local")
|
||||
must(err)
|
||||
if count != 1 {
|
||||
t.Fatalf("count by email: %d", count)
|
||||
}
|
||||
byID, err := q.GetUserByID(ctx, urow.ID)
|
||||
must(err)
|
||||
if byID.Email != "ins-"+suffix+"@test.local" {
|
||||
t.Fatalf("by id: %+v", byID)
|
||||
}
|
||||
|
||||
// Access predicate: unapproved project is owner/admin/member only.
|
||||
ownerAccess, err := q.CheckSecurityProjectAccess(ctx, sqlcdb.CheckSecurityProjectAccessParams{
|
||||
ID: projectID, OwnerID: userID, Column3: false,
|
||||
})
|
||||
must(err)
|
||||
if !ownerAccess {
|
||||
t.Fatal("owner should have access")
|
||||
}
|
||||
strangerAccess, err := q.CheckSecurityProjectAccess(ctx, sqlcdb.CheckSecurityProjectAccessParams{
|
||||
ID: projectID, OwnerID: otherID, Column3: false,
|
||||
})
|
||||
must(err)
|
||||
if strangerAccess {
|
||||
t.Fatal("stranger should not have access")
|
||||
}
|
||||
adminAccess, err := q.CheckSecurityProjectAccess(ctx, sqlcdb.CheckSecurityProjectAccessParams{
|
||||
ID: projectID, OwnerID: otherID, Column3: true,
|
||||
})
|
||||
must(err)
|
||||
if !adminAccess {
|
||||
t.Fatal("admin should have access")
|
||||
}
|
||||
ownerText, err := q.GetProjectOwnerText(ctx, projectID)
|
||||
must(err)
|
||||
if ownerText != userID.String() {
|
||||
t.Fatalf("owner text: %q", ownerText)
|
||||
}
|
||||
exists, err := q.ServiceExistsInProject(ctx, sqlcdb.ServiceExistsInProjectParams{
|
||||
ID: serviceID, ProjectID: projectID,
|
||||
})
|
||||
must(err)
|
||||
if !exists {
|
||||
t.Fatal("service should exist in project")
|
||||
}
|
||||
|
||||
// Scan/report/vuln rows via raw inserts (fixtures), reads via sqlc.
|
||||
_, err = db.Exec(`INSERT INTO security_scans (id, project_id, service_id, scan_type, status, summary, started_at)
|
||||
VALUES ($1, $2, $3, 'dependency', 'completed', '{"score": 85}', NOW() - interval '1 hour')`,
|
||||
scanID, projectID, serviceID)
|
||||
must(err)
|
||||
_, err = db.Exec(`INSERT INTO compliance_frameworks (id, name, description, version, enabled)
|
||||
VALUES ($1, $2, 'desc', '1.0', true)`,
|
||||
frameworkID, "fw-"+suffix)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM compliance_frameworks WHERE id = $1`, frameworkID) }()
|
||||
_, err = db.Exec(`INSERT INTO compliance_reports (id, project_id, framework_id, overall_status, score, assessment_date)
|
||||
VALUES ($1, $2, $3, 'compliant', 92, NOW() - interval '30 minutes')`,
|
||||
reportID, projectID, frameworkID)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM compliance_reports WHERE id = $1`, reportID) }()
|
||||
_, err = db.Exec(`INSERT INTO vulnerabilities (id, type, severity, title, description, service_id, project_id, status, found_at)
|
||||
VALUES ($1, 'dependency', 'low', 'low-v', 'd', $3, $4, 'open', NOW() - interval '2 hours'),
|
||||
($2, 'dependency', 'critical', 'crit-v', 'd', $3, $4, 'open', NOW() - interval '1 hour')`,
|
||||
vulnLow, vulnCrit, serviceID, projectID)
|
||||
must(err)
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM vulnerabilities WHERE project_id = $1`, projectID) }()
|
||||
defer func() { _, _ = db.Exec(`DELETE FROM security_scans WHERE id = $1`, scanID) }()
|
||||
|
||||
// Severity ordering: critical first.
|
||||
vulns, err := q.ListVulnerabilities(ctx, projectID)
|
||||
must(err)
|
||||
if len(vulns) != 2 || vulns[0].ID != vulnCrit {
|
||||
t.Fatalf("vuln ordering: %+v", vulns)
|
||||
}
|
||||
metrics, err := q.GetVulnerabilityMetrics(ctx, projectID)
|
||||
must(err)
|
||||
if metrics.Total != 2 || metrics.Critical != 1 || metrics.Low != 1 || metrics.Open != 2 {
|
||||
t.Fatalf("metrics: %+v", metrics)
|
||||
}
|
||||
must(q.UpdateVulnerabilityStatus(ctx, sqlcdb.UpdateVulnerabilityStatusParams{
|
||||
Status: "resolved",
|
||||
ResolvedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: vulnCrit,
|
||||
}))
|
||||
metrics, err = q.GetVulnerabilityMetrics(ctx, projectID)
|
||||
must(err)
|
||||
if metrics.Open != 1 || metrics.Resolved != 1 {
|
||||
t.Fatalf("metrics after resolve: %+v", metrics)
|
||||
}
|
||||
|
||||
// Latest scan/report lookups.
|
||||
scan, err := q.GetLatestSecurityScan(ctx, projectID)
|
||||
must(err)
|
||||
if scan.ID != scanID || scan.Score != 85 || scan.Status != "completed" {
|
||||
t.Fatalf("latest scan: %+v", scan)
|
||||
}
|
||||
report, err := q.GetLatestComplianceReport(ctx, projectID)
|
||||
must(err)
|
||||
if report.OverallStatus != "compliant" || report.Score != 92 {
|
||||
t.Fatalf("latest report: %+v", report)
|
||||
}
|
||||
|
||||
// Project-ID lookups used by the access guards.
|
||||
scanPID, err := q.GetSecurityScanProjectID(ctx, scanID)
|
||||
must(err)
|
||||
reportPID, err := q.GetComplianceReportProjectID(ctx, reportID)
|
||||
must(err)
|
||||
vulnPID, err := q.GetVulnerabilityProjectID(ctx, vulnCrit)
|
||||
must(err)
|
||||
if scanPID != projectID || reportPID != projectID || vulnPID != projectID {
|
||||
t.Fatalf("project id lookups: %v %v %v", scanPID, reportPID, vulnPID)
|
||||
}
|
||||
|
||||
// Framework list only returns enabled rows.
|
||||
frameworks, err := q.ListComplianceFrameworks(ctx)
|
||||
must(err)
|
||||
found := false
|
||||
for _, f := range frameworks {
|
||||
if f.ID == frameworkID && f.Enabled {
|
||||
found = true
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatal("enabled framework missing from list")
|
||||
}
|
||||
frameworkExists, err := q.ComplianceFrameworkExists(ctx, frameworkID)
|
||||
must(err)
|
||||
if !frameworkExists {
|
||||
t.Fatal("framework should exist")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,226 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
type CloneServiceRequest struct {
|
||||
Name string `json:"name"`
|
||||
ProjectID uuid.UUID `json:"project_id"` // empty = clone within the same project
|
||||
Environment string `json:"environment" binding:"omitempty,oneof=production preview development"`
|
||||
}
|
||||
|
||||
// handleCloneService duplicates a service's full configuration — image/git
|
||||
// source, volumes, domains, access gates, builder settings, and variables —
|
||||
// into the same or another project. Containers are not cloned; the copy
|
||||
// starts stopped until its first deploy.
|
||||
func handleCloneService(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
userID, _ := c.Get("user_id")
|
||||
|
||||
var req CloneServiceRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
source, ok := loadOwnedService(c, db.(*database.DB))
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
|
||||
targetProjectID := source.ProjectID
|
||||
if req.ProjectID != uuid.Nil {
|
||||
targetProjectID = req.ProjectID
|
||||
}
|
||||
if !ownsProject(db.(*database.DB), targetProjectID, userID.(string), contextIsAdmin(c)) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied to target project", "code": "FORBIDDEN"})
|
||||
return
|
||||
}
|
||||
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
name = source.Name + "-copy"
|
||||
}
|
||||
count, _ := sqlcdb.New(db.(*database.DB).DB).CountServicesByName(
|
||||
c.Request.Context(), sqlcdb.CountServicesByNameParams{ProjectID: targetProjectID, Name: name})
|
||||
if count > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "Service name already exists in target project", "code": "CONFLICT"})
|
||||
return
|
||||
}
|
||||
|
||||
environment := source.Environment
|
||||
if req.Environment != "" {
|
||||
environment = req.Environment
|
||||
}
|
||||
|
||||
newID, err := cloneServiceRow(db.(*database.DB), source.ID, targetProjectID, name, environment)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clone service"})
|
||||
return
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "service", source.ID.String(), "service.clone", map[string]interface{}{"cloned_to": newID.String(), "name": name})
|
||||
c.JSON(http.StatusCreated, gin.H{"service_id": newID, "name": name, "message": "Service cloned"})
|
||||
}
|
||||
|
||||
// cloneServiceRow copies a service's full configuration — source, build
|
||||
// settings, volumes, access gates, domains, and variables — under a new id
|
||||
// in the target project. The clone starts stopped. An empty environment
|
||||
// inherits the source's. Used by the clone endpoint and preview
|
||||
// environments, which additionally override git_branch and domains.
|
||||
func cloneServiceRow(db *database.DB, sourceID uuid.UUID, targetProjectID uuid.UUID, name, environment string) (uuid.UUID, error) {
|
||||
ctx := context.Background()
|
||||
q := sqlcdb.New(db.DB)
|
||||
|
||||
sourceEnv, err := q.GetServiceEnvironment(ctx, sourceID)
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
if environment == "" {
|
||||
environment = sourceEnv
|
||||
}
|
||||
if environment == "" {
|
||||
environment = "production"
|
||||
}
|
||||
environmentID, err := getProjectEnvironmentID(db, targetProjectID, environment)
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
maintenance, basicAuth := serviceAccess(db, sourceID)
|
||||
newID := uuid.New()
|
||||
now := time.Now()
|
||||
|
||||
err = q.CloneServiceRow(ctx, sqlcdb.CloneServiceRowParams{
|
||||
ID: newID,
|
||||
ProjectID: targetProjectID,
|
||||
Name: name,
|
||||
EnvironmentID: environmentID,
|
||||
Environment: sql.NullString{String: environment, Valid: true},
|
||||
MaintenanceMode: maintenance,
|
||||
BasicAuthUsers: basicAuth,
|
||||
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
ID_2: sourceID,
|
||||
})
|
||||
if err != nil {
|
||||
return uuid.Nil, err
|
||||
}
|
||||
|
||||
if err := q.CloneServiceDomains(ctx, sqlcdb.CloneServiceDomainsParams{
|
||||
ServiceID: newID, ServiceID_2: sourceID}); err != nil {
|
||||
_ = q.DeleteServiceByID(ctx, newID)
|
||||
return uuid.Nil, err
|
||||
}
|
||||
// Variable values are copied as stored — ciphertext stays ciphertext.
|
||||
if err := q.CloneServiceVariables(ctx, sqlcdb.CloneServiceVariablesParams{
|
||||
ServiceID: newID, ServiceID_2: sourceID}); err != nil {
|
||||
_ = q.DeleteServiceByID(ctx, newID)
|
||||
return uuid.Nil, err
|
||||
}
|
||||
return newID, nil
|
||||
}
|
||||
|
||||
type MoveServiceRequest struct {
|
||||
ProjectID uuid.UUID `json:"project_id" binding:"required"`
|
||||
}
|
||||
|
||||
// handleMoveService re-parents a service to another project owned by the
|
||||
// caller. Variables, domains, and volumes follow via the service id;
|
||||
// running containers keep running (they join the new project's network on
|
||||
// the next deploy).
|
||||
func handleMoveService(c *gin.Context) {
|
||||
db, exists := c.Get("db")
|
||||
if !exists {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Database connection not available"})
|
||||
return
|
||||
}
|
||||
userID, _ := c.Get("user_id")
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid service ID", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
var req MoveServiceRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
service, ok := loadOwnedService(c, db.(*database.DB))
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
if req.ProjectID == service.ProjectID {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Service is already in that project", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if !ownsProject(db.(*database.DB), req.ProjectID, userID.(string), contextIsAdmin(c)) {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "Access denied to target project", "code": "FORBIDDEN"})
|
||||
return
|
||||
}
|
||||
|
||||
count, _ := sqlcdb.New(db.(*database.DB).DB).CountServicesByName(
|
||||
c.Request.Context(), sqlcdb.CountServicesByNameParams{ProjectID: req.ProjectID, Name: service.Name})
|
||||
if count > 0 {
|
||||
c.JSON(http.StatusConflict, gin.H{"error": "A service with this name already exists in the target project", "code": "CONFLICT"})
|
||||
return
|
||||
}
|
||||
|
||||
envName := service.Environment
|
||||
if envName == "" {
|
||||
envName = "production"
|
||||
}
|
||||
environmentID, err := getProjectEnvironmentID(db.(*database.DB), req.ProjectID, envName)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to resolve target environment"})
|
||||
return
|
||||
}
|
||||
|
||||
n, err := sqlcdb.New(db.(*database.DB).DB).MoveServiceProject(
|
||||
c.Request.Context(), sqlcdb.MoveServiceProjectParams{
|
||||
ProjectID: req.ProjectID,
|
||||
EnvironmentID: environmentID,
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
ID: serviceID,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to move service"})
|
||||
return
|
||||
}
|
||||
if n == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Service not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "service", serviceID.String(), "service.move", map[string]interface{}{"to_project": req.ProjectID.String()})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Service moved", "project_id": req.ProjectID})
|
||||
}
|
||||
|
||||
// ownsProject is true when the user owns the project or is an admin.
|
||||
func ownsProject(db *database.DB, projectID uuid.UUID, userID string, isAdmin bool) bool {
|
||||
if isAdmin {
|
||||
return true
|
||||
}
|
||||
uid, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
owner, err := sqlcdb.New(db.DB).GetProjectOwner(context.Background(), projectID)
|
||||
return err == nil && owner == uid
|
||||
}
|
||||
@@ -2,9 +2,16 @@ package api
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/deployqueue"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
@@ -27,53 +34,326 @@ type Service struct {
|
||||
CPU string `json:"cpu" db:"cpu"`
|
||||
Memory string `json:"memory" db:"memory"`
|
||||
// Runtime spec
|
||||
Replicas int `json:"replicas" db:"replicas"`
|
||||
Port int `json:"port" db:"port"` // container port to expose
|
||||
Domain string `json:"domain" db:"domain"` // public hostname via Traefik
|
||||
HealthCheckPath string `json:"healthcheck_path" db:"healthcheck_path"` // probed on Port
|
||||
RestartPolicy string `json:"restart_policy" db:"restart_policy"`
|
||||
PublicURL string `json:"public_url,omitempty" db:"-"` // computed at read time
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
Replicas int `json:"replicas" db:"replicas"`
|
||||
Port int `json:"port" db:"port"` // container port to expose
|
||||
Domain string `json:"domain" db:"domain"` // public hostname via Traefik
|
||||
HealthCheckPath string `json:"healthcheck_path" db:"healthcheck_path"` // probed on Port
|
||||
RestartPolicy string `json:"restart_policy" db:"restart_policy"`
|
||||
PublicURL string `json:"public_url,omitempty" db:"-"` // computed at read time
|
||||
Volumes []ServiceVolume `json:"volumes" db:"-"` // loaded lazily — stored as JSONB
|
||||
Domains []ServiceDomain `json:"domains,omitempty" db:"-"`
|
||||
MaintenanceMode bool `json:"maintenance_mode" db:"-"`
|
||||
BasicAuth []string `json:"basic_auth,omitempty" db:"-"` // usernames only, never hashes
|
||||
Builder string `json:"builder" db:"builder"` // auto|railpack|nixpacks|dockerfile|static
|
||||
CPUReserve string `json:"cpu_reserve,omitempty" db:"cpu_reserve"`
|
||||
MemoryReserve string `json:"memory_reserve,omitempty" db:"memory_reserve"`
|
||||
StaticBuildCmd string `json:"static_build_cmd,omitempty" db:"static_build_cmd"`
|
||||
StaticDir string `json:"static_dir,omitempty" db:"static_dir"`
|
||||
SleepEnabled bool `json:"sleep_enabled" db:"-"`
|
||||
SleepIdleMinutes int `json:"sleep_idle_minutes" db:"-"`
|
||||
// NodeID pins the service to a node agent ("" = local Docker host,
|
||||
// resolved "auto" picks the least-loaded online agent at write time).
|
||||
NodeID string `json:"node_id,omitempty" db:"-"`
|
||||
NodeName string `json:"node_name,omitempty" db:"-"`
|
||||
// Spread distributes replicas across every online, schedulable agent.
|
||||
// Mutually exclusive with an explicit NodeID pin.
|
||||
Spread bool `json:"spread" db:"-"`
|
||||
// PlacementTags restrict auto/spread candidates to agents carrying all
|
||||
// listed tags. An explicit pin overrides them — pin + tags is rejected.
|
||||
PlacementTags []string `json:"placement_tags,omitempty" db:"-"`
|
||||
// TraefikLabels holds per-service middleware overrides —
|
||||
// "middlewares.<name>.<type>.<field>" → value. Lazy-read.
|
||||
TraefikLabels map[string]string `json:"traefik_labels,omitempty" db:"-"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
}
|
||||
|
||||
// loadServiceSleep fills the sleep-mode columns, which live outside the
|
||||
// wide service SELECTs. Missing columns (pre-migration) degrade to off.
|
||||
func loadServiceSleep(db *database.DB, s *Service) {
|
||||
row, err := sqlcdb.New(db.DB).GetServiceSleep(context.Background(), s.ID)
|
||||
if err == nil {
|
||||
s.SleepEnabled = row.SleepEnabled
|
||||
s.SleepIdleMinutes = int(row.SleepIdleMinutes)
|
||||
}
|
||||
}
|
||||
|
||||
// serviceNodeID reads the pin column directly — kept out of the wide service
|
||||
// SELECTs so pre-migration schemas keep working.
|
||||
func serviceNodeID(db *database.DB, serviceID uuid.UUID) string {
|
||||
id, err := sqlcdb.New(db.DB).GetServiceNodeID(context.Background(), serviceID)
|
||||
if err == nil {
|
||||
return id.String
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// serviceSpread reads the spread column with the same lazy pattern.
|
||||
func serviceSpread(db *database.DB, serviceID uuid.UUID) bool {
|
||||
spread, err := sqlcdb.New(db.DB).GetServiceSpread(context.Background(), serviceID)
|
||||
return err == nil && spread
|
||||
}
|
||||
|
||||
// servicePlacementTags reads the placement_tags column with the lazy pattern.
|
||||
func servicePlacementTags(db *database.DB, serviceID uuid.UUID) []string {
|
||||
raw, err := sqlcdb.New(db.DB).GetServicePlacementTags(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var tags []string
|
||||
if err := json.Unmarshal(raw, &tags); err != nil {
|
||||
return nil
|
||||
}
|
||||
return tags
|
||||
}
|
||||
|
||||
// normalizePlacementTags trims, lowercases, and dedupes tag input. Tags are
|
||||
// matched verbatim against node_agents.tags.
|
||||
func normalizePlacementTags(tags []string) []string {
|
||||
seen := map[string]bool{}
|
||||
out := make([]string, 0, len(tags))
|
||||
for _, t := range tags {
|
||||
t = strings.ToLower(strings.TrimSpace(t))
|
||||
if t == "" || seen[t] {
|
||||
continue
|
||||
}
|
||||
seen[t] = true
|
||||
out = append(out, t)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// traefikLabelKey matches "middlewares.<name>.<type>" with an optional
|
||||
// ".<subkey>" tail — e.g. "middlewares.rl.ratelimit.average".
|
||||
var traefikLabelKey = regexp.MustCompile(`^middlewares\.([A-Za-z0-9][A-Za-z0-9_-]{0,39})\.([A-Za-z]+)(\.[A-Za-z0-9_.*+-]+)?$`)
|
||||
|
||||
// traefikMiddlewareTypes are the Traefik middleware kinds a service may
|
||||
// define. Router/service/provider-level keys are never accepted — those
|
||||
// would let a tenant hijack someone else's routing.
|
||||
var traefikMiddlewareTypes = map[string]bool{
|
||||
"addprefix": true, "basicauth": true, "buffering": true, "chain": true,
|
||||
"circuitbreaker": true, "compress": true, "digestauth": true,
|
||||
"errors": true, "forwardauth": true, "grpcweb": true, "headers": true,
|
||||
"inflightreq": true, "ipallowlist": true, "ipwhitelist": true,
|
||||
"passtlsclientcert": true, "ratelimit": true, "redirectregex": true,
|
||||
"redirectscheme": true, "replacepath": true, "replacepathregex": true,
|
||||
"retry": true, "stripprefix": true, "stripprefixregex": true,
|
||||
}
|
||||
|
||||
// normalizeTraefikLabels validates a raw override map. Returns the cleaned
|
||||
// map (middleware type lowercased — Traefik label keys are canonical-lower)
|
||||
// or an error naming the offending key.
|
||||
func normalizeTraefikLabels(raw map[string]string) (map[string]string, error) {
|
||||
if len(raw) > 32 {
|
||||
return nil, fmt.Errorf("traefik_labels: at most 32 entries (got %d)", len(raw))
|
||||
}
|
||||
out := make(map[string]string, len(raw))
|
||||
names := map[string]bool{}
|
||||
for k, v := range raw {
|
||||
m := traefikLabelKey.FindStringSubmatch(k)
|
||||
if m == nil {
|
||||
return nil, fmt.Errorf("traefik_labels: invalid key %q — want middlewares.<name>.<type>[.<field>]", k)
|
||||
}
|
||||
mwType := strings.ToLower(m[2])
|
||||
if !traefikMiddlewareTypes[mwType] {
|
||||
return nil, fmt.Errorf("traefik_labels: unsupported middleware type %q", m[2])
|
||||
}
|
||||
if len(v) == 0 {
|
||||
return nil, fmt.Errorf("traefik_labels: %q has an empty value", k)
|
||||
}
|
||||
if len(v) > 512 || strings.ContainsAny(v, "\r\n") {
|
||||
return nil, fmt.Errorf("traefik_labels: %q value must be a single line under 512 chars", k)
|
||||
}
|
||||
out["middlewares."+m[1]+"."+mwType+m[3]] = v
|
||||
names[m[1]] = true
|
||||
}
|
||||
if len(names) > 16 {
|
||||
return nil, fmt.Errorf("traefik_labels: at most 16 middleware names")
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// serviceTraefikLabels lazy-reads the traefik_labels column.
|
||||
func serviceTraefikLabels(db *database.DB, serviceID uuid.UUID) map[string]string {
|
||||
raw, err := sqlcdb.New(db.DB).GetServiceTraefikLabels(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var labels map[string]string
|
||||
if err := json.Unmarshal(raw, &labels); err != nil || len(labels) == 0 {
|
||||
return nil
|
||||
}
|
||||
return labels
|
||||
}
|
||||
|
||||
// traefikLabelsJSON marshals an override map for the jsonb column.
|
||||
func traefikLabelsJSON(labels map[string]string) []byte {
|
||||
if len(labels) == 0 {
|
||||
return []byte("{}")
|
||||
}
|
||||
raw, _ := json.Marshal(labels)
|
||||
return raw
|
||||
}
|
||||
|
||||
// tagsJSON marshals a tag list for a jsonb column; empty stays an array, not null.
|
||||
func tagsJSON(tags []string) []byte {
|
||||
if len(tags) == 0 {
|
||||
return []byte("[]")
|
||||
}
|
||||
raw, _ := json.Marshal(tags)
|
||||
return raw
|
||||
}
|
||||
|
||||
// loadServiceNode fills the pin + spread + placement tags + the agent's
|
||||
// display name for responses.
|
||||
func loadServiceNode(db *database.DB, s *Service) {
|
||||
s.NodeID = serviceNodeID(db, s.ID)
|
||||
s.Spread = serviceSpread(db, s.ID)
|
||||
s.PlacementTags = servicePlacementTags(db, s.ID)
|
||||
s.TraefikLabels = serviceTraefikLabels(db, s.ID)
|
||||
if s.NodeID == "" {
|
||||
return
|
||||
}
|
||||
if name, err := sqlcdb.New(db.DB).GetNodeAgentName(context.Background(), s.NodeID); err == nil {
|
||||
s.NodeName = name
|
||||
}
|
||||
}
|
||||
|
||||
// ServiceVolume is a volume/bind mount applied to every replica.
|
||||
type ServiceVolume struct {
|
||||
Type string `json:"type"` // volume | bind (empty = volume)
|
||||
Source string `json:"source"` // volume name or host path
|
||||
Target string `json:"target"` // container path (absolute)
|
||||
ReadOnly bool `json:"read_only"`
|
||||
}
|
||||
|
||||
// loadServiceVolumes reads the JSONB column; absent/invalid data degrades
|
||||
// to no mounts rather than failing the request.
|
||||
func loadServiceVolumes(db *database.DB, serviceID uuid.UUID) []deployment.VolumeMount {
|
||||
raw, err := sqlcdb.New(db.DB).GetServiceVolumes(context.Background(), serviceID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
var stored []ServiceVolume
|
||||
if err := json.Unmarshal(raw, &stored); err != nil {
|
||||
return nil
|
||||
}
|
||||
out := make([]deployment.VolumeMount, 0, len(stored))
|
||||
for _, v := range stored {
|
||||
out = append(out, deployment.VolumeMount{
|
||||
Type: v.Type,
|
||||
Source: v.Source,
|
||||
Destination: v.Target,
|
||||
ReadOnly: v.ReadOnly,
|
||||
})
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// validateServiceVolumes enforces the mount contract — bad paths fail
|
||||
// fast at write time instead of surfacing as docker create errors later.
|
||||
func validateServiceVolumes(vols []ServiceVolume) error {
|
||||
for i, v := range vols {
|
||||
if v.Type == "" {
|
||||
v.Type = "volume"
|
||||
}
|
||||
if v.Type != "volume" && v.Type != "bind" {
|
||||
return fmt.Errorf("volumes[%d].type must be volume or bind", i)
|
||||
}
|
||||
if strings.TrimSpace(v.Source) == "" {
|
||||
return fmt.Errorf("volumes[%d].source is required", i)
|
||||
}
|
||||
if !strings.HasPrefix(v.Target, "/") {
|
||||
return fmt.Errorf("volumes[%d].target must be an absolute container path", i)
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// CreateServiceRequest represents a request to create a service
|
||||
type CreateServiceRequest struct {
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
Name string `json:"name" binding:"required,min=1,max=255"`
|
||||
Type string `json:"type" binding:"required,oneof=web worker database cron"`
|
||||
Image string `json:"image"`
|
||||
Command string `json:"command"`
|
||||
Environment string `json:"environment" binding:"required,oneof=production preview development"`
|
||||
GitRepo string `json:"git_repo"`
|
||||
GitBranch string `json:"git_branch"`
|
||||
BuildPath string `json:"build_path"`
|
||||
CPU string `json:"cpu"`
|
||||
Memory string `json:"memory"`
|
||||
Replicas int `json:"replicas"`
|
||||
Port int `json:"port"`
|
||||
Domain string `json:"domain"`
|
||||
HealthCheckPath string `json:"healthcheck_path"`
|
||||
RestartPolicy string `json:"restart_policy"`
|
||||
ProjectID uuid.UUID `json:"project_id"`
|
||||
Name string `json:"name" binding:"required,min=1,max=255"`
|
||||
Type string `json:"type" binding:"required,oneof=web worker database cron"`
|
||||
Image string `json:"image"`
|
||||
Command string `json:"command"`
|
||||
Environment string `json:"environment" binding:"required,oneof=production preview development"`
|
||||
GitRepo string `json:"git_repo"`
|
||||
GitBranch string `json:"git_branch"`
|
||||
BuildPath string `json:"build_path"`
|
||||
CPU string `json:"cpu"`
|
||||
Memory string `json:"memory"`
|
||||
Replicas int `json:"replicas"`
|
||||
Port int `json:"port"`
|
||||
Domain string `json:"domain"`
|
||||
HealthCheckPath string `json:"healthcheck_path"`
|
||||
RestartPolicy string `json:"restart_policy"`
|
||||
Volumes []ServiceVolume `json:"volumes"`
|
||||
MaintenanceMode bool `json:"maintenance_mode"`
|
||||
BasicAuth []BasicAuthCred `json:"basic_auth"`
|
||||
Builder string `json:"builder" binding:"omitempty,oneof=auto railpack nixpacks dockerfile static"`
|
||||
CPUReserve string `json:"cpu_reserve"`
|
||||
MemoryReserve string `json:"memory_reserve"`
|
||||
StaticBuildCmd string `json:"static_build_cmd"`
|
||||
StaticDir string `json:"static_dir"`
|
||||
// SleepEnabled/SleepIdleMinutes opt the service into scale-to-zero at
|
||||
// create time — previously update-only, which surprised API/CLI users.
|
||||
SleepEnabled *bool `json:"sleep_enabled"`
|
||||
SleepIdleMinutes *int `json:"sleep_idle_minutes"`
|
||||
// NodeID pins placement to a node agent; "auto" picks the least-loaded
|
||||
// online agent, "local"/"" runs on the Containr host.
|
||||
NodeID string `json:"node_id"`
|
||||
// Spread distributes replicas across every online, schedulable agent.
|
||||
// Mutually exclusive with an explicit node pin.
|
||||
Spread *bool `json:"spread"`
|
||||
// PlacementTags restrict auto/spread candidates to agents carrying all
|
||||
// listed tags. Mutually exclusive with an explicit node pin.
|
||||
PlacementTags []string `json:"placement_tags"`
|
||||
// TraefikLabels defines extra Traefik middlewares for this service's
|
||||
// router — keys "middlewares.<name>.<type>[.<field>]", validated against
|
||||
// an allowlist. Defined middlewares attach to the router automatically.
|
||||
TraefikLabels map[string]string `json:"traefik_labels"`
|
||||
}
|
||||
|
||||
// UpdateServiceRequest represents a request to update a service
|
||||
type UpdateServiceRequest struct {
|
||||
Name string `json:"name" binding:"omitempty,min=1,max=255"`
|
||||
Type string `json:"type" binding:"omitempty,oneof=web worker database cron"`
|
||||
Image string `json:"image"`
|
||||
Command string `json:"command"`
|
||||
Environment string `json:"environment" binding:"omitempty,oneof=production preview development"`
|
||||
GitRepo string `json:"git_repo"`
|
||||
GitBranch string `json:"git_branch"`
|
||||
BuildPath string `json:"build_path"`
|
||||
CPU string `json:"cpu"`
|
||||
Memory string `json:"memory"`
|
||||
Replicas *int `json:"replicas"`
|
||||
Port *int `json:"port"`
|
||||
Domain *string `json:"domain"`
|
||||
HealthCheckPath *string `json:"healthcheck_path"`
|
||||
RestartPolicy string `json:"restart_policy"`
|
||||
Name string `json:"name" binding:"omitempty,min=1,max=255"`
|
||||
Type string `json:"type" binding:"omitempty,oneof=web worker database cron"`
|
||||
Image string `json:"image"`
|
||||
Command string `json:"command"`
|
||||
Environment string `json:"environment" binding:"omitempty,oneof=production preview development"`
|
||||
GitRepo string `json:"git_repo"`
|
||||
GitBranch string `json:"git_branch"`
|
||||
BuildPath string `json:"build_path"`
|
||||
CPU string `json:"cpu"`
|
||||
Memory string `json:"memory"`
|
||||
Replicas *int `json:"replicas"`
|
||||
Port *int `json:"port"`
|
||||
Domain *string `json:"domain"`
|
||||
HealthCheckPath *string `json:"healthcheck_path"`
|
||||
RestartPolicy string `json:"restart_policy"`
|
||||
Volumes *[]ServiceVolume `json:"volumes"`
|
||||
MaintenanceMode *bool `json:"maintenance_mode"`
|
||||
BasicAuth *[]BasicAuthCred `json:"basic_auth"`
|
||||
Builder string `json:"builder" binding:"omitempty,oneof=auto railpack nixpacks dockerfile static"`
|
||||
CPUReserve *string `json:"cpu_reserve"`
|
||||
MemoryReserve *string `json:"memory_reserve"`
|
||||
StaticBuildCmd *string `json:"static_build_cmd"`
|
||||
StaticDir *string `json:"static_dir"`
|
||||
SleepEnabled *bool `json:"sleep_enabled"`
|
||||
SleepIdleMinutes *int `json:"sleep_idle_minutes"`
|
||||
// NodeID pins placement; pointer so "local"/"" explicitly clears the pin.
|
||||
NodeID *string `json:"node_id"`
|
||||
// Spread toggles multi-node replica distribution; pointer so false is
|
||||
// a real update, not a missing field.
|
||||
Spread *bool `json:"spread"`
|
||||
// PlacementTags restrict auto/spread candidates to agents carrying all
|
||||
// listed tags; pointer so [] clears the requirement. Mutually exclusive
|
||||
// with an explicit node pin.
|
||||
PlacementTags *[]string `json:"placement_tags"`
|
||||
// TraefikLabels replaces the service's middleware overrides; pointer so
|
||||
// {} clears them and nil leaves them untouched.
|
||||
TraefikLabels *map[string]string `json:"traefik_labels"`
|
||||
}
|
||||
|
||||
// handleGetServices retrieves all services for a project
|
||||
@@ -118,6 +398,10 @@ func handleGetServices(c *gin.Context) {
|
||||
COALESCE(replicas, 1), COALESCE(port, 0),
|
||||
COALESCE(domain, ''), COALESCE(healthcheck_path, ''),
|
||||
COALESCE(restart_policy, 'unless-stopped'),
|
||||
COALESCE(builder, 'auto'), COALESCE(cpu_reserve, ''),
|
||||
COALESCE(memory_reserve, ''), COALESCE(static_build_cmd, ''),
|
||||
COALESCE(static_dir, ''),
|
||||
COALESCE(sleep_enabled, false), COALESCE(sleep_idle_minutes, 15),
|
||||
created_at, updated_at
|
||||
FROM services
|
||||
WHERE project_id = $1
|
||||
@@ -138,7 +422,10 @@ func handleGetServices(c *gin.Context) {
|
||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt,
|
||||
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||
&service.SleepEnabled, &service.SleepIdleMinutes,
|
||||
&service.CreatedAt, &service.UpdatedAt,
|
||||
)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to scan service"})
|
||||
@@ -150,6 +437,7 @@ func handleGetServices(c *gin.Context) {
|
||||
// Reconcile stored status with live container state (no-op without Docker).
|
||||
for i := range services {
|
||||
liveServiceStatus(c, db.(*database.DB), &services[i])
|
||||
loadServiceNode(db.(*database.DB), &services[i])
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"services": services})
|
||||
@@ -249,12 +537,46 @@ func handleCreateService(c *gin.Context) {
|
||||
UpdatedAt: time.Now(),
|
||||
}
|
||||
|
||||
// Set default values if not provided
|
||||
// Set default values if not provided; app_settings.default_cpu /
|
||||
// default_memory override the hardcoded fallbacks.
|
||||
if service.CPU == "" {
|
||||
service.CPU = "0.5"
|
||||
service.CPU = firstNonEmpty(settingValue(db.(*database.DB), "default_cpu", "", ""), "0.5")
|
||||
}
|
||||
if service.Memory == "" {
|
||||
service.Memory = "512Mi"
|
||||
service.Memory = firstNonEmpty(settingValue(db.(*database.DB), "default_memory", "", ""), "512Mi")
|
||||
}
|
||||
if req.Builder == "" {
|
||||
service.Builder = "auto"
|
||||
} else {
|
||||
service.Builder = req.Builder
|
||||
}
|
||||
service.CPUReserve = req.CPUReserve
|
||||
service.MemoryReserve = req.MemoryReserve
|
||||
service.StaticBuildCmd = req.StaticBuildCmd
|
||||
service.StaticDir = req.StaticDir
|
||||
if service.Builder == "static" && service.StaticDir == "" {
|
||||
service.StaticDir = "dist"
|
||||
}
|
||||
if len(req.TraefikLabels) > 0 {
|
||||
labels, err := normalizeTraefikLabels(req.TraefikLabels)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", err.Error())
|
||||
return
|
||||
}
|
||||
service.TraefikLabels = labels
|
||||
}
|
||||
for label, check := range map[string]func(string) error{
|
||||
"cpu": validateCPUSpec, "cpu_reserve": validateCPUSpec,
|
||||
"memory": validateMemorySpec, "memory_reserve": validateMemorySpec,
|
||||
} {
|
||||
v := map[string]string{
|
||||
"cpu": service.CPU, "cpu_reserve": service.CPUReserve,
|
||||
"memory": service.Memory, "memory_reserve": service.MemoryReserve,
|
||||
}[label]
|
||||
if err := check(v); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid %s: %s", label, err.Error()), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
}
|
||||
if service.Replicas < 1 {
|
||||
service.Replicas = 1
|
||||
@@ -262,6 +584,15 @@ func handleCreateService(c *gin.Context) {
|
||||
if service.RestartPolicy == "" {
|
||||
service.RestartPolicy = "unless-stopped"
|
||||
}
|
||||
if err := validateServiceVolumes(req.Volumes); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
service.Volumes = req.Volumes
|
||||
volumesJSON, _ := json.Marshal(req.Volumes)
|
||||
if len(req.Volumes) == 0 {
|
||||
volumesJSON = []byte("[]")
|
||||
}
|
||||
|
||||
environmentID, err := getProjectEnvironmentID(db.(*database.DB), service.ProjectID, service.Environment)
|
||||
if err != nil {
|
||||
@@ -271,19 +602,64 @@ func handleCreateService(c *gin.Context) {
|
||||
|
||||
sourceType := inferServiceSourceType(service)
|
||||
|
||||
if req.SleepEnabled != nil {
|
||||
service.SleepEnabled = *req.SleepEnabled
|
||||
}
|
||||
if req.SleepIdleMinutes != nil {
|
||||
if *req.SleepIdleMinutes < 1 || *req.SleepIdleMinutes > 1440 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "sleep_idle_minutes must be 1-1440", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
service.SleepIdleMinutes = *req.SleepIdleMinutes
|
||||
}
|
||||
if req.Spread != nil {
|
||||
service.Spread = *req.Spread
|
||||
}
|
||||
service.PlacementTags = normalizePlacementTags(req.PlacementTags)
|
||||
if req.NodeID != "" {
|
||||
if service.Spread && req.NodeID != "auto" && req.NodeID != "local" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "spread and an explicit node pin are mutually exclusive", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if len(service.PlacementTags) > 0 && req.NodeID != "auto" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "placement tags only apply to remote placement — set node_id=\"auto\" or omit it", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
nodeID, nodeErr := resolveNodePin(c.Request.Context(), db.(*database.DB), req.NodeID, service.PlacementTags)
|
||||
if nodeErr != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": nodeErr.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if !service.Spread {
|
||||
service.NodeID = nodeID
|
||||
}
|
||||
}
|
||||
|
||||
// Insert service into database
|
||||
_, err = db.(*database.DB).Exec(
|
||||
`INSERT INTO services
|
||||
(id, project_id, name, environment_id, service_type, source_type, source_url, image_name,
|
||||
build_command, start_command, type, status, image, command, environment,
|
||||
git_repo, git_branch, build_path, cpu, memory, replicas, port, domain,
|
||||
healthcheck_path, restart_policy, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26, $27)`,
|
||||
healthcheck_path, restart_policy, volumes,
|
||||
builder, cpu_reserve, memory_reserve, static_build_cmd, static_dir,
|
||||
node_id, spread, placement_tags, sleep_enabled, sleep_idle_minutes,
|
||||
traefik_labels, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20, $21, $22, $23, $24, $25, $26,
|
||||
$27, $28, $29, $30, $31, $32, $33, $34, $35, $36, $37, $38, $39)`,
|
||||
service.ID, service.ProjectID, service.Name, environmentID, service.Type,
|
||||
sourceType, firstNonEmpty(service.GitRepo, service.Image), service.Image,
|
||||
"", service.Command, service.Type, service.Status, service.Image, service.Command,
|
||||
service.Environment, service.GitRepo, service.GitBranch, service.BuildPath, service.CPU, service.Memory,
|
||||
service.Replicas, service.Port, service.Domain, service.HealthCheckPath, service.RestartPolicy,
|
||||
volumesJSON, service.Builder, service.CPUReserve, service.MemoryReserve,
|
||||
service.StaticBuildCmd, service.StaticDir,
|
||||
sql.NullString{String: service.NodeID, Valid: service.NodeID != ""},
|
||||
service.Spread,
|
||||
tagsJSON(service.PlacementTags),
|
||||
service.SleepEnabled,
|
||||
sql.NullInt32{Int32: int32(service.SleepIdleMinutes), Valid: service.SleepIdleMinutes > 0},
|
||||
traefikLabelsJSON(service.TraefikLabels),
|
||||
service.CreatedAt, service.UpdatedAt,
|
||||
)
|
||||
|
||||
@@ -292,17 +668,41 @@ func handleCreateService(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Domains live in service_domains; services.domain is the derived default.
|
||||
if req.Domain != "" && validHostname(strings.ToLower(req.Domain)) {
|
||||
_, _ = db.(*database.DB).Exec(
|
||||
`INSERT INTO service_domains (service_id, domain, is_default) VALUES ($1, $2, true)
|
||||
ON CONFLICT (service_id, domain) DO UPDATE SET is_default = true`,
|
||||
service.ID, strings.ToLower(req.Domain))
|
||||
}
|
||||
if req.MaintenanceMode || len(req.BasicAuth) > 0 {
|
||||
basicAuthUsers := ""
|
||||
if encoded, encErr := encodeBasicAuth(req.BasicAuth); encErr == nil {
|
||||
basicAuthUsers = encoded
|
||||
}
|
||||
_, _ = db.(*database.DB).Exec(
|
||||
`UPDATE services SET maintenance_mode = $1, basic_auth_users = $2 WHERE id = $3`,
|
||||
req.MaintenanceMode, basicAuthUsers, service.ID)
|
||||
service.MaintenanceMode = req.MaintenanceMode
|
||||
service.BasicAuth = basicAuthUsernames(basicAuthUsers)
|
||||
}
|
||||
service.Domains = loadServiceDomains(db.(*database.DB), service.ID)
|
||||
|
||||
LogAuditWithRequest(c, "service", service.ID.String(), "create", map[string]interface{}{
|
||||
"name": service.Name, "type": service.Type, "project_id": service.ProjectID,
|
||||
"node_id": service.NodeID,
|
||||
})
|
||||
loadServiceNode(db.(*database.DB), &service)
|
||||
|
||||
c.JSON(http.StatusCreated, gin.H{"service": service})
|
||||
}
|
||||
|
||||
func getProjectEnvironmentID(db *database.DB, projectID uuid.UUID, environment string) (uuid.UUID, error) {
|
||||
var environmentID uuid.UUID
|
||||
err := db.QueryRow(
|
||||
"SELECT id FROM environments WHERE project_id = $1 AND name = $2",
|
||||
projectID,
|
||||
environment,
|
||||
).Scan(&environmentID)
|
||||
return environmentID, err
|
||||
row, err := sqlcdb.New(db.DB).GetProjectEnvironmentByName(context.Background(), sqlcdb.GetProjectEnvironmentByNameParams{
|
||||
ProjectID: projectID,
|
||||
Name: environment,
|
||||
})
|
||||
return row.ID, err
|
||||
}
|
||||
|
||||
func inferServiceSourceType(service Service) string {
|
||||
@@ -349,6 +749,9 @@ func handleGetService(c *gin.Context) {
|
||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||
COALESCE(s.static_dir, ''),
|
||||
s.created_at, s.updated_at
|
||||
FROM services s
|
||||
JOIN projects p ON s.project_id = p.id
|
||||
@@ -360,7 +763,9 @@ func handleGetService(c *gin.Context) {
|
||||
&service.Image, &service.Command, &service.Environment, &service.GitRepo,
|
||||
&service.GitBranch, &service.BuildPath, &service.CPU, &service.Memory,
|
||||
&service.Replicas, &service.Port, &service.Domain, &service.HealthCheckPath,
|
||||
&service.RestartPolicy, &service.CreatedAt, &service.UpdatedAt,
|
||||
&service.RestartPolicy, &service.Builder, &service.CPUReserve,
|
||||
&service.MemoryReserve, &service.StaticBuildCmd, &service.StaticDir,
|
||||
&service.CreatedAt, &service.UpdatedAt,
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -369,6 +774,17 @@ func handleGetService(c *gin.Context) {
|
||||
}
|
||||
|
||||
liveServiceStatus(c, db.(*database.DB), &service)
|
||||
for _, v := range loadServiceVolumes(db.(*database.DB), service.ID) {
|
||||
service.Volumes = append(service.Volumes, ServiceVolume{
|
||||
Type: v.Type, Source: v.Source, Target: v.Destination, ReadOnly: v.ReadOnly,
|
||||
})
|
||||
}
|
||||
service.Domains = loadServiceDomains(db.(*database.DB), service.ID)
|
||||
loadServiceSleep(db.(*database.DB), &service)
|
||||
loadServiceNode(db.(*database.DB), &service)
|
||||
maintenance, basicAuth := serviceAccess(db.(*database.DB), service.ID)
|
||||
service.MaintenanceMode = maintenance
|
||||
service.BasicAuth = basicAuthUsernames(basicAuth)
|
||||
c.JSON(http.StatusOK, gin.H{"service": service})
|
||||
}
|
||||
|
||||
@@ -417,6 +833,9 @@ func handleUpdateService(c *gin.Context) {
|
||||
COALESCE(s.replicas, 1), COALESCE(s.port, 0),
|
||||
COALESCE(s.domain, ''), COALESCE(s.healthcheck_path, ''),
|
||||
COALESCE(s.restart_policy, 'unless-stopped'),
|
||||
COALESCE(s.builder, 'auto'), COALESCE(s.cpu_reserve, ''),
|
||||
COALESCE(s.memory_reserve, ''), COALESCE(s.static_build_cmd, ''),
|
||||
COALESCE(s.static_dir, ''),
|
||||
s.created_at, s.updated_at
|
||||
FROM services s
|
||||
JOIN projects p ON s.project_id = p.id
|
||||
@@ -429,6 +848,9 @@ func handleUpdateService(c *gin.Context) {
|
||||
&existingService.BuildPath, &existingService.CPU, &existingService.Memory,
|
||||
&existingService.Replicas, &existingService.Port, &existingService.Domain,
|
||||
&existingService.HealthCheckPath, &existingService.RestartPolicy,
|
||||
&existingService.Builder, &existingService.CPUReserve,
|
||||
&existingService.MemoryReserve, &existingService.StaticBuildCmd,
|
||||
&existingService.StaticDir,
|
||||
&existingService.CreatedAt, &existingService.UpdatedAt,
|
||||
)
|
||||
|
||||
@@ -483,6 +905,108 @@ func handleUpdateService(c *gin.Context) {
|
||||
if req.RestartPolicy != "" {
|
||||
existingService.RestartPolicy = req.RestartPolicy
|
||||
}
|
||||
if req.Builder != "" {
|
||||
existingService.Builder = req.Builder
|
||||
}
|
||||
if req.CPUReserve != nil {
|
||||
existingService.CPUReserve = *req.CPUReserve
|
||||
}
|
||||
if req.MemoryReserve != nil {
|
||||
existingService.MemoryReserve = *req.MemoryReserve
|
||||
}
|
||||
if req.StaticBuildCmd != nil {
|
||||
existingService.StaticBuildCmd = *req.StaticBuildCmd
|
||||
}
|
||||
if req.StaticDir != nil {
|
||||
existingService.StaticDir = *req.StaticDir
|
||||
}
|
||||
if existingService.Builder == "static" && existingService.StaticDir == "" {
|
||||
existingService.StaticDir = "dist"
|
||||
}
|
||||
loadServiceSleep(db.(*database.DB), &existingService)
|
||||
if req.SleepEnabled != nil {
|
||||
existingService.SleepEnabled = *req.SleepEnabled
|
||||
}
|
||||
if req.SleepIdleMinutes != nil {
|
||||
if *req.SleepIdleMinutes < 1 || *req.SleepIdleMinutes > 1440 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "sleep_idle_minutes must be 1-1440", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
existingService.SleepIdleMinutes = *req.SleepIdleMinutes
|
||||
}
|
||||
if req.TraefikLabels != nil {
|
||||
labels, err := normalizeTraefikLabels(*req.TraefikLabels)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", err.Error())
|
||||
return
|
||||
}
|
||||
existingService.TraefikLabels = labels
|
||||
} else {
|
||||
existingService.TraefikLabels = serviceTraefikLabels(db.(*database.DB), serviceID)
|
||||
}
|
||||
for label, check := range map[string]func(string) error{
|
||||
"cpu": validateCPUSpec, "cpu_reserve": validateCPUSpec,
|
||||
"memory": validateMemorySpec, "memory_reserve": validateMemorySpec,
|
||||
} {
|
||||
v := map[string]string{
|
||||
"cpu": existingService.CPU, "cpu_reserve": existingService.CPUReserve,
|
||||
"memory": existingService.Memory, "memory_reserve": existingService.MemoryReserve,
|
||||
}[label]
|
||||
if err := check(v); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("invalid %s: %s", label, err.Error()), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
}
|
||||
// lib/pq sends a nil []byte as an empty bytea literal rather than NULL,
|
||||
// so the COALESCE fallback needs an untyped nil when volumes aren't sent.
|
||||
var volumesArg interface{}
|
||||
if req.Volumes != nil {
|
||||
if err := validateServiceVolumes(*req.Volumes); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
existingService.Volumes = *req.Volumes
|
||||
volumesJSON, _ := json.Marshal(*req.Volumes)
|
||||
volumesArg = volumesJSON
|
||||
}
|
||||
|
||||
if req.Spread != nil {
|
||||
existingService.Spread = *req.Spread
|
||||
} else {
|
||||
existingService.Spread = serviceSpread(db.(*database.DB), serviceID)
|
||||
}
|
||||
if req.PlacementTags != nil {
|
||||
existingService.PlacementTags = normalizePlacementTags(*req.PlacementTags)
|
||||
} else {
|
||||
existingService.PlacementTags = servicePlacementTags(db.(*database.DB), serviceID)
|
||||
}
|
||||
if req.NodeID != nil {
|
||||
nodeValue := strings.TrimSpace(*req.NodeID)
|
||||
if existingService.Spread && nodeValue != "" && nodeValue != "auto" && nodeValue != "local" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "spread and an explicit node pin are mutually exclusive — set spread=false first", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if len(existingService.PlacementTags) > 0 && nodeValue != "" && nodeValue != "auto" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "service has placement tags — clear them (placement_tags=[]) before pinning or going local", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
nodeID, nodeErr := resolveNodePin(c.Request.Context(), db.(*database.DB), nodeValue, existingService.PlacementTags)
|
||||
if nodeErr != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": nodeErr.Error(), "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
existingService.NodeID = nodeID
|
||||
} else {
|
||||
existingService.NodeID = serviceNodeID(db.(*database.DB), serviceID)
|
||||
}
|
||||
if existingService.Spread && existingService.NodeID != "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "service already has an explicit node pin — clear it (node_id=\"local\") before enabling spread", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if len(existingService.PlacementTags) > 0 && existingService.NodeID != "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "service already has an explicit node pin — clear it (node_id=\"local\") before setting placement tags", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
existingService.UpdatedAt = time.Now()
|
||||
|
||||
@@ -492,14 +1016,25 @@ func handleUpdateService(c *gin.Context) {
|
||||
SET name = $1, type = $2, image = $3, command = $4, environment = $5,
|
||||
git_repo = $6, git_branch = $7, build_path = $8, cpu = $9, memory = $10,
|
||||
replicas = $11, port = $12, domain = $13, healthcheck_path = $14,
|
||||
restart_policy = $15, updated_at = $16
|
||||
WHERE id = $17`,
|
||||
restart_policy = $15, volumes = COALESCE($17::jsonb, volumes), updated_at = $16,
|
||||
builder = $19, cpu_reserve = $20, memory_reserve = $21,
|
||||
static_build_cmd = $22, static_dir = $23,
|
||||
sleep_enabled = $24, sleep_idle_minutes = $25, node_id = $26, spread = $27,
|
||||
placement_tags = $28::jsonb, traefik_labels = $29::jsonb
|
||||
WHERE id = $18`,
|
||||
existingService.Name, existingService.Type, existingService.Image, existingService.Command,
|
||||
existingService.Environment, existingService.GitRepo, existingService.GitBranch,
|
||||
existingService.BuildPath, existingService.CPU, existingService.Memory,
|
||||
existingService.Replicas, existingService.Port, existingService.Domain,
|
||||
existingService.HealthCheckPath, existingService.RestartPolicy,
|
||||
existingService.UpdatedAt, existingService.ID,
|
||||
existingService.UpdatedAt, volumesArg, existingService.ID,
|
||||
existingService.Builder, existingService.CPUReserve, existingService.MemoryReserve,
|
||||
existingService.StaticBuildCmd, existingService.StaticDir,
|
||||
existingService.SleepEnabled, existingService.SleepIdleMinutes,
|
||||
sql.NullString{String: existingService.NodeID, Valid: existingService.NodeID != ""},
|
||||
existingService.Spread,
|
||||
tagsJSON(existingService.PlacementTags),
|
||||
traefikLabelsJSON(existingService.TraefikLabels),
|
||||
)
|
||||
|
||||
if err != nil {
|
||||
@@ -507,26 +1042,69 @@ func handleUpdateService(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Domain writes route through service_domains — services.domain is the
|
||||
// derived default and gets rewritten by syncDefaultDomain.
|
||||
if req.Domain != nil {
|
||||
domain := strings.ToLower(strings.TrimSpace(*req.Domain))
|
||||
if domain == "" {
|
||||
_, _ = db.(*database.DB).Exec(`DELETE FROM service_domains WHERE service_id = $1`, serviceID)
|
||||
} else if validHostname(domain) {
|
||||
_, _ = db.(*database.DB).Exec(`UPDATE service_domains SET is_default = false WHERE service_id = $1`, serviceID)
|
||||
_, _ = db.(*database.DB).Exec(
|
||||
`INSERT INTO service_domains (service_id, domain, is_default) VALUES ($1, $2, true)
|
||||
ON CONFLICT (service_id, domain) DO UPDATE SET is_default = true`,
|
||||
serviceID, domain)
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid hostname", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
syncDefaultDomain(db.(*database.DB), serviceID)
|
||||
_ = db.(*database.DB).QueryRow(`SELECT domain FROM services WHERE id = $1`, serviceID).Scan(&existingService.Domain)
|
||||
}
|
||||
|
||||
if req.MaintenanceMode != nil || req.BasicAuth != nil {
|
||||
maintenance, basicAuth := serviceAccess(db.(*database.DB), serviceID)
|
||||
if req.MaintenanceMode != nil {
|
||||
maintenance = *req.MaintenanceMode
|
||||
}
|
||||
if req.BasicAuth != nil {
|
||||
encoded, encErr := encodeBasicAuth(*req.BasicAuth)
|
||||
if encErr != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to encode basic auth", "code": "INTERNAL"})
|
||||
return
|
||||
}
|
||||
basicAuth = encoded
|
||||
}
|
||||
_, _ = db.(*database.DB).Exec(
|
||||
`UPDATE services SET maintenance_mode = $1, basic_auth_users = $2 WHERE id = $3`,
|
||||
maintenance, basicAuth, serviceID)
|
||||
existingService.MaintenanceMode = maintenance
|
||||
}
|
||||
existingService.Domains = loadServiceDomains(db.(*database.DB), serviceID)
|
||||
_, storedAuth := serviceAccess(db.(*database.DB), serviceID)
|
||||
existingService.BasicAuth = basicAuthUsernames(storedAuth)
|
||||
|
||||
// A replica change applies immediately when the service has live
|
||||
// containers; other spec fields take effect on the next deploy.
|
||||
// Runs through the deploy queue so it can't overlap a build.
|
||||
if req.Replicas != nil && existingService.Status == "running" {
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
engine := engineValue.(*deployment.DeploymentEngine)
|
||||
serviceCopy := existingService
|
||||
go func() {
|
||||
spec, err := serviceRuntimeSpec(db.(*database.DB), serviceCopy)
|
||||
dbCopy := db.(*database.DB)
|
||||
getDeployQueue(c).Enqueue(serviceCopy.ID, deployqueue.Job{Run: func(jctx context.Context) {
|
||||
spec, err := serviceRuntimeSpec(dbCopy, serviceCopy)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
|
||||
defer cancel()
|
||||
if state, err := engine.ReconcileService(ctx, spec); err == nil && state != nil {
|
||||
persistPublishedPort(db.(*database.DB), serviceCopy.ID, state.Ports)
|
||||
if state, err := engine.ReconcileService(jctx, spec); err == nil && state != nil {
|
||||
persistPublishedPort(dbCopy, serviceCopy.ID, state.Ports)
|
||||
}
|
||||
}()
|
||||
}})
|
||||
}
|
||||
}
|
||||
|
||||
loadServiceNode(db.(*database.DB), &existingService)
|
||||
c.JSON(http.StatusOK, gin.H{"service": existingService})
|
||||
}
|
||||
|
||||
@@ -576,7 +1154,7 @@ func handleDeleteService(c *gin.Context) {
|
||||
// Remove runtime containers before dropping the row.
|
||||
if engineValue, exists := c.Get("deployment_engine"); exists && engineValue != nil {
|
||||
if engine, ok := engineValue.(*deployment.DeploymentEngine); ok {
|
||||
_ = engine.RemoveServiceContainers(c.Request.Context(), serviceID.String())
|
||||
removeServiceRuntime(c.Request.Context(), db.(*database.DB), engine, serviceID)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -591,5 +1169,7 @@ func handleDeleteService(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
LogAuditWithRequest(c, "service", serviceID.String(), "delete", map[string]interface{}{})
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Service deleted successfully"})
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeTraefikLabels(t *testing.T) {
|
||||
valid := map[string]string{
|
||||
"middlewares.rl.ratelimit.average": "100",
|
||||
"middlewares.rl.ratelimit.period": "1m",
|
||||
"middlewares.sec.headers.customrequestheaders.X-Frame-Options": "DENY",
|
||||
"middlewares.gzip.compress": "true",
|
||||
}
|
||||
out, err := normalizeTraefikLabels(valid)
|
||||
if err != nil {
|
||||
t.Fatalf("valid labels rejected: %v", err)
|
||||
}
|
||||
if len(out) != len(valid) {
|
||||
t.Fatalf("expected %d labels, got %d", len(valid), len(out))
|
||||
}
|
||||
if out["middlewares.gzip.compress"] != "true" {
|
||||
t.Fatalf("flag-style middleware missing: %v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeTraefikLabelsLowercasesType(t *testing.T) {
|
||||
out, err := normalizeTraefikLabels(map[string]string{
|
||||
"middlewares.rl.RateLimit.average": "10",
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("mixed-case type rejected: %v", err)
|
||||
}
|
||||
if out["middlewares.rl.ratelimit.average"] != "10" {
|
||||
t.Fatalf("type not lowercased: %v", out)
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeTraefikLabelsRejects(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"traefik.enable": "true", // provider-level
|
||||
"middlewares..ratelimit.average": "1", // empty name
|
||||
"middlewares.x.evilinject.thing": "1", // unknown type
|
||||
"routers.svc.rule": "x", // router hijack
|
||||
"middlewares.x.ratelimit.average.bad\ninjection": "1", // control char in key fails regex
|
||||
}
|
||||
for k, v := range cases {
|
||||
if _, err := normalizeTraefikLabels(map[string]string{k: v}); err == nil {
|
||||
t.Fatalf("key %q should have been rejected", k)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestNormalizeTraefikLabelsRejectsBadValues(t *testing.T) {
|
||||
if _, err := normalizeTraefikLabels(map[string]string{"middlewares.a.ratelimit.average": ""}); err == nil {
|
||||
t.Fatal("empty value accepted")
|
||||
}
|
||||
if _, err := normalizeTraefikLabels(map[string]string{"middlewares.a.ratelimit.average": strings.Repeat("x", 600)}); err == nil {
|
||||
t.Fatal("oversized value accepted")
|
||||
}
|
||||
if _, err := normalizeTraefikLabels(map[string]string{"middlewares.a.ratelimit.average": "1\nmalicious"}); err == nil {
|
||||
t.Fatal("newline value accepted")
|
||||
}
|
||||
}
|
||||
@@ -10,11 +10,13 @@ import (
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/docker"
|
||||
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Platform setting keys. Values stored in app_settings take precedence over
|
||||
@@ -23,16 +25,54 @@ const (
|
||||
settingSignupEnabled = "signup_enabled"
|
||||
settingCloudflareTunnelToken = "cloudflare_tunnel_token"
|
||||
|
||||
// White-label branding keys — unset fields fall back to Containr
|
||||
// defaults. Self-hosters rebrand their instance via these.
|
||||
settingBrandProductName = "branding.product_name"
|
||||
settingBrandLogoURL = "branding.logo_url"
|
||||
settingBrandFaviconURL = "branding.favicon_url"
|
||||
settingBrandAccent = "branding.accent_color"
|
||||
settingBrandDocsURL = "branding.docs_url"
|
||||
settingBrandSupportURL = "branding.support_url"
|
||||
|
||||
cloudflaredContainerName = "containr-cloudflared"
|
||||
cloudflaredImage = "cloudflare/cloudflared:latest"
|
||||
)
|
||||
|
||||
// brandingConfig is the public white-label payload. Fields are always
|
||||
// populated — defaults keep the instance Containr-branded.
|
||||
type brandingConfig struct {
|
||||
ProductName string `json:"product_name"`
|
||||
LogoURL string `json:"logo_url"`
|
||||
FaviconURL string `json:"favicon_url"`
|
||||
AccentColor string `json:"accent_color"`
|
||||
DocsURL string `json:"docs_url"`
|
||||
SupportURL string `json:"support_url"`
|
||||
}
|
||||
|
||||
func resolveBranding(db *database.DB) brandingConfig {
|
||||
return brandingConfig{
|
||||
ProductName: settingValue(db, settingBrandProductName, "BRAND_NAME", "Containr"),
|
||||
LogoURL: settingValue(db, settingBrandLogoURL, "BRAND_LOGO_URL", ""),
|
||||
FaviconURL: settingValue(db, settingBrandFaviconURL, "BRAND_FAVICON_URL", ""),
|
||||
AccentColor: settingValue(db, settingBrandAccent, "BRAND_ACCENT_COLOR", ""),
|
||||
DocsURL: settingValue(db, settingBrandDocsURL, "BRAND_DOCS_URL", ""),
|
||||
SupportURL: settingValue(db, settingBrandSupportURL, "BRAND_SUPPORT_URL", ""),
|
||||
}
|
||||
}
|
||||
|
||||
// GET /branding — public so the login page and shell can brand themselves
|
||||
// before a session exists.
|
||||
func handleGetBranding(c *gin.Context) {
|
||||
db, _ := c.Get("db")
|
||||
database, _ := db.(*database.DB)
|
||||
c.JSON(http.StatusOK, resolveBranding(database))
|
||||
}
|
||||
|
||||
// settingValue resolves a setting: app_settings row first, env fallback,
|
||||
// then the caller's default.
|
||||
func settingValue(db *database.DB, key, envKey, fallback string) string {
|
||||
if db != nil && db.DB != nil {
|
||||
var value string
|
||||
err := db.QueryRow(`SELECT value FROM app_settings WHERE key = $1`, key).Scan(&value)
|
||||
value, err := sqlcdb.New(db.DB).GetAppSetting(context.Background(), key)
|
||||
if err == nil {
|
||||
return value
|
||||
}
|
||||
@@ -49,18 +89,15 @@ func settingValue(db *database.DB, key, envKey, fallback string) string {
|
||||
}
|
||||
|
||||
func setSetting(db *database.DB, key, value string, secret bool) error {
|
||||
_, err := db.Exec(`
|
||||
INSERT INTO app_settings (key, value, is_secret, updated_at)
|
||||
VALUES ($1, $2, $3, NOW())
|
||||
ON CONFLICT (key) DO UPDATE
|
||||
SET value = EXCLUDED.value, is_secret = EXCLUDED.is_secret, updated_at = NOW()
|
||||
`, key, value, secret)
|
||||
return err
|
||||
return sqlcdb.New(db.DB).UpsertAppSetting(context.Background(), sqlcdb.UpsertAppSettingParams{
|
||||
Key: key,
|
||||
Value: value,
|
||||
IsSecret: secret,
|
||||
})
|
||||
}
|
||||
|
||||
func deleteSetting(db *database.DB, key string) error {
|
||||
_, err := db.Exec(`DELETE FROM app_settings WHERE key = $1`, key)
|
||||
return err
|
||||
return sqlcdb.New(db.DB).DeleteAppSetting(context.Background(), key)
|
||||
}
|
||||
|
||||
// signupEnabled controls whether public registration stays open after the
|
||||
@@ -71,11 +108,12 @@ func signupEnabled(db *database.DB) bool {
|
||||
}
|
||||
|
||||
func isAdminUser(db *database.DB, userID string) bool {
|
||||
var isAdmin bool
|
||||
if err := db.QueryRow(`SELECT is_admin FROM users WHERE id = $1`, userID).Scan(&isAdmin); err != nil {
|
||||
parsed, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return isAdmin
|
||||
isAdmin, err := sqlcdb.New(db.DB).GetUserIsAdmin(context.Background(), parsed)
|
||||
return err == nil && isAdmin
|
||||
}
|
||||
|
||||
func requireAdmin(c *gin.Context) (*database.DB, bool) {
|
||||
@@ -110,6 +148,7 @@ func handleGetSettings(c *gin.Context) {
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"signup_enabled": signupEnabled(db),
|
||||
"cloudflare_tunnel": cloudflareTunnelState(c.Request.Context(), db, client),
|
||||
"branding": resolveBranding(db),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -122,6 +161,14 @@ func handleUpdateSettings(c *gin.Context) {
|
||||
var req struct {
|
||||
SignupEnabled *bool `json:"signup_enabled"`
|
||||
CloudflareTunnelToken *string `json:"cloudflare_tunnel_token"`
|
||||
Branding *struct {
|
||||
ProductName *string `json:"product_name"`
|
||||
LogoURL *string `json:"logo_url"`
|
||||
FaviconURL *string `json:"favicon_url"`
|
||||
AccentColor *string `json:"accent_color"`
|
||||
DocsURL *string `json:"docs_url"`
|
||||
SupportURL *string `json:"support_url"`
|
||||
} `json:"branding"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -158,14 +205,43 @@ func handleUpdateSettings(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
if req.Branding != nil {
|
||||
fields := []struct {
|
||||
key string
|
||||
value *string
|
||||
}{
|
||||
{settingBrandProductName, req.Branding.ProductName},
|
||||
{settingBrandLogoURL, req.Branding.LogoURL},
|
||||
{settingBrandFaviconURL, req.Branding.FaviconURL},
|
||||
{settingBrandAccent, req.Branding.AccentColor},
|
||||
{settingBrandDocsURL, req.Branding.DocsURL},
|
||||
{settingBrandSupportURL, req.Branding.SupportURL},
|
||||
}
|
||||
for _, f := range fields {
|
||||
if f.value == nil {
|
||||
continue
|
||||
}
|
||||
v := strings.TrimSpace(*f.value)
|
||||
var err error
|
||||
if v == "" {
|
||||
err = deleteSetting(db, f.key)
|
||||
} else {
|
||||
err = setSetting(db, f.key, v, false)
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to save branding"})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
handleGetSettings(c)
|
||||
}
|
||||
|
||||
// resolvedTunnelToken prefers the in-app value and falls back to the env.
|
||||
func resolvedTunnelToken(db *database.DB) (string, string) {
|
||||
if db != nil && db.DB != nil {
|
||||
var value string
|
||||
if err := db.QueryRow(`SELECT value FROM app_settings WHERE key = $1`, settingCloudflareTunnelToken).Scan(&value); err == nil && strings.TrimSpace(value) != "" {
|
||||
if value, err := sqlcdb.New(db.DB).GetAppSetting(context.Background(), settingCloudflareTunnelToken); err == nil && strings.TrimSpace(value) != "" {
|
||||
return strings.TrimSpace(value), "app"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
package api
|
||||
|
||||
// First-run setup wizard state. GET /setup/status is public so the web
|
||||
// shell can redirect fresh installs before a session exists; the flag
|
||||
// lives in app_settings so it survives restarts.
|
||||
|
||||
import (
|
||||
"context"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
const settingSetupCompleted = "setup_completed"
|
||||
|
||||
// setupComplete resolves the flag. Installs that predate the wizard
|
||||
// (users + projects already exist) auto-complete — nobody who has been
|
||||
// running Containr for months should get a welcome screen.
|
||||
func setupComplete(db *database.DB, hasUsers bool) bool {
|
||||
if strings.EqualFold(settingValue(db, settingSetupCompleted, "", ""), "true") {
|
||||
return true
|
||||
}
|
||||
if !hasUsers {
|
||||
return false
|
||||
}
|
||||
if projects, err := sqlcdb.New(db.DB).CountProjects(context.Background()); err == nil && projects > 0 {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// GET /api/v1/setup/status — public: fresh installs hit it before any
|
||||
// session exists.
|
||||
func handleSetupStatus(c *gin.Context) {
|
||||
db, ok := c.MustGet("db").(*database.DB)
|
||||
if !ok {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Database connection not available")
|
||||
return
|
||||
}
|
||||
count, err := countLocalUsers(db)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to inspect setup state")
|
||||
return
|
||||
}
|
||||
hasUsers := count > 0
|
||||
completed := setupComplete(db, hasUsers)
|
||||
tunnelToken, _ := resolvedTunnelToken(db)
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"has_users": hasUsers,
|
||||
"setup_completed": completed,
|
||||
"needs_setup": !completed,
|
||||
"has_github_app": githubAppConfigured(db),
|
||||
"has_tunnel": tunnelToken != "",
|
||||
"signup_enabled": signupEnabled(db),
|
||||
})
|
||||
}
|
||||
|
||||
// POST /api/v1/setup/complete — marks the wizard done. Open to any
|
||||
// authenticated user: the first account is the owner, and the flag only
|
||||
// suppresses a banner.
|
||||
func handleSetupComplete(c *gin.Context) {
|
||||
if _, ok := requireAuthenticatedUserID(c); !ok {
|
||||
return
|
||||
}
|
||||
db, ok := c.MustGet("db").(*database.DB)
|
||||
if !ok {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Database connection not available")
|
||||
return
|
||||
}
|
||||
if err := setSetting(db, settingSetupCompleted, "true", false); err != nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL_ERROR", "Failed to save setup state")
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Setup completed"})
|
||||
}
|
||||
@@ -0,0 +1,571 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/docker"
|
||||
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/docker/docker/api/types/network"
|
||||
"github.com/docker/docker/api/types/registry"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Sleep mode: services with sleep_enabled are scaled to zero after
|
||||
// sleep_idle_minutes without inbound traffic. Idleness is measured as
|
||||
// NetworkIO byte deltas across the service's containers — we have no
|
||||
// per-request layer in front of Traefik, so counters are the signal.
|
||||
//
|
||||
// When a service sleeps its containers are removed and a tiny busybox
|
||||
// "wake" container takes over the same Traefik host rule at a higher
|
||||
// priority: it serves a reloading "waking" page and pings the unauthenticated
|
||||
// internal wake endpoint, which reconciles the service back to running.
|
||||
// Wake containers carry a dedicated label (not the service label) so
|
||||
// RuntimeState and reconcile never count them as replicas; the sweeper
|
||||
// prunes them once the workload is running again.
|
||||
|
||||
const (
|
||||
wakeImage = "busybox:1.36"
|
||||
wakeLabel = "containr.wake"
|
||||
wakeForLabel = "containr.wake-for"
|
||||
wakeSweepInterval = 30 * time.Second
|
||||
)
|
||||
|
||||
type sleepTracker struct {
|
||||
mu sync.Mutex
|
||||
activity map[uuid.UUID]sleepActivity
|
||||
}
|
||||
|
||||
type sleepActivity struct {
|
||||
bytes uint64
|
||||
idleSince time.Time
|
||||
}
|
||||
|
||||
var tracker = &sleepTracker{activity: make(map[uuid.UUID]sleepActivity)}
|
||||
|
||||
// StartSleepSweeper polls enabled services and scales idle ones to zero.
|
||||
func StartSleepSweeper(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, apiPort int) {
|
||||
if db == nil || dc == nil || engine == nil {
|
||||
return
|
||||
}
|
||||
go func() {
|
||||
ticker := time.NewTicker(wakeSweepInterval)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return
|
||||
case <-ticker.C:
|
||||
sweepSleeping(ctx, db, dc, engine, apiPort)
|
||||
pruneWakeContainers(ctx, db, dc, engine)
|
||||
}
|
||||
}
|
||||
}()
|
||||
log.Printf("sleep sweeper: running (interval %s)", wakeSweepInterval)
|
||||
}
|
||||
|
||||
type sleepCandidate struct {
|
||||
ID uuid.UUID
|
||||
ProjectID uuid.UUID
|
||||
Name string
|
||||
Status string
|
||||
IdleMinutes int
|
||||
}
|
||||
|
||||
func sweepSleeping(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, apiPort int) {
|
||||
sweepRows, err := sqlcdb.New(db.DB).ListSleepCandidates(ctx)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var candidates []sleepCandidate
|
||||
for _, r := range sweepRows {
|
||||
candidates = append(candidates, sleepCandidate{
|
||||
ID: r.ID, ProjectID: r.ProjectID, Name: r.Name,
|
||||
Status: r.Status, IdleMinutes: int(r.SleepIdleMinutes),
|
||||
})
|
||||
}
|
||||
|
||||
q := sqlcdb.New(db.DB)
|
||||
for _, svc := range candidates {
|
||||
// Remote replicas live in inventory, not the local docker host —
|
||||
// idleness comes from heartbeat-reported net counters instead.
|
||||
if remote := remoteLiveContainers(ctx, q, svc.ID.String()); len(remote) > 0 {
|
||||
var total uint64
|
||||
for _, row := range remote {
|
||||
total += rowNetBytes(row)
|
||||
}
|
||||
if markIdle(svc.ID, total, time.Duration(svc.IdleMinutes)*time.Minute) {
|
||||
putToSleepRemote(ctx, db, q, svc, apiPort)
|
||||
}
|
||||
continue
|
||||
}
|
||||
containers, err := engine.ListServiceContainers(ctx, svc.ID.String())
|
||||
if err != nil || len(containers) == 0 {
|
||||
continue
|
||||
}
|
||||
// Ignore wake containers — they're the placeholder, not the workload.
|
||||
workload := containers[:0]
|
||||
for _, c := range containers {
|
||||
if c.Labels[wakeLabel] != "true" {
|
||||
workload = append(workload, c)
|
||||
}
|
||||
}
|
||||
if len(workload) == 0 {
|
||||
continue
|
||||
}
|
||||
var total uint64
|
||||
for _, c := range workload {
|
||||
total += containerNetBytes(ctx, dc, c.ID)
|
||||
}
|
||||
if markIdle(svc.ID, total, time.Duration(svc.IdleMinutes)*time.Minute) {
|
||||
putToSleep(ctx, db, dc, engine, svc, apiPort)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// remoteLiveContainers returns inventory rows whose replicas are alive on
|
||||
// node agents — the signal that idleness must be measured remotely.
|
||||
func remoteLiveContainers(ctx context.Context, q *sqlcdb.Queries, serviceID string) []sqlcdb.ListServiceContainersRow {
|
||||
rows, err := q.ListServiceContainers(ctx, serviceID)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
live := rows[:0]
|
||||
for _, row := range rows {
|
||||
var st struct {
|
||||
State string `json:"state"`
|
||||
}
|
||||
if err := json.Unmarshal(row.Status.RawMessage, &st); err == nil &&
|
||||
st.State != "" && st.State != "removed" && st.State != "sleeping" {
|
||||
live = append(live, row)
|
||||
}
|
||||
}
|
||||
return live
|
||||
}
|
||||
|
||||
// rowNetBytes reads the cumulative rx+tx the agent reported at its last
|
||||
// heartbeat — 0 for rows written before the counter existed.
|
||||
func rowNetBytes(row sqlcdb.ListServiceContainersRow) uint64 {
|
||||
var res struct {
|
||||
NetBytes uint64 `json:"net_bytes"`
|
||||
}
|
||||
_ = json.Unmarshal(row.Resources.RawMessage, &res)
|
||||
return res.NetBytes
|
||||
}
|
||||
|
||||
// putToSleepRemote removes the service's remote replicas via their agents,
|
||||
// marks the service sleeping, and repoints the domain route at the wake
|
||||
// endpoint so inbound traffic brings it back.
|
||||
func putToSleepRemote(ctx context.Context, db *database.DB, q *sqlcdb.Queries, svc sleepCandidate, apiPort int) {
|
||||
runner := newAgentNodeRunner(db)
|
||||
for _, agentID := range remoteAgentsForService(ctx, db, svc.ID) {
|
||||
if err := runner.ControlService(ctx, svc.ID.String(), agentID, "remove"); err != nil {
|
||||
log.Printf("sleep: remote remove on %s failed: %v", agentID, err)
|
||||
continue
|
||||
}
|
||||
_ = q.DeleteServiceContainersOnAgent(ctx, sqlcdb.DeleteServiceContainersOnAgentParams{
|
||||
NodeAgentID: agentID,
|
||||
ServiceID: svc.ID.String(),
|
||||
})
|
||||
}
|
||||
tracker.mu.Lock()
|
||||
delete(tracker.activity, svc.ID)
|
||||
tracker.mu.Unlock()
|
||||
setServiceStatusNow(db, svc.ID, "sleeping")
|
||||
log.Printf("sleep: %s (%s) remote replicas removed after idle timeout", svc.Name, svc.ID)
|
||||
|
||||
domain, _ := q.GetServiceDomainText(ctx, svc.ID)
|
||||
domains := serviceDomainNames(db, svc.ID, domain)
|
||||
if len(domains) > 0 {
|
||||
writeWakeIngress(svc.ID.String(), domains, apiPort)
|
||||
}
|
||||
}
|
||||
|
||||
// markIdle returns true when the network counter stopped growing past the
|
||||
// timeout. First sighting only sets the baseline — no idle without history.
|
||||
func markIdle(id uuid.UUID, bytes uint64, timeout time.Duration) bool {
|
||||
tracker.mu.Lock()
|
||||
defer tracker.mu.Unlock()
|
||||
a := tracker.activity[id]
|
||||
if bytes != a.bytes || a.idleSince.IsZero() {
|
||||
tracker.activity[id] = sleepActivity{bytes: bytes, idleSince: time.Now()}
|
||||
return false
|
||||
}
|
||||
return time.Since(a.idleSince) >= timeout
|
||||
}
|
||||
|
||||
func containerNetBytes(ctx context.Context, dc *docker.Client, id string) uint64 {
|
||||
stats, err := dc.GetContainerStats(ctx, id, false)
|
||||
if err != nil || stats == nil {
|
||||
return 0
|
||||
}
|
||||
defer stats.Body.Close()
|
||||
var snap struct {
|
||||
Networks map[string]struct {
|
||||
RxBytes uint64 `json:"rx_bytes"`
|
||||
TxBytes uint64 `json:"tx_bytes"`
|
||||
} `json:"networks"`
|
||||
}
|
||||
if err := json.NewDecoder(stats.Body).Decode(&snap); err != nil {
|
||||
return 0
|
||||
}
|
||||
var total uint64
|
||||
for _, n := range snap.Networks {
|
||||
total += n.RxBytes + n.TxBytes
|
||||
}
|
||||
return total
|
||||
}
|
||||
|
||||
// putToSleep removes the workload containers, marks the service sleeping,
|
||||
// and installs the wake placeholder when the service has a public domain.
|
||||
func putToSleep(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, svc sleepCandidate, apiPort int) {
|
||||
containers, err := engine.ListServiceContainers(ctx, svc.ID.String())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, c := range containers {
|
||||
if c.Labels[wakeLabel] == "true" {
|
||||
continue
|
||||
}
|
||||
_ = dc.StopContainer(ctx, c.ID, nil)
|
||||
_ = dc.RemoveContainer(ctx, c.ID, true)
|
||||
}
|
||||
tracker.mu.Lock()
|
||||
delete(tracker.activity, svc.ID)
|
||||
tracker.mu.Unlock()
|
||||
setServiceStatusNow(db, svc.ID, "sleeping")
|
||||
log.Printf("sleep: %s (%s) scaled to zero after idle timeout", svc.Name, svc.ID)
|
||||
|
||||
domain, _ := sqlcdb.New(db.DB).GetServiceDomainText(ctx, svc.ID)
|
||||
domains := serviceDomainNames(db, svc.ID, domain)
|
||||
if len(domains) == 0 {
|
||||
return
|
||||
}
|
||||
if err := spawnWakeContainer(ctx, db, dc, engine, svc, domains, apiPort); err != nil {
|
||||
log.Printf("sleep: wake placeholder for %s failed: %v", svc.Name, err)
|
||||
}
|
||||
}
|
||||
|
||||
// wakeCGIScript is installed as /cgi-bin/wake in the wake container. It
|
||||
// fires the wake callback on the first reachable candidate URL, in the
|
||||
// background, then serves the reloading placeholder page. Deliberately
|
||||
// single-quote-free — the file is written through printf '%s'.
|
||||
const wakeCGIScript = `#!/bin/sh
|
||||
for u in $WAKE_URLS; do wget -q -O /dev/null --timeout=20 "$u" && break; done >/dev/null 2>&1 &
|
||||
printf "Content-Type: text/html\r\n\r\n"
|
||||
printf "%s" "$WAKE_PAGE"
|
||||
`
|
||||
|
||||
// spawnWakeContainer runs a busybox httpd holding the service's Traefik
|
||||
// host rule at higher priority. Inbound hits redirect to /cgi-bin/wake,
|
||||
// which calls the internal wake endpoint and serves a reloading page
|
||||
// until the reconcile removes the placeholder.
|
||||
func spawnWakeContainer(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine, svc sleepCandidate, domains []string, apiPort int) error {
|
||||
edgeNet := ""
|
||||
for _, n := range networkList(ctx, dc) {
|
||||
if n == "containr-network" {
|
||||
edgeNet = n
|
||||
}
|
||||
}
|
||||
if edgeNet == "" {
|
||||
return fmt.Errorf("no edge network")
|
||||
}
|
||||
router := "svc-" + svc.ID.String()[:8]
|
||||
sort.Strings(domains)
|
||||
rules := make([]string, 0, len(domains))
|
||||
for _, d := range domains {
|
||||
rules = append(rules, "Host(`"+d+"`)")
|
||||
}
|
||||
labels := map[string]string{
|
||||
deployment.LabelManaged: "true",
|
||||
deployment.LabelProject: svc.ProjectID.String(),
|
||||
wakeForLabel: svc.ID.String(),
|
||||
wakeLabel: "true",
|
||||
"traefik.enable": "true",
|
||||
"traefik.docker.network": edgeNet,
|
||||
"traefik.http.routers." + router + ".rule": strings.Join(rules, " || "),
|
||||
"traefik.http.routers." + router + ".entrypoints": "web",
|
||||
"traefik.http.routers." + router + ".priority": "100",
|
||||
"traefik.http.services." + router + ".loadbalancer.server.port": "80",
|
||||
}
|
||||
|
||||
wakeURLs := wakeEndpointURLs(ctx, dc, apiPort, svc.ID, edgeNet)
|
||||
page := `<!doctype html><html><head><meta http-equiv="refresh" content="4"><title>Waking up</title>` +
|
||||
`<style>body{font-family:system-ui,sans-serif;background:#0b0d12;color:#dde2ea;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0}` +
|
||||
`.card{text-align:center;max-width:34rem;padding:2rem}h1{font-size:1.25rem;font-weight:600}p{color:#98a2b3}</style></head>` +
|
||||
`<body><div class="card"><h1>Service is waking up</h1><p>This Containr service was sleeping and is resuming now. The page reloads automatically.</p></div></body></html>`
|
||||
// The wake callback must fire when a request arrives, not at boot —
|
||||
// firing unconditionally makes the service flap sleep→wake forever.
|
||||
// index.html redirects to /cgi-bin/wake; the CGI fires the callback
|
||||
// (first reachable URL wins) and serves the reloading page.
|
||||
indexPage := `<!doctype html><html><head><meta http-equiv="refresh" content="0;url=/cgi-bin/wake"></head><body></body></html>`
|
||||
// httpd without -f daemonizes, so PID1 ends on sleep infinity. URLs
|
||||
// and the page travel via env so the CGI script needs no quoting.
|
||||
cmd := fmt.Sprintf(
|
||||
`mkdir -p /www/cgi-bin && printf '%%s' '%s' > /www/index.html && `+
|
||||
`printf '%%s' '%s' > /www/cgi-bin/wake && chmod +x /www/cgi-bin/wake && `+
|
||||
`printf '%%s\n' 'E404:/cgi-bin/wake' > /www/httpd.conf && `+
|
||||
`httpd -p 80 -h /www && sleep infinity`,
|
||||
strings.ReplaceAll(indexPage, "'", "'\\''"),
|
||||
strings.ReplaceAll(wakeCGIScript, "'", "'\\''"))
|
||||
|
||||
endpoints := map[string]*network.EndpointSettings{edgeNet: {}}
|
||||
if ownNet := dc.OwnNetworkName(ctx); ownNet != "" && ownNet != edgeNet {
|
||||
endpoints[ownNet] = &network.EndpointSettings{}
|
||||
}
|
||||
|
||||
// Pull is best-effort — the image almost always sits in cache.
|
||||
_ = dc.PullImageWait(ctx, wakeImage, registry.AuthConfig{})
|
||||
|
||||
// A previous placeholder may linger as Exited — clear the name first.
|
||||
name := fmt.Sprintf("containr-wake-%s", svc.ID.String()[:8])
|
||||
if stale, err := dc.ListContainersFiltered(ctx, filters.NewArgs(
|
||||
filters.Arg("name", "^/"+name+"$"),
|
||||
), true); err == nil {
|
||||
for _, s := range stale {
|
||||
_ = dc.RemoveContainer(ctx, s.ID, true)
|
||||
}
|
||||
}
|
||||
|
||||
id, err := dc.CreateContainer(ctx, docker.ContainerConfig{
|
||||
Name: name,
|
||||
Image: wakeImage,
|
||||
Cmd: []string{"sh", "-c", cmd},
|
||||
Env: []string{
|
||||
"WAKE_URLS=" + strings.Join(wakeURLs, " "),
|
||||
"WAKE_PAGE=" + page,
|
||||
},
|
||||
Labels: labels,
|
||||
Networks: endpoints,
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return dc.StartContainer(ctx, id)
|
||||
}
|
||||
|
||||
func networkList(ctx context.Context, dc *docker.Client) []string {
|
||||
networks, err := dc.ListNetworks(ctx)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
names := make([]string, 0, len(networks))
|
||||
for _, n := range networks {
|
||||
names = append(names, n.Name)
|
||||
}
|
||||
return names
|
||||
}
|
||||
|
||||
// wakeEndpointURLs builds ordered candidate URLs for the wake container to
|
||||
// call, deduplicated. No single address covers every topology: a containerized
|
||||
// API resolves by name on the shared network; Docker Desktop containers reach
|
||||
// the host via host.docker.internal; on native Docker the edge network's
|
||||
// gateway routes to host-published ports. busybox wget walks the chain until
|
||||
// one answers.
|
||||
func wakeEndpointURLs(ctx context.Context, dc *docker.Client, apiPort int, serviceID uuid.UUID, edgeNet string) []string {
|
||||
var hosts []string
|
||||
if dc.OwnNetworkName(ctx) != "" {
|
||||
if self, err := dc.GetContainer(ctx, selfID()); err == nil {
|
||||
if name := strings.TrimPrefix(self.Name, "/"); name != "" {
|
||||
hosts = append(hosts, name)
|
||||
}
|
||||
}
|
||||
}
|
||||
hosts = append(hosts, "host.docker.internal")
|
||||
if gw := networkGateway(ctx, dc, edgeNet); gw != "" {
|
||||
hosts = append(hosts, gw)
|
||||
}
|
||||
if probe := dc.HostProbeHost(ctx); probe != "" {
|
||||
hosts = append(hosts, probe)
|
||||
}
|
||||
seen := map[string]bool{}
|
||||
var urls []string
|
||||
for _, h := range hosts {
|
||||
if h == "" || seen[h] {
|
||||
continue
|
||||
}
|
||||
seen[h] = true
|
||||
urls = append(urls, fmt.Sprintf("http://%s:%d/api/v1/internal/wake/%s", h, apiPort, serviceID))
|
||||
}
|
||||
if len(urls) == 0 {
|
||||
urls = append(urls, fmt.Sprintf("http://127.0.0.1:%d/api/v1/internal/wake/%s", apiPort, serviceID))
|
||||
}
|
||||
return urls
|
||||
}
|
||||
|
||||
func networkGateway(ctx context.Context, dc *docker.Client, name string) string {
|
||||
// ListNetworks does not populate IPAM config — inspect is required.
|
||||
return dc.NetworkGateway(ctx, name)
|
||||
}
|
||||
|
||||
func selfID() string {
|
||||
// Inside a container the hostname is the short container ID.
|
||||
if hn, err := os.Hostname(); err == nil {
|
||||
return hn
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
// pruneWakeContainers removes wake placeholders whose service is healthy
|
||||
// again — belt & braces over the reconcile drift sweep.
|
||||
func pruneWakeContainers(ctx context.Context, db *database.DB, dc *docker.Client, engine *deployment.DeploymentEngine) {
|
||||
containers, err := dc.ListContainersFiltered(ctx, filters.NewArgs(filters.Arg("label", wakeLabel+"=true")), true)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, c := range containers {
|
||||
svcID := c.Labels[wakeForLabel]
|
||||
if svcID == "" {
|
||||
continue
|
||||
}
|
||||
running, _ := engine.ListServiceContainers(ctx, svcID)
|
||||
live := 0
|
||||
for _, r := range running {
|
||||
if r.State == "running" {
|
||||
live++
|
||||
}
|
||||
}
|
||||
var status string
|
||||
if sid, err := uuid.Parse(svcID); err == nil {
|
||||
status, _ = sqlcdb.New(db.DB).GetServiceStatusText(ctx, sid)
|
||||
}
|
||||
if live > 0 || status == "stopped" || status == "" {
|
||||
_ = dc.RemoveContainer(ctx, c.ID, true)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handleServiceWake is the unauthenticated internal endpoint the wake
|
||||
// container hits. It reconciles the service back to running. Waking is
|
||||
// idempotent and non-destructive, so no token is required; the endpoint
|
||||
// never mutates configuration.
|
||||
func handleServiceWake(c *gin.Context) {
|
||||
code, body := triggerServiceWake(c)
|
||||
if code != 0 {
|
||||
c.JSON(code, body)
|
||||
}
|
||||
}
|
||||
|
||||
// handleServiceWakePage is the remote ingress wake target — Traefik routes
|
||||
// a sleeping remote service's domain here via the file provider. Every hit
|
||||
// (re)triggers the wake reconcile and returns a reloading page.
|
||||
func handleServiceWakePage(c *gin.Context) {
|
||||
triggerServiceWake(c)
|
||||
c.Header("Content-Type", "text/html; charset=utf-8")
|
||||
c.String(http.StatusOK, wakePageHTML)
|
||||
}
|
||||
|
||||
const wakePageHTML = `<!doctype html><html><head><meta http-equiv="refresh" content="4"><title>Waking up</title>` +
|
||||
`<style>body{font-family:system-ui,sans-serif;background:#0b0d12;color:#dde2ea;display:flex;align-items:center;justify-content:center;min-height:100vh;margin:0}` +
|
||||
`.card{text-align:center;max-width:34rem;padding:2rem}h1{font-size:1.25rem;font-weight:600}p{color:#98a2b3}</style></head>` +
|
||||
`<body><div class="card"><h1>Service is waking up</h1><p>This Containr service was sleeping and is resuming now. The page reloads automatically.</p></div></body></html>`
|
||||
|
||||
// triggerServiceWake loads the service and reconciles it back to running
|
||||
// in the background. Returns (0, nil) when a response was already written
|
||||
// by an embedded handler path, else the JSON status to send.
|
||||
func triggerServiceWake(c *gin.Context) (int, gin.H) {
|
||||
dbVal, _ := c.Get("db")
|
||||
db := dbVal.(*database.DB)
|
||||
engineVal, _ := c.Get("deployment_engine")
|
||||
engine, _ := engineVal.(*deployment.DeploymentEngine)
|
||||
if engine == nil {
|
||||
return http.StatusServiceUnavailable, gin.H{"error": "deployment engine unavailable"}
|
||||
}
|
||||
id, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
return http.StatusBadRequest, gin.H{"error": "invalid service id"}
|
||||
}
|
||||
row, err := sqlcdb.New(db.DB).GetServiceRuntimeWithOwner(context.Background(), id)
|
||||
if err != nil {
|
||||
return http.StatusNotFound, gin.H{"error": "service not found"}
|
||||
}
|
||||
service := serviceFromRuntimeRow(row)
|
||||
if service.Status == "running" || service.Status == "deployed" {
|
||||
return http.StatusOK, gin.H{"status": service.Status}
|
||||
}
|
||||
go func() {
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
defer cancel()
|
||||
spec, err := serviceRuntimeSpec(db, service)
|
||||
if err != nil {
|
||||
log.Printf("wake: runtime spec for %s failed: %v", service.ID, err)
|
||||
setServiceStatusNow(db, service.ID, "failed")
|
||||
return
|
||||
}
|
||||
state, err := engine.ReconcileService(ctx, spec)
|
||||
if err != nil {
|
||||
log.Printf("wake: reconcile for %s failed: %v", service.ID, err)
|
||||
setServiceStatusNow(db, service.ID, "failed")
|
||||
return
|
||||
}
|
||||
status := "running"
|
||||
if state != nil {
|
||||
status = state.Status
|
||||
persistPublishedPort(db, service.ID, state.Ports)
|
||||
}
|
||||
setServiceStatusNow(db, service.ID, status)
|
||||
}()
|
||||
return http.StatusAccepted, gin.H{"status": "waking"}
|
||||
}
|
||||
|
||||
// handleServiceSleep puts a service to sleep immediately (owner+ only).
|
||||
func handleServiceSleep(c *gin.Context) {
|
||||
dbVal, _ := c.Get("db")
|
||||
db := dbVal.(*database.DB)
|
||||
engineVal, _ := c.Get("deployment_engine")
|
||||
engine, _ := engineVal.(*deployment.DeploymentEngine)
|
||||
dcVal, _ := c.Get("docker_client")
|
||||
dc, _ := dcVal.(*docker.Client)
|
||||
if engine == nil || dc == nil {
|
||||
c.JSON(http.StatusServiceUnavailable, gin.H{"error": "deployment engine unavailable"})
|
||||
return
|
||||
}
|
||||
service, ok := loadOwnedService(c, db)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
svc := sleepCandidate{
|
||||
ID: service.ID,
|
||||
ProjectID: service.ProjectID,
|
||||
Name: service.Name,
|
||||
Status: service.Status,
|
||||
}
|
||||
q := sqlcdb.New(db.DB)
|
||||
if len(remoteLiveContainers(c.Request.Context(), q, service.ID.String())) > 0 {
|
||||
go putToSleepRemote(context.Background(), db, q, svc, apiPortFromContext(c))
|
||||
} else {
|
||||
go putToSleep(context.Background(), db, dc, engine, svc, apiPortFromContext(c))
|
||||
}
|
||||
c.JSON(http.StatusAccepted, gin.H{"status": "sleeping"})
|
||||
}
|
||||
|
||||
// handleServiceWakeNow wakes a sleeping service on demand (owner+ only).
|
||||
func handleServiceWakeNow(c *gin.Context) {
|
||||
if _, ok := loadOwnedService(c, c.MustGet("db").(*database.DB)); !ok {
|
||||
return
|
||||
}
|
||||
handleServiceWake(c)
|
||||
}
|
||||
|
||||
// apiPortFromContext pulls the configured HTTP port for the wake callback URL.
|
||||
func apiPortFromContext(c *gin.Context) int {
|
||||
if v, exists := c.Get("api_port"); exists {
|
||||
if p, ok := v.(int); ok {
|
||||
return p
|
||||
}
|
||||
}
|
||||
return 8080
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func TestMarkIdleTracksCounter(t *testing.T) {
|
||||
id := uuid.New()
|
||||
timeout := 50 * time.Millisecond
|
||||
|
||||
// First sighting sets the baseline only.
|
||||
if markIdle(id, 100, timeout) {
|
||||
t.Fatal("slept on first sighting")
|
||||
}
|
||||
// Counter growth resets the idle clock.
|
||||
if markIdle(id, 200, timeout) {
|
||||
t.Fatal("slept while counter grew")
|
||||
}
|
||||
// Flat counter within the window doesn't sleep.
|
||||
if markIdle(id, 200, timeout) {
|
||||
t.Fatal("slept before timeout")
|
||||
}
|
||||
time.Sleep(60 * time.Millisecond)
|
||||
if !markIdle(id, 200, timeout) {
|
||||
t.Fatal("did not sleep after timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMarkIdleZeroCounterSleeps(t *testing.T) {
|
||||
id := uuid.New()
|
||||
timeout := 20 * time.Millisecond
|
||||
|
||||
markIdle(id, 0, timeout)
|
||||
time.Sleep(30 * time.Millisecond)
|
||||
if !markIdle(id, 0, timeout) {
|
||||
t.Fatal("zero-traffic service never slept")
|
||||
}
|
||||
}
|
||||
@@ -42,8 +42,8 @@ type hostMonitoringResponse struct {
|
||||
Docker map[string]any `json:"docker,omitempty"`
|
||||
// Scope names what the telemetry actually measures: the physical host for
|
||||
// native dockerd, or the Docker Desktop VM when the daemon runs in one.
|
||||
Scope string `json:"scope"`
|
||||
CollectedAt string `json:"collected_at"`
|
||||
Scope string `json:"scope"`
|
||||
CollectedAt string `json:"collected_at"`
|
||||
}
|
||||
|
||||
type hostCPU struct {
|
||||
|
||||
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,167 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"gopkg.in/yaml.v3"
|
||||
)
|
||||
|
||||
func composeToSpecsForTest(t *testing.T, y string) ([]TemplateServiceSpec, []string, error) {
|
||||
t.Helper()
|
||||
var file composeFileDef
|
||||
if err := yaml.Unmarshal([]byte(y), &file); err != nil {
|
||||
t.Fatalf("yaml: %v", err)
|
||||
}
|
||||
return composeToSpecs(&file)
|
||||
}
|
||||
|
||||
func TestComposeToSpecsConvertsStack(t *testing.T) {
|
||||
specs, warnings, err := composeToSpecsForTest(t, `services:
|
||||
web:
|
||||
image: traefik/whoami:latest
|
||||
ports:
|
||||
- "80"
|
||||
environment:
|
||||
GREETING: hello-compose
|
||||
depends_on:
|
||||
- db
|
||||
db:
|
||||
image: postgres:16-alpine
|
||||
environment:
|
||||
POSTGRES_PASSWORD: pgsecret123
|
||||
POSTGRES_DB: appdb
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(specs) != 2 {
|
||||
t.Fatalf("want 2 specs, got %d", len(specs))
|
||||
}
|
||||
db, web := specs[0], specs[1]
|
||||
if db.Key != "db" || db.Type != "database" || db.Runtime != "postgresql" {
|
||||
t.Errorf("db spec wrong: %+v", db)
|
||||
}
|
||||
if db.Environment["POSTGRES_PASSWORD"] != "pgsecret123" {
|
||||
t.Errorf("db env lost: %v", db.Environment)
|
||||
}
|
||||
if web.Key != "web" || web.Type != "web" || web.Runtime != "traefik/whoami:latest" {
|
||||
t.Errorf("web spec wrong: %+v", web)
|
||||
}
|
||||
if web.Port != 80 {
|
||||
t.Errorf("web port = %d, want 80", web.Port)
|
||||
}
|
||||
if len(web.DependsOn) != 1 || web.DependsOn[0] != "db" {
|
||||
t.Errorf("web depends_on = %v, want [db]", web.DependsOn)
|
||||
}
|
||||
if len(warnings) != 0 {
|
||||
t.Errorf("unexpected warnings: %v", warnings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeToSpecsDeterministicOrder(t *testing.T) {
|
||||
y := `services:
|
||||
zebra:
|
||||
image: alpine
|
||||
alpha:
|
||||
image: alpine
|
||||
`
|
||||
for i := 0; i < 5; i++ {
|
||||
specs, _, err := composeToSpecsForTest(t, y)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if specs[0].Key != "alpha" || specs[1].Key != "zebra" {
|
||||
t.Fatalf("unstable order: %v", specs)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeToSpecsDatabaseImages(t *testing.T) {
|
||||
cases := map[string]string{
|
||||
"postgres:16": "postgresql",
|
||||
"redis:7": "redis",
|
||||
"mysql:8": "mysql",
|
||||
"mariadb:11": "mariadb",
|
||||
"mongo:7": "mongodb",
|
||||
"clickhouse:24": "clickhouse",
|
||||
}
|
||||
for image, want := range cases {
|
||||
specs, _, err := composeToSpecsForTest(t, "services:\n db:\n image: "+image+"\n")
|
||||
if err != nil {
|
||||
t.Fatalf("%s: %v", image, err)
|
||||
}
|
||||
if specs[0].Type != "database" || specs[0].Runtime != want {
|
||||
t.Errorf("%s → %+v, want database/%s", image, specs[0], want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeToSpecsNoServices(t *testing.T) {
|
||||
_, _, err := composeToSpecsForTest(t, "version: '3'\n")
|
||||
if err == nil || !strings.Contains(err.Error(), "no services") {
|
||||
t.Errorf("want no-services error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestComposeToSpecsWarnings(t *testing.T) {
|
||||
_, warnings, err := composeToSpecsForTest(t, `services:
|
||||
app:
|
||||
build: .
|
||||
orphan:
|
||||
image: alpine
|
||||
environment:
|
||||
- UNSET_VAR
|
||||
`)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
joined := strings.Join(warnings, "\n")
|
||||
if !strings.Contains(joined, "build contexts are not supported") {
|
||||
t.Errorf("missing build warning: %v", warnings)
|
||||
}
|
||||
if !strings.Contains(joined, "UNSET_VAR") {
|
||||
t.Errorf("missing env warning: %v", warnings)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveRepoFileContainment(t *testing.T) {
|
||||
dir := t.TempDir()
|
||||
inner := filepath.Join(dir, "sub")
|
||||
if err := os.MkdirAll(inner, 0o755); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if err := os.WriteFile(filepath.Join(inner, "compose.yml"), []byte("x"), 0o644); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
p, err := resolveRepoFile(dir, "sub/compose.yml")
|
||||
if err != nil {
|
||||
t.Fatalf("valid file rejected: %v", err)
|
||||
}
|
||||
if p != filepath.Join(dir, "sub", "compose.yml") {
|
||||
t.Errorf("unexpected path %q", p)
|
||||
}
|
||||
|
||||
for _, bad := range []string{"", "/etc/passwd", "../escape.yml", "..", "sub/../../out.yml"} {
|
||||
if _, err := resolveRepoFile(dir, bad); err == nil {
|
||||
t.Errorf("path %q should have been rejected", bad)
|
||||
}
|
||||
}
|
||||
if _, err := resolveRepoFile(dir, "sub"); err == nil {
|
||||
t.Error("directory accepted as file")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIsCommitSHA(t *testing.T) {
|
||||
if !isCommitSHA("0123456789abcdef0123456789abcdef01234567") {
|
||||
t.Error("40-hex ref not recognised as a commit")
|
||||
}
|
||||
for _, bad := range []string{"main", "v1.2.3", "0123456789abcdef0123456789abcdef0123456", "0123456789abcdef0123456789abcdef0123456G"} {
|
||||
if isCommitSHA(bad) {
|
||||
t.Errorf("ref %q misclassified as commit SHA", bad)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,202 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestTopoSortServicesOrdersByDependsOn(t *testing.T) {
|
||||
sorted, err := topoSortServices([]TemplateServiceSpec{
|
||||
{Key: "web", DependsOn: []string{"db"}},
|
||||
{Key: "db", Type: "database"},
|
||||
{Key: "worker", DependsOn: []string{"web", "db"}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("topoSortServices: %v", err)
|
||||
}
|
||||
if sorted[0].Key != "db" || sorted[1].Key != "web" || sorted[2].Key != "worker" {
|
||||
t.Fatalf("unexpected order: %v", []string{sorted[0].Key, sorted[1].Key, sorted[2].Key})
|
||||
}
|
||||
}
|
||||
|
||||
func TestTopoSortServicesRejectsCycle(t *testing.T) {
|
||||
_, err := topoSortServices([]TemplateServiceSpec{
|
||||
{Key: "a", DependsOn: []string{"b"}},
|
||||
{Key: "b", DependsOn: []string{"a"}},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "cycle") {
|
||||
t.Fatalf("expected cycle error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestTopoSortServicesRejectsUnknownDep(t *testing.T) {
|
||||
_, err := topoSortServices([]TemplateServiceSpec{
|
||||
{Key: "a", DependsOn: []string{"ghost"}},
|
||||
})
|
||||
if err == nil || !strings.Contains(err.Error(), "unknown key") {
|
||||
t.Fatalf("expected unknown-key error, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveStaticExpr(t *testing.T) {
|
||||
unresolved := []string{}
|
||||
var generated bool
|
||||
|
||||
v := resolveStaticExpr("{{secret}}", nil, &unresolved, &generated)
|
||||
if len(v) != 32 || !generated {
|
||||
t.Fatalf("secret: got %q generated=%v", v, generated)
|
||||
}
|
||||
v = resolveStaticExpr("{{secret(8)}}", nil, &unresolved, &generated)
|
||||
if len(v) != 16 {
|
||||
t.Fatalf("secret(8): got %q", v)
|
||||
}
|
||||
v = resolveStaticExpr("port-{{random:10}}", nil, &unresolved, &generated)
|
||||
if !strings.HasPrefix(v, "port-") {
|
||||
t.Fatalf("random: got %q", v)
|
||||
}
|
||||
v = resolveStaticExpr("{{DOMAIN:-example.com}}", nil, &unresolved, &generated)
|
||||
if v != "example.com" {
|
||||
t.Fatalf("var default: got %q", v)
|
||||
}
|
||||
v = resolveStaticExpr("{{DOMAIN}}", map[string]string{"DOMAIN": "x.io"}, &unresolved, &generated)
|
||||
if v != "x.io" {
|
||||
t.Fatalf("var override: got %q", v)
|
||||
}
|
||||
v = resolveStaticExpr("{{MISSING}}", nil, &unresolved, &generated)
|
||||
if v != "" || len(unresolved) == 0 || unresolved[0] != "MISSING" {
|
||||
t.Fatalf("missing var: got %q unresolved=%v", v, unresolved)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveTemplateGraphDatabaseRefs(t *testing.T) {
|
||||
sorted, err := topoSortServices([]TemplateServiceSpec{
|
||||
{Key: "db", Type: "database", Runtime: "postgresql", Environment: map[string]string{
|
||||
"POSTGRES_DB": "shop",
|
||||
}},
|
||||
{Key: "web", Type: "web", Runtime: "nginx", DependsOn: []string{"db"}, Environment: map[string]string{
|
||||
"DATABASE_URL": "{{service.db.url}}",
|
||||
"DB_HOST": "{{service.db.host}}",
|
||||
"DB_PORT": "{{service.db.port}}",
|
||||
"SESSION_TOKEN": "{{secret}}",
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("topo: %v", err)
|
||||
}
|
||||
graph := resolveTemplateGraph(sorted, nil)
|
||||
if len(graph.unresolved) > 0 {
|
||||
t.Fatalf("unresolved: %v", graph.unresolved)
|
||||
}
|
||||
web := graph.members[1]
|
||||
if web.env["DB_HOST"] != "db" {
|
||||
t.Fatalf("DB_HOST=%q", web.env["DB_HOST"])
|
||||
}
|
||||
if web.env["DB_PORT"] != "5432" {
|
||||
t.Fatalf("DB_PORT=%q", web.env["DB_PORT"])
|
||||
}
|
||||
dbURL := web.env["DATABASE_URL"]
|
||||
if !strings.HasPrefix(dbURL, "postgresql://") || !strings.Contains(dbURL, "@db:5432/shop") {
|
||||
t.Fatalf("DATABASE_URL=%q", dbURL)
|
||||
}
|
||||
if len(web.env["SESSION_TOKEN"]) != 32 || !web.secretKeys["SESSION_TOKEN"] {
|
||||
t.Fatalf("SESSION_TOKEN=%q secret=%v", web.env["SESSION_TOKEN"], web.secretKeys["SESSION_TOKEN"])
|
||||
}
|
||||
// Database member must carry the generated password so refs and the
|
||||
// container agree.
|
||||
db := graph.members[0]
|
||||
if pw := db.env["POSTGRES_PASSWORD"]; len(pw) != 32 || !db.secretKeys["POSTGRES_PASSWORD"] {
|
||||
t.Fatalf("POSTGRES_PASSWORD=%q secret=%v", pw, db.secretKeys["POSTGRES_PASSWORD"])
|
||||
}
|
||||
if !strings.Contains(dbURL, db.env["POSTGRES_PASSWORD"]) {
|
||||
t.Fatalf("url %q does not carry the resolved password", dbURL)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveTemplateGraphServiceRefs(t *testing.T) {
|
||||
sorted, err := topoSortServices([]TemplateServiceSpec{
|
||||
{Key: "api", Type: "web", Runtime: "ghcr.io/x/api:1", Port: 8080},
|
||||
{Key: "front", Type: "web", Runtime: "ghcr.io/x/front:1", Port: 3000, DependsOn: []string{"api"}, Environment: map[string]string{
|
||||
"API_URL": "{{service.api.url}}/v2",
|
||||
}},
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("topo: %v", err)
|
||||
}
|
||||
graph := resolveTemplateGraph(sorted, nil)
|
||||
if got := graph.members[1].env["API_URL"]; got != "http://api:8080/v2" {
|
||||
t.Fatalf("API_URL=%q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestResolveTemplateGraphUnresolvedServiceProp(t *testing.T) {
|
||||
sorted, _ := topoSortServices([]TemplateServiceSpec{
|
||||
{Key: "web", Type: "web", Runtime: "nginx", Environment: map[string]string{
|
||||
"X": "{{service.nope.host}}",
|
||||
}},
|
||||
})
|
||||
graph := resolveTemplateGraph(sorted, nil)
|
||||
if len(graph.unresolved) == 0 {
|
||||
t.Fatal("expected unresolved ref")
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateTemplateGraph(t *testing.T) {
|
||||
if err := validateTemplateGraph([]TemplateServiceSpec{
|
||||
{Key: "db", Type: "database", Runtime: "postgresql"},
|
||||
}); err != nil {
|
||||
t.Fatalf("valid db spec rejected: %v", err)
|
||||
}
|
||||
if err := validateTemplateGraph([]TemplateServiceSpec{
|
||||
{Key: "db", Type: "database", Runtime: "nonsense"},
|
||||
}); err == nil {
|
||||
t.Fatal("expected unsupported db engine error")
|
||||
}
|
||||
if err := validateTemplateGraph([]TemplateServiceSpec{
|
||||
{Key: "web", Type: "web"},
|
||||
}); err == nil {
|
||||
t.Fatal("expected missing runtime error")
|
||||
}
|
||||
}
|
||||
|
||||
// TestOfficialTemplatesAllParse walks every seeded template and asserts the
|
||||
// config parses, graph templates topo-sort + validate, and a dry-run resolve
|
||||
// produces no unresolved refs (defaults cover all expressions).
|
||||
func TestOfficialTemplatesAllParse(t *testing.T) {
|
||||
for _, tpl := range SeedTemplates() {
|
||||
var cfg TemplateConfig
|
||||
if err := json.Unmarshal([]byte(tpl.Config), &cfg); err != nil {
|
||||
t.Errorf("%s: config parse: %v", tpl.ID, err)
|
||||
continue
|
||||
}
|
||||
if len(cfg.Services) == 0 {
|
||||
continue
|
||||
}
|
||||
sorted, err := topoSortServices(cfg.Services)
|
||||
if err != nil {
|
||||
t.Errorf("%s: topo sort: %v", tpl.ID, err)
|
||||
continue
|
||||
}
|
||||
if err := validateTemplateGraph(sorted); err != nil {
|
||||
t.Errorf("%s: validate: %v", tpl.ID, err)
|
||||
continue
|
||||
}
|
||||
var vars []TemplateVariable
|
||||
if tpl.Variables != "" {
|
||||
if err := json.Unmarshal([]byte(tpl.Variables), &vars); err != nil {
|
||||
t.Errorf("%s: variables parse: %v", tpl.ID, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
userVars := map[string]string{}
|
||||
for _, v := range vars {
|
||||
if v.Default != "" {
|
||||
userVars[v.Key] = v.Default
|
||||
}
|
||||
}
|
||||
graph := resolveTemplateGraph(sorted, userVars)
|
||||
if len(graph.unresolved) > 0 {
|
||||
t.Errorf("%s: unresolved refs: %v", tpl.ID, graph.unresolved)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,186 @@
|
||||
package api
|
||||
|
||||
// Template registry — CONTAINR_TEMPLATE_REGISTRY_URL points at another
|
||||
// Containr instance (or any server answering the same template shape):
|
||||
// GET {base}/api/v1/templates → {"templates": [ServiceTemplate…]}
|
||||
// GET {base}/api/v1/templates/:id → {"template": …, "config": …, "variables": …}
|
||||
// Registry entries merge into the catalog as source:"registry"; deploying
|
||||
// one materializes it as a user template for the deployer first. The
|
||||
// bundled catalog always works offline — a dead registry never blocks it.
|
||||
|
||||
import (
|
||||
"containr/internal/database/sqlcdb"
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sqlc-dev/pqtype"
|
||||
)
|
||||
|
||||
const registryCacheTTL = 60 * time.Second
|
||||
|
||||
var registryCache struct {
|
||||
mu sync.Mutex
|
||||
fetchedAt time.Time
|
||||
templates []ServiceTemplate
|
||||
}
|
||||
|
||||
func registryBaseURL() string {
|
||||
base := strings.TrimRight(strings.TrimSpace(os.Getenv("CONTAINR_TEMPLATE_REGISTRY_URL")), "/")
|
||||
base = strings.TrimSuffix(base, "/api/v1")
|
||||
return base
|
||||
}
|
||||
|
||||
// fetchRegistryTemplates lists the remote catalog, cached for
|
||||
// registryCacheTTL. Failures degrade to an empty list — never fatal.
|
||||
func fetchRegistryTemplates(ctx context.Context) []ServiceTemplate {
|
||||
if registryBaseURL() == "" {
|
||||
return nil
|
||||
}
|
||||
registryCache.mu.Lock()
|
||||
fresh := time.Since(registryCache.fetchedAt) < registryCacheTTL && registryCache.templates != nil
|
||||
cached := registryCache.templates
|
||||
registryCache.mu.Unlock()
|
||||
if fresh {
|
||||
return cached
|
||||
}
|
||||
|
||||
var templates []ServiceTemplate
|
||||
var payload struct {
|
||||
Templates []ServiceTemplate `json:"templates"`
|
||||
}
|
||||
if err := registryGet(ctx, "/api/v1/templates", &payload); err == nil {
|
||||
templates = payload.Templates
|
||||
}
|
||||
for i := range templates {
|
||||
templates[i].Source = "registry"
|
||||
}
|
||||
|
||||
registryCache.mu.Lock()
|
||||
registryCache.fetchedAt = time.Now()
|
||||
registryCache.templates = templates
|
||||
registryCache.mu.Unlock()
|
||||
return templates
|
||||
}
|
||||
|
||||
// fetchRegistryTemplate fetches one remote template's full document
|
||||
// (template + config + variables). nil when the registry lacks it.
|
||||
func fetchRegistryTemplate(ctx context.Context, id string) *ServiceTemplate {
|
||||
base := registryBaseURL()
|
||||
if base == "" {
|
||||
return nil
|
||||
}
|
||||
var payload struct {
|
||||
Template ServiceTemplate `json:"template"`
|
||||
Config json.RawMessage `json:"config"`
|
||||
Variables []TemplateVariable `json:"variables"`
|
||||
}
|
||||
if err := registryGet(ctx, "/api/v1/templates/"+id, &payload); err != nil {
|
||||
return nil
|
||||
}
|
||||
t := payload.Template
|
||||
if t.ID == "" {
|
||||
t.ID = id
|
||||
}
|
||||
t.Source = "registry"
|
||||
// The detail endpoint returns config/variables decoded; store them back
|
||||
// as raw JSON so they can persist unmodified into a local copy.
|
||||
if len(payload.Config) > 0 {
|
||||
t.Config = string(payload.Config)
|
||||
}
|
||||
if payload.Variables != nil {
|
||||
if raw, err := json.Marshal(payload.Variables); err == nil {
|
||||
t.Variables = string(raw)
|
||||
}
|
||||
}
|
||||
return &t
|
||||
}
|
||||
|
||||
func registryGet(ctx context.Context, path string, out interface{}) error {
|
||||
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
||||
defer cancel()
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, registryBaseURL()+path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := http.DefaultClient.Do(req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return errRegistry{resp.StatusCode}
|
||||
}
|
||||
return json.NewDecoder(resp.Body).Decode(out)
|
||||
}
|
||||
|
||||
type errRegistry struct{ status int }
|
||||
|
||||
func (e errRegistry) Error() string { return "template registry status " + http.StatusText(e.status) }
|
||||
|
||||
// respondRegistryTemplate answers GET /templates/:id for a registry-only
|
||||
// template with the same envelope shape as local templates.
|
||||
func respondRegistryTemplate(c *gin.Context, t *ServiceTemplate) {
|
||||
config := json.RawMessage(t.Config)
|
||||
if len(config) == 0 {
|
||||
config = json.RawMessage(`{}`)
|
||||
}
|
||||
variables := json.RawMessage(t.Variables)
|
||||
if len(variables) == 0 {
|
||||
variables = json.RawMessage(`[]`)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"template": t,
|
||||
"config": config,
|
||||
"variables": variables,
|
||||
})
|
||||
}
|
||||
|
||||
// materializeRegistryTemplate copies a registry template into the local
|
||||
// catalog as a private template owned by the deployer, so the normal
|
||||
// deploy path (and future re-deploys) work without the registry. The
|
||||
// local id is deterministic per (registry, template, user) so repeat
|
||||
// deploys reuse the same copy instead of accumulating duplicates.
|
||||
func materializeRegistryTemplate(c *gin.Context, q *sqlcdb.Queries, userID, templateID string) (sqlcdb.ServiceTemplate, error) {
|
||||
ownerUUID, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
return sqlcdb.ServiceTemplate{}, err
|
||||
}
|
||||
sum := sha256.Sum256([]byte(registryBaseURL() + "|" + templateID + "|" + userID))
|
||||
localID := "reg-" + hex.EncodeToString(sum[:8])
|
||||
if existing, err := q.GetServiceTemplateByID(c.Request.Context(), localID); err == nil {
|
||||
return existing, nil
|
||||
}
|
||||
|
||||
rt := fetchRegistryTemplate(c.Request.Context(), templateID)
|
||||
if rt == nil {
|
||||
return sqlcdb.ServiceTemplate{}, sql.ErrNoRows
|
||||
}
|
||||
variables := pqtype.NullRawMessage{}
|
||||
if rt.Variables != "" {
|
||||
variables = pqtype.NullRawMessage{RawMessage: json.RawMessage(rt.Variables), Valid: true}
|
||||
}
|
||||
if err := q.CreateUserTemplate(c.Request.Context(), sqlcdb.CreateUserTemplateParams{
|
||||
ID: localID,
|
||||
Name: rt.Name,
|
||||
Description: nullableText(rt.Description),
|
||||
Category: rt.Category,
|
||||
Logo: nullableText(rt.Logo),
|
||||
Config: json.RawMessage(rt.Config),
|
||||
Variables: variables,
|
||||
OwnerID: uuid.NullUUID{UUID: ownerUUID, Valid: true},
|
||||
IsPublic: false,
|
||||
}); err != nil {
|
||||
return sqlcdb.ServiceTemplate{}, err
|
||||
}
|
||||
return q.GetServiceTemplateByID(c.Request.Context(), localID)
|
||||
}
|
||||
@@ -3,6 +3,8 @@ package api
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/deployment"
|
||||
"containr/internal/secrets"
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
@@ -19,21 +21,24 @@ import (
|
||||
)
|
||||
|
||||
type ServiceTemplate struct {
|
||||
ID string `json:"id" db:"id"`
|
||||
Name string `json:"name" db:"name"`
|
||||
Description string `json:"description" db:"description"`
|
||||
Category string `json:"category" db:"category"`
|
||||
Logo string `json:"logo" db:"logo"`
|
||||
Config string `json:"config" db:"config"`
|
||||
Variables string `json:"variables" db:"variables"`
|
||||
IsOfficial bool `json:"is_official" db:"is_official"`
|
||||
OwnerID string `json:"owner_id,omitempty" db:"owner_id"`
|
||||
IsPublic bool `json:"is_public" db:"is_public"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
ID string `json:"id" db:"id"`
|
||||
Name string `json:"name" db:"name"`
|
||||
Description string `json:"description" db:"description"`
|
||||
Category string `json:"category" db:"category"`
|
||||
Logo string `json:"logo" db:"logo"`
|
||||
Config string `json:"config" db:"config"`
|
||||
Variables string `json:"variables" db:"variables"`
|
||||
IsOfficial bool `json:"is_official" db:"is_official"`
|
||||
OwnerID string `json:"owner_id,omitempty" db:"owner_id"`
|
||||
IsPublic bool `json:"is_public" db:"is_public"`
|
||||
// Source marks catalog origin: "" (local/official) or "registry".
|
||||
Source string `json:"source,omitempty" db:"-"`
|
||||
CreatedAt time.Time `json:"created_at" db:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at" db:"updated_at"`
|
||||
}
|
||||
|
||||
type TemplateConfig struct {
|
||||
Version int `json:"version,omitempty"`
|
||||
Type string `json:"type"`
|
||||
Runtime string `json:"runtime"`
|
||||
BuildCommand string `json:"build_command"`
|
||||
@@ -43,6 +48,9 @@ type TemplateConfig struct {
|
||||
Environment map[string]string `json:"environment"`
|
||||
Dockerfile string `json:"dockerfile,omitempty"`
|
||||
NixpacksConfig map[string]string `json:"nixpacks_config,omitempty"`
|
||||
// Services makes this a v2 graph template. When non-empty the flat
|
||||
// fields above are ignored and deployTemplateGraph handles the deploy.
|
||||
Services []TemplateServiceSpec `json:"services,omitempty"`
|
||||
}
|
||||
|
||||
type TemplateVariable struct {
|
||||
@@ -91,10 +99,24 @@ func handleGetTemplates(c *gin.Context) {
|
||||
}
|
||||
|
||||
templates := make([]ServiceTemplate, 0, len(templateRows))
|
||||
localIDs := make(map[string]bool, len(templateRows))
|
||||
for _, row := range templateRows {
|
||||
localIDs[row.ID] = true
|
||||
templates = append(templates, mapSQLCTemplate(row))
|
||||
}
|
||||
|
||||
// Merge the configured remote registry (CONTAINR_TEMPLATE_REGISTRY_URL).
|
||||
// Registry ids never shadow local templates.
|
||||
for _, t := range fetchRegistryTemplates(ctx) {
|
||||
if localIDs[t.ID] {
|
||||
continue
|
||||
}
|
||||
if category != "" && t.Category != category {
|
||||
continue
|
||||
}
|
||||
templates = append(templates, t)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"templates": templates})
|
||||
}
|
||||
|
||||
@@ -106,6 +128,11 @@ func handleGetTemplate(c *gin.Context) {
|
||||
row, err := queries.GetServiceTemplateByID(c.Request.Context(), templateID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
// Registry fallback — remote templates materialize read-only.
|
||||
if rt := fetchRegistryTemplate(c.Request.Context(), templateID); rt != nil {
|
||||
respondRegistryTemplate(c, rt)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
|
||||
return
|
||||
}
|
||||
@@ -147,7 +174,7 @@ func handleCreateFromTemplate(c *gin.Context) {
|
||||
|
||||
var req struct {
|
||||
ProjectID string `json:"project_id" binding:"required"`
|
||||
Name string `json:"name" binding:"required"`
|
||||
Name string `json:"name"`
|
||||
Plan string `json:"plan,omitempty"`
|
||||
Region string `json:"region,omitempty"`
|
||||
Variables map[string]string `json:"variables"`
|
||||
@@ -160,10 +187,6 @@ func handleCreateFromTemplate(c *gin.Context) {
|
||||
|
||||
req.ProjectID = strings.TrimSpace(req.ProjectID)
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
if req.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"})
|
||||
return
|
||||
}
|
||||
|
||||
projectID, err := uuid.Parse(req.ProjectID)
|
||||
if err != nil {
|
||||
@@ -188,11 +211,18 @@ func handleCreateFromTemplate(c *gin.Context) {
|
||||
templateRow, err := queries.GetServiceTemplateByID(ctx, templateID)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
|
||||
// Registry template — materialize a local copy for the
|
||||
// deployer, then continue down the normal deploy path.
|
||||
row, mErr := materializeRegistryTemplate(c, queries, userID, templateID)
|
||||
if mErr != nil {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Template not found"})
|
||||
return
|
||||
}
|
||||
templateRow = row
|
||||
} else {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch template"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch template"})
|
||||
return
|
||||
}
|
||||
|
||||
template := mapSQLCTemplate(templateRow)
|
||||
@@ -213,6 +243,51 @@ func handleCreateFromTemplate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if len(config.Services) > 0 {
|
||||
var engine *deployment.DeploymentEngine
|
||||
if v, exists := c.Get("deployment_engine"); exists && v != nil {
|
||||
engine, _ = v.(*deployment.DeploymentEngine)
|
||||
}
|
||||
var dbHandler *DatabaseHandler
|
||||
if v, exists := c.Get("database_handler"); exists {
|
||||
dbHandler, _ = v.(*DatabaseHandler)
|
||||
}
|
||||
|
||||
userVars := map[string]string{}
|
||||
for _, v := range templateVars {
|
||||
if override, ok := req.Variables[v.Key]; ok {
|
||||
userVars[v.Key] = override
|
||||
} else if v.Default != "" {
|
||||
userVars[v.Key] = v.Default
|
||||
}
|
||||
}
|
||||
for k, v := range req.Variables {
|
||||
userVars[k] = v
|
||||
}
|
||||
|
||||
created, fail := deployTemplateGraph(c, db, engine, dbHandler, projectID, userID,
|
||||
config.Services, templateVars, userVars, req.Plan, req.Region, "template:"+templateID)
|
||||
if fail != nil {
|
||||
status := http.StatusBadRequest
|
||||
if (*fail)["code"] == "CONFLICT" {
|
||||
status = http.StatusConflict
|
||||
}
|
||||
c.JSON(status, *fail)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusCreated, gin.H{
|
||||
"resource": "stack",
|
||||
"created": created,
|
||||
"message": fmt.Sprintf("Deployed %d resources from template %s", len(created), template.Name),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
if req.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required"})
|
||||
return
|
||||
}
|
||||
|
||||
serviceType, err := normalizeTemplateServiceType(config.Type)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -336,12 +411,11 @@ func handleCreateFromTemplate(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// sqlc's schema predates the runtime columns; port and healthcheck_path are
|
||||
// written via raw SQL like every other service mutation path.
|
||||
if _, err = tx.ExecContext(ctx,
|
||||
`UPDATE services SET port = $1, healthcheck_path = $2 WHERE id = $3`,
|
||||
config.Port, strings.TrimSpace(config.HealthCheck), serviceID,
|
||||
); err != nil {
|
||||
if err = sqlcdb.New(tx).UpdateServiceRuntimeMeta(ctx, sqlcdb.UpdateServiceRuntimeMetaParams{
|
||||
Port: int32(config.Port),
|
||||
HealthcheckPath: strings.TrimSpace(config.HealthCheck),
|
||||
ID: serviceID,
|
||||
}); err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create service from template"})
|
||||
return
|
||||
}
|
||||
@@ -351,11 +425,15 @@ func handleCreateFromTemplate(c *gin.Context) {
|
||||
continue
|
||||
}
|
||||
|
||||
stored := value
|
||||
if secretKeys[key] {
|
||||
stored = secrets.Encrypt(value)
|
||||
}
|
||||
err = txQueries.UpsertEnvironmentVariable(ctx, sqlcdb.UpsertEnvironmentVariableParams{
|
||||
ID: uuid.New(),
|
||||
ServiceID: serviceID,
|
||||
Key: key,
|
||||
Value: value,
|
||||
Value: stored,
|
||||
IsSecret: sql.NullBool{Bool: secretKeys[key], Valid: true},
|
||||
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
@@ -927,6 +1005,194 @@ func SeedTemplates() []ServiceTemplate {
|
||||
Variables: `[]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
// Multi-service stacks — v2 graph configs. Database members become
|
||||
// managed databases attached to the project network under their key.
|
||||
{
|
||||
ID: "tpl-wordpress-stack",
|
||||
Name: "WordPress + MySQL",
|
||||
Description: "WordPress with a managed MySQL database on a private project network",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/wordpress",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"mysql","environment":{"MYSQL_DATABASE":"wordpress"}},
|
||||
{"key":"web","name":"wordpress","type":"web","runtime":"wordpress:6-apache","port":80,"health_check":"/wp-login.php","depends_on":["db"],
|
||||
"environment":{"WORDPRESS_DB_HOST":"{{service.db.host}}","WORDPRESS_DB_USER":"{{service.db.user}}","WORDPRESS_DB_PASSWORD":"{{service.db.password}}","WORDPRESS_DB_NAME":"{{service.db.database}}"}}]}`,
|
||||
Variables: `[]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-n8n-stack",
|
||||
Name: "n8n + PostgreSQL",
|
||||
Description: "n8n workflow automation backed by a managed PostgreSQL database",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/n8n",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql"},
|
||||
{"key":"n8n","type":"web","runtime":"n8nio/n8n:latest","port":5678,"health_check":"/healthz","depends_on":["db"],
|
||||
"environment":{"DB_TYPE":"postgresdb","DB_POSTGRESDB_HOST":"{{service.db.host}}","DB_POSTGRESDB_PORT":"{{service.db.port}}","DB_POSTGRESDB_DATABASE":"{{service.db.database}}","DB_POSTGRESDB_USER":"{{service.db.user}}","DB_POSTGRESDB_PASSWORD":"{{service.db.password}}","N8N_ENCRYPTION_KEY":"{{secret}}"}}]}`,
|
||||
Variables: `[]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-ghost-stack",
|
||||
Name: "Ghost + MySQL",
|
||||
Description: "Ghost publishing platform backed by a managed MySQL database",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/ghost",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"mysql","environment":{"MYSQL_DATABASE":"ghost"}},
|
||||
{"key":"web","name":"ghost","type":"web","runtime":"ghost:5-alpine","port":2368,"depends_on":["db"],
|
||||
"environment":{"url":"{{APP_URL:-http://localhost:2368}}","database__client":"mysql","database__connection__host":"{{service.db.host}}","database__connection__user":"{{service.db.user}}","database__connection__password":"{{service.db.password}}","database__connection__database":"{{service.db.database}}"}}]}`,
|
||||
Variables: `[{"key":"APP_URL","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-gitea-stack",
|
||||
Name: "Gitea + PostgreSQL",
|
||||
Description: "Self-hosted Git service backed by a managed PostgreSQL database",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/gitea",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"gitea"}},
|
||||
{"key":"web","name":"gitea","type":"web","runtime":"gitea/gitea:1","port":3000,"health_check":"/api/healthz","depends_on":["db"],
|
||||
"environment":{"GITEA__database__DB_TYPE":"postgres","GITEA__database__HOST":"{{service.db.host}}:{{service.db.port}}","GITEA__database__NAME":"{{service.db.database}}","GITEA__database__USER":"{{service.db.user}}","GITEA__database__PASSWD":"{{service.db.password}}","GITEA__security__SECRET_KEY":"{{secret}}","GITEA__security__INTERNAL_TOKEN":"{{secret(64)}}"}}]}`,
|
||||
Variables: `[]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-nextcloud-stack",
|
||||
Name: "Nextcloud + PostgreSQL + Redis",
|
||||
Description: "Nextcloud with a managed PostgreSQL database and Redis cache",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/nextcloud",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"nextcloud"}},
|
||||
{"key":"cache","type":"database","runtime":"redis"},
|
||||
{"key":"web","name":"nextcloud","type":"web","runtime":"nextcloud:30-apache","port":80,"health_check":"/status.php","depends_on":["db","cache"],
|
||||
"environment":{"POSTGRES_HOST":"{{service.db.host}}","POSTGRES_DB":"{{service.db.database}}","POSTGRES_USER":"{{service.db.user}}","POSTGRES_PASSWORD":"{{service.db.password}}","REDIS_HOST":"{{service.cache.host}}","REDIS_HOST_PASSWORD":"{{service.cache.password}}","NEXTCLOUD_ADMIN_USER":"admin","NEXTCLOUD_ADMIN_PASSWORD":"{{secret}}","NEXTCLOUD_TRUSTED_DOMAINS":"{{APP_DOMAIN:-localhost}}"}}]}`,
|
||||
Variables: `[{"key":"APP_DOMAIN","label":"Trusted domain","default":"","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-paperless-stack",
|
||||
Name: "Paperless-ngx + PostgreSQL + Redis",
|
||||
Description: "Document management with managed PostgreSQL and Redis",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/paperlessngx",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"paperless"}},
|
||||
{"key":"cache","type":"database","runtime":"redis"},
|
||||
{"key":"web","name":"paperless","type":"web","runtime":"ghcr.io/paperless-ngx/paperless-ngx:latest","port":8000,"depends_on":["db","cache"],
|
||||
"environment":{"PAPERLESS_DBHOST":"{{service.db.host}}","PAPERLESS_DBPORT":"{{service.db.port}}","PAPERLESS_DBNAME":"{{service.db.database}}","PAPERLESS_DBUSER":"{{service.db.user}}","PAPERLESS_DBPASS":"{{service.db.password}}","PAPERLESS_REDIS":"{{service.cache.url}}","PAPERLESS_ADMIN_USER":"admin","PAPERLESS_ADMIN_PASSWORD":"{{secret}}","PAPERLESS_SECRET_KEY":"{{secret(48)}}","PAPERLESS_URL":"{{APP_URL:-http://localhost:8000}}"}}]}`,
|
||||
Variables: `[{"key":"APP_URL","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-plausible-stack",
|
||||
Name: "Plausible + PostgreSQL + ClickHouse",
|
||||
Description: "Privacy-friendly analytics with managed PostgreSQL and ClickHouse",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/plausibleanalytics",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"plausible"}},
|
||||
{"key":"events","type":"database","runtime":"clickhouse","environment":{"CLICKHOUSE_DB":"plausible"}},
|
||||
{"key":"web","name":"plausible","type":"web","runtime":"ghcr.io/plausible/community-edition:v2","port":8000,"health_check":"/api/health","depends_on":["db","events"],
|
||||
"environment":{"BASE_URL":"{{APP_URL:-http://localhost:8000}}","SECRET_KEY_BASE":"{{secret(64)}}","DATABASE_URL":"{{service.db.url}}","CLICKHOUSE_DATABASE_URL":"http://{{service.events.host}}:{{service.events.port}}/{{service.events.database}}","TOTP_VAULT_KEY":"{{secret(32)}}"}}]}`,
|
||||
Variables: `[{"key":"APP_URL","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-n8n",
|
||||
Name: "n8n + PostgreSQL + Redis",
|
||||
Description: "Workflow automation in queue mode with managed PostgreSQL and Redis",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/n8n",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"n8n","POSTGRES_USER":"n8n"},"volumes":[{"source":"n8n-db","target":"/var/lib/postgresql/data"}]},
|
||||
{"key":"redis","type":"database","runtime":"redis"},
|
||||
{"key":"n8n","type":"web","runtime":"docker.n8n.io/n8nio/n8n:latest","port":5678,"health_check":"/healthz","depends_on":["db","redis"],
|
||||
"environment":{"DB_TYPE":"postgresdb","DB_POSTGRESDB_HOST":"{{service.db.host}}","DB_POSTGRESDB_PORT":"5432","DB_POSTGRESDB_DATABASE":"{{service.db.database}}","DB_POSTGRESDB_USER":"{{service.db.user}}","DB_POSTGRESDB_PASSWORD":"{{service.db.password}}","QUEUE_BULL_REDIS_HOST":"{{service.redis.host}}","QUEUE_BULL_REDIS_PASSWORD":"{{service.redis.password}}","EXECUTIONS_MODE":"queue","N8N_ENCRYPTION_KEY":"{{secret(32)}}","N8N_HOST":"{{APP_URL:-http://localhost:5678}}","N8N_EDITOR_BASE_URL":"{{APP_URL:-http://localhost:5678}}"},
|
||||
"volumes":[{"source":"n8n-data","target":"/home/node/.n8n"}]},
|
||||
{"key":"worker","name":"n8n-worker","type":"worker","runtime":"docker.n8n.io/n8nio/n8n:latest","start_command":"worker","depends_on":["n8n","db","redis"],
|
||||
"environment":{"DB_TYPE":"postgresdb","DB_POSTGRESDB_HOST":"{{service.db.host}}","DB_POSTGRESDB_PORT":"5432","DB_POSTGRESDB_DATABASE":"{{service.db.database}}","DB_POSTGRESDB_USER":"{{service.db.user}}","DB_POSTGRESDB_PASSWORD":"{{service.db.password}}","QUEUE_BULL_REDIS_HOST":"{{service.redis.host}}","QUEUE_BULL_REDIS_PASSWORD":"{{service.redis.password}}","EXECUTIONS_MODE":"queue","N8N_ENCRYPTION_KEY":"{{secret(32)}}","QUEUE_HEALTH_CHECK_ACTIVE":"true"},
|
||||
"volumes":[{"source":"n8n-data","target":"/home/node/.n8n"}]}]}`,
|
||||
Variables: `[{"key":"APP_URL","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-gitea",
|
||||
Name: "Gitea + PostgreSQL",
|
||||
Description: "Self-hosted git service with managed PostgreSQL",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/gitea",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"gitea","POSTGRES_USER":"gitea"},"volumes":[{"source":"gitea-db","target":"/var/lib/postgresql/data"}]},
|
||||
{"key":"gitea","type":"web","runtime":"gitea/gitea:latest","port":3000,"health_check":"/api/healthz","depends_on":["db"],
|
||||
"environment":{"GITEA__database__DB_TYPE":"postgres","GITEA__database__HOST":"{{service.db.host}}:{{service.db.port}}","GITEA__database__NAME":"{{service.db.database}}","GITEA__database__USER":"{{service.db.user}}","GITEA__database__PASSWD":"{{service.db.password}}","GITEA__security__INSTALL_LOCK":"true","GITEA__security__SECRET_KEY":"{{secret(32)}}","GITEA__security__INTERNAL_TOKEN":"{{secret(64)}}","GITEA__server__DOMAIN":"{{DOMAIN:-localhost}}","GITEA__server__ROOT_URL":"{{ROOT_URL:-http://localhost:3000}}","GITEA__server__SSH_PORT":"2222","USER_UID":"1000","USER_GID":"1000"},
|
||||
"volumes":[{"source":"gitea-data","target":"/data"}]}]}`,
|
||||
Variables: `[{"key":"DOMAIN","label":"Public domain","default":"","required":false,"secret":false},{"key":"ROOT_URL","label":"Root URL","default":"","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-nextcloud",
|
||||
Name: "Nextcloud + PostgreSQL + Redis",
|
||||
Description: "Self-hosted file sync and collaboration with managed PostgreSQL and Redis",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/nextcloud",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"nextcloud","POSTGRES_USER":"nextcloud"},"volumes":[{"source":"nextcloud-db","target":"/var/lib/postgresql/data"}]},
|
||||
{"key":"redis","type":"database","runtime":"redis"},
|
||||
{"key":"nextcloud","type":"web","runtime":"nextcloud:latest","port":80,"depends_on":["db","redis"],
|
||||
"environment":{"POSTGRES_HOST":"{{service.db.host}}","POSTGRES_DB":"{{service.db.database}}","POSTGRES_USER":"{{service.db.user}}","POSTGRES_PASSWORD":"{{service.db.password}}","REDIS_HOST":"{{service.redis.host}}","REDIS_HOST_PASSWORD":"{{service.redis.password}}","NEXTCLOUD_ADMIN_USER":"admin","NEXTCLOUD_ADMIN_PASSWORD":"{{secret(24)}}","NEXTCLOUD_TRUSTED_DOMAINS":"{{DOMAIN:-localhost}}","OVERWRITEHOST":"{{DOMAIN:-}}","OVERWRITEPROTOCOL":"https"},
|
||||
"volumes":[{"source":"nextcloud-data","target":"/var/www/html"}]}]}`,
|
||||
Variables: `[{"key":"DOMAIN","label":"Trusted domain","default":"localhost","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-vaultwarden",
|
||||
Name: "Vaultwarden + PostgreSQL",
|
||||
Description: "Bitwarden-compatible password manager with managed PostgreSQL",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/vaultwarden",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"vaultwarden","POSTGRES_USER":"vaultwarden"},"volumes":[{"source":"vaultwarden-db","target":"/var/lib/postgresql/data"}]},
|
||||
{"key":"vaultwarden","type":"web","runtime":"vaultwarden/server:latest","port":80,"health_check":"/alive","depends_on":["db"],
|
||||
"environment":{"DATABASE_URL":"{{service.db.url}}","ADMIN_TOKEN":"{{secret(48)}}","DOMAIN":"{{DOMAIN:-http://localhost:80}}","SIGNUPS_ALLOWED":"false","WEBSOCKET_ENABLED":"true"},
|
||||
"volumes":[{"source":"vaultwarden-data","target":"/data"}]}]}`,
|
||||
Variables: `[{"key":"DOMAIN","label":"Public URL","default":"","required":false,"secret":false}]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-umami",
|
||||
Name: "Umami + PostgreSQL",
|
||||
Description: "Lightweight privacy-first web analytics with managed PostgreSQL",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/umami",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"umami","POSTGRES_USER":"umami"},"volumes":[{"source":"umami-db","target":"/var/lib/postgresql/data"}]},
|
||||
{"key":"umami","type":"web","runtime":"ghcr.io/umami-software/umami:postgresql-latest","port":3000,"depends_on":["db"],
|
||||
"environment":{"DATABASE_URL":"{{service.db.url}}","APP_SECRET":"{{secret(32)}}"}}]}`,
|
||||
Variables: `[]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
{
|
||||
ID: "tpl-immich",
|
||||
Name: "Immich + PostgreSQL + Redis",
|
||||
Description: "Self-hosted photo and video backup with managed PostgreSQL and Redis",
|
||||
Category: "stack",
|
||||
Logo: "https://cdn.simpleicons.org/immich",
|
||||
Config: `{"version":2,"services":[
|
||||
{"key":"db","type":"database","runtime":"postgresql","environment":{"POSTGRES_DB":"immich","POSTGRES_USER":"immich"},"volumes":[{"source":"immich-db","target":"/var/lib/postgresql/data"}]},
|
||||
{"key":"redis","type":"database","runtime":"redis"},
|
||||
{"key":"ml","name":"immich-machine-learning","type":"worker","runtime":"ghcr.io/immich-app/immich-machine-learning:release",
|
||||
"environment":{"DB_DATABASE_NAME":"{{service.db.database}}","DB_HOSTNAME":"{{service.db.host}}","DB_USERNAME":"{{service.db.user}}","DB_PASSWORD":"{{service.db.password}}","REDIS_HOSTNAME":"{{service.redis.host}}","REDIS_PASSWORD":"{{service.redis.password}}"},
|
||||
"volumes":[{"source":"immich-model-cache","target":"/cache"}]},
|
||||
{"key":"immich","name":"immich-server","type":"web","runtime":"ghcr.io/immich-app/immich-server:release","port":2283,"health_check":"/api/server/ping","depends_on":["db","redis","ml"],
|
||||
"environment":{"DB_DATABASE_NAME":"{{service.db.database}}","DB_HOSTNAME":"{{service.db.host}}","DB_USERNAME":"{{service.db.user}}","DB_PASSWORD":"{{service.db.password}}","REDIS_HOSTNAME":"{{service.redis.host}}","REDIS_PASSWORD":"{{service.redis.password}}"},
|
||||
"volumes":[{"source":"immich-upload","target":"/usr/src/app/upload"}]}]}`,
|
||||
Variables: `[]`,
|
||||
IsOfficial: true,
|
||||
},
|
||||
}
|
||||
return templates
|
||||
}
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
package api
|
||||
|
||||
import "testing"
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNormalizeTemplateServiceType(t *testing.T) {
|
||||
valid := []string{"web", "worker", "database", "cron"}
|
||||
@@ -71,3 +74,32 @@ func TestResolveTemplateRuntimeImage(t *testing.T) {
|
||||
t.Fatalf("expected unknown runtime passthrough, got %q", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSeedTemplatesResolveCleanly(t *testing.T) {
|
||||
for _, tpl := range SeedTemplates() {
|
||||
var config TemplateConfig
|
||||
if err := json.Unmarshal([]byte(tpl.Config), &config); err != nil {
|
||||
t.Fatalf("%s: invalid config JSON: %v", tpl.ID, err)
|
||||
}
|
||||
var variables []TemplateVariable
|
||||
if err := json.Unmarshal([]byte(tpl.Variables), &variables); err != nil {
|
||||
t.Fatalf("%s: invalid variables JSON: %v", tpl.ID, err)
|
||||
}
|
||||
if len(config.Services) == 0 {
|
||||
continue // legacy single-service template
|
||||
}
|
||||
sorted, err := topoSortServices(config.Services)
|
||||
if err != nil {
|
||||
t.Fatalf("%s: topo sort: %v", tpl.ID, err)
|
||||
}
|
||||
if err := validateTemplateGraph(sorted); err != nil {
|
||||
t.Fatalf("%s: graph validation: %v", tpl.ID, err)
|
||||
}
|
||||
// User vars left empty — every expression must still resolve via
|
||||
// defaults, secrets, or service refs.
|
||||
graph := resolveTemplateGraph(sorted, map[string]string{})
|
||||
if len(graph.unresolved) > 0 {
|
||||
t.Fatalf("%s: unresolved expressions: %v", tpl.ID, graph.unresolved)
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,212 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"sort"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/docker"
|
||||
|
||||
"github.com/docker/docker/api/types"
|
||||
"github.com/docker/docker/api/types/filters"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/websocket"
|
||||
)
|
||||
|
||||
// Interactive terminal: a WebSocket bridged to a `docker exec` pty in the
|
||||
// service's container. Protocol is JSON text frames both ways:
|
||||
// client → server: {"type":"stdin","data":"..."} | {"type":"resize","cols":N,"rows":N}
|
||||
// server → client: {"type":"stdout","data":"..."} | {"type":"exit","code":N} | {"type":"error","message":"..."}
|
||||
// Shell preference: $SHELL, then bash, then sh.
|
||||
|
||||
type terminalFrame struct {
|
||||
Type string `json:"type"`
|
||||
Data string `json:"data,omitempty"`
|
||||
Cols uint `json:"cols,omitempty"`
|
||||
Rows uint `json:"rows,omitempty"`
|
||||
Code int `json:"code,omitempty"`
|
||||
Message string `json:"message,omitempty"`
|
||||
}
|
||||
|
||||
func handleServiceTerminal(c *gin.Context) {
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
serviceID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "INVALID_ID", "invalid service id")
|
||||
return
|
||||
}
|
||||
projectID, found := serviceProjectID(db, serviceID)
|
||||
if !found {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "service not found")
|
||||
return
|
||||
}
|
||||
if _, allowed := projectManageAccess(c, db, projectID); !allowed {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "service not found")
|
||||
return
|
||||
}
|
||||
dockerClient, _ := c.Get("docker_client")
|
||||
client, ok := dockerClient.(*docker.Client)
|
||||
if !ok || client == nil {
|
||||
respondError(c, http.StatusServiceUnavailable, "DOCKER_UNAVAILABLE", "Docker is not configured")
|
||||
return
|
||||
}
|
||||
|
||||
ctx := c.Request.Context()
|
||||
containers, err := client.ListContainersFiltered(ctx, filters.NewArgs(
|
||||
filters.Arg("label", "containr.service="+serviceID.String()),
|
||||
), false)
|
||||
if err != nil || len(containers) == 0 {
|
||||
respondError(c, http.StatusConflict, "NO_CONTAINER", "service has no running container")
|
||||
return
|
||||
}
|
||||
// Prefer a running container. With several matches, deterministically
|
||||
// pick the first `containr-<service>-<n>` replica by name — that is the
|
||||
// actual workload naming; auxiliary containers get ignored.
|
||||
target := ""
|
||||
sort.Slice(containers, func(i, j int) bool { return containers[i].Names[0] < containers[j].Names[0] })
|
||||
for _, ctr := range containers {
|
||||
if ctr.State == "running" && strings.HasPrefix(ctr.Names[0], "/containr-") {
|
||||
target = ctr.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
if target == "" {
|
||||
for _, ctr := range containers {
|
||||
if ctr.State == "running" {
|
||||
target = ctr.ID
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if target == "" {
|
||||
target = containers[0].ID
|
||||
}
|
||||
|
||||
conn, err := upgrader.Upgrade(c.Writer, c.Request, nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
defer conn.Close()
|
||||
|
||||
send := func(f terminalFrame) bool {
|
||||
return conn.WriteJSON(f) == nil
|
||||
}
|
||||
|
||||
// Pick a shell that actually exists — distroless images may have none.
|
||||
// `command -v` can't probe without a shell, so start each candidate and
|
||||
// watch for the OCI "no such file" failure.
|
||||
var stream types.HijackedResponse
|
||||
var execID string
|
||||
attached := false
|
||||
for _, candidate := range []string{"/bin/bash", "/bin/sh", "sh"} {
|
||||
exec, err := client.ExecCreate(ctx, target, docker.ExecConfig{
|
||||
Cmd: []string{candidate},
|
||||
AttachStdin: true,
|
||||
AttachStdout: true,
|
||||
AttachStderr: true,
|
||||
Tty: true,
|
||||
Env: []string{"TERM=xterm-256color", "COLORTERM=truecolor"},
|
||||
})
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
s, err := client.ExecAttach(ctx, exec.ID)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if execStartFailed(ctx, client, exec.ID, s) {
|
||||
s.Close()
|
||||
continue
|
||||
}
|
||||
stream, execID, attached = s, exec.ID, true
|
||||
break
|
||||
}
|
||||
if !attached {
|
||||
send(terminalFrame{Type: "error", Message: "no shell available in this container image"})
|
||||
return
|
||||
}
|
||||
defer stream.Close()
|
||||
|
||||
// The client sends a resize frame with its real size right after
|
||||
// connecting; start at a sane default meanwhile.
|
||||
_ = client.ExecResize(ctx, execID, 80, 24)
|
||||
|
||||
var wsWrite sync.Mutex
|
||||
writeJSON := func(f terminalFrame) {
|
||||
wsWrite.Lock()
|
||||
defer wsWrite.Unlock()
|
||||
_ = conn.WriteJSON(f)
|
||||
}
|
||||
|
||||
// Container → client.
|
||||
go func() {
|
||||
buf := make([]byte, 8192)
|
||||
for {
|
||||
n, err := stream.Reader.Read(buf)
|
||||
if n > 0 {
|
||||
writeJSON(terminalFrame{Type: "stdout", Data: string(buf[:n])})
|
||||
}
|
||||
if err != nil {
|
||||
if err != io.EOF {
|
||||
log.Printf("terminal: exec stream ended: %v", err)
|
||||
}
|
||||
inspect, ierr := client.ExecInspect(context.Background(), execID)
|
||||
code := 0
|
||||
if ierr == nil {
|
||||
code = inspect.ExitCode
|
||||
}
|
||||
writeJSON(terminalFrame{Type: "exit", Code: code})
|
||||
_ = conn.WriteControl(websocket.CloseMessage,
|
||||
websocket.FormatCloseMessage(websocket.CloseNormalClosure, "session ended"),
|
||||
time.Now().Add(time.Second))
|
||||
return
|
||||
}
|
||||
}
|
||||
}()
|
||||
|
||||
// Client → container.
|
||||
for {
|
||||
_, raw, err := conn.ReadMessage()
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
var f terminalFrame
|
||||
if err := json.Unmarshal(raw, &f); err != nil {
|
||||
continue
|
||||
}
|
||||
switch f.Type {
|
||||
case "stdin":
|
||||
if _, err := stream.Conn.Write([]byte(f.Data)); err != nil {
|
||||
return
|
||||
}
|
||||
case "resize":
|
||||
_ = client.ExecResize(ctx, execID, f.Cols, f.Rows)
|
||||
case "ping":
|
||||
writeJSON(terminalFrame{Type: "pong"})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// execStartFailed reports whether an attached exec failed to launch
|
||||
// (missing binary → non-zero exit, not running). The OCI failure takes a
|
||||
// moment to surface, so a short grace period elapses before judging.
|
||||
func execStartFailed(ctx context.Context, client *docker.Client, execID string, _ types.HijackedResponse) bool {
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
return true
|
||||
case <-time.After(500 * time.Millisecond):
|
||||
}
|
||||
inspect, err := client.ExecInspect(ctx, execID)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
return !inspect.Running && inspect.ExitCode != 0
|
||||
}
|
||||
@@ -0,0 +1,194 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
sqlcdb "containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// Personal access tokens let the CLI, MCP server, and third-party agents
|
||||
// authenticate without a browser session. The raw token (cnp_…) is
|
||||
// returned exactly once at creation; only its sha256 hash persists.
|
||||
|
||||
var userTokenScopes = map[string]bool{"read": true, "write": true, "admin": true}
|
||||
|
||||
func generateUserToken() (token string, prefix string, hash string, err error) {
|
||||
buf := make([]byte, 24)
|
||||
if _, err = rand.Read(buf); err != nil {
|
||||
return "", "", "", err
|
||||
}
|
||||
token = "cnp_" + hex.EncodeToString(buf)
|
||||
prefix = token[:12] // cnp_ + 8 hex chars, enough to identify in a list
|
||||
sum := sha256.Sum256([]byte(token))
|
||||
return token, prefix, hex.EncodeToString(sum[:]), nil
|
||||
}
|
||||
|
||||
func userTokenJSON(t sqlcdb.UserToken) gin.H {
|
||||
row := gin.H{
|
||||
"id": t.ID.String(),
|
||||
"name": t.Name,
|
||||
"key_prefix": t.KeyPrefix,
|
||||
"scope": t.Scope,
|
||||
}
|
||||
if t.CreatedAt.Valid {
|
||||
row["created_at"] = t.CreatedAt.Time
|
||||
}
|
||||
if t.LastUsedAt.Valid {
|
||||
row["last_used_at"] = t.LastUsedAt.Time
|
||||
}
|
||||
if t.ExpiresAt.Valid {
|
||||
row["expires_at"] = t.ExpiresAt.Time
|
||||
}
|
||||
return row
|
||||
}
|
||||
|
||||
func handleCreateUserToken(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
queries := sqlcdb.New(db.DB)
|
||||
|
||||
var req struct {
|
||||
Name string `json:"name"`
|
||||
Scope string `json:"scope"`
|
||||
ExpiresInDays int `json:"expires_in_days"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
if req.Name == "" {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "name is required", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
if req.Scope == "" {
|
||||
req.Scope = "write"
|
||||
}
|
||||
if !userTokenScopes[req.Scope] {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "scope must be one of: read, write, admin", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
// A token can never outrank its owner: non-admin users cannot mint
|
||||
// admin-scope tokens even for themselves.
|
||||
if req.Scope == "admin" && !c.GetBool("is_admin") {
|
||||
c.JSON(http.StatusForbidden, gin.H{"error": "admin scope requires a platform admin account", "code": "FORBIDDEN"})
|
||||
return
|
||||
}
|
||||
if req.ExpiresInDays < 0 || req.ExpiresInDays > 3650 {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "expires_in_days must be between 0 (never) and 3650", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
token, prefix, hash, err := generateUserToken()
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to generate token", "code": "INTERNAL"})
|
||||
return
|
||||
}
|
||||
|
||||
ownerUUID, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid user context", "code": "INVALID_TOKEN"})
|
||||
return
|
||||
}
|
||||
|
||||
expiresAt := sql.NullTime{}
|
||||
if req.ExpiresInDays > 0 {
|
||||
expiresAt = sql.NullTime{Time: time.Now().Add(time.Duration(req.ExpiresInDays) * 24 * time.Hour), Valid: true}
|
||||
}
|
||||
|
||||
row, err := queries.CreateUserToken(c.Request.Context(), sqlcdb.CreateUserTokenParams{
|
||||
UserID: ownerUUID,
|
||||
Name: req.Name,
|
||||
KeyPrefix: prefix,
|
||||
TokenHash: hash,
|
||||
Scope: req.Scope,
|
||||
ExpiresAt: expiresAt,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to store token", "code": "INTERNAL"})
|
||||
return
|
||||
}
|
||||
|
||||
LogAudit(userID, "user_token", row.ID.String(), "create", map[string]interface{}{"name": req.Name, "scope": req.Scope})
|
||||
|
||||
payload := userTokenJSON(row)
|
||||
payload["token"] = token
|
||||
c.JSON(http.StatusCreated, payload)
|
||||
}
|
||||
|
||||
func handleListUserTokens(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
queries := sqlcdb.New(db.DB)
|
||||
|
||||
ownerUUID, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid user context", "code": "INVALID_TOKEN"})
|
||||
return
|
||||
}
|
||||
|
||||
rows, err := queries.ListUserTokens(c.Request.Context(), ownerUUID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to fetch tokens", "code": "INTERNAL"})
|
||||
return
|
||||
}
|
||||
|
||||
out := make([]gin.H, 0, len(rows))
|
||||
for _, t := range rows {
|
||||
out = append(out, userTokenJSON(t))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"tokens": out})
|
||||
}
|
||||
|
||||
func handleDeleteUserToken(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
queries := sqlcdb.New(db.DB)
|
||||
|
||||
ownerUUID, err := uuid.Parse(userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusUnauthorized, gin.H{"error": "Invalid user context", "code": "INVALID_TOKEN"})
|
||||
return
|
||||
}
|
||||
tokenUUID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "Invalid token ID", "code": "VALIDATION"})
|
||||
return
|
||||
}
|
||||
|
||||
affected, err := queries.RevokeUserToken(c.Request.Context(), sqlcdb.RevokeUserTokenParams{
|
||||
ID: tokenUUID,
|
||||
UserID: ownerUUID,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to revoke token", "code": "INTERNAL"})
|
||||
return
|
||||
}
|
||||
if affected == 0 {
|
||||
c.JSON(http.StatusNotFound, gin.H{"error": "Token not found", "code": "NOT_FOUND"})
|
||||
return
|
||||
}
|
||||
|
||||
LogAudit(userID, "user_token", tokenUUID.String(), "revoke", nil)
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Token revoked"})
|
||||
}
|
||||
@@ -1,10 +1,14 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"containr/internal/database"
|
||||
"database/sql"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
"containr/internal/secrets"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
@@ -25,6 +29,7 @@ type EnvironmentVariable struct {
|
||||
|
||||
type UpdateVariablesRequest struct {
|
||||
Variables []VariableInput `json:"variables" binding:"required"`
|
||||
Redeploy bool `json:"redeploy"`
|
||||
}
|
||||
|
||||
type VariableInput struct {
|
||||
@@ -145,7 +150,8 @@ func handleUpdateVariables(c *gin.Context) {
|
||||
}
|
||||
defer tx.Rollback()
|
||||
|
||||
_, err = tx.Exec("DELETE FROM environment_variables WHERE service_id = $1", serviceID)
|
||||
txq := sqlcdb.New(tx)
|
||||
err = txq.DeleteEnvVarsForService(c.Request.Context(), serviceID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to clear existing variables"})
|
||||
return
|
||||
@@ -159,12 +165,18 @@ func handleUpdateVariables(c *gin.Context) {
|
||||
value = stored
|
||||
}
|
||||
}
|
||||
varID := uuid.New()
|
||||
_, err = tx.Exec(
|
||||
`INSERT INTO environment_variables (id, service_id, key, value, is_secret, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
||||
varID, serviceID, v.Key, value, v.IsSecret, now, now,
|
||||
)
|
||||
if v.IsSecret {
|
||||
value = secrets.Encrypt(value)
|
||||
}
|
||||
err = txq.InsertEnvVar(c.Request.Context(), sqlcdb.InsertEnvVarParams{
|
||||
ID: uuid.New(),
|
||||
ServiceID: serviceID,
|
||||
Key: v.Key,
|
||||
Value: value,
|
||||
IsSecret: sql.NullBool{Bool: v.IsSecret, Valid: true},
|
||||
CreatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: now, Valid: true},
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to insert variable: " + v.Key})
|
||||
return
|
||||
@@ -204,5 +216,24 @@ func handleUpdateVariables(c *gin.Context) {
|
||||
variables = append(variables, v)
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"variables": variables, "message": "Environment variables updated successfully"})
|
||||
resp := gin.H{"variables": variables, "message": "Environment variables updated successfully"}
|
||||
if req.Redeploy {
|
||||
// Load the service spec inputs and queue a reconcile behind any
|
||||
// in-flight deployment — new env lands in the recreated containers.
|
||||
var svc Service
|
||||
svcErr := db.(*database.DB).QueryRow(
|
||||
`SELECT id, project_id, name, COALESCE(image, ''), COALESCE(command, ''),
|
||||
COALESCE(replicas, 1), COALESCE(port, 0), COALESCE(domain, ''),
|
||||
COALESCE(healthcheck_path, ''), COALESCE(restart_policy, 'unless-stopped'),
|
||||
COALESCE(cpu, ''), COALESCE(memory, ''),
|
||||
COALESCE(builder, 'auto'), COALESCE(cpu_reserve, ''), COALESCE(memory_reserve, '')
|
||||
FROM services WHERE id = $1`,
|
||||
serviceID,
|
||||
).Scan(&svc.ID, &svc.ProjectID, &svc.Name, &svc.Image, &svc.Command,
|
||||
&svc.Replicas, &svc.Port, &svc.Domain, &svc.HealthCheckPath,
|
||||
&svc.RestartPolicy, &svc.CPU, &svc.Memory,
|
||||
&svc.Builder, &svc.CPUReserve, &svc.MemoryReserve)
|
||||
resp["redeploy_queued"] = svcErr == nil && enqueueServiceRedeploy(c, db.(*database.DB), svc)
|
||||
}
|
||||
c.JSON(http.StatusOK, resp)
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"containr/internal/docker"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// VolumeInfo is one docker volume plus whether any container mounts it.
|
||||
type VolumeInfo struct {
|
||||
Name string `json:"name"`
|
||||
Driver string `json:"driver"`
|
||||
Mountpoint string `json:"mountpoint"`
|
||||
Scope string `json:"scope"`
|
||||
InUse bool `json:"in_use"`
|
||||
UsedBy []string `json:"used_by,omitempty"`
|
||||
Labels map[string]string `json:"labels,omitempty"`
|
||||
CreatedAt time.Time `json:"created_at,omitempty"`
|
||||
}
|
||||
|
||||
func dockerClientOr500(c *gin.Context) (*docker.Client, bool) {
|
||||
v, exists := c.Get("docker_client")
|
||||
if !exists || v == nil {
|
||||
respondError(c, http.StatusInternalServerError, "INTERNAL", "Docker client not available")
|
||||
return nil, false
|
||||
}
|
||||
return v.(*docker.Client), true
|
||||
}
|
||||
|
||||
// handleAdminListVolumes returns the node's docker volume inventory with
|
||||
// in-use flags. Admin-only: mountpoints reveal host filesystem layout.
|
||||
func handleAdminListVolumes(c *gin.Context) {
|
||||
client, ok := dockerClientOr500(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
ctx := c.Request.Context()
|
||||
|
||||
vols, err := client.ListVolumes(ctx)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadGateway, "DEPENDENCY_UNAVAILABLE", "Failed to list volumes: "+err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
containers, err := client.ListContainers(ctx, true)
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadGateway, "DEPENDENCY_UNAVAILABLE", "Failed to list containers: "+err.Error())
|
||||
return
|
||||
}
|
||||
inUse := map[string][]string{}
|
||||
for _, cont := range containers {
|
||||
for _, m := range cont.Mounts {
|
||||
if m.Type == "volume" {
|
||||
name := cont.ID[:12]
|
||||
if len(cont.Names) > 0 {
|
||||
name = cont.Names[0]
|
||||
}
|
||||
inUse[m.Name] = append(inUse[m.Name], name)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
out := make([]VolumeInfo, 0, len(vols.Volumes))
|
||||
for _, v := range vols.Volumes {
|
||||
if v == nil {
|
||||
continue
|
||||
}
|
||||
info := VolumeInfo{
|
||||
Name: v.Name,
|
||||
Driver: v.Driver,
|
||||
Mountpoint: v.Mountpoint,
|
||||
Scope: v.Scope,
|
||||
Labels: v.Labels,
|
||||
UsedBy: inUse[v.Name],
|
||||
}
|
||||
info.InUse = len(info.UsedBy) > 0
|
||||
if t, err := time.Parse(time.RFC3339Nano, v.CreatedAt); err == nil {
|
||||
info.CreatedAt = t
|
||||
}
|
||||
out = append(out, info)
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"volumes": out})
|
||||
}
|
||||
|
||||
// handleAdminDeleteVolume removes a docker volume. Docker refuses removal
|
||||
// of in-use volumes — that error is surfaced as a conflict.
|
||||
func handleAdminDeleteVolume(c *gin.Context) {
|
||||
client, ok := dockerClientOr500(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name := c.Param("name")
|
||||
if name == "" {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Volume name is required")
|
||||
return
|
||||
}
|
||||
if err := client.RemoveVolume(c.Request.Context(), name, false); err != nil {
|
||||
respondError(c, http.StatusConflict, "CONFLICT", "Failed to remove volume: "+err.Error())
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"deleted": name})
|
||||
}
|
||||
@@ -0,0 +1,543 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"crypto/hmac"
|
||||
"crypto/rand"
|
||||
"crypto/sha256"
|
||||
"database/sql"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"containr/internal/database"
|
||||
"containr/internal/database/sqlcdb"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/google/uuid"
|
||||
"github.com/sqlc-dev/pqtype"
|
||||
)
|
||||
|
||||
// Outbound webhooks: every audited action (LogAudit) emits an event named
|
||||
// "resource.action" (e.g. "service.deploy"). Webhooks subscribe with an
|
||||
// event list supporting "service.*" and "*" wildcards. Deliveries are
|
||||
// HMAC-signed and retried up to 3 times with backoff; each attempt lands
|
||||
// in webhook_deliveries.
|
||||
|
||||
var (
|
||||
webhookQueue = make(chan webhookJob, 512)
|
||||
webhookWorkers sync.Once
|
||||
)
|
||||
|
||||
type webhookJob struct {
|
||||
Webhook sqlcdb.OutboundWebhook
|
||||
Event string
|
||||
Payload []byte
|
||||
}
|
||||
|
||||
type webhookEventPayload struct {
|
||||
Event string `json:"event"`
|
||||
Resource string `json:"resource"`
|
||||
ResourceID string `json:"resource_id"`
|
||||
Action string `json:"action"`
|
||||
UserID string `json:"user_id,omitempty"`
|
||||
Details map[string]interface{} `json:"details,omitempty"`
|
||||
Timestamp time.Time `json:"timestamp"`
|
||||
}
|
||||
|
||||
// emitWebhookEvent fans an audited action out to all enabled webhooks whose
|
||||
// event list matches. Called from LogAudit — fire-and-forget.
|
||||
func emitWebhookEvent(userID, resource, resourceID, action string, details map[string]interface{}) {
|
||||
db := GetAuditDB()
|
||||
if db == nil {
|
||||
return
|
||||
}
|
||||
// Actions that already carry a resource prefix ("service.clone") pass
|
||||
// through unchanged; bare actions ("create") get resource-qualified.
|
||||
event := action
|
||||
if !strings.Contains(action, ".") {
|
||||
event = resource + "." + action
|
||||
}
|
||||
|
||||
queries := sqlcdb.New(db.DB)
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
hooks, err := queries.ListEnabledOutboundWebhooks(ctx)
|
||||
if err != nil || len(hooks) == 0 {
|
||||
return
|
||||
}
|
||||
|
||||
payload, err := json.Marshal(webhookEventPayload{
|
||||
Event: event, Resource: resource, ResourceID: resourceID,
|
||||
Action: action, UserID: userID, Details: details, Timestamp: time.Now().UTC(),
|
||||
})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
uid, uidErr := uuid.Parse(strings.TrimSpace(userID))
|
||||
for _, hook := range hooks {
|
||||
if !webhookEventMatches(hook.Events, event) {
|
||||
continue
|
||||
}
|
||||
// Webhooks only receive events for their owner — unless subscribed to "*"
|
||||
// is deliberate admin reach; owner scoping keeps per-user isolation.
|
||||
if uidErr != nil || hook.UserID != uid {
|
||||
continue
|
||||
}
|
||||
select {
|
||||
case webhookQueue <- webhookJob{Webhook: hook, Event: event, Payload: payload}:
|
||||
default:
|
||||
// Queue full: drop rather than block the request path.
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func webhookEventMatches(events json.RawMessage, event string) bool {
|
||||
var subs []string
|
||||
if err := json.Unmarshal(events, &subs); err != nil || len(subs) == 0 {
|
||||
return false
|
||||
}
|
||||
for _, sub := range subs {
|
||||
sub = strings.TrimSpace(sub)
|
||||
if sub == "*" || sub == event {
|
||||
return true
|
||||
}
|
||||
if strings.HasSuffix(sub, ".*") && strings.HasPrefix(event, strings.TrimSuffix(sub, "*")) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// StartWebhookDispatcher launches delivery workers; call once at boot.
|
||||
func StartWebhookDispatcher() {
|
||||
webhookWorkers.Do(func() {
|
||||
for i := 0; i < 4; i++ {
|
||||
go webhookDeliveryWorker()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
func webhookDeliveryWorker() {
|
||||
for job := range webhookQueue {
|
||||
deliverWebhook(job)
|
||||
}
|
||||
}
|
||||
|
||||
var webhookHTTPClient = &http.Client{Timeout: 10 * time.Second}
|
||||
|
||||
func deliverWebhook(job webhookJob) {
|
||||
db := GetAuditDB()
|
||||
if db == nil {
|
||||
return
|
||||
}
|
||||
queries := sqlcdb.New(db.DB)
|
||||
ctx := context.Background()
|
||||
|
||||
deliveryID := uuid.New()
|
||||
_ = queries.CreateWebhookDelivery(ctx, sqlcdb.CreateWebhookDeliveryParams{
|
||||
ID: deliveryID,
|
||||
WebhookID: job.Webhook.ID,
|
||||
Event: job.Event,
|
||||
Payload: pqtype.NullRawMessage{RawMessage: job.Payload, Valid: true},
|
||||
Status: "pending",
|
||||
})
|
||||
|
||||
var lastStatus string = "failed"
|
||||
var respStatus sql.NullInt32
|
||||
var respBody sql.NullString
|
||||
var durationMs sql.NullInt32
|
||||
var deliveredAt sql.NullTime
|
||||
|
||||
for attempt := int32(1); attempt <= 3; attempt++ {
|
||||
start := time.Now()
|
||||
status, code, body := postWebhook(job, deliveryID, attempt)
|
||||
durationMs = sql.NullInt32{Int32: int32(time.Since(start).Milliseconds()), Valid: true}
|
||||
respStatus = code
|
||||
respBody = body
|
||||
if status == "success" {
|
||||
lastStatus = "success"
|
||||
deliveredAt = sql.NullTime{Time: time.Now().UTC(), Valid: true}
|
||||
_ = queries.UpdateWebhookDeliveryResult(ctx, sqlcdb.UpdateWebhookDeliveryResultParams{
|
||||
ID: deliveryID,
|
||||
Status: lastStatus,
|
||||
ResponseStatus: respStatus,
|
||||
ResponseBody: respBody,
|
||||
Attempts: attempt,
|
||||
DurationMs: durationMs,
|
||||
DeliveredAt: deliveredAt,
|
||||
})
|
||||
return
|
||||
}
|
||||
if attempt < 3 {
|
||||
time.Sleep(time.Duration(attempt*attempt) * 2 * time.Second)
|
||||
}
|
||||
}
|
||||
_ = queries.UpdateWebhookDeliveryResult(ctx, sqlcdb.UpdateWebhookDeliveryResultParams{
|
||||
ID: deliveryID,
|
||||
Status: lastStatus,
|
||||
ResponseStatus: respStatus,
|
||||
ResponseBody: respBody,
|
||||
Attempts: 3,
|
||||
DurationMs: durationMs,
|
||||
DeliveredAt: deliveredAt,
|
||||
})
|
||||
}
|
||||
|
||||
func postWebhook(job webhookJob, deliveryID uuid.UUID, attempt int32) (string, sql.NullInt32, sql.NullString) {
|
||||
req, err := http.NewRequest(http.MethodPost, job.Webhook.Url, bytes.NewReader(job.Payload))
|
||||
if err != nil {
|
||||
return "failed", sql.NullInt32{}, sql.NullString{String: err.Error(), Valid: true}
|
||||
}
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("User-Agent", "Containr-Webhook/1.0")
|
||||
req.Header.Set("X-Containr-Event", job.Event)
|
||||
req.Header.Set("X-Containr-Delivery", deliveryID.String())
|
||||
if job.Webhook.Secret != "" {
|
||||
mac := hmac.New(sha256.New, []byte(job.Webhook.Secret))
|
||||
mac.Write(job.Payload)
|
||||
req.Header.Set("X-Containr-Signature", "sha256="+hex.EncodeToString(mac.Sum(nil)))
|
||||
}
|
||||
if job.Webhook.Headers.Valid {
|
||||
var custom map[string]string
|
||||
if err := json.Unmarshal(job.Webhook.Headers.RawMessage, &custom); err == nil {
|
||||
for k, v := range custom {
|
||||
req.Header.Set(k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
resp, err := webhookHTTPClient.Do(req)
|
||||
if err != nil {
|
||||
return "failed", sql.NullInt32{}, sql.NullString{String: err.Error(), Valid: true}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, _ := io.ReadAll(io.LimitReader(resp.Body, 4096))
|
||||
code := sql.NullInt32{Int32: int32(resp.StatusCode), Valid: true}
|
||||
bodyStr := sql.NullString{String: string(body), Valid: len(body) > 0}
|
||||
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
|
||||
return "success", code, bodyStr
|
||||
}
|
||||
return "failed", code, bodyStr
|
||||
}
|
||||
|
||||
// ---------- HTTP API ----------
|
||||
|
||||
type outboundWebhookResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Secret string `json:"secret,omitempty"`
|
||||
Events []string `json:"events"`
|
||||
Headers map[string]string `json:"headers,omitempty"`
|
||||
Enabled bool `json:"enabled"`
|
||||
CreatedAt time.Time `json:"created_at"`
|
||||
UpdatedAt time.Time `json:"updated_at"`
|
||||
}
|
||||
|
||||
func outboundWebhookJSON(row sqlcdb.OutboundWebhook, revealSecret bool) outboundWebhookResponse {
|
||||
var events []string
|
||||
_ = json.Unmarshal(row.Events, &events)
|
||||
if events == nil {
|
||||
events = []string{}
|
||||
}
|
||||
var headers map[string]string
|
||||
if row.Headers.Valid {
|
||||
_ = json.Unmarshal(row.Headers.RawMessage, &headers)
|
||||
}
|
||||
resp := outboundWebhookResponse{
|
||||
ID: row.ID.String(),
|
||||
Name: row.Name,
|
||||
URL: row.Url,
|
||||
Events: events,
|
||||
Headers: headers,
|
||||
Enabled: row.Enabled,
|
||||
}
|
||||
if revealSecret {
|
||||
resp.Secret = row.Secret
|
||||
}
|
||||
if row.CreatedAt.Valid {
|
||||
resp.CreatedAt = row.CreatedAt.Time
|
||||
}
|
||||
if row.UpdatedAt.Valid {
|
||||
resp.UpdatedAt = row.UpdatedAt.Time
|
||||
}
|
||||
return resp
|
||||
}
|
||||
|
||||
func validWebhookURL(raw string) error {
|
||||
u, err := url.Parse(strings.TrimSpace(raw))
|
||||
if err != nil || (u.Scheme != "http" && u.Scheme != "https") || u.Host == "" {
|
||||
return errors.New("url must be a valid http(s) URL")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type webhookMutationRequest struct {
|
||||
Name string `json:"name"`
|
||||
URL string `json:"url"`
|
||||
Secret *string `json:"secret"`
|
||||
Events []string `json:"events"`
|
||||
Headers map[string]string `json:"headers"`
|
||||
Enabled *bool `json:"enabled"`
|
||||
}
|
||||
|
||||
func handleListOutboundWebhooks(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserUUID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
rows, err := sqlcdb.New(db.DB).ListOutboundWebhooksByUser(c.Request.Context(), userID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list webhooks"})
|
||||
return
|
||||
}
|
||||
out := make([]outboundWebhookResponse, 0, len(rows))
|
||||
for _, row := range rows {
|
||||
out = append(out, outboundWebhookJSON(row, false))
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"webhooks": out})
|
||||
}
|
||||
|
||||
func handleCreateOutboundWebhook(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserUUID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
var req webhookMutationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
req.Name = strings.TrimSpace(req.Name)
|
||||
if req.Name == "" {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "name is required")
|
||||
return
|
||||
}
|
||||
if err := validWebhookURL(req.URL); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", err.Error())
|
||||
return
|
||||
}
|
||||
if len(req.Events) == 0 {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "at least one event subscription is required (e.g. \"service.*\" or \"*\")")
|
||||
return
|
||||
}
|
||||
|
||||
secret := ""
|
||||
if req.Secret != nil {
|
||||
secret = strings.TrimSpace(*req.Secret)
|
||||
}
|
||||
if secret == "" {
|
||||
buf := make([]byte, 24)
|
||||
_, _ = rand.Read(buf)
|
||||
secret = hex.EncodeToString(buf)
|
||||
}
|
||||
eventsJSON, _ := json.Marshal(req.Events)
|
||||
var headersJSON pqtype.NullRawMessage
|
||||
if len(req.Headers) > 0 {
|
||||
if raw, err := json.Marshal(req.Headers); err == nil {
|
||||
headersJSON = pqtype.NullRawMessage{RawMessage: raw, Valid: true}
|
||||
}
|
||||
}
|
||||
enabled := true
|
||||
if req.Enabled != nil {
|
||||
enabled = *req.Enabled
|
||||
}
|
||||
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
id := uuid.New()
|
||||
err := sqlcdb.New(db.DB).CreateOutboundWebhook(c.Request.Context(), sqlcdb.CreateOutboundWebhookParams{
|
||||
ID: id,
|
||||
UserID: userID,
|
||||
Name: req.Name,
|
||||
Url: strings.TrimSpace(req.URL),
|
||||
Secret: secret,
|
||||
Events: eventsJSON,
|
||||
Headers: headersJSON,
|
||||
Enabled: enabled,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to create webhook"})
|
||||
return
|
||||
}
|
||||
LogAuditWithRequest(c, "webhook", id.String(), "create", map[string]interface{}{"name": req.Name})
|
||||
c.JSON(http.StatusCreated, outboundWebhookJSON(sqlcdb.OutboundWebhook{
|
||||
ID: id, Name: req.Name, Url: strings.TrimSpace(req.URL), Secret: secret,
|
||||
Events: eventsJSON, Headers: headersJSON, Enabled: enabled,
|
||||
CreatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
UpdatedAt: sql.NullTime{Time: time.Now(), Valid: true},
|
||||
}, true))
|
||||
}
|
||||
|
||||
func handleUpdateOutboundWebhook(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserUUID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
webhookID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
queries := sqlcdb.New(db.DB)
|
||||
row, err := queries.GetOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.GetOutboundWebhookByIDAndUserParams{
|
||||
ID: webhookID, UserID: userID,
|
||||
})
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Webhook not found")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load webhook"})
|
||||
return
|
||||
}
|
||||
|
||||
var req webhookMutationRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
return
|
||||
}
|
||||
name := row.Name
|
||||
if req.Name != "" {
|
||||
name = strings.TrimSpace(req.Name)
|
||||
}
|
||||
targetURL := row.Url
|
||||
if req.URL != "" {
|
||||
if err := validWebhookURL(req.URL); err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", err.Error())
|
||||
return
|
||||
}
|
||||
targetURL = strings.TrimSpace(req.URL)
|
||||
}
|
||||
secret := row.Secret
|
||||
if req.Secret != nil {
|
||||
secret = strings.TrimSpace(*req.Secret)
|
||||
}
|
||||
eventsJSON := row.Events
|
||||
if req.Events != nil {
|
||||
if len(req.Events) == 0 {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "at least one event subscription is required")
|
||||
return
|
||||
}
|
||||
eventsJSON, _ = json.Marshal(req.Events)
|
||||
}
|
||||
headersJSON := row.Headers
|
||||
if req.Headers != nil {
|
||||
if raw, err := json.Marshal(req.Headers); err == nil {
|
||||
headersJSON = pqtype.NullRawMessage{RawMessage: raw, Valid: true}
|
||||
}
|
||||
}
|
||||
enabled := row.Enabled
|
||||
if req.Enabled != nil {
|
||||
enabled = *req.Enabled
|
||||
}
|
||||
|
||||
err = queries.UpdateOutboundWebhook(c.Request.Context(), sqlcdb.UpdateOutboundWebhookParams{
|
||||
ID: webhookID, UserID: userID,
|
||||
Name: name, Url: targetURL, Secret: secret,
|
||||
Events: eventsJSON, Headers: headersJSON, Enabled: enabled,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to update webhook"})
|
||||
return
|
||||
}
|
||||
LogAuditWithRequest(c, "webhook", webhookID.String(), "update", map[string]interface{}{"name": name})
|
||||
row.Name, row.Url, row.Secret, row.Events, row.Headers, row.Enabled = name, targetURL, secret, eventsJSON, headersJSON, enabled
|
||||
c.JSON(http.StatusOK, outboundWebhookJSON(row, true))
|
||||
}
|
||||
|
||||
func handleDeleteOutboundWebhook(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserUUID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
webhookID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
err = sqlcdb.New(db.DB).DeleteOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.DeleteOutboundWebhookByIDAndUserParams{
|
||||
ID: webhookID, UserID: userID,
|
||||
})
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to delete webhook"})
|
||||
return
|
||||
}
|
||||
LogAuditWithRequest(c, "webhook", webhookID.String(), "delete", map[string]interface{}{})
|
||||
c.JSON(http.StatusOK, gin.H{"message": "Webhook deleted"})
|
||||
}
|
||||
|
||||
func handleListOutboundWebhookDeliveries(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserUUID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
webhookID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
queries := sqlcdb.New(db.DB)
|
||||
if _, err := queries.GetOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.GetOutboundWebhookByIDAndUserParams{
|
||||
ID: webhookID, UserID: userID,
|
||||
}); errors.Is(err, sql.ErrNoRows) {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Webhook not found")
|
||||
return
|
||||
}
|
||||
rows, err := queries.ListWebhookDeliveriesByWebhook(c.Request.Context(), webhookID)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to list deliveries"})
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, gin.H{"deliveries": rows})
|
||||
}
|
||||
|
||||
func handleTestOutboundWebhook(c *gin.Context) {
|
||||
userID, ok := requireAuthenticatedUserUUID(c)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
webhookID, err := uuid.Parse(c.Param("id"))
|
||||
if err != nil {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Invalid webhook id")
|
||||
return
|
||||
}
|
||||
db := c.MustGet("db").(*database.DB)
|
||||
row, err := sqlcdb.New(db.DB).GetOutboundWebhookByIDAndUser(c.Request.Context(), sqlcdb.GetOutboundWebhookByIDAndUserParams{
|
||||
ID: webhookID, UserID: userID,
|
||||
})
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
respondError(c, http.StatusNotFound, "NOT_FOUND", "Webhook not found")
|
||||
return
|
||||
}
|
||||
if err != nil {
|
||||
c.JSON(http.StatusInternalServerError, gin.H{"error": "Failed to load webhook"})
|
||||
return
|
||||
}
|
||||
if !row.Enabled {
|
||||
respondError(c, http.StatusBadRequest, "VALIDATION", "Webhook is disabled")
|
||||
return
|
||||
}
|
||||
payload, _ := json.Marshal(webhookEventPayload{
|
||||
Event: "ping", Resource: "webhook", ResourceID: webhookID.String(),
|
||||
Action: "ping", UserID: userID.String(), Timestamp: time.Now().UTC(),
|
||||
})
|
||||
select {
|
||||
case webhookQueue <- webhookJob{Webhook: row, Event: "ping", Payload: payload}:
|
||||
c.JSON(http.StatusAccepted, gin.H{"message": "Test delivery queued"})
|
||||
default:
|
||||
respondError(c, http.StatusServiceUnavailable, "UNAVAILABLE", "Delivery queue is full")
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
package api
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestWebhookEventMatches(t *testing.T) {
|
||||
subs := func(list ...string) json.RawMessage {
|
||||
raw, _ := json.Marshal(list)
|
||||
return raw
|
||||
}
|
||||
cases := []struct {
|
||||
events json.RawMessage
|
||||
event string
|
||||
want bool
|
||||
}{
|
||||
{subs("service.*"), "service.deploy", true},
|
||||
{subs("service.*"), "database.create", false},
|
||||
{subs("*"), "service.deploy", true},
|
||||
{subs("deployment.fail", "service.*"), "deployment.fail", true},
|
||||
{subs("deployment.fail"), "deployment.success", false},
|
||||
{subs(" ping "), "ping", true},
|
||||
{subs(), "service.deploy", false},
|
||||
{json.RawMessage(`"not-a-list"`), "service.deploy", false},
|
||||
{json.RawMessage(`invalid`), "service.deploy", false},
|
||||
}
|
||||
for i, tc := range cases {
|
||||
if got := webhookEventMatches(tc.events, tc.event); got != tc.want {
|
||||
t.Errorf("case %d: matches(%s, %q) = %v, want %v", i, tc.events, tc.event, got, tc.want)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidWebhookURL(t *testing.T) {
|
||||
cases := []struct {
|
||||
name string
|
||||
url string
|
||||
wantErr bool
|
||||
}{
|
||||
{"https", "https://hooks.example.com/wh/abc", false},
|
||||
{"http", "http://internal.lan:8080/notify", false},
|
||||
{"whitespace trimmed", " https://example.com/hook ", false},
|
||||
{"with query", "https://example.com/hook?token=x", false},
|
||||
{"ftp rejected", "ftp://files.example.com/x", true},
|
||||
{"no scheme", "example.com/hook", true},
|
||||
{"bare scheme", "https://", true},
|
||||
{"empty", "", true},
|
||||
{"javascript rejected", "javascript:alert(1)", true},
|
||||
{"host empty", "http:///path-only", true},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
err := validWebhookURL(tc.url)
|
||||
if tc.wantErr && err == nil {
|
||||
t.Errorf("validWebhookURL(%q) = nil, want error", tc.url)
|
||||
}
|
||||
if !tc.wantErr && err != nil {
|
||||
t.Errorf("validWebhookURL(%q) = %v, want nil", tc.url, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -211,8 +211,12 @@ func (d *DockerfileBuilder) Build(ctx context.Context, req *types.BuildRequest)
|
||||
// Build Docker image
|
||||
imageName := fmt.Sprintf("%s:%s", req.ImageName, req.ImageTag)
|
||||
|
||||
// Create build context tar
|
||||
buildCtx, err := createBuildContext(req.SourcePath, optimizedDockerfile)
|
||||
// createBuildContext wants the Dockerfile contents, not its path.
|
||||
dockerfileContent, err := os.ReadFile(optimizedDockerfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read Dockerfile: %w", err)
|
||||
}
|
||||
buildCtx, err := createBuildContext(req.SourcePath, string(dockerfileContent))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create build context: %w", err)
|
||||
}
|
||||
@@ -222,6 +226,7 @@ func (d *DockerfileBuilder) Build(ctx context.Context, req *types.BuildRequest)
|
||||
Dockerfile: "Dockerfile",
|
||||
Tags: []string{imageName},
|
||||
BuildArgs: buildArgs,
|
||||
NoCache: req.NoCache,
|
||||
Remove: true,
|
||||
}
|
||||
|
||||
@@ -534,12 +539,16 @@ func createBuildContext(sourcePath, dockerfileContent string) (io.ReadCloser, er
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// Add source files to tar
|
||||
// Add source files to tar. The checkout's .git metadata is never build
|
||||
// context — it bloats the tar and ships clone URLs to remote daemons.
|
||||
err = filepath.Walk(sourcePath, func(file string, fi os.FileInfo, err error) error {
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if fi.IsDir() {
|
||||
if fi.Name() == ".git" {
|
||||
return filepath.SkipDir
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
@@ -3,6 +3,8 @@ package build
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
@@ -16,6 +18,7 @@ type BuildManager struct {
|
||||
railpackBuilder *RailpackBuilder
|
||||
nixpacksBuilder *NixpacksBuilder
|
||||
dockerfileBuilder *DockerfileBuilder
|
||||
staticBuilder *StaticBuilder
|
||||
dockerClient *docker.Client
|
||||
cacheManager *CacheManager
|
||||
parallelBuilder *ParallelBuilder
|
||||
@@ -28,6 +31,7 @@ const (
|
||||
BuildTypeRailpack BuildType = "railpack"
|
||||
BuildTypeNixpacks BuildType = "nixpacks"
|
||||
BuildTypeDockerfile BuildType = "dockerfile"
|
||||
BuildTypeStatic BuildType = "static"
|
||||
BuildTypePrebuilt BuildType = "prebuilt"
|
||||
)
|
||||
|
||||
@@ -39,6 +43,7 @@ func NewBuildManager(workDir string, dockerClient *docker.Client) *BuildManager
|
||||
railpackBuilder: NewRailpackBuilder(workDir, dockerClient),
|
||||
nixpacksBuilder: NewNixpacksBuilder(workDir, dockerClient),
|
||||
dockerfileBuilder: NewDockerfileBuilder(workDir, dockerClient),
|
||||
staticBuilder: NewStaticBuilder(dockerClient),
|
||||
dockerClient: dockerClient,
|
||||
cacheManager: cacheManager,
|
||||
parallelBuilder: parallelBuilder,
|
||||
@@ -80,6 +85,8 @@ func (bm *BuildManager) Build(ctx context.Context, req *types.BuildRequest) (*ty
|
||||
return bm.nixpacksBuilder.Build(ctx, req)
|
||||
case BuildTypeDockerfile:
|
||||
return bm.dockerfileBuilder.Build(ctx, req)
|
||||
case BuildTypeStatic:
|
||||
return bm.staticBuilder.Build(ctx, req)
|
||||
case BuildTypePrebuilt:
|
||||
return bm.buildPrebuilt(ctx, req)
|
||||
default:
|
||||
@@ -87,6 +94,75 @@ func (bm *BuildManager) Build(ctx context.Context, req *types.BuildRequest) (*ty
|
||||
}
|
||||
}
|
||||
|
||||
// PackageContext produces the same build-context tar Build would hand the
|
||||
// local daemon, without building it. Node agents download the tar and run
|
||||
// docker build themselves — the Dockerfile lands at the tar root.
|
||||
func (bm *BuildManager) PackageContext(ctx context.Context, req *types.BuildRequest) (io.ReadCloser, error) {
|
||||
if req.BuildType == "" {
|
||||
detectedType, err := bm.DetectBuildType(ctx, req.SourcePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to detect build type: %w", err)
|
||||
}
|
||||
req.BuildType = string(detectedType)
|
||||
}
|
||||
|
||||
var dockerfileContent string
|
||||
switch BuildType(req.BuildType) {
|
||||
case BuildTypeDockerfile:
|
||||
name, err := bm.dockerfileBuilder.DetectDockerfile(ctx, req.SourcePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to detect Dockerfile: %w", err)
|
||||
}
|
||||
path := filepath.Join(req.SourcePath, name)
|
||||
if optimized, err := bm.dockerfileBuilder.OptimizeDockerfile(ctx, path); err == nil {
|
||||
path = optimized
|
||||
}
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read Dockerfile: %w", err)
|
||||
}
|
||||
dockerfileContent = string(data)
|
||||
case BuildTypeRailpack:
|
||||
if err := bm.railpackBuilder.DetectRailpack(ctx, req.SourcePath); err != nil {
|
||||
return nil, fmt.Errorf("Railpack cannot build this project: %w", err)
|
||||
}
|
||||
content, err := bm.railpackBuilder.generateDockerfile(ctx, req.SourcePath, &RailpackConfig{
|
||||
BuildCmd: req.BuildCommand,
|
||||
StartCmd: req.StartCommand,
|
||||
Env: req.Environment,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate Dockerfile with Railpack: %w", err)
|
||||
}
|
||||
dockerfileContent = content
|
||||
case BuildTypeNixpacks:
|
||||
plan, err := bm.nixpacksBuilder.GeneratePlan(ctx, req.SourcePath, &NixpacksConfig{
|
||||
BuildCmd: req.BuildCommand,
|
||||
StartCmd: req.StartCommand,
|
||||
Env: req.Environment,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate build plan: %w", err)
|
||||
}
|
||||
path, err := bm.nixpacksBuilder.generateDockerfile(plan)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate Dockerfile: %w", err)
|
||||
}
|
||||
defer os.Remove(path)
|
||||
data, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read generated Dockerfile: %w", err)
|
||||
}
|
||||
dockerfileContent = string(data)
|
||||
case BuildTypeStatic:
|
||||
dockerfileContent = staticDockerfile
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported build type for remote packaging: %s", req.BuildType)
|
||||
}
|
||||
|
||||
return createBuildContext(req.SourcePath, dockerfileContent)
|
||||
}
|
||||
|
||||
// buildPrebuilt handles prebuilt image deployments
|
||||
func (bm *BuildManager) buildPrebuilt(ctx context.Context, req *types.BuildRequest) (*types.BuildResponse, error) {
|
||||
if req.PrebuiltImage == "" {
|
||||
@@ -95,7 +171,7 @@ func (bm *BuildManager) buildPrebuilt(ctx context.Context, req *types.BuildReque
|
||||
|
||||
// Pull the prebuilt image — must drain the pull stream or the daemon
|
||||
// aborts it and the tag below races a missing image.
|
||||
auth := registry.AuthConfig{}
|
||||
auth := registry.AuthConfig{Username: req.PullUsername, Password: req.PullPassword}
|
||||
if err := bm.dockerClient.PullImageWait(ctx, req.PrebuiltImage, auth); err != nil {
|
||||
return nil, fmt.Errorf("failed to pull prebuilt image: %w", err)
|
||||
}
|
||||
|
||||
@@ -476,17 +476,22 @@ func (n *NixpacksBuilder) Build(ctx context.Context, req *types.BuildRequest) (*
|
||||
}
|
||||
|
||||
// Create temporary Dockerfile
|
||||
dockerfile, err := n.generateDockerfile(plan)
|
||||
dockerfilePath, err := n.generateDockerfile(plan)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to generate Dockerfile: %w", err)
|
||||
}
|
||||
defer os.Remove(dockerfile)
|
||||
defer os.Remove(dockerfilePath)
|
||||
|
||||
// Build Docker image
|
||||
imageName := fmt.Sprintf("%s:%s", req.ImageName, req.ImageTag)
|
||||
|
||||
// Create build context tar
|
||||
buildCtx, err := createBuildContext(req.SourcePath, dockerfile)
|
||||
// Create build context tar — createBuildContext takes the Dockerfile
|
||||
// contents, not its path.
|
||||
dockerfileData, err := os.ReadFile(dockerfilePath)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read generated Dockerfile: %w", err)
|
||||
}
|
||||
buildCtx, err := createBuildContext(req.SourcePath, string(dockerfileData))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create build context: %w", err)
|
||||
}
|
||||
@@ -498,7 +503,8 @@ func (n *NixpacksBuilder) Build(ctx context.Context, req *types.BuildRequest) (*
|
||||
BuildArgs: map[string]*string{
|
||||
"BUILDKIT_INLINE_CACHE": strPtr("1"),
|
||||
},
|
||||
Remove: true,
|
||||
NoCache: req.NoCache,
|
||||
Remove: true,
|
||||
}
|
||||
|
||||
_, err = n.dockerClient.BuildImage(ctx, buildCtx, buildOptions)
|
||||
|
||||
@@ -156,6 +156,7 @@ func (rb *RailpackBuilder) Build(ctx context.Context, req *types.BuildRequest) (
|
||||
Tags: []string{imageName},
|
||||
BuildArgs: buildArgs,
|
||||
Labels: req.Labels,
|
||||
NoCache: req.NoCache,
|
||||
Remove: true,
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,95 @@
|
||||
package build
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
|
||||
"containr/internal/docker"
|
||||
"containr/internal/types"
|
||||
)
|
||||
|
||||
// StaticBuilder produces an nginx-served image from a repo's built assets.
|
||||
// STATIC_BUILD_CMD runs inside a node build stage; STATIC_DIR names the
|
||||
// output directory copied into the nginx image. Both are overridable via
|
||||
// build args so repos with non-node toolchains can swap the build image too.
|
||||
type StaticBuilder struct {
|
||||
dockerClient *docker.Client
|
||||
}
|
||||
|
||||
func NewStaticBuilder(dockerClient *docker.Client) *StaticBuilder {
|
||||
return &StaticBuilder{dockerClient: dockerClient}
|
||||
}
|
||||
|
||||
const staticDockerfile = `ARG BUILD_IMAGE=node:20-alpine
|
||||
FROM ${BUILD_IMAGE} AS assets
|
||||
WORKDIR /app
|
||||
COPY . .
|
||||
ARG STATIC_BUILD_CMD
|
||||
RUN sh -c "${STATIC_BUILD_CMD}"
|
||||
|
||||
FROM nginx:alpine
|
||||
ARG STATIC_DIR=dist
|
||||
COPY --from=assets /app/${STATIC_DIR} /usr/share/nginx/html
|
||||
EXPOSE 80
|
||||
CMD ["nginx", "-g", "daemon off;"]
|
||||
`
|
||||
|
||||
func (s *StaticBuilder) Build(ctx context.Context, req *types.BuildRequest) (*types.BuildResponse, error) {
|
||||
buildCmd := req.BuildCommand
|
||||
if buildCmd == "" {
|
||||
buildCmd = "npm ci && npm run build"
|
||||
}
|
||||
staticDir := req.BuildArgs["STATIC_DIR"]
|
||||
if staticDir == "" {
|
||||
staticDir = "dist"
|
||||
}
|
||||
|
||||
buildArgs := map[string]*string{}
|
||||
for k, v := range req.BuildArgs {
|
||||
v := v
|
||||
buildArgs[k] = &v
|
||||
}
|
||||
for k, v := range req.Environment {
|
||||
v := v
|
||||
buildArgs[k] = &v
|
||||
}
|
||||
buildArgs["STATIC_BUILD_CMD"] = &buildCmd
|
||||
buildArgs["STATIC_DIR"] = &staticDir
|
||||
|
||||
imageName := fmt.Sprintf("%s:%s", req.ImageName, req.ImageTag)
|
||||
buildCtx, err := createBuildContext(req.SourcePath, staticDockerfile)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create build context: %w", err)
|
||||
}
|
||||
defer buildCtx.Close()
|
||||
|
||||
if _, err := s.dockerClient.BuildImage(ctx, buildCtx, docker.BuildOptions{
|
||||
Dockerfile: "Dockerfile",
|
||||
Tags: []string{imageName},
|
||||
BuildArgs: buildArgs,
|
||||
NoCache: req.NoCache,
|
||||
Remove: true,
|
||||
}); err != nil {
|
||||
return nil, fmt.Errorf("failed to build static image: %w", err)
|
||||
}
|
||||
|
||||
if req.RegistryURL != "" {
|
||||
full := fmt.Sprintf("%s/%s", req.RegistryURL, imageName)
|
||||
if err := s.dockerClient.PushImage(ctx, imageName, req.RegistryURL); err != nil {
|
||||
return nil, fmt.Errorf("failed to push image: %w", err)
|
||||
}
|
||||
imageName = full
|
||||
}
|
||||
|
||||
imageInfo, err := s.dockerClient.GetImageInfo(ctx, imageName)
|
||||
if err != nil {
|
||||
imageInfo = &docker.ImageInfo{Size: 0}
|
||||
}
|
||||
|
||||
return &types.BuildResponse{
|
||||
ImageName: imageName,
|
||||
ImageTag: req.ImageTag,
|
||||
Size: imageInfo.Size,
|
||||
Digest: imageInfo.Digest,
|
||||
}, nil
|
||||
}
|
||||
@@ -0,0 +1,67 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
activitySeverity string
|
||||
activityCategory string
|
||||
activityResource string
|
||||
activityProject string
|
||||
activityLimit int
|
||||
)
|
||||
|
||||
// ActivityCmd shows the enriched activity feed (audit events with
|
||||
// severity/category/label).
|
||||
var ActivityCmd = &cobra.Command{
|
||||
Use: "activity",
|
||||
Short: "Activity feed — enriched audit events",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
params := []string{fmt.Sprintf("limit=%d", activityLimit)}
|
||||
if activitySeverity != "" {
|
||||
params = append(params, "severity="+activitySeverity)
|
||||
}
|
||||
if activityCategory != "" {
|
||||
params = append(params, "category="+activityCategory)
|
||||
}
|
||||
if activityResource != "" {
|
||||
params = append(params, "resource="+activityResource)
|
||||
}
|
||||
if activityProject != "" {
|
||||
params = append(params, "project_id="+activityProject)
|
||||
}
|
||||
data, err := c.Do("GET", "/activity?"+strings.Join(params, "&"), nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, err := unwrapList(data, "activity")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, e := range items {
|
||||
rows = append(rows, []string{
|
||||
str(e, "severity"), str(e, "category"), str(e, "label"),
|
||||
str(e, "user_email"), relTime(e, "created_at"),
|
||||
})
|
||||
}
|
||||
printRows(data, []string{"SEVERITY", "CATEGORY", "EVENT", "ACTOR", "WHEN"}, rows)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
ActivityCmd.Flags().StringVar(&activitySeverity, "severity", "", "filter: info|success|warning|error")
|
||||
ActivityCmd.Flags().StringVar(&activityCategory, "category", "", "filter by category")
|
||||
ActivityCmd.Flags().StringVar(&activityResource, "resource", "", "filter by resource type")
|
||||
ActivityCmd.Flags().StringVar(&activityProject, "project", "", "filter by project id")
|
||||
ActivityCmd.Flags().IntVar(&activityLimit, "limit", 50, "max events")
|
||||
}
|
||||
@@ -0,0 +1,445 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// AdminCmd groups platform-admin operations.
|
||||
var AdminCmd = &cobra.Command{
|
||||
Use: "admin",
|
||||
Short: "Platform administration (admin scope required)",
|
||||
}
|
||||
|
||||
var adminOverviewCmd = &cobra.Command{
|
||||
Use: "overview",
|
||||
Short: "Platform overview",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/admin/overview", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var adminUsersCmd = &cobra.Command{
|
||||
Use: "users",
|
||||
Short: "List all users",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/admin/users", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, err := unwrapList(data, "users")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, u := range items {
|
||||
rows = append(rows, []string{
|
||||
str(u, "id"), str(u, "email"), str(u, "name"), str(u, "is_admin"), relTime(u, "created_at"),
|
||||
})
|
||||
}
|
||||
printRows(data, []string{"ID", "EMAIL", "NAME", "ADMIN", "CREATED"}, rows)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var adminImpersonateCmd = &cobra.Command{
|
||||
Use: "impersonate <user-id>",
|
||||
Short: "Mint a 15-minute token acting as a user (audit-logged)",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("POST", "/admin/users/"+args[0]+"/impersonate", map[string]interface{}{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
fmt.Fprintln(cmd.ErrOrStderr(), "\nUse it with: CONTAINR_TOKEN=<token> containr <command>")
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var adminGitHubAppCmd = &cobra.Command{
|
||||
Use: "github-app",
|
||||
Short: "GitHub App self-provisioning",
|
||||
}
|
||||
|
||||
var adminGitHubAppStatusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "Show GitHub App provisioning status",
|
||||
RunE: simpleGet("/admin/git/github-app"),
|
||||
}
|
||||
|
||||
var adminGitHubAppManifestCmd = &cobra.Command{
|
||||
Use: "manifest",
|
||||
Short: "Build the manifest to create the instance GitHub App",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req := map[string]interface{}{"base_url": ghAppBaseURL}
|
||||
if ghAppOrg != "" {
|
||||
req["organization"] = ghAppOrg
|
||||
}
|
||||
if ghAppName != "" {
|
||||
req["name"] = ghAppName
|
||||
}
|
||||
data, err := c.Do("POST", "/admin/git/github-app/manifest", req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var adminGitHubAppConvertCmd = &cobra.Command{
|
||||
Use: "convert <code>",
|
||||
Short: "Exchange the manifest callback code for app credentials",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("POST", "/admin/git/github-app/convert", map[string]interface{}{"code": args[0]})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var (
|
||||
ghAppBaseURL string
|
||||
ghAppOrg string
|
||||
ghAppName string
|
||||
)
|
||||
|
||||
var adminSettingsCmd = &cobra.Command{
|
||||
Use: "settings",
|
||||
Short: "Show platform settings",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/settings", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var adminAuditCmd = &cobra.Command{
|
||||
Use: "audit-logs",
|
||||
Short: "List audit log entries",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/audit-logs", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// GatewayCmd manages the API gateway (admin).
|
||||
var GatewayCmd = &cobra.Command{
|
||||
Use: "gateway",
|
||||
Short: "Manage the API gateway (admin)",
|
||||
}
|
||||
|
||||
var gatewayServicesCmd = &cobra.Command{
|
||||
Use: "services",
|
||||
Short: "List gateway services",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/gateway/services", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var gatewayKeysCmd = &cobra.Command{
|
||||
Use: "keys",
|
||||
Short: "List gateway API keys",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/gateway/keys", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var gatewayAnalyticsCmd = &cobra.Command{
|
||||
Use: "analytics",
|
||||
Short: "Gateway traffic analytics",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
kind, _ := cmd.Flags().GetString("type")
|
||||
data, err := c.Do("GET", "/gateway/analytics/"+kind, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// HACmd manages high availability.
|
||||
var HACmd = &cobra.Command{
|
||||
Use: "ha",
|
||||
Short: "High availability controls",
|
||||
}
|
||||
|
||||
var haStatusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "Show HA status",
|
||||
RunE: simpleGet("/ha/status"),
|
||||
}
|
||||
|
||||
var haAlertsCmd = &cobra.Command{
|
||||
Use: "alerts",
|
||||
Short: "List active HA alerts",
|
||||
RunE: simpleGet("/ha/alerts/active"),
|
||||
}
|
||||
|
||||
var haHealthCmd = &cobra.Command{
|
||||
Use: "health",
|
||||
Short: "List health check results",
|
||||
RunE: simpleGet("/ha/health/results"),
|
||||
}
|
||||
|
||||
var haPoliciesCmd = &cobra.Command{
|
||||
Use: "policies",
|
||||
Short: "List failover policies",
|
||||
RunE: simpleGet("/ha/failover/policies"),
|
||||
}
|
||||
|
||||
var haEnableCmd = &cobra.Command{
|
||||
Use: "enable",
|
||||
Short: "Enable the HA manager",
|
||||
RunE: simplePost("/ha/enable"),
|
||||
}
|
||||
|
||||
var haDisableCmd = &cobra.Command{
|
||||
Use: "disable",
|
||||
Short: "Disable the HA manager",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !Confirm("Disable HA? Services will no longer be failed over.") {
|
||||
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||
}
|
||||
return simplePost("/ha/disable")(cmd, args)
|
||||
},
|
||||
}
|
||||
|
||||
var haFailoverCmd = &cobra.Command{
|
||||
Use: "failover",
|
||||
Short: "Trigger a manual failover",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !Confirm("Trigger a platform failover now? Services may be rescheduled.") {
|
||||
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||
}
|
||||
return simplePost("/ha/failover")(cmd, args)
|
||||
},
|
||||
}
|
||||
|
||||
// ScalingCmd manages autoscaling.
|
||||
var ScalingCmd = &cobra.Command{
|
||||
Use: "scaling",
|
||||
Short: "Autoscaling controls",
|
||||
}
|
||||
|
||||
var scalingStatusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "Show autoscaler status",
|
||||
RunE: simpleGet("/scaling/status"),
|
||||
}
|
||||
|
||||
var scalingPoliciesCmd = &cobra.Command{
|
||||
Use: "policies",
|
||||
Short: "List scaling policies",
|
||||
RunE: simpleGet("/scaling/policies"),
|
||||
}
|
||||
|
||||
var scalingServicesCmd = &cobra.Command{
|
||||
Use: "services",
|
||||
Short: "Show per-service scaling state",
|
||||
RunE: simpleGet("/scaling/services"),
|
||||
}
|
||||
|
||||
var scalingScaleCmd = &cobra.Command{
|
||||
Use: "scale <service-id> <replicas>",
|
||||
Short: "Manually scale a service",
|
||||
Args: cobra.ExactArgs(2),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
var replicas int
|
||||
fmt.Sscanf(args[1], "%d", &replicas)
|
||||
data, err := c.Do("POST", "/scaling/services/"+args[0]+"/scale", map[string]int{"replicas": replicas})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// SecurityCmd manages security scans and findings.
|
||||
var SecurityCmd = &cobra.Command{
|
||||
Use: "security",
|
||||
Short: "Security scans and vulnerabilities",
|
||||
}
|
||||
|
||||
var securityScanCmd = &cobra.Command{
|
||||
Use: "scan <project-id>",
|
||||
Short: "Run a security scan",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
scanType, _ := cmd.Flags().GetString("type")
|
||||
serviceID, _ := cmd.Flags().GetString("service")
|
||||
body := map[string]interface{}{"project_id": args[0], "scan_type": scanType}
|
||||
if serviceID != "" {
|
||||
body["service_id"] = serviceID
|
||||
}
|
||||
data, err := c.Do("POST", "/security/scans", body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var securityVulnsCmd = &cobra.Command{
|
||||
Use: "vulnerabilities <project-id>",
|
||||
Short: "List a project's vulnerabilities",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/projects/"+args[0]+"/vulnerabilities", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var securityMetricsCmd = &cobra.Command{
|
||||
Use: "metrics <project-id>",
|
||||
Short: "Show a project's security metrics",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/projects/"+args[0]+"/security/metrics", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
// simpleGet/simplePost cover the many read/trigger endpoints whose full
|
||||
// JSON response is the useful output.
|
||||
func simpleGet(path string) func(*cobra.Command, []string) error {
|
||||
return func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", path, nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func simplePost(path string) func(*cobra.Command, []string) error {
|
||||
return func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("POST", path, map[string]interface{}{})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
gatewayAnalyticsCmd.Flags().String("type", "traffic", "traffic|ops")
|
||||
securityScanCmd.Flags().String("type", "configuration", "dependency|configuration|comprehensive")
|
||||
securityScanCmd.Flags().String("service", "", "limit scan to one service")
|
||||
|
||||
adminGitHubAppManifestCmd.Flags().StringVar(&ghAppBaseURL, "base-url", "", "public base URL of this instance (required)")
|
||||
adminGitHubAppManifestCmd.Flags().StringVar(&ghAppOrg, "org", "", "create the app under this organization")
|
||||
adminGitHubAppManifestCmd.Flags().StringVar(&ghAppName, "name", "", "app name (defaults to product name + date)")
|
||||
adminGitHubAppManifestCmd.MarkFlagRequired("base-url")
|
||||
|
||||
adminGitHubAppCmd.AddCommand(adminGitHubAppStatusCmd, adminGitHubAppManifestCmd, adminGitHubAppConvertCmd)
|
||||
AdminCmd.AddCommand(adminOverviewCmd, adminUsersCmd, adminSettingsCmd, adminAuditCmd, adminImpersonateCmd, adminGitHubAppCmd)
|
||||
GatewayCmd.AddCommand(gatewayServicesCmd, gatewayKeysCmd, gatewayAnalyticsCmd)
|
||||
HACmd.AddCommand(haStatusCmd, haAlertsCmd, haHealthCmd, haPoliciesCmd, haEnableCmd, haDisableCmd, haFailoverCmd)
|
||||
ScalingCmd.AddCommand(scalingStatusCmd, scalingPoliciesCmd, scalingServicesCmd, scalingScaleCmd)
|
||||
SecurityCmd.AddCommand(securityScanCmd, securityVulnsCmd, securityMetricsCmd)
|
||||
}
|
||||
@@ -2,139 +2,166 @@ package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
"os"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/spf13/viper"
|
||||
"golang.org/x/term"
|
||||
)
|
||||
|
||||
// AuthCmd represents the auth command
|
||||
// AuthCmd groups authentication commands.
|
||||
var AuthCmd = &cobra.Command{
|
||||
Use: "auth",
|
||||
Short: "Authenticate with Containr API",
|
||||
Long: `Manage authentication with the Containr API.
|
||||
You can login, logout, and check your current authentication status.`,
|
||||
Short: "Authenticate with a Containr instance",
|
||||
Long: `Login with a personal access token (cnp_...) created under
|
||||
Settings → Personal Access Tokens, check status, or switch profiles.`,
|
||||
}
|
||||
|
||||
// loginCmd represents the login command
|
||||
var loginCmd = &cobra.Command{
|
||||
Use: "login [token]",
|
||||
Short: "Login to Containr",
|
||||
Long: `Login to Containr using your API token.
|
||||
You can get your token from the Containr web interface.`,
|
||||
Short: "Store a personal access token",
|
||||
Long: `Login with a cnp_ token. Reads interactively when no argument is
|
||||
given. The token is verified against the API before it is stored.`,
|
||||
Args: cobra.MaximumNArgs(1),
|
||||
RunE: runLogin,
|
||||
}
|
||||
|
||||
// logoutCmd represents the logout command
|
||||
var logoutCmd = &cobra.Command{
|
||||
Use: "logout",
|
||||
Short: "Logout from Containr",
|
||||
Long: `Remove stored authentication credentials.`,
|
||||
Short: "Remove the stored token",
|
||||
RunE: runLogout,
|
||||
}
|
||||
|
||||
// statusCmd represents the status command
|
||||
var statusCmd = &cobra.Command{
|
||||
Use: "status",
|
||||
Short: "Check authentication status",
|
||||
Long: `Check if you are currently authenticated with Containr.`,
|
||||
Short: "Show the active profile and verify the token",
|
||||
RunE: runStatus,
|
||||
}
|
||||
|
||||
func init() {
|
||||
AuthCmd.AddCommand(loginCmd)
|
||||
AuthCmd.AddCommand(logoutCmd)
|
||||
AuthCmd.AddCommand(statusCmd)
|
||||
loginCmd.Flags().String("url", "", "API URL to store in the profile (e.g. https://containr.example.com)")
|
||||
loginCmd.Flags().String("name", "", "profile name to save under (default: active profile)")
|
||||
AuthCmd.AddCommand(loginCmd, logoutCmd, statusCmd)
|
||||
}
|
||||
|
||||
type profileResponse struct {
|
||||
ID string `json:"id"`
|
||||
Email string `json:"email"`
|
||||
Name string `json:"name"`
|
||||
IsAdmin bool `json:"is_admin"`
|
||||
}
|
||||
|
||||
func runLogin(cmd *cobra.Command, args []string) error {
|
||||
var token string
|
||||
|
||||
if len(args) > 0 {
|
||||
token = args[0]
|
||||
} else {
|
||||
// Prompt for token
|
||||
fmt.Print("Enter your Containr API token: ")
|
||||
fmt.Scanln(&token)
|
||||
} else if isTerminal() {
|
||||
fmt.Fprint(os.Stderr, "Token: ")
|
||||
raw, err := term.ReadPassword(int(os.Stdin.Fd()))
|
||||
fmt.Fprintln(os.Stderr)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
token = string(raw)
|
||||
}
|
||||
|
||||
if token == "" {
|
||||
return fmt.Errorf("token is required")
|
||||
return &APIError{Message: "token is required — pass it as an argument or pipe it via stdin", ExitCode: ExitValidation}
|
||||
}
|
||||
|
||||
// Store token in config
|
||||
viper.Set("token", token)
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
return fmt.Errorf("failed to save token: %w", err)
|
||||
// Verify before saving so a mistyped token fails loudly.
|
||||
urlFlag, _ := cmd.Flags().GetString("url")
|
||||
probe := &Client{BaseURL: ResolveAPIURL(), Token: token}
|
||||
if urlFlag != "" {
|
||||
probe.BaseURL = normalizeBase(urlFlag)
|
||||
}
|
||||
var profile profileResponse
|
||||
if err := probe.DoJSON("GET", "/user/profile", nil, &profile); err != nil {
|
||||
return fmt.Errorf("token verification failed: %w", err)
|
||||
}
|
||||
|
||||
fmt.Println("✓ Successfully logged in to Containr")
|
||||
name, _ := cmd.Flags().GetString("name")
|
||||
if name == "" {
|
||||
name = ActiveProfile()
|
||||
}
|
||||
if err := SaveProfile(name, probe.BaseURL, token); err != nil {
|
||||
return fmt.Errorf("failed to save profile: %w", err)
|
||||
}
|
||||
|
||||
if JSONMode() {
|
||||
return PrintJSON(map[string]interface{}{
|
||||
"profile": name, "api_url": probe.BaseURL,
|
||||
"user": profile,
|
||||
})
|
||||
}
|
||||
fmt.Printf("Logged in as %s (%s) — profile %q → %s\n", profile.Name, profile.Email, name, probe.BaseURL)
|
||||
return nil
|
||||
}
|
||||
|
||||
func runLogout(cmd *cobra.Command, args []string) error {
|
||||
// Remove token from config
|
||||
viper.Set("token", "")
|
||||
if err := viper.WriteConfig(); err != nil {
|
||||
return fmt.Errorf("failed to remove token: %w", err)
|
||||
name := ActiveProfile()
|
||||
if err := ClearProfileToken(name); err != nil {
|
||||
return fmt.Errorf("failed to update config: %w", err)
|
||||
}
|
||||
if !JSONMode() {
|
||||
fmt.Printf("Removed token from profile %q\n", name)
|
||||
} else {
|
||||
return PrintJSON(map[string]string{"status": "ok", "profile": name})
|
||||
}
|
||||
|
||||
fmt.Println("✓ Successfully logged out from Containr")
|
||||
return nil
|
||||
}
|
||||
|
||||
func runStatus(cmd *cobra.Command, args []string) error {
|
||||
token := viper.GetString("token")
|
||||
if token == "" {
|
||||
fmt.Println("❌ Not authenticated")
|
||||
fmt.Println("Run 'containr auth login <token>' to authenticate")
|
||||
client := NewClient()
|
||||
profile := ActiveProfile()
|
||||
|
||||
if JSONMode() {
|
||||
out := map[string]interface{}{
|
||||
"profile": profile,
|
||||
"api_url": client.BaseURL,
|
||||
}
|
||||
if client.Token != "" {
|
||||
var p profileResponse
|
||||
if err := client.DoJSON("GET", "/user/profile", nil, &p); err == nil {
|
||||
out["user"] = p
|
||||
out["authenticated"] = true
|
||||
} else {
|
||||
out["authenticated"] = false
|
||||
}
|
||||
} else {
|
||||
out["authenticated"] = false
|
||||
}
|
||||
return PrintJSON(out)
|
||||
}
|
||||
|
||||
fmt.Printf("Profile: %s\n", profile)
|
||||
fmt.Printf("API URL: %s\n", client.BaseURL)
|
||||
if client.Token == "" {
|
||||
fmt.Println("Token: not set — run `containr auth login <token>`")
|
||||
return nil
|
||||
}
|
||||
|
||||
fmt.Println("✓ Authenticated with Containr")
|
||||
apiURL := buildAPIURL("/user/profile")
|
||||
fmt.Printf("API URL: %s\n", strings.TrimSuffix(buildAPIURL(""), "/"))
|
||||
|
||||
req, err := http.NewRequest(http.MethodGet, apiURL, nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to create verification request: %w", err)
|
||||
}
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
|
||||
client := &http.Client{Timeout: 5 * time.Second}
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
fmt.Printf("⚠ Token verification unavailable: %v\n", err)
|
||||
var p profileResponse
|
||||
if err := client.DoJSON("GET", "/user/profile", nil, &p); err != nil {
|
||||
fmt.Printf("Token: present but rejected (%v)\n", err)
|
||||
return nil
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
switch resp.StatusCode {
|
||||
case http.StatusOK:
|
||||
fmt.Println("✓ Token verified against API")
|
||||
case http.StatusUnauthorized, http.StatusForbidden:
|
||||
fmt.Println("❌ Token rejected by API (expired/invalid)")
|
||||
default:
|
||||
fmt.Printf("⚠ Token verification returned unexpected status: %s\n", resp.Status)
|
||||
fmt.Printf("Identity: %s <%s>\n", p.Name, p.Email)
|
||||
if p.IsAdmin {
|
||||
fmt.Println("Role: platform admin")
|
||||
}
|
||||
|
||||
var bannersResp struct {
|
||||
Banners []struct {
|
||||
Title string `json:"title"`
|
||||
Body string `json:"body"`
|
||||
Level string `json:"level"`
|
||||
} `json:"banners"`
|
||||
}
|
||||
if err := client.DoJSON("GET", "/banners/active", nil, &bannersResp); err == nil {
|
||||
for _, b := range bannersResp.Banners {
|
||||
fmt.Printf("[%s] %s\n", b.Level, b.Title)
|
||||
if b.Body != "" {
|
||||
fmt.Printf(" %s\n", b.Body)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func buildAPIURL(endpoint string) string {
|
||||
baseURL := viper.GetString("api-url")
|
||||
if baseURL == "" {
|
||||
baseURL = "http://localhost:8080/api/v1"
|
||||
}
|
||||
baseURL = strings.TrimSuffix(baseURL, "/")
|
||||
if endpoint == "" {
|
||||
return baseURL
|
||||
}
|
||||
if !strings.HasPrefix(endpoint, "/") {
|
||||
endpoint = "/" + endpoint
|
||||
}
|
||||
return baseURL + endpoint
|
||||
}
|
||||
@@ -6,16 +6,52 @@ import (
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
func TestBuildAPIURLDefaultsAndTrimming(t *testing.T) {
|
||||
func TestResolveAPIURLDefaultsAndTrimming(t *testing.T) {
|
||||
viper.Set("api-url", "")
|
||||
got := buildAPIURL("/user/profile")
|
||||
if got != "http://localhost:8080/api/v1/user/profile" {
|
||||
viper.Set("current_profile", "")
|
||||
viper.Set("profiles.default.api_url", "")
|
||||
if got := ResolveAPIURL(); got != "http://localhost:8080/api/v1" {
|
||||
t.Fatalf("unexpected default api url: %s", got)
|
||||
}
|
||||
|
||||
viper.Set("api-url", "https://api.example.com/api/v1/")
|
||||
got = buildAPIURL("user/profile")
|
||||
if got != "https://api.example.com/api/v1/user/profile" {
|
||||
if got := ResolveAPIURL(); got != "https://api.example.com/api/v1" {
|
||||
t.Fatalf("unexpected trimmed api url: %s", got)
|
||||
}
|
||||
|
||||
// Bare hosts gain the /api/v1 suffix.
|
||||
viper.Set("api-url", "https://containr.example.com")
|
||||
if got := ResolveAPIURL(); got != "https://containr.example.com/api/v1" {
|
||||
t.Fatalf("unexpected suffixed api url: %s", got)
|
||||
}
|
||||
viper.Set("api-url", "")
|
||||
}
|
||||
|
||||
func TestResolveTokenProfileAndEnv(t *testing.T) {
|
||||
viper.Set("token", "")
|
||||
viper.Set("current_profile", "staging")
|
||||
viper.Set("profiles.staging.token", "cnp_staging")
|
||||
if got := ResolveToken(); got != "cnp_staging" {
|
||||
t.Fatalf("expected profile token, got %s", got)
|
||||
}
|
||||
t.Setenv("CONTAINR_TOKEN", "cnp_env")
|
||||
if got := ResolveToken(); got != "cnp_env" {
|
||||
t.Fatalf("env should win over profile, got %s", got)
|
||||
}
|
||||
viper.Set("token", "cnp_flag")
|
||||
if got := ResolveToken(); got != "cnp_flag" {
|
||||
t.Fatalf("flag should win over env, got %s", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestExitCodeMapping(t *testing.T) {
|
||||
if got := exitCodeFor(401); got != ExitAuth {
|
||||
t.Fatalf("401 → %d, want %d", got, ExitAuth)
|
||||
}
|
||||
if got := exitCodeFor(404); got != ExitNotFound {
|
||||
t.Fatalf("404 → %d, want %d", got, ExitNotFound)
|
||||
}
|
||||
if got := exitCodeFor(400); got != ExitValidation {
|
||||
t.Fatalf("400 → %d, want %d", got, ExitValidation)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
// BackupTargetsCmd manages S3-compatible backup destinations.
|
||||
var BackupTargetsCmd = &cobra.Command{
|
||||
Use: "backup-targets",
|
||||
Aliases: []string{"backup-target", "targets"},
|
||||
Short: "Manage S3-compatible backup targets",
|
||||
}
|
||||
|
||||
var btListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List backup targets",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/backup-targets", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, err := unwrapList(data, "backup_targets")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, t := range items {
|
||||
creds := "no"
|
||||
if str(t, "has_credentials") == "true" {
|
||||
creds = "yes"
|
||||
}
|
||||
rows = append(rows, []string{str(t, "id"), str(t, "name"), str(t, "endpoint"), str(t, "bucket"), creds})
|
||||
}
|
||||
printRows(data, []string{"ID", "NAME", "ENDPOINT", "BUCKET", "CREDS"}, rows)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var btAddCmd = &cobra.Command{
|
||||
Use: "add <name>",
|
||||
Short: "Register an S3-compatible backup target (probed before storing)",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body := map[string]interface{}{"name": args[0]}
|
||||
for _, f := range []string{"endpoint", "bucket", "region", "prefix", "access-key", "secret-key"} {
|
||||
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||
key := f
|
||||
switch f {
|
||||
case "access-key":
|
||||
key = "access_key"
|
||||
case "secret-key":
|
||||
key = "secret_key"
|
||||
}
|
||||
body[key] = v
|
||||
}
|
||||
}
|
||||
if cmd.Flags().Changed("no-tls") {
|
||||
v, _ := cmd.Flags().GetBool("no-tls")
|
||||
body["use_tls"] = !v
|
||||
}
|
||||
data, err := c.Do("POST", "/backup-targets", body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var btUpdateCmd = &cobra.Command{
|
||||
Use: "update <id>",
|
||||
Short: "Update a backup target (re-probed; empty keys keep stored credentials)",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
body := map[string]interface{}{}
|
||||
// endpoint/bucket are required by the API — fall back to stored values.
|
||||
for _, f := range []string{"name", "endpoint", "bucket", "region", "prefix", "access-key", "secret-key"} {
|
||||
if v, _ := cmd.Flags().GetString(f); v != "" {
|
||||
key := f
|
||||
switch f {
|
||||
case "access-key":
|
||||
key = "access_key"
|
||||
case "secret-key":
|
||||
key = "secret_key"
|
||||
}
|
||||
body[key] = v
|
||||
}
|
||||
}
|
||||
if _, hasEndpoint := body["endpoint"]; !hasEndpoint {
|
||||
data, err := c.Do("GET", "/backup-targets", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, err := unwrapList(data, "backup_targets")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
for _, t := range items {
|
||||
if str(t, "id") == args[0] {
|
||||
body["endpoint"] = str(t, "endpoint")
|
||||
if _, ok := body["bucket"]; !ok {
|
||||
body["bucket"] = str(t, "bucket")
|
||||
}
|
||||
if _, ok := body["name"]; !ok {
|
||||
body["name"] = str(t, "name")
|
||||
}
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
if cmd.Flags().Changed("no-tls") {
|
||||
v, _ := cmd.Flags().GetBool("no-tls")
|
||||
body["use_tls"] = !v
|
||||
}
|
||||
data, err := c.Do("PUT", "/backup-targets/"+args[0], body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var btRemoveCmd = &cobra.Command{
|
||||
Use: "remove <id>",
|
||||
Short: "Delete a backup target (fails while databases still point at it)",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if !Confirm(fmt.Sprintf("Delete backup target %s?", args[0])) {
|
||||
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||
}
|
||||
if _, err := c.Do("DELETE", "/backup-targets/"+args[0], nil); err != nil {
|
||||
return err
|
||||
}
|
||||
if JSONMode() {
|
||||
return PrintJSON(map[string]string{"status": "deleted", "id": args[0]})
|
||||
}
|
||||
fmt.Printf("Deleted backup target %s\n", args[0])
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var btTestCmd = &cobra.Command{
|
||||
Use: "test <id>",
|
||||
Short: "Re-probe a backup target's bucket access",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("POST", "/backup-targets/"+args[0]+"/test", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m, _ := unwrapObject(data)
|
||||
if str(m, "ok") == "false" {
|
||||
return &APIError{Message: "probe failed: " + str(m, "error"), ExitCode: ExitError}
|
||||
}
|
||||
if JSONMode() {
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
}
|
||||
fmt.Printf("Target ok (%sms)\n", str(m, "latency_ms"))
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
btAddCmd.Flags().String("endpoint", "", "S3 endpoint host[:port] (e.g. s3.amazonaws.com, garage.local:3900)")
|
||||
btAddCmd.Flags().String("bucket", "", "bucket name")
|
||||
btAddCmd.Flags().String("region", "", "region (optional for most S3-compatible stores)")
|
||||
btAddCmd.Flags().String("prefix", "", "key prefix for archives")
|
||||
btAddCmd.Flags().String("access-key", "", "access key (stored encrypted)")
|
||||
btAddCmd.Flags().String("secret-key", "", "secret key (stored encrypted)")
|
||||
btAddCmd.Flags().Bool("no-tls", false, "use plain HTTP")
|
||||
btUpdateCmd.Flags().String("name", "", "new name")
|
||||
btUpdateCmd.Flags().String("endpoint", "", "S3 endpoint host[:port]")
|
||||
btUpdateCmd.Flags().String("bucket", "", "bucket name")
|
||||
btUpdateCmd.Flags().String("region", "", "region")
|
||||
btUpdateCmd.Flags().String("prefix", "", "key prefix")
|
||||
btUpdateCmd.Flags().String("access-key", "", "access key")
|
||||
btUpdateCmd.Flags().String("secret-key", "", "secret key")
|
||||
btUpdateCmd.Flags().Bool("no-tls", false, "use plain HTTP")
|
||||
BackupTargetsCmd.AddCommand(btListCmd, btAddCmd, btUpdateCmd, btRemoveCmd, btTestCmd)
|
||||
}
|
||||
@@ -0,0 +1,133 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
var (
|
||||
bannerTitle string
|
||||
bannerLevel string
|
||||
bannerBody string
|
||||
bannerActive bool
|
||||
)
|
||||
|
||||
// BannersCmd manages instance-wide announcement banners (admin).
|
||||
var BannersCmd = &cobra.Command{
|
||||
Use: "banners",
|
||||
Short: "Manage announcement banners (admin)",
|
||||
}
|
||||
|
||||
var bannerListCmd = &cobra.Command{
|
||||
Use: "list",
|
||||
Short: "List all banners",
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
data, err := c.Do("GET", "/admin/banners", nil)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
items, err := unwrapList(data, "banners")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
rows := make([][]string, 0, len(items))
|
||||
for _, b := range items {
|
||||
rows = append(rows, []string{
|
||||
str(b, "id"), str(b, "title"), str(b, "level"), str(b, "active"),
|
||||
})
|
||||
}
|
||||
printRows(data, []string{"ID", "TITLE", "LEVEL", "ACTIVE"}, rows)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var bannerCreateCmd = &cobra.Command{
|
||||
Use: "create <title>",
|
||||
Short: "Create a banner (--body --level info|success|warning|error)",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req := map[string]interface{}{"title": args[0]}
|
||||
if cmd.Flags().Changed("body") {
|
||||
req["body"] = bannerBody
|
||||
}
|
||||
if cmd.Flags().Changed("level") {
|
||||
req["level"] = bannerLevel
|
||||
}
|
||||
data, err := c.Do("POST", "/admin/banners", req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var bannerUpdateCmd = &cobra.Command{
|
||||
Use: "update <id>",
|
||||
Short: "Update a banner (--title --body --level --active/--inactive)",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
req := map[string]interface{}{}
|
||||
if cmd.Flags().Changed("title") {
|
||||
req["title"] = bannerTitle
|
||||
}
|
||||
if cmd.Flags().Changed("body") {
|
||||
req["body"] = bannerBody
|
||||
}
|
||||
if cmd.Flags().Changed("level") {
|
||||
req["level"] = bannerLevel
|
||||
}
|
||||
if cmd.Flags().Changed("active") {
|
||||
req["active"] = bannerActive
|
||||
}
|
||||
if len(req) == 0 {
|
||||
return fmt.Errorf("nothing to update — pass --title/--body/--level/--active")
|
||||
}
|
||||
data, err := c.Do("PATCH", "/admin/banners/"+args[0], req)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
PrintRaw(data)
|
||||
return nil
|
||||
},
|
||||
}
|
||||
|
||||
var bannerDeleteCmd = &cobra.Command{
|
||||
Use: "delete <id>",
|
||||
Short: "Delete a banner",
|
||||
Args: cobra.ExactArgs(1),
|
||||
RunE: func(cmd *cobra.Command, args []string) error {
|
||||
if !Confirm("Delete banner " + args[0] + "?") {
|
||||
return &APIError{Message: "aborted (pass --yes to skip confirmation)", ExitCode: ExitError}
|
||||
}
|
||||
c, err := client()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.Do("DELETE", "/admin/banners/"+args[0], nil)
|
||||
return err
|
||||
},
|
||||
}
|
||||
|
||||
func init() {
|
||||
bannerCreateCmd.Flags().StringVar(&bannerBody, "body", "", "banner body text")
|
||||
bannerCreateCmd.Flags().StringVar(&bannerLevel, "level", "info", "info|success|warning|error")
|
||||
bannerUpdateCmd.Flags().StringVar(&bannerTitle, "title", "", "new title")
|
||||
bannerUpdateCmd.Flags().StringVar(&bannerBody, "body", "", "new body")
|
||||
bannerUpdateCmd.Flags().StringVar(&bannerLevel, "level", "", "info|success|warning|error")
|
||||
bannerUpdateCmd.Flags().BoolVar(&bannerActive, "active", true, "set banner active/inactive")
|
||||
BannersCmd.AddCommand(bannerListCmd, bannerCreateCmd, bannerUpdateCmd, bannerDeleteCmd)
|
||||
}
|
||||
@@ -0,0 +1,211 @@
|
||||
package commands
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/viper"
|
||||
)
|
||||
|
||||
// Exit codes — stable for scripting and agent use.
|
||||
const (
|
||||
ExitOK = 0
|
||||
ExitError = 1
|
||||
ExitAuth = 2
|
||||
ExitNotFound = 3
|
||||
ExitValidation = 4
|
||||
ExitUnavailable = 5
|
||||
)
|
||||
|
||||
// APIError is a non-2xx response from the Containr API. Code carries the
|
||||
// stable exit code so `main` can map errors to exit statuses.
|
||||
type APIError struct {
|
||||
Status int
|
||||
Message string
|
||||
ExitCode int
|
||||
}
|
||||
|
||||
func (e *APIError) Error() string {
|
||||
if e.Message != "" {
|
||||
return e.Message
|
||||
}
|
||||
return fmt.Sprintf("request failed with status %d", e.Status)
|
||||
}
|
||||
|
||||
func exitCodeFor(status int) int {
|
||||
switch status {
|
||||
case http.StatusUnauthorized, http.StatusForbidden:
|
||||
return ExitAuth
|
||||
case http.StatusNotFound:
|
||||
return ExitNotFound
|
||||
case http.StatusBadRequest, http.StatusConflict, http.StatusUnprocessableEntity:
|
||||
return ExitValidation
|
||||
default:
|
||||
return ExitError
|
||||
}
|
||||
}
|
||||
|
||||
// Client is a thin authenticated HTTP client for the Containr API. All CLI
|
||||
// commands go through it — no command builds its own request.
|
||||
type Client struct {
|
||||
BaseURL string
|
||||
Token string
|
||||
HTTPClient *http.Client
|
||||
}
|
||||
|
||||
// NewClient resolves the active profile (flag > env > current_profile) and
|
||||
// returns a client ready to call the API. Call RequireAuth() for commands
|
||||
// that need a token.
|
||||
func NewClient() *Client {
|
||||
return &Client{
|
||||
BaseURL: ResolveAPIURL(),
|
||||
Token: ResolveToken(),
|
||||
HTTPClient: &http.Client{Timeout: 60 * time.Second},
|
||||
}
|
||||
}
|
||||
|
||||
// RequireAuth returns an error suitable for the CLI when no token is set.
|
||||
func (c *Client) RequireAuth() error {
|
||||
if c.Token == "" {
|
||||
return &APIError{
|
||||
Status: 0,
|
||||
Message: "not authenticated — run `containr auth login <token>` or set CONTAINR_TOKEN",
|
||||
ExitCode: ExitAuth,
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Do performs an API request. body may be nil, a []byte, or any value that
|
||||
// marshals to JSON. Returns the raw response body.
|
||||
func (c *Client) Do(method, path string, body interface{}) ([]byte, error) {
|
||||
var reader io.Reader
|
||||
if body != nil {
|
||||
var data []byte
|
||||
switch v := body.(type) {
|
||||
case []byte:
|
||||
data = v
|
||||
default:
|
||||
var err error
|
||||
data, err = json.Marshal(body)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to encode request: %w", err)
|
||||
}
|
||||
}
|
||||
reader = bytes.NewReader(data)
|
||||
}
|
||||
|
||||
url := c.BaseURL + path
|
||||
req, err := http.NewRequest(method, url, reader)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.Token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.Token)
|
||||
}
|
||||
if body != nil {
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
}
|
||||
if k := viper.GetString("idempotency-key"); k != "" && method == http.MethodPost {
|
||||
req.Header.Set("Idempotency-Key", k)
|
||||
}
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
hc := c.HTTPClient
|
||||
if hc == nil {
|
||||
hc = &http.Client{Timeout: 60 * time.Second}
|
||||
}
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, &APIError{
|
||||
Status: 0,
|
||||
Message: fmt.Sprintf("cannot reach %s: %v", c.BaseURL, err),
|
||||
ExitCode: ExitUnavailable,
|
||||
}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
data, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
|
||||
if resp.StatusCode >= 400 {
|
||||
msg := strings.TrimSpace(string(data))
|
||||
var envelope struct {
|
||||
Error string `json:"error"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if json.Unmarshal(data, &envelope) == nil && envelope.Error != "" {
|
||||
msg = envelope.Error
|
||||
if envelope.Code != "" {
|
||||
msg = envelope.Code + ": " + msg
|
||||
}
|
||||
}
|
||||
return nil, &APIError{
|
||||
Status: resp.StatusCode,
|
||||
Message: msg,
|
||||
ExitCode: exitCodeFor(resp.StatusCode),
|
||||
}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// DoRaw sends a non-JSON request body (file uploads) and returns the response.
|
||||
func (c *Client) DoRaw(method, path string, body io.Reader, contentType string) ([]byte, error) {
|
||||
req, err := http.NewRequest(method, c.BaseURL+path, body)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if c.Token != "" {
|
||||
req.Header.Set("Authorization", "Bearer "+c.Token)
|
||||
}
|
||||
req.Header.Set("Content-Type", contentType)
|
||||
req.Header.Set("Accept", "application/json")
|
||||
|
||||
hc := c.HTTPClient
|
||||
if hc == nil {
|
||||
hc = &http.Client{Timeout: 5 * time.Minute}
|
||||
}
|
||||
resp, err := hc.Do(req)
|
||||
if err != nil {
|
||||
return nil, &APIError{Status: 0, Message: fmt.Sprintf("cannot reach %s: %v", c.BaseURL, err), ExitCode: ExitUnavailable}
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
data, err := io.ReadAll(io.LimitReader(resp.Body, 64<<20))
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to read response: %w", err)
|
||||
}
|
||||
if resp.StatusCode >= 400 {
|
||||
msg := strings.TrimSpace(string(data))
|
||||
var envelope struct {
|
||||
Error string `json:"error"`
|
||||
Code string `json:"code"`
|
||||
}
|
||||
if json.Unmarshal(data, &envelope) == nil && envelope.Error != "" {
|
||||
msg = envelope.Error
|
||||
if envelope.Code != "" {
|
||||
msg = envelope.Code + ": " + msg
|
||||
}
|
||||
}
|
||||
return nil, &APIError{Status: resp.StatusCode, Message: msg, ExitCode: exitCodeFor(resp.StatusCode)}
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// DoJSON performs a request and unmarshals the response into out.
|
||||
func (c *Client) DoJSON(method, path string, body interface{}, out interface{}) error {
|
||||
data, err := c.Do(method, path, body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if out == nil || len(data) == 0 {
|
||||
return nil
|
||||
}
|
||||
return json.Unmarshal(data, out)
|
||||
}
|
||||
Loaded 100 of 340 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user