代码之家  ›  专栏  ›  技术社区  ›  raphink

用curl和secret模拟github钩子

  •  1
  • raphink  · 技术社区  · 9 年前

    我有类似的需求 Emulate github service hooks wih curl ,但我也在我的webhook中使用了一个秘密,它无法正常工作。

    下面是我作为一个post-receive钩子所做的:

    #!/bin/bash
    
    while read oldrev newrev refname; do
      tmpfile=$(mktemp --suffix=.json)
    
      cat << EOF > $tmpfile
      {
        "ref": "${refname}"
      }
    EOF
    
      sig=$(cat "${tmpfile}" | openssl dgst -sha1 -hmac "${WEBHOOK_SECRET}" | awk '{print "X-Hub-Signature: sha1="$2}')
    
      curl -X POST -H "Content-Type: application/json" -H "${sig}" --data-urlencode "payload@${tmpfile}" http://webhook:9000/hooks/r10k
    
      rm -f "${tmpfile}"
    done
    

    webhook(与github一起工作)抱怨签名错误。

    我做错了什么?

    1 回复  |  直到 7 年前
        1
  •  5
  •   raphink    9 年前

    问题来自openssl命令中输入流末尾的回车。

    我改为:

    #!/bin/bash
    
    while read oldrev newrev refname; do
      tmpfile=$(mktemp --suffix=.json)
    
      data="{\"ref\": \"${refname}\"}"
    
      sig=$(echo -n "${data}" | openssl dgst -sha1 -hmac "%{WEBHOOK_SECRET}" | awk '{print "X-Hub-Signature: sha1="$2}')
    
      curl -X POST -H "Content-Type: application/json" -H "${sig}" --data "${data}" http://webhook:9000/hooks/r10k
    
      rm -f "${tmpfile}"
    done
    

    它起了作用。