onwebhook.adoc 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. [#exec-webhook]
  2. = Execute on webhook
  3. Webhooks allow external services to trigger OliveTin actions by sending HTTP POST requests. This is useful for integrating OliveTin with CI/CD pipelines, monitoring systems, IoT devices, or any service that can send HTTP requests.
  4. OliveTin provides a dedicated webhook endpoint at `/webhooks` that can receive webhook payloads and match them to configured actions.
  5. == Basic Configuration
  6. To configure an action to run on a webhook, add the `execOnWebhook` property to your action. Webhook-triggered actions **must** use `exec:` (not `shell:` or `shellAfterCompleted`). This also applies to any action started through `triggers` from a webhook-triggered action, at any depth of the trigger chain (see xref:upgrade/upgrade_notes.adoc#webhook-trigger-chains-exec-only[Upgrade Notes]).
  7. [source,yaml]
  8. .`config.yaml`
  9. ----
  10. actions:
  11. - title: Deploy Application
  12. id: deploy
  13. exec:
  14. - /opt/scripts/deploy.sh
  15. execOnWebhook:
  16. - authType: bearer
  17. secret: "${{ WEBHOOK_TOKEN }}"
  18. matchHeaders:
  19. X-Event-Type: deploy
  20. ----
  21. This action will be triggered when a POST request is sent to `/webhooks` with the header `X-Event-Type: deploy` and the header `Authorization: Bearer <token>`, where the token is the value of the `WEBHOOK_TOKEN` environment variable. Every webhook needs an `authType`; see <<every-webhook-needs-an-authtype,Webhook Authentication>>.
  22. == Webhook Endpoint
  23. All webhooks are received at:
  24. ----
  25. http://your-olivetin-server:1337/webhooks
  26. ----
  27. or
  28. ----
  29. http://your-olivetin-server:1337/webhooks/
  30. ----
  31. Both paths work identically. All webhook requests must use the HTTP POST method.
  32. == Matching Webhooks
  33. OliveTin can match incoming webhooks based on several criteria:
  34. === Match by Headers
  35. Match webhooks based on HTTP header values:
  36. [source,yaml]
  37. ----
  38. actions:
  39. - title: Process Event
  40. exec:
  41. - echo
  42. - "Processing event"
  43. execOnWebhook:
  44. - authType: bearer
  45. secret: "${{ WEBHOOK_TOKEN }}"
  46. matchHeaders:
  47. X-Event-Type: my-event
  48. X-Source: my-service
  49. ----
  50. All specified headers must match for the webhook to trigger the action.
  51. === Match by Query Parameters
  52. Match webhooks based on URL query parameters:
  53. [source,yaml]
  54. ----
  55. actions:
  56. - title: Process Request
  57. exec:
  58. - echo
  59. - "Processing request for {{ service }}"
  60. arguments:
  61. - name: service
  62. type: ascii
  63. execOnWebhook:
  64. - authType: bearer
  65. secret: "${{ WEBHOOK_TOKEN }}"
  66. matchQuery:
  67. action: deploy
  68. env: production
  69. extract:
  70. service: "$.service"
  71. ----
  72. A request to `/webhooks?action=deploy&env=production` with a JSON body containing `"service"` would match this action and pass that field into the `service` argument.
  73. === Match by JSON Body Path
  74. Match webhooks based on values in the JSON request body using JSONPath expressions:
  75. [source,yaml]
  76. ----
  77. actions:
  78. - title: Handle Push Event
  79. exec:
  80. - echo
  81. - "Push to {{ branch }}"
  82. arguments:
  83. - name: branch
  84. type: ascii
  85. execOnWebhook:
  86. - authType: bearer
  87. secret: "${{ WEBHOOK_TOKEN }}"
  88. matchPath: "$.event_type=push"
  89. extract:
  90. branch: "$.branch"
  91. ----
  92. The `matchPath` format is `jsonpath=value`. You can also just specify a JSONPath without a value to match if the path exists:
  93. [source,yaml]
  94. ----
  95. execOnWebhook:
  96. - authType: bearer
  97. secret: "${{ WEBHOOK_TOKEN }}"
  98. matchPath: "$.repository.name" # Matches if this path exists in the JSON
  99. ----
  100. === Using Regex for Matching
  101. Header and query parameter values can use regex patterns by prefixing with `regex:`:
  102. [source,yaml]
  103. ----
  104. actions:
  105. - title: Handle Multiple Events
  106. exec:
  107. - echo
  108. - "Handling event"
  109. execOnWebhook:
  110. - authType: bearer
  111. secret: "${{ WEBHOOK_TOKEN }}"
  112. matchHeaders:
  113. X-Event-Type: "regex:^(push|pull_request|release)$"
  114. ----
  115. === Combining Match Criteria
  116. You can combine multiple match criteria. All criteria must match for the webhook to trigger:
  117. [source,yaml]
  118. ----
  119. actions:
  120. - title: Production Deploy
  121. exec:
  122. - /opt/scripts/deploy.sh
  123. - production
  124. execOnWebhook:
  125. - authType: bearer
  126. secret: "${{ WEBHOOK_TOKEN }}"
  127. matchHeaders:
  128. X-Event-Type: deploy
  129. matchQuery:
  130. environment: production
  131. matchPath: "$.status=approved"
  132. ----
  133. == Extracting Arguments from Webhooks
  134. You can extract values from the webhook payload and pass them as arguments to your action using JSONPath expressions:
  135. [source,yaml]
  136. ----
  137. actions:
  138. - title: Deploy Version
  139. exec:
  140. - /opt/scripts/deploy.sh
  141. - "{{ version }}"
  142. - "{{ environment }}"
  143. arguments:
  144. - name: version
  145. type: ascii
  146. - name: environment
  147. type: ascii
  148. execOnWebhook:
  149. - authType: bearer
  150. secret: "${{ WEBHOOK_TOKEN }}"
  151. matchHeaders:
  152. X-Event-Type: deploy
  153. extract:
  154. version: "$.release.tag_name"
  155. environment: "$.target.environment"
  156. ----
  157. The `extract` map defines which action arguments to populate from the webhook payload. The key is the argument name, and the value is the JSONPath expression to extract the value.
  158. === Automatic Webhook Metadata
  159. OliveTin automatically adds several metadata arguments from each webhook request:
  160. * `webhook_method` - The HTTP method (always POST for webhooks)
  161. * `webhook_path` - The request URL path
  162. * `webhook_query` - The raw query string
  163. * `webhook_header_<name>` - Each HTTP header (lowercase name)
  164. For example, to access the `X-Request-Id` header in your action:
  165. [source,yaml]
  166. ----
  167. actions:
  168. - title: Log Request
  169. exec:
  170. - echo
  171. - 'Request ID: {{ index .Arguments "webhook_header_x-request-id" }}'
  172. arguments:
  173. - name: webhook_header_x-request-id
  174. type: ascii
  175. execOnWebhook:
  176. - authType: bearer
  177. secret: "${{ WEBHOOK_TOKEN }}"
  178. matchHeaders:
  179. X-Event-Type: log
  180. ----
  181. Header names that contain hyphens become argument keys with the same hyphens (for example `webhook_header_x-request-id`). Use the `index` map-lookup form shown above, because Go templates treat hyphens in bare identifiers as subtraction.
  182. These metadata arguments come from the request's path, query string and headers, which HMAC signatures do not cover. Do not rely on them for values an action needs to trust; see <<what-hmac-signatures-cover,What HMAC signatures cover>>.
  183. == Webhook Authentication
  184. OliveTin supports several authentication methods to verify webhook requests:
  185. [#every-webhook-needs-an-authtype]
  186. === Every webhook needs an `authType`
  187. Every webhook must set an `authType`. A webhook without one is rejected, and the xref:advanced_configuration/diagnostics.adoc[Diagnostics] page shows an error for it (code `webhook_auth_type_unset`). Webhooks that use one of the xref:action_execution/onwebhook_github.adoc[GitHub templates] choose their own `authType`. A `template` name that OliveTin does not know is ignored, and Diagnostics shows an error for it (code `webhook_template_unknown`). See xref:upgrade/upgrade_notes.adoc#webhook-auth-type-required[Upgrade Notes].
  188. `matchHeaders`, `matchQuery` and `matchPath` only decide which action a request is for. They are not secrets, so use one of the authentication methods below unless anyone who can reach `/webhooks` may run the action. Webhook-triggered actions run as the `system` usergroup, not as the caller (see xref:security/acl.adoc#acls-for-triggers[ACLs for triggers]).
  189. === No Authentication
  190. To accept webhook requests without any authentication, set `authType: none` explicitly. Any request that matches will then trigger the action, so only do this for actions that are safe for anyone to run:
  191. [source,yaml]
  192. ----
  193. execOnWebhook:
  194. - authType: none
  195. matchHeaders:
  196. X-Event-Type: my-event
  197. ----
  198. === HMAC-SHA256 Signature
  199. Verify webhooks using HMAC-SHA256 signatures (commonly used by GitHub, GitLab, etc.):
  200. [source,yaml]
  201. ----
  202. execOnWebhook:
  203. - authType: hmac-sha256
  204. authHeader: X-Hub-Signature-256
  205. secret: your-webhook-secret
  206. matchHeaders:
  207. X-Event-Type: push
  208. ----
  209. The `authHeader` specifies which header contains the signature. The signature should be in the format `sha256=<hex-encoded-signature>`.
  210. === HMAC-SHA1 Signature
  211. For services using HMAC-SHA1 (legacy GitHub webhooks):
  212. [source,yaml]
  213. ----
  214. execOnWebhook:
  215. - authType: hmac-sha1
  216. authHeader: X-Hub-Signature
  217. secret: your-webhook-secret
  218. matchHeaders:
  219. X-Event-Type: push
  220. ----
  221. [#what-hmac-signatures-cover]
  222. === What HMAC signatures cover
  223. HMAC signatures, including the ones checked for the GitHub `template` webhooks, are worked out by the sender over the request body only. This is how GitHub, GitLab and similar services sign webhooks, so OliveTin cannot check anything more than the sender signed:
  224. * The request path, query string and headers are not signed. Anyone who has seen one signed request can send it again with a different path, query string or headers, and it will still pass the signature check.
  225. * There is no timestamp or nonce, so a signed request can be sent again, and it will run the action again.
  226. To keep webhooks safe:
  227. * Send webhooks to OliveTin over HTTPS, so that signed requests cannot be read or changed on the way.
  228. * Take any value that an action needs to trust from the request body, with `extract`. Do not use the `webhook_path`, `webhook_query` or `webhook_header_*` arguments for those values with HMAC webhooks. The xref:advanced_configuration/diagnostics.adoc[Diagnostics] page shows a warning (code `webhook_unsigned_argument`) for an action that does.
  229. * Make webhook-triggered actions safe to run more than once.
  230. `bearer` and `basic` authentication do not sign anything: they check that the request carries the secret. Anyone who has the secret can send any request.
  231. === Bearer Token
  232. Verify webhooks using a Bearer token in the Authorization header:
  233. [source,yaml]
  234. ----
  235. execOnWebhook:
  236. - authType: bearer
  237. secret: your-bearer-token
  238. matchHeaders:
  239. X-Event-Type: deploy
  240. ----
  241. The webhook sender must include `Authorization: Bearer your-bearer-token` in the request.
  242. === Basic Authentication
  243. Verify webhooks using HTTP Basic authentication:
  244. [source,yaml]
  245. ----
  246. execOnWebhook:
  247. - authType: basic
  248. secret: "username:password"
  249. matchHeaders:
  250. X-Event-Type: deploy
  251. ----
  252. Or with password only:
  253. [source,yaml]
  254. ----
  255. execOnWebhook:
  256. - authType: basic
  257. secret: "mypassword"
  258. matchHeaders:
  259. X-Event-Type: deploy
  260. ----
  261. == Multiple Webhook Triggers
  262. An action can have multiple webhook configurations. The action will be triggered if any of them match:
  263. [source,yaml]
  264. ----
  265. actions:
  266. - title: Deploy
  267. exec:
  268. - /opt/scripts/deploy.sh
  269. execOnWebhook:
  270. - authType: bearer
  271. secret: "${{ WEBHOOK_TOKEN }}"
  272. matchHeaders:
  273. X-Event-Type: deploy-manual
  274. - authType: bearer
  275. secret: "${{ WEBHOOK_TOKEN }}"
  276. matchHeaders:
  277. X-Event-Type: deploy-auto
  278. matchPath: "$.status=success"
  279. ----
  280. == Testing Webhooks
  281. You can test your webhook configuration using `curl`:
  282. [source,bash]
  283. ----
  284. # Simple webhook with headers and a bearer token
  285. curl -X POST \
  286. -H "Content-Type: application/json" \
  287. -H "Authorization: Bearer $WEBHOOK_TOKEN" \
  288. -H "X-Event-Type: deploy" \
  289. -d '{"version": "1.2.3"}' \
  290. http://localhost:1337/webhooks
  291. # Webhook with HMAC-SHA256 authentication
  292. SECRET="your-secret"
  293. PAYLOAD='{"event": "push", "branch": "main"}'
  294. SIGNATURE=$(echo -n "$PAYLOAD" | openssl dgst -sha256 -hmac "$SECRET" | cut -d' ' -f2)
  295. curl -X POST \
  296. -H "Content-Type: application/json" \
  297. -H "X-Event-Type: push" \
  298. -H "X-Hub-Signature-256: sha256=$SIGNATURE" \
  299. -d "$PAYLOAD" \
  300. http://localhost:1337/webhooks
  301. ----
  302. == See Also
  303. * xref:action_execution/onwebhook_github.adoc[GitHub Webhooks] - Specific configuration for GitHub webhook events
  304. * xref:solutions/on-git-push/index.adoc[GitOps Solution] - Running actions on Git push using hooks
  305. * xref:api/start_action.adoc[Start Action API] - Alternative method for triggering actions via API