gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Main branchmainbd3591c7conductor: gates run on the live day44 binarymremain/lib/intake.hl

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

Branches

Latest commits

  • bd3591c7conductor: gates run on the live day44 binarymre
  • 47f747a7records: jukebox #9 fix livemre
  • 0a3169fdMerge branch 'vendored-size' into mainKunibert
  • e447bb09Vendored size fix: keep renames with an app side whole; type the filter parameter (L10)mre
  • 99ccc742Vendored size fix: no renames in the app diff, filter the resumed delta toomre
  • 1a52e100|||PR Diff size limit counts only app files when vendored paths are setmre
  • 50a1867estatus: jukebox #9mre
  • c57484e6log: jukebox #9 diff sizemre
  • 795a20carecords: guard fix live (PR 8)mre
  • 6981d96fMerge branch 'fresh-on-nocmd' into mainKunibert
  • 37c47840runner: one shared prompt for both fresh-session fallbacks (L16)mre
  • f945d2e1|||PR runner: a resumed run with no command on a new head gets one fresh session instead of a tool errormre
  • 10b2a49estatus: PR 314, guard fix proposedmre
  • e73995a3log: hycon PR 314 approved after fresh reviewmre
  • f60427falog: hycon PR 314 false tool errormre
  • 481974f7log: Hermes accepts PR 259 findingmre
  • bb0976acrecords: hycon PR 259 design findingmre
  • 5c637c24records: hycon PR 259 false tool errormre
  • a344c406log: routger 132 reviewedmre
  • 56e70dcbrecords: review time limit 1800 s (routger 132)mre