mirror of
https://github.com/drone-plugins/drone-webhook.git
synced 2026-06-14 05:12:43 +08:00
add support for signature header
This commit is contained in:
@@ -1,5 +1,7 @@
|
|||||||
module github.com/drone-plugins/drone-webhook
|
module github.com/drone-plugins/drone-webhook
|
||||||
|
|
||||||
|
go 1.14
|
||||||
|
|
||||||
require (
|
require (
|
||||||
bou.ke/monkey v1.0.1 // indirect
|
bou.ke/monkey v1.0.1 // indirect
|
||||||
github.com/aymerick/raymond v2.0.2+incompatible // indirect
|
github.com/aymerick/raymond v2.0.2+incompatible // indirect
|
||||||
|
|||||||
@@ -70,6 +70,11 @@ func main() {
|
|||||||
Usage: "skip ssl verification",
|
Usage: "skip ssl verification",
|
||||||
EnvVar: "PLUGIN_SKIP_VERIFY",
|
EnvVar: "PLUGIN_SKIP_VERIFY",
|
||||||
},
|
},
|
||||||
|
cli.StringFlag{
|
||||||
|
Name: "secret",
|
||||||
|
Usage: "secret to generate signature",
|
||||||
|
EnvVar: "PLUGIN_SECRET,WEBHOOK_SECRET",
|
||||||
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "repo.owner",
|
Name: "repo.owner",
|
||||||
Usage: "repository owner",
|
Usage: "repository owner",
|
||||||
@@ -196,6 +201,7 @@ func run(c *cli.Context) error {
|
|||||||
ValidCodes: c.IntSlice("valid-response-codes"),
|
ValidCodes: c.IntSlice("valid-response-codes"),
|
||||||
Debug: c.Bool("debug"),
|
Debug: c.Bool("debug"),
|
||||||
SkipVerify: c.Bool("skip-verify"),
|
SkipVerify: c.Bool("skip-verify"),
|
||||||
|
Secret: c.String("secret"),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -2,7 +2,10 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"crypto/hmac"
|
||||||
|
"crypto/sha256"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
@@ -47,6 +50,7 @@ type (
|
|||||||
ValidCodes []int
|
ValidCodes []int
|
||||||
Debug bool
|
Debug bool
|
||||||
SkipVerify bool
|
SkipVerify bool
|
||||||
|
Secret string
|
||||||
}
|
}
|
||||||
|
|
||||||
Job struct {
|
Job struct {
|
||||||
@@ -117,6 +121,16 @@ func (p Plugin) Exec() error {
|
|||||||
|
|
||||||
req.Header.Set("Content-Type", p.Config.ContentType)
|
req.Header.Set("Content-Type", p.Config.ContentType)
|
||||||
|
|
||||||
|
if p.Config.Secret != "" {
|
||||||
|
// generate signature with secret and body
|
||||||
|
h := hmac.New(sha256.New, []byte(p.Config.Secret))
|
||||||
|
h.Write(b)
|
||||||
|
sha := hex.EncodeToString(h.Sum(nil))
|
||||||
|
|
||||||
|
// append signature to headers
|
||||||
|
req.Header.Set("X-Drone-Signature", sha)
|
||||||
|
}
|
||||||
|
|
||||||
for _, value := range p.Config.Headers {
|
for _, value := range p.Config.Headers {
|
||||||
header := strings.Split(value, "=")
|
header := strings.Split(value, "=")
|
||||||
req.Header.Set(header[0], header[1])
|
req.Header.Set(header[0], header[1])
|
||||||
|
|||||||
Reference in New Issue
Block a user