gitoria

kunibert

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitc9b48bfec9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibertc9b48bfe/lib/intake.hl

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

Branches

Latest commits

  • c9b48bfeVendor Hybriel plugins; deploy ships bin/ from zig-outKunibert
  • a41ba15fKunibert: event intake, git cache, review runner, posting, linked tickets, deploy filesKunibert