forked from DataDog/datadog-agent
/
server.go
125 lines (113 loc) · 3.72 KB
/
server.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
package http
import (
"encoding/json"
"html"
"net/http"
ddconfig "github.com/StackVista/stackstate-agent/pkg/config"
"github.com/StackVista/stackstate-agent/pkg/config/settings"
"github.com/StackVista/stackstate-agent/pkg/util/log"
"github.com/StackVista/stackstate-agent/pkg/util/scrubber"
"github.com/gorilla/mux"
"gopkg.in/yaml.v2"
)
// Server offers functions that implement the standard runtime settings HTTP API
var Server = struct {
GetFull func(string) http.HandlerFunc
GetValue http.HandlerFunc
SetValue http.HandlerFunc
ListConfigurable http.HandlerFunc
}{
GetFull: getFullConfig,
GetValue: getConfigValue,
SetValue: setConfigValue,
ListConfigurable: listConfigurableSettings,
}
func getFullConfig(namespace string) http.HandlerFunc {
return func(w http.ResponseWriter, _ *http.Request) {
var nsSettings interface{}
allSettings := ddconfig.Datadog.AllSettings()
if namespace != "" {
for k, v := range allSettings {
if k == namespace {
nsSettings = v
break
}
}
} else {
nsSettings = allSettings
}
runtimeConfig, err := yaml.Marshal(nsSettings)
if err != nil {
log.Errorf("Unable to marshal runtime config response: %s", err)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), http.StatusInternalServerError)
return
}
scrubbed, err := scrubber.ScrubBytes(runtimeConfig)
if err != nil {
log.Errorf("Unable to scrub sensitive data from runtime config: %s", err)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), http.StatusInternalServerError)
return
}
_, _ = w.Write(scrubbed)
}
}
func listConfigurableSettings(w http.ResponseWriter, _ *http.Request) {
configurableSettings := make(map[string]settings.RuntimeSettingResponse)
for name, setting := range settings.RuntimeSettings() {
configurableSettings[name] = settings.RuntimeSettingResponse{
Description: setting.Description(),
Hidden: setting.Hidden(),
}
}
body, err := json.Marshal(configurableSettings)
if err != nil {
log.Errorf("Unable to marshal runtime configurable settings list response: %s", err)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), http.StatusInternalServerError)
return
}
_, _ = w.Write(body)
}
func getConfigValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
setting := vars["setting"]
log.Infof("Got a request to read a setting value: %s", setting)
val, err := settings.GetRuntimeSetting(setting)
if err != nil {
body, _ := json.Marshal(map[string]string{"error": err.Error()})
switch err.(type) {
case *settings.SettingNotFoundError:
http.Error(w, string(body), http.StatusBadRequest)
default:
http.Error(w, string(body), http.StatusInternalServerError)
}
return
}
body, err := json.Marshal(map[string]interface{}{"value": val})
if err != nil {
log.Errorf("Unable to marshal runtime setting value response: %s", err)
body, _ := json.Marshal(map[string]string{"error": err.Error()})
http.Error(w, string(body), http.StatusInternalServerError)
return
}
_, _ = w.Write(body)
}
func setConfigValue(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
setting := vars["setting"]
log.Infof("Got a request to change a setting: %s", setting)
_ = r.ParseForm()
value := html.UnescapeString(r.Form.Get("value"))
if err := settings.SetRuntimeSetting(setting, value); err != nil {
body, _ := json.Marshal(map[string]string{"error": err.Error()})
switch err.(type) {
case *settings.SettingNotFoundError:
http.Error(w, string(body), http.StatusBadRequest)
default:
http.Error(w, string(body), http.StatusInternalServerError)
}
return
}
}