Compare commits

..

4 Commits

5 changed files with 61 additions and 24 deletions

View File

@ -7,14 +7,28 @@
"AppendPayload": true "AppendPayload": true
}, },
"Secret": "THISISVERYSECRET", "Secret": "THISISVERYSECRET",
"SignatureHeader": "X-Hub-Signature", "SignatureHeader": "X-Gitea-Signature",
"SignaturePrefix": "sha256=",
"Tests": [ "Tests": [
{ {
"Program": "echo", "Program": "echo",
"Arguments": [ "test" ] "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 package config
import "os/exec" import (
"fmt"
"os/exec"
)
type Command struct { type Command struct {
Program string Program string
@ -17,3 +20,11 @@ func (c Command) Execute(payload string) ([]byte, error) {
return exec.Command(c.Program, arguments...).Output() 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 package config
import ( // The struct that represents the config.json file
"encoding/json"
"fmt"
)
type Config struct { type Config struct {
ListenAddress string ListenAddress string
Services map[string]struct { Services map[string]struct {
@ -16,14 +12,12 @@ type Config struct {
} }
} }
// Check that all required fields are filled in
func (c Config) Validate() error { func (c Config) Validate() error {
if c.ListenAddress == "" { if c.ListenAddress == "" {
return requiredFieldError{"ListenAddress", ""} return requiredFieldError{"ListenAddress", ""}
} }
jsonbytes, _ := json.MarshalIndent(c, "", " ")
fmt.Println(string(jsonbytes))
for serviceName, service := range c.Services { for serviceName, service := range c.Services {
if service.Script.Program == "" { if service.Script.Program == "" {
return requiredFieldError{"Script.Program", serviceName} return requiredFieldError{"Script.Program", serviceName}

26
main.go
View File

@ -46,21 +46,26 @@ func main() {
} }
func webhookHandler(w http.ResponseWriter, r *http.Request) { 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 := "" payload := ""
if p, err := ioutil.ReadAll(r.Body); err != nil { if p, err := ioutil.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")
return return
} else { } else {
payload = string(p) 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 // Verify that signature provided matches signature calculated using secretsss
signature := r.Header.Get(service.SignatureHeader) signature := r.Header.Get(service.SignatureHeader)
calculatedSignature := fmt.Sprintf( calculatedSignature := fmt.Sprintf(
@ -72,17 +77,16 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) {
fmt.Printf("calcuatedSignature = %v\n", calculatedSignature) fmt.Printf("calcuatedSignature = %v\n", calculatedSignature)
if signature != calculatedSignature && checkSignature { if signature != calculatedSignature && checkSignature {
writeResponse(w, 400, "Bad Request: Signatures do not match") writeResponse(w, 400, "Bad Request: Signatures do not match")
fmt.Println("Signatures do not match!")
return return
} }
// run test and script in parralel 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); err != nil {
writeResponse(w, 409, fmt.Printf("Test failed(%v) for service %v\n", test, serviceName)
fmt.Sprintf("Conflict: Test failed: %v", err.Error()),
)
return return
} }
} }

View File

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