feat(backup): pinned backup-tools image + egress allow list for the three nightly jobs #87

Merged
sirius merged 3 commits from wt/t_9d0428d8 into master 2026-12-04 05:20:44 +00:00
12 changed files with 365 additions and 22 deletions

View file

@ -0,0 +1,74 @@
name: backup-image
# Builds the backup tooling image used by the three nightly CronJobs
# (monitoring/grafana-backup, forgejo/cluster-backup, sirius-growth/leadgen-backup).
#
# Why this is a CI build at all (card t_9d0428d8): the jobs used to run
# `apk add rsync openssh-client` at container start, so the backup depended on
# the Alpine CDN at run time. Everything is baked here instead, from a base
# image pinned by digest, and the manifests pin the resulting image by digest.
#
# CI has no cluster credentials: this job only pushes an immutable :$sha tag to
# the in-cluster Forgejo registry and prints the digest to pin. The operator
# copies that digest into the three CronJob manifests in this same repo.
#
# amd64 only, on purpose: all three Jobs are nodeSelector'd to siriussec and
# the baked kubectl release checksum is amd64. Nothing here runs on rpi.
on:
push:
branches: [master]
paths:
- 'images/backup/**'
- '.forgejo/workflows/backup-image.yml'
workflow_dispatch:
jobs:
build:
runs-on: docker-amd64
steps:
- uses: actions/checkout@v4
# The job image (node:20-bullseye) has no docker CLI of its own; the
# runner mounts the dind sidecar's client and cli-plugins. Verify both
# instead of assuming, and fail loudly rather than half-pushing.
- name: Verify docker CLI + buildx
run: |
set -euo pipefail
command -v docker
docker version
docker buildx version
- name: Build and push (amd64, no attestation manifests)
env:
REG_USER: ${{ secrets.REGISTRY_USER }}
REG_PASS: ${{ secrets.REGISTRY_TOKEN }}
run: |
set -euo pipefail
sha=$(git rev-parse HEAD)
image=forgejo.siriusdevops.com/sirius/backup-tools
user="${REG_USER:-sirius}"
pass="${REG_PASS:-${{ github.token }}}"
echo "$pass" | docker login forgejo.siriusdevops.com -u "$user" --password-stdin
# --provenance=false --sbom=false: the tag must resolve to the single
# amd64 image manifest, so the digest a CronJob pins is exactly the
# image the pod runs (an index digest would resolve per-arch).
# --metadata-file carries the pushed manifest digest. Do NOT read it
# back with `buildx imagetools inspect`: Forgejo's /v2/token answers
# 403 to that scope set (measured), while push and kubelet pull are
# fine.
docker buildx build --platform linux/amd64 \
--provenance=false --sbom=false \
-t "$image:$sha" \
--push --metadata-file /tmp/build-meta.json images/backup
echo "pushed $image:$sha"
digest=$(tr -d '\n' < /tmp/build-meta.json \
| sed -n 's/.*"containerimage\.digest": *"\(sha256:[a-f0-9]*\)".*/\1/p')
[ -n "$digest" ] || { echo "::error::no containerimage.digest in build metadata"; cat /tmp/build-meta.json; exit 1; }
echo "BACKUP_TOOLS_DIGEST=$digest"
{
echo "### Pin this in the three backup CronJob manifests"
echo ""
echo '```'
echo "$image@$digest"
echo '```'
} | tee -a "${GITHUB_STEP_SUMMARY:-/dev/stdout}"

View file

@ -87,12 +87,10 @@ data:
# (the node runs close to DiskPressure and `local-path` lives on it).
set -euo pipefail
# alpine/k8s ships kubectl/bash but not rsync/ssh; install them per run
# (~2 s). Nothing else is needed: the DB dumps run inside the CNPG pod.
if ! command -v rsync >/dev/null || ! command -v ssh >/dev/null; then
echo "== installing rsync + openssh-client"
apk add --no-cache rsync openssh-client >/dev/null
fi
# No package install here on purpose: rsync/openssh-client/bash/kubectl are
# baked into the pinned image (images/backup/Dockerfile) and this pod's
# egress policy admits DNS, 10.10.0.5:22 and the API only, so a runtime
# fetch would fail rather than silently dial the Alpine CDN (t_9d0428d8).
BACKUP_HOST=${BACKUP_HOST:[email protected]}
BACKUP_DIR=${BACKUP_DIR:-/root/cluster-backup}
@ -246,16 +244,20 @@ spec:
spec:
restartPolicy: Never
serviceAccountName: cluster-backup
# Pulled from the in-cluster Forgejo registry (which requires auth).
imagePullSecrets:
- name: forgejo-pull
# The forgejo-data PVC is local-path on siriussec; the pod must run
# where the volume is.
nodeSelector:
kubernetes.io/hostname: siriussec
containers:
- name: backup
# alpine/k8s carries kubectl 1.36.4 (== cluster version) so the
# CNPG dumps can be taken through the exec API; rsync + openssh
# are added at start (the pod has normal egress).
image: alpine/k8s:1.36.4
# python3.12-alpine + rsync + openssh-client + bash + kubectl
# v1.36.4 (== cluster version, so the CNPG dumps can be taken
# through the exec API), all baked at build time and pinned by
# digest. Nothing is installed at run time.
image: forgejo.siriusdevops.com/sirius/backup-tools@sha256:cc8dc1645111b3a80b7c4d5168f6bce3002cae7417b3725683c057976f6e379a
imagePullPolicy: IfNotPresent
command: ["/bin/bash", "/scripts/backup.sh"]
env:

View file

@ -8,6 +8,10 @@ resources:
- httproute.yaml
# Default-deny ingress+egress + the namespace allow list (audit F3 / t_fed52919).
- networkpolicy.yaml
# Egress lockdown for the cluster-backup pod: DNS + 10.10.0.5:22 + the API
# only. The allow list above cannot be narrowed per pod, so the deny for
# `world` in this policy is what applies to the job (card t_9d0428d8).
- networkpolicy-backup.yaml
- pvc.yaml
- cluster.yaml
- podmonitor.yaml

View file

@ -0,0 +1,64 @@
# Egress lockdown for the nightly cluster-backup CronJob (card t_9d0428d8).
#
# Why: the job used to `apk add --no-cache rsync openssh-client` at container
# start, so each run depended on the Alpine CDN (Fastly 151.101.x.132:443) and
# tripped `egress-new-dst` whenever that anycast address rotated. rsync/ssh/
# bash/kubectl are baked into images/backup/Dockerfile now.
#
# `forgejo-allow` (networkpolicy.yaml) selects every pod in this namespace and
# allows `world`, so a pod-scoped allow list alone would NOT restrict the job —
# Cilium unions multiple policies. The `egressDeny` for `world` below is what
# actually closes it (deny always beats allow), and it is scoped to this pod by
# the endpointSelector, so Forgejo's own webhooks/mirror/avatar egress is
# untouched.
#
# Allow list, and nothing else:
# * cluster DNS;
# * [email protected]:22 — the Linode node's InternalIP, so `remote-node`, not
# `world` (the rsync target and every ssh_r call);
# * kube-apiserver on 6443/443 — `kubectl get cluster` for the CNPG primary
# and `kubectl exec ... pg_dump` for both CNPG and the plain Postgres
# Deployment. Port-scoped on purpose: the apiserver shares its host IP with
# the internal Gateway listeners on 8081/8084-8087.
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: cluster-backup-egress
namespace: forgejo
labels:
app: cluster-backup
spec:
description: >-
cluster-backup pod: cluster DNS, SSH to the backup host (10.10.0.5:22) and
the API (6443/443) only; no internet egress at run time.
endpointSelector:
matchLabels:
app: cluster-backup
egress:
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
- port: "53"
protocol: TCP
- toEntities:
- remote-node
toPorts:
- ports:
- port: "22"
protocol: TCP
- toEntities:
- kube-apiserver
toPorts:
- ports:
- port: "6443"
protocol: TCP
- port: "443"
protocol: TCP
egressDeny:
- toEntities:
- world

View file

@ -16,6 +16,10 @@ resources:
- httproute.yaml
# Default-deny ingress+egress + the namespace allow list (audit F3 / t_fed52919).
- networkpolicy.yaml
# Egress lockdown for the leadgen-backup pod: DNS + 10.10.0.5:22 only. The
# allow list above cannot be narrowed per pod, so the deny for `world` in
# this policy is what applies to the job (card t_9d0428d8).
- networkpolicy-backup.yaml
# The WAL archive store must exist before the Cluster references it.
- objectstore-portal-wal.yaml
- cluster.yaml

View file

@ -20,10 +20,10 @@ data:
# Snapshot mirror of /src -> $BACKUP_DIR/leadgen-data/<stamp>/ + `latest`.
set -euo pipefail
if ! command -v rsync >/dev/null || ! command -v ssh >/dev/null; then
echo "== installing rsync + openssh-client"
apk add --no-cache rsync openssh-client >/dev/null
fi
# No package install here on purpose: rsync/openssh-client/bash are baked
# into the pinned image (images/backup/Dockerfile) and this pod's egress
# policy admits DNS + 10.10.0.5:22 only, so a runtime fetch would fail
# rather than silently dial the Alpine CDN (card t_9d0428d8).
BACKUP_HOST=${BACKUP_HOST:[email protected]}
BACKUP_DIR=${BACKUP_DIR:-/root/cluster-backup}
@ -102,12 +102,18 @@ spec:
app: leadgen-backup
spec:
restartPolicy: Never
# Pulled from the in-cluster Forgejo registry (which requires auth).
imagePullSecrets:
- name: forgejo-pull
# The PVC is local-path on siriussec.
nodeSelector:
kubernetes.io/hostname: siriussec
containers:
- name: backup
image: alpine/k8s:1.36.4
# rsync + openssh-client + bash baked at build time, pinned by
# digest (images/backup/Dockerfile). Nothing is installed at run
# time.
image: forgejo.siriusdevops.com/sirius/backup-tools@sha256:cc8dc1645111b3a80b7c4d5168f6bce3002cae7417b3725683c057976f6e379a
imagePullPolicy: IfNotPresent
command: ["/bin/bash", "/scripts/backup.sh"]
env:

View file

@ -0,0 +1,49 @@
# Egress lockdown for the nightly leadgen-backup CronJob (card t_9d0428d8).
#
# Why: the job used to `apk add --no-cache rsync openssh-client` at container
# start, so each run depended on the Alpine CDN (Fastly 151.101.x.132:443).
# rsync/ssh/bash (+kubectl, unused here) are baked into
# images/backup/Dockerfile now.
#
# `sirius-growth-allow` (networkpolicy.yaml) selects every pod in this
# namespace and allows `world` (Telegram Bot API, the leadgen crawlers), so a
# pod-scoped allow list alone would NOT restrict this job — Cilium unions
# policies. The `egressDeny` for `world` closes it for this pod only; the app
# pods keep their internet access.
#
# Allow list, and nothing else: cluster DNS, and [email protected]:22 (the Linode
# node's InternalIP — `remote-node`, not `world`) for the rsync mirror.
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: leadgen-backup-egress
namespace: sirius-growth
labels:
app: leadgen-backup
spec:
description: >-
leadgen-backup pod: cluster DNS + SSH to the backup host (10.10.0.5:22)
only; no internet egress at run time.
endpointSelector:
matchLabels:
app: leadgen-backup
egress:
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
- port: "53"
protocol: TCP
- toEntities:
- remote-node
toPorts:
- ports:
- port: "22"
protocol: TCP
egressDeny:
- toEntities:
- world

49
images/backup/Dockerfile Normal file
View file

@ -0,0 +1,49 @@
# Backup tooling image for the three nightly state-backup CronJobs
# (monitoring/grafana-backup, forgejo/cluster-backup, sirius-growth/leadgen-backup).
#
# Why this exists (card t_9d0428d8): every one of those jobs used to run
# `apk add --no-cache rsync openssh-client` at container start, which dialled
# dl-cdn.alpinelinux.org (Fastly anycast 151.101.x.132:443) on every run. That
# made the nightly backup depend on the public internet, tripped the
# `egress-new-dst` flow alert whenever Fastly rotated an address, and put a
# package install on the one pod that carries the Grafana PVC and an SSH key.
# The toolchain is baked in at build time instead, and the pods get an egress
# NetworkPolicy that admits DNS + the backup host only.
#
# Base image is pinned by digest (multi-arch index digest; this build is
# linux/amd64 because all three jobs are nodeSelector'd to siriussec).
FROM python:3.12-alpine@sha256:c5ba3deef00c7758561a0f03c26f44f10589263d40e732b8748e0d1f38f6dd89
# kubectl is needed by forgejo/cluster-backup only (CNPG `get cluster` +
# `exec` pg_dump); the cluster runs v1.36.4, so the client is pinned to the
# same minor and its release checksum is verified at build time.
ARG KUBECTL_VERSION=v1.36.4
ARG KUBECTL_SHA256=8b8f088da2dab964f853b38464033b1be15ede2839eca751482357c45abdd05a
LABEL org.opencontainers.image.title="backup-tools" \
org.opencontainers.image.description="python3.12-alpine + rsync + openssh-client + bash + kubectl for the nightly state backups" \
org.opencontainers.image.source="https://forgejo.siriusdevops.com/sirius/homek8s"
RUN set -eux; \
# bash: cluster-backup and leadgen-backup use arrays, `set -o pipefail`
# and `tee >(...)` process substitution, so /bin/sh (busybox ash) is not
# enough -- the CronJob entrypoints stay /bin/bash, unchanged.
apk add --no-cache rsync openssh-client bash; \
test "${TARGETARCH:-amd64}" = "amd64" || { echo "kubectl pin is amd64-only; the backup jobs run on siriussec" >&2; exit 1; }; \
apk add --no-cache --virtual .fetch curl ca-certificates; \
curl -fsSL -o /usr/local/bin/kubectl \
"https://dl.k8s.io/release/${KUBECTL_VERSION}/bin/linux/amd64/kubectl"; \
echo "${KUBECTL_SHA256} /usr/local/bin/kubectl" | sha256sum -c -; \
chmod 0755 /usr/local/bin/kubectl; \
apk del .fetch; \
rm -rf /var/cache/apk/*
# Build-time proof that nothing has to be installed at run time.
RUN set -eux; \
rsync --version | head -1; \
ssh -V; \
bash --version | head -1; \
python3 --version; \
kubectl version --client
CMD ["/bin/sh"]

View file

@ -0,0 +1,54 @@
# Egress lockdown for the nightly Grafana sqlite snapshot (card t_9d0428d8).
#
# Why: grafana-backup used to run `apk add --no-cache rsync openssh-client` at
# container start, so every night it dialled the Alpine CDN
# (dl-cdn.alpinelinux.org -> Fastly 151.101.x.132:443) and kept firing the
# `egress-new-dst` flow alert whenever that anycast address rotated. The
# toolchain is now baked into the image (images/backup/Dockerfile) and this
# policy makes any future runtime fetch fail loudly instead of silently
# reaching the internet.
#
# `monitoring` has no namespace-wide default-deny, so selecting the backup pod
# here isolates it on its own: the rules below are the complete egress allow
# list and everything else (including `world`) is dropped. `egressDeny` is
# stated explicitly so the intent survives a future namespace-wide allow being
# added — Cilium deny always beats allow.
#
# Needs: cluster DNS (the script resolves nothing itself, but the image and any
# `ssh` target lookup would), and [email protected]:22 — that is the Linode node's
# InternalIP, so Cilium classifies it `remote-node`, not `world`. The registry
# pull and the Grafana PVC access are not pod traffic and are unaffected.
apiVersion: cilium.io/v2
kind: CiliumNetworkPolicy
metadata:
name: grafana-backup-egress
namespace: monitoring
labels:
app: grafana-backup
spec:
description: >-
grafana-backup pod: cluster DNS + SSH to the backup host (10.10.0.5:22)
only; no internet egress at run time.
endpointSelector:
matchLabels:
app: grafana-backup
egress:
- toEndpoints:
- matchLabels:
io.kubernetes.pod.namespace: kube-system
k8s-app: kube-dns
toPorts:
- ports:
- port: "53"
protocol: UDP
- port: "53"
protocol: TCP
- toEntities:
- remote-node
toPorts:
- ports:
- port: "22"
protocol: TCP
egressDeny:
- toEntities:
- world

View file

@ -27,10 +27,10 @@ data:
# T480's root filesystem, the only local file is a ~3 MB copy in /tmp.
set -eu
if ! command -v rsync >/dev/null || ! command -v ssh >/dev/null; then
echo "== installing rsync + openssh-client"
apk add --no-cache rsync openssh-client >/dev/null
fi
# No package install here on purpose: rsync/openssh-client/python3 are
# baked into the pinned image (images/backup/Dockerfile) and this pod's
# egress policy admits DNS + 10.10.0.5:22 only, so a runtime fetch would
# fail rather than silently dial the Alpine CDN (card t_9d0428d8).
BACKUP_HOST=${BACKUP_HOST:[email protected]}
BACKUP_DIR=${BACKUP_DIR:-/root/cluster-backup}
@ -128,15 +128,22 @@ spec:
app: grafana-backup
spec:
restartPolicy: Never
# Pulled from the in-cluster Forgejo registry (which requires auth);
# monitoring had no registry pull secret before this job needed one.
imagePullSecrets:
- name: forgejo-pull
# The grafana PVC is local-path on siriussec; RWO still allows a
# second pod on the same node.
nodeSelector:
kubernetes.io/hostname: siriussec
containers:
- name: backup
# python3 alpine: the sqlite backup API is the only consistent
# way to copy a live database without a shell inside Grafana.
image: python:3.12-alpine
# python3.12-alpine + rsync + openssh-client, baked at build time
# and pinned by digest (images/backup/Dockerfile, CI workflow
# .forgejo/workflows/backup-image.yml). The sqlite backup API is
# the only consistent way to copy a live database without a shell
# inside Grafana. Nothing is installed at run time.
image: forgejo.siriusdevops.com/sirius/backup-tools@sha256:cc8dc1645111b3a80b7c4d5168f6bce3002cae7417b3725683c057976f6e379a
imagePullPolicy: IfNotPresent
command: ["/bin/sh", "/scripts/backup.sh"]
env:

View file

@ -17,6 +17,9 @@ resources:
- rosenpass.yaml
# Nightly consistent sqlite snapshot of Grafana's dashboards/alerts/users.
- grafana-backup.yaml
# Egress lockdown for that backup pod: DNS + the backup host only, so no
# run-time package fetch (card t_9d0428d8).
- grafana-backup-networkpolicy.yaml
- dashboards
# Secrets, SOPS/age-encrypted in git (decrypted by the `monitoring`
# Kustomization; key = flux-system/sops-age). See docs/secrets-sops.md.
@ -24,6 +27,10 @@ resources:
- secret-grafana-secret-key.sops.yaml
- secret-alertmanager-telegram.sops.yaml
- secret-cluster-backup-ssh.sops.yaml
# Registry pull credential for this namespace. Added because grafana-backup
# now runs an image from the in-cluster Forgejo registry instead of
# python:3.12-alpine from Docker Hub (card t_9d0428d8).
- secret-forgejo-pull.sops.yaml
configMapGenerator:
- name: kube-prometheus-stack-values

View file

@ -0,0 +1,23 @@
apiVersion: v1
data:
.dockerconfigjson: ENC[AES256_GCM,data:c7NwYAKS00RAzkEW3z1IA2XP24cdIJgQAaBjg7+WcsxLMEs2h42cg/fdtW6UYrrsQtq7X/T4wgfpzUjHq8aMI2lyiVhJa9vxHknpK32i2mP63YQ7o2axI2p5eGPQpB+uuLPr3e1A6I939co6LQCBrY1ueK9+9atCcSEflv/wJyXwrwvlV5EL20PTLTM/y8gGN9cXXfgk6aYV7F5mowQCcUIUDvCbj7cHIonumMfCANQgGjQHrACf0+2ko853nBwLQK1AtIP+U/NS1wDSbFmxqyO6OstF10Sq5t+8dY8baIt73RWN0BqB/7i08TsMpnNUXH4XJcP6WhP0FM+X3M5goJr80DNy2uG3fxTHwRSAeWGzIo2GuuK9qlMtIjjnU97K,iv:xPgSzx6+EEJRnTGYG+QZWJ7G3MUf6Ajd+CXpl8+XIxs=,tag:alsSmEFhXW46SeoVzdqyFA==,type:str]
kind: Secret
metadata:
name: forgejo-pull
namespace: monitoring
type: kubernetes.io/dockerconfigjson
sops:
age:
- enc: |
-----BEGIN AGE ENCRYPTED FILE-----
YWdlLWVuY3J5cHRpb24ub3JnL3YxCi0+IFgyNTUxOSA1eU8ydkR5SmF2UG9ZQWNY
TWdKTFJkdjFlWWtBSW4wdmFDWXA0akNSSFI4CmVpL3dMKy91b1JTUTRyV1QyRXlI
YnlQZVJhL0hNRENNdlM1cDZKZmRUN0kKLS0tIEZOVHBLOUsvbFp6VjJXbW1tVmhK
Y1M4a2VyL0Z2MDhLeGJZUCtHRnQ2cmMKU9Q4m7ZuZDYbNQgsvOXEAKBE1hW1pa7m
7/pmRAP43OjXfsjWUbnPmORcHSrVDtRrBxQZ2d1svVdwd6vMUPPjqw==
-----END AGE ENCRYPTED FILE-----
recipient: age12zdn7x72zrgu9cdv8wdj7xfx42nz09nrwpc2g09cwx5fwdm0ry6qp5h4jp
encrypted_regex: ^(data|stringData)$
lastmodified: "2026-12-04T05:16:30Z"
mac: ENC[AES256_GCM,data:yYt1AWNi+Ts4c/p3OwrknVVPMPf9PEeYvgz/LckFqsJweSq36b6gzinMcLxJGnenmCokftUA4RvMMF2f0Lcpo3+RqmpPGnCzpAVQ0ypuS2+n+bET7tF/tu9icuwZaTu0sNW+/j8VK5T5nlrrw+aLf3O9yIuP7XVLXyjodJqbH7c=,iv:jSMmP96DHN6qLmpHoisVQu1DYlMVehxdXNWkzh5GZwk=,tag:b98z963bBjpM5gGZnKHqBg==,type:str]
version: 3.13.3