Skip to content

Commit

Permalink
[issue 486] Add logging for incoming webhook events fixes #486 (#487)
Browse files Browse the repository at this point in the history
Co-authored-by: Siba sankar Nayak <sibasankarnayak@Sibas-Air.lan>
Co-authored-by: siba <siba@ip-172-31-33-5.ap-south-1.compute.internal>
  • Loading branch information
3 people authored Nov 29, 2021
1 parent f7e7ce8 commit 5ac2c14
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 8 deletions.
7 changes: 7 additions & 0 deletions plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@
"value": "disable"
}
]
},
{
"key": "EnableWebhookEventLogging",
"display_name": "Enable Webhook Event Logging:",
"type": "bool",
"help_text": "Allow the plugin to log the webhook event. The log level needs to be set to DEBUG.",
"default": false
}
],
"footer": "* To report an issue, make a suggestion or a contribution, [check the repository](https://github.com/mattermost/mattermost-plugin-github)."
Expand Down
1 change: 1 addition & 0 deletions server/plugin/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type Configuration struct {
EnterpriseBaseURL string
EnterpriseUploadURL string
EnableCodePreview string
EnableWebhookEventLogging bool
UsePreregisteredApplication bool
}

Expand Down
8 changes: 8 additions & 0 deletions server/plugin/manifest.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,14 @@ const manifestStr = `
"value": "disable"
}
]
},
{
"key": "EnableWebhookEventLogging",
"display_name": "Enable Webhook Event Logging:",
"type": "bool",
"help_text": "Allow the plugin to log the webhook event. The log level needs to be set to DEBUG.",
"placeholder": "",
"default": false
}
]
}
Expand Down
25 changes: 17 additions & 8 deletions server/plugin/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/hmac"
"crypto/sha1" //nolint:gosec // GitHub webhooks are signed using sha1 https://developer.github.com/webhooks/.
"encoding/hex"
"encoding/json"
"io/ioutil"
"net/http"
"strings"
Expand Down Expand Up @@ -61,13 +62,28 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
config := p.getConfiguration()

signature := r.Header.Get("X-Hub-Signature")

body, err := ioutil.ReadAll(r.Body)
if err != nil {
http.Error(w, "Bad request body", http.StatusBadRequest)
return
}

event, err := github.ParseWebHook(github.WebHookType(r), body)
if err != nil {
p.API.LogDebug("GitHub webhook content type should be set to \"application/json\"", "error", err.Error)
http.Error(w, "wrong mime-type. should be \"application/json\"", http.StatusBadRequest)
return
}

if config.EnableWebhookEventLogging {
bodyByte, appErr := json.Marshal(event)
if appErr != nil {
p.API.LogWarn("Error while Marshal Webhook Request", "err", appErr.Error())
http.Error(w, "Error while Marshal Webhook Request", http.StatusBadRequest)
return
}
p.API.LogDebug("Webhook Event Log", "event", string(bodyByte))
}
valid, err := verifyWebhookSignature([]byte(config.WebhookSecret), signature, body)
if err != nil {
p.API.LogWarn("Failed to verify webhook signature", "error", err.Error())
Expand All @@ -80,13 +96,6 @@ func (p *Plugin) handleWebhook(w http.ResponseWriter, r *http.Request) {
return
}

event, err := github.ParseWebHook(github.WebHookType(r), body)
if err != nil {
p.API.LogDebug("GitHub webhook content type should be set to \"application/json\"", "error", err.Error)
http.Error(w, "wrong mime-type. should be \"application/json\"", http.StatusBadRequest)
return
}

var repo *github.Repository
var handler func()

Expand Down
8 changes: 8 additions & 0 deletions webapp/src/manifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,14 @@ const manifest = JSON.parse(`
"value": "disable"
}
]
},
{
"key": "EnableWebhookEventLogging",
"display_name": "Enable Webhook Event Logging:",
"type": "bool",
"help_text": "Allow the plugin to log the webhook event. The log level needs to be set to DEBUG.",
"placeholder": "",
"default": false
}
]
}
Expand Down

0 comments on commit 5ac2c14

Please sign in to comment.