kunibert
All repositories: gitoria
5.6 KB
import { Response } from 'hl:http1'import { readFile } from 'hl:fs'import { run } from 'hl:proc'import { timestamp } from 'hl:time'import { hkdfExtract } from 'hl:crypto'import Queue from './queue.hl'import { classify, isKnown, repoId, headerEvent, headerDelivery, headerSignature, signaturePrefix } from './gitoria-events.hl'\\ Gitoria's signed events: check, answer at once, queue. Thin: the queue logic is lib/queue.hl,\\ the event names are lib/gitoria-events.hl. Never logs a secret or the body of a bad request.String secretsFile = ''String dataDir = ''Number maxTries = 3Number backoffSeconds = 30String reposFile = '' \\ per-repo config: { "<repo.id>": { api, clone, keyFile, secretFile } }; '' = legacy (secretsFile only)String hybriel = 'hybriel'Number timeoutSeconds = 900String runnerData = ''String ticketsBase = ''String ticketsTokenFile = ''String reviewUser = '' \\ unix user the model (codex) runs as; '' = same user as the serviceString codexBin = 'codex'String workDir = '' \\ job worktrees; '' = <runnerData>/workrepoCfg = nullqueue = nulldefaultSecret = nullrepoSecrets = {}\\ a secret/key file must be mode 600; its first line is the valuereadSecret = (String path, String what) => {m = run(['stat', '-c', '%a', path], { timeout = 10 })if (m.exit != 0) { hlError(what + ' file not readable: ' + path) }if (!m.lines.join('').trim().endsWith('00')) { hlError(what + ' file must have mode 600: ' + path) }v = readFile(path).split(JSON.parse('"\n"'))[0].trim()if (v == '') { hlError(what + ' file is empty: ' + path) }return v}start = () => {if (secretsFile != '') {s = JSON.parse(readFile(secretsFile))defaultSecret = s.defaultrepoSecrets = s.reposif (repoSecrets == null) { repoSecrets = {} }}if (reposFile != '') {repoCfg = JSON.parse(readFile(reposFile))for (id in repoCfg) {c = repoCfg[id]if (c.api == null || c.clone == null || c.keyFile == null || c.secretFile == null) { hlError('repo config ' + id + ' needs api, clone, keyFile, secretFile') }readSecret(c.keyFile, 'key')repoSecrets[id] = readSecret(c.secretFile, 'secret')}}queue = new Queue(dataDir = dataDir, maxTries = maxTries, backoffSeconds = backoffSeconds, repos = repoCfg, hybriel = hybriel, timeoutSeconds = timeoutSeconds, runnerData = runnerData, =ticketsBase, =ticketsTokenFile, =reviewUser, =codexBin, =workDir)queue.load()}reply = (Number status, String code, String line, Hybrid extra = {}) => {console.log(timestamp() + ' hook: ' + status + ' ' + code + (line == '' ? '' : ' ' + line))body = { status = code }for (k in extra) { body[k] = extra[k] }return new Response(JSON.stringify(body), { status = status, headers = { 'Content-Type' = 'application/json' } })}\\ "sha256=" + hex HMAC-SHA256(secret, raw body). hl:crypto has no hmac: hkdfExtract(salt, ikm) IS HMAC(salt, ikm).expected = (String secret, Bytes raw) => {return signaturePrefix + hkdfExtract(toBytes(secret), raw).hex()}anyRepoSecrets = () => {for (k in repoSecrets) { return true }return false}hook = (Hybrid req) => {let delivery = req.headers[headerDelivery]given = req.headers[headerSignature]if (given == null || given == '') { return reply(401, 'missing_signature', 'delivery=' + delivery) }let body = nulllet secret = defaultSecretif (anyRepoSecrets()) {body = JSON.parse(req.body)id = repoId(body)if (id != null && repoSecrets[id] != null) { secret = repoSecrets[id] }}if (secret == null || given != expected(secret, req.bytes)) { return reply(401, 'bad_signature', 'delivery=' + delivery) }name = req.headers[headerEvent]if (!isKnown(name)) { return reply(202, 'ignored', 'unknown event ' + name + ' delivery=' + delivery, { reason = 'unknown event' }) }if (body == null) { body = JSON.parse(req.body) }if (delivery == null || delivery == '') { delivery = body.delivery }if (delivery == null || delivery == '') { return reply(400, 'missing_delivery', 'event=' + name) }if (repoCfg != null && body.repo != null) {id = repoId(body)c = repoCfg[id]if (c == null) { return reply(403, 'repo_mismatch', 'repo ' + id + ' is not configured delivery=' + delivery) }if (body.repo.api != c.api) { return reply(403, 'repo_mismatch', 'repo ' + id + ' api differs from the config delivery=' + delivery) }if (body.repo.clone != c.clone) { return reply(403, 'repo_mismatch', 'repo ' + id + ' clone differs from the config delivery=' + delivery) }}if (queue.wasSeen(delivery)) { return reply(200, 'duplicate', 'delivery=' + delivery) }queue.markSeen(delivery)c = classify(name, body)if (c.kind == 'ping') { return reply(202, 'ping', 'delivery=' + delivery) }if (c.kind == 'ignore') { return reply(202, 'ignored', c.reason + ' delivery=' + delivery, { reason = c.reason }) }if (c.kind == 'close') {r = queue.close(c)return reply(202, 'closed', r.key + ' dropped=' + r.dropped + ' removeSession=true delivery=' + delivery, { job = r.key, dropped = r.dropped })}if (c.kind == 'thread-state') {r = queue.recordThread(c)return reply(202, r.action, r.key + ' ' + c.event + ' delivery=' + delivery, { job = r.key })}r = queue.enqueue(c)return reply(202, r.action, r.key + ' ' + c.event + ' head=' + c.head + ' delivery=' + delivery, { job = r.key, threadReplyOnly = r.threadReplyOnly })}health = () => {return queue.health()}tick = () => {return queue.tick()}
Branches
Latest commits
- 88aaacc5Records: real proof part 2 (posting works, sandbox blocked by AppArmor userns)mre
- 401df4e8Records: review user logged inmre
- 6ca681b3Records: login command, status cleanupmre
- 6b8d763cRecords: review user, device login startedKunibert
- 07025b99Open the job worktree read-only for the review user before each model runKunibert
- 2a44bd54Runner CLI: --review-user/--codex-bin/--work-dir work in every modeKunibert
- 2177f66fReview model runs as its own unix user (reviewUser, codexBin, workDir)Kunibert
- 35df8306Records: live service on byrodin, real proof part 1Kunibert
- 7480a98dkunibert.service: SIGTERM stop is a clean exitKunibert
- 3a8d93f9gitoria client: parse only JSON answers, accept a plain-text errorKunibert
- c9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibert
- a41ba15fKunibert: event intake, git cache, review runner, posting, linked tickets, deploy filesKunibert