switch from url to secret to detect hook

This commit is contained in:
Martin/Geno 2019-02-14 03:18:55 +01:00
parent cad2441716
commit d9061412e5
No known key found for this signature in database
GPG Key ID: 9D7D3C6BFF600C6A
5 changed files with 35 additions and 23 deletions

View File

@ -5,8 +5,8 @@ import (
"net/http" "net/http"
libHTTP "dev.sum7.eu/genofire/golang-lib/http"
"github.com/bdlm/log" "github.com/bdlm/log"
libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -52,7 +52,7 @@ func init() {
ok := false ok := false
for _, hook := range hooks { for _, hook := range hooks {
if request.Payload.VCSURL != hook.URL { if request.Payload.VCSURL != hook.Secret {
continue continue
} }
logger.Infof("run hook") logger.Infof("run hook")

View File

@ -9,5 +9,5 @@ startup_notify_user = ["geno@fireorbit.de"]
startup_notify_muc = [] startup_notify_muc = []
[[hooks.git]] [[hooks.git]]
secret = "github-FreifunkBremen-yanic-notShared-Secret"
notify_user = ["geno@fireorbit.de"] notify_user = ["geno@fireorbit.de"]
url = "https://github.com/FreifunkBremen/yanic"

View File

@ -5,15 +5,19 @@ import (
"net/http" "net/http"
libHTTP "dev.sum7.eu/genofire/golang-lib/http"
"github.com/bdlm/log" "github.com/bdlm/log"
libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"dev.sum7.eu/genofire/hook2xmpp/runtime" "dev.sum7.eu/genofire/hook2xmpp/runtime"
) )
var eventHeader = []string{"X-GitHub-Event", "X-Gogs-Event", "X-Gitlab-Event"} var eventHeader = map[string]string{
"X-GitHub-Event": "X-Hub-Signature",
"X-Gogs-Event": "X-Gogs-Delivery",
"X-Gitlab-Event": "X-Gitlab-Token",
}
const hookType = "git" const hookType = "git"
@ -24,21 +28,29 @@ func init() {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)
event := "" event := ""
for _, head := range eventHeader { secret := ""
for head, headSecret := range eventHeader {
event = r.Header.Get(head) event = r.Header.Get(head)
if event != "" { if event != "" {
secret = r.Header.Get(headSecret)
break break
} }
} }
if event == "" || event == "status" {
return
}
var body map[string]interface{} var body map[string]interface{}
libHTTP.Read(r, &body) libHTTP.Read(r, &body)
if s, ok := body["secret"]; ok && secret == "" {
secret = s.(string)
}
if event == "" || secret == "" {
logger.Warnf("no secret or event found")
http.Error(w, fmt.Sprintf("no secret or event found"), http.StatusNotFound)
return
}
var request requestBody var request requestBody
if err := mapstructure.Decode(body, &request); err != nil { if err := mapstructure.Decode(body, &request); err != nil {
logger.Errorf("no readable payload: %s", err) logger.Errorf("no readable payload: %s", err)
@ -52,7 +64,7 @@ func init() {
ok := false ok := false
for _, hook := range hooks { for _, hook := range hooks {
if request.Repository.URL != hook.URL { if secret != hook.Secret {
continue continue
} }
logger.Infof("run hook") logger.Infof("run hook")

View File

@ -3,10 +3,9 @@ package grafana
import ( import (
"fmt" "fmt"
"net/http" "net/http"
"net/url"
libHTTP "dev.sum7.eu/genofire/golang-lib/http"
"github.com/bdlm/log" "github.com/bdlm/log"
libHTTP "github.com/genofire/golang-lib/http"
xmpp "github.com/mattn/go-xmpp" xmpp "github.com/mattn/go-xmpp"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
@ -46,6 +45,14 @@ func init() {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
logger := log.WithField("type", hookType) logger := log.WithField("type", hookType)
_, secret, ok := r.BasicAuth()
if !ok {
logger.Errorf("no secret found")
http.Error(w, fmt.Sprintf("no secret found (basic-auth password)"), http.StatusUnauthorized)
return
}
var body interface{} var body interface{}
libHTTP.Read(r, &body) libHTTP.Read(r, &body)
@ -61,16 +68,9 @@ func init() {
"image": request.ImageURL, "image": request.ImageURL,
}) })
ruleURL, err := url.Parse(request.RuleURL) ok = false
if err != nil {
logger.Errorf("could not parse ruleURL: %s", err)
http.Error(w, fmt.Sprintf("no readable payload"), http.StatusInternalServerError)
return
}
ok := false
for _, hook := range hooks { for _, hook := range hooks {
if ruleURL.Hostname() != hook.URL { if secret != hook.Secret {
continue continue
} }

View File

@ -28,7 +28,7 @@ type Config struct {
} }
type Hook struct { type Hook struct {
URL string `toml:"url"` Secret string `toml:"secret"`
NotifyUser []string `toml:"notify_user"` NotifyUser []string `toml:"notify_user"`
NotifyMuc []string `toml:"notify_muc"` NotifyMuc []string `toml:"notify_muc"`
} }