From 6cb184404ad26e4199fbb14535276b42dbf33c42 Mon Sep 17 00:00:00 2001 From: Sam Catlow Date: Tue, 6 Oct 2026 03:04:12 +1100 Subject: [PATCH] feat(install): bundled PostgreSQL has no network and is reached by Unix socket; print an AppArmor profile instead of disabling the userns restriction --- .github/workflows/deploy.yml | 2 +- CHANGELOG.md | 2 +- README.md | 5 +- docker-compose.yml | 4 +- install.sh | 77 ++++++++++++++----- .../infrastructure/configuration/defaults.go | 6 ++ .../configuration/setup/setup.go | 9 ++- tools/release.sh | 2 +- 8 files changed, 77 insertions(+), 30 deletions(-) diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index 5b621a131..cc74a237f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -350,7 +350,7 @@ jobs: for cmd in $(jq -r '.required_topology.units[]' release.json); do cp "dist/$cmd" "$name/" done - cp deployments/INSTRUCTIONS.md CHANGELOG.md docker-compose.yml "$name/" + cp deployments/INSTRUCTIONS.md CHANGELOG.md "$name/" cp scripts/archie-update-* "$name/" digest='${{ needs.build-and-push.outputs.agent_digest }}' test -n "$digest" diff --git a/CHANGELOG.md b/CHANGELOG.md index e4843e039..bb5ded847 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,7 +13,7 @@ release's per-component sections are labelled beneath its heading. - `archied setup` asks for the PostgreSQL URL. Left blank, it uses the bundled Compose database with a generated password stored in `~/.config/archie/env` as `PGPASSWORD`, not in `config.toml`. - The bundled PostgreSQL listens on 127.0.0.1 only. A database created before this release keeps its old `archie` password; enter its URL at setup or recreate the volume. - The release zip carries `docker-compose.yml`. -- Extensions need unprivileged user namespaces. On Ubuntu 23.10 and later run `sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0` or extensions fail to start. +- Extensions need unprivileged user namespaces, which Ubuntu 23.10 and later restrict. Allow them for the Archie binaries with an AppArmor profile; from the next release the installer prints it. Do not disable the restriction system-wide. - fix(app): assign the temp-file close result on the error path - fix(webhook): set a ReadHeaderTimeout on the forge webhook receivers - fix(forge): refuse a review line number that does not fit an int diff --git a/README.md b/README.md index 997ca5d99..1eed0f318 100644 --- a/README.md +++ b/README.md @@ -117,8 +117,9 @@ or a hosted provider API key. checkout instead. Re-run `archied setup` at any time to change the answers. Extensions run in an unprivileged user namespace. Ubuntu 23.10 and later - block these by default; allow them with - `sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0`. + block these by default; on such a host the installer prints an AppArmor + profile that allows them for the Archie binaries only, to install with + `sudo`. 2. **Check it is up.** diff --git a/docker-compose.yml b/docker-compose.yml index 5769c8b5d..3fede36f5 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -25,9 +25,7 @@ services: image: postgres:18 environment: POSTGRES_USER: archie - # archied setup generates PGPASSWORD into ~/.config/archie/env; the fallback is - # the dev password deployments/dev.toml uses. - POSTGRES_PASSWORD: ${PGPASSWORD:-archie} + POSTGRES_PASSWORD: archie POSTGRES_DB: archie # postgres:18 moved its default PGDATA under a versioned path; pin the # classic location so the named volume keeps working across image updates. diff --git a/install.sh b/install.sh index 9b44aad78..f34d133d2 100755 --- a/install.sh +++ b/install.sh @@ -89,8 +89,8 @@ fi mkdir -p "${ARCHIE_CONFIG_DIR}" "${ARCHIE_DATA_DIR}" "${ARCHIE_BIN_DIR}" -# 2. Binaries. STAGE ends up holding the binaries, release.json, the updater -# scripts and docker-compose.yml, whichever way they were obtained. +# 2. Binaries. STAGE ends up holding the binaries, release.json and the +# updater scripts, whichever way they were obtained. TMP="$(mktemp -d)" trap 'rm -rf "${TMP}"' EXIT @@ -111,7 +111,7 @@ if [ "${FROM_SOURCE}" = true ]; then for cmd in $(jq -er '.required_topology.units | join(" ")' "${SRC_DIR}/release.json"); do (cd "${SRC_DIR}" && go build -ldflags "${LDFLAGS}" -o "${STAGE}/${cmd}" "./cmd/${cmd}") done - cp "${SRC_DIR}/release.json" "${SRC_DIR}/docker-compose.yml" "${SRC_DIR}"/scripts/archie-update-* "${STAGE}/" + cp "${SRC_DIR}/release.json" "${SRC_DIR}"/scripts/archie-update-* "${STAGE}/" else if [ -z "${VERSION}" ]; then tag="$(curl -fsSL "https://api.github.com/repos/${REPO}/releases/latest" | sed -n 's/.*"tag_name": *"\([^"]*\)".*/\1/p')" @@ -138,7 +138,6 @@ for cmd in ${TOPOLOGY_UNITS}; do install -m755 "${STAGE}/${cmd}" "${ARCHIE_BIN_DIR}/${cmd}" done install -m755 "${STAGE}"/archie-update-* "${ARCHIE_BIN_DIR}/" -install -m644 "${STAGE}/docker-compose.yml" "${ARCHIE_DATA_DIR}/docker-compose.yml" echo " Installed ${TOPOLOGY_UNITS} and the updater to ${ARCHIE_BIN_DIR}/" # 3. Configuration. archied setup asks every question and writes config.toml @@ -161,24 +160,66 @@ else echo " [SKIP] ${ARCHIE_CONFIG_DIR}/config.toml already exists. Re-run 'archied setup' to change it." fi -# 4. PostgreSQL. Every service fails closed without it. The default database_url is -# the Compose postgres service, so start it when the config still uses it. -if ! grep -qE '@127\.0\.0\.1:5432/archie' "${ARCHIE_CONFIG_DIR}/config.toml"; then +# 4. PostgreSQL. Every service fails closed without it. The bundled database +# has no network at all: the services reach it through its Unix socket, +# mounted at PG_SOCKET_DIR, so it publishes no port and cannot collide with +# another PostgreSQL. Its password is the PGPASSWORD setup generated. +PG_SOCKET_DIR="${ARCHIE_DATA_DIR}/postgres" +PG_COMPOSE="${ARCHIE_DATA_DIR}/postgres.yml" +if ! grep -qF "host=${PG_SOCKET_DIR}" "${ARCHIE_CONFIG_DIR}/config.toml"; then echo " [OK] Using the PostgreSQL server named by database_url." -elif command -v docker &>/dev/null && [ -f "${ARCHIE_DATA_DIR}/docker-compose.yml" ]; then - echo "==> Starting PostgreSQL 18 (docker compose up -d postgres)..." - touch "${ENV_FILE}" && chmod 600 "${ENV_FILE}" - if ! docker compose --env-file "${ENV_FILE}" -f "${ARCHIE_DATA_DIR}/docker-compose.yml" up -d postgres; then - # Started without their database the services only crash-loop, and if - # something else holds 5432 they hit the wrong server. +elif ! command -v docker &>/dev/null; then + echo " [WARN] Docker not found: the bundled PostgreSQL needs it. Install Docker, or run 'archied setup' with your own PostgreSQL 18 URL." >&2 + AUTO_START=false +else + echo "==> Starting the bundled PostgreSQL 18..." + mkdir -p "${PG_SOCKET_DIR}" + cat >"${PG_COMPOSE}" <<'EOF' +services: + postgres: + image: postgres:18 + network_mode: none + environment: + POSTGRES_USER: archie + POSTGRES_PASSWORD: ${PGPASSWORD:-} + POSTGRES_DB: archie + POSTGRES_INITDB_ARGS: --auth-local=scram-sha-256 + PGDATA: /var/lib/postgresql/data/pgdata + volumes: + - pg_data:/var/lib/postgresql/data + # :z relabels the directory for SELinux hosts. + - ./postgres:/var/run/postgresql:z + restart: unless-stopped + healthcheck: + test: ["CMD-SHELL", "pg_isready -U archie -d archie"] + interval: 5s + timeout: 5s + retries: 10 +volumes: + pg_data: +EOF + if ! docker compose -p archie --env-file "${ENV_FILE}" -f "${PG_COMPOSE}" up -d --wait; then echo " [WARN] The bundled PostgreSQL did not start, so the services are installed but not started." >&2 - echo " If another PostgreSQL holds port 5432, run 'archied setup' and enter its URL, then:" >&2 - echo " systemctl --user enable --now ${TOPOLOGY_UNITS}" >&2 + echo " Check: docker compose -p archie -f ${PG_COMPOSE} logs" >&2 AUTO_START=false fi -else - echo " [WARN] Docker not found: point database_url in ${ARCHIE_CONFIG_DIR}/config.toml at a PostgreSQL 18 server before starting the services." - AUTO_START=false +fi + +# Extensions run in an unprivileged user namespace. Ubuntu 23.10+ forbids +# those unless an AppArmor profile allows them, and only root can add one, so +# print the profile that allows it for these binaries alone. +if [ "$(cat /proc/sys/kernel/apparmor_restrict_unprivileged_userns 2>/dev/null)" = 1 ]; then + echo " [WARN] This host restricts user namespaces, so extensions will not start until you run:" >&2 + cat >&2 </dev/null <<'PROFILE' + abi , + include + profile archie ${ARCHIE_BIN_DIR}/archie{d,-*} flags=(unconfined) { + userns, + } + PROFILE + sudo apparmor_parser -r /etc/apparmor.d/archie +EOF fi # 5. Systemd user service setup & linger configuration diff --git a/internal/infrastructure/configuration/defaults.go b/internal/infrastructure/configuration/defaults.go index 51f162517..6553dced9 100644 --- a/internal/infrastructure/configuration/defaults.go +++ b/internal/infrastructure/configuration/defaults.go @@ -161,6 +161,12 @@ func DefaultWorkDir() string { return filepath.Join(xdgDataHome(), "archie", "work") } +// DefaultPostgresSocketDir is where the bundled PostgreSQL's Unix socket is +// mounted on the host. +func DefaultPostgresSocketDir() string { + return filepath.Join(xdgDataHome(), "archie", "postgres") +} + // DefaultConfigDir returns the directory archie's configuration lives in: // $XDG_CONFIG_HOME/archie, or ~/.config/archie. It is the base every // config-home-derived path is built on. It is not the -config source diff --git a/internal/infrastructure/configuration/setup/setup.go b/internal/infrastructure/configuration/setup/setup.go index 5c7f9a83f..b199ef001 100644 --- a/internal/infrastructure/configuration/setup/setup.go +++ b/internal/infrastructure/configuration/setup/setup.go @@ -10,6 +10,7 @@ import ( "strings" "github.com/samcharles93/archie-core/internal/config" + "github.com/samcharles93/archie-core/internal/infrastructure/configuration" "github.com/samcharles93/archie-core/internal/infrastructure/configuration/tomlwrite" ) @@ -144,9 +145,9 @@ func RunParams(ctx context.Context, p Prompter, discovery ModelDiscovery, secret } // stepDatabase returns the PostgreSQL URL. A blank answer on a fresh install -// means the bundled Compose database with a generated password. The password -// goes only to the env file as PGPASSWORD, which pgx and Compose both read, so -// config.toml holds no secret. +// means the bundled database: no network, reached through its Unix socket, with +// a generated password that goes only to the env file as PGPASSWORD, which pgx +// and Compose both read, so config.toml holds no secret. func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, param string) (string, error) { if strings.TrimSpace(param) != "" { return param, nil @@ -170,5 +171,5 @@ func stepDatabase(ctx context.Context, p Prompter, secrets SecretSink, existing, if err := secrets.Put("env", "PGPASSWORD", password); err != nil { return "", fmt.Errorf("setup: store database password: %w", err) } - return "postgres://archie@127.0.0.1:5432/archie?sslmode=disable", nil + return "postgres://archie@/archie?host=" + configuration.DefaultPostgresSocketDir(), nil } diff --git a/tools/release.sh b/tools/release.sh index ba0ab8db2..ef99ac945 100755 --- a/tools/release.sh +++ b/tools/release.sh @@ -93,7 +93,7 @@ archied_dirs() { printf '%s\n' "cmd/archied" "Dockerfile.archied" "cmd/archie-ui" "internal/app/archieui" \ "Taskfile.yml" ".github/workflows/deploy.yml" "install.sh" \ "scripts/archie-update-install" "scripts/archie-update-check" \ - "scripts/archie-update-watchdog" "deployments/INSTRUCTIONS.md" "docker-compose.yml" + "scripts/archie-update-watchdog" "deployments/INSTRUCTIONS.md" } # The UI Service ships with the archied release on purpose (archie-ui shares