forked from jfrog/xray_msteam
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathintegrationPage.go
138 lines (117 loc) · 3.75 KB
/
integrationPage.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
package main
import (
"net/http"
"io/ioutil"
"io"
"encoding/json"
"fmt"
"github.com/go-resty/resty/v2"
"time"
"errors"
)
type Violation struct {
Created string `json:"created"`
TopSeverity string `json:"top_severity"`
WatchName string `json:"watch_name"`
PolicyName string `json:"policy_name"`
Issues Issues `json:"issues"`
}
type Issue struct {
Severity string `json:"severity"`
Type string `json:"type"` // Issue type license/security
Summary string `json:"summary"`
Description string `json:"description"`
Cve string `json:"cve"`
}
type Issues []Issue
type TeamMessageActionTarget struct {
Os string `json:"os"`
Uri string `json:"uri"`
}
type TeamMessageAction struct {
Type string `json:"@type"`
Name string `json:"name"`
Targets []TeamMessageActionTarget `json:"targets"`
}
type TeamPayload struct {
Context string `json:"@context"`
Type string `json:"@type"`
ThemeColor string `json:"themeColor"`
Title string `json:"title"`
Text string `json:"text"`
PotentialActions []TeamMessageAction `json:"potentialAction"`
}
func PingPage(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
}
func SendPage(w http.ResponseWriter, r *http.Request) {
err := SendMessage(r)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
} else {
w.WriteHeader(200)
}
}
func SendMessage(r *http.Request) error {
var violation Violation
body, err := ioutil.ReadAll(io.LimitReader(r.Body, 5048576))
if err != nil{
return err
}
err = json.Unmarshal(body, &violation)
if err != nil {
return err
}
if len(violation.PolicyName) == 0 {
return errors.New("Unable to read webhook payload for critical data to send to MS Teams")
}
// VIOLATION PAYLOAD
violationMessage := fmt.Sprintf("🔔 Policy: %s 🕐 Watch: %s ⌚ Created: %s 🔢 Number Of Issues: %d", violation.PolicyName, violation.WatchName, violation.Created, len(violation.Issues))
count := 0
issueMessage := ""
for _, thisIssue := range violation.Issues {
count++
if count <= 5 {
issueMessage = fmt.Sprintf("%s<br/>⚙️ Issue: %s",issueMessage,thisIssue.Summary)
}
}
if count > 5 {
issueMessage = fmt.Sprintf("%s\nAdditional issues available but not displayed under watch: %s",issueMessage, violation.WatchName)
}
client := resty.New()
// Retries are configured per client
client.
// Set retry count to non zero to enable retries
SetRetryCount(3).
// You can override initial retry wait time.
// Default is 100 milliseconds.
SetRetryWaitTime(5 * time.Second).
// MaxWaitTime can be overridden as well.
// Default is 2 seconds.
SetRetryMaxWaitTime(20 * time.Second)
firstCve := ""
if len(violation.Issues) > 0 {
firstCve = violation.Issues[0].Cve
}
actionTarget := fmt.Sprintf("https://cve.mitre.org/cgi-bin/cvename.cgi?name=%s", firstCve)
target := TeamMessageActionTarget{Os:"default",Uri:actionTarget}
targets := []TeamMessageActionTarget{target}
action := TeamMessageAction{Type: "OpenUri", Name: "Research more...", Targets: targets}
actions := []TeamMessageAction{action}
teamPayload := TeamPayload{Context:"https://schema.org/extensions",Type:"MessageCard",ThemeColor:"0ac70d",Title:violationMessage,Text:issueMessage,PotentialActions:actions}
// Marshal to JSON payload
payload, payloadErr := json.Marshal(teamPayload)
if payloadErr != nil {
return payloadErr
}
// Send Payload toe Microsoft Teams Channel Webhook to post message to Teams Channel setup by the user
_, errored := client.R().
SetHeader("Content-Type", "application/json").
SetBody(string(payload)).
Post(MicrosoftTeamWebhook)
if errored != nil {
return errored
}
return nil
}