diff --git a/apps/base/farrisilk/httproute.yaml b/apps/base/farrisilk/httproute.yaml new file mode 100644 index 0000000..5c99157 --- /dev/null +++ b/apps/base/farrisilk/httproute.yaml @@ -0,0 +1,44 @@ +# Two separate HTTPRoutes and two separate Gateways, on purpose. +# +# A hostname-less route answers on the bare WireGuard address +# (http://10.10.0.2:8086/ and :8087/) for ANY Host header, because a phone +# cannot edit /etc/hosts. Cilium renders ONE RouteConfiguration per Gateway and +# merges every hostname-less route in it into a single weighted route, so two +# services sharing one Gateway serve each other's traffic at random regardless +# of listener port. Hence gateway-farrisilk-odoo.yaml (8086) and +# gateway-farrisilk-metabase.yaml (8087) in infrastructure/networking/internal/. +apiVersion: gateway.networking.k8s.io/v1 +kind: HTTPRoute +metadata: + name: odoo + namespace: farrisilk +spec: + parentRefs: + - name: farrisilk-odoo + namespace: internal + rules: + - matches: + - path: + type: PathPrefix + value: / + backendRefs: + - name: odoo + port: 8069 +--- +apiVersion: gateway.networking.k8s.io/v1 +kind: HTTPRoute +metadata: + name: metabase + namespace: farrisilk +spec: + parentRefs: + - name: farrisilk-metabase + namespace: internal + rules: + - matches: + - path: + type: PathPrefix + value: / + backendRefs: + - name: metabase + port: 3000 diff --git a/apps/base/farrisilk/kustomization.yaml b/apps/base/farrisilk/kustomization.yaml new file mode 100644 index 0000000..5a9d57e --- /dev/null +++ b/apps/base/farrisilk/kustomization.yaml @@ -0,0 +1,19 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization +resources: + - namespace.yaml + # Secrets are created out of band, before this Kustomization applies: + # farrisilk/pg-app basic-auth, postgres superuser + # farrisilk/pg-users odoo/metabase/metabase_ro role passwords + # farrisilk/odoo-config admin_passwd (database master password) + # farrisilk/odoo-admin email, password (web admin login) + # farrisilk/metabase-admin email, password (Metabase's own login) + # farrisilk/metabase-data-source-creds username=metabase_ro + password + # farrisilk/metabase-secret-key MB_ENCRYPTION_SECRET_KEY + # None of them appear in git; they are not managed (or pruned) by Flux. + - postgres-init.yaml + - postgres.yaml + - odoo-init.yaml + - odoo.yaml + - metabase.yaml + - httproute.yaml diff --git a/apps/base/farrisilk/metabase.yaml b/apps/base/farrisilk/metabase.yaml new file mode 100644 index 0000000..4cc9330 --- /dev/null +++ b/apps/base/farrisilk/metabase.yaml @@ -0,0 +1,106 @@ +# Metabase for the lab replica — its own state in the SAME plain Postgres +# (database `metabase`), one data source: `farrisilk odoo` as `metabase_ro`. +# +# This is a SECOND instance next to the evaluation one in namespace `metabase`. +# The laptop is on a marginal USB-C supply, so it does not repeat that +# instance's 1.5 GiB heap: 1 GiB heap, 2 GiB container limit. +# +# MB_ENCRYPTION_SECRET_KEY is set here on purpose. Without it Metabase encrypts +# stored data-source passwords with a key derived from its own default, so a +# copy of the app DB is enough to read them. The key lives in Secret +# `farrisilk/metabase-secret-key` and must stay stable for the life of the app +# DB — rotating it makes every stored datasource password undecryptable. +apiVersion: apps/v1 +kind: Deployment +metadata: + name: metabase + namespace: farrisilk +spec: + replicas: 1 + selector: + matchLabels: + app: metabase + template: + metadata: + labels: + app: metabase + app.kubernetes.io/part-of: farrisilk + spec: + nodeSelector: + kubernetes.io/hostname: siriussec + tolerations: + - key: dedicated + operator: Exists + effect: PreferNoSchedule + containers: + - name: metabase + image: metabase/metabase:v0.65.6.3 + imagePullPolicy: IfNotPresent + ports: + - name: http + containerPort: 3000 + env: + - name: JAVA_OPTS + value: "-Xmx1024m" + - name: MB_DB_TYPE + value: postgres + - name: MB_DB_HOST + value: pg.farrisilk.svc.cluster.local + - name: MB_DB_PORT + value: "5432" + - name: MB_DB_DBNAME + value: metabase + - name: MB_DB_USER + value: metabase + - name: MB_DB_PASS + valueFrom: + secretKeyRef: + name: pg-users + key: metabase_password + - name: MB_ENCRYPTION_SECRET_KEY + valueFrom: + secretKeyRef: + name: metabase-secret-key + key: MB_ENCRYPTION_SECRET_KEY + - name: MB_JETTY_PORT + value: "3000" + - name: TZ + value: America/Los_Angeles + readinessProbe: + httpGet: + path: /api/health + port: http + initialDelaySeconds: 20 + periodSeconds: 10 + timeoutSeconds: 5 + failureThreshold: 6 + livenessProbe: + httpGet: + path: /api/health + port: http + initialDelaySeconds: 120 + periodSeconds: 30 + timeoutSeconds: 10 + failureThreshold: 3 + resources: + requests: + cpu: 250m + memory: 1Gi + limits: + cpu: "2" + memory: 2Gi +--- +apiVersion: v1 +kind: Service +metadata: + name: metabase + namespace: farrisilk +spec: + type: ClusterIP + selector: + app: metabase + ports: + - name: http + port: 3000 + targetPort: http + protocol: TCP diff --git a/apps/base/farrisilk/namespace.yaml b/apps/base/farrisilk/namespace.yaml new file mode 100644 index 0000000..d06ff54 --- /dev/null +++ b/apps/base/farrisilk/namespace.yaml @@ -0,0 +1,7 @@ +apiVersion: v1 +kind: Namespace +metadata: + name: farrisilk + labels: + app.kubernetes.io/part-of: farrisilk + sirius.dev/purpose: lab-replica-of-client-stack diff --git a/apps/base/farrisilk/odoo-init.yaml b/apps/base/farrisilk/odoo-init.yaml new file mode 100644 index 0000000..2b64e84 --- /dev/null +++ b/apps/base/farrisilk/odoo-init.yaml @@ -0,0 +1,164 @@ +# Odoo provisioning, run as an initContainer on every pod start — idempotent by +# construction, so a restart never re-initialises a live database. +# +# 1. wait for Postgres +# 2. if the `farrisilk` database has no tables: `odoo -i base,web +# --without-demo=all --stop-after-init` (the "init once" step, done by the +# pod rather than by a separate Job so there is exactly one thing to wait +# on and no ordering race between a Job and the Deployment) +# 3. CREATE EXTENSION unaccent, as the database owner +# 4. (re)apply the read-only BI grants over every table Odoo just created, +# including revoking the tables that carry credentials +# 5. set the admin login/password from Secret `farrisilk/odoo-admin` +# +# Step 4 runs unconditionally, not only on first install: it is what picks up +# tables added by a later module install (`-i`/`-u` from /mnt/extra-addons), and +# `ALTER DEFAULT PRIVILEGES` alone does not cover tables created before the +# grant. Nothing here writes a credential to stdout. +apiVersion: v1 +kind: ConfigMap +metadata: + name: odoo-init + namespace: farrisilk +data: + init_db.py: | + #!/usr/bin/env python3 + """Idempotent first-run provisioning of the Farrisilk lab Odoo database.""" + import os + import subprocess + import sys + import time + + import psycopg2 + + HOST = os.environ["DB_HOST"] + PORT = int(os.environ.get("DB_PORT", "5432")) + SUPER_USER = os.environ["PG_SUPERUSER"] + SUPER_PW = os.environ["PG_SUPERPASS"] + DB = os.environ.get("ODOO_DB_NAME", "farrisilk") + CONF = "/etc/odoo-runtime/odoo.conf" + ADMIN_LOGIN = os.environ["ODOO_ADMIN_LOGIN"] + ADMIN_PASSWORD = os.environ["ODOO_ADMIN_PASSWORD"] + + # Tables that hold credentials looking like data. `ir_config_parameter` is + # the one the card calls out (API keys/tokens for integrations); the rest + # are the same class of column. `res_users` is deliberately NOT in this + # list: it carries a password *hash* only, and Metabase needs it for user + # counts. Everything found by inspection at runtime is added and logged. + CREDENTIAL_TABLES = [ + "ir_config_parameter", + "ir_mail_server", + "fetchmail_server", + "res_users_apikeys", + "payment_provider", + "res_users_identitycheck", + ] + + def connect(dbname, user, password): + conn = psycopg2.connect( + host=HOST, port=PORT, dbname=dbname, user=user, password=password, + connect_timeout=10, + ) + conn.autocommit = True + return conn + + def wait_for_pg(): + for _ in range(90): + try: + connect("postgres", SUPER_USER, SUPER_PW).close() + return + except psycopg2.Error: + time.sleep(2) + sys.exit("postgres unreachable") + + def tables(cur): + cur.execute( + "select table_name from information_schema.tables " + "where table_schema = 'public' and table_type = 'BASE TABLE'" + ) + return {r[0] for r in cur.fetchall()} + + def run(cmd, **kw): + print("+ " + " ".join(cmd), flush=True) + subprocess.run(cmd, check=True, **kw) + + wait_for_pg() + + conn = connect("postgres", SUPER_USER, SUPER_PW) + cur = conn.cursor() + cur.execute("select 1 from pg_database where datname = %s", (DB,)) + exists = cur.fetchone() is not None + conn.close() + + have_tables = False + if exists: + dconn = connect(DB, SUPER_USER, SUPER_PW) + dcur = dconn.cursor() + have_tables = bool(tables(dcur)) + dconn.close() + + if not have_tables: + print(f"== initialising {DB}: base,web (no demo data) ==", flush=True) + run(["odoo", "-c", CONF, "-d", DB, "-i", "base,web", + "--without-demo=all", "--stop-after-init", "--no-http"]) + else: + print(f"== {DB} already initialised: skipping -i base ==", flush=True) + + # unaccent as the database owner (trusted extension in PG16). Odoo uses it + # for accent-insensitive search in the web client. + oconn = connect(DB, "odoo", os.environ["ODOO_DB_PASSWORD"]) + ocur = oconn.cursor() + ocur.execute("CREATE EXTENSION IF NOT EXISTS unaccent") + ocur.execute("select extname, extowner::regrole::text from pg_extension where extname = 'unaccent'") + print("unaccent:", ocur.fetchone(), flush=True) + oconn.close() + + # Read-only BI grants + credential-table revokes. + gconn = connect(DB, SUPER_USER, SUPER_PW) + gcur = gconn.cursor() + gcur.execute("GRANT USAGE ON SCHEMA public TO metabase_ro") + gcur.execute("GRANT SELECT ON ALL TABLES IN SCHEMA public TO metabase_ro") + gcur.execute( + "ALTER DEFAULT PRIVILEGES FOR ROLE odoo IN SCHEMA public " + "GRANT SELECT ON TABLES TO metabase_ro" + ) + present = tables(gcur) + revoked = sorted({t for t in CREDENTIAL_TABLES if t in present}) + # Inspect rather than assume the static list is complete: anything whose + # columns look like credentials is added to the revoke set (logged below). + gcur.execute( + "select distinct table_name from information_schema.columns " + "where table_schema = 'public' and (" + " column_name ~ '(^|_)(password|secret|token)($|_)' or " + " column_name ~ '(api_key|access_key|private_key)'" + ") and table_name not in ('res_users', 'res_partner')" + ) + for (t,) in gcur.fetchall(): + if t in present and t not in revoked: + revoked.append(t) + revoked = sorted(revoked) + for t in revoked: + gcur.execute("REVOKE SELECT ON public.%s FROM metabase_ro" % t) + gcur.execute( + "select table_name from information_schema.table_privileges " + "where grantee = 'metabase_ro' and privilege_type = 'SELECT' " + "and table_schema = 'public' order by 1" + ) + readable = len(gcur.fetchall()) + gconn.close() + print(f"metabase_ro: SELECT granted on {readable} tables; " + f"revoked on {revoked}", flush=True) + + # Admin login/password from Secret farrisilk/odoo-admin. `odoo shell` is + # the supported way to set a res.users password without hand-rolling a + # pbkdf2 hash. Re-writing the same value on every start is harmless. + payload = ( + "u = env.ref('base.user_admin')\n" + f"u.write({{'login': {ADMIN_LOGIN!r}, 'password': {ADMIN_PASSWORD!r}}})\n" + "env.cr.commit()\n" + "print('admin uid', u.id, 'login', u.login)\n" + ) + run(["odoo", "shell", "-c", CONF, "-d", DB, "--no-http", + "--shell-interface=python"], input=payload, text=True) + + print("== provisioning complete ==", flush=True) diff --git a/apps/base/farrisilk/odoo.yaml b/apps/base/farrisilk/odoo.yaml new file mode 100644 index 0000000..2e89fcb --- /dev/null +++ b/apps/base/farrisilk/odoo.yaml @@ -0,0 +1,258 @@ +# Odoo 18 — the application half of the Farrisilk lab replica. +# +# Image is pinned to the DATED build, never `18.0`/`latest`: Odoo auto-migrates +# its database schema on start, so a floating tag turns a pod restart into an +# unplanned upgrade of a database you may be auditing. +# +# This image already ships wkhtmltopdf 0.12.6.1 (patched Qt) at +# /usr/local/bin/wkhtmltopdf, so PDF reports/ invoices render without a derived +# image. Verified 2026-11-22 — see README. +--- +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: odoo-data + namespace: farrisilk +spec: + accessModes: [ReadWriteOnce] + storageClassName: local-path + resources: + requests: + storage: 10Gi +--- +# Empty by default. This is where the client's custom modules are copied in +# (`kubectl cp ./module farrisilk/:/mnt/extra-addons/...`), which is +# why it is a PVC and not an emptyDir: a module installed from here must survive +# a pod restart, or the modules table and the addons path disagree and Odoo +# refuses to start the affected modules. +apiVersion: v1 +kind: PersistentVolumeClaim +metadata: + name: odoo-extra-addons + namespace: farrisilk +spec: + accessModes: [ReadWriteOnce] + storageClassName: local-path + resources: + requests: + storage: 5Gi +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: odoo + namespace: farrisilk +spec: + replicas: 1 + strategy: + type: Recreate + selector: + matchLabels: + app: odoo + template: + metadata: + labels: + app: odoo + app.kubernetes.io/part-of: farrisilk + spec: + # Envoy has to reach the backend on the same node it serves from, and the + # filestore PVC is local-path (siriussec-only anyway). + nodeSelector: + kubernetes.io/hostname: siriussec + tolerations: + - key: dedicated + operator: Exists + effect: PreferNoSchedule + initContainers: + # Odoo has no env-var support for db_password, so the config file is + # rendered from the ConfigMap template + the Secret on every pod start + # into an emptyDir. Runs as root only to fix PVC ownership (the image + # user is uid 100 / gid 101 and local-path hands out root-owned dirs). + - name: render-config + image: odoo:18.0-20261112 + imagePullPolicy: IfNotPresent + securityContext: + runAsUser: 0 + command: + - /bin/sh + - -ec + - | + mkdir -p /mnt/extra-addons /var/lib/odoo /etc/odoo-runtime + chown -R 100:101 /var/lib/odoo /mnt/extra-addons + chmod 0755 /mnt/extra-addons + umask 077 + cat > /etc/odoo-runtime/odoo.conf < hostNetwork, so +# the host firewall (infrastructure/networking/internal/host-firewall/) must be +# installed BEFORE this is pushed; otherwise the listener is open on the LAN the +# moment it is programmed. +apiVersion: gateway.networking.k8s.io/v1 +kind: Gateway +metadata: + name: farrisilk-odoo + namespace: internal + labels: + dedicated: network + annotations: + sirius.dev/purpose: bare-IP listener for the Farrisilk lab Odoo (WireGuard only) +spec: + gatewayClassName: cilium + listeners: + - name: http-internal + protocol: HTTP + # Verified free 2026-11-22: `ss -lntH "sport = :8086" | wc -l` = 0 on + # siriussec. Taken: 8080 public, 8081 Grafana, 8084 Hubble, 8085 Metabase. + port: 8086 + allowedRoutes: + namespaces: + from: All diff --git a/infrastructure/networking/internal/host-firewall/rpi-ufw-internal-gw.sh b/infrastructure/networking/internal/host-firewall/rpi-ufw-internal-gw.sh index fcbba69..16dfe94 100644 --- a/infrastructure/networking/internal/host-firewall/rpi-ufw-internal-gw.sh +++ b/infrastructure/networking/internal/host-firewall/rpi-ufw-internal-gw.sh @@ -7,14 +7,16 @@ # # Ports: 8081 = Grafana (`internal` Gateway listener http-internal), # 8084 = Hubble UI (`internal` Gateway listener hubble-ui), -# 8085 = Metabase (`metabase` Gateway listener http-internal). +# 8085 = Metabase (`metabase` Gateway listener http-internal), +# 8086 = Farrisilk lab Odoo (`farrisilk-odoo` Gateway), +# 8087 = Farrisilk lab Metabase (`farrisilk-metabase` Gateway). # Add a new internal listener port to PORTS; the loop is idempotent, so # re-running after a port is already firewalled only adds what is missing. # # Run as root on rpi (192.168.1.125). set -euo pipefail -PORTS="${PORTS:-8081 8084 8085}" +PORTS="${PORTS:-8081 8084 8085 8086 8087}" for port in $PORTS; do if ufw status | grep -q "${port}/tcp"; then diff --git a/infrastructure/networking/internal/host-firewall/siriussec-internal-gw.nft b/infrastructure/networking/internal/host-firewall/siriussec-internal-gw.nft index 16a9801..046a53c 100644 --- a/infrastructure/networking/internal/host-firewall/siriussec-internal-gw.nft +++ b/infrastructure/networking/internal/host-firewall/siriussec-internal-gw.nft @@ -1,6 +1,7 @@ #!/usr/sbin/nft -f # Internal (WireGuard-only) gateway listeners: TCP 8081 (Grafana), 8084 -# (Hubble UI) and 8085 (Metabase). +# (Hubble UI), 8085 (Metabase) and 8086/8087 (Farrisilk lab replica: Odoo, +# Metabase). # # Cilium's hostNetwork Envoy binds every Gateway listener on 0.0.0.0, so the # internal listeners are open on the LAN interface the moment the Gateway is @@ -22,13 +23,13 @@ table inet internal_gw { type filter hook input priority filter; policy accept; # local process / kubelet probes - tcp dport { 8081, 8084, 8085 } iifname "lo" counter accept + tcp dport { 8081, 8084, 8085, 8086, 8087 } iifname "lo" counter accept # WireGuard mesh (Legion = 10.10.0.3, nodes, remote hub spokes) # and the pod network (in-cluster scrapers, probes) - tcp dport { 8081, 8084, 8085 } ip saddr { 10.10.0.0/24, 10.244.0.0/16 } counter accept + tcp dport { 8081, 8084, 8085, 8086, 8087 } ip saddr { 10.10.0.0/24, 10.244.0.0/16 } counter accept # everything else: LAN, tailscale, public - tcp dport { 8081, 8084, 8085 } counter drop + tcp dport { 8081, 8084, 8085, 8086, 8087 } counter drop } } \ No newline at end of file diff --git a/infrastructure/networking/internal/kustomization.yaml b/infrastructure/networking/internal/kustomization.yaml index d6d0f1e..fc93e64 100644 --- a/infrastructure/networking/internal/kustomization.yaml +++ b/infrastructure/networking/internal/kustomization.yaml @@ -5,3 +5,5 @@ resources: - gateway.yaml - gateway-hubble-ui.yaml - gateway-metabase.yaml + - gateway-farrisilk-odoo.yaml + - gateway-farrisilk-metabase.yaml