From 32f681e17504a30be36426b258b34d2449f68041 Mon Sep 17 00:00:00 2001 From: Michelangelo Date: Wed, 24 Jun 2020 09:56:28 +0200 Subject: [PATCH] Customizable signature header name, rename `secret` to `signature_secret` --- main.go | 33 ++++++++++++++++++++------------- plugin.go | 29 +++++++++++++++-------------- 2 files changed, 35 insertions(+), 27 deletions(-) diff --git a/main.go b/main.go index 2980c0d..9fae1cc 100644 --- a/main.go +++ b/main.go @@ -71,9 +71,15 @@ func main() { EnvVar: "PLUGIN_SKIP_VERIFY", }, cli.StringFlag{ - Name: "secret", + Name: "signature_header", + Usage: "header name to use in request", + EnvVar: "PLUGIN_SIGNATURE_HEADER,WEBHOOK_SIGNATURE_HEADER", + Value: "X-Drone-Signature", + }, + cli.StringFlag{ + Name: "signature_secret", Usage: "secret to generate signature", - EnvVar: "PLUGIN_SECRET,WEBHOOK_SECRET", + EnvVar: "PLUGIN_SIGNATURE_SECRET,WEBHOOK_SIGNATURE_SECRET", }, cli.StringFlag{ Name: "repo.owner", @@ -191,17 +197,18 @@ func run(c *cli.Context) error { Started: c.Int64("job.started"), }, Config: Config{ - Method: c.String("method"), - Username: c.String("username"), - Password: c.String("password"), - ContentType: c.String("content-type"), - Template: c.String("template"), - Headers: c.StringSlice("headers"), - URLs: c.StringSlice("urls"), - ValidCodes: c.IntSlice("valid-response-codes"), - Debug: c.Bool("debug"), - SkipVerify: c.Bool("skip-verify"), - Secret: c.String("secret"), + Method: c.String("method"), + Username: c.String("username"), + Password: c.String("password"), + ContentType: c.String("content-type"), + Template: c.String("template"), + Headers: c.StringSlice("headers"), + URLs: c.StringSlice("urls"), + ValidCodes: c.IntSlice("valid-response-codes"), + Debug: c.Bool("debug"), + SkipVerify: c.Bool("skip-verify"), + SignatureHeader: c.String("signature_header"), + SignatureSecret: c.String("signature_secret"), }, } diff --git a/plugin.go b/plugin.go index 528775d..0746429 100644 --- a/plugin.go +++ b/plugin.go @@ -40,17 +40,18 @@ type ( } Config struct { - Method string - Username string - Password string - ContentType string - Template string - Headers []string - URLs []string - ValidCodes []int - Debug bool - SkipVerify bool - Secret string + Method string + Username string + Password string + ContentType string + Template string + Headers []string + URLs []string + ValidCodes []int + Debug bool + SkipVerify bool + SignatureHeader string + SignatureSecret string } Job struct { @@ -121,14 +122,14 @@ func (p Plugin) Exec() error { req.Header.Set("Content-Type", p.Config.ContentType) - if p.Config.Secret != "" { + if p.Config.SignatureSecret != "" { // generate signature with secret and body - h := hmac.New(sha256.New, []byte(p.Config.Secret)) + h := hmac.New(sha256.New, []byte(p.Config.SignatureSecret)) h.Write(b) sha := hex.EncodeToString(h.Sum(nil)) // append signature to headers - req.Header.Set("X-Drone-Signature", fmt.Sprintf("sha256=%s", sha)) + req.Header.Set(p.Config.SignatureHeader, fmt.Sprintf("sha256=%s", sha)) } for _, value := range p.Config.Headers {