Compare commits

..

4 Commits

5 changed files with 61 additions and 24 deletions

View File

@ -7,14 +7,28 @@
"AppendPayload": true
},
"Secret": "THISISVERYSECRET",
"SignatureHeader": "X-Hub-Signature",
"SignaturePrefix": "sha256=",
"SignatureHeader": "X-Gitea-Signature",
"Tests": [
{
"Program": "echo",
"Arguments": [ "test" ]
}
]
},
"fails_tests": {
"Script": {
"Program": "./example.sh",
"AppendPayload": true
},
"Secret": "who_cares",
"SignatureHeader": "X-Hub-Signature-256",
"SignaturePrefix": "sha256=",
"Tests": [
{
"Program": "false",
"Arguments": []
}
]
}
}
}

View File

@ -1,6 +1,9 @@
package config
import "os/exec"
import (
"fmt"
"os/exec"
)
type Command struct {
Program string
@ -17,3 +20,11 @@ func (c Command) Execute(payload string) ([]byte, error) {
return exec.Command(c.Program, arguments...).Output()
}
func (c Command) String() string {
return fmt.Sprintf(
"<Command cmd=%v AppendPayload=%v>",
append([]string{c.Program}, c.Arguments...),
c.AppendPayload,
)
}

View File

@ -1,10 +1,6 @@
package config
import (
"encoding/json"
"fmt"
)
// The struct that represents the config.json file
type Config struct {
ListenAddress string
Services map[string]struct {
@ -16,14 +12,12 @@ type Config struct {
}
}
// Check that all required fields are filled in
func (c Config) Validate() error {
if c.ListenAddress == "" {
return requiredFieldError{"ListenAddress", ""}
}
jsonbytes, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(jsonbytes))
for serviceName, service := range c.Services {
if service.Script.Program == "" {
return requiredFieldError{"Script.Program", serviceName}

26
main.go
View File

@ -46,21 +46,26 @@ func main() {
}
func webhookHandler(w http.ResponseWriter, r *http.Request) {
// Check what service is specified in URL (/webhooks/{service}) and if it exists
serviceName := string(mux.Vars(r)["service"])
service, ok := c.Services[serviceName]
if !ok {
writeResponse(w, 404, "Service Not Found")
fmt.Printf("Service not found: %v\n", serviceName)
return
}
fmt.Printf("Got webhook for: %v\n", serviceName)
// Read payload or return 500 if that doesn't work out
payload := ""
if p, err := ioutil.ReadAll(r.Body); err != nil {
writeResponse(w, 500, "Internal Server Error: Could not read payload")
fmt.Println("Error: Could not read payload")
return
} else {
payload = string(p)
}
// check what service is specified in URL (/webhooks/{service}) and if it exists
service, ok := c.Services[string(mux.Vars(r)["service"])]
if !ok {
writeResponse(w, 404, "Service Not Found")
return
}
// Verify that signature provided matches signature calculated using secretsss
signature := r.Header.Get(service.SignatureHeader)
calculatedSignature := fmt.Sprintf(
@ -72,17 +77,16 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("calcuatedSignature = %v\n", calculatedSignature)
if signature != calculatedSignature && checkSignature {
writeResponse(w, 400, "Bad Request: Signatures do not match")
fmt.Println("Signatures do not match!")
return
}
// run test and script in parralel to prevent timing out
// Run tests and script as goroutine to prevent timing out
go func(){
// Run tests, immediately stop if one fails
for _, test := range service.Tests {
if _, err := test.Execute(payload); err != nil {
writeResponse(w, 409,
fmt.Sprintf("Conflict: Test failed: %v", err.Error()),
)
fmt.Printf("Test failed(%v) for service %v\n", test, serviceName)
return
}
}

View File

@ -64,14 +64,28 @@ An example config file can be found [here](./config.json) but also below:
"AppendPayload": true
},
"Secret": "THISISVERYSECRET",
"SignatureHeader": "X-Hub-Signature",
"SignaturePrefix": "sha256=",
"SignatureHeader": "X-Gitea-Signature",
"Tests": [
{
"Program": "echo",
"Arguments": [ "test" ]
}
]
},
"fails_tests": {
"Script": {
"Program": "./example.sh",
"AppendPayload": true
},
"Secret": "who_cares",
"SignatureHeader": "X-Hub-Signature-256",
"SignaturePrefix": "sha256=",
"Tests": [
{
"Program": "false",
"Arguments": []
}
]
}
}
}