-
Notifications
You must be signed in to change notification settings - Fork 93
/
Copy pathtelegram.go
169 lines (134 loc) · 4.03 KB
/
telegram.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
169
package telegram
import (
"bytes"
"context"
"fmt"
"strings"
"text/template"
"time"
"go.uber.org/zap"
corev1 "k8s.io/api/core/v1"
"github.com/kyverno/policy-reporter/pkg/crd/api/policyreport/v1alpha2"
"github.com/kyverno/policy-reporter/pkg/target"
"github.com/kyverno/policy-reporter/pkg/target/http"
)
var replacer = strings.NewReplacer(
"_", "\\_", "*", "\\*", "[", "\\[", "]", "\\]", "(",
"\\(", ")", "\\)", "~", "\\~", "`", "\\`", ">", "\\>",
"#", "\\#", "+", "\\+", "-", "\\-", "=", "\\=", "|",
"\\|", "{", "\\{", "}", "\\}", ".", "\\.", "!", "\\!",
)
func escape(text interface{}) string {
return replacer.Replace(fmt.Sprintf("%v", text))
}
var notificationTempl = `*\[Policy Reporter\] \[{{ .Result.Severity }}\] {{ escape (or .Result.Policy .Result.Rule) }}*
{{- if .Resource }}
*Resource*: {{ .Resource.Kind }} {{ if .Resource.Namespace }}{{ escape .Resource.Namespace }}/{{ end }}{{ escape .Resource.Name }}
{{- end }}
*Status*: {{ escape .Result.Result }}
*Time*: {{ escape (.Time.Format "02 Jan 06 15:04 MST") }}
{{ if .Result.Category }}*Category*: {{ escape .Result.Category }}{{ end }}
{{ if .Result.Policy }}*Rule*: {{ escape .Result.Rule }}{{ end }}
*Source*: {{ escape .Result.Source }}
*Message*:
{{ escape .Result.Message }}
*Properties*:
{{ range $key, $value := .Result.Properties }}• *{{ escape $key }}*: {{ escape $value }}
{{ end }}
`
type Payload struct {
Text string `json:"text,omitempty"`
ParseMode string `json:"parse_mode,omitempty"`
DisableWebPagePreview bool `json:"disable_web_page_preview,omitempty"`
ChatID string `json:"chat_id,omitempty"`
}
type values struct {
Result v1alpha2.PolicyReportResult
Time time.Time
Resource *corev1.ObjectReference
Props map[string]string
Priority string
}
// Options to configure the Discord target
type Options struct {
target.ClientOptions
ChatID string
Host string
Headers map[string]string
CustomFields map[string]string
HTTPClient http.Client
}
type client struct {
target.BaseClient
chatID string
host string
headers map[string]string
customFields map[string]string
client http.Client
}
func (e *client) Send(result v1alpha2.PolicyReportResult) {
if len(e.customFields) > 0 {
props := make(map[string]string, 0)
for property, value := range e.customFields {
props[property] = value
}
for property, value := range result.Properties {
props[property] = value
}
result.Properties = props
}
payload := Payload{
ParseMode: "MarkdownV2",
DisableWebPagePreview: true,
ChatID: e.chatID,
}
var textBuffer bytes.Buffer
ttmpl, err := template.New("telegram").Funcs(template.FuncMap{"escape": escape}).Parse(notificationTempl)
if err != nil {
zap.L().Error(e.Name()+": PUSH FAILED", zap.Error(err))
return
}
var res *corev1.ObjectReference
if result.HasResource() {
res = result.GetResource()
}
err = ttmpl.Execute(&textBuffer, values{
Result: result,
Time: time.Now(),
Resource: res,
})
if err != nil {
zap.L().Error(e.Name()+": PUSH FAILED", zap.Error(err))
return
}
payload.Text = textBuffer.String()
req, err := http.CreateJSONRequest("POST", e.host, payload)
if err != nil {
zap.L().Error(e.Name()+": PUSH FAILED", zap.Error(err))
return
}
for header, value := range e.headers {
req.Header.Set(header, value)
}
resp, err := e.client.Do(req)
http.ProcessHTTPResponse(e.Name(), resp, err)
}
func (e *client) CleanUp(_ context.Context, _ v1alpha2.ReportInterface) {}
func (e *client) Reset(_ context.Context) error {
return nil
}
func (e *client) BatchSend(_ v1alpha2.ReportInterface, _ []v1alpha2.PolicyReportResult) {}
func (e *client) Type() target.ClientType {
return target.SingleSend
}
// NewClient creates a new loki.client to send Results to Elasticsearch
func NewClient(options Options) target.Client {
return &client{
target.NewBaseClient(options.ClientOptions),
options.ChatID,
options.Host,
options.Headers,
options.CustomFields,
options.HTTPClient,
}
}