Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
.git
.github
.venv
.pytest_cache
.coverage
**/__pycache__
tests
examples
charts
docker-bake.hcl
frontend/node_modules
frontend/dist
133 changes: 133 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
# Backend tests, the frontend's typecheck and build, the chart's lint and schema checks, the
# example catalog, and the whole thing end to end on a kind cluster with CloudNativePG.
#
# Its own file, not a step in release.yml, security.yml or quality.yml: those are rewritten
# from org-wide templates by Caldrith provisioning.
#
# All actions are SHA-pinned; the trailing comment is the tag each SHA corresponds to.
name: CI

on:
pull_request:
push:
branches: [main]
paths-ignore: ['**/*.md', 'LICENSE', '.gitignore']
workflow_dispatch:

permissions:
contents: read

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
backend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0
with:
python-version: '3.12'
- run: uv sync --locked
- run: uv run pytest -q

frontend:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version: '24'
cache: npm
cache-dependency-path: frontend/package-lock.json
- name: Typecheck and build
working-directory: frontend
run: |
npm ci --ignore-scripts --no-audit --no-fund
npm run build
# The CSP allows no inline script or style, so the built page must carry none.
- name: No inline code in the page
run: |
set -euo pipefail
if grep -qE '<script>|<script [^>]*>[^<]+</script>|<style|style=' frontend/dist/index.html; then
echo "frontend/dist/index.html has inline script or style, which the CSP blocks" >&2
exit 1
fi

chart:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: azure/setup-helm@9bc31f4ebc9c6b171d7bfbaa5d006ae7abdb4310 # v5.0.1
- uses: yokawasa/action-setup-kube-tools@b16d49b2c459d33cf9d4c300a220ef0bcdbc730a # v0.14.0
with:
setup-tools: kubeconform
kubeconform: '0.6.7'
- name: helm lint
run: helm lint charts/databases-ui --values charts/databases-ui/ci/default-values.yaml
# Everything switched on and the smallest working install, against the real API schemas.
- name: Render and validate
run: |
set -euo pipefail
for values in charts/databases-ui/ci/default-values.yaml charts/databases-ui/ci/sas-values.yaml; do
helm template t charts/databases-ui --namespace databases --values "$values" >rendered.yaml
kubeconform -strict -summary -kubernetes-version 1.33.0 -schema-location default rendered.yaml
done
# Missing required values must fail the render, not install something half-configured.
if helm template t charts/databases-ui >/dev/null 2>&1; then
echo "the chart rendered without azure.tenantId" >&2
exit 1
fi

catalog:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0
with:
python-version: '3.12'
# The example catalog is the README's reference: it has to hold together.
- run: uv sync --locked --no-dev
- run: uv run python -m databases_ui check examples/catalog.yaml

# The image, the chart, CloudNativePG, Azurite and real PostgreSQL: every action through the
# API, checked in the databases themselves (tests/e2e/run.py).
e2e:
runs-on: ubuntu-latest
timeout-minutes: 30
steps:
- uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
- uses: astral-sh/setup-uv@1c37ad07a6a961277cf70c0d37d6f313000f5884 # v10.3.0
with:
python-version: '3.12'
- uses: azure/setup-helm@9bc31f4ebc9c6b171d7bfbaa5d006ae7abdb4310 # v5.0.1
- name: Build the image
run: docker build --tag databases-ui:dev .
# kind comes with the runner image.
- name: A cluster with CloudNativePG
run: |
set -euo pipefail
kind create cluster --name dbui --wait 120s
kind load docker-image databases-ui:dev --name dbui
helm install cnpg cloudnative-pg --repo https://cloudnative-pg.github.io/charts --version 0.29.1 \
--namespace cnpg-system --create-namespace --wait --timeout 5m
- run: uv sync --locked
- run: uv run python tests/e2e/run.py
- name: What the cluster looked like
if: failure()
run: |
kubectl get pods,jobs -A -o wide
kubectl -n database get clusters,databases.postgresql.cnpg.io -o wide
kubectl -n databases-ui logs deployment/databases-ui --tail=200
for pod in $(kubectl -n database get pods -l app.kubernetes.io/component=job -o name); do kubectl -n database logs "$pod" --tail=50; done
16 changes: 16 additions & 0 deletions .mega-linter.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
# Additive to the env Chargate injects (MagmaMoose/chargate, docs/setup.md).

# tests/e2e/fixtures.yaml is the world the end-to-end test builds on a throwaway kind or k3d
# cluster and deletes with it: Azurite as Microsoft ships it, and RoleBindings that give test
# groups `edit` and `admin` in app namespaces, because that access is what the test exercises.
# It is applied nowhere else, so the hardening and RBAC rules these scanners hold manifests to
# (resource limits, seccomp, a read-only root, trusted registries, no binding to admin) are
# about deployments it is not. Trivy, Checkov and Semgrep still read every other file, the
# chart included, and the secret scanners still read this one.
#
# Per-linter ARGUMENTS rather than FILTER_REGEX_EXCLUDE: all three are project-scope linters,
# which MegaLinter's file filter never reaches.
REPOSITORY_TRIVY_ARGUMENTS: "--skip-files tests/e2e/fixtures.yaml"
REPOSITORY_CHECKOV_ARGUMENTS: "--skip-path tests/e2e/fixtures.yaml"
REPOSITORY_SEMGREP_ARGUMENTS: "--exclude tests/e2e/fixtures.yaml"
40 changes: 40 additions & 0 deletions CLAUDE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# databases-ui

FastAPI backend (`src/databases_ui`) serving a React/TypeScript console (`frontend/`), and the Jobs'
runner (`runner.py`) in the same image, with a Helm chart (`charts/databases-ui`). State lives in
Kubernetes (Database resources, Jobs, RBAC) and Blob Storage (snapshots and their shares, the audit
log); the server stores nothing.

## Things that fail silently

- **The user and groups the access reviews ask about must be the API server's.** They come from
`DATABASES_UI_USERNAME_*` and `DATABASES_UI_GROUPS_*` and the ID token, as the session took them
at sign-in. A claim, a prefix or a lowercased name that differs from the cluster's authentication
configuration asks about someone RBAC doesn't know, and every database disappears. A change in
someone's groups reaches the app when they sign in again.
- **A database outside your namespaces answers 404, never 403.** Keep it that way in new routes
(`Console._database`), or a 403 tells who may not see a database that it exists.
- **The admission policy (`templates/jobpolicy.yaml`) knows the Job's shape.** It checks the
variables `PGPASSWORD`, `DBUI_STORAGE_SAS`, `PGHOST`, `PGSSLMODE`, `DBUI_BLOB_URL` and
`DBUI_AUDIT_URL`, the volume kinds and the `job` argument that `jobs.py` writes. Renaming one or
adding a Secret, a volume or a container in `jobs.py` without changing the policy in the same
change makes Kubernetes refuse every Job.
- **Job pods carry `app.kubernetes.io/component: job`, never `web`.** `web` is the Service and
PodDisruptionBudget selector; the chart's database NetworkPolicy admits `job`.
- **COMMIT reaches psql only after pg_restore exited 0** (`runner.apply`). The restore drops what
the owner owns first, so a COMMIT after a partial script would leave the database half empty;
any new early exit there must close psql without it.
- **A storage lifecycle rule must be scoped to `snapshots/`.** One on the whole container also
deletes the audit log under `audit/`.
- **Snapshots leave extensions out** (`--exclude-extension=*`): the owner role can't drop one the
operator created. A copy gets its extensions from its `Database` resource, copied from the
source's `spec.extensions`; a restore into a database without them fails, and changes nothing.
- **No `from __future__ import annotations` in `app.py`.** The routes use `Annotated` path types;
postponed annotations turn them into strings and FastAPI answers 422 everywhere.
- **The page may hold no inline script or style.** The CSP blocks it and the page renders blank;
CI greps `frontend/dist/index.html` for it.
- **The image name follows the repository name** (`ghcr.io/magmamoose/databases-ui`). The chart's
default image is that name, and the Jobs run the same image; a rename means a chart change in the
same PR.
- **No client or employer names anywhere in this public repository**: code, tests, fixtures,
docs, commits. Examples use example.com and fictional teams.
52 changes: 52 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# databases-ui: the FastAPI backend serving the built React frontend, and the Jobs' runner with the
# PostgreSQL client, as one image.

# The frontend is static files, the same on every architecture, so it builds once on the
# build host rather than under emulation for each target.
FROM --platform=$BUILDPLATFORM node:25-slim@sha256:81db02c4b671288a03915da9534dbd54f96d0e7c24d80ccc54f5b36b2e684370 AS frontend
WORKDIR /frontend
COPY frontend/package.json frontend/package-lock.json ./
RUN npm ci --ignore-scripts --no-audit --no-fund
COPY frontend/ ./
RUN npm run build

FROM python:3.14-slim@sha256:a2b82f3c48559aa0a8446d9af49826b6e2b2016f4cd2afabfe6013ec53729170 AS backend
RUN pip install --no-cache-dir uv==0.11.19
ENV UV_COMPILE_BYTECODE=1 \
UV_LINK_MODE=copy \
UV_PYTHON_DOWNLOADS=never
WORKDIR /app
COPY pyproject.toml uv.lock README.md ./
COPY src ./src
# The locked dependencies and the package itself, no dev group, into /app/.venv.
RUN uv sync --locked --no-dev --no-editable

FROM python:3.14-slim@sha256:a2b82f3c48559aa0a8446d9af49826b6e2b2016f4cd2afabfe6013ec53729170

# pg_dump, pg_restore and psql from the PostgreSQL project's repository: pg_dump has to be at
# least the server's version, and Debian's own lags behind. Its signing key is kept here
# (fingerprint B97B0AFCAA1A47F044F244A07FCC7D46ACCC4CF8), so a rotation is a reviewed change.
COPY docker/apt.postgresql.org.asc /usr/share/postgresql-common/pgdg/apt.postgresql.org.asc
# hadolint ignore=DL3008
RUN set -eux; \
codename="$(. /etc/os-release && echo "$VERSION_CODENAME")"; \
echo "deb [signed-by=/usr/share/postgresql-common/pgdg/apt.postgresql.org.asc] https://apt.postgresql.org/pub/repos/apt ${codename}-pgdg main" > /etc/apt/sources.list.d/pgdg.list; \
apt-get update; \
apt-get install -y --no-install-recommends postgresql-client-18; \
rm -rf /var/lib/apt/lists/*

ARG VERSION=dev
ENV PATH=/app/.venv/bin:/usr/lib/postgresql/18/bin:$PATH \
PYTHONUNBUFFERED=1 \
PYTHONDONTWRITEBYTECODE=1 \
APP_VERSION=${VERSION} \
PORT=8080

COPY --from=backend /app/.venv /app/.venv
COPY --from=frontend /frontend/dist /app/static

# Numeric and above 10000, so a pod's runAsUser can match the image exactly.
USER 10001:10001
EXPOSE 8080
HEALTHCHECK --interval=30s --timeout=3s CMD ["python", "-c", "import urllib.request; urllib.request.urlopen('http://127.0.0.1:8080/healthz', timeout=2)"]
ENTRYPOINT ["python", "-m", "databases_ui"]
Loading
Loading