gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commit8d34ca338d34ca33records: repo rename (gitoria#53)mre8d34ca33/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

  • 8d34ca33records: repo rename (gitoria#53)mre
  • 1e05cb3fMerge branch 'repo-rename' into mainKunibert
  • 6ee0d2e9|||PR intake: match a delivery by repo.id and signature, not URLs (gitoria#53 rename)mre
  • 900ffcd4status: routgermre
  • 59b0b793log: routger merge waitmre
  • e52f094alog: routger intake check (hycon#122)mre
  • f979027frecords: design rule livemre
  • 71efaf84Merge branch 'design-guard' into mainKunibert
  • c0656581|||PR Flag removal of a documented design as major unless his recorded yes is linked; rules version 5mre
  • 901cdd75records: L10 update 2 livemre
  • 4a68016bMerge branch 'hl-standard-l10b' into mainKunibert
  • 246d0f30|||PR HL-STANDARD L10 update (hybriel ab293e5a), rules version 4mre
  • 1aa4d64crecords: HL-STANDARD L10 update livemre
  • 2b04c240Merge branch 'hl-standard-l10' into mainKunibert
  • 88c7b09d|||PR HL-STANDARD L10 update (hybriel 5885d688), rules version 3mre
  • 00869ecbrecords: gitoria#50 step 3 livemre
  • f97a0e5fMerge branch 'hl-standard' into mainKunibert
  • 9710e39fType STANDARD as String (L10, Kunibert review)mre
  • 4140b664|||PR Review against HL-STANDARD and cite rule ids (gitoria#50 step 3)mre
  • 75c6888drecords: gitoria#50 step 1 connected and provenmre