Compare commits

...

4 Commits

Author SHA1 Message Date
c5fef8e42d
AppendHeaders 2023-10-28 21:48:54 +01:00
e74c3a684e
stop using deprecated ioutil 2023-10-28 21:47:35 +01:00
0f8b4d2e1e
update gitignore 2023-10-28 21:46:58 +01:00
f3cdde5fe6
update makefile 2023-10-28 21:46:15 +01:00
7 changed files with 26 additions and 11 deletions

1
.gitignore vendored
View File

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

View File

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

View File

@ -4,7 +4,8 @@
"test": { "test": {
"Script": { "Script": {
"Program": "./example.sh", "Program": "./example.sh",
"AppendPayload": true "AppendPayload": true,
"AppendHeaders": true
}, },
"DisableSignatureVerification": true, "DisableSignatureVerification": true,
"Tests": [ "Tests": [

View File

@ -2,22 +2,33 @@ package config
import ( import (
"fmt" "fmt"
"net/http"
"os/exec" "os/exec"
"strings"
) )
type Command struct { type Command struct {
Program string Program string
Arguments []string Arguments []string
AppendPayload bool AppendPayload bool
AppendHeaders bool
} }
func (c Command) Execute(payload string) ([]byte, error) { func (c Command) Execute(payload string, header http.Header) ([]byte, error) {
arguments := make([]string, 0) arguments := make([]string, 0)
copy(c.Arguments, arguments) copy(c.Arguments, arguments)
if c.AppendPayload { if c.AppendPayload {
arguments = append(arguments, payload) 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() return exec.Command(c.Program, arguments...).Output()
} }

View File

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

11
main.go
View File

@ -7,7 +7,6 @@ import (
"encoding/json" "encoding/json"
"fmt" "fmt"
"io" "io"
"io/ioutil"
"log" "log"
"net/http" "net/http"
"os" "os"
@ -32,7 +31,7 @@ func main() {
checkSignature = p != "true" checkSignature = p != "true"
} }
raw_config, err := ioutil.ReadFile(config_filename) raw_config, err := os.ReadFile(config_filename)
if err != nil { if err != nil {
panic(err.Error()) panic(err.Error())
} }
@ -61,7 +60,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
// Read payload or return 500 if that doesn't work out // Read payload or return 500 if that doesn't work out
payload := "" 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") writeResponse(w, 500, "Internal Server Error: Could not read payload")
fmt.Println("Error: Could not read payload") fmt.Println("Error: Could not read payload")
return return
@ -85,15 +84,15 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
} }
// Run tests and script as goroutine to prevent timing out // Run tests and script as goroutine to prevent timing out
go func(){ go func() {
// Run tests, immediately stop if one fails // Run tests, immediately stop if one fails
for _, test := range service.Tests { 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) fmt.Printf("Test failed(%v) for service %v\n", test, serviceName)
return return
} }
} }
stdout, err := service.Script.Execute(payload) stdout, err := service.Script.Execute(payload, r.Header)
fmt.Println(string(stdout)) fmt.Println(string(stdout))
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())

View File

@ -41,7 +41,8 @@ You can disable signature verification for all services by setting environment v
gohookr doesn't care what the command is as long as the `Program` is executable. 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 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