-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.go
401 lines (374 loc) · 11.1 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
package main
import (
"bytes"
"crypto/tls"
"crypto/x509"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"strings"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu/sensu-go/types"
)
// Config represents the handler plugin config.
type Config struct {
sensu.PluginConfig
AuthHeader string
ApiUrl string
ApiKey string
AccessToken string
TrustedCaFile string
Labels map[string]string
Annotations map[string]string
Subscriptions []string
AddSubscriptions bool
AddLabels bool
AddAnnotations bool
AddAll bool
}
// EntitySubscriptions is a partial Entity definition for use with the
// PATCH /entities API
// type Deregistration struct {
// Handler string `json:"handler"`
// }
type ObjectMeta struct {
Labels map[string]string `json:"labels,omitempty"`
Annotations map[string]string `json:"annotations,omitempty"`
}
// EntityPatch is a shell of an Entity object for use with the
// PATCH /entities API
type EntityPatch struct {
Subscriptions []string `json:"subscriptions,omitempty"`
Metadata ObjectMeta `json:"metadata,omitempty"`
// TBD if we want to support other Entity-patchable fields:
// CreatedBy string `json:"created_by,omitempty"`
// EntityClass string `json:"entity_class,omitempty"`
// User string `json:"user,omitempty"`
// Deregister string `json:"deregister,omitempty"`
// Deregistration Deregistration `json:"deregistration,omitempty"`
// Redact []string `json:"redact"`
// KeepaliveHandler string `json:"keepalive_handler,omitempty"`
}
var (
plugin = Config{
PluginConfig: sensu.PluginConfig{
Name: "sensu-entity-manager",
Short: "Event-based Sensu entity management for service-discovery (add/remove subscriptions) and other automation workflows.",
Keyspace: "sensu.io/plugins/sensu-entity-manager/config",
},
}
options = []*sensu.PluginConfigOption{
{
Path: "api-url",
Env: "SENSU_API_URL",
Argument: "api-url",
Shorthand: "a",
Default: "http://127.0.0.1:8080",
Usage: "Sensu API URL",
Value: &plugin.ApiUrl,
},
{
Path: "api-key",
Env: "SENSU_API_KEY",
Argument: "api-key",
Shorthand: "k",
Default: "",
Secret: true,
Usage: "Sensu API Key",
Value: &plugin.ApiKey,
},
{
Path: "access-token",
Env: "SENSU_ACCESS_TOKEN",
Argument: "access-token",
Shorthand: "t",
Default: "",
Secret: true,
Usage: "Sensu Access Token",
Value: &plugin.AccessToken,
},
{
Path: "trusted-ca-file",
Env: "SENSU_TRUSTED_CA_FILE",
Argument: "trusted-ca-file",
Shorthand: "c",
Default: "",
Usage: "Sensu Trusted Certificate Authority file",
Value: &plugin.TrustedCaFile,
},
{
Path: "",
Env: "",
Argument: "add-subscriptions",
Shorthand: "",
Default: false,
Usage: "Checks event.Check.Output for a newline-separated list of subscriptions to add",
Value: &plugin.AddSubscriptions,
},
{
Path: "",
Env: "",
Argument: "add-labels",
Shorthand: "",
Default: false,
Usage: "Checks event.Check.Output for a newline-separated list of label key=value pairs to add",
Value: &plugin.AddLabels,
},
{
Path: "",
Env: "",
Argument: "add-annotations",
Shorthand: "",
Default: false,
Usage: "Checks event.Check.Output for a newline-separated list of annotation key=value pairs to add",
Value: &plugin.AddAnnotations,
},
{
Path: "",
Env: "",
Argument: "add-all",
Shorthand: "",
Default: false,
Usage: "Checks event.Check.Output for a newline-separated list of entity management commands to execute",
Value: &plugin.AddAll,
},
}
)
func main() {
handler := sensu.NewGoHandler(&plugin.PluginConfig, options, checkArgs, executeHandler)
handler.Execute()
}
func checkArgs(event *types.Event) error {
if len(plugin.ApiKey) == 0 && len(plugin.AccessToken) == 0 {
return fmt.Errorf("--api-key or $SENSU_API_KEY, or --access-token or $SENSU_ACCESS_TOKEN environment variable is required!")
}
if len(os.Getenv("SENSU_ACCESS_TOKEN")) > 0 {
plugin.AccessToken = os.Getenv("SENSU_ACCESS_TOKEN")
plugin.AuthHeader = fmt.Sprintf(
"Bearer %s",
os.Getenv("SENSU_API_KEY"),
)
}
if len(os.Getenv("SENSU_API_KEY")) > 0 {
plugin.ApiKey = os.Getenv("SENSU_API_KEY")
plugin.AuthHeader = fmt.Sprintf(
"Key %s",
os.Getenv("SENSU_API_KEY"),
)
}
if len(os.Getenv("SENSU_API_URL")) > 0 {
plugin.ApiUrl = os.Getenv("SENSU_API_URL")
}
if plugin.AddSubscriptions {
fmt.Printf("Adding subscriptions from \"event.check.output\"\n")
addSubscriptions(strings.Split(event.Check.Output, "\n"))
}
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"]) > 0 {
fmt.Printf("Adding subscriptions from the \"sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions\" event annotation\n")
addSubscriptions(strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/subscriptions"], ","))
}
if plugin.AddLabels {
fmt.Printf("Adding labels from \"event.check.output\"\n")
addLabels(strings.Split(event.Check.Output, "\n"))
}
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/labels"]) > 0 {
fmt.Printf("Adding labels from the \"sensu.io/plugins/sensu-entity-manager/config/patch/labels\" event annotation\n")
addLabels(strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/labels"], ","))
}
if plugin.AddAnnotations {
fmt.Printf("Adding annotations from \"event.check.output\"\n")
addAnnotations(strings.Split(event.Check.Output, "\n"))
}
if len(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/annotations"]) > 0 {
fmt.Printf("Adding annotations from the \"sensu.io/plugins/sensu-entity-manager/config/patch/annotations\" event annotation\n")
addAnnotations(strings.Split(event.Annotations["sensu.io/plugins/sensu-entity-manager/config/patch/annotations"], ","))
}
if plugin.AddAll {
fmt.Printf("Adding entity properties from \"event.check.output\"\n")
parseCommands(strings.Split(event.Check.Output, "\n"))
}
return nil
}
// LoadCACerts loads the system cert pool.
func LoadCACerts(path string) (*x509.CertPool, error) {
rootCAs, err := x509.SystemCertPool()
if err != nil {
log.Printf("ERROR: failed to load system cert pool: %s", err)
rootCAs = x509.NewCertPool()
}
if rootCAs == nil {
rootCAs = x509.NewCertPool()
}
if path != "" {
certs, err := ioutil.ReadFile(path)
if err != nil {
log.Fatalf("ERROR: failed to read CA file (%s): %s", path, err)
return nil, err
}
rootCAs.AppendCertsFromPEM(certs)
}
return rootCAs, nil
}
func initHTTPClient() *http.Client {
certs, err := LoadCACerts(plugin.TrustedCaFile)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
}
tlsConfig := &tls.Config{
RootCAs: certs,
}
tr := &http.Transport{
TLSClientConfig: tlsConfig,
}
client := &http.Client{
Transport: tr,
}
return client
}
// Return the index location of a string in a []string
func indexOf(s []string, k string) int {
for i, v := range s {
if v == k {
return i
}
}
return -1
}
// Merge two map[string]string objects
// NOTE: this is a potentially destructive method (values may be overwritten)
func mergeMapStringStrings(a map[string]string, b map[string]string) map[string]string {
if a == nil {
fmt.Printf("Error: no entity labels; %v", a)
}
for k, v := range b {
a[k] = v
}
return a
}
func mergeStringSlices(a []string, b []string) []string {
for _, v := range b {
if indexOf(a, v) < 0 {
a = append(a, v)
}
}
return a
}
func trimSlice(s []string) []string {
for _, v := range s {
if len(strings.TrimSpace(v)) == 0 {
i := indexOf(s, v)
s = trimSlice(append(s[:i], s[i+1:]...))
}
}
return s
}
// Parse a slice of strings containing key=value pairs
func parseKvStringSlice(s []string) map[string]string {
var m = make(map[string]string)
for _, kvString := range s {
i := strings.Split(kvString, "=")
if len(i) > 1 {
k := strings.TrimSpace(i[0])
v := strings.TrimSpace(i[1])
if len(strings.Split(k, " ")) > 1 {
fmt.Printf("WARNING: invalid key name: \"%s\" (did you mean to use --add-all?)\n", k)
} else {
m[k] = v
}
}
}
return m
}
func addSubscriptions(subs []string) {
plugin.Subscriptions = mergeStringSlices(plugin.Subscriptions, subs)
}
func addLabels(labels []string) {
plugin.Labels = parseKvStringSlice(labels)
}
func addAnnotations(annotations []string) {
plugin.Annotations = parseKvStringSlice(annotations)
}
func patchEntity(event *types.Event) *EntityPatch {
entity := new(EntityPatch)
// Merge subscriptions
entity.Subscriptions = trimSlice(mergeStringSlices(event.Entity.Subscriptions, plugin.Subscriptions))
// Init Metadata
entity.Metadata = ObjectMeta{}
// Merge labels
entity.Metadata.Labels = mergeMapStringStrings(event.Entity.Labels, plugin.Labels)
// Merge annotations
entity.Metadata.Annotations = mergeMapStringStrings(event.Entity.Annotations, plugin.Annotations)
return entity
}
// Parse commands
func parseCommands(s []string) {
for _, str := range s {
instructions := strings.Split(str, " ")
if len(instructions) < 2 {
fmt.Printf("WARNING: invalid command: \"%s\"\n", str)
} else {
command := strings.TrimSpace(instructions[0])
argument := strings.TrimSpace(instructions[1])
switch command {
case "add-subscription":
addSubscriptions([]string{argument})
case "add-label":
addLabels([]string{argument})
case "add-annotation":
addAnnotations([]string{argument})
default:
fmt.Printf("WARNING: nothing to do for command: \"%v\" (argument: \"%s\").\n", command, argument)
}
}
}
}
func executeHandler(event *types.Event) error {
data := patchEntity(event)
postBody, err := json.Marshal(data)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
return err
}
body := bytes.NewReader(postBody)
req, err := http.NewRequest(
"PATCH",
fmt.Sprintf("%s/api/core/v2/namespaces/%s/entities/%s",
plugin.ApiUrl,
event.Entity.Namespace,
event.Entity.Name,
),
body,
)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
}
var httpClient *http.Client = initHTTPClient()
req.Header.Set("Authorization", plugin.AuthHeader)
req.Header.Set("Content-Type", "application/merge-patch+json")
resp, err := httpClient.Do(req)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
return err
} else if resp.StatusCode == 404 {
log.Fatalf("ERROR: %v %s (%s)\n", resp.StatusCode, http.StatusText(resp.StatusCode), req.URL)
return err
} else if resp.StatusCode == 401 {
log.Fatalf("ERROR: %v %s (%s)\n", resp.StatusCode, http.StatusText(resp.StatusCode), req.URL)
return err
} else if resp.StatusCode >= 300 {
log.Fatalf("ERROR: %v %s", resp.StatusCode, http.StatusText(resp.StatusCode))
return err
} else {
defer resp.Body.Close()
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Fatalf("ERROR: %s\n", err)
return err
}
fmt.Printf("%s\n", string(b))
return nil
}
}