Compare commits

...

11 Commits

9 changed files with 66 additions and 40 deletions

2
.gitignore vendored Normal file
View File

@@ -0,0 +1,2 @@
gohookr
test_output

View File

@@ -3,9 +3,11 @@ all: install
clean:
rm -rf gohookr
install:
build:
go mod tidy
go build -o gohookr
install: build
cp gohookr /usr/local/bin/
cp gohookr.service /usr/lib/systemd/system/
cp -n config.json /etc/gohookr.json

View File

@@ -4,10 +4,10 @@
"test": {
"Script": {
"Program": "./example.sh",
"AppendPayload": true
"AppendPayload": true,
"AppendHeaders": true
},
"Secret": "THISISVERYSECRET",
"SignatureHeader": "X-Gitea-Signature",
"DisableSignatureVerification": true,
"Tests": [
{
"Program": "echo",

View File

@@ -2,22 +2,33 @@ package config
import (
"fmt"
"net/http"
"os/exec"
"strings"
)
type Command struct {
Program string
Arguments []string
AppendPayload bool
AppendHeaders bool
}
func (c Command) Execute(payload string) ([]byte, error) {
arguments := make([]string, 0)
copy(c.Arguments, arguments)
func (c Command) Execute(payload string, header http.Header) ([]byte, error) {
arguments := make([]string, len(c.Arguments))
copy(arguments, c.Arguments)
if c.AppendPayload {
arguments = append(arguments, payload)
}
if c.AppendHeaders {
var header_builder strings.Builder;
header.Write(&header_builder);
arguments = append(arguments, header_builder.String())
}
return exec.Command(c.Program, arguments...).Output()
}

View File

@@ -8,6 +8,7 @@ type Config struct {
Secret string
SignaturePrefix string
SignatureHeader string
DisableSignatureVerification bool
Tests []Command
}
}
@@ -22,10 +23,10 @@ func (c Config) Validate() error {
if service.Script.Program == "" {
return requiredFieldError{"Script.Program", serviceName}
}
if service.SignatureHeader == "" {
if !service.DisableSignatureVerification && service.SignatureHeader == "" {
return requiredFieldError{"SignatureHeader", serviceName}
}
if service.Secret == "" {
if !service.DisableSignatureVerification && service.Secret == "" {
return requiredFieldError{"Secret", serviceName}
}
}

View File

@@ -1,3 +1,3 @@
#!/usr/bin/bash
date >> test_output
echo "$1" >> test_output
echo "$1" "$2" >> test_output

2
go.mod
View File

@@ -1,4 +1,4 @@
module git.alra.uk/alvierahman90/gohookr
module git.alv.cx/alvierahman90/gohookr
go 1.16

18
main.go
View File

@@ -7,12 +7,11 @@ import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"git.alra.uk/alvierahman90/gohookr/config"
"git.alv.cx/alvierahman90/gohookr/config"
"github.com/gorilla/mux"
)
@@ -32,12 +31,15 @@ func main() {
checkSignature = p != "true"
}
raw_config, err := ioutil.ReadFile(config_filename)
raw_config, err := os.ReadFile(config_filename)
if err != nil {
panic(err.Error())
}
json.Unmarshal(raw_config, &c)
if err := json.Unmarshal(raw_config, &c); err != nil {
panic(err.Error())
}
if err := c.Validate(); err != nil {
panic(err.Error())
}
@@ -58,7 +60,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
// Read payload or return 500 if that doesn't work out
payload := ""
if p, err := ioutil.ReadAll(r.Body); err != nil {
if p, err := io.ReadAll(r.Body); err != nil {
writeResponse(w, 500, "Internal Server Error: Could not read payload")
fmt.Println("Error: Could not read payload")
return
@@ -75,7 +77,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
)
fmt.Printf("signature = %v\n", signature)
fmt.Printf("calcuatedSignature = %v\n", calculatedSignature)
if signature != calculatedSignature && checkSignature {
if checkSignature && !service.DisableSignatureVerification && signature != calculatedSignature {
writeResponse(w, 400, "Bad Request: Signatures do not match")
fmt.Println("Signatures do not match!")
return
@@ -85,12 +87,12 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
go func() {
// Run tests, immediately stop if one fails
for _, test := range service.Tests {
if _, err := test.Execute(payload); err != nil {
if _, err := test.Execute(payload, r.Header); err != nil {
fmt.Printf("Test failed(%v) for service %v\n", test, serviceName)
return
}
}
stdout, err := service.Script.Execute(payload)
stdout, err := service.Script.Execute(payload, r.Header)
fmt.Println(string(stdout))
if err != nil {
fmt.Println(err.Error())

View File

@@ -2,11 +2,6 @@
A _really_ simple webhook receiver, which listens at `/webhooks/<webhook-name>`.
Default config path is `/etc/gohookr.conf` and can be overriden by setting environment variable
`CONFIG`.
Check below for an example configuration.
## Installation
After you [install go](https://golang.org/doc/install):
@@ -15,7 +10,17 @@ After you [install go](https://golang.org/doc/install):
make
```
## Signature Verification
## Configuration
Default config path is `/etc/gohookr.json`.
It can be overriden by setting environment variable `CONFIG`.
Check below for an example configuration, which should tell you most of the things you need to know
to configure gohookr.
Currently gohookr must be restarted after config changes.
### Signature Verification
Signature verificaiton is done using SHA256 HMACs.
You **must** set which HTTP header gohookr will receive a signature from using the `SignatureHeader`
@@ -25,18 +30,21 @@ You should also specify a shared secret in the `Secret` key.
You may also need to specify a `SignaturePrefix`.
For GitHub it would be `sha256=`.
### Disable Signature Verification
#### Disable Signature Verification
You can disable signature verification altogether by setting environment variable
You can disable signature verification by setting `DisableSignatureVerification` for a service to `true`.
You can disable signature verification for all services by setting environment variable
`NO_SIGNATURE_VERIFICATION` to `true`.
## Writing Commands
### Writing Commands
gohookr doesn't care what the command is as long as the `Program` is executable.
You can specify extra arguments with the `Arguments` field.
You can ask it to put the payload as the last argument by setting `AppendPayload` to true.
You can ask it to put the payload as the last (or second to last if `AppendHeaders` is set) argument by setting `AppendPayload` to true.
You can ask it to put the request headers as the last argument by setting `AppendHeaders` to true.
## Writing Tests
### Writing Tests
gohookr can run test before running your script.
Tests must be in the form of bash scripts.
@@ -46,7 +54,7 @@ deploy.
Tests are run in the order they're listed so any actions that need to be done before
tests are run can simply be put in this section before the tests.
## Example Config
### Example Config
Required config keys are `ListenAddress` and `Services`.
@@ -61,10 +69,10 @@ An example config file can be found [here](./config.json) but also below:
"test": {
"Script": {
"Program": "./example.sh",
"AppendPayload": true
"AppendPayload": true,
"AppendHeaders": true
},
"Secret": "THISISVERYSECRET",
"SignatureHeader": "X-Gitea-Signature",
"DisableSignatureVerification": true,
"Tests": [
{
"Program": "echo",