From c05ca8c528d9246a2125cf6354b0a39a8376bb24 Mon Sep 17 00:00:00 2001 From: Andreas Lundblad Date: Wed, 20 Jun 2018 23:34:18 +0200 Subject: [PATCH 1/5] Added HttpResponseCode hook setting --- docs/Hook-Definition.md | 1 + hook/hook.go | 1 + webhook.go | 24 +++++++++++++++++------- 3 files changed, 19 insertions(+), 7 deletions(-) diff --git a/docs/Hook-Definition.md b/docs/Hook-Definition.md index c248a77..42397a2 100644 --- a/docs/Hook-Definition.md +++ b/docs/Hook-Definition.md @@ -8,6 +8,7 @@ Hooks are defined as JSON objects. Please note that in order to be considered va * `command-working-directory` - specifies the working directory that will be used for the script when it's executed * `response-message` - specifies the string that will be returned to the hook initiator * `response-headers` - specifies the list of headers in format `{"name": "X-Example-Header", "value": "it works"}` that will be returned in HTTP response for the hook + * `http-response-code` - specifies the HTTP status code to be returned * `include-command-output-in-response` - boolean whether webhook should wait for the command to finish and return the raw output as a response to the hook initiator. If the command fails to execute or encounters any errors while executing the response will result in 500 Internal Server Error HTTP status code, otherwise the 200 OK status code will be returned. * `include-command-output-in-response-on-error` - boolean whether webhook should include command stdout & stderror as a response in failed executions. It only works if `include-command-output-in-response` is set to `true`. * `parse-parameters-as-json` - specifies the list of arguments that contain JSON strings. These parameters will be decoded by webhook and you can access them like regular objects in rules and `pass-arguments-to-command`. diff --git a/hook/hook.go b/hook/hook.go index 721b731..75d7119 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -427,6 +427,7 @@ type Hook struct { TriggerRule *Rules `json:"trigger-rule,omitempty"` TriggerRuleMismatchHttpResponseCode int `json:"trigger-rule-mismatch-http-response-code,omitempty"` IncomingPayloadContentType string `json:"incoming-payload-content-type,omitempty"` + HttpResponseCode int `json:"http-response-code,omitempty"` } // ParseJSONParameters decodes specified arguments to JSON objects and replaces the diff --git a/webhook.go b/webhook.go index f106697..590ae84 100644 --- a/webhook.go +++ b/webhook.go @@ -297,6 +297,12 @@ func hookHandler(w http.ResponseWriter, r *http.Request) { } } else { go handleHook(matchedHook, rid, &headers, &query, &payload, &body) + + // Check if a return code is configured for the hook + if matchedHook.HttpResponseCode != 0 { + writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.HttpResponseCode) + } + fmt.Fprintf(w, matchedHook.ResponseMessage) } return @@ -304,13 +310,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) { // Check if a return code is configured for the hook if matchedHook.TriggerRuleMismatchHttpResponseCode != 0 { - // Check if the configured return code is supported by the http package - // by testing if there is a StatusText for this code. - if len(http.StatusText(matchedHook.TriggerRuleMismatchHttpResponseCode)) > 0 { - w.WriteHeader(matchedHook.TriggerRuleMismatchHttpResponseCode) - } else { - log.Printf("[%s] %s got matched, but the configured return code %d is unknown - defaulting to 200\n", rid, matchedHook.ID, matchedHook.TriggerRuleMismatchHttpResponseCode) - } + writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.TriggerRuleMismatchHttpResponseCode) } // if none of the hooks got triggered @@ -408,6 +408,16 @@ func handleHook(h *hook.Hook, rid string, headers, query, payload *map[string]in return string(out), err } +func writeHttpResponseCode(w http.ResponseWriter, rid string, hookId string, responseCode int) { + // Check if the given return code is supported by the http package + // by testing if there is a StatusText for this code. + if len(http.StatusText(responseCode)) > 0 { + w.WriteHeader(responseCode) + } else { + log.Printf("[%s] %s got matched, but the configured return code %d is unknown - defaulting to 200\n", rid, hookId, responseCode) + } +} + func reloadHooks(hooksFilePath string) { hooksInFile := hook.Hooks{} From 22073d88473d50679a3da508df2f42475ecd494b Mon Sep 17 00:00:00 2001 From: Andreas Lundblad Date: Sat, 15 Sep 2018 16:00:42 +0200 Subject: [PATCH 2/5] Renamed http-response-code to success-http-response-code --- docs/Hook-Definition.md | 2 +- hook/hook.go | 2 +- webhook.go | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/docs/Hook-Definition.md b/docs/Hook-Definition.md index 42397a2..a3941af 100644 --- a/docs/Hook-Definition.md +++ b/docs/Hook-Definition.md @@ -8,7 +8,7 @@ Hooks are defined as JSON objects. Please note that in order to be considered va * `command-working-directory` - specifies the working directory that will be used for the script when it's executed * `response-message` - specifies the string that will be returned to the hook initiator * `response-headers` - specifies the list of headers in format `{"name": "X-Example-Header", "value": "it works"}` that will be returned in HTTP response for the hook - * `http-response-code` - specifies the HTTP status code to be returned + * `success-http-response-code` - specifies the HTTP status code to be returned upon success * `include-command-output-in-response` - boolean whether webhook should wait for the command to finish and return the raw output as a response to the hook initiator. If the command fails to execute or encounters any errors while executing the response will result in 500 Internal Server Error HTTP status code, otherwise the 200 OK status code will be returned. * `include-command-output-in-response-on-error` - boolean whether webhook should include command stdout & stderror as a response in failed executions. It only works if `include-command-output-in-response` is set to `true`. * `parse-parameters-as-json` - specifies the list of arguments that contain JSON strings. These parameters will be decoded by webhook and you can access them like regular objects in rules and `pass-arguments-to-command`. diff --git a/hook/hook.go b/hook/hook.go index 75d7119..bba4e9c 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -427,7 +427,7 @@ type Hook struct { TriggerRule *Rules `json:"trigger-rule,omitempty"` TriggerRuleMismatchHttpResponseCode int `json:"trigger-rule-mismatch-http-response-code,omitempty"` IncomingPayloadContentType string `json:"incoming-payload-content-type,omitempty"` - HttpResponseCode int `json:"http-response-code,omitempty"` + SuccessHttpResponseCode int `json:"success-http-response-code,omitempty"` } // ParseJSONParameters decodes specified arguments to JSON objects and replaces the diff --git a/webhook.go b/webhook.go index 590ae84..25e2296 100644 --- a/webhook.go +++ b/webhook.go @@ -299,8 +299,8 @@ func hookHandler(w http.ResponseWriter, r *http.Request) { go handleHook(matchedHook, rid, &headers, &query, &payload, &body) // Check if a return code is configured for the hook - if matchedHook.HttpResponseCode != 0 { - writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.HttpResponseCode) + if matchedHook.SuccessHttpResponseCode != 0 { + writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.SuccessHttpResponseCode) } fmt.Fprintf(w, matchedHook.ResponseMessage) From ef3f43f89f1a2ac6777a0a8538e16ecf59df9207 Mon Sep 17 00:00:00 2001 From: Andreas Lundblad Date: Sat, 15 Sep 2018 16:06:18 +0200 Subject: [PATCH 3/5] Added SuccessHttpResponseCode handling for case when capture output is set to true. --- webhook.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/webhook.go b/webhook.go index 25e2296..4d6ae4e 100644 --- a/webhook.go +++ b/webhook.go @@ -293,12 +293,16 @@ func hookHandler(w http.ResponseWriter, r *http.Request) { fmt.Fprintf(w, "Error occurred while executing the hook's command. Please check your logs for more details.") } } else { + // Check if a success return code is configured for the hook + if matchedHook.SuccessHttpResponseCode != 0 { + writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.HttpResponseCode) + } fmt.Fprintf(w, response) } } else { go handleHook(matchedHook, rid, &headers, &query, &payload, &body) - // Check if a return code is configured for the hook + // Check if a success return code is configured for the hook if matchedHook.SuccessHttpResponseCode != 0 { writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.SuccessHttpResponseCode) } From b65bdbbb246a12769d3725dc81a2d01735036a16 Mon Sep 17 00:00:00 2001 From: Andreas Lundblad Date: Mon, 17 Sep 2018 20:35:51 +0200 Subject: [PATCH 4/5] Removed trailing tab --- hook/hook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hook/hook.go b/hook/hook.go index bba4e9c..43bc225 100644 --- a/hook/hook.go +++ b/hook/hook.go @@ -426,7 +426,7 @@ type Hook struct { JSONStringParameters []Argument `json:"parse-parameters-as-json,omitempty"` TriggerRule *Rules `json:"trigger-rule,omitempty"` TriggerRuleMismatchHttpResponseCode int `json:"trigger-rule-mismatch-http-response-code,omitempty"` - IncomingPayloadContentType string `json:"incoming-payload-content-type,omitempty"` + IncomingPayloadContentType string `json:"incoming-payload-content-type,omitempty"` SuccessHttpResponseCode int `json:"success-http-response-code,omitempty"` } From 54a7190113a98b814bf796b5bc8053f6ac06e92c Mon Sep 17 00:00:00 2001 From: Andreas Lundblad Date: Mon, 17 Sep 2018 20:41:51 +0200 Subject: [PATCH 5/5] Forgot a rename in previous refactoring. --- webhook.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/webhook.go b/webhook.go index 4d6ae4e..cb44032 100644 --- a/webhook.go +++ b/webhook.go @@ -295,7 +295,7 @@ func hookHandler(w http.ResponseWriter, r *http.Request) { } else { // Check if a success return code is configured for the hook if matchedHook.SuccessHttpResponseCode != 0 { - writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.HttpResponseCode) + writeHttpResponseCode(w, rid, matchedHook.ID, matchedHook.SuccessHttpResponseCode) } fmt.Fprintf(w, response) }