-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwebhook.go
82 lines (69 loc) · 1.67 KB
/
webhook.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package main
import (
"fmt"
"io/ioutil"
"net/http"
"os"
"github.com/gorilla/mux"
"github.com/sirupsen/logrus"
)
var logging = logrus.New()
var log = logging.WithFields(logrus.Fields{"server": "0.0.0.0:9000"})
/*
* Determine whether the request is authorized
*/
func authorized(w http.ResponseWriter, r *http.Request) bool {
if u, p, ok := r.BasicAuth(); ok {
if u == os.Getenv("WEBHOOK_USERNAME") && p == os.Getenv("WEBHOOK_PASSWORD") {
return true
}
log.WithFields(logrus.Fields{
"username": u,
}).Error("Unauthorized")
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Unauthorized")
return false
}
w.WriteHeader(http.StatusUnauthorized)
fmt.Fprintf(w, "Unauthorized")
log.Error("Parsing basic auth failed")
return false
}
/*
* Process the webhook and log out the payload
*/
func handler(w http.ResponseWriter, r *http.Request, ntype string) {
body, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Fprintf(w, "Incorrect request")
}
log.WithFields(logrus.Fields{
"url": r.URL,
"remote": r.RemoteAddr,
}).Infof("Received %s\n", ntype)
log.Info(string(body))
w.WriteHeader(http.StatusOK)
}
/*
* Process the webhook and log out the payload
*/
func handleGeneral(w http.ResponseWriter, r *http.Request) {
handler(w, r, "general")
}
/*
* Process the webhook and log out the payload
*/
func handleVulnUpdate(w http.ResponseWriter, r *http.Request) {
handler(w, r, "vuln_update")
}
/*
* Home landing page
*/
func handleHome(w http.ResponseWriter, r *http.Request) {
handler(w, r, "")
}
func main() {
router := mux.NewRouter().StrictSlash(true)
router.HandleFunc("/", handleHome)
log.Fatal(http.ListenAndServe(":9000", router))
}