# Branch previews (T4) Every branch Fabrica builds can get its own address: `--.`, with a certificate from the Domi CA (internal) or Let's Encrypt (public). It closes when the branch is merged, or when its lifetime runs out. Fabrica's coherence roadmap, `docs/32_coherence.md` in Fabrica, phase T4. ## Set up (once per server) 1. A wildcard DNS record for the preview domain points at the server: `*.preview.example.org`. 2. `[previews]` in control.toml (the installers write it from `IANITOR_PREVIEW_DOMAIN` and `IANITOR_PREVIEW_UPSTREAMS`; NixOS: `services.ianitor.previews`): ```toml [previews] domain = "preview.example.org" tls = "domi" # or "letsencrypt" (one certificate per preview; mind the rate limits) auth = "none" # or "forward-auth" (behind the site's Authelia) upstreams = ["10.0.0.0/8", "127.0.0.1:20000-29999", "app.internal", "*.svc.internal:8080"] default_ttl_secs = 259200 # 72 h; min_ttl_secs 300, max_ttl_secs 30 days, max_previews 100, sweep_secs 30 ``` `upstreams` is the allow list of where previews may send traffic: a network (`10.0.0.0/8`, `fd00::/8`), an address with a port or port range (`127.0.0.1:20000-29999`), a name (`app.internal`) or every name under a suffix (`*.svc.internal`), each with an optional port. Nothing else is accepted. Whatever the list says, a preview can never point at control's own port, Caddy's admin port (2019 and the configured one), an unspecified, link-local or multicast address, or an IPv4-mapped IPv6 address. A name is checked as written, not as it resolves: list only names you control. 3. A token for Fabrica that may only manage previews: `ianitor-control token create --name "Fabrica previews" --scope previews --config `, or Settings → API → *Previews only* → New token. It goes into the Custos team vault (see *The token* below). ## The API All calls carry `Authorization: Bearer `; the token may be a full one or a previews-only one. A previews-only token gets `403 token.scope` on every other route, reads included. | Call | Body | Answer | | :-- | :-- | :-- | | `POST /api/v1/previews` | `{project, branch, upstream, ttl?}` | `202 {preview, apply}`: opens the branch's preview, or moves it (new upstream, new lifetime) | | `GET /api/v1/previews/{hostname}` | | `200 {preview}` with `state`: `applying`, `live` or `failed` (with `problem`) | | `GET /api/v1/previews` | | `200 {items, domain}` | | `POST /api/v1/previews/close` | `{project, branch}` | `202 {apply}`: on merge or branch deletion; `404` when there is none | | `DELETE /api/v1/previews/{hostname}` | | `202 {apply}`; `404 preview.not_found` for any host that is not a preview | - `project` is Fabrica's project name (`mnovak/Arca`, `github:acme/web`): 1–128 of `A-Za-z0-9._/:-`. - `branch` is the branch as the git host names it, any characters except control characters, up to 255 bytes. - `upstream` is `http(s)://host:port` with no path, user or query, and must be in the allow list. - `ttl` is seconds (default `default_ttl_secs`). - A preview is an ordinary host file in the routes repository (`hosts/.toml` with a `[preview]` table), so it goes through the same validation, compilers, differential tests and commit-confirmed apply (trial, watch, revert) as any host, and shows in History. Its answers carry `X-Robots-Tag: noindex, nofollow`. - The apply is live when `GET /api/v1/previews/{hostname}` says `live`; poll every 2 s. A failed apply (the branch build did not answer during the watch) says `failed` with Ianitor's reason, and the preview is not served. - Errors: `preview.off` (no `[previews]`), `preview.taken` (another host or branch has that name), `preview.branch`, `preview.project`, `preview.ttl`, `preview.upstream`, `preview.upstream_not_allowed`, `preview.too_many`, `token.scope`. ## Names The label is `--` (core `preview::label`, tests in `crates/core/tests/preview.rs`): - each part is lowercased, every run of other characters than `a-z0-9` becomes one `-`, dashes trimmed; the project part is the repository name only (after the last `/` or `:`); - if that lost anything (case, a character, the owner, length), the part gets `-` and 6 hex digits of SHA-256 of the whole original, so `feature/login` and `feature-login`, or `alice/app` and `bob/app`, never share a host; - the branch part is 3–40 characters, the project part 1–20, so the label is at most 62 and never has `--` in positions 3–4 (reserved for IDNA; Let's Encrypt refuses those). | Project | Branch | Host | | :-- | :-- | :-- | | `shop` | `fix-42` | `fix-42--shop.preview.example.org` | | `mnovak/Arca` | `main` | `main--arca-4a93cc.preview.example.org` | | `github:acme/Web.App` | `feature/Ünï ../../etc` | `feature-n-etc-50d099--web-app-4ff2e1.preview.example.org` | Two branches can still collide only by a 24-bit hash collision; control then answers `409 preview.taken` instead of taking the other branch's host. ## The token, from the Custos team vault The previews-only token is a secret: it lives in the team vault, and Fabrica gets it per deployment, the way it gets any target credential (Custos `docs/27_team_vault.md`, Fabrica `docs/handover/t1-sign-request.md`): the preview build is signed under the team's `preview` environment (0 approvals: the test key signs at once), the sign reply's ticket authorises one `target.request` for the Ianitor target of that project and environment, and the token comes back sealed to Fabrica's one-time key. Fabrica uses it for that deployment and drops it. Custos's target kinds today are `ssh-key`, `winrm`, `openssh`, `registry` and `kubeconfig`; an API token for an HTTP service has no kind of its own yet. Until Custos adds one (`http-token`, written up in docs/custos-ticket.md), keep it as `registry` (a token with a host), which carries the same fields. ## A client `scripts/preview.sh` (curl and jq) and `scripts/preview.ps1` are what a Fabrica pipeline step runs until Fabrica has a preview step of its own: ```bash IANITOR_URL=https://ianitor.example.org IANITOR_PREVIEW_TOKEN=… scripts/preview.sh open mnovak/Arca "$BRANCH" http://10.0.0.7:8080 86400 ``` ```bash scripts/preview.sh close mnovak/Arca "$BRANCH" ``` ## Screens Previews (the nav): every open preview with its state, branch, project, upstream and when it closes (1 click); Close → Close preview (2 clicks).