forked from influxdata/kapacitor
-
Notifications
You must be signed in to change notification settings - Fork 1
/
service.go
540 lines (488 loc) · 14.6 KB
/
service.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
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
package config
import (
"encoding/json"
"fmt"
"net/http"
"path"
"regexp"
"strings"
"time"
client "github.com/influxdata/kapacitor/client/v1"
"github.com/influxdata/kapacitor/services/config/override"
"github.com/influxdata/kapacitor/services/httpd"
"github.com/influxdata/kapacitor/services/storage"
"github.com/pkg/errors"
)
const (
configPath = "/config"
configPathAnchored = "/config/"
configBasePath = httpd.BasePath + configPathAnchored
// The amount of time an update is allowed take, when sending and receiving.
updateTimeout = 5 * time.Second
)
type Diagnostic interface {
Error(msg string, err error)
}
type ConfigUpdate struct {
Name string
NewConfig []interface{}
ErrC chan<- error
}
type Service struct {
enabled bool
config interface{}
diag Diagnostic
updates chan<- ConfigUpdate
routes []httpd.Route
// Cached map of section name to element key name
elementKeys map[string]string
overrides OverrideDAO
StorageService interface {
Store(namespace string) storage.Interface
Register(name string, store storage.StoreActioner)
}
HTTPDService interface {
AddRoutes([]httpd.Route) error
DelRoutes([]httpd.Route)
}
}
func NewService(c Config, config interface{}, d Diagnostic, updates chan<- ConfigUpdate) *Service {
return &Service{
enabled: c.Enabled,
config: config,
diag: d,
updates: updates,
}
}
const (
// Public name of overrides store
overridesAPIName = "overrides"
// The storage namespace for all configuration override data.
configNamespace = "config_overrides"
)
func (s *Service) Open() error {
store := s.StorageService.Store(configNamespace)
overrides, err := newOverrideKV(store)
if err != nil {
return err
}
s.overrides = overrides
s.StorageService.Register(overridesAPIName, s.overrides)
// Cache element keys
if elementKeys, err := override.ElementKeys(s.config); err != nil {
return errors.Wrap(err, "failed to determine the element keys")
} else {
s.elementKeys = elementKeys
}
// Define API routes
s.routes = []httpd.Route{
{
Method: "GET",
Pattern: configPath,
HandlerFunc: s.handleGetConfig,
},
{
Method: "GET",
Pattern: configPathAnchored,
HandlerFunc: s.handleGetConfig,
},
{
Method: "POST",
Pattern: configPathAnchored,
HandlerFunc: s.handleUpdateSection,
},
}
err = s.HTTPDService.AddRoutes(s.routes)
return errors.Wrap(err, "failed to add API routes")
}
func (s *Service) Close() error {
close(s.updates)
s.HTTPDService.DelRoutes(s.routes)
return nil
}
type updateAction struct {
section string
element string
hasElement bool
Set map[string]interface{} `json:"set"`
Delete []string `json:"delete"`
Add map[string]interface{} `json:"add"`
Remove []string `json:"remove"`
}
func (ua updateAction) Validate() error {
if ua.section == "" {
return errors.New("must provide section name")
}
if !validSectionOrElement.MatchString(ua.section) {
return fmt.Errorf("invalid section name %q", ua.section)
}
if ua.element != "" && !validSectionOrElement.MatchString(ua.element) {
return fmt.Errorf("invalid element name %q", ua.element)
}
sEmpty := len(ua.Set) == 0
dEmpty := len(ua.Delete) == 0
aEmpty := len(ua.Add) == 0
rEmpty := len(ua.Remove) == 0
if (!sEmpty || !dEmpty) && !(aEmpty && rEmpty) {
return errors.New("cannot provide both set/delete and add/remove actions in the same update")
}
if !aEmpty && ua.element != "" {
return errors.New("must not provide an element name when adding an a new override")
}
if !rEmpty && ua.element != "" {
return errors.New("must not provide element when removing an override")
}
if rEmpty && aEmpty && !ua.hasElement {
return errors.New("element not specified, are you missing a trailing '/'?")
}
return nil
}
var validSectionOrElement = regexp.MustCompile(`^[-\w+]+$`)
func sectionAndElementToID(section, element string) string {
id := path.Join(section, element)
if element == "" {
id += "/"
}
return id
}
func sectionAndElementFromPath(p, basePath string) (section, element string, hasSection, hasElement bool) {
if !strings.HasPrefix(p, basePath) {
return "", "", false, false
}
s, e := sectionAndElementFromID(strings.TrimPrefix(p, basePath))
return s, e, true, (e != "" || s != "" && strings.HasSuffix(p, "/"))
}
func sectionAndElementFromID(id string) (section, element string) {
parts := strings.Split(id, "/")
if l := len(parts); l == 1 {
section = parts[0]
} else if l == 2 {
section = parts[0]
element = parts[1]
}
return
}
func (s *Service) sectionLink(section string) client.Link {
return client.Link{Relation: client.Self, Href: path.Join(configBasePath, section)}
}
func (s *Service) elementLink(section, element string) client.Link {
href := path.Join(configBasePath, section, element)
if element == "" {
href += "/"
}
return client.Link{Relation: client.Self, Href: href}
}
var configLink = client.Link{Relation: client.Self, Href: path.Join(httpd.BasePath, configPath)}
func (s *Service) handleUpdateSection(w http.ResponseWriter, r *http.Request) {
if !s.enabled {
httpd.HttpError(w, "config override service is not enabled", true, http.StatusForbidden)
return
}
section, element, hasSection, hasElement := sectionAndElementFromPath(r.URL.Path, configBasePath)
if !hasSection {
httpd.HttpError(w, "no section specified", true, http.StatusBadRequest)
return
}
ua := updateAction{
section: section,
element: element,
hasElement: hasElement,
}
err := json.NewDecoder(r.Body).Decode(&ua)
if err != nil {
httpd.HttpError(w, fmt.Sprint("failed to decode JSON:", err), true, http.StatusBadRequest)
return
}
// Apply sets/deletes to stored overrides
overrides, saveFunc, err := s.overridesForUpdateAction(ua)
if err != nil {
httpd.HttpError(w, fmt.Sprint("failed to apply update: ", err), true, http.StatusBadRequest)
return
}
// Apply overrides to config object
os := convertOverrides(overrides)
newConfig, err := override.OverrideConfig(s.config, os)
if err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusBadRequest)
return
}
// collect element values
sectionList := make([]interface{}, len(newConfig[section]))
for i, s := range newConfig[section] {
sectionList[i] = s.Value()
}
// Construct ConfigUpdate
errC := make(chan error, 1)
cu := ConfigUpdate{
Name: section,
NewConfig: sectionList,
ErrC: errC,
}
// Send update
sendTimer := time.NewTimer(updateTimeout)
defer sendTimer.Stop()
select {
case <-sendTimer.C:
httpd.HttpError(w, fmt.Sprintf("failed to send configuration update %s/%s: timeout", section, element), true, http.StatusInternalServerError)
return
case s.updates <- cu:
}
// Wait for error
recvTimer := time.NewTimer(updateTimeout)
defer recvTimer.Stop()
select {
case <-recvTimer.C:
httpd.HttpError(w, fmt.Sprintf("failed to update configuration %s/%s: timeout", section, element), true, http.StatusInternalServerError)
return
case err := <-errC:
if err != nil {
httpd.HttpError(w, fmt.Sprintf("failed to update configuration %s/%s: %v", section, element, err), true, http.StatusInternalServerError)
return
}
}
// Save the result of the update
if err := saveFunc(); err != nil {
httpd.HttpError(w, err.Error(), true, http.StatusInternalServerError)
return
}
// Success
w.WriteHeader(http.StatusNoContent)
}
func (s *Service) handleGetConfig(w http.ResponseWriter, r *http.Request) {
if !s.enabled {
httpd.HttpError(w, "config override service is not enabled", true, http.StatusForbidden)
return
}
section, element, hasSection, hasElement := sectionAndElementFromPath(r.URL.Path, configBasePath)
config, err := s.getConfig(section)
if err != nil {
httpd.HttpError(w, fmt.Sprint("failed to resolve current config:", err), true, http.StatusInternalServerError)
return
}
if hasSection && section == "" {
httpd.HttpError(w, "section not specified, do you have an extra trailing '/'?", true, http.StatusBadRequest)
return
}
if !hasSection {
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(config); err != nil {
s.diag.Error("failed to JSON encode configuration", err)
}
} else if section != "" {
sec, ok := config.Sections[section]
if !ok {
httpd.HttpError(w, fmt.Sprint("unknown section: ", section), true, http.StatusNotFound)
return
}
if hasElement {
var elementEntry client.ConfigElement
// Find specified element
elementKey := s.elementKeys[section]
found := false
for _, e := range sec.Elements {
if (element == "" && elementKey == "") || e.Options[elementKey] == element {
elementEntry = e
found = true
break
}
}
if found {
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(elementEntry); err != nil {
s.diag.Error("failed to JSON encode element", err)
}
} else {
httpd.HttpError(w, fmt.Sprintf("unknown section/element: %s/%s", section, element), true, http.StatusNotFound)
return
}
} else {
w.WriteHeader(http.StatusOK)
if err := json.NewEncoder(w).Encode(sec); err != nil {
s.diag.Error("failed to JSON encode sec", err)
}
}
}
}
// overridesForUpdateAction produces a list of overrides relevant to the update action and
// returns save function. Call the save function to permanently store the result of the update.
func (s *Service) overridesForUpdateAction(ua updateAction) ([]Override, func() error, error) {
if err := ua.Validate(); err != nil {
return nil, nil, errors.Wrap(err, "invalid update action")
}
section := ua.section
element := ua.element
if len(ua.Remove) == 0 {
// If we are adding find element value based on the element key
if len(ua.Add) > 0 {
key, ok := s.elementKeys[section]
if !ok {
return nil, nil, fmt.Errorf("unknown section %q", section)
}
if key == "" {
return nil, nil, fmt.Errorf("section %q is not a list, cannot add new element", section)
}
elementValue, ok := ua.Add[key]
if !ok {
return nil, nil, fmt.Errorf("missing key %q in \"add\" map", key)
}
if str, ok := elementValue.(string); !ok {
return nil, nil, fmt.Errorf("expected %q key to be a string, got %T", key, elementValue)
} else {
element = str
}
}
id := sectionAndElementToID(section, element)
// Apply changes to single override
o, err := s.overrides.Get(id)
if err == ErrNoOverrideExists {
o = Override{
ID: id,
Options: make(map[string]interface{}),
}
} else if err != nil {
return nil, nil, errors.Wrapf(err, "failed to retrieve existing overrides for %s", id)
} else if err == nil && len(ua.Add) > 0 {
return nil, nil, errors.Wrapf(err, "cannot add new override, override already exists for %s", id)
}
if len(ua.Add) > 0 {
// Drop all previous options and only use the current set.
o.Options = make(map[string]interface{}, len(ua.Add))
o.Create = true
for k, v := range ua.Add {
o.Options[k] = v
}
} else {
for k, v := range ua.Set {
o.Options[k] = v
}
for _, k := range ua.Delete {
delete(o.Options, k)
}
}
saveFunc := func() error {
if err := s.overrides.Set(o); err != nil {
return errors.Wrapf(err, "failed to save override %s", o.ID)
}
return nil
}
// Get all overrides for the section
overrides, err := s.overrides.List(section)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to get existing overrides for section %s", ua.section)
}
// replace modified override
found := false
for i := range overrides {
if overrides[i].ID == id {
overrides[i] = o
found = true
break
}
}
if !found {
overrides = append(overrides, o)
}
return overrides, saveFunc, nil
} else {
// Remove the list of overrides
removed := make([]string, len(ua.Remove))
removeLookup := make(map[string]bool, len(ua.Remove))
for i, r := range ua.Remove {
id := sectionAndElementToID(section, r)
removed[i] = id
removeLookup[id] = true
}
// Get overrides for the section
overrides, err := s.overrides.List(section)
if err != nil {
return nil, nil, errors.Wrapf(err, "failed to get existing overrides for section %s", ua.section)
}
// Filter overrides
filtered := overrides[:0]
for _, o := range overrides {
if !removeLookup[o.ID] {
filtered = append(filtered, o)
}
}
saveFunc := func() error {
for _, id := range removed {
if err := s.overrides.Delete(id); err != nil {
return errors.Wrapf(err, "failed to remove existing override %s", id)
}
}
return nil
}
return filtered, saveFunc, nil
}
}
func convertOverrides(overrides []Override) []override.Override {
os := make([]override.Override, len(overrides))
for i, o := range overrides {
section, element := sectionAndElementFromID(o.ID)
if o.Create {
element = ""
}
os[i] = override.Override{
Section: section,
Element: element,
Options: o.Options,
Create: o.Create,
}
}
return os
}
// getConfig returns a map of a fully resolved configuration object.
func (s *Service) getConfig(section string) (client.ConfigSections, error) {
overrides, err := s.overrides.List(section)
if err != nil {
return client.ConfigSections{}, errors.Wrap(err, "failed to retrieve config overrides")
}
os := convertOverrides(overrides)
sections, err := override.OverrideConfig(s.config, os)
if err != nil {
return client.ConfigSections{}, errors.Wrap(err, "failed to apply configuration overrides")
}
config := client.ConfigSections{
Link: configLink,
Sections: make(map[string]client.ConfigSection, len(sections)),
}
for name, elements := range sections {
if !strings.HasPrefix(name, section) {
// Skip sections we did not request
continue
}
sec := config.Sections[name]
sec.Link = s.sectionLink(name)
for _, element := range elements {
redacted, list, err := element.Redacted()
if err != nil {
return client.ConfigSections{}, errors.Wrap(err, "failed to get redacted configuration data")
}
sec.Elements = append(sec.Elements, client.ConfigElement{
Link: s.elementLink(name, element.ElementID()),
Options: redacted,
Redacted: list,
})
}
config.Sections[name] = sec
}
return config, nil
}
func (s *Service) Config() (map[string][]interface{}, error) {
overrides, err := s.overrides.List("")
if err != nil {
return nil, errors.Wrap(err, "failed to retrieve config overrides")
}
os := convertOverrides(overrides)
sections, err := override.OverrideConfig(s.config, os)
if err != nil {
return nil, errors.Wrap(err, "failed to apply configuration overrides")
}
config := make(map[string][]interface{}, len(sections))
for name, sectionList := range sections {
for _, section := range sectionList {
config[name] = append(config[name], section.Value())
}
}
return config, nil
}