From 8f0cf776b12aa10da10171bce5ba139963474868 Mon Sep 17 00:00:00 2001 From: Alvie Rahman Date: Sat, 12 Feb 2022 14:07:11 +0000 Subject: [PATCH] reload config on new webhook request --- Makefile | 8 +++++--- main.go | 43 ++++++++++++++++++++++++++++++++++--------- readme.md | 12 ++++++++++-- 3 files changed, 49 insertions(+), 14 deletions(-) diff --git a/Makefile b/Makefile index bc09b9c..e961eba 100644 --- a/Makefile +++ b/Makefile @@ -3,15 +3,17 @@ all: install clean: rm -rf gohookr -install: - 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 systemctl daemon-reload systemctl enable --now gohookr +build: + go mod tidy + go build -o gohookr + uninstall: systemctl disable --now gohookr rm -rf /usr/local/bin/gohookr /usr/lib/systemd/system/gohookr.service diff --git a/main.go b/main.go index 7e5ab0c..bdca2c0 100644 --- a/main.go +++ b/main.go @@ -18,9 +18,10 @@ import ( var config_filename = "/etc/gohookr.json" var checkSignature = true -var c config.Config func main() { + var c config.Config + r := mux.NewRouter() r.HandleFunc("/webhooks/{service}", webhookHandler) @@ -32,23 +33,47 @@ func main() { checkSignature = p != "true" } - raw_config, err := ioutil.ReadFile(config_filename) + c, err := loadConfig(config_filename) if err != nil { panic(err.Error()) } + fmt.Printf("CONFIG OK: %s\n", config_filename) + fmt.Printf("LISTENING AT: %s\n", c.ListenAddress) - if err := json.Unmarshal(raw_config, &c); err != nil { - panic(err.Error()) - } - - if err := c.Validate(); err != nil { - panic(err.Error()) + for _, v := range os.Args { + if v == "checkConfig" { + return + } } log.Fatal(http.ListenAndServe(c.ListenAddress, r)) } +func loadConfig(config_filename string) (config.Config, error) { + var c config.Config + + raw_config, err := ioutil.ReadFile(config_filename) + if err != nil { + return config.Config{}, err + } + + if err := json.Unmarshal(raw_config, &c); err != nil { + return config.Config{}, err + } + + if err := c.Validate(); err != nil { + return config.Config{}, err + } + + return c, nil + +} + func webhookHandler(w http.ResponseWriter, r *http.Request) { + c, err := loadConfig(config_filename) + if err != nil { + writeResponse(w, 500, "Unable to read config file") + } // Check what service is specified in URL (/webhooks/{service}) and if it exists serviceName := string(mux.Vars(r)["service"]) service, ok := c.Services[serviceName] @@ -85,7 +110,7 @@ func webhookHandler(w http.ResponseWriter, r *http.Request) { } // Run tests and script as goroutine to prevent timing out - go func(){ + go func() { // Run tests, immediately stop if one fails for _, test := range service.Tests { if _, err := test.Execute(payload); err != nil { diff --git a/readme.md b/readme.md index f45063c..ee04a6a 100644 --- a/readme.md +++ b/readme.md @@ -15,10 +15,17 @@ make Default config path is `/etc/gohookr.json`. It can be overriden by setting environment variable `CONFIG`. +The config file will be re-read every request so service configs can be changed without restarting +the service (unless you want to change the listening port). + 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. +You can test your config file by running + +``` +gohookr checkConfig +``` ### Signature Verification @@ -32,7 +39,8 @@ For GitHub it would be `sha256=`. #### Disable Signature Verification -You can disable signature verification by setting `DisableSignatureVerification` for a service to `true`. +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`.