gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commita6b54efaa6b54efaStatus updatemrea6b54efa/lib/intake.hl

5.6 KB

  1. import { Response } from 'hl:http1'
  2. import { readFile } from 'hl:fs'
  3. import { run } from 'hl:proc'
  4. import { timestamp } from 'hl:time'
  5. import { hkdfExtract } from 'hl:crypto'
  6. import Queue from './queue.hl'
  7. import { classify, isKnown, repoId, headerEvent, headerDelivery, headerSignature, signaturePrefix } from './gitoria-events.hl'
  8. \\ Gitoria's signed events: check, answer at once, queue. Thin: the queue logic is lib/queue.hl,
  9. \\ the event names are lib/gitoria-events.hl. Never logs a secret or the body of a bad request.
  10. String secretsFile = ''
  11. String dataDir = ''
  12. Number maxTries = 3
  13. Number backoffSeconds = 30
  14. String reposFile = '' \\ per-repo config: { "<repo.id>": { api, clone, keyFile, secretFile } }; '' = legacy (secretsFile only)
  15. String hybriel = 'hybriel'
  16. Number timeoutSeconds = 900
  17. String runnerData = ''
  18. String ticketsBase = ''
  19. String ticketsTokenFile = ''
  20. String reviewUser = '' \\ unix user the model (codex) runs as; '' = same user as the service
  21. String codexBin = 'codex'
  22. String workDir = '' \\ job worktrees; '' = <runnerData>/work
  23. repoCfg = null
  24. queue = null
  25. defaultSecret = null
  26. repoSecrets = {}
  27. \\ a secret/key file must be mode 600; its first line is the value
  28. readSecret = (String path, String what) => {
  29. m = run(['stat', '-c', '%a', path], { timeout = 10 })
  30. if (m.exit != 0) { hlError(what + ' file not readable: ' + path) }
  31. if (!m.lines.join('').trim().endsWith('00')) { hlError(what + ' file must have mode 600: ' + path) }
  32. v = readFile(path).split(JSON.parse('"\n"'))[0].trim()
  33. if (v == '') { hlError(what + ' file is empty: ' + path) }
  34. return v
  35. }
  36. start = () => {
  37. if (secretsFile != '') {
  38. s = JSON.parse(readFile(secretsFile))
  39. defaultSecret = s.default
  40. repoSecrets = s.repos
  41. if (repoSecrets == null) { repoSecrets = {} }
  42. }
  43. if (reposFile != '') {
  44. repoCfg = JSON.parse(readFile(reposFile))
  45. for (id in repoCfg) {
  46. c = repoCfg[id]
  47. if (c.api == null || c.clone == null || c.keyFile == null || c.secretFile == null) { hlError('repo config ' + id + ' needs api, clone, keyFile, secretFile') }
  48. readSecret(c.keyFile, 'key')
  49. repoSecrets[id] = readSecret(c.secretFile, 'secret')
  50. }
  51. }
  52. queue = new Queue(dataDir = dataDir, maxTries = maxTries, backoffSeconds = backoffSeconds, repos = repoCfg, hybriel = hybriel, timeoutSeconds = timeoutSeconds, runnerData = runnerData, =ticketsBase, =ticketsTokenFile, =reviewUser, =codexBin, =workDir)
  53. queue.load()
  54. }
  55. reply = (Number status, String code, String line, Hybrid extra = {}) => {
  56. console.log(timestamp() + ' hook: ' + status + ' ' + code + (line == '' ? '' : ' ' + line))
  57. body = { status = code }
  58. for (k in extra) { body[k] = extra[k] }
  59. return new Response(JSON.stringify(body), { status = status, headers = { 'Content-Type' = 'application/json' } })
  60. }
  61. \\ "sha256=" + hex HMAC-SHA256(secret, raw body). hl:crypto has no hmac: hkdfExtract(salt, ikm) IS HMAC(salt, ikm).
  62. expected = (String secret, Bytes raw) => {
  63. return signaturePrefix + hkdfExtract(toBytes(secret), raw).hex()
  64. }
  65. anyRepoSecrets = () => {
  66. for (k in repoSecrets) { return true }
  67. return false
  68. }
  69. hook = (Hybrid req) => {
  70. let delivery = req.headers[headerDelivery]
  71. given = req.headers[headerSignature]
  72. if (given == null || given == '') { return reply(401, 'missing_signature', 'delivery=' + delivery) }
  73. let body = null
  74. let secret = defaultSecret
  75. if (anyRepoSecrets()) {
  76. body = JSON.parse(req.body)
  77. id = repoId(body)
  78. if (id != null && repoSecrets[id] != null) { secret = repoSecrets[id] }
  79. }
  80. if (secret == null || given != expected(secret, req.bytes)) { return reply(401, 'bad_signature', 'delivery=' + delivery) }
  81. name = req.headers[headerEvent]
  82. if (!isKnown(name)) { return reply(202, 'ignored', 'unknown event ' + name + ' delivery=' + delivery, { reason = 'unknown event' }) }
  83. if (body == null) { body = JSON.parse(req.body) }
  84. if (delivery == null || delivery == '') { delivery = body.delivery }
  85. if (delivery == null || delivery == '') { return reply(400, 'missing_delivery', 'event=' + name) }
  86. if (repoCfg != null && body.repo != null) {
  87. id = repoId(body)
  88. c = repoCfg[id]
  89. if (c == null) { return reply(403, 'repo_mismatch', 'repo ' + id + ' is not configured delivery=' + delivery) }
  90. if (body.repo.api != c.api) { return reply(403, 'repo_mismatch', 'repo ' + id + ' api differs from the config delivery=' + delivery) }
  91. if (body.repo.clone != c.clone) { return reply(403, 'repo_mismatch', 'repo ' + id + ' clone differs from the config delivery=' + delivery) }
  92. }
  93. if (queue.wasSeen(delivery)) { return reply(200, 'duplicate', 'delivery=' + delivery) }
  94. queue.markSeen(delivery)
  95. c = classify(name, body)
  96. if (c.kind == 'ping') { return reply(202, 'ping', 'delivery=' + delivery) }
  97. if (c.kind == 'ignore') { return reply(202, 'ignored', c.reason + ' delivery=' + delivery, { reason = c.reason }) }
  98. if (c.kind == 'close') {
  99. r = queue.close(c)
  100. return reply(202, 'closed', r.key + ' dropped=' + r.dropped + ' removeSession=true delivery=' + delivery, { job = r.key, dropped = r.dropped })
  101. }
  102. if (c.kind == 'thread-state') {
  103. r = queue.recordThread(c)
  104. return reply(202, r.action, r.key + ' ' + c.event + ' delivery=' + delivery, { job = r.key })
  105. }
  106. r = queue.enqueue(c)
  107. return reply(202, r.action, r.key + ' ' + c.event + ' head=' + c.head + ' delivery=' + delivery, { job = r.key, threadReplyOnly = r.threadReplyOnly })
  108. }
  109. health = () => {
  110. return queue.health()
  111. }
  112. tick = () => {
  113. return queue.tick()
  114. }

Branches

Latest commits

  • a6b54efaStatus updatemre
  • 5a72268eRecords: #4-#10 done, #11 started, review package on #1mre
  • 194c7da6Records: real proof greenmre
  • cdfa94a9Records: AppArmor profile for the review sandboxmre
  • c1bf02d8Status: sandbox blockermre
  • 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