-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
168 lines (147 loc) · 3.85 KB
/
main.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package main
import (
"crypto"
"crypto/rsa"
"crypto/sha512"
"encoding/base64"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"path"
"strconv"
"time"
"github.com/Graylog2/go-gelf/gelf"
"github.com/nimbusec-oss/nimbusec"
)
type WebhookBody struct {
Domain nimbusec.Domain `json:"domain"`
Issues []nimbusec.Issue `json:"issues"`
}
func main() {
if os.Getenv("PORT") == "" {
log.Fatal("env variable PORT is empty or missing")
}
if os.Getenv("GELF_ADDR") == "" {
log.Fatal("env variable GELF_ADDR is empty or missing")
}
port := os.Getenv("PORT")
err := http.ListenAndServe(":"+port, http.HandlerFunc(WebhookR))
if err != nil {
log.Fatalf("failed to listen: %v", err)
}
}
func WebhookR(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost {
log.Printf("invalid method: %s", r.Method)
http.Error(w, "invalid method", http.StatusMethodNotAllowed)
return
}
data, err := ioutil.ReadAll(r.Body)
if err != nil {
log.Printf("failed to read body: %v", err)
http.Error(w, "oops", http.StatusInternalServerError)
return
}
header := r.Header.Get("X-Nimbusec-Signature")
sig, _ := base64.StdEncoding.DecodeString(header)
if !VerifySignature(sig, data) {
log.Printf("invalid signature")
http.Error(w, "invalid signature", http.StatusBadRequest)
return
}
body, err := ParseWebhookBody(data)
if err != nil {
log.Printf("failed to decode json: %v", err)
http.Error(w, "invalid json", http.StatusBadRequest)
return
}
err = SendToGelfEndpoint(body)
if err != nil {
log.Printf("failed to send data to gelf server: %v", err)
http.Error(w, "oops", http.StatusInternalServerError)
return
}
// use http.Error because it is the easiest way to send plain text with
// correct headers set.
log.Printf("sent issues to gelf server :)")
http.Error(w, "ok", http.StatusOK)
}
func VerifySignature(signature []byte, data []byte) bool {
hashed := sha512.Sum512(data)
err := rsa.VerifyPKCS1v15(PublicKey, crypto.SHA512, hashed[:], signature)
return err == nil
}
func ParseWebhookBody(data []byte) (WebhookBody, error) {
body := WebhookBody{}
err := json.Unmarshal(data, &body)
return body, err
}
func SendToGelfEndpoint(body WebhookBody) error {
facility := path.Base(os.Args[0])
host, err := os.Hostname()
if err != nil {
return err
}
addr := os.Getenv("GELF_ADDR")
writer, err := gelf.NewWriter(addr)
if err != nil {
return err
}
for _, issue := range body.Issues {
short := fmt.Sprintf(
"Nimbusec detected a %q issue with severity \"%d\" for %q\n",
issue.Event, issue.Severity, body.Domain.Name)
extra := map[string]interface{}{
"_nimbusec_domain_id": body.Domain.ID,
"_nimbusec_domain_name": body.Domain.Name,
"_nimbusec_domain_url": body.Domain.URL,
"_nimbusec_issue_id": issue.ID,
"_nimbusec_issue_event": issue.Event,
"_nimbusec_issue_category": issue.Category,
"_nimbusec_issue_severity": issue.Severity,
}
extra = flatten(extra, issue.Details, "_nimbusec_issue_details")
msg := &gelf.Message{
Version: "1.1",
Host: host,
Short: string(short),
TimeUnix: float64(time.Now().Unix()),
Level: 6, // info
Facility: facility,
Extra: extra,
}
err = writer.WriteMessage(msg)
if err != nil {
return err
}
}
return nil
}
func flatten(flat map[string]interface{}, nested interface{}, prefix string) map[string]interface{} {
assign := func(key string, v interface{}) {
switch v.(type) {
case map[string]interface{}, []interface{}:
flatten(flat, v, key)
default:
flat[key] = v
}
}
switch nested.(type) {
case map[string]interface{}:
for k, v := range nested.(map[string]interface{}) {
key := prefix + "_" + k
assign(key, v)
}
case []interface{}:
for i, v := range nested.([]interface{}) {
key := prefix + "_" + strconv.Itoa(i)
assign(key, v)
}
default:
flat[prefix] = nested
}
return flat
}