adnanh webhook 框架execute-command 以及參數傳遞處理

adnanh webhook是一個很不錯的webhook 實現,方便靈活。
adnanh webhook 支持如下功能:git

  • 接收請求
  • 解析header 以及負載以及查詢變量
  • 規則檢查
  • 執行命令
    下面進行簡單的參數傳遞測試

環境準備

參考git https://github.com/rongfengliang/adnanh-webhook-docker-composegithub

  • docker-compose
version: "3"
services:
  webhook:
    image: almir/webhook
    command: ["-verbose", "-hooks=/etc/webhook/hooks.json","-hotreload"]
    volumes:
    - "./hooks.json:/etc/webhook/hooks.json"
    - "./shell/:/shells/"
    ports:
    - "9000:9000"
  • hooks 文件
[
    {
      "id": "simple-one",
      "execute-command": "/shells/app",
      "include-command-output-in-response":true,
      "include-command-output-in-response-on-error":true,
      "command-working-directory":"/shells",
      "pass-arguments-to-command":
    [
      {
        "source": "payload",
        "name": "id"
      },
      {
        "source": "url",
        "name": "token"
      }
    ],
      "trigger-rule":
      {
        "match":
        {
          "type": "value",
          "value": "42",
          "parameter":
          {
            "source": "url",
            "name": "token"
          }
        }
      }
    }
  ]
  • shell 腳本
#!/bin/sh
echo $@ , $1, $2

說明

對於請求參數包含token=42 的纔會執行shell 命令,同時將url 參數token 以及payload(post請求)中的id,傳遞給shell
同時配置了將shell 結果輸出到響應,從運行模式來看就相似當前serverless框架的運行原理。web

測試

  • 不符合wehook rule
curl -X POST \
  'http://localhost:9000/hooks/simple-one?token=44' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 85f3425b-a0a7-cf80-c49a-87299d905a6c' \
  -d '{
 "id":"dalongdemo"
}'
Hook rules were not satisfied

  • 規則匹配
curl -X POST \
  'http://localhost:9000/hooks/simple-one?token=42' \
  -H 'cache-control: no-cache' \
  -H 'content-type: application/json' \
  -H 'postman-token: 76d20d8d-a72e-9ae4-2570-115f57479116' \
  -d '{
 "id":"dalongdemo"
}'
dalongdemo 42 , dalongdemo, 42

支持的傳遞給執行命令的參數

  • url
  • header
  • json (payload)

參考資料

https://github.com/rongfengliang/adnanh-webhook-docker-compose
https://github.com/adnanh/webhook/blob/master/docs/Referencing-Request-Values.mddocker

相關文章
相關標籤/搜索