.gitbay/wiki/Admin.org

5c2215b8455f840087844b1237205f1b010ff842
gitbay/.gitbay/wiki/Admin.org

824 lines · 41811 bytes

  1#+title: gitbay admin guide
  2
  3One static binary (=gitbayd=), one SQLite file, bare repositories on
  4disk, and the system =git=. Schema migrations run automatically on
  5startup and on every admin command.
  6
  7* Install
  8
  9Build from source (=go build ./cmd/gitbayd=), install via the vanity
 10module path (=go install gitbay.org/gitbay/cmd/gitbayd@latest=), or use
 11a release build: =deploy/release.sh <tag>= cross-compiles reproducible
 12linux/amd64, linux/arm64, and darwin/arm64 binaries with a SHA256SUMS
 13manifest (CGO off, trimpath, stripped — byte-identical per commit and
 14toolchain).
 15
 16#+begin_src sh
 17install -m 755 gitbayd /usr/local/bin/
 18adduser --system --group --home /var/lib/gitbay --shell /usr/sbin/nologin gitbay
 19install -d -o gitbay -g gitbay -m 750 /var/lib/gitbay
 20gitbayd --config /etc/gitbay/config.toml check-config
 21#+end_src
 22
 23=deploy/= in the source tree has a cloud-init file, a hardened systemd
 24unit, and a nightly backup timer. Run as the unprivileged =gitbay= user;
 25the unit's =AmbientCapabilities=CAP_NET_BIND_SERVICE= covers ports
 2622/80/443 without root.
 27
 28** The SSH port decision
 29
 30- =ssh.mode = "embedded"= (default): gitbayd itself listens, normally on
 31  22 — move the host's admin sshd to another port. Remotes read
 32  =git@host:owner/repo= with no port gymnastics.
 33- =ssh.mode = "system"=: the host sshd owns 22 and invokes gitbayd via
 34  =AuthorizedKeysCommand=:
 35  #+begin_example
 36  AuthorizedKeysCommand /usr/local/bin/gitbayd --config /etc/gitbay/config.toml authorized-keys %t %k
 37  AuthorizedKeysCommandUser gitbay
 38  #+end_example
 39  sshd requires that binary to be root-owned and not group/world
 40  writable. Unknown keys fail authentication inside sshd, so system mode
 41  requires =registration.mode = "closed"= (check-config enforces this).
 42  Under the forced command the CLI's leading =--term=<cols>[,color]=
 43  argument works as is; a client that sets =GITBAY_TERM= instead needs
 44  =AcceptEnv GITBAY_TERM= in =sshd_config=, since that env request is
 45  handled by the host's sshd, not gitbayd.
 46
 47* Configuration reference
 48
 49=/etc/gitbay/config.toml=. =check-config= validates and names every
 50contradiction; =--no-host-checks= skips port/path probes.
 51=gitbayd admin config show= prints the configuration in effect as TOML,
 52every default filled in and =smtp_pass= redacted. A file that fails
 53validation still prints, followed by the contradiction.
 54
 55** [server]
 56- =root= (default =/var/lib/gitbay=) — repositories, database, host
 57  keys, ACME cache all live here.
 58- =site_url= (required) — canonical =https://host=; drives ACME, clone
 59  URLs, mail links.
 60- =source_repo= (optional, =owner/name=) — the repository this instance
 61  develops itself in. Startup warns when the running build's commit is
 62  not on that repository's default branch, which is how a binary built
 63  from an unmerged branch stops being invisible. Leave it unset unless
 64  the instance hosts its own source.
 65
 66** [ssh]
 67- =mode= — =embedded= | =system= (above).
 68- =port= (22) — embedded listener port.
 69- =host_keys= — list of private key paths; empty generates an ed25519
 70  key at =<root>/ssh/host_ed25519=.
 71
 72** [http]
 73- =addr= (=:443=), =tls= — =acme= | =files= | =off=.
 74- =acme=: certificates via TLS-ALPN-01 on the HTTPS port, cached at
 75  =<root>/acme=; =acme_email= for the CA account; =acme_http_addr=
 76  (=:80=, ="off"= to disable) adds HTTP-01 and an https redirect —
 77  failing to bind it is a warning, not fatal. Requires an =https://=
 78  site_url with a public DNS name.
 79- =files=: =cert_file= + =key_file=.
 80- =off=: plain HTTP — development, or behind a TLS-terminating proxy.
 81- The HTTPS listener accepts TLS 1.2 and 1.3 only (=serverTLS= in
 82  =cmd/gitbayd/tls.go=), with the default cipher suites of the Go
 83  release the binary was built with. =openssl s_client -connect
 84  <host>:443 -tls1_1= fails the handshake.
 85
 86=trusted_proxies= lists the addresses or CIDRs of reverse proxies in
 87front of the daemon. A request from one of them is attributed, for API
 88rate limiting, to the last =X-Forwarded-For= hop that is not itself a
 89trusted proxy; from anyone else the header is ignored. Empty, the
 90default, is right when gitbayd terminates TLS itself.
 91
 92A reverse proxy in front of gitbay must not buffer responses, or the
 93build page's live log arrives only when the build ends;
 94=X-Accel-Buffering: no= covers nginx.
 95
 96** [web]
 97- =mode= — =view_only= (default) | =accounts=. In view_only the mutating
 98  web routes are never registered; in accounts, browser sessions are
 99  minted over SSH (=web login=), and users with write access can create
100  repos, comment, and make simple file edits (which commit unsigned,
101  honestly). =password_auth= is reserved and currently rejected.
102- =title= — the instance's display name in the rail and page titles;
103  empty falls back to the site host. Lower case is the convention for
104  gitbay itself.
105- =privacy_notice= — operator text shown on =/privacy= under the fixed
106  statement.
107
108** [registration]
109- =mode= — =closed= (default) | =invite= | =open=. invite/open require
110  [mail]. See the user guide for the flows.
111
112- =pending_expiry= (empty, never) — a duration such as ="168h"=; a
113  self-registered account still unverified after that long is removed,
114  hourly and at start, audited as =pending.expired=.
115
116- =notify_admin= (false) — mail every instance admin when an account
117  becomes active: an invite redeemed, or an open-mode signup that
118  verified its address. The unverified row an open signup creates is
119  not reported, because anyone can post the form and mailing on that
120  would point a flood at the admins. Recipients are the verified
121  primary addresses of active admins who have activity mail on, the
122  same rule any other notice follows, so an admin with no verified
123  address hears nothing. The notice is queued, so a dead SMTP host
124  shows up in the admin page's Mail table instead of failing the
125  registration. Requires [mail].
126
127** [mail]
128- =smtp_host= (host:port; 587 assumed, 465 with =tls = "implicit"=),
129  =from=, optional =smtp_user= / =smtp_pass=. Required for invite/open
130  registration and self-service =email add=; in closed mode you may omit
131  it entirely and assert addresses by hand (below).
132- =tls= — =starttls= (default) or =implicit= (TLS from the first byte,
133  for relays on 465).
134- =require_tls= — with =starttls=, a relay that does not offer STARTTLS
135  gets no mail: delivery fails and retries, and the admin page's Mail
136  table shows the error. Unset, it is on for every relay except
137  =localhost= and loopback addresses; set =false= to allow plaintext
138  to a remote relay.
139
140** [push]
141Push notifications to Apple devices, delivered by gitbayd talking to
142APNs directly over HTTP/2, authenticated by an ES256 JWT signed with an
143operator-supplied provider key. Off unless configured.
144
145- =enabled= (false).
146- =key_file= — path to the =.p8= provider key from Apple's developer
147  portal (Certificates, Identifiers & Profiles → Keys). It belongs at
148  =/etc/gitbay/apns.p8=, mode 0600, owned by the account gitbayd runs
149  as. Read and validated at startup: it must parse as a PEM-wrapped
150  PKCS#8 EC (P-256) private key, or the daemon refuses to start rather
151  than fill a queue nobody is watching.
152- =key_id=, =team_id= — the key's id and your Apple developer team id,
153  both from the same portal page.
154- =topic= — the app's bundle identifier. *An APNs key belongs to a
155  bundle ID.* gitbay.org pushes to the App Store build under its own
156  bundle id; a self-hoster who wants push ships their own iOS build
157  under their own bundle id, with its own =.p8= key from their own
158  developer account, and points =topic= at that id. There is no way to
159  push to someone else's build, by design — this is Apple's model, not
160  gitbay's.
161- =environment= — =production= or =sandbox=, naming the APNs host
162  rather than taking a URL, so a typo cannot aim the key at a host that
163  is not Apple's.
164
165All five of =key_file=, =key_id=, =team_id=, =topic= and =environment=
166are required when =enabled= is true; validation runs at config load,
167so a misconfigured =[push]= is caught before the daemon serves
168anything. The delivery queue (a device's undelivered and attempted
169pushes) is capped the same way the mail queue is, by =[retention]
170push=.
171
172** [api]
173- =enabled= (false) — the JSON API surface; see [[API]]. Off
174  means no credential-bearing HTTP endpoint exists at all.
175
176** [webhooks]
177- =allow_local= (false) — permit webhook and mirror targets on
178  loopback, private, shared (100.64.0.0/10), link-local or multicast
179  addresses. Leave off unless you know why you need it (SSRF).
180
181** [limits]
182- =clone_timeout= (3600s) — cap on =repo import= fetches.
183- =max_blob_bytes= (100MB) — cap on raw file serving over the web.
184- =max_asset_bytes= (512MB) — cap per uploaded release asset.
185- =max_snippet_bytes= (1MB) — cap per snippet file.
186- =max_snippets_per_user= (0, unlimited) — snippets an account may own.
187- =max_repos_per_user= (0, unlimited) — repositories an account may own
188  directly; =repo create=, =fork= and =import= refuse past it.
189  Organizations are not capped.
190- =max_bytes_per_user= (0, unlimited) — disk the account's own
191  repositories may take; a push may be no larger than what is left.
192- =max_pack_bytes=, =ssh_auth_rate= — reserved, not yet enforced.
193
194** [git_daemon]
195- =enabled= (false), =port= (9418) — the anonymous =git://= listener.
196  Serves only public repositories that additionally ran
197  =repo settings git-daemon <repo> on=.
198
199** [mirrors]
200- =pull_interval_minutes= (15) — how often pull mirrors fetch their
201  upstream. Push mirrors sync shortly after each local ref update.
202  Mirror URLs pass the same SSRF rules as webhook targets, when saved
203  and again before every sync; git then connects only to the addresses
204  that were checked (=http.curloptResolve=) and does not follow
205  redirects, so a mirror of a renamed repository fails until its URL
206  is updated. Needs git 2.37 or later on the server; with an older git
207  the worker logs an error at start and syncs no mirror, recording the
208  reason on each. Sync ignores the system and global gitconfig.
209
210** [go_import]
211Vanity Go module paths, one per line: ="host/module" = "owner/repo"=.
212Requests with =?go-get=1= at or under the module path answer with the
213go-import meta tag pointing at the repository's HTTPS clone URL, so
214=go install host/module/cmd/...@latest= resolves. The repository should
215be public (the module path itself confirms it exists).
216
217* Users, email, invites
218
219Every =gitbayd admin= subcommand except =backup=, =gc= and the one-shot
220backfills is a wrapper that dispatches the registry command of the same
221name as the host: an admin context with no account behind it, so its
222audit rows carry no actor and =source: host=. The same commands run in an
223instance admin's SSH session (=ssh git@<host> admin ...=) and write the
224same rows with the key fingerprint as source. One implementation, two
225credentials.
226
227#+begin_src sh
228gitbayd admin user create alice --key alice.pub --email a@example.org --verified [--admin]
229gitbayd admin email verify alice a@example.org   # admin assertion, no SMTP needed
230gitbayd admin invite --email b@example.org       # mails a code; prints it if no SMTP
231#+end_src
232
233"Verified" means SMTP-confirmed or host-admin-asserted; the database
234records which. Verified emails are what make commit signatures
235meaningful — an unverified address never produces a =verified= badge.
236
237* Audit and account control
238
239The audit log is the security feed (events are the product feed): every
240successful mutating command with its argv and source credential (SSH key
241fingerprint or API), registrations, admin actions, force-pushes, and
242auth failures/throttling. Secrets never appear — they travel on stdin,
243never in argv.
244
245#+begin_src sh
246gitbayd admin audit [--actor u|-] [--action prefix] [--since 24h|7d|date] [--limit n] [--json]
247ssh git@<host> audit ...             # the same, from an admin session
248ssh git@<host> admin user list [--state active|pending|disabled|admin]
249ssh git@<host> admin user show <name>   # keys, emails, orgs, tokens, sessions
250ssh git@<host> admin user limits <name> [--repos n|default] [--bytes n|default]   # per-account caps
251ssh git@<host> admin user promote <name>   # grant instance admin
252ssh git@<host> admin user demote <name>    # remove it; the last admin is refused
253gitbayd admin user promote <name>    # host-local: recovery when no admin key is reachable
254gitbayd admin user disable <name>    # suspend: SSH, web, API all refused;
255gitbayd admin user enable <name>     #   sessions dropped, nothing deleted
256gitbayd admin user delete <name> --yes  # only for accounts anchoring nothing:
257                                     #   refused (with each blocker named) while
258                                     #   the account owns repos, authored
259                                     #   issues/MRs/comments/reviews, or is an
260                                     #   org's only admin
261#+end_src
262
263=--actor= takes a username, or =-= for rows with no actor: host commands
264and auth failures. =--action= is a prefix, so =cmd repo= catches every
265repository command and =admin= every host or admin-session action.
266=--since= is a duration back from now (=30m=, =24h=, =7d=) or a date.
267
268=admin user list= pages by username (=--limit=, =--cursor=) and carries
269each account's state and =last_seen=, the newest use of any of its SSH
270keys or API tokens. =admin user show= adds the keys with their last use,
271each address with how it was verified, PGP keys, org roles, the owned
272repository count, API token names, and live browser sessions. Both are
273Both are refused to non-admins, like =audit=, on every surface.
274
275=/admin/users= is the same list in a browser, linked from the admin
276page: the state filter the command takes, keyset paging on its cursor,
277and a row per account with promote, demote, disable and enable, each
278dispatching the command. Demote and disable ask for the username to be
279typed, since both take someone's access away. Creating and deleting an
280account, issuing an invite and asserting an address stay on the command
281line: each takes a key, mints a credential, or cannot be undone. A
282non-admin gets the 404 a missing page would, so the URL confirms
283nothing.
284
285Promotion needs an active account: a pending or disabled one is refused.
286Demotion is refused when it would leave no admin, over SSH, in the
287browser and on the host alike, so the host-local =promote= is the way
288back in when the only admin key is lost.
289
290Instance admin carries no right on anyone's repository: policy does not
291consult it, and a private repository still answers not-found to an
292admin. Moderation goes through explicit overrides that skip the access
293check and write their own audit row:
294
295#+begin_src sh
296ssh git@<host> admin repo list [--owner o] [--visibility public|private]  # size, last push
297ssh git@<host> admin repo archive|unarchive <owner/name>
298ssh git@<host> admin repo visibility <owner/name> public|private
299ssh git@<host> admin repo delete <owner/name> --yes
300#+end_src
301
302Each lands in the audit log as =admin repo.<action>= naming the
303repository, on top of the =cmd= row every mutating command gets.
304
305=limits.ssh_auth_rate= (10) throttles per-IP authentication *failures*
306per minute — successful auths never count and clear the slate.
307=limits.max_pack_bytes= is enforced as =receive.maxInputSize= on every
308push.
309
310=limits.write_rate= (60) bounds *mutating commands per account per
311minute*. It is counted in the dispatcher, so SSH, the JSON API and the
312web spend one budget and a caller cannot refresh it by changing surface;
313=limits.api_rate= stays in front of it, bounding a network source rather
314than an account. A command is one token whatever it writes, so a bundle
315import costs one and only a loop of separate commands spends the budget.
316Read-only commands, the runner protocol (a build streams its log in many
317small writes) and the host CLI are exempt. Refusals exit 4 and say when
318to retry. A negative value turns the limit off; it matters most with
319=registration = "open"=, where every write also queues notification mail
320and webhook deliveries.
321
322* Queues
323
324Every background worker keeps a backlog and a failure state. An instance
325admin reads them all in one place:
326
327#+begin_src sh
328gitbay dashboard --json | jq .queues   # webhooks, mail, push, mirrors, builds, deps
329#+end_src
330
331Per worker: pending, retrying (pending with a failed attempt) and
332dead-lettered counts with the oldest pending age, and the retrying or
333failed rows themselves, capped at twenty each. Builds list what is
334running and then what is pending, each since when, so a build no runner
335is scoped to claim is visible here rather than only in its repository;
336mirrors list the ones whose last sync failed;
337dependency checks list the ones whose last check errored. Non-admins get
338no =queues= key at all.
339
340Push rows name the device id, never the token. Watch this one after
341configuring =[push]=: a =key_id= or =team_id= Apple did not issue passes
342config validation, which can only check that the =.p8= parses, and then
343every send comes back =403 InvalidProviderToken= and dead-letters on its
344first attempt.
345
346In accounts mode the same read renders at =/admin=, linked from the rail
347for admins. Anyone else gets a 404 there.
348
349A dead-lettered mail is logged as =notification dead-lettered mail=<id>=,
350with the address redacted out of the relay's error. The id is the queue
351row: find it in the Mail table on =/admin=, or in =dashboard --json=,
352where the recipient and the unredacted error are. That is deliberate —
353see the Threat-Model page.
354
355* Maintenance
356
357#+begin_src sh
358gitbayd admin stats [--json]         # counts, database size, per-repo disk
359ssh git@<host> admin stats [--json]  # the same, from an admin session
360gitbayd admin gc [--repo owner/name] # git gc: repack and prune; per-repo sizes
361gitbayd admin gc --aggressive        # thorough repack; slow, rarely needed
362gitbayd admin gc --lfs               # also drop LFS objects no pointer names (older than a day)
363#+end_src
364
365A history rewrite leaves the commits it removed reachable through
366=refs/merge-requests/N/head= of the merge requests that landed them, so
367they stay fetchable by anyone who can read the repository. Nothing drops
368a head ref on its own — an open or source-gone MR is merged through it,
369and a merged or closed one keeps its diff readable through it — so the
370cleanup is a command an instance admin runs, naming the MRs:
371
372#+begin_src sh
373ssh git@<host> admin mr prune owner/name 1 2 3 --yes
374#+end_src
375
376It refuses an open or source-gone MR, deletes the named refs, runs
377=git gc --prune=now= on that one repository so the objects go at once
378rather than after git's two-week grace, leaves a system comment on each
379MR, and audits as =admin mr.prune=. The MR keeps its title, comments,
380reviews and head sha; =mr diff= and the MR page say the head is gone.
381Run it when nothing is pushing to that repository: without the grace, a
382push caught between leaving quarantine and writing its ref loses its
383objects. Objects also survive in offsite backups until those are
384rewritten; see "Removing a repository's history from every snapshot".
385
386=deploy/cloud-init.yaml= ships a =gitbay-gc.timer= that runs =admin gc=
387weekly (Sunday 07:00 UTC). Imported repositories keep whatever pack
388layout the source sent, so a first manual =admin gc= after a bulk
389import is worthwhile.
390
391* Backup and restore
392
393#+begin_src sh
394gitbayd admin backup --out /var/backups/gitbay/backup.tar.gz
395gitbayd admin backup --verify /var/backups/gitbay/backup.tar.gz   # read it back
396#+end_src
397
398One archive: a consistent SQLite snapshot (taken *before* the
399repositories are read, so the database never references objects the
400archive missed), every repository, and the SSH host keys. Excluded:
401hook socket, regenerated hook scripts, WAL files. Safe to run against a
402live daemon.
403
404=--verify= reads an archive back: the snapshot must pass SQLite's
405integrity check, and every repository the snapshot names must be in the
406archive. A database-only archive is checked for integrity and says so.
407Exit is non-zero on damage or a missing repository.
408
409Restore: extract into an empty directory, point =server.root= at it,
410start gitbayd. Host keys are preserved, so clients keep their
411known_hosts entries; hooks regenerate at startup.
412
413** Schedule and recovery point
414
415Two timers, because the two halves of the data have different exposure.
416
417- =gitbay-backup.timer=, nightly. The full archive above, last 7 kept.
418- =gitbay-db-backup.timer=, hourly. =admin backup --db-only=, which
419  writes the SQLite snapshot alone, last 48 kept. A few MB against the
420  full archive's hundreds, which is what makes the frequency affordable.
421
422The split follows what a loss would actually cost. Repositories are git,
423so a mirror or any clone is a second copy; the database is the only copy
424of issues, merge requests, comments and review state. So the recovery
425point is about an hour for the data that exists nowhere else, and a day
426for the data that does.
427
428Continuous replication (litestream and similar) was considered and not
429adopted. It would take the database's recovery point to seconds, but the
430repositories would still be on the nightly archive, so a restore could
431produce a database referencing commits the repository backup does not
432have. Consistency between the two halves is worth more here than latency
433on one of them. Revisit if repository replication becomes continuous
434too.
435
436** Offsite copies
437
438bay1 also takes a nightly restic snapshot of =/var/lib/gitbay= and
439=/var/lib/gitbay-stage= (the staged database copy) to an S3 bucket at
440Scaleway, with a key that can only add snapshots. The key that can
441remove them lives on the operator's machine, in
442=~/.config/gitbay/offsite.env=, and never on bay1: a compromised host
443cannot destroy its own history. Forgetting, pruning and rewriting all
444run from there.
445
446*** Removing a repository's history from every snapshot
447
448A history rewrite plus =admin mr prune= takes commits off the server,
449but every snapshot taken before it still holds them, and the retention
450window is the only thing that ages them out. To remove them now,
451rewrite the snapshots without that repository rather than forgetting
452the snapshots: everything else in them stays restorable. The next
453nightly run adds the repository back in its current state.
454
455Repositories are stored under the name they had on disk when each
456snapshot was taken, so a renamed repository needs every name it has
457carried. Check what an older snapshot holds before choosing the paths:
458
459#+begin_src sh
460set -a; . ~/.config/gitbay/offsite.env; set +a
461restic $RESTIC_OPTS snapshots
462restic $RESTIC_OPTS ls <old-snapshot> /var/lib/gitbay/repos/<owner>
463#+end_src
464
465Then dry-run, apply, prune, and confirm nothing matches:
466
467#+begin_src sh
468EXCL="--exclude /var/lib/gitbay/repos/<owner>/<name>.git --exclude /var/lib/gitbay/repos/<owner>/<old-name>.git"
469restic $RESTIC_OPTS rewrite --dry-run $EXCL     # "would modify N snapshots"
470restic $RESTIC_OPTS rewrite --forget $EXCL      # new snapshots replace the originals
471restic $RESTIC_OPTS prune                       # drops the data nothing references
472restic $RESTIC_OPTS find <name>.git <old-name>.git   # expect no output
473#+end_src
474
475=--forget= is what makes the originals go; without it the rewritten
476snapshots sit beside them and the data stays referenced. Snapshot IDs
477change; their times do not. Done for krz/keycask (formerly rust-pass)
478on 2026-09-18, across 22 snapshots.
479
480* Upgrades
481
482Replace the binary, restart the unit. Migrations apply automatically and
483are transactional; hook scripts under =<root>/hooks= are rewritten at
484startup to point at the current binary path.
485
486* CI runner
487
488=gitbay-runner= executes builds queued by pushes and merge requests. It
489polls over SSH with a key of scope =runner=, which reaches only the
490runner protocol and read-only git (a runner executes arbitrary
491repository code, so the key it holds must not do more). A runner key
492claims builds only for the repositories it is attached to, by =repo
493runner add= from a repository admin or an instance admin; an admin key
494claims any. Users attach their own runners: see the Users page. For an
495instance runner, run it as a dedicated unprivileged user on a non-admin
496account. =admin user create --key= registers a full-scope key, so the
497runner key is added afterwards through a bootstrap key that is then
498removed, and attached to each repository it should build:
499
500#+begin_src sh
501useradd --system --create-home --home-dir /var/lib/gitbay-runner ci-runner
502sudo -u ci-runner ssh-keygen -t ed25519 -N "" -f /var/lib/gitbay-runner/.ssh/id_ed25519
503ssh-keygen -t ed25519 -N "" -f /tmp/ci-bootstrap
504gitbayd --config /etc/gitbay/config.toml admin user create ci --key /tmp/ci-bootstrap.pub
505ssh -i /tmp/ci-bootstrap git@127.0.0.1 keys add --scope runner < /var/lib/gitbay-runner/.ssh/id_ed25519.pub
506ssh -i /tmp/ci-bootstrap git@127.0.0.1 keys remove "$(ssh-keygen -lf /tmp/ci-bootstrap.pub | awk '{print $2}')"
507rm /tmp/ci-bootstrap /tmp/ci-bootstrap.pub
508gitbay-runner -remote git@127.0.0.1 -workdir /var/lib/gitbay-runner/work
509#+end_src
510
511#+begin_src sh
512gitbay repo runner add krz/site < /var/lib/gitbay-runner/.ssh/id_ed25519.pub
513#+end_src
514
515=-jobs N= runs N builds at once. Claiming is one transaction that
516selects and updates, and each build works in its own =build-<id>=
517directory, so workers do not collide; idle polls are staggered across
518the interval so N of them do not wake together. The drop-in's weights
519below are per service, not per build, so raising =-jobs= divides them
520rather than multiplying the host's load.
521
522=admin runners= shows which account each runner polls as, and what each
523may claim. A runner key claims builds only for the repositories it is
524attached to: with none attached it claims nothing, and =-repos= may only
525narrow within them. An admin's full-scope key claims any repository —
526that is what =-repos= was for — and still works for the protocol during
527a rotation. A merge request head from a fork is built in the target
528repository as untrusted: the claim carries no secrets, and only a runner
529started with =-untrusted= takes it. Same-repository heads were built by
530their branch push and are not built again.
531
532=make deploy-runner= also installs
533=deploy/gitbay-runner.override.conf= as a systemd drop-in: =Nice=10=,
534=CPUWeight=30=, =IOWeight=30=, so a build never starves the host's sshd,
535the daemon or the backup timers, and =NoNewPrivileges=,
536=ProtectSystem=full=, =ProtectKernelTunables=, =ProtectControlGroups=
537and =RestrictSUIDSGID=, so a step cannot reach outside its workspace
538and the runner's home. The e2e suite alone starts sixty
539daemon instances; without the drop-in a deploy's copy over the admin
540sshd stalled. Both deploy targets copy with =rsync --partial=, which
541resumes a stalled transfer.
542
543Under =-isolation podman=, the default and what bay1 runs, each build
544is confined to a container (see Container isolation below). Under
545=-isolation none= steps run directly on the host as the runner's user,
546so treat that machine as executing whatever your users push, and
547install the toolchains your builds need on it.
548
549A runner claims the oldest pending build among the repositories its key
550is attached to — for an admin key, the oldest in the instance. =-repos=
551narrows within that set, which is what makes a runner outside the server
552practical: one on a machine that should build a single project, or that
553holds credentials for one deployment, stays on it.
554
555Oldest-first is across everything the key may claim, so a repository
556with a deep queue holds every other repository the same runner serves;
557bay1 measured a 15-minute average wait on a day of merge request
558stacks from one repository. A runner attached to one repository cannot
559be starved. That is the rule, decided in krz/gitbay#207: a runner
560serving several repositories takes them oldest-first, and an operator
561who wants one repository never to wait on another runs a second
562runner attached to it alone. Nothing caps what an account queues,
563and nothing needs to (krz/gitbay#206): a schedule tick queues nothing
564while the job's last build is pending or running, so a repository
565with no runner holds one row per scheduled job rather than one per
566tick, and a build runs only on a runner its owner attaches, so a busy
567schedule spends the owner's compute. Pushes are bounded by what an
568account can push.
569
570#+begin_src sh
571gitbay-runner -remote git@gitbay.org -repos krz/site,krz/docs \
572  -workdir /var/lib/gitbay-runner/work
573#+end_src
574
575Add =-untrusted= only with =-isolation podman=.
576
577gitbay.org's runner is attached to the forge's own repositories and
578the isolation canary, nothing else, because it shares the host with
579the forge; its unit names no =-repos=, the attachments are the
580boundary. Any other repository builds on a runner its owner attaches.
581
582=-repos= narrows an admin runner; for a runner key the attachments are
583the boundary, held by the server, and =-repos= may only name
584repositories among them. =-untrusted= makes a runner claim merge
585request heads from forks; the bay1 unit sets it because it isolates in
586podman. A runner without it builds trusted commits only.
587
588=gitbay dashboard= and =ssh git@<host> admin runners= list every key
589that has polled as a runner: the account, the key's fingerprint, when it
590last polled, the repositories it may claim — its attachments for a runner
591key, the =-repos= it asked for or =any= for an admin key — and the build
592it holds; =admin runners remove <fingerprint>= (=forget= until the next release) drops the row for a key
593that polled by mistake, the key itself untouched. =admin runners= also
594heads the list with the queue: builds
595pending now, and over the last day how many were claimed, how long they
596waited to be claimed (average and worst), and
597how many the reaper ended instead of a runner reporting them. A build a runner claimed and never
598reported is failed by the scheduler's minute tick, whether or not any
599runner is still alive: within about two minutes of its log stream ending
600with no outcome reported — the runner reports right after closing the
601stream, retrying for half a minute if gitbayd is unreachable — or, if no
602stream was ever seen, at the deadline.
603
604Instance admin on the runner account only authorizes the claim/report
605protocol; it grants no repo access. A build that pushes back — a pages
606deploy, an archive publish, an automated MR branch — needs an explicit
607grant on that repo: =repo access grant <owner/name> ci write=. Private
608repos likewise need at least read for the clone.
609
610** Container isolation
611
612Builds run in a rootless podman container, one per job, with the
613workspace bind mounted and nothing else: the clone happens outside with
614the runner's key, so a step cannot read it. =-isolation none= keeps the
615old behaviour — steps on the host as the runner's user — for an instance
616where every repository is trusted. There is no automatic fallback: a
617runner started with =-isolation podman= that cannot find a working
618podman exits rather than running a build unsandboxed.
619
620gitbay's own jobs name =localhost/gitbay-ci:2=, built from
621=deploy/Containerfile.ci= on the runner host. A job's image must carry
622what its steps need: the suite drives real git, git-lfs, gpg and sshd and
623asserts they exist before running, so the stock runner default would fail
624it immediately. Build or rebuild it with:
625
626#+begin_src sh
627ssh -p 2222 root@<host> 'cat > /tmp/Containerfile.ci' < deploy/Containerfile.ci
628ssh -p 2222 root@<host> 'su - ci-runner -s /bin/sh -c \
629  "podman build -t localhost/gitbay-ci:2 -f /tmp/Containerfile.ci /tmp"'
630#+end_src
631
632The tag is deliberate rather than =:latest=: changing the file means
633bumping the tag in =.gitbay/ci.yml=, so a running branch's image does not
634change under it.
635
636=-image= names the image a job runs in when it declares none, and is
637required under =-isolation podman=: there is no built-in default,
638because an image this host does not have would fail every build. A job
639overrides it with =image:= in =.gitbay/ci.yml=, validated as a reference
640so a config file cannot turn it into podman arguments.
641
642=-cpus= and =-memory= cap one build (podman's units, e.g. =-cpus 2
643-memory 4g=); unset means uncapped. The runner applies them itself: it
644creates a cgroup per build under its own delegated service cgroup,
645writes the limits there, and starts every podman process for the build
646inside it, with podman's cgroup handling off. Podman's own =--memory=
647and =--cpus= never applied under rootless cgroupfs, which is what a
648system service gets (krz/gitbay#188). The unit therefore needs
649=Delegate=yes=, which the drop-in sets; without it the runner refuses
650to start when a limit is set, and logs that builds run unconfined when
651none is. bay1 runs =-cpus 3 -memory 6g= per build inside =MemoryMax=6G=
652and =CPUQuota=300%= on the unit, on a 7.7GB four-core host with no
653swap: the memory cap is what keeps the forge alive when a build
654allocates without bound, and it sits above the e2e suite's 5GB peak
655rather than at a fair share. =OOMPolicy=continue= keeps systemd from
656stopping the runner when a build is OOM-killed.
657
658Each repository gets its own build home under the runner's workdir,
659mounted into its containers as =HOME=. Caches persist between builds of
660one repository and are never read by another's.
661
662*Images are provisioned, never pulled by a build.* The runner passes
663=--pull=never=. Two reasons, and the second is the better one: the
664service runs with =RestrictSUIDSGID=yes= so podman cannot unpack a layer
665holding a setuid file, which is nearly every distribution image; and on
666an instance where anyone can push a =ci.yml=, =image:= would otherwise
667mean "fetch and run anything from the internet". An operator pulls or
668builds what is allowed and a build picks among those. A job naming an
669image the host does not have fails with a message saying so.
670
671#+begin_src sh
672su - ci-runner -s /bin/sh -c "podman pull docker.io/library/alpine:3.20"
673su - ci-runner -s /bin/sh -c "podman images"
674#+end_src
675
676Prepare a host before pointing an isolating runner at it:
677
678#+begin_src sh
679ssh -p 2222 root@<host> 'sh -s' < deploy/runner-podman-setup.sh
680make deploy-runner
681#+end_src
682
683The script installs podman, delegates a subuid/subgid range to
684=ci-runner=, checks that user namespaces are enabled rather than
685assuming, enables lingering, and verifies rootless podman actually runs
686as that user. It is idempotent.
687
688The drop-in sets =NoNewPrivileges=no=, without which rootless podman
689cannot call =newuidmap= and the runner refuses to start. That is a
690considered trade, explained in the file and in the Threat-Model; if you
691run with =-isolation none=, set it back to =yes=.
692
693*Restarting the runner is safe.* On SIGTERM it stops claiming, finishes
694the build in flight, reports it, and exits; the drop-in's
695=TimeoutStopSec=50min= covers the longest build, and its =KillMode=mixed=
696is what makes the signal reach the runner alone — under systemd's default
697the build's container and the log session are signalled with it, and the
698runner drains a build that is already dead. So =make deploy-runner=
699waits for a running build rather than orphaning it, and a build's result
700is retried for half a minute if gitbayd is restarting at that moment. A
701second SIGTERM ends the runner at once, abandoning the build to the
702reaper. The suite checks all three: =TestRunnerDrainsOnSIGTERM= signals
703the process, =TestRunnerDropInLetsTheDrainHappen= reads the drop-in's
704=KillMode= and =TimeoutStopSec=, and =TestRunnerDrainUnderSystemd= runs
705the runner as a transient user unit under =systemd-run= and stops it
706under both kill modes. That last one needs a systemd user manager, so it
707skips in the container CI runs in; run it on a Linux host with
708=go test ./e2e -run TestRunnerDrainUnderSystemd -v=.
709
710*Validate podman mode on a scratch repository before pointing the runner
711at real ones.* Every deploy that switched the whole instance to
712containers and failed took CI down with it. Instead: create a throwaway
713repository the runner account can read (public, or granted read — a
714private one is "not found" to the runner and the build stays pending),
715give it one job that names the CI image, and deploy the runner with
716=-repos= naming only that repository. The production unit, with its real
717hardening, then claims nothing else; other repositories' builds queue
718until =-repos= is switched back, which is a pause, not an outage.
719
720#+begin_src sh
721gitbay repo create cmc/ci-smoke          # then push a .gitbay/ci.yml naming the image
722sed -i 's#-repos krz/gitbay #-repos cmc/ci-smoke #' /etc/systemd/system/gitbay-runner.service.d/override.conf
723systemctl daemon-reload && systemctl restart gitbay-runner
724gitbay build log cmc/ci-smoke 1         # green: switch -repos back, redeploy
725#+end_src
726
727*Do not deploy an isolating runner to a host that has not been
728prepared.* The runner is specified to refuse to start without a working
729podman rather than fall back to running builds unsandboxed — a fallback
730that silently drops isolation is worse than a stopped runner, because
731nothing surfaces it. On an unprepared host that refusal stops every
732build on the instance.
733
734The service drop-in carries =Delegate=yes= for rootless cgroup
735management and =ReadWritePaths= for podman's store under
736=/var/lib/gitbay-runner=, which =ProtectSystem=full= would otherwise
737make read-only. Those paths are prefixed =-= so they are ignored when
738absent: the drop-in installs on unprepared hosts too, and a unit that
739refused to start would stop every build.
740The nightly canary on =cmc/ci-smoke= only runs if the runner's =-repos=
741names that repository too; a scoped runner claims nothing else.
742=gitbay-runner-prune.timer= prunes unused images weekly, as the runner's
743user: rootless storage belongs to that user, and root's prune would not
744see it. An unpruned image store on a 40GB host is a slow outage.
745
746* LFS storage
747
748Objects live content-addressed under =[lfs] root= (default
749=<server.root>/lfs=); =[lfs] max_object_bytes= caps a single object
750(512MB default). Storage sits behind a small interface — an
751S3-compatible backend is a drop-in with the server proxying, and
752presigned URLs a later optimization. LFS objects do not travel with
753push mirrors (mirrors move git refs only), and gc does not yet collect
754orphaned objects.
755
756* Pages
757
758=[pages] domain = "example.site"= serves public repos' =pages= branches
759on =<owner>.<domain>=. DNS needs a wildcard record =*.<domain>= to the
760server; ACME issues per-subdomain certificates on demand (only for
761owners that exist). The domain must not be the site host or a parent of
762it — pages content runs its own scripts and must stay off the forge's
763origin.
764
765Users with repo admin claim custom domains with =repo domain add=.
766Claims activate only after a DNS TXT challenge proves control of the
767domain (=repo domain verify=, audit-logged); pending claims serve
768nothing, get no certificates, and expire after 7 days. ACME issues
769certificates only for verified hosts, so stray DNS pointed at the
770server gets nothing.
771
772* Security
773
774The [[Threat-Model]] file is the reference for what the forge
775trusts and refuses to do. Operational checklist:
776
777- *Software checks.* =deploy/audit.sh= runs =go vet=, =govulncheck=
778  (the module list is deliberately short — review it on each release),
779  and a short fuzz pass over every attacker-facing parser (pkt-line,
780  commit, SSHSIG armor, OpenPGP key, SSH tokenizer). Run it before
781  tagging a release. CI's own =vuln= job runs =govulncheck= nightly
782  against main rather than per push, because =@latest= scans today's
783  advisory database and an advisory lands without anyone pushing;
784  =build trigger krz/gitbay vuln= runs it on demand.
785- *Web responses* carry a scripts-forbidden CSP, =X-Frame-Options:
786  DENY=, =nosniff=, =no-referrer=, and HSTS when TLS is on — no
787  configuration needed.
788- *Host sandboxing.* The systemd unit in =deploy/cloud-init.yaml= runs
789  gitbayd unprivileged with =ProtectSystem=strict=, =PrivateDevices=,
790  =LockPersonality=, =MemoryDenyWriteExecute=,
791  =SystemCallFilter=@system-service=, and =RestrictAddressFamilies= to
792  INET/INET6/UNIX. It keeps =CAP_NET_BIND_SERVICE= only, to bind 22/80/443.
793- *OS patches* apply via =unattended-upgrades= (security origins,
794  auto-reboot 04:30 if required).
795- *Admin sshd (2222)* is throttled by =MaxStartups=/=MaxAuthTries= and
796  watched by =fail2ban=; gitbayd's own port 22 is throttled by
797  =limits.ssh_auth_rate= (auth failures per IP per minute), and every
798  account's writes by =limits.write_rate=.
799- *Monitoring.* =gitbay-monitor.timer= writes a reading hourly to
800journald and, when =/etc/gitbay/monitor.url= exists, posts it to that
801webhook: disk, service, the daemon's own =/healthz= answer, certificate
802expiry, and the age of the newest full backup and database snapshot.
803It exits non-zero on an alert so the unit shows in =systemctl
804--failed=: a stopped service, =/healthz= not answering =ok=, disk ≥ 85%,
805a certificate under 21 days, a full backup over 25 hours old, or a
806database snapshot over 2 hours old.
807
808=GET /healthz= is unauthenticated and cache-free: whether the database
809answers and which commit serves, 503 when it does not.
810- *Database.* =gitbay.db= and its WAL live under =/var/lib/gitbay= (mode
811  0750, owned by =gitbay=). The nightly archive plus provider snapshots
812  are the recovery path; for tighter RPO, add continuous replication
813  (litestream) against the same file — it coexists with the WAL.
814
815* Odds and ends
816
817- deleting a fork marks MRs sourced from it =source_gone=; their diffs
818  remain viewable and mergeable because the target repo owns the
819  objects.
820- =refs/merge-requests/*= is server-owned and unpushable by clients;
821  only =admin mr prune= removes one (see Maintenance).
822- audit-relevant activity (issue/MR lifecycle, imports, pushes) lands in
823  the =events= table, which also feeds webhooks.
824- the daemon idles under 10MB RSS; the smallest VPS tier is adequate.