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
20 changes: 20 additions & 0 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
name: docs

on:
pull_request:
paths:
- docs/**
- "*.md"
- .github/workflows/docs.yml
schedule:
- cron: "41 5 * * 1"
workflow_dispatch:

permissions:
contents: read

jobs:
docs:
uses: phorge-fr/.github/.github/workflows/docs-lint.yml@main
with:
external-links: ${{ github.event_name != 'pull_request' }}
8 changes: 4 additions & 4 deletions AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ The Phorge API: a Go REST API that manages tenants and their VMs by creating, re
The contract and the plan are in Hangar. Read both pages in full:

```bash
gh api repos/phorge-fr/Hangar/contents/docs/phorge-api.md -H 'Accept: application/vnd.github.raw'
gh api repos/phorge-fr/Hangar/contents/docs/phorge-api-implementation.md -H 'Accept: application/vnd.github.raw'
gh api repos/phorge-fr/Hangar/contents/docs/reference/phorge-api-kinds.md -H 'Accept: application/vnd.github.raw'
gh api repos/phorge-fr/Hangar/contents/docs/explanation/phorge-api-implementation-plan.md -H 'Accept: application/vnd.github.raw'
```

`phorge-api.md` describes the five kinds. `phorge-api-implementation.md` is the plan: how to reach the platform from your machine, how authentication and authorization work, what the platform does (and which behaviours to build around), the order of work, and the rules. The XRDs in `overlays/control/controllers/crossplane/` of Hangar are the source of truth for field names and limits; if a page contradicts the cluster, the cluster is right.
`phorge-api-kinds.md` describes the five kinds. `phorge-api-implementation-plan.md` is the plan: how to reach the platform from your machine, how authentication and authorization work, what the platform does (and which behaviours to build around), the order of work, and the rules. The XRDs in `overlays/control/controllers/crossplane/` of Hangar are the source of truth for field names and limits; if a page contradicts the cluster, the cluster is right.

## Workflow

Expand All @@ -38,5 +38,5 @@ The Go version is the one in `go.mod`.
- Never print, log or commit a secret, a key or a token. Configuration comes from the environment.
- **Do not change the platform by hand.** An XRD, a composition, the OpenFGA model, RBAC, a firewall rule or an Authentik blueprint is a pull request on Hangar (or Network), never an edit on the cluster: Flux reverts live edits. Use a `git worktree` for the Hangar change if its checkout holds someone else's branch.
- Test tenants are named `apitest-<run>-*`. Never touch `e2e-*` tenants (the platform suite's), the Incus projects `ct-poc` and `default`, or a tenant you did not create. Clean up what you create: children first, then the tenant, then wait until the Incus project is gone.
- API keys are personal: `phk_<id>_<secret>`, owned by a user, acting as that user within scopes, expiring within a year; only a person with an Authentik token manages keys, and a key cannot create keys. See Hangar `docs/decisions/0007-tenant-policy.md`.
- API keys are personal: `phk_<id>_<secret>`, owned by a user, acting as that user within scopes, expiring within a year; only a person with an Authentik token manages keys, and a key cannot create keys. See Hangar `docs/decisions/0007-tenant-policy.md`. This repository's own pages are in [docs/](docs/README.md).
- When something is missing or ambiguous (public host names, who may create tenants), ask; do not invent.
144 changes: 6 additions & 138 deletions README.md

Large diffs are not rendered by default.

28 changes: 28 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# phorge-api documentation

Pages are grouped by what the reader wants, following [the organization's documentation standard](https://github.com/phorge-fr/.github/blob/main/docs/reference/documentation-standard.md). The kinds the API manages are documented in Hangar: [Phorge API kinds](https://github.com/phorge-fr/Hangar/blob/main/docs/reference/phorge-api-kinds.md).

## How-to guides

| Page | Task |
| --- | --- |
| [Get a token with the device flow](how-to/get-a-token-with-the-device-flow.md) | Call the API from a terminal with your identity |
| [Run the integration tests](how-to/run-the-integration-tests.md) | Test against the real cluster and OpenFGA store |
| [Release the API](how-to/release-the-api.md) | Cut a version with release-please |

## Reference

| Page | Content |
| --- | --- |
| [Configuration](reference/configuration.md) | Environment variables |
| [Endpoints](reference/endpoints.md) | Routes |
| [Authentication](reference/authentication.md) | Accepted credentials |
| [Tenants](reference/tenants.md) | Behavior of the tenant routes |
| [Images and instances](reference/images-and-instances.md) | Behavior of the image and instance routes |
| [Memberships and API keys](reference/memberships-and-api-keys.md) | Roles and personal keys |

## Explanation

| Page | Question it answers |
| --- | --- |
| [How the API authorizes a request](explanation/authorization.md) | How OpenFGA and key scopes decide |
23 changes: 23 additions & 0 deletions docs/explanation/authorization.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
type: explanation
audience: developer who needs to understand how the API decides what a caller may do
verified: 2026-10-28
---

# How the API authorizes a request

Authentication tells who calls, OpenFGA decides what they may do (`internal/authz`). Every decision is a `Check` in the `phorge-api` store on the object `tenant:<name>`, for the subject of the identity (`user:<email>`, which is also the owner of a key):

| Action | Relation | Scope of a key |
|---|---|---|
| read the tenant | `can_read_tenant` | `tenants:read` |
| read instances, images | `can_read_instances`, `can_read_images` | `instances:read`, `images:read` |
| write instances | `can_write_instances` | `instances:write` |
| write images | `can_write_images` | `images:write` |
| read, write memberships and keys | `can_read_memberships`, `can_write_memberships` | `memberships:read`, `memberships:write` |

A user gets those through a role (`admin` over `operator` over `user` over `viewer`). A key does what its owner may **and** what its scopes name: the scope is checked first (`instances:write` does not give `instances:read`), then OpenFGA is asked about the owner. `Authorizer.Require(action, ...)` is the middleware of a route: `404` when the caller cannot read the tenant (a tenant that is not yours is not shown to exist), `403` when it can but may not do this, `503` when OpenFGA cannot answer. A decision that cannot be taken is never an "allowed". `Authorizer.PlatformAdmin` tells who creates, resizes and deletes tenants: the members of the Authentik group `PLATFORM_ADMIN_GROUP` (the model has no relation for it). A token carries its groups; a key does not, so the API asks Authentik whether its owner is a member of that group (`internal/authentik`, `AUTHENTIK_URL` and `AUTHENTIK_TOKEN`: the token of a service account that may read that one group and nothing else, the members remembered for `AUTHENTIK_CACHE_TTL`; if the group cannot be read, that is an error, never "nobody is in it"): a key stops being an administrator's within `AUTHENTIK_CACHE_TTL` of its owner leaving the group. The scopes `tenants:read` and `tenants:write` gate what a platform administrator's key does on tenants. `GET /v1/whoami` says whether the caller may write tenants. Members are added in Authentik, the group itself is created by the blueprint in Hangar.

OpenFGA is reached with `OPENFGA_URL`, `OPENFGA_API_KEY` and `OPENFGA_STORE_ID`, and `CA_FILE` for the internal CA of `openfga.core.phorge` (the verification is never turned off).

To run the tests against the real model, see [Run the integration tests](../how-to/run-the-integration-tests.md).
28 changes: 28 additions & 0 deletions docs/how-to/get-a-token-with-the-device-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
type: how-to
audience: developer who needs an Authentik access token from a terminal
verified: 2026-10-28
---

# Get a token with the device flow

Use this to call the API from a terminal with your own Authentik identity.

## Steps

```bash
# a token from a terminal, with the device code flow of Authentik
curl -s https://auth.phorge.fr/application/o/device/ -d client_id=<OIDC_AUDIENCE> -d 'scope=openid email profile offline_access'
# approve verification_uri_complete in a browser, then poll /application/o/token/ with
# grant_type=urn:ietf:params:oauth:grant-type:device_code and --data-urlencode "device_code=..."
# (the code holds characters that `curl -d` does not encode, Authentik then answers invalid_grant for ever;
# while you have not approved it answers authorization_pending)
```

## Check that it worked

Call `GET /v1/whoami` with `Authorization: Bearer <access_token>`; it names you. Authentik answers `authorization_pending` until you approve in the browser.

## Related

- [Authentication](../reference/authentication.md)
13 changes: 13 additions & 0 deletions docs/how-to/release-the-api.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
type: how-to
audience: maintainer who releases a new version of the API
verified: 2026-10-28
---

# Release the API

Releases are cut by [release-please](https://github.com/googleapis/release-please) from the commit messages (Conventional Commits). On every push to `main` it keeps one pull request open, "chore(main): release x.y.z", that bumps `.release-please-manifest.json` and writes `CHANGELOG.md`. Merging that pull request is the release: it creates the tag and the GitHub release, and the tag builds and pushes the image `ghcr.io/phorge-fr/phorge-api:x.y.z`. Nothing is released until you merge it.

- `fix:` gives a patch, `feat:` a minor (while the version is below 1.0.0, a breaking change `feat!:` also gives a minor), `chore:`, `docs:`, `test:` and `ci:` give nothing.
- Hangar pins the image as `<tag>@<digest>`: Renovate opens the pull request there when a release is published.
- The workflow uses the `RENOVATE_TOKEN` secret, because pull requests and tags created with `GITHUB_TOKEN` start no workflow, so the checks of the release pull request and the image build would never run.
52 changes: 52 additions & 0 deletions docs/how-to/run-the-integration-tests.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
---
type: how-to
audience: developer who tests the API against the real platform
verified: 2026-10-28
---

# Run the integration tests

These tests need the real cluster and the OpenFGA store, and are skipped unless you give them. The tenant tests name their tenant `apitest-api-<random>` and delete it, whatever happens.

## Before you start

- A kubeconfig with the `core` and `control` contexts, and permission to read the `phorge-api-openfga` secret on `control`.
- Never touch `e2e-*` tenants, which belong to the platform suite.

## Authorization against the real model

```bash
kubectl --context core -n openfga port-forward svc/openfga 18080:8080 &
export PHORGE_FGA_URL=http://127.0.0.1:18080 PHORGE_FGA_STORE=01M50R0P12MY8JHNKP3ATYSJC2
export PHORGE_FGA_KEY=$(kubectl --context control -n phorge-api get secret phorge-api-openfga -o jsonpath='{.data.apiKey}' | base64 -d)
go test -run TestAgainstTheRealModel ./internal/authz
```

## The life of a tenant (about five minutes)

```bash
kubectl --context core -n openfga port-forward svc/openfga 18080:8080 &
export PHORGE_KUBE_CONTEXT=control PHORGE_FGA_URL=http://127.0.0.1:18080 PHORGE_FGA_STORE=01M50R0P12MY8JHNKP3ATYSJC2
export PHORGE_FGA_KEY=$(kubectl --context control -n phorge-api get secret phorge-api-openfga -o jsonpath='{.data.apiKey}' | base64 -d)
go test -run TestTenantLifecycle -timeout 20m ./internal/api
```

## Images and instances (ten to twenty minutes)

Set the variables of the previous section, then:

```bash
go test -run TestImageAndInstance -timeout 40m ./internal/api
```

## Memberships and API keys (about five minutes)

Set the same variables, then:

```bash
go test -run TestMembershipsAndKeys -timeout 30m ./internal/api
```

## Related

- [How the API authorizes a request](../explanation/authorization.md)
16 changes: 16 additions & 0 deletions docs/reference/authentication.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
type: reference
audience: developer or operator who needs to know which credentials the API accepts
verified: 2026-10-28
---

# Authentication

`Authorization: Bearer <credential>`, the credential is either:

- an access token of Authentik (signature against the JWKS of `OIDC_ISSUER`, issuer, audience, expiry, and a verified `email` claim, which is the user);
- a personal API key `phk_<id>_<secret>`, looked up as the `ApiKey` object `<id>` of the namespace `phorge-keys`. `spec.hash` is the SHA-256 in hex of the secret part as written. A key past its `expiresAt`, marked `status.expired`, being deleted or without an owner is refused. A key is **its owner**: the identity is `user:<owner>`, limited to the scopes of the key.

Whatever is wrong with a credential, the answer is the same `401`; the reason is only logged. When Authentik or the Kubernetes API cannot be reached the answer is `503`, not `401`.

To get a token from a terminal, see [Get a token with the device flow](../how-to/get-a-token-with-the-device-flow.md).
29 changes: 29 additions & 0 deletions docs/reference/configuration.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
---
type: reference
audience: operator who deploys or configures the API
verified: 2026-10-28
---

# Configuration

Configuration is read from the environment and the process refuses to start, listing everything wrong, if a required value is missing:

| Variable | Required | Meaning |
|---|---|---|
| `OIDC_ISSUER` | yes | Authentik issuer, the JWKS is found from it |
| `OIDC_AUDIENCE` | yes | Client id of the Phorge application |
| `OPENFGA_URL` | yes | Base URL of OpenFGA |
| `OPENFGA_API_KEY` | yes | Bearer key for OpenFGA (never logged) |
| `OPENFGA_STORE_ID` | yes | Id of the `phorge-api` store |
| `CA_FILE` | no | PEM bundle added to the trusted roots (the internal CA) |
| `PLATFORM_ADMIN_GROUP` | no | Name of the Authentik group (`phorge-platform-admins`) whose members create, resize and delete tenants, read from the `groups` claim of their token. Empty: nobody can |
| `KUBE_CONTEXT` | no | Context of the kubeconfig, outside the cluster only (development). In a pod the service account is used |
| `LISTEN_ADDR` | no | Default `:8080` |
| `INSTANCE_BOOT_GRACE` | yes | How long after a creation or a start a stop is refused (`409`, `starting`): a stop sent while the guest boots is lost and then hangs, and nothing Incus or Crossplane report says when the system inside listens (measured: the address and the interfaces are there before the agent). `0` never refuses |
| `INSTANCE_STUCK_AFTER` | yes | How long a start or a stop may last before it is reported in `problems` |
| `API_KEY_MAX_LIFETIME` | yes | The longest an API key lives. Positive: an expiry is required and capped to it. `0`: a key may be permanent and its expiry is optional |
| `API_KEY_CREATION` | yes | Who creates and changes API keys: `platform-admins` or `users` (any person signed in with Authentik: a key never exceeds its owner, so this only decides who gets one). Anyone lists and revokes their own |
| `AUTHENTIK_URL`, `AUTHENTIK_TOKEN` | together | Where the API asks whether the owner of a key is a platform administrator, and the token that may read that group |
| `AUTHENTIK_CACHE_TTL`, `AUTHENTIK_TIMEOUT` | with `AUTHENTIK_URL` | How long the members of the group are remembered (also how long someone removed from it keeps what it gave to their keys), and how long a request to Authentik may last |

Nothing that decides how the API behaves has a default in the code: a value is written in the deployment, where it is seen. The format constants (the `phk_` prefix, the size of a secret, the names of the annotations) stay in the code.
22 changes: 22 additions & 0 deletions docs/reference/endpoints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
---
type: reference
audience: developer calling the API
verified: 2026-10-28
---

# Endpoints

| Route | |
|---|---|
| `GET /docs` | Swagger UI |
| `GET /openapi.yaml` | The OpenAPI document, written by hand in `internal/openapi/openapi.yaml`. A test compares it with the registered routes, so a route and its description go in together |
| `GET /v1/whoami` | Who the credential stands for |
| `GET /v1/tenants`, `GET /v1/tenants/{t}` | The tenants you can see (the platform administrators see all of them) |
| `POST /v1/tenants`, `PATCH /v1/tenants/{t}`, `DELETE /v1/tenants/{t}` | Create, resize and delete a tenant: platform administrators only |
| `GET`, `POST /v1/tenants/{t}/images`, `GET`, `DELETE /v1/tenants/{t}/images/{name}` | The images of a tenant: read with `images:read` or a role, write with `operator` or `images:write` |
| `GET`, `POST /v1/tenants/{t}/memberships`, `GET`, `DELETE /v1/tenants/{t}/memberships/{name}` | The roles of the users: admins of the tenant (a key of one, with `memberships:read` and `memberships:write`) |
| `GET`, `POST /v1/apikeys`, `GET`, `PATCH`, `DELETE /v1/apikeys/{id}` | Your own API keys, with a token of Authentik (a key cannot manage keys). Creating and changing: platform administrators for now |
| `GET`, `POST /v1/tenants/{t}/instances`, `GET`, `PATCH`, `DELETE /v1/tenants/{t}/instances/{name}` | The virtual machines: read with `instances:read` or a role, write with `user` or `instances:write` |
| `/healthz`, `/readyz` (503 while shutting down), `/metrics` | Probes and metrics |

Credentials are described in [Authentication](authentication.md).
16 changes: 16 additions & 0 deletions docs/reference/images-and-instances.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
type: reference
audience: developer or operator who works with images and virtual machines through the API
verified: 2026-10-28
---

# Images and instances

Both live in the namespace `t-<tenant>` of the `phorge.fr` group; the platform administrators may act on any tenant that exists.

- **An instance starts from an image of its tenant**, never from a bare image name: the API refuses an image that does not exist in the tenant (`422`) and one that is not `Ready` yet (`409`, `image_not_ready`). While it is pulled (a few minutes) an instance would report problems that are not one and be called `Failed` after three minutes.
- **Images** are pulled from the `images` server (linuxcontainers.org) only, as virtual machines for the architecture of the compute nodes. The Image kind is the only gate to the image servers, so the API restricts the remote. An image cannot be changed, only deleted and pulled again.
- **Deleting an image** that instances start from is a `409` that names them. Delete them first.
- **Instances**: `cpu` 1 to 16, `memoryMiB` 256 to 65536, `diskGiB` 1 to 500, the rest is the defaults of the XRD (what is not given is not sent). `PATCH` changes `running` (start and stop keep the same VM), `cpu`, `memoryMiB`, `diskGiB` and `sshPublicKeys`, applied live. The image, `nesting` and `secureBoot` cannot change: delete and create.
- **The quota of the plan is Incus's**: a refusal such as `Reached maximum number of instances` comes back as the `problems` of the instance, which becomes `Failed` after three minutes. Stopped VMs count. An instance whose first start failed is stuck: delete it and create it again.
- **SSH keys are checked strictly.** The composition writes them into the cloud-init of the VM with a plain `printf`, one `- <key>` line per key, indented by two spaces, and nothing else protects it: a line break in a key would add a line of cloud-init, a command run as root in the VM. A key is accepted only as `<type> <base64>` and at most a one word comment of letters, digits and `@._+=/-`.
Loading
Loading