diff --git a/.env.example b/.env.example index 13a2788..08ef96f 100644 --- a/.env.example +++ b/.env.example @@ -217,6 +217,19 @@ PYTHON_PORT=8000 # `docker compose pull jobs && docker compose up -d jobs`. JOBS_IMAGE_TAG= +# --- USACO site (/usaco/) ------------------------------------------------------ +# An image built and published by the usaco repo's CI. Blank runs :latest; set +# a commit sha to pin. Changing it needs +# `docker compose pull usaco && docker compose up -d usaco`. +USACO_IMAGE_TAG= +# Gitea access token (repository: read and write on adamf/usaco). The site +# clones with it and pushes the admin's edits back. +USACO_GIT_TOKEN= +# Shared secret of the usaco repo's push webhook in Gitea +# (http://usaco:8080/usaco/api/hooks/gitea). Blank turns the hook off and +# the site only pulls every 10 minutes. Generate with `openssl rand -hex 32`. +USACO_WEBHOOK_SECRET= + # --- Activity tracker (/tracker/) -------------------------------------------- # An image built and published by the ccp repo's CI. Blank runs :latest; set a # commit sha to pin. Changing it needs diff --git a/.gitignore b/.gitignore index 84744ca..587b050 100644 --- a/.gitignore +++ b/.gitignore @@ -5,6 +5,7 @@ certbot/conf certbot/www backend/token/ jobs-data/ +usaco-data/ .env .env.bak diff --git a/backend/services/env_manager_test.go b/backend/services/env_manager_test.go index 0bb8ed6..5cc4742 100644 --- a/backend/services/env_manager_test.go +++ b/backend/services/env_manager_test.go @@ -110,12 +110,13 @@ func composeServiceEnv(t *testing.T, service string) []string { // explicitEnvServices are the containers given an explicit `environment:` // list instead of the whole .env, so that they never hold the database -// password or the JWT secret: the Python service, and the job and activity -// trackers (images from their own repos, each with its own database role). The +// password or the JWT secret: the Python service, the job and activity +// trackers (images from their own repos, each with its own database role) and +// the USACO site (an image from its own repo, with no database at all). The // cost is a third place a new variable for one of them has to be added — the // registry, .env.example and that list — and forgetting the third is silent: // the variable is simply empty inside the container. -var explicitEnvServices = []string{"python", "jobs", "activity-tracker"} +var explicitEnvServices = []string{"python", "jobs", "activity-tracker", "usaco"} // So the registry's claim "this service reads this" and the compose list must // agree, in both directions: diff --git a/backend/services/env_registry.json b/backend/services/env_registry.json index 4f7de88..dcc25df 100644 --- a/backend/services/env_registry.json +++ b/backend/services/env_registry.json @@ -46,6 +46,7 @@ {"name": "PYTHON_HOST", "group": "Infrastructure", "purpose": "Python container name and nginx upstream", "kind": "text", "editable": false, "lockedReason": "Renames a container and repoints nginx."}, {"name": "PYTHON_PORT", "group": "Infrastructure", "purpose": "Python listen port", "kind": "text", "editable": false, "lockedReason": "Renames a container or repoints nginx."}, {"name": "ACTIVITY_TRACKER_IMAGE_TAG", "group": "Infrastructure", "purpose": "Activity tracker image tag to run (default latest)", "kind": "text", "editable": false, "lockedReason": "Changing it needs a pull: docker compose pull activity-tracker && docker compose up -d activity-tracker."}, + {"name": "USACO_IMAGE_TAG", "group": "Infrastructure", "purpose": "USACO site image tag to run (default latest)", "kind": "text", "editable": false, "lockedReason": "Changing it needs a pull: docker compose pull usaco && docker compose up -d usaco."}, {"name": "JOBS_IMAGE_TAG", "group": "Infrastructure", "purpose": "Job tracker image tag to run (default latest)", "kind": "text", "editable": false, "lockedReason": "Changing it needs a pull: docker compose pull jobs && docker compose up -d jobs."}, {"name": "ICECAST_HOST", "group": "Infrastructure", "purpose": "Icecast container name and nginx upstream", "kind": "text", "editable": false, "lockedReason": "Renames a container and repoints nginx."}, {"name": "ICECAST_PORT", "group": "Infrastructure", "purpose": "Icecast listen port", "kind": "text", "editable": false, "lockedReason": "Renames a container or repoints nginx."}, @@ -69,6 +70,8 @@ {"name": "POSTGRES_TRACKER_DB", "group": "Database", "purpose": "Activity tracker database name", "kind": "text", "editable": false, "lockedReason": "Created by hand on first install; see the ccp README."}, {"name": "POSTGRES_TRACKER_USER", "group": "Database", "purpose": "Activity tracker database role", "kind": "text", "editable": false, "lockedReason": "Created by hand on first install; changing it here locks the activity tracker out."}, {"name": "POSTGRES_TRACKER_PASSWORD", "group": "Database", "purpose": "Activity tracker database password", "kind": "secret", "editable": false, "lockedReason": "Set on the role by hand; changing it here locks the activity tracker out."}, + {"name": "USACO_GIT_TOKEN", "group": "USACO site", "purpose": "Gitea token (repository read and write on adamf/usaco) the site clones and pushes edits with", "kind": "secret", "editable": false, "lockedReason": "Made in Gitea (Settings, Applications); rotate it there, then set it here and run docker compose up -d usaco.", "docsUrl": "https://adam-french.co.uk/gitea/user/settings/applications"}, + {"name": "USACO_WEBHOOK_SECRET", "group": "USACO site", "purpose": "Shared secret of the usaco repo's Gitea push webhook", "kind": "secret", "editable": false, "lockedReason": "Must match the secret on the webhook in Gitea; change both together by hand."}, {"name": "TRACKER_TZ", "group": "Activity tracker", "purpose": "Timezone where the activity tracker's day starts, e.g. Europe/London (blank is UTC)", "kind": "text", "editable": true, "services": ["activity-tracker"]}, {"name": "GITEA_HOST", "group": "Gitea", "purpose": "Gitea container name and nginx upstream", "kind": "text", "editable": false, "lockedReason": "Renames a container and repoints nginx."}, diff --git a/docker-compose.yml b/docker-compose.yml index e1233cf..2464f1e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -474,6 +474,37 @@ services: volumes: - ./jobs-data:/data + # The USACO practice log, served at /usaco/. Built and published by the + # usaco repo's CI (adamf/usaco on this Gitea; the server is in its site/ + # directory), which pulls :latest into this service on every push to main + # that touches the site. Pin USACO_IMAGE_TAG to hold a version. + # + # It has no database: git is its store. It keeps a clone of adamf/usaco in + # ./usaco-data, pulled over the Docker network when Gitea's push webhook + # fires (USACO_WEBHOOK_SECRET signs it), and commits the admin's edits + # (status, link, notes) back with USACO_GIT_TOKEN. ./usaco-data holds only + # that clone, so deleting it is safe; the image runs as uid 1000, so the + # directory must be owned by it. + # + # Same explicit `environment:` list as jobs. Like the tracker it has no + # login: visitors see solved problems, and AUTH_VALIDATE_URL decides who + # is the admin from the visitor's cookies. + usaco: + image: adam-french.co.uk/adamf/usaco:${USACO_IMAGE_TAG:-latest} + container_name: usaco + restart: always + depends_on: + - gitea + networks: + - app-network + environment: + - GIT_REMOTE_URL=http://${GITEA_HOST}:${GITEA_PORT}/adamf/usaco.git + - GIT_TOKEN=${USACO_GIT_TOKEN:-} + - WEBHOOK_SECRET=${USACO_WEBHOOK_SECRET:-} + - AUTH_VALIDATE_URL=http://${BACKEND_HOST}:${BACKEND_PORT}/auth/validate-admin + volumes: + - ./usaco-data:/data + # The activity tracker (time spent per app, site, walk...), served at # /tracker/ with the prefix stripped. Built and published by its own repo # (adamf/ccp on this Gitea), whose CI pulls :latest into this service on @@ -534,6 +565,10 @@ services: - GITEA__server__LFS_JWT_SECRET=${GITEA_LFS_JWT_SECRET} - GITEA__security__INTERNAL_TOKEN=${GITEA_INTERNAL_TOKEN} - GITEA__oauth2__JWT_SECRET=${GITEA_OAUTH2_JWT_SECRET} + # Webhooks are refused to private addresses by default ("external"). + # The usaco site's push hook targets http://usaco:8080 on the Docker + # network, so that one host is allowed by name. + - GITEA__webhook__ALLOWED_HOST_LIST=external,usaco - USER_UID=1000 - USER_GID=1000 restart: always diff --git a/nginx/nginx_routes.conf.template b/nginx/nginx_routes.conf.template index c568d43..1954038 100644 --- a/nginx/nginx_routes.conf.template +++ b/nginx/nginx_routes.conf.template @@ -79,6 +79,8 @@ set $upstream_jobs http://jobs:8080; # Likewise the activity tracker's. set $upstream_tracker http://activity-tracker:8080; + # And the USACO site's. + set $upstream_usaco http://usaco:8080; # ---- Backend API ----------------------------------------------------- # Two patterns repeat through every proxied location below: @@ -365,6 +367,28 @@ proxy_set_header X-Forwarded-Proto $scheme; } + # ---- USACO practice log (/usaco/) ---- + # Its own container (the usaco repo's site/), shaped like /jobs/: SPA + # and API under one prefix that is NOT stripped, and no + # auth_request, because most of it is public and the app checks the + # session itself for the admin's edits. Gitea's push webhook does not + # come through here: it calls the container directly on the Docker + # network. ^~ so no regex location can claim a /usaco/ path. + location = /usaco { + return 301 /usaco/; + } + + location ^~ /usaco/ { + limit_req zone=api burst=20 nodelay; + # An admin edit commits and pushes to Gitea before answering. + proxy_read_timeout 120s; + proxy_pass $upstream_usaco; + proxy_set_header Host $host; + proxy_set_header X-Real-IP $remote_addr; + proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; + proxy_set_header X-Forwarded-Proto $scheme; + } + # ---- Activity tracker (/tracker/) ---- # Its own container (the adamf/ccp repo): an ingest API for device # collectors, which send batches of events with a per-device bearer