Compare commits

...

84 Commits

Author SHA1 Message Date
Adnan Hajdarević c51971fd37 Bump version 2017-02-10 19:19:53 +01:00
Adnan Hajdarević 93e5fe7712 Merge pull request #113 from DG-i/master
make http return code for mismatch rules configurable
2017-02-10 19:19:33 +01:00
Mathias Merscher 058f820cbd rename trigger rule http response code config option
Signed-off-by: Mathias Merscher <Mathias.Merscher@dg-i.net>
2017-02-10 18:42:02 +01:00
Mathias Merscher ddb1f2441a make http return code for mismatched rules configurable
Signed-off-by: Mathias Merscher <Mathias.Merscher@dg-i.net>
2017-02-10 12:32:11 +01:00
Adnan Hajdarević 8226d5e50f Update README.md 2017-02-09 05:22:59 +01:00
Adnan Hajdarević e149c99724 Merge pull request #111 from moorereason/fix106tests
Fix failing header tests
2016-12-22 20:44:57 +01:00
Cameron Moore 6ea2d68b44 Fix failing header tests
Now that we use textproto.CanonicalMIMEHeaderKey, all header field test
data needs to be title-cased.
2016-12-22 10:23:55 -06:00
Adnan Hajdarević 5789362186 Merge pull request #108 from adnanh/development
Update README.md
2016-11-22 15:23:23 +01:00
Adnan Hajdarević 07f166616c Update README.md 2016-11-22 15:23:09 +01:00
Adnan Hajdarević b66216675a Merge pull request #107 from adnanh/development
2.6.0
2016-11-01 20:11:07 +01:00
Adnan Hajdarevic ecbcf11153 fix #106 2016-11-01 20:09:41 +01:00
Adnan Hajdarević 7d525cf317 Merge pull request #105 from adnanh/development
Fixes #103
2016-10-28 16:07:07 +02:00
Adnan Hajdarević e83d7029ff Merge pull request #104 from adnanh/master
Backmerge
2016-10-28 16:06:33 +02:00
Adnan Hajdarević 36c2c692d6 Add instructions for debian 2016-10-28 16:05:39 +02:00
Adnan Hajdarević 86cef3e421 Merge pull request #101 from adnanh/development
webhook 2.5.0
2016-09-29 20:11:46 +02:00
Adnan Hajdarevic 75cf8952be remove \n 2016-09-29 20:11:20 +02:00
Adnan Hajdarevic c53596df59 override content type header when returning error message 2016-09-29 20:08:47 +02:00
Adnan Hajdarevic cc0d9b2cba fix tests, return raw output, return 500 if the command did not execute properly - fixes #87
return raw stdout instead of json wrapped message - fixes #88
2016-09-29 19:57:06 +02:00
Adnan Hajdarević c6530b17e7 Merge pull request #100 from adnanh/env-names
Do not prefix EnvName with HOOK_
2016-09-29 19:20:06 +02:00
Adnan Hajdarevic 1943c5311f bump version to 2.5.0 2016-09-29 19:16:47 +02:00
Adnan Hajdarevic 3b59539a33 do not prefix specified environment variable name with HOOK_ (fixes #98) 2016-09-29 19:15:51 +02:00
Adnan Hajdarević 923b0c6daa Merge pull request #95 from denji/Makefile
Makefile cross-platform binary
2016-09-13 09:20:11 +02:00
Denis Denisov 8530255ae6 Makefile build cross-binary 2016-09-12 23:09:05 +03:00
Adnan Hajdarević 032c74451f Merge pull request #93 from adnanh/development
Development
2016-09-02 18:30:31 +02:00
Adnan Hajdarević 1c50853d8d Merge pull request #92 from moorereason/iss91
Update negroni Logger usage
2016-09-02 18:29:27 +02:00
Cameron Moore b5ed4cbea7 Update negroni Logger usage
negroni made a breaking change to the Logger struct.

Fixes #91
2016-09-02 08:57:46 -05:00
Adnan Hajdarević b6d176705e Merge pull request #90 from adnanh/development
Development
2016-08-25 23:45:27 +02:00
Adnan Hajdarević 421fc2cbcd Hotfix backmerge (#89)
* fixes #76, fixes #78, fixes #82, fixes #83 (#84)

* Never disclose expected payload signature (#86)

Fixes #85
2016-08-25 23:42:33 +02:00
Cameron Moore 10d65dd2fd Never disclose expected payload signature (#86)
Fixes #85
2016-08-25 23:41:05 +02:00
Adnan Hajdarević 54a9dbe1d6 fixes #76, fixes #78, fixes #82, fixes #83 (#84) 2016-06-27 22:15:37 +02:00
Adnan Hajdarevic 30baec91df fixes #76, fixes #78, fixes #82, fixes #83 2016-06-27 22:13:00 +02:00
Adnan Hajdarevic 3bcf6d5e2b bump version to 2.3.9 2016-06-18 15:33:37 +02:00
Adnan Hajdarevic 67343e281d Merge branch 'master' into development 2016-06-18 15:31:13 +02:00
Florent Aide 18b0573bc4 Add support for naming env variables (#75)
* Adding ignore patterns

* Adding support for env var naming

* Fixed typo in docstring

* Adding tests for the env var extraction w & w/o explicit naming

* remove coverage script from ignore patterns

* Adding the coverage script to help see which code is tested and which is not

* remove coverage script from sources

* Ignore coverage script from sources tree
2016-05-26 23:33:56 +02:00
Adnan Hajdarević ec42679305 Merge pull request #69 from adnanh/development
version 2.3.8
2016-03-24 16:55:34 +01:00
Adnan Hajdarevic e85e0592dd Merge branch 'master' into development 2016-03-24 16:53:37 +01:00
Adnan Hajdarević 4d20af8027 Update webhook.go 2016-03-24 16:43:42 +01:00
Adnan Hajdarević d4e772c815 Add almir's docker image to the readme. 2016-03-15 19:09:06 +01:00
Adnan Hajdarević 04a2b2a680 Update README.md 2016-03-09 14:12:37 +01:00
Adnan Hajdarevic 4914a4131f Merge branch 'master' into development 2016-02-27 22:16:28 +01:00
Adnan Hajdarevic 37698e63b6 add support for setting global response headers using -header flag
add support for setting response headers for a successfuly triggered hook
2016-02-27 22:13:09 +01:00
Adnan Hajdarević 80aa9800bf Merge pull request #64 from adnanh/development
Update README.md to include Dockerfile discussion
2016-02-04 15:13:07 +01:00
Adnan Hajdarević f620cb056b Update README.md 2016-02-04 15:12:24 +01:00
Adnan Hajdarević e55e7efe14 Merge pull request #62 from adnanh/development
Update README.md
2015-12-29 20:39:41 +01:00
Adnan Hajdarević 9fa02f7341 Update README.md 2015-12-29 20:38:36 +01:00
Adnan Hajdarevic f59f0a5c84 - added omitempty to json fields 2015-12-27 20:05:52 +01:00
Adnan Hajdarević 5594a62f8f Merge pull request #61 from adnanh/development
- added omitempty to json fields
2015-12-27 20:04:03 +01:00
Adnan Hajdarevic 642516d46e Merge branch 'master' into development 2015-11-27 09:57:44 +01:00
Adnan Hajdarevic 9cef8ed882 add omitempty to json fields 2015-11-21 17:06:02 +01:00
Adnan Hajdarević 8de1a51926 Merge pull request #56 from adnanh/development
Bugfixes
2015-11-19 12:35:16 +01:00
Adnan Hajdarević ed9d557a13 Merge pull request #55 from moorereason/fixenv
Fix environment and argument passing
2015-11-19 09:53:31 +01:00
Cameron Moore d2e315d9c6 Fix environment and argument passing
Two issues are addressed in this commit:

1. Instead of only sending the predefined environment arguments, this
commit appends the arguments to the existing OS environment.  Fixes #53.

2. If an argument is not found in the payload, allow the command to run
and pass in an empty string as a placeholder.  Fixes #54.

Additionally, I replaced `hook.ErrInvalidPayloadSignature` with a new
`SignatureError` type so that we can embed the signature in the error.
2015-11-18 12:00:47 -06:00
Adnan Hajdarević a7aa7f2327 Merge pull request #52 from adnanh/development
2.3.6
2015-11-13 09:26:08 +01:00
Adnan Hajdarevic 82bba7ba07 bump up the version 2015-11-13 09:24:17 +01:00
Adnan Hajdarevic 8a9ff6d06c removed makefile conflict 2015-11-11 09:50:36 +01:00
Adnan Hajdarević 3080fd115b Merge pull request #51 from moorereason/focus
Remove webhook-contrib content and update README
2015-11-11 09:44:05 +01:00
Cameron Moore b314eda1f9 Remove webhook-contrib content and update README
The Docker, Travis-CI, RPM, and init files are being moved to the
separate `webhook-contrib` repository.
2015-11-06 15:09:29 -06:00
Adnan Hajdarević 8544653787 Merge pull request #49 from moorereason/earlyreturn
Remove unnecessary else clause
2015-11-03 21:14:32 +01:00
Cameron Moore 8d1498e285 Remove unnecessary else clause 2015-11-03 10:48:16 -06:00
Adnan Hajdarević 2ed15e35bc Merge pull request #48 from almir/development
Adjust Makefile syntax and add docker section in README.md
2015-11-03 10:13:46 +01:00
almir aff3a155d7 - fix hooks.json file name in readme 2015-11-03 09:28:42 +01:00
almir 5f041cb9ae - add docker section to readme 2015-11-03 09:25:53 +01:00
almir b7081f3934 - adjust Makefile syntax 2015-11-03 09:07:38 +01:00
Adnan Hajdarević 2026328c56 Merge pull request #47 from moorereason/hookecho
Add environment arguments and improve testing
2015-11-02 22:32:27 +01:00
Cameron Moore ea3dbf3438 Add environment arguments and improve testing
There's a lot in this commit.

 1. Add `pass-environment-to-command` option that works much like
 `pass-arguments-to-command`.  You can see an example usage in the
 "github" test case.

 2. Add a test program called "hookecho" that is used to test the
 webhook package instead of relying upon a system `echo` command.

 3. Move hooks_test.json to a template so that we can update the path to
 hookecho on the fly.

 4. Don't return an error at the end of hook.MatchRule.Evaluate().  All
 tests succeed for me now.
2015-11-02 15:11:23 -06:00
Adnan Hajdarević 6774079a57 Merge pull request #46 from moorereason/testing
Add testing framework for main webhook app
2015-10-31 09:29:34 +01:00
Adnan Hajdarević 334fbc814c Merge pull request #45 from moorereason/signals
Refactor signal handling and Windows support
2015-10-31 09:29:22 +01:00
Cameron Moore 802f3f572c Add testing framework for main webhook app
This commit adds a testing framework modeled after the godoc tests. It builds
webhook in a temporary directory, runs it with the supplied `hooks_test.json`
configuration, and then tests different payloads.  I use `/bin/echo` for the
test executable, so I've added build tags to exclude Windows.

Three minor (I hope) changes in functionality:

- I ended up moving everything from `init()` to `main()` because `init()` was
  firing while trying to build the tests, and it was dying since `hooks.json`
  didn't exist. I'm still not 100% sure `init()` was firing, but I didn't see
  any real need for anything to be in `init()` in the first place.

- make sure logger is using `os.Stderr`

- don't send `http.StatusBadRequest` when the Hook rules don't match.  "Bad
  Request" is used to identify malformed requests.  The request was properly
  formed and processed, so I think we should send back `http.StatusOK`.  For
  example, if I setup a webhook rule to only execute when commits are made to
  the `master` branch, we shouldn't send back `http.StatusBadRequest` when we
  ingest a payload for the `development` branch.

The test payloads are pretty verbose and could probably be shortened, but I kind
of like having an example payload for each service.  We can pare them down if we
want to do more focused, minimalist testing.
2015-10-30 21:32:48 -05:00
Adnan Hajdarević 6da00561bf Merge pull request #44 from timhughes/master
Pull request into development instead of master
2015-10-30 17:25:58 +01:00
Adnan Hajdarević 2e40847eb9 Merge pull request #43 from adnanh/revert-42-master
Revert "adding rpm spec file and sysv init script"
2015-10-30 17:22:48 +01:00
Adnan Hajdarević 5f7dd7211b Revert "adding rpm spec file and sysv init script" 2015-10-30 17:22:38 +01:00
Adnan Hajdarević db25fcd06d Merge pull request #42 from timhughes/master
adding rpm spec file and sysv init script
2015-10-30 17:22:03 +01:00
Tim Hughes d1b4ef767a Add travis test status badge to README.md 2015-10-30 01:17:06 +00:00
Tim Hughes 64b3256408 enable unit tests and add in init script and example hooks.json file to rpmspec 2015-10-30 00:49:38 +00:00
Tim Hughes bfe4c148a5 add in make target 2015-10-29 21:58:29 +00:00
Tim Hughes 033764601d typo in travis.yml 2015-10-29 21:51:43 +00:00
Tim Hughes 8173f4a346 add travis.yml 2015-10-29 21:50:46 +00:00
Tim Hughes ffd05a2101 adding a rpm spec file and init script 2015-10-29 21:43:13 +00:00
Adnan Hajdarević fea31474bd Merge pull request #41 from moorereason/devsidefx
Remove logging side-effects from hook package
2015-10-29 19:05:15 +01:00
Cameron Moore 1c319a7a08 Refactor signal handling and Windows support
This commit drops webhook_windows.go in favor of simply pulling out the
signal handling code to separate files.
2015-10-29 11:17:15 -05:00
Cameron Moore 2947e5e0e8 Remove logging side-effects from hook package
The hook package should be self-contained and return errors instead of
relying on the log subsystem.  This commit removes the logging
side-effects from the hook package.  Custom errors are returned that
should be transparent to the caller -- they can just treat them as
simple errors if they don't care to check the type.
2015-10-29 11:00:30 -05:00
Adnan Hajdarević 409b441c31 Merge pull request #40 from adnanh/development
Add Dockerfile & Makefile
2015-10-28 11:52:31 +01:00
Adnan Hajdarević 93505b4132 Merge pull request #39 from almir/development
- create dockerfile and makefile for building docker image
2015-10-28 11:34:11 +01:00
almir c350784507 - create dockerfile and makefile for building docker image 2015-10-28 10:54:36 +01:00
13 changed files with 1313 additions and 546 deletions
+4
View File
@@ -0,0 +1,4 @@
.idea
.cover
coverage
webhook
+29
View File
@@ -0,0 +1,29 @@
OS = darwin freebsd linux openbsd windows
ARCHS = 386 arm amd64 arm64
all: build release
build: deps
go build
release: clean deps
@for arch in $(ARCHS);\
do \
for os in $(OS);\
do \
echo "Building $$os-$$arch"; \
mkdir -p build/webhook-$$os-$$arch/; \
GOOS=$$os GOARCH=$$arch go build -o build/webhook-$$os-$$arch/webhook; \
tar cz -C build -f build/webhook-$$os-$$arch.tar.gz webhook-$$os-$$arch; \
done \
done
test: deps
go test ./...
deps:
go get -d -v -t ./...
clean:
rm -rf build
rm -f webhook
+23 -3
View File
@@ -1,5 +1,4 @@
[![Join the chat at https://gitter.im/adnanh/webhook](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/adnanh/webhook?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Flattr this](https://button.flattr.com/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=adnanh&url=https%3A%2F%2Fwww.github.com%2Fadnanh%2Fwebhook) [![ghit.me](https://ghit.me/badge.svg?repo=adnanh/webhook)](https://ghit.me/repo/adnanh/webhook) [![Join the chat at https://gitter.im/adnanh/webhook](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/adnanh/webhook?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) [![Flattr this](https://button.flattr.com/flattr-badge-large.png)](https://flattr.com/submit/auto?user_id=adnanh&url=https%3A%2F%2Fwww.github.com%2Fadnanh%2Fwebhook) [Donate via PayPal](https://paypal.me/hookdoo) | [Patreon page](https://www.patreon.com/webhook)
# What is webhook? # What is webhook?
[webhook](https://github.com/adnanh/webhook/) is a lightweight configurable tool written in Go, that allows you to easily create HTTP endpoints (hooks) on your server, which you can use to execute configured commands. You can also pass data from the HTTP request (such as headers, payload or query variables) to your commands. [webhook](https://github.com/adnanh/webhook/) also allows you to specify rules which have to be satisfied in order for the hook to be triggered. [webhook](https://github.com/adnanh/webhook/) is a lightweight configurable tool written in Go, that allows you to easily create HTTP endpoints (hooks) on your server, which you can use to execute configured commands. You can also pass data from the HTTP request (such as headers, payload or query variables) to your commands. [webhook](https://github.com/adnanh/webhook/) also allows you to specify rules which have to be satisfied in order for the hook to be triggered.
@@ -11,19 +10,27 @@ If you use Slack, you can set up an "Outgoing webhook integration" to run variou
1. receive the request, 1. receive the request,
2. parse the headers, payload and query variables, 2. parse the headers, payload and query variables,
3. check if the specified rules for the hook are satisfied, 3. check if the specified rules for the hook are satisfied,
3. and finally, pass the specified arguments to the specified command. 3. and finally, pass the specified arguments to the specified command via
command line arguments or via environment variables.
Everything else is the responsibility of the command's author. Everything else is the responsibility of the command's author.
--- ---
# Getting started # Getting started
## Installation
### Building from source
To get started, first make sure you've properly set up your [Golang](http://golang.org/doc/install) environment and then run the To get started, first make sure you've properly set up your [Golang](http://golang.org/doc/install) environment and then run the
```bash ```bash
$ go get github.com/adnanh/webhook $ go get github.com/adnanh/webhook
``` ```
to get the latest version of the [webhook](https://github.com/adnanh/webhook/). to get the latest version of the [webhook](https://github.com/adnanh/webhook/).
### Using package manager
#### Debian "sid"
If you are using unstable version of Debian linux ("sid"), you can install webhook using `apt-get install webhook` which will install community packaged version (thanks [@freeekanayaka](https://github.com/freeekanayaka)) from https://packages.debian.org/sid/webhook
## Configuration
Next step is to define some hooks you want [webhook](https://github.com/adnanh/webhook/) to serve. Begin by creating an empty file named `hooks.json`. This file will contain an array of hooks the [webhook](https://github.com/adnanh/webhook/) will serve. Check [Hook definition page](https://github.com/adnanh/webhook/wiki/Hook-Definition) to see the detailed description of what properties a hook can contain, and how to use them. Next step is to define some hooks you want [webhook](https://github.com/adnanh/webhook/) to serve. Begin by creating an empty file named `hooks.json`. This file will contain an array of hooks the [webhook](https://github.com/adnanh/webhook/) will serve. Check [Hook definition page](https://github.com/adnanh/webhook/wiki/Hook-Definition) to see the detailed description of what properties a hook can contain, and how to use them.
Let's define a simple hook named `redeploy-webhook` that will run a redeploy script located in `/var/scripts/redeploy.sh`. Let's define a simple hook named `redeploy-webhook` that will run a redeploy script located in `/var/scripts/redeploy.sh`.
@@ -58,6 +65,12 @@ However, hook defined like that could pose a security threat to your system, bec
# Using HTTPS # Using HTTPS
[webhook](https://github.com/adnanh/webhook/) by default serves hooks using http. If you want [webhook](https://github.com/adnanh/webhook/) to serve secure content using https, you can use the `-secure` flag while starting [webhook](https://github.com/adnanh/webhook/). Files containing a certificate and matching private key for the server must be provided using the `-cert /path/to/cert.pem` and `-key /path/to/key.pem` flags. If the certificate is signed by a certificate authority, the cert file should be the concatenation of the server's certificate followed by the CA's certificate. [webhook](https://github.com/adnanh/webhook/) by default serves hooks using http. If you want [webhook](https://github.com/adnanh/webhook/) to serve secure content using https, you can use the `-secure` flag while starting [webhook](https://github.com/adnanh/webhook/). Files containing a certificate and matching private key for the server must be provided using the `-cert /path/to/cert.pem` and `-key /path/to/key.pem` flags. If the certificate is signed by a certificate authority, the cert file should be the concatenation of the server's certificate followed by the CA's certificate.
# CORS Headers
If you want to set CORS headers, you can use the `-header name=value` flag while starting [webhook](https://github.com/adnanh/webhook/) to set the appropriate CORS headers that will be returned with each response.
# Interested in running webhook inside of a Docker container?
You can use [almir/webhook](https://hub.docker.com/r/almir/webhook/) docker image, or create your own (please read [this discussion](https://github.com/adnanh/webhook/issues/63)).
# Examples # Examples
Check out [Hook examples page](https://github.com/adnanh/webhook/wiki/Hook-Examples) for more complex examples of hooks. Check out [Hook examples page](https://github.com/adnanh/webhook/wiki/Hook-Examples) for more complex examples of hooks.
@@ -66,6 +79,9 @@ Any form of contribution is welcome and highly appreciated.
Big thanks to [all the current contributors](https://github.com/adnanh/webhook/graphs/contributors) for their contributions! Big thanks to [all the current contributors](https://github.com/adnanh/webhook/graphs/contributors) for their contributions!
# Community Contributions
See the [webhook-contrib][wc] repository for a collections of tools and helpers related to [webhook][w] that have been contributed by the [webhook][w] community.
# License # License
The MIT License (MIT) The MIT License (MIT)
@@ -89,3 +105,7 @@ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE. THE SOFTWARE.
[w]: https://github.com/adnanh/webhook
[wc]: https://github.com/adnanh/webhook-contrib
+227 -99
View File
@@ -5,9 +5,10 @@ import (
"crypto/sha1" "crypto/sha1"
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"errors"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"log" "net/textproto"
"reflect" "reflect"
"regexp" "regexp"
"strconv" "strconv"
@@ -18,6 +19,7 @@ import (
const ( const (
SourceHeader string = "header" SourceHeader string = "header"
SourceQuery string = "url" SourceQuery string = "url"
SourceQueryAlias string = "query"
SourcePayload string = "payload" SourcePayload string = "payload"
SourceString string = "string" SourceString string = "string"
SourceEntirePayload string = "entire-payload" SourceEntirePayload string = "entire-payload"
@@ -25,17 +27,77 @@ const (
SourceEntireHeaders string = "entire-headers" SourceEntireHeaders string = "entire-headers"
) )
const (
// EnvNamespace is the prefix used for passing arguments into the command
// environment.
EnvNamespace string = "HOOK_"
)
// SignatureError describes an invalid payload signature passed to Hook.
type SignatureError struct {
Signature string
}
func (e *SignatureError) Error() string {
if e == nil {
return "<nil>"
}
return fmt.Sprintf("invalid payload signature %s", e.Signature)
}
// ArgumentError describes an invalid argument passed to Hook.
type ArgumentError struct {
Argument Argument
}
func (e *ArgumentError) Error() string {
if e == nil {
return "<nil>"
}
return fmt.Sprintf("couldn't retrieve argument for %+v", e.Argument)
}
// SourceError describes an invalid source passed to Hook.
type SourceError struct {
Argument Argument
}
func (e *SourceError) Error() string {
if e == nil {
return "<nil>"
}
return fmt.Sprintf("invalid source for argument %+v", e.Argument)
}
// ParseError describes an error parsing user input.
type ParseError struct {
Err error
}
func (e *ParseError) Error() string {
if e == nil {
return "<nil>"
}
return e.Err.Error()
}
// CheckPayloadSignature calculates and verifies SHA1 signature of the given payload // CheckPayloadSignature calculates and verifies SHA1 signature of the given payload
func CheckPayloadSignature(payload []byte, secret string, signature string) (string, bool) { func CheckPayloadSignature(payload []byte, secret string, signature string) (string, error) {
if strings.HasPrefix(signature, "sha1=") { if strings.HasPrefix(signature, "sha1=") {
signature = signature[5:] signature = signature[5:]
} }
mac := hmac.New(sha1.New, []byte(secret)) mac := hmac.New(sha1.New, []byte(secret))
mac.Write(payload) _, err := mac.Write(payload)
if err != nil {
return "", err
}
expectedMAC := hex.EncodeToString(mac.Sum(nil)) expectedMAC := hex.EncodeToString(mac.Sum(nil))
return expectedMAC, hmac.Equal([]byte(signature), []byte(expectedMAC)) if !hmac.Equal([]byte(signature), []byte(expectedMAC)) {
return expectedMAC, &SignatureError{signature}
}
return expectedMAC, err
} }
// ReplaceParameter replaces parameter value with the passed value in the passed map // ReplaceParameter replaces parameter value with the passed value in the passed map
@@ -136,19 +198,22 @@ func ExtractParameterAsString(s string, params interface{}) (string, bool) {
// Argument type specifies the parameter key name and the source it should // Argument type specifies the parameter key name and the source it should
// be extracted from // be extracted from
type Argument struct { type Argument struct {
Source string `json:"source"` Source string `json:"source,omitempty"`
Name string `json:"name"` Name string `json:"name,omitempty"`
EnvName string `json:"envname,omitempty"`
} }
// Get Argument method returns the value for the Argument's key name // Get Argument method returns the value for the Argument's key name
// based on the Argument's source // based on the Argument's source
func (ha *Argument) Get(headers, query, payload *map[string]interface{}) (string, bool) { func (ha *Argument) Get(headers, query, payload *map[string]interface{}) (string, bool) {
var source *map[string]interface{} var source *map[string]interface{}
key := ha.Name
switch ha.Source { switch ha.Source {
case SourceHeader: case SourceHeader:
source = headers source = headers
case SourceQuery: key = textproto.CanonicalMIMEHeaderKey(ha.Name)
case SourceQuery, SourceQueryAlias:
source = query source = query
case SourcePayload: case SourcePayload:
source = payload source = payload
@@ -181,27 +246,68 @@ func (ha *Argument) Get(headers, query, payload *map[string]interface{}) (string
} }
if source != nil { if source != nil {
return ExtractParameterAsString(ha.Name, *source) return ExtractParameterAsString(key, *source)
} }
return "", false return "", false
} }
// Header is a structure containing header name and it's value
type Header struct {
Name string `json:"name"`
Value string `json:"value"`
}
// ResponseHeaders is a slice of Header objects
type ResponseHeaders []Header
func (h *ResponseHeaders) String() string {
// a 'hack' to display name=value in flag usage listing
if len(*h) == 0 {
return "name=value"
}
result := make([]string, len(*h))
for idx, responseHeader := range *h {
result[idx] = fmt.Sprintf("%s=%s", responseHeader.Name, responseHeader.Value)
}
return fmt.Sprint(strings.Join(result, ", "))
}
// Set method appends new Header object from header=value notation
func (h *ResponseHeaders) Set(value string) error {
splitResult := strings.SplitN(value, "=", 2)
if len(splitResult) != 2 {
return errors.New("header flag must be in name=value format")
}
*h = append(*h, Header{Name: splitResult[0], Value: splitResult[1]})
return nil
}
// Hook type is a structure containing details for a single hook // Hook type is a structure containing details for a single hook
type Hook struct { type Hook struct {
ID string `json:"id"` ID string `json:"id,omitempty"`
ExecuteCommand string `json:"execute-command"` ExecuteCommand string `json:"execute-command,omitempty"`
CommandWorkingDirectory string `json:"command-working-directory"` CommandWorkingDirectory string `json:"command-working-directory,omitempty"`
ResponseMessage string `json:"response-message"` ResponseMessage string `json:"response-message,omitempty"`
CaptureCommandOutput bool `json:"include-command-output-in-response"` ResponseHeaders ResponseHeaders `json:"response-headers,omitempty"`
PassArgumentsToCommand []Argument `json:"pass-arguments-to-command"` CaptureCommandOutput bool `json:"include-command-output-in-response,omitempty"`
JSONStringParameters []Argument `json:"parse-parameters-as-json"` PassEnvironmentToCommand []Argument `json:"pass-environment-to-command,omitempty"`
TriggerRule *Rules `json:"trigger-rule"` PassArgumentsToCommand []Argument `json:"pass-arguments-to-command,omitempty"`
JSONStringParameters []Argument `json:"parse-parameters-as-json,omitempty"`
TriggerRule *Rules `json:"trigger-rule,omitempty"`
TriggerRuleMismatchHttpResponseCode int `json:"trigger-rule-mismatch-http-response-code,omitempty"`
} }
// ParseJSONParameters decodes specified arguments to JSON objects and replaces the // ParseJSONParameters decodes specified arguments to JSON objects and replaces the
// string with the newly created object // string with the newly created object
func (h *Hook) ParseJSONParameters(headers, query, payload *map[string]interface{}) { func (h *Hook) ParseJSONParameters(headers, query, payload *map[string]interface{}) []error {
var errors = make([]error, 0)
for i := range h.JSONStringParameters { for i := range h.JSONStringParameters {
if arg, ok := h.JSONStringParameters[i].Get(headers, query, payload); ok { if arg, ok := h.JSONStringParameters[i].Get(headers, query, payload); ok {
var newArg map[string]interface{} var newArg map[string]interface{}
@@ -212,35 +318,49 @@ func (h *Hook) ParseJSONParameters(headers, query, payload *map[string]interface
err := decoder.Decode(&newArg) err := decoder.Decode(&newArg)
if err != nil { if err != nil {
log.Printf("error parsing argument as JSON payload %+v\n", err) errors = append(errors, &ParseError{err})
continue
}
var source *map[string]interface{}
switch h.JSONStringParameters[i].Source {
case SourceHeader:
source = headers
case SourcePayload:
source = payload
case SourceQuery, SourceQueryAlias:
source = query
}
if source != nil {
key := h.JSONStringParameters[i].Name
if h.JSONStringParameters[i].Source == SourceHeader {
key = textproto.CanonicalMIMEHeaderKey(h.JSONStringParameters[i].Name)
}
ReplaceParameter(key, source, newArg)
} else { } else {
var source *map[string]interface{} errors = append(errors, &SourceError{h.JSONStringParameters[i]})
switch h.JSONStringParameters[i].Source {
case SourceHeader:
source = headers
case SourcePayload:
source = payload
case SourceQuery:
source = query
}
if source != nil {
ReplaceParameter(h.JSONStringParameters[i].Name, source, newArg)
} else {
log.Printf("invalid source for argument %+v\n", h.JSONStringParameters[i])
}
} }
} else { } else {
log.Printf("couldn't retrieve argument for %+v\n", h.JSONStringParameters[i]) errors = append(errors, &ArgumentError{h.JSONStringParameters[i]})
} }
} }
if len(errors) > 0 {
return errors
}
return nil
} }
// ExtractCommandArguments creates a list of arguments, based on the // ExtractCommandArguments creates a list of arguments, based on the
// PassArgumentsToCommand property that is ready to be used with exec.Command() // PassArgumentsToCommand property that is ready to be used with exec.Command()
func (h *Hook) ExtractCommandArguments(headers, query, payload *map[string]interface{}) []string { func (h *Hook) ExtractCommandArguments(headers, query, payload *map[string]interface{}) ([]string, []error) {
var args = make([]string, 0) var args = make([]string, 0)
var errors = make([]error, 0)
args = append(args, h.ExecuteCommand) args = append(args, h.ExecuteCommand)
@@ -249,11 +369,42 @@ func (h *Hook) ExtractCommandArguments(headers, query, payload *map[string]inter
args = append(args, arg) args = append(args, arg)
} else { } else {
args = append(args, "") args = append(args, "")
log.Printf("couldn't retrieve argument for %+v\n", h.PassArgumentsToCommand[i]) errors = append(errors, &ArgumentError{h.PassArgumentsToCommand[i]})
} }
} }
return args if len(errors) > 0 {
return args, errors
}
return args, nil
}
// ExtractCommandArgumentsForEnv creates a list of arguments in key=value
// format, based on the PassEnvironmentToCommand property that is ready to be used
// with exec.Command().
func (h *Hook) ExtractCommandArgumentsForEnv(headers, query, payload *map[string]interface{}) ([]string, []error) {
var args = make([]string, 0)
var errors = make([]error, 0)
for i := range h.PassEnvironmentToCommand {
if arg, ok := h.PassEnvironmentToCommand[i].Get(headers, query, payload); ok {
if h.PassEnvironmentToCommand[i].EnvName != "" {
// first try to use the EnvName if specified
args = append(args, h.PassEnvironmentToCommand[i].EnvName+"="+arg)
} else {
// then fallback on the name
args = append(args, EnvNamespace+h.PassEnvironmentToCommand[i].Name+"="+arg)
}
} else {
errors = append(errors, &ArgumentError{h.PassEnvironmentToCommand[i]})
}
}
if len(errors) > 0 {
return args, errors
}
return args, nil
} }
// Hooks is an array of Hook objects // Hooks is an array of Hook objects
@@ -288,34 +439,17 @@ func (h *Hooks) Match(id string) *Hook {
return nil return nil
} }
// MatchAll iterates through Hooks and returns all of the hooks that match the
// given ID, if no hook matches the given ID, nil is returned
func (h *Hooks) MatchAll(id string) []*Hook {
matchedHooks := make([]*Hook, 0)
for i := range *h {
if (*h)[i].ID == id {
matchedHooks = append(matchedHooks, &(*h)[i])
}
}
if len(matchedHooks) > 0 {
return matchedHooks
}
return nil
}
// Rules is a structure that contains one of the valid rule types // Rules is a structure that contains one of the valid rule types
type Rules struct { type Rules struct {
And *AndRule `json:"and"` And *AndRule `json:"and,omitempty"`
Or *OrRule `json:"or"` Or *OrRule `json:"or,omitempty"`
Not *NotRule `json:"not"` Not *NotRule `json:"not,omitempty"`
Match *MatchRule `json:"match"` Match *MatchRule `json:"match,omitempty"`
} }
// Evaluate finds the first rule property that is not nil and returns the value // Evaluate finds the first rule property that is not nil and returns the value
// it evaluates to // it evaluates to
func (r Rules) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) bool { func (r Rules) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) (bool, error) {
switch { switch {
case r.And != nil: case r.And != nil:
return r.And.Evaluate(headers, query, payload, body) return r.And.Evaluate(headers, query, payload, body)
@@ -327,58 +461,69 @@ func (r Rules) Evaluate(headers, query, payload *map[string]interface{}, body *[
return r.Match.Evaluate(headers, query, payload, body) return r.Match.Evaluate(headers, query, payload, body)
} }
return false return false, nil
} }
// AndRule will evaluate to true if and only if all of the ChildRules evaluate to true // AndRule will evaluate to true if and only if all of the ChildRules evaluate to true
type AndRule []Rules type AndRule []Rules
// Evaluate AndRule will return true if and only if all of ChildRules evaluate to true // Evaluate AndRule will return true if and only if all of ChildRules evaluate to true
func (r AndRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) bool { func (r AndRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) (bool, error) {
res := true res := true
for _, v := range r { for _, v := range r {
res = res && v.Evaluate(headers, query, payload, body) rv, err := v.Evaluate(headers, query, payload, body)
if err != nil {
return false, err
}
res = res && rv
if res == false { if res == false {
return res return res, nil
} }
} }
return res return res, nil
} }
// OrRule will evaluate to true if any of the ChildRules evaluate to true // OrRule will evaluate to true if any of the ChildRules evaluate to true
type OrRule []Rules type OrRule []Rules
// Evaluate OrRule will return true if any of ChildRules evaluate to true // Evaluate OrRule will return true if any of ChildRules evaluate to true
func (r OrRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) bool { func (r OrRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) (bool, error) {
res := false res := false
for _, v := range r { for _, v := range r {
res = res || v.Evaluate(headers, query, payload, body) rv, err := v.Evaluate(headers, query, payload, body)
if err != nil {
return false, err
}
res = res || rv
if res == true { if res == true {
return res return res, nil
} }
} }
return res return res, nil
} }
// NotRule will evaluate to true if any and only if the ChildRule evaluates to false // NotRule will evaluate to true if any and only if the ChildRule evaluates to false
type NotRule Rules type NotRule Rules
// Evaluate NotRule will return true if and only if ChildRule evaluates to false // Evaluate NotRule will return true if and only if ChildRule evaluates to false
func (r NotRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) bool { func (r NotRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) (bool, error) {
return !Rules(r).Evaluate(headers, query, payload, body) rv, err := Rules(r).Evaluate(headers, query, payload, body)
return !rv, err
} }
// MatchRule will evaluate to true based on the type // MatchRule will evaluate to true based on the type
type MatchRule struct { type MatchRule struct {
Type string `json:"type"` Type string `json:"type,omitempty"`
Regex string `json:"regex"` Regex string `json:"regex,omitempty"`
Secret string `json:"secret"` Secret string `json:"secret,omitempty"`
Value string `json:"value"` Value string `json:"value,omitempty"`
Parameter Argument `json:"parameter"` Parameter Argument `json:"parameter,omitempty"`
} }
// Constants for the MatchRule type // Constants for the MatchRule type
@@ -389,34 +534,17 @@ const (
) )
// Evaluate MatchRule will return based on the type // Evaluate MatchRule will return based on the type
func (r MatchRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) bool { func (r MatchRule) Evaluate(headers, query, payload *map[string]interface{}, body *[]byte) (bool, error) {
if arg, ok := r.Parameter.Get(headers, query, payload); ok { if arg, ok := r.Parameter.Get(headers, query, payload); ok {
switch r.Type { switch r.Type {
case MatchValue: case MatchValue:
return arg == r.Value return arg == r.Value, nil
case MatchRegex: case MatchRegex:
ok, err := regexp.MatchString(r.Regex, arg) return regexp.MatchString(r.Regex, arg)
if err != nil {
log.Printf("error while trying to evaluate regex: %+v", err)
}
return ok
case MatchHashSHA1: case MatchHashSHA1:
expected, ok := CheckPayloadSignature(*body, r.Secret, arg) _, err := CheckPayloadSignature(*body, r.Secret, arg)
if !ok { return err == nil, err
log.Printf("payload signature mismatch, expected %s got %s", expected, arg)
}
return ok
} }
} else {
log.Printf("couldn't retrieve argument for %+v\n", r.Parameter)
} }
return false return false, nil
}
// CommandStatusResponse type encapsulates the executed command exit code, message, stdout and stderr
type CommandStatusResponse struct {
ResponseMessage string `json:"message"`
Output string `json:"output"`
Error string `json:"error"`
} }
+164 -75
View File
@@ -2,6 +2,7 @@ package hook
import ( import (
"reflect" "reflect"
"strings"
"testing" "testing"
) )
@@ -21,9 +22,13 @@ var checkPayloadSignatureTests = []struct {
func TestCheckPayloadSignature(t *testing.T) { func TestCheckPayloadSignature(t *testing.T) {
for _, tt := range checkPayloadSignatureTests { for _, tt := range checkPayloadSignatureTests {
mac, ok := CheckPayloadSignature(tt.payload, tt.secret, tt.signature) mac, err := CheckPayloadSignature(tt.payload, tt.secret, tt.signature)
if ok != tt.ok || mac != tt.mac { if (err == nil) != tt.ok || mac != tt.mac {
t.Errorf("failed to check payload signature {%q, %q, %q}:\nexpected {mac:%#v, ok:%#v},\ngot {mac:%#v, ok:%#v}", tt.payload, tt.secret, tt.signature, tt.mac, tt.ok, mac, ok) t.Errorf("failed to check payload signature {%q, %q, %q}:\nexpected {mac:%#v, ok:%#v},\ngot {mac:%#v, ok:%#v}", tt.payload, tt.secret, tt.signature, tt.mac, tt.ok, mac, (err == nil))
}
if err != nil && strings.Contains(err.Error(), tt.mac) {
t.Errorf("error message should not disclose expected mac: %s", err)
} }
} }
} }
@@ -67,20 +72,20 @@ var argumentGetTests = []struct {
value string value string
ok bool ok bool
}{ }{
{"header", "a", &map[string]interface{}{"a": "z"}, nil, nil, "z", true}, {"header", "a", &map[string]interface{}{"A": "z"}, nil, nil, "z", true},
{"url", "a", nil, &map[string]interface{}{"a": "z"}, nil, "z", true}, {"url", "a", nil, &map[string]interface{}{"a": "z"}, nil, "z", true},
{"payload", "a", nil, nil, &map[string]interface{}{"a": "z"}, "z", true}, {"payload", "a", nil, nil, &map[string]interface{}{"a": "z"}, "z", true},
{"string", "a", nil, nil, &map[string]interface{}{"a": "z"}, "a", true}, {"string", "a", nil, nil, &map[string]interface{}{"a": "z"}, "a", true},
// failures // failures
{"header", "a", nil, &map[string]interface{}{"a": "z"}, &map[string]interface{}{"a": "z"}, "", false}, // nil headers {"header", "a", nil, &map[string]interface{}{"a": "z"}, &map[string]interface{}{"a": "z"}, "", false}, // nil headers
{"url", "a", &map[string]interface{}{"a": "z"}, nil, &map[string]interface{}{"a": "z"}, "", false}, // nil query {"url", "a", &map[string]interface{}{"A": "z"}, nil, &map[string]interface{}{"a": "z"}, "", false}, // nil query
{"payload", "a", &map[string]interface{}{"a": "z"}, &map[string]interface{}{"a": "z"}, nil, "", false}, // nil payload {"payload", "a", &map[string]interface{}{"A": "z"}, &map[string]interface{}{"a": "z"}, nil, "", false}, // nil payload
{"foo", "a", &map[string]interface{}{"a": "z"}, nil, nil, "", false}, // invalid source {"foo", "a", &map[string]interface{}{"A": "z"}, nil, nil, "", false}, // invalid source
} }
func TestArgumentGet(t *testing.T) { func TestArgumentGet(t *testing.T) {
for _, tt := range argumentGetTests { for _, tt := range argumentGetTests {
a := Argument{tt.source, tt.name} a := Argument{tt.source, tt.name, ""}
value, ok := a.Get(tt.headers, tt.query, tt.payload) value, ok := a.Get(tt.headers, tt.query, tt.payload)
if ok != tt.ok || value != tt.value { if ok != tt.ok || value != tt.value {
t.Errorf("failed to get {%q, %q}:\nexpected {value:%#v, ok:%#v},\ngot {value:%#v, ok:%#v}", tt.source, tt.name, tt.value, tt.ok, value, ok) t.Errorf("failed to get {%q, %q}:\nexpected {value:%#v, ok:%#v},\ngot {value:%#v, ok:%#v}", tt.source, tt.name, tt.value, tt.ok, value, ok)
@@ -92,23 +97,24 @@ var hookParseJSONParametersTests = []struct {
params []Argument params []Argument
headers, query, payload *map[string]interface{} headers, query, payload *map[string]interface{}
rheaders, rquery, rpayload *map[string]interface{} rheaders, rquery, rpayload *map[string]interface{}
ok bool
}{ }{
{[]Argument{Argument{"header", "a"}}, &map[string]interface{}{"a": `{"b": "y"}`}, nil, nil, &map[string]interface{}{"a": map[string]interface{}{"b": "y"}}, nil, nil}, {[]Argument{Argument{"header", "a", ""}}, &map[string]interface{}{"A": `{"b": "y"}`}, nil, nil, &map[string]interface{}{"A": map[string]interface{}{"b": "y"}}, nil, nil, true},
{[]Argument{Argument{"url", "a"}}, nil, &map[string]interface{}{"a": `{"b": "y"}`}, nil, nil, &map[string]interface{}{"a": map[string]interface{}{"b": "y"}}, nil}, {[]Argument{Argument{"url", "a", ""}}, nil, &map[string]interface{}{"a": `{"b": "y"}`}, nil, nil, &map[string]interface{}{"a": map[string]interface{}{"b": "y"}}, nil, true},
{[]Argument{Argument{"payload", "a"}}, nil, nil, &map[string]interface{}{"a": `{"b": "y"}`}, nil, nil, &map[string]interface{}{"a": map[string]interface{}{"b": "y"}}}, {[]Argument{Argument{"payload", "a", ""}}, nil, nil, &map[string]interface{}{"a": `{"b": "y"}`}, nil, nil, &map[string]interface{}{"a": map[string]interface{}{"b": "y"}}, true},
{[]Argument{Argument{"header", "z"}}, &map[string]interface{}{"z": `{}`}, nil, nil, &map[string]interface{}{"z": map[string]interface{}{}}, nil, nil}, {[]Argument{Argument{"header", "z", ""}}, &map[string]interface{}{"Z": `{}`}, nil, nil, &map[string]interface{}{"Z": map[string]interface{}{}}, nil, nil, true},
// failures // failures
{[]Argument{Argument{"header", "z"}}, &map[string]interface{}{"z": ``}, nil, nil, &map[string]interface{}{"z": ``}, nil, nil}, // empty string {[]Argument{Argument{"header", "z", ""}}, &map[string]interface{}{"Z": ``}, nil, nil, &map[string]interface{}{"Z": ``}, nil, nil, false}, // empty string
{[]Argument{Argument{"header", "y"}}, &map[string]interface{}{"X": `{}`}, nil, nil, &map[string]interface{}{"X": `{}`}, nil, nil}, // missing parameter {[]Argument{Argument{"header", "y", ""}}, &map[string]interface{}{"X": `{}`}, nil, nil, &map[string]interface{}{"X": `{}`}, nil, nil, false}, // missing parameter
{[]Argument{Argument{"string", "z"}}, &map[string]interface{}{"z": ``}, nil, nil, &map[string]interface{}{"z": ``}, nil, nil}, // invalid argument source {[]Argument{Argument{"string", "z", ""}}, &map[string]interface{}{"Z": ``}, nil, nil, &map[string]interface{}{"Z": ``}, nil, nil, false}, // invalid argument source
} }
func TestHookParseJSONParameters(t *testing.T) { func TestHookParseJSONParameters(t *testing.T) {
for _, tt := range hookParseJSONParametersTests { for _, tt := range hookParseJSONParametersTests {
h := &Hook{JSONStringParameters: tt.params} h := &Hook{JSONStringParameters: tt.params}
h.ParseJSONParameters(tt.headers, tt.query, tt.payload) err := h.ParseJSONParameters(tt.headers, tt.query, tt.payload)
if !reflect.DeepEqual(tt.headers, tt.rheaders) { if (err == nil) != tt.ok || !reflect.DeepEqual(tt.headers, tt.rheaders) {
t.Errorf("failed to parse %v:\nexpected %#v,\ngot %#v", tt.params, *tt.rheaders, *tt.headers) t.Errorf("failed to parse %v:\nexpected %#v, ok: %v\ngot %#v, ok: %v", tt.params, *tt.rheaders, tt.ok, *tt.headers, (err == nil))
} }
} }
} }
@@ -118,18 +124,80 @@ var hookExtractCommandArgumentsTests = []struct {
args []Argument args []Argument
headers, query, payload *map[string]interface{} headers, query, payload *map[string]interface{}
value []string value []string
ok bool
}{ }{
{"test", []Argument{Argument{"header", "a"}}, &map[string]interface{}{"a": "z"}, nil, nil, []string{"test", "z"}}, {"test", []Argument{Argument{"header", "a", ""}}, &map[string]interface{}{"A": "z"}, nil, nil, []string{"test", "z"}, true},
// failures // failures
{"fail", []Argument{Argument{"payload", "a"}}, &map[string]interface{}{"a": "z"}, nil, nil, []string{"fail", ""}}, {"fail", []Argument{Argument{"payload", "a", ""}}, &map[string]interface{}{"A": "z"}, nil, nil, []string{"fail", ""}, false},
} }
func TestHookExtractCommandArguments(t *testing.T) { func TestHookExtractCommandArguments(t *testing.T) {
for _, tt := range hookExtractCommandArgumentsTests { for _, tt := range hookExtractCommandArgumentsTests {
h := &Hook{ExecuteCommand: tt.exec, PassArgumentsToCommand: tt.args} h := &Hook{ExecuteCommand: tt.exec, PassArgumentsToCommand: tt.args}
value := h.ExtractCommandArguments(tt.headers, tt.query, tt.payload) value, err := h.ExtractCommandArguments(tt.headers, tt.query, tt.payload)
if !reflect.DeepEqual(value, tt.value) { if (err == nil) != tt.ok || !reflect.DeepEqual(value, tt.value) {
t.Errorf("failed to extract args {cmd=%q, args=%v}:\nexpected %#v,\ngot %#v", tt.exec, tt.args, tt.value, value) t.Errorf("failed to extract args {cmd=%q, args=%v}:\nexpected %#v, ok: %v\ngot %#v, ok: %v", tt.exec, tt.args, tt.value, tt.ok, value, (err == nil))
}
}
}
// Here we test the extraction of env variables when the user defined a hook
// with the "pass-environment-to-command" directive
// we test both cases where the name of the data is used as the name of the
// env key & the case where the hook definition sets the env var name to a
// fixed value using the envname construct like so::
// [
// {
// "id": "push",
// "execute-command": "bb2mm",
// "command-working-directory": "/tmp",
// "pass-environment-to-command":
// [
// {
// "source": "entire-payload",
// "envname": "PAYLOAD"
// },
// ]
// }
// ]
var hookExtractCommandArgumentsForEnvTests = []struct {
exec string
args []Argument
headers, query, payload *map[string]interface{}
value []string
ok bool
}{
// successes
{
"test",
[]Argument{Argument{"header", "a", ""}},
&map[string]interface{}{"A": "z"}, nil, nil,
[]string{"HOOK_a=z"},
true,
},
{
"test",
[]Argument{Argument{"header", "a", "MYKEY"}},
&map[string]interface{}{"A": "z"}, nil, nil,
[]string{"MYKEY=z"},
true,
},
// failures
{
"fail",
[]Argument{Argument{"payload", "a", ""}},
&map[string]interface{}{"A": "z"}, nil, nil,
[]string{},
false,
},
}
func TestHookExtractCommandArgumentsForEnv(t *testing.T) {
for _, tt := range hookExtractCommandArgumentsForEnvTests {
h := &Hook{ExecuteCommand: tt.exec, PassEnvironmentToCommand: tt.args}
value, err := h.ExtractCommandArgumentsForEnv(tt.headers, tt.query, tt.payload)
if (err == nil) != tt.ok || !reflect.DeepEqual(value, tt.value) {
t.Errorf("failed to extract args for env {cmd=%q, args=%v}:\nexpected %#v, ok: %v\ngot %#v, ok: %v", tt.exec, tt.args, tt.value, tt.ok, value, (err == nil))
} }
} }
} }
@@ -178,24 +246,26 @@ var matchRuleTests = []struct {
headers, query, payload *map[string]interface{} headers, query, payload *map[string]interface{}
body []byte body []byte
ok bool ok bool
err bool
}{ }{
{"value", "", "", "z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true}, {"value", "", "", "z", Argument{"header", "a", ""}, &map[string]interface{}{"A": "z"}, nil, nil, []byte{}, true, false},
{"regex", "^z", "", "z", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true}, {"regex", "^z", "", "z", Argument{"header", "a", ""}, &map[string]interface{}{"A": "z"}, nil, nil, []byte{}, true, false},
{"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": "b17e04cbb22afa8ffbff8796fc1894ed27badd9e"}, nil, nil, []byte(`{"a": "z"}`), true}, {"payload-hash-sha1", "", "secret", "", Argument{"header", "a", ""}, &map[string]interface{}{"A": "b17e04cbb22afa8ffbff8796fc1894ed27badd9e"}, nil, nil, []byte(`{"a": "z"}`), true, false},
// failures // failures
{"value", "", "", "X", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false}, {"value", "", "", "X", Argument{"header", "a", ""}, &map[string]interface{}{"A": "z"}, nil, nil, []byte{}, false, false},
{"value", "", "", "X", Argument{"header", "a"}, &map[string]interface{}{"y": "z"}, nil, nil, []byte{}, false}, {"regex", "^X", "", "", Argument{"header", "a", ""}, &map[string]interface{}{"A": "z"}, nil, nil, []byte{}, false, false},
{"regex", "^X", "", "", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false}, {"value", "", "2", "X", Argument{"header", "a", ""}, &map[string]interface{}{"Y": "z"}, nil, nil, []byte{}, false, false}, // reference invalid header
{"regex", "*", "", "", Argument{"header", "a"}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false}, // errors
{"payload-hash-sha1", "", "secret", "", Argument{"header", "a"}, &map[string]interface{}{"a": ""}, nil, nil, []byte{}, false}, {"regex", "*", "", "", Argument{"header", "a", ""}, &map[string]interface{}{"A": "z"}, nil, nil, []byte{}, false, true}, // invalid regex
{"payload-hash-sha1", "", "secret", "", Argument{"header", "a", ""}, &map[string]interface{}{"A": ""}, nil, nil, []byte{}, false, true}, // invalid hmac
} }
func TestMatchRule(t *testing.T) { func TestMatchRule(t *testing.T) {
for _, tt := range matchRuleTests { for i, tt := range matchRuleTests {
r := MatchRule{tt.typ, tt.regex, tt.secret, tt.value, tt.param} r := MatchRule{tt.typ, tt.regex, tt.secret, tt.value, tt.param}
ok := r.Evaluate(tt.headers, tt.query, tt.payload, &tt.body) ok, err := r.Evaluate(tt.headers, tt.query, tt.payload, &tt.body)
if ok != tt.ok { if ok != tt.ok || (err != nil) != tt.err {
t.Errorf("failed to match %#v:\nexpected %#v,\ngot %#v", r, tt.ok, ok) t.Errorf("%d failed to match %#v:\nexpected ok: %#v, err: %v\ngot ok: %#v, err: %v", i, r, tt.ok, tt.err, ok, (err != nil))
} }
} }
} }
@@ -206,59 +276,67 @@ var andRuleTests = []struct {
headers, query, payload *map[string]interface{} headers, query, payload *map[string]interface{}
body []byte body []byte
ok bool ok bool
err bool
}{ }{
{ {
"(a=z, b=y): a=z && b=y", "(a=z, b=y): a=z && b=y",
AndRule{ AndRule{
{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a"}}}, {Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}},
{Match: &MatchRule{"value", "", "", "y", Argument{"header", "b"}}}, {Match: &MatchRule{"value", "", "", "y", Argument{"header", "b", ""}}},
}, },
&map[string]interface{}{"a": "z", "b": "y"}, nil, nil, []byte{}, &map[string]interface{}{"A": "z", "B": "y"}, nil, nil, []byte{},
true, true, false,
}, },
{ {
"(a=z, b=Y): a=z && b=y", "(a=z, b=Y): a=z && b=y",
AndRule{ AndRule{
{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a"}}}, {Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}},
{Match: &MatchRule{"value", "", "", "y", Argument{"header", "b"}}}, {Match: &MatchRule{"value", "", "", "y", Argument{"header", "b", ""}}},
}, },
&map[string]interface{}{"a": "z", "b": "Y"}, nil, nil, []byte{}, &map[string]interface{}{"A": "z", "B": "Y"}, nil, nil, []byte{},
false, false, false,
}, },
// Complex test to cover Rules.Evaluate // Complex test to cover Rules.Evaluate
{ {
"(a=z, b=y, c=x, d=w=, e=X, f=X): a=z && (b=y && c=x) && (d=w || e=v) && !f=u", "(a=z, b=y, c=x, d=w=, e=X, f=X): a=z && (b=y && c=x) && (d=w || e=v) && !f=u",
AndRule{ AndRule{
{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a"}}}, {Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}},
{ {
And: &AndRule{ And: &AndRule{
{Match: &MatchRule{"value", "", "", "y", Argument{"header", "b"}}}, {Match: &MatchRule{"value", "", "", "y", Argument{"header", "b", ""}}},
{Match: &MatchRule{"value", "", "", "x", Argument{"header", "c"}}}, {Match: &MatchRule{"value", "", "", "x", Argument{"header", "c", ""}}},
}, },
}, },
{ {
Or: &OrRule{ Or: &OrRule{
{Match: &MatchRule{"value", "", "", "w", Argument{"header", "d"}}}, {Match: &MatchRule{"value", "", "", "w", Argument{"header", "d", ""}}},
{Match: &MatchRule{"value", "", "", "v", Argument{"header", "e"}}}, {Match: &MatchRule{"value", "", "", "v", Argument{"header", "e", ""}}},
}, },
}, },
{ {
Not: &NotRule{ Not: &NotRule{
Match: &MatchRule{"value", "", "", "u", Argument{"header", "f"}}, Match: &MatchRule{"value", "", "", "u", Argument{"header", "f", ""}},
}, },
}, },
}, },
&map[string]interface{}{"a": "z", "b": "y", "c": "x", "d": "w", "e": "X", "f": "X"}, nil, nil, []byte{}, &map[string]interface{}{"A": "z", "B": "y", "C": "x", "D": "w", "E": "X", "F": "X"}, nil, nil, []byte{},
true, true, false,
},
{"empty rule", AndRule{{}}, nil, nil, nil, nil, false, false},
// failures
{
"invalid rule",
AndRule{{Match: &MatchRule{"value", "", "", "X", Argument{"header", "a", ""}}}},
&map[string]interface{}{"Y": "z"}, nil, nil, nil,
false, false,
}, },
{"empty rule", AndRule{{}}, nil, nil, nil, nil, false},
} }
func TestAndRule(t *testing.T) { func TestAndRule(t *testing.T) {
for _, tt := range andRuleTests { for _, tt := range andRuleTests {
ok := tt.rule.Evaluate(tt.headers, tt.query, tt.payload, &tt.body) ok, err := tt.rule.Evaluate(tt.headers, tt.query, tt.payload, &tt.body)
if ok != tt.ok { if ok != tt.ok || (err != nil) != tt.err {
t.Errorf("failed to match %#v:\nexpected %#v,\ngot %#v", tt.desc, tt.ok, ok) t.Errorf("failed to match %#v:\nexpected ok: %#v, err: %v\ngot ok: %#v, err: %v", tt.desc, tt.ok, tt.err, ok, err)
} }
} }
} }
@@ -269,41 +347,51 @@ var orRuleTests = []struct {
headers, query, payload *map[string]interface{} headers, query, payload *map[string]interface{}
body []byte body []byte
ok bool ok bool
err bool
}{ }{
{ {
"(a=z, b=X): a=z || b=y", "(a=z, b=X): a=z || b=y",
OrRule{ OrRule{
{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a"}}}, {Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}},
{Match: &MatchRule{"value", "", "", "y", Argument{"header", "b"}}}, {Match: &MatchRule{"value", "", "", "y", Argument{"header", "b", ""}}},
}, },
&map[string]interface{}{"a": "z", "b": "X"}, nil, nil, []byte{}, &map[string]interface{}{"A": "z", "B": "X"}, nil, nil, []byte{},
true, true, false,
}, },
{ {
"(a=X, b=y): a=z || b=y", "(a=X, b=y): a=z || b=y",
OrRule{ OrRule{
{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a"}}}, {Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}},
{Match: &MatchRule{"value", "", "", "y", Argument{"header", "b"}}}, {Match: &MatchRule{"value", "", "", "y", Argument{"header", "b", ""}}},
}, },
&map[string]interface{}{"a": "X", "b": "y"}, nil, nil, []byte{}, &map[string]interface{}{"A": "X", "B": "y"}, nil, nil, []byte{},
true, true, false,
}, },
{ {
"(a=Z, b=Y): a=z || b=y", "(a=Z, b=Y): a=z || b=y",
OrRule{ OrRule{
{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a"}}}, {Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}},
{Match: &MatchRule{"value", "", "", "y", Argument{"header", "b"}}}, {Match: &MatchRule{"value", "", "", "y", Argument{"header", "b", ""}}},
}, },
&map[string]interface{}{"a": "Z", "b": "Y"}, nil, nil, []byte{}, &map[string]interface{}{"A": "Z", "B": "Y"}, nil, nil, []byte{},
false, false, false,
},
// failures
{
"invalid rule",
OrRule{
{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}},
},
&map[string]interface{}{"Y": "Z"}, nil, nil, []byte{},
false, false,
}, },
} }
func TestOrRule(t *testing.T) { func TestOrRule(t *testing.T) {
for _, tt := range orRuleTests { for _, tt := range orRuleTests {
ok := tt.rule.Evaluate(tt.headers, tt.query, tt.payload, &tt.body) ok, err := tt.rule.Evaluate(tt.headers, tt.query, tt.payload, &tt.body)
if ok != tt.ok { if ok != tt.ok || (err != nil) != tt.err {
t.Errorf("%#v:\nexpected %#v,\ngot %#v", tt.desc, tt.ok, ok) t.Errorf("%#v:\nexpected ok: %#v, err: %v\ngot ok: %#v err: %v", tt.desc, tt.ok, tt.err, ok, err)
} }
} }
} }
@@ -314,16 +402,17 @@ var notRuleTests = []struct {
headers, query, payload *map[string]interface{} headers, query, payload *map[string]interface{}
body []byte body []byte
ok bool ok bool
err bool
}{ }{
{"(a=z): !a=X", NotRule{Match: &MatchRule{"value", "", "", "X", Argument{"header", "a"}}}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, true}, {"(a=z): !a=X", NotRule{Match: &MatchRule{"value", "", "", "X", Argument{"header", "a", ""}}}, &map[string]interface{}{"A": "z"}, nil, nil, []byte{}, true, false},
{"(a=z): !a=z", NotRule{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a"}}}, &map[string]interface{}{"a": "z"}, nil, nil, []byte{}, false}, {"(a=z): !a=z", NotRule{Match: &MatchRule{"value", "", "", "z", Argument{"header", "a", ""}}}, &map[string]interface{}{"A": "z"}, nil, nil, []byte{}, false, false},
} }
func TestNotRule(t *testing.T) { func TestNotRule(t *testing.T) {
for _, tt := range notRuleTests { for _, tt := range notRuleTests {
ok := tt.rule.Evaluate(tt.headers, tt.query, tt.payload, &tt.body) ok, err := tt.rule.Evaluate(tt.headers, tt.query, tt.payload, &tt.body)
if ok != tt.ok { if ok != tt.ok || (err != nil) != tt.err {
t.Errorf("failed to match %#v:\nexpected %#v,\ngot %#v", tt.rule, tt.ok, ok) t.Errorf("failed to match %#v:\nexpected ok: %#v, err: %v\ngot ok: %#v, err: %v", tt.rule, tt.ok, tt.err, ok, err)
} }
} }
} }
+7
View File
@@ -4,6 +4,13 @@
"execute-command": "/home/adnan/redeploy-go-webhook.sh", "execute-command": "/home/adnan/redeploy-go-webhook.sh",
"command-working-directory": "/home/adnan/go", "command-working-directory": "/home/adnan/go",
"response-message": "I got the payload!", "response-message": "I got the payload!",
"response-headers":
[
{
"name": "Access-Control-Allow-Origin",
"value": "*"
}
],
"pass-arguments-to-command": "pass-arguments-to-command":
[ [
{ {
+34
View File
@@ -0,0 +1,34 @@
// +build !windows
package main
import (
"log"
"os"
"os/signal"
"syscall"
)
func setupSignals() {
log.Printf("setting up os signal watcher\n")
signals = make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGUSR1)
go watchForSignals()
}
func watchForSignals() {
log.Println("os signal watcher ready")
for {
sig := <-signals
if sig == syscall.SIGUSR1 {
log.Println("caught USR1 signal")
reloadHooks()
} else {
log.Printf("caught unhandled signal %+v\n", sig)
}
}
}
+7
View File
@@ -0,0 +1,7 @@
// +build windows
package main
func setupSignals() {
// NOOP: Windows doesn't have signals equivalent to the Unix world.
}
+26
View File
@@ -0,0 +1,26 @@
// Hook Echo is a simply utility used for testing the Webhook package.
package main
import (
"fmt"
"os"
"strings"
)
func main() {
if len(os.Args) > 1 {
fmt.Printf("arg: %s\n", strings.Join(os.Args[1:], " "))
}
var env []string
for _, v := range os.Environ() {
if strings.HasPrefix(v, "HOOK_") {
env = append(env, v)
}
}
if len(env) > 0 {
fmt.Printf("env: %s\n", strings.Join(env, " "))
}
}
+140
View File
@@ -0,0 +1,140 @@
[
{
"id": "github",
"execute-command": "{{ .Hookecho }}",
"command-working-directory": "/",
"include-command-output-in-response": true,
"trigger-rule-mismatch-http-response-code": 400,
"pass-environment-to-command":
[
{
"source": "payload",
"name": "head_commit.timestamp"
}
],
"pass-arguments-to-command":
[
{
"source": "payload",
"name": "head_commit.id"
},
{
"source": "payload",
"name": "head_commit.author.email"
}
],
"trigger-rule":
{
"and":
[
{
"match":
{
"type": "payload-hash-sha1",
"secret": "mysecret",
"parameter":
{
"source": "header",
"name": "X-Hub-Signature"
}
}
},
{
"match":
{
"type": "value",
"value": "refs/heads/master",
"parameter":
{
"source": "payload",
"name": "ref"
}
}
}
]
}
},
{
"id": "bitbucket",
"execute-command": "{{ .Hookecho }}",
"command-working-directory": "/",
"include-command-output-in-response": false,
"response-message": "success",
"trigger-rule-mismatch-http-response-code": 999,
"parse-parameters-as-json": [
{
"source": "payload",
"name": "payload"
}
],
"trigger-rule": {
"and": [
{
"match": {
"type": "value",
"parameter": {
"source": "payload",
"name": "payload.canon_url"
},
"value": "https://bitbucket.org"
}
},
{
"match": {
"type": "value",
"parameter": {
"source": "payload",
"name": "payload.repository.absolute_url"
},
"value": "/webhook/testing/"
}
},
{
"match": {
"type": "value",
"parameter": {
"source": "payload",
"name": "payload.commits.0.branch"
},
"value": "master"
}
}
]
}
},
{
"id": "gitlab",
"execute-command": "{{ .Hookecho }}",
"command-working-directory": "/",
"response-message": "success",
"include-command-output-in-response": true,
"pass-arguments-to-command":
[
{
"source": "payload",
"name": "commits.0.id"
},
{
"source": "payload",
"name": "user_name"
},
{
"source": "payload",
"name": "user_email"
}
],
"trigger-rule":
{
"match":
{
"type": "value",
"value": "refs/heads/master",
"parameter":
{
"source": "payload",
"name": "ref"
}
}
}
}
]
+128 -82
View File
@@ -1,5 +1,3 @@
//+build !windows
package main package main
import ( import (
@@ -12,9 +10,7 @@ import (
"net/url" "net/url"
"os" "os"
"os/exec" "os/exec"
"os/signal"
"strings" "strings"
"syscall"
"github.com/adnanh/webhook/hook" "github.com/adnanh/webhook/hook"
@@ -25,20 +21,23 @@ import (
) )
const ( const (
version = "2.3.5" version = "2.6.1"
) )
var ( var (
ip = flag.String("ip", "", "ip the webhook should serve hooks on") ip = flag.String("ip", "0.0.0.0", "ip the webhook should serve hooks on")
port = flag.Int("port", 9000, "port the webhook should serve hooks on") port = flag.Int("port", 9000, "port the webhook should serve hooks on")
verbose = flag.Bool("verbose", false, "show verbose output") verbose = flag.Bool("verbose", false, "show verbose output")
noPanic = flag.Bool("nopanic", false, "do not panic if hooks cannot be loaded when webhook is not running in verbose mode") noPanic = flag.Bool("nopanic", false, "do not panic if hooks cannot be loaded when webhook is not running in verbose mode")
hotReload = flag.Bool("hotreload", false, "watch hooks file for changes and reload them automatically") hotReload = flag.Bool("hotreload", false, "watch hooks file for changes and reload them automatically")
hooksFilePath = flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve") hooksFilePath = flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve")
hooksURLPrefix = flag.String("urlprefix", "hooks", "url prefix to use for served hooks (protocol://yourserver:port/PREFIX/:hook-id)") hooksURLPrefix = flag.String("urlprefix", "hooks", "url prefix to use for served hooks (protocol://yourserver:port/PREFIX/:hook-id)")
secure = flag.Bool("secure", false, "use HTTPS instead of HTTP") secure = flag.Bool("secure", false, "use HTTPS instead of HTTP")
cert = flag.String("cert", "cert.pem", "path to the HTTPS certificate pem file") cert = flag.String("cert", "cert.pem", "path to the HTTPS certificate pem file")
key = flag.String("key", "key.pem", "path to the HTTPS certificate private key pem file") key = flag.String("key", "key.pem", "path to the HTTPS certificate private key pem file")
justDisplayVersion = flag.Bool("version", false, "display webhook version and quit")
responseHeaders hook.ResponseHeaders
watcher *fsnotify.Watcher watcher *fsnotify.Watcher
signals chan os.Signal signals chan os.Signal
@@ -46,11 +45,18 @@ var (
hooks hook.Hooks hooks hook.Hooks
) )
func init() { func main() {
hooks = hook.Hooks{} hooks = hook.Hooks{}
flag.Var(&responseHeaders, "header", "response header to return, specified in format name=value, use multiple times to set multiple headers")
flag.Parse() flag.Parse()
if *justDisplayVersion {
fmt.Println("webhook version " + version)
os.Exit(0)
}
log.SetPrefix("[webhook] ") log.SetPrefix("[webhook] ")
log.SetFlags(log.Ldate | log.Ltime) log.SetFlags(log.Ldate | log.Ltime)
@@ -61,12 +67,7 @@ func init() {
log.Println("version " + version + " starting") log.Println("version " + version + " starting")
// set os signal watcher // set os signal watcher
log.Printf("setting up os signal watcher\n") setupSignals()
signals = make(chan os.Signal, 1)
signal.Notify(signals, syscall.SIGUSR1)
go watchForSignals()
// load and parse hooks // load and parse hooks
log.Printf("attempting to load hooks from %s\n", *hooksFilePath) log.Printf("attempting to load hooks from %s\n", *hooksFilePath)
@@ -81,15 +82,19 @@ func init() {
log.Printf("couldn't load hooks from file! %+v\n", err) log.Printf("couldn't load hooks from file! %+v\n", err)
} else { } else {
log.Printf("loaded %d hook(s) from file\n", len(hooks)) seenHooksIds := make(map[string]bool)
log.Printf("found %d hook(s) in file\n", len(hooks))
for _, hook := range hooks { for _, hook := range hooks {
log.Printf("\t> %s\n", hook.ID) if seenHooksIds[hook.ID] == true {
log.Fatalf("error: hook with the id %s has already been loaded!\nplease check your hooks file for duplicate hooks ids!\n", hook.ID)
}
seenHooksIds[hook.ID] = true
log.Printf("\tloaded: %s\n", hook.ID)
} }
} }
}
func main() {
if *hotReload { if *hotReload {
// set up file watcher // set up file watcher
log.Printf("setting up file watcher for %s\n", *hooksFilePath) log.Printf("setting up file watcher for %s\n", *hooksFilePath)
@@ -112,10 +117,10 @@ func main() {
} }
l := negroni.NewLogger() l := negroni.NewLogger()
l.Logger = log.New(os.Stdout, "[webhook] ", log.Ldate|log.Ltime) l.ALogger = log.New(os.Stderr, "[webhook] ", log.Ldate|log.Ltime)
negroniRecovery := &negroni.Recovery{ negroniRecovery := &negroni.Recovery{
Logger: l.Logger, Logger: l.ALogger,
PrintStack: true, PrintStack: true,
StackAll: false, StackAll: false,
StackSize: 1024 * 8, StackSize: 1024 * 8,
@@ -138,22 +143,24 @@ func main() {
n.UseHandler(router) n.UseHandler(router)
if *secure { if *secure {
log.Printf("starting secure (https) webhook on %s:%d", *ip, *port) log.Printf("serving hooks on https://%s:%d%s", *ip, *port, hooksURL)
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *ip, *port), *cert, *key, n)) log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *ip, *port), *cert, *key, n))
} else { } else {
log.Printf("starting insecure (http) webhook on %s:%d", *ip, *port) log.Printf("serving hooks on http://%s:%d%s", *ip, *port, hooksURL)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *ip, *port), n)) log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *ip, *port), n))
} }
} }
func hookHandler(w http.ResponseWriter, r *http.Request) { func hookHandler(w http.ResponseWriter, r *http.Request) {
for _, responseHeader := range responseHeaders {
w.Header().Set(responseHeader.Name, responseHeader.Value)
}
id := mux.Vars(r)["id"] id := mux.Vars(r)["id"]
matchedHooks := hooks.MatchAll(id) if matchedHook := hooks.Match(id); matchedHook != nil {
log.Printf("%s got matched\n", id)
if matchedHooks != nil {
log.Printf("%s got matched (%d time(s))\n", id, len(matchedHooks))
body, err := ioutil.ReadAll(r.Body) body, err := ioutil.ReadAll(r.Body)
if err != nil { if err != nil {
@@ -190,27 +197,65 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
} }
// handle hook // handle hook
for _, h := range matchedHooks { if errors := matchedHook.ParseJSONParameters(&headers, &query, &payload); errors != nil {
h.ParseJSONParameters(&headers, &query, &payload) for _, err := range errors {
if h.TriggerRule == nil || h.TriggerRule != nil && h.TriggerRule.Evaluate(&headers, &query, &payload, &body) { log.Printf("error parsing JSON parameters: %s\n", err)
log.Printf("%s hook triggered successfully\n", h.ID) }
}
if h.CaptureCommandOutput { var ok bool
response := handleHook(h, &headers, &query, &payload, &body)
fmt.Fprintf(w, response)
} else {
go handleHook(h, &headers, &query, &payload, &body)
fmt.Fprintf(w, h.ResponseMessage)
}
if matchedHook.TriggerRule == nil {
ok = true
} else {
ok, err = matchedHook.TriggerRule.Evaluate(&headers, &query, &payload, &body)
if err != nil {
msg := fmt.Sprintf("error evaluating hook: %s", err)
log.Printf(msg)
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error occurred while evaluating hook rules.")
return return
} }
} }
// if none of the hooks got triggered if ok {
log.Printf("%s got matched (%d time(s)), but didn't get triggered because the trigger rules were not satisfied\n", matchedHooks[0].ID, len(matchedHooks)) log.Printf("%s hook triggered successfully\n", matchedHook.ID)
for _, responseHeader := range matchedHook.ResponseHeaders {
w.Header().Set(responseHeader.Name, responseHeader.Value)
}
if matchedHook.CaptureCommandOutput {
response, err := handleHook(matchedHook, &headers, &query, &payload, &body)
if err != nil {
w.Header().Set("Content-Type", "text/plain; charset=utf-8")
w.WriteHeader(http.StatusInternalServerError)
fmt.Fprintf(w, "Error occurred while executing the hook's command. Please check your logs for more details.")
} else {
fmt.Fprintf(w, response)
}
} else {
go handleHook(matchedHook, &headers, &query, &payload, &body)
fmt.Fprintf(w, matchedHook.ResponseMessage)
}
return
}
// Check if a return code is configured for the hook
if matchedHook.TriggerRuleMismatchHttpResponseCode != 0 {
// Check if the configured return code is supported by the http package
// by testing if there is a StatusText for this code.
if len(http.StatusText(matchedHook.TriggerRuleMismatchHttpResponseCode)) > 0 {
w.WriteHeader(matchedHook.TriggerRuleMismatchHttpResponseCode)
} else {
log.Printf("%s got matched, but the configured return code %d is unknown - defaulting to 200\n", matchedHook.ID, matchedHook.TriggerRuleMismatchHttpResponseCode)
}
}
// if none of the hooks got triggered
log.Printf("%s got matched, but didn't get triggered because the trigger rules were not satisfied\n", matchedHook.ID)
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Hook rules were not satisfied.") fmt.Fprintf(w, "Hook rules were not satisfied.")
} else { } else {
w.WriteHeader(http.StatusNotFound) w.WriteHeader(http.StatusNotFound)
@@ -218,35 +263,43 @@ func hookHandler(w http.ResponseWriter, r *http.Request) {
} }
} }
func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) string { func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) (string, error) {
var errors []error
cmd := exec.Command(h.ExecuteCommand) cmd := exec.Command(h.ExecuteCommand)
cmd.Args = h.ExtractCommandArguments(headers, query, payload)
cmd.Dir = h.CommandWorkingDirectory cmd.Dir = h.CommandWorkingDirectory
log.Printf("executing %s (%s) with arguments %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, cmd.Dir) cmd.Args, errors = h.ExtractCommandArguments(headers, query, payload)
if errors != nil {
for _, err := range errors {
log.Printf("error extracting command arguments: %s\n", err)
}
}
out, err := cmd.CombinedOutput() var envs []string
envs, errors = h.ExtractCommandArgumentsForEnv(headers, query, payload)
if errors != nil {
for _, err := range errors {
log.Printf("error extracting command arguments for environment: %s\n", err)
}
}
cmd.Env = append(os.Environ(), envs...)
log.Printf("executing %s (%s) with arguments %q and environment %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, envs, cmd.Dir)
out, err := cmd.Output()
log.Printf("command output: %s\n", out) log.Printf("command output: %s\n", out)
var errorResponse string
if err != nil { if err != nil {
log.Printf("error occurred: %+v\n", err) log.Printf("error occurred: %+v\n", err)
errorResponse = fmt.Sprintf("%+v", err)
} }
log.Printf("finished handling %s\n", h.ID) log.Printf("finished handling %s\n", h.ID)
var response []byte return string(out), err
response, err = json.Marshal(&hook.CommandStatusResponse{ResponseMessage: h.ResponseMessage, Output: string(out), Error: errorResponse})
if err != nil {
log.Printf("error marshalling response: %+v", err)
return h.ResponseMessage
}
return string(response)
} }
func reloadHooks() { func reloadHooks() {
@@ -260,10 +313,18 @@ func reloadHooks() {
if err != nil { if err != nil {
log.Printf("couldn't load hooks from file! %+v\n", err) log.Printf("couldn't load hooks from file! %+v\n", err)
} else { } else {
log.Printf("loaded %d hook(s) from file\n", len(hooks)) seenHooksIds := make(map[string]bool)
for _, hook := range hooks { log.Printf("found %d hook(s) in file\n", len(newHooks))
log.Printf("\t> %s\n", hook.ID)
for _, hook := range newHooks {
if seenHooksIds[hook.ID] == true {
log.Printf("error: hook with the id %s has already been loaded!\nplease check your hooks file for duplicate hooks ids!", hook.ID)
log.Println("reverting hooks back to the previous configuration")
return
}
seenHooksIds[hook.ID] = true
log.Printf("\tloaded: %s\n", hook.ID)
} }
hooks = newHooks hooks = newHooks
@@ -285,21 +346,6 @@ func watchForFileChange() {
} }
} }
func watchForSignals() {
log.Println("os signal watcher ready")
for {
sig := <-signals
if sig == syscall.SIGUSR1 {
log.Println("caught USR1 signal")
reloadHooks()
} else {
log.Printf("caught unhandled signal %+v\n", sig)
}
}
}
// valuesToMap converts map[string][]string to a map[string]string object // valuesToMap converts map[string][]string to a map[string]string object
func valuesToMap(values map[string][]string) map[string]interface{} { func valuesToMap(values map[string][]string) map[string]interface{} {
ret := make(map[string]interface{}) ret := make(map[string]interface{})
+524
View File
@@ -0,0 +1,524 @@
package main
import (
"fmt"
"io/ioutil"
"net"
"net/http"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"text/template"
"time"
"github.com/adnanh/webhook/hook"
)
func TestWebhook(t *testing.T) {
hookecho, cleanupHookecho := buildHookecho(t)
defer cleanupHookecho()
config, cleanupConfig := genConfig(t, hookecho)
defer cleanupConfig()
webhook, cleanupWebhook := buildWebhook(t)
defer cleanupWebhook()
ip, port := serverAddress(t)
args := []string{fmt.Sprintf("-hooks=%s", config), fmt.Sprintf("-ip=%s", ip), fmt.Sprintf("-port=%s", port), "-verbose"}
cmd := exec.Command(webhook, args...)
//cmd.Stderr = os.Stderr // uncomment to see verbose output
cmd.Env = webhookEnv()
cmd.Args[0] = "webhook"
if err := cmd.Start(); err != nil {
t.Fatalf("failed to start webhook: %s", err)
}
defer killAndWait(cmd)
waitForServerReady(t, ip, port)
for _, tt := range hookHandlerTests {
url := fmt.Sprintf("http://%s:%s/hooks/%s", ip, port, tt.id)
req, err := http.NewRequest("POST", url, ioutil.NopCloser(strings.NewReader(tt.body)))
if err != nil {
t.Errorf("New request failed: %s", err)
}
for k, v := range tt.headers {
req.Header.Add(k, v)
}
var res *http.Response
if tt.urlencoded == true {
req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
} else {
req.Header.Add("Content-Type", "application/json")
}
client := &http.Client{}
res, err = client.Do(req)
if err != nil {
t.Errorf("client.Do failed: %s", err)
}
body, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Errorf("POST %q: failed to ready body: %s", tt.desc, err)
}
if res.StatusCode != tt.respStatus || string(body) != tt.respBody {
t.Errorf("failed %q (id: %s):\nexpected status: %#v, response: %s\ngot status: %#v, response: %s", tt.desc, tt.id, tt.respStatus, tt.respBody, res.StatusCode, body)
}
}
}
func buildHookecho(t *testing.T) (bin string, cleanup func()) {
tmp, err := ioutil.TempDir("", "hookecho-test-")
if err != nil {
t.Fatal(err)
}
defer func() {
if cleanup == nil {
os.RemoveAll(tmp)
}
}()
bin = filepath.Join(tmp, "hookecho")
if runtime.GOOS == "windows" {
bin += ".exe"
}
cmd := exec.Command("go", "build", "-o", bin, "test/hookecho.go")
if err := cmd.Run(); err != nil {
t.Fatalf("Building hookecho: %v", err)
}
return bin, func() { os.RemoveAll(tmp) }
}
func genConfig(t *testing.T, bin string) (config string, cleanup func()) {
tmpl := template.Must(template.ParseFiles("test/hooks.json.tmpl"))
tmp, err := ioutil.TempDir("", "webhook-config-")
if err != nil {
t.Fatal(err)
}
defer func() {
if cleanup == nil {
os.RemoveAll(tmp)
}
}()
path := filepath.Join(tmp, "hooks.json")
file, err := os.Create(path)
if err != nil {
t.Fatalf("Creating config template: %v", err)
}
defer file.Close()
data := struct{ Hookecho string }{filepath.ToSlash(bin)}
if err := tmpl.Execute(file, data); err != nil {
t.Fatalf("Executing template: %v", err)
}
return path, func() { os.RemoveAll(tmp) }
}
func buildWebhook(t *testing.T) (bin string, cleanup func()) {
tmp, err := ioutil.TempDir("", "webhook-test-")
if err != nil {
t.Fatal(err)
}
defer func() {
if cleanup == nil {
os.RemoveAll(tmp)
}
}()
bin = filepath.Join(tmp, "webhook")
if runtime.GOOS == "windows" {
bin += ".exe"
}
cmd := exec.Command("go", "build", "-o", bin)
if err := cmd.Run(); err != nil {
t.Fatalf("Building webhook: %v", err)
}
return bin, func() { os.RemoveAll(tmp) }
}
func serverAddress(t *testing.T) (string, string) {
ln, err := net.Listen("tcp", "127.0.0.1:0")
if err != nil {
ln, err = net.Listen("tcp6", "[::1]:0")
}
if err != nil {
t.Fatal(err)
}
defer ln.Close()
host, port, err := net.SplitHostPort(ln.Addr().String())
if err != nil {
t.Fatalf("Failed to split network address: %v", err)
}
return host, port
}
func waitForServerReady(t *testing.T, ip, port string) {
waitForServer(t,
fmt.Sprintf("http://%v:%v/", ip, port),
http.StatusNotFound,
5*time.Second)
}
const pollInterval = 200 * time.Millisecond
func waitForServer(t *testing.T, url string, status int, timeout time.Duration) {
deadline := time.Now().Add(timeout)
for time.Now().Before(deadline) {
time.Sleep(pollInterval)
res, err := http.Get(url)
if err != nil {
continue
}
if res.StatusCode == status {
return
}
}
t.Fatalf("Server failed to respond in %v", timeout)
}
func killAndWait(cmd *exec.Cmd) {
cmd.Process.Kill()
cmd.Wait()
}
// webhookEnv returns the process environment without any existing hook
// namespace variables.
func webhookEnv() (env []string) {
for _, v := range os.Environ() {
if strings.HasPrefix(v, hook.EnvNamespace) {
continue
}
env = append(env, v)
}
return
}
var hookHandlerTests = []struct {
desc string
id string
headers map[string]string
body string
urlencoded bool
respStatus int
respBody string
}{
{
"github",
"github",
map[string]string{"X-Hub-Signature": "f68df0375d7b03e3eb29b4cf9f9ec12e08f42ff8"},
`{
"after":"1481a2de7b2a7d02428ad93446ab166be7793fbb",
"before":"17c497ccc7cca9c2f735aa07e9e3813060ce9a6a",
"commits":[
{
"added":[
],
"author":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"committer":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"distinct":true,
"id":"c441029cf673f84c8b7db52d0a5944ee5c52ff89",
"message":"Test",
"modified":[
"README.md"
],
"removed":[
],
"timestamp":"2013-02-22T13:50:07-08:00",
"url":"https://github.com/octokitty/testing/commit/c441029cf673f84c8b7db52d0a5944ee5c52ff89"
},
{
"added":[
],
"author":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"committer":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"distinct":true,
"id":"36c5f2243ed24de58284a96f2a643bed8c028658",
"message":"This is me testing the windows client.",
"modified":[
"README.md"
],
"removed":[
],
"timestamp":"2013-02-22T14:07:13-08:00",
"url":"https://github.com/octokitty/testing/commit/36c5f2243ed24de58284a96f2a643bed8c028658"
},
{
"added":[
"words/madame-bovary.txt"
],
"author":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"committer":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"distinct":true,
"id":"1481a2de7b2a7d02428ad93446ab166be7793fbb",
"message":"Rename madame-bovary.txt to words/madame-bovary.txt",
"modified":[
],
"removed":[
"madame-bovary.txt"
],
"timestamp":"2013-03-12T08:14:29-07:00",
"url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb"
}
],
"compare":"https://github.com/octokitty/testing/compare/17c497ccc7cc...1481a2de7b2a",
"created":false,
"deleted":false,
"forced":false,
"head_commit":{
"added":[
"words/madame-bovary.txt"
],
"author":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"committer":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"distinct":true,
"id":"1481a2de7b2a7d02428ad93446ab166be7793fbb",
"message":"Rename madame-bovary.txt to words/madame-bovary.txt",
"modified":[
],
"removed":[
"madame-bovary.txt"
],
"timestamp":"2013-03-12T08:14:29-07:00",
"url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb"
},
"pusher":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian"
},
"ref":"refs/heads/master",
"repository":{
"created_at":1332977768,
"description":"",
"fork":false,
"forks":0,
"has_downloads":true,
"has_issues":true,
"has_wiki":true,
"homepage":"",
"id":3860742,
"language":"Ruby",
"master_branch":"master",
"name":"testing",
"open_issues":2,
"owner":{
"email":"lolwut@noway.biz",
"name":"octokitty"
},
"private":false,
"pushed_at":1363295520,
"size":2156,
"stargazers":1,
"url":"https://github.com/octokitty/testing",
"watchers":1
}
}`,
false,
http.StatusOK,
`arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb lolwut@noway.biz
env: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00
`,
},
{
"bitbucket", // bitbucket sends their payload using uriencoded params.
"bitbucket",
nil,
`payload={"canon_url": "https://bitbucket.org","commits": [{"author": "marcus","branch": "master","files": [{"file": "somefile.py","type": "modified"}],"message": "Added some more things to somefile.py\n","node": "620ade18607a","parents": ["702c70160afc"],"raw_author": "Marcus Bertrand <marcus@somedomain.com>","raw_node": "620ade18607ac42d872b568bb92acaa9a28620e9","revision": null,"size": -1,"timestamp": "2012-05-30 05:58:56","utctimestamp": "2014-11-07 15:19:02+00:00"}],"repository": {"absolute_url": "/webhook/testing/","fork": false,"is_private": true,"name": "Project X","owner": "marcus","scm": "git","slug": "project-x","website": "https://atlassian.com/"},"user": "marcus"}`,
true,
http.StatusOK,
`success`,
},
{
"gitlab",
"gitlab",
map[string]string{"X-Gitlab-Event": "Push Hook"},
`{
"object_kind": "push",
"before": "95790bf891e76fee5e1747ab589903a6a1f80f22",
"after": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"ref": "refs/heads/master",
"user_id": 4,
"user_name": "John Smith",
"user_email": "john@example.com",
"project_id": 15,
"repository": {
"name": "Diaspora",
"url": "git@example.com:mike/diasporadiaspora.git",
"description": "",
"homepage": "http://example.com/mike/diaspora",
"git_http_url":"http://example.com/mike/diaspora.git",
"git_ssh_url":"git@example.com:mike/diaspora.git",
"visibility_level":0
},
"commits": [
{
"id": "b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"message": "Update Catalan translation to e38cb41.",
"timestamp": "2011-12-12T14:27:31+02:00",
"url": "http://example.com/mike/diaspora/commit/b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327",
"author": {
"name": "Jordi Mallach",
"email": "jordi@softcatala.org"
}
},
{
"id": "da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"message": "fixed readme",
"timestamp": "2012-01-03T23:36:29+02:00",
"url": "http://example.com/mike/diaspora/commit/da1560886d4f094c3e6c9ef40349f7d38b5d27d7",
"author": {
"name": "GitLab dev user",
"email": "gitlabdev@dv6700.(none)"
}
}
],
"total_commits_count": 4
}`,
false,
http.StatusOK,
`arg: b6568db1bc1dcd7f8b4d5a946b0b91f9dacd7327 John Smith john@example.com
`,
},
{
"missing-cmd-arg", // missing head_commit.author.email
"github",
map[string]string{"X-Hub-Signature": "ab03955b9377f530aa298b1b6d273ae9a47e1e40"},
`{
"head_commit":{
"added":[
"words/madame-bovary.txt"
],
"author":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"committer":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"distinct":true,
"id":"1481a2de7b2a7d02428ad93446ab166be7793fbb",
"message":"Rename madame-bovary.txt to words/madame-bovary.txt",
"modified":[
],
"removed":[
"madame-bovary.txt"
],
"timestamp":"2013-03-12T08:14:29-07:00",
"url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb"
},
"ref":"refs/heads/master"
}`,
false,
http.StatusOK,
`arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb lolwut@noway.biz
env: HOOK_head_commit.timestamp=2013-03-12T08:14:29-07:00
`,
},
{
"missing-env-arg", // missing head_commit.timestamp
"github",
map[string]string{"X-Hub-Signature": "2cf8b878cb6b74a25090a140fa4a474be04b97fa"},
`{
"head_commit":{
"added":[
"words/madame-bovary.txt"
],
"author":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"committer":{
"email":"lolwut@noway.biz",
"name":"Garen Torikian",
"username":"octokitty"
},
"distinct":true,
"id":"1481a2de7b2a7d02428ad93446ab166be7793fbb",
"message":"Rename madame-bovary.txt to words/madame-bovary.txt",
"modified":[
],
"removed":[
"madame-bovary.txt"
],
"url":"https://github.com/octokitty/testing/commit/1481a2de7b2a7d02428ad93446ab166be7793fbb"
},
"ref":"refs/heads/master"
}`,
false,
http.StatusOK,
`arg: 1481a2de7b2a7d02428ad93446ab166be7793fbb lolwut@noway.biz
`,
},
// test with custom return code
{"empty payload", "github", nil, `{}`, false, http.StatusBadRequest, `Hook rules were not satisfied.`},
// test with custom invalid http code, should default to 200 OK
{"empty payload", "bitbucket", nil, `{}`, false, http.StatusOK, `Hook rules were not satisfied.`},
// test with no configured http return code, should default to 200 OK
{"empty payload", "gitlab", nil, `{}`, false, http.StatusOK, `Hook rules were not satisfied.`},
}
-287
View File
@@ -1,287 +0,0 @@
package main
import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"os/exec"
"strings"
"github.com/adnanh/webhook/hook"
"github.com/codegangsta/negroni"
"github.com/gorilla/mux"
fsnotify "gopkg.in/fsnotify.v1"
)
const (
version = "2.3.5"
)
var (
ip = flag.String("ip", "", "ip the webhook should serve hooks on")
port = flag.Int("port", 9000, "port the webhook should serve hooks on")
verbose = flag.Bool("verbose", false, "show verbose output")
noPanic = flag.Bool("nopanic", false, "do not panic if hooks cannot be loaded when webhook is not running in verbose mode")
hotReload = flag.Bool("hotreload", false, "watch hooks file for changes and reload them automatically")
hooksFilePath = flag.String("hooks", "hooks.json", "path to the json file containing defined hooks the webhook should serve")
hooksURLPrefix = flag.String("urlprefix", "hooks", "url prefix to use for served hooks (protocol://yourserver:port/PREFIX/:hook-id)")
secure = flag.Bool("secure", false, "use HTTPS instead of HTTP")
cert = flag.String("cert", "cert.pem", "path to the HTTPS certificate pem file")
key = flag.String("key", "key.pem", "path to the HTTPS certificate private key pem file")
watcher *fsnotify.Watcher
signals chan os.Signal
hooks hook.Hooks
)
func init() {
hooks = hook.Hooks{}
flag.Parse()
log.SetPrefix("[webhook] ")
log.SetFlags(log.Ldate | log.Ltime)
if !*verbose {
log.SetOutput(ioutil.Discard)
}
log.Println("version " + version + " starting")
// load and parse hooks
log.Printf("attempting to load hooks from %s\n", *hooksFilePath)
err := hooks.LoadFromFile(*hooksFilePath)
if err != nil {
if !*verbose && !*noPanic {
log.SetOutput(os.Stdout)
log.Fatalf("couldn't load any hooks from file! %+v\naborting webhook execution since the -verbose flag is set to false.\nIf, for some reason, you want webhook to start without the hooks, either use -verbose flag, or -nopanic", err)
}
log.Printf("couldn't load hooks from file! %+v\n", err)
} else {
log.Printf("loaded %d hook(s) from file\n", len(hooks))
for _, hook := range hooks {
log.Printf("\t> %s\n", hook.ID)
}
}
}
func main() {
if *hotReload {
// set up file watcher
log.Printf("setting up file watcher for %s\n", *hooksFilePath)
var err error
watcher, err = fsnotify.NewWatcher()
if err != nil {
log.Fatal("error creating file watcher instance", err)
}
defer watcher.Close()
go watchForFileChange()
err = watcher.Add(*hooksFilePath)
if err != nil {
log.Fatal("error adding hooks file to the watcher", err)
}
}
l := negroni.NewLogger()
l.Logger = log.New(os.Stdout, "[webhook] ", log.Ldate|log.Ltime)
negroniRecovery := &negroni.Recovery{
Logger: l.Logger,
PrintStack: true,
StackAll: false,
StackSize: 1024 * 8,
}
n := negroni.New(negroniRecovery, l)
router := mux.NewRouter()
var hooksURL string
if *hooksURLPrefix == "" {
hooksURL = "/{id}"
} else {
hooksURL = "/" + *hooksURLPrefix + "/{id}"
}
router.HandleFunc(hooksURL, hookHandler)
n.UseHandler(router)
if *secure {
log.Printf("starting secure (https) webhook on %s:%d", *ip, *port)
log.Fatal(http.ListenAndServeTLS(fmt.Sprintf("%s:%d", *ip, *port), *cert, *key, n))
} else {
log.Printf("starting insecure (http) webhook on %s:%d", *ip, *port)
log.Fatal(http.ListenAndServe(fmt.Sprintf("%s:%d", *ip, *port), n))
}
}
func hookHandler(w http.ResponseWriter, r *http.Request) {
id := mux.Vars(r)["id"]
matchedHooks := hooks.MatchAll(id)
if matchedHooks != nil {
log.Printf("%s got matched (%d time(s))\n", id, len(matchedHooks))
body, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("error reading the request body. %+v\n", err)
}
// parse headers
headers := valuesToMap(r.Header)
// parse query variables
query := valuesToMap(r.URL.Query())
// parse body
var payload map[string]interface{}
contentType := r.Header.Get("Content-Type")
if strings.Contains(contentType, "json") {
decoder := json.NewDecoder(strings.NewReader(string(body)))
decoder.UseNumber()
err := decoder.Decode(&payload)
if err != nil {
log.Printf("error parsing JSON payload %+v\n", err)
}
} else if strings.Contains(contentType, "form") {
fd, err := url.ParseQuery(string(body))
if err != nil {
log.Printf("error parsing form payload %+v\n", err)
} else {
payload = valuesToMap(fd)
}
}
// handle hook
for _, h := range matchedHooks {
h.ParseJSONParameters(&headers, &query, &payload)
if h.TriggerRule == nil || h.TriggerRule != nil && h.TriggerRule.Evaluate(&headers, &query, &payload, &body) {
log.Printf("%s hook triggered successfully\n", h.ID)
if h.CaptureCommandOutput {
response := handleHook(h, &headers, &query, &payload, &body)
fmt.Fprintf(w, response)
} else {
go handleHook(h, &headers, &query, &payload, &body)
fmt.Fprintf(w, h.ResponseMessage)
}
return
}
}
// if none of the hooks got triggered
log.Printf("%s got matched (%d time(s)), but didn't get triggered because the trigger rules were not satisfied\n", matchedHooks[0].ID, len(matchedHooks))
w.WriteHeader(http.StatusBadRequest)
fmt.Fprintf(w, "Hook rules were not satisfied.")
} else {
w.WriteHeader(http.StatusNotFound)
fmt.Fprintf(w, "Hook not found.")
}
}
func handleHook(h *hook.Hook, headers, query, payload *map[string]interface{}, body *[]byte) string {
cmd := exec.Command(h.ExecuteCommand)
cmd.Args = h.ExtractCommandArguments(headers, query, payload)
cmd.Dir = h.CommandWorkingDirectory
log.Printf("executing %s (%s) with arguments %s using %s as cwd\n", h.ExecuteCommand, cmd.Path, cmd.Args, cmd.Dir)
out, err := cmd.CombinedOutput()
log.Printf("command output: %s\n", out)
var errorResponse string
if err != nil {
log.Printf("error occurred: %+v\n", err)
errorResponse = fmt.Sprintf("%+v", err)
}
log.Printf("finished handling %s\n", h.ID)
var response []byte
response, err = json.Marshal(&hook.CommandStatusResponse{ResponseMessage: h.ResponseMessage, Output: string(out), Error: errorResponse})
if err != nil {
log.Printf("error marshalling response: %+v", err)
return h.ResponseMessage
}
return string(response)
}
func reloadHooks() {
newHooks := hook.Hooks{}
// parse and swap
log.Printf("attempting to reload hooks from %s\n", *hooksFilePath)
err := newHooks.LoadFromFile(*hooksFilePath)
if err != nil {
log.Printf("couldn't load hooks from file! %+v\n", err)
} else {
log.Printf("loaded %d hook(s) from file\n", len(hooks))
for _, hook := range hooks {
log.Printf("\t> %s\n", hook.ID)
}
hooks = newHooks
}
}
func watchForFileChange() {
for {
select {
case event := <-(*watcher).Events:
if event.Op&fsnotify.Write == fsnotify.Write {
log.Println("hooks file modified")
reloadHooks()
}
case err := <-(*watcher).Errors:
log.Println("watcher error:", err)
}
}
}
// valuesToMap converts map[string][]string to a map[string]string object
func valuesToMap(values map[string][]string) map[string]interface{} {
ret := make(map[string]interface{})
for key, value := range values {
if len(value) > 0 {
ret[key] = value[0]
}
}
return ret
}