Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.DS_Store
.vscode/
.idea/
*.sqlite
Expand Down
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,26 @@

## vX.X.X (YYYY-MM-DD)

### Added
- Challenge repository management through the admin API: list, clone (only from the URL prefixes of the new `repositories.sources`, with optional per-source tokens kept out of the clones), update (fetch and reset to the followed branch or tag, naming the local changes it discards; a clone or update the server answers with a credentials prompt fails with "the repository does not exist, or it is private and its source in repositories.sources has no token able to read it" instead of git's askpass message) and remove (refused while instances of its challenges exist) the top-level folders of `challenge_dir`, reloading the challenge index afterwards (an update builds nothing: builds are explicit)
- `GET /admin/challenges` lists every challenge definition, whatever its type, with its full content, its repository's `commit`, its build plan (`build.buildable`, `build.refused`), its declared `files` resolved in the challenge folder then its `files/` subfolder (`{declared, path, size, error}`; Git LFS pointers are errors) and the definitions that could not be read (404 for an unknown `repo`); `GET /admin/challenges/file` serves the files of challenge folders (or their SHA-1), refusing `.git` and paths leading outside the challenge folder. A CTF platform can import challenges without access to the server
- `POST /admin/build` builds challenge images on every Ansible host (BuildKit, Docker Compose), `POST /admin/images` checks or removes them (state of each image per host; `unreachable` or `error` on a host the check could not run on, never left out); settings in the new `build` section. Challenges are named by their definition path (`{path}`, or `{category, name}`) in requests, and by definition path in results (`built`, `failed` with refused builds, `skipped`; `challenges` of an image action)
- Long admin actions run as background operations, one at a time, with their log: `GET /admin/operations`, `GET /admin/operations/{id}`, `POST /admin/operations/{id}/cancel` (409 for a finished operation); an operation is reported finished once the next one can start. A cancelled build or image action stops ansible-playbook (SIGTERM, which stops its workers; 15 s later, every local process of the run is killed, on Linux), then what it started on the hosts (processes marked with the run's `GALVANIZE_RUN_ID`, build folder), within 55 s, before the operation ends and the next one can start; a cancelled run is reported cancelled even when ansible-playbook exited 0, and a panic in a build fails that build only
- The Docker image includes git

### Changed
- A Compose service with a `build` section but no `image` is deployed as `galvanize/<category>-<challenge>-<service>-<hash>:latest`, the name the build gives it: such services could not be deployed before (the build context does not exist on the host)
- Hidden folders of `challenge_dir` (`.github`, temporary clones...) are no longer indexed: a challenge in a folder whose name starts with `.` disappears from the index
- A challenge deployed from `deploy_parameters.image` without a Dockerfile in its folder is taken to use an image from a registry: building it does nothing (ctfd-management's `challenges.sh` failed its build)
- Every JWT must have an expiry (`exp`); repository, build, image, challenge-file and operation endpoints also need the `manage` scope
- `deploy_parameters.compose_file`, and an auto-detected Compose file, must stay inside the challenge folder (symlinks included), also next to an inline `compose_definition`; otherwise the definition is skipped
- `build.protected_images` defaults no longer include `ctfd/ctfd`: list the CTF platform's images in the setting
- A challenge definition JSON cannot encode (`.nan`, keys that are not strings once converted) is skipped instead of breaking `GET /admin/challenges`

### Fixed
- Ansible's SSH control socket was shared by all the hosts of a run (`%%h` reaches ssh as a literal `%h`), so with several hosts a task could run on the wrong one
- A config file changed in several writes (`cp` truncates it first) could be reloaded half written, leaving Galvanize with an empty config until a restart: the change is now applied once the file has not changed for 500 ms, and a reloaded config without `challenge_dir` or `ansible_dir` is refused (the current one is kept)

## v0.7.5 (2026-11-11)

### Added
Expand Down
50 changes: 50 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,56 @@ The API is documented in `galvanize-instancer/api/openapi.yaml`. Main endpoints:

All endpoints except `/health` require a JWT bearer token. Tokens are generated automatically by the Zync plugin.

Admin endpoints (role `admin`) also manage the challenges themselves, see [Challenge repositories](#challenge-repositories). Every token must carry an expiry (`exp`).

## Challenge repositories

Galvanize owns its challenge folder (`challenge_dir`). Each top-level folder of it is a repository: a git clone made through the API, or a folder copied there by hand. The admin API lets any client (Zync, a script, curl) manage them without access to the server.

These endpoints need an admin token whose `scope` claim is `manage`. The CTF platform's server puts it in the tokens it uses for its own calls; the admin tokens a dashboard hands to browsers must not have it, so that they can manage instances but not clone repositories, run builds on the hosts or read challenge files.

| Endpoint | What it does |
|---|---|
| `GET /admin/repos` | Repositories with their git state (remote, branch or tag, commit, local changes), and the allowed clone sources |
| `POST /admin/repos` | Clone `{url, name?, ref?, force?}`. The URL must be inside one of `repositories.sources` (cloning is disabled without any). `force` replaces an existing folder once the new clone succeeded |
| `POST /admin/repos/{name}/pull` | Fetch the branch or tag the repository follows and reset the folder to it (local changes are discarded). Images are not rebuilt: build them with `/admin/build` |
| `DELETE /admin/repos/{name}` | Delete the folder. Refused while instances of its challenges exist |
| `GET /admin/challenges[?repo=]` | Every challenge definition, whatever its type, with its full content, its repository's `commit`, its declared `files` and what building it means (`build`); definitions that could not be read are listed in `skipped` |
| `GET /admin/challenges/file?path=[&checksum=true]` | A file of a challenge folder (or its SHA-1 and size). Paths into `.git`, or leading outside the challenge folder, are refused |
| `POST /admin/build` | Build images `{repo?, challenges?, no_cache?}` on every Ansible host. The result lists the definition paths `built`, `failed` (refused builds included) and `skipped` (nothing to build) |
| `POST /admin/images` | `{action: status\|remove, repo?, challenges?}`: which images exist on each host, or remove the built ones (pulled images, images another challenge folder uses and platform images are never removed; refused while instances use them). The result maps each image to its state per host (`images`: `present`, `absent`, `removed`, `failed` when the removal failed, or `unreachable`/`error` on a host that did not answer or where the check failed) and each definition path to its images (`challenges`) |
| `GET /admin/operations[/{id}?offset=]` | Clones, pulls, removals, builds and image actions run in the background, one at a time: their status, result and log (from line `offset`) |
| `POST /admin/operations/{id}/cancel` | Cancel a running operation |

After a clone, pull or removal, the challenge index is reloaded.

Challenges are named by their definition file, relative to `challenge_dir` (`path` in the listing): `challenges` of a build or image request is a list of `{path}` (or `{category, name}`), and results use these paths.

**Declared files.** Each entry of a definition's `files` list is looked up in the challenge folder, then in its `files/` subfolder. The listing gives, for each one, `{declared, path, size}` (`path` to download with `/admin/challenges/file`), or an `error`: not found, absolute path, path leaving the challenge folder (symlinks included) or going through `.git`, or a Git LFS pointer (the repository was cloned without `git-lfs`). The listing does not hash files: ask `checksum=true` for the files whose SHA-1 you need.

**Commit.** `commit` is the repository's current commit: a client comparing it before and after a long task knows whether the repository changed in between (e.g. a pull).

**Sources.** A `url_prefix` is a URL ending with `/` (`https://github.com/your-org/`). A clone URL is allowed when it has the same scheme, host and port and its path continues the prefix's folders: `https://github.com/your-org-evil/x`, `https://github.com.evil.tld/your-org/x` and `https://github.com/your-org/../other/x` are refused, and so are URLs with credentials, a query, percent-encoding or `.`/`..` segments. Schemes: `https`, `http`, `ssh`, `git` and `file` (`git://` and `http://` are unauthenticated and unencrypted: keep them for a lab network).

**Credentials.** For a private repository, set `token` on its `https` source in `config.yaml`. Git receives it through its environment for each command, as a header scoped to the source's URL (an LFS server named by a repository's `.lfsconfig` does not get it, and redirects are not followed); it never appears in a URL, a command line, the API or the clone's `.git/config`. Clients cannot pass credentials: a URL holding some is refused. Git runs without hooks or fsmonitor.

**Builds** run where the instances run: Galvanize sends the challenge folder (without `.git`, and without symlinks leading outside it) to every Ansible host and runs Docker there, so builds use BuildKit and Compose and a deployment finds its images locally (the playbooks deploy with `pull: missing`):

- a challenge deployed from one image (`http`, `tcp`...) is built from the first `Dockerfile*` of its folder (`Dockerfile` preferred), tagged `deploy_parameters.image`. Without a Dockerfile, the image is taken to come from a registry (pulled at deployment) and there is nothing to build;
- a `custom_compose` challenge has its services with a `build` section built, from its Compose file when there is one (`deploy_parameters.compose_file`, else `compose.yaml`, `compose.yml`, `docker-compose.yaml` or `docker-compose.yml`; it must stay inside the challenge folder, symlinks included), even with an inline `compose_definition`, which usually references the images the file builds; otherwise from the inline definition. When both exist, a service must have the same image in both, or the build is refused. A built service without `image` is tagged `galvanize/<category>-<challenge>-<service>-<hash>:latest` (the hash of the exact names keeps two challenges apart even when their names give the same slug), and the index gives it the same name for deployments.

What a repository may build is limited, since anyone who can push to it decides what runs on the hosts:

- a `build` section may only use `context`, `dockerfile`, `dockerfile_inline`, `args`, `target`, `labels`, `shm_size`, `no_cache`, `pull` and `platforms`; contexts and Dockerfiles are relative paths inside the challenge folder (no host network, secrets, SSH agent, extra contexts, host caches or extra tags);
- image, service and Dockerfile names must be plain names;
- a challenge may not build an image another challenge folder deploys, builds or builds `FROM` (challenges of one folder share theirs), nor a platform image (`build.protected_images`; by default Galvanize's images, Traefik, Redis, MariaDB, MySQL and PostgreSQL: list your CTF platform's images there too, the list replaces the defaults). A local image of that name would replace the real one for everyone: deployments use `pull: missing`.

The listing shows refused builds with `build.refused` and the reason, and `build.buildable` tells whether building the challenge builds anything (it is deployable and its plan is `image` or `compose`). A build request may name challenges with nothing to build or refused: they come back as `skipped` or `failed`. Values sent to the build playbooks are marked unsafe for Ansible, so a name holding `{{ ... }}` is never rendered.

The build and image playbooks are part of the binary (they do not need to be in `ansible_dir`).

**Trust.** Cloning a repository means trusting everyone who can push to it: its challenges' Dockerfiles run on every Ansible host, and their deployment parameters reach the deployment playbooks. Allow only repositories whose writers you trust as much as the hosts' administrators, and keep Galvanize's API off the Internet when you can (only the CTF platform's server needs it for these endpoints).

## Challenge Definitions

Example challenge files (one per playbook type):
Expand Down
18 changes: 18 additions & 0 deletions config.example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,24 @@ instancer:
cpus: "1" # CPU limit per container (e.g. "0.5", "1", "2")
memory: "512M" # Memory limit per container (e.g. "256M", "512M", "1G")
pids_limit: 256 # Max number of PIDs per container (fork bomb protection)
repositories: # Challenge repositories managed through the admin API (/admin/repos)
# Clones are allowed only from URLs inside one of these prefixes (same
# scheme, host and port, path below the prefix's folder; the prefix must
# end with "/"). With no source, cloning is disabled (repositories already
# in challenge_dir can still be listed, updated and removed).
sources:
- url_prefix: "https://github.com/your-org/"
# token: "" # optional, https only, for private repositories (read-only token); never written to the clone
# username: "x-access-token" # HTTP user sent with the token (default: x-access-token)
git_timeout: "10m" # Time limit of one git command (default: 10m)
build: # Image builds (/admin/build), run with Docker on every Ansible host
concurrency: 2 # Challenges built at the same time (default: 2)
timeout: "30m" # Time limit of one challenge build (default: 30m)
# Images challenges may never build (repository, any tag; or a prefix
# ending with "/"). Unset: traefik, mariadb, mysql, redis, postgres and
# ghcr.io/28pollux28/. Setting it replaces the defaults: list them with
# your CTF platform's images.
# protected_images: ["traefik", "mariadb", "mysql", "redis", "postgres", "ghcr.io/28pollux28/", "ctfd/ctfd"]
extra_deployment_parameters: # Additional parameters for deployment (added to vars in Ansible)
# Name of the external Docker network Traefik watches on the target host(s).
# Required for http challenges and for http exposures in compose challenges.
Expand Down
6 changes: 4 additions & 2 deletions galvanize-instancer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,11 @@ LABEL authors="28Pollux28"

RUN useradd -ms /bin/bash galvanize

# Install curl to allow health checks, ansible, and tini for zombie process reaping
RUN apt-get update && apt-get install -y curl pipx ssh-client tini \
# Install curl to allow health checks, ansible, tini for zombie process
# reaping, and git with Git LFS for the challenge repositories (admin API)
RUN apt-get update && apt-get install -y curl git git-lfs pipx ssh-client tini \
&& rm -rf /var/lib/apt/lists/* \
&& git lfs install --system --skip-repo \
&& pipx ensurepath --global \
&& pipx install --include-deps --global ansible \
&& ansible-galaxy collection install community.docker -p /usr/share/ansible/collections
Expand Down
Loading
Loading