Skip to content

Manage challenge repositories, builds and images through the admin API - #9

Merged
CorentinFelten merged 1 commit into
28Pollux28:masterfrom
LounnM:feat/challenge-repositories
Dec 2, 2026
Merged

CorentinFelten merged 1 commit into
28Pollux28:masterfrom
LounnM:feat/challenge-repositories

Conversation

@LounnM

@LounnM LounnM commented Nov 26, 2026

Copy link
Copy Markdown
Contributor

Manage challenge repositories, builds and images through the admin API

Replaces the challenge tooling of ctfd-management (challenges.sh) on the Galvanize side: Galvanize owns challenge_dir, and a CTF platform (Zync) manages challenges through the admin API, without access to the server. Galvanize stays independent of CTFd and can run on its own machine.

What's new

  • Repositories – GET/POST /admin/repos, POST /admin/repos/{name}/pull, DELETE /admin/repos/{name}: list, clone (only from repositories.sources), update (fetch + reset to the followed branch or tag), remove (refused while instances of its challenges exist). The challenge index is reloaded afterwards. A pull names the local changes it discards; a repository that does not exist, or is private without a token, gives a clear error instead of git's askpass message.
  • Definitions and files – GET /admin/challenges lists every definition (any type) with its content, build plan (buildable, refused + reason), declared files resolved (files[]), and the repository commit (404 for an unknown repository); GET /admin/challenges/file serves a file or its SHA-1.
  • Builds and images – POST /admin/build builds on every Ansible host (docker build, or docker compose build for the services with a build section) from an archive of the challenge folder; POST /admin/images checks or removes the built images, with the state of each image on each host (unreachable or error for a host the check could not run on, never left out). Updating a repository builds nothing. Challenges are referenced by definition path ({path}) or {category, name}.
  • Operations – long actions run in the background, one at a time, with logs and cancellation (/admin/operations; 409 for a finished operation). Cancelling a build or image action really stops it: ansible-playbook gets SIGTERM, every local process of the run is killed 15 s later, and a stop playbook ends what the run started on the hosts (commands marked with GALVANIZE_RUN_ID) and removes its build folder. The operation ends, and releases the lock, only once all of it has stopped; an image is never retagged after a cancellation. A panic in a run or a build fails it instead of the server.
  • New config sections: repositories (sources, git_timeout), build (concurrency, timeout, protected_images). The Docker image includes git and git-lfs.

Security

  • Clone URLs are compared by scheme, host, port and path segments (no string prefix); url_prefix must end with /; credentials, queries, percent-encoding and ./.. segments are refused. A source token is https only, sent as a header scoped to the source, never written to the clone; redirects are not followed with a token. Git runs without hooks, fsmonitor or the ext transport.
  • Declared files and compose files must stay inside the challenge folder (symlinks resolved); .git is never served; Git LFS pointers are reported.
  • Builds: compose build sections are allowlisted (no host network, secrets, SSH, extra contexts, host caches or extra tags); image, service and Dockerfile names are validated; a challenge may not build an image another challenge folder deploys, builds or builds FROM, nor a protected image; extra vars of the build playbooks are marked unsafe, so Jinja in a name is never rendered (verified with Ansible).
  • The new endpoints need an admin token with scope: manage; every token must now have exp.

Fix

  • The Ansible SSH ControlPath used %%h, which reaches ssh as a literal %h: every host of a run shared the first host's control socket. Now %C, for deployments too.
  • Config hot reload: a file changed in several writes (cp truncates it first) could be applied half written, leaving Galvanize with an empty config until a restart. The change is now applied once the file has been quiet for 500 ms, and a config without challenge_dir or ansible_dir is refused (the current one is kept). Reproduced and verified on the test VM.

Behaviour changes (see CHANGELOG)

  • Tokens without exp are rejected.
  • Hidden folders of challenge_dir are no longer indexed.
  • A built compose service without image is deployed as galvanize/<slugs>-<hash>:latest.
  • compose_file must stay inside the challenge folder; a definition JSON cannot encode is skipped.

Tests

  • make test: 245 passed on macOS, 247 passed on Linux with -race (six runs, no race); golangci-lint: 0 issues; make api-gen reproduces api.gen.go.
  • Cancellation checked on the test VM: docker build --no-cache, slow docker compose build, rebuild + ingest cancelled from Zync, and docker stop of Galvanize during a build: stopped in under 2 s, no process, build folder or zombie left on the host or in the container, image not retagged. Deployments behave exactly as before this PR.
  • End-to-end with Zync against a real repository (PolyPwnCTF-2026-Challenges, 56 definitions), Galvanize on a separate VM from CTFd: clone, build (11 built; 5 amd64-only challenges fail on the arm64 test VM and are not ingested), ingest, sync, player deployment, guards, delete, image and repository removal – 0 failure (43 checks).

Not covered / follow-ups

  • Not tested with several Ansible hosts.
  • deploy_parameters still reach the deployment playbooks as Jinja-rendered extra vars (pre-existing; same class as the build issue fixed here).
  • ctfd-management pins galvanize:0.7.5: a release is needed before it can use this.
  • Kept out of this PR, for separate PRs: stopping cancelled deployments on the hosts, a maximum lifetime for manage tokens, warnings on unknown keys of challenge definitions (e.g. file: for files:).

…min API

Galvanize owns challenge_dir: CTF platforms (Zync) manage challenges
through the admin API instead of scripts on the server, and Galvanize can
run on its own machine.

- Repositories: list, clone (only from repositories.sources, compared by
  scheme, host, port and path segments; optional per-source token sent as a
  header scoped to the source), pull (fetch + reset to the followed ref),
  remove (refused while instances exist). Git runs without hooks, fsmonitor
  or the ext transport.
- GET /admin/challenges lists every definition with its build plan, its
  declared files resolved (challenge folder, then files/; absolute paths,
  "..", .git, symlinks leading outside and Git LFS pointers refused) and
  the repository commit. GET /admin/challenges/file serves a file or its
  SHA-1. Definitions JSON cannot encode are skipped.
- POST /admin/build builds images on every Ansible host (docker build,
  docker compose build) from an archive of the challenge folder;
  POST /admin/images checks or removes them, reporting unreachable hosts.
  Compose build sections are allowlisted, names validated, and a
  challenge may not build an image another challenge folder uses or a
  protected one (build.protected_images).
  Extra vars of the build playbooks are marked unsafe for Ansible.
- Background operations with logs and cancellation (/admin/operations).
  Cancelling stops the run: ansible-playbook gets SIGTERM, then every
  local process of the run is killed after 15 s, and a stop playbook
  ends what the run started on the hosts (marked by GALVANIZE_RUN_ID)
  and removes its build folder; the operation ends, and releases the
  lock, once all of it has stopped. A panic in a run or a build fails it
  instead of the server. Cancelling a finished operation answers 409.
- GET /admin/challenges answers 404 for an unknown repository. A pull
  names the local changes it discards; a clone or pull answered with a
  credentials prompt says the repository does not exist or is private.
- These endpoints need an admin token with the "manage" scope; every token
  must have an expiry.
- Built compose services get unique tags (slugs + hash).

Fixes the SSH ControlPath of Ansible runs: "%%h" reached ssh as a literal
"%h", so all hosts of a run shared one control socket.

Fixes config hot reloads: a file changed in several writes (cp truncates
it first) could be applied half written, leaving an empty config until a
restart. Changes are applied once the file is quiet for 500 ms, and a
config without challenge_dir or ansible_dir is refused.
@CorentinFelten
CorentinFelten merged commit e3ae270 into 28Pollux28:master Dec 2, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants