diff --git a/.gitea/workflows/deploy.yml b/.gitea/workflows/deploy.yml index 796f2b8..91bd4ba 100644 --- a/.gitea/workflows/deploy.yml +++ b/.gitea/workflows/deploy.yml @@ -40,5 +40,8 @@ jobs: if: steps.checkout.outputs.skip != '1' env: APP_PORT: ${{ vars.APP_PORT }} + EDITOR_TOTP_SECRET: ${{ secrets.EDITOR_TOTP_SECRET }} + EDITOR_SESSION_SECRET: ${{ secrets.EDITOR_SESSION_SECRET }} + EDITOR_ORIGIN: http://8.211.154.134:18231 REFRESH_DATA: ${{ github.event_name == 'schedule' || github.event.inputs.refresh_data == 'true' }} run: sh scripts/deploy.sh "$GITHUB_SHA" diff --git a/README.md b/README.md index 99039e9..392d624 100644 --- a/README.md +++ b/README.md @@ -95,3 +95,9 @@ TLS and the editor secrets before using editorial login over the internet. The authenticator QR is provisioned once and delivered privately in chat, never committed. TOTP-only login is a single possession factor, as requested. + +The deployment job can provision `EDITOR_TOTP_SECRET` and `EDITOR_SESSION_SECRET` +from repository Actions secrets after explicit operator authorization. It writes +them atomically to the existing environment file with mode 0600. Missing secrets +preserve the server configuration. Update `EDITOR_ORIGIN` in the deployment job +when moving editorial access to a TLS domain. diff --git a/scripts/configure-editor.py b/scripts/configure-editor.py new file mode 100644 index 0000000..a437259 --- /dev/null +++ b/scripts/configure-editor.py @@ -0,0 +1,32 @@ +"""Runner-only: persist optional Actions editor secrets without logging values.""" +import os +from pathlib import Path +import re +import sys +import tempfile + +path = Path(sys.argv[1]) +values = {key: os.environ.get(key, '') for key in + ('EDITOR_TOTP_SECRET', 'EDITOR_SESSION_SECRET', 'EDITOR_ORIGIN')} +# Missing Actions configuration preserves the current server configuration. +if values['EDITOR_TOTP_SECRET'] and values['EDITOR_SESSION_SECRET']: + if not re.fullmatch(r'[A-Z2-7]{32,}', values['EDITOR_TOTP_SECRET']): + raise SystemExit('Invalid editor authenticator configuration') + if not re.fullmatch(r'[A-Za-z0-9_-]{32,}', values['EDITOR_SESSION_SECRET']): + raise SystemExit('Invalid editor session configuration') + if not values['EDITOR_ORIGIN']: + raise SystemExit('Editor origin is required when provisioning secrets') + if not re.fullmatch(r'https?://[A-Za-z0-9.:-]+', values['EDITOR_ORIGIN']): + raise SystemExit('Invalid editor origin') + lines = [line for line in path.read_text().splitlines() + if not any(line.startswith(key + '=') for key in values)] + lines += [f'{key}={value}' for key, value in values.items()] + fd, temp = tempfile.mkstemp(dir=path.parent, prefix='.editor-env-') + try: + with os.fdopen(fd, 'w') as output: + output.write('\n'.join(lines) + '\n') + os.chmod(temp, 0o600) + os.replace(temp, path) + finally: + if os.path.exists(temp): + os.unlink(temp) diff --git a/scripts/deploy.sh b/scripts/deploy.sh index fc81ab1..e5e4fc8 100644 --- a/scripts/deploy.sh +++ b/scripts/deploy.sh @@ -41,7 +41,7 @@ if [ ! -s "$ENVF" ]; then umask 077 printf 'DATABASE_URL=postgres://%s:%s@127.0.0.1:%s/%s\n' "$DB" "$PW" "$PGPORT" "$DB" > "$ENVF" fi -PW=$(sed -E 's#^DATABASE_URL=postgres://[^:]+:([^@]+)@.*#\1#' "$ENVF") +PW=$(sed -nE 's#^DATABASE_URL=postgres://[^:]+:([^@]+)@.*#\1#p' "$ENVF") # keep the role password and the env file in sync; create role/db only when missing if [ "$(psql_admin "-c \"select 1 from pg_roles where rolname='$DB'\"")" = 1 ]; then psql_admin "-c \"alter role $DB with login password '$PW'\"" >/dev/null @@ -50,6 +50,7 @@ else fi [ "$(psql_admin "-c \"select 1 from pg_database where datname='$DB'\"")" = 1 ] || psql_admin "-c \"create database $DB owner $DB\"" >/dev/null chmod 600 "$ENVF" +python3 scripts/configure-editor.py "$ENVF" set -a; . "$ENVF"; set +a PGPASSWORD=$PW psql -h 127.0.0.1 -p "$PGPORT" -U "$DB" -d "$DB" -qtAc 'select 1' >/dev/null || { echo "cannot log in to postgres as $DB" >&2; exit 1; }