-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathconfig.go
321 lines (274 loc) · 8.5 KB
/
config.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
package backend
import (
"context"
"encoding/base64"
"encoding/pem"
"errors"
"fmt"
"os"
"strconv"
"strings"
"github.com/grafana/grafana-plugin-sdk-go/backend/proxy"
"github.com/grafana/grafana-plugin-sdk-go/backend/useragent"
"github.com/grafana/grafana-plugin-sdk-go/experimental/featuretoggles"
)
const (
AppURL = "GF_APP_URL"
ConcurrentQueryCount = "GF_CONCURRENT_QUERY_COUNT"
UserFacingDefaultError = "GF_USER_FACING_DEFAULT_ERROR"
SQLRowLimit = "GF_SQL_ROW_LIMIT"
SQLMaxOpenConnsDefault = "GF_SQL_MAX_OPEN_CONNS_DEFAULT"
SQLMaxIdleConnsDefault = "GF_SQL_MAX_IDLE_CONNS_DEFAULT"
SQLMaxConnLifetimeSecondsDefault = "GF_SQL_MAX_CONN_LIFETIME_SECONDS_DEFAULT"
ResponseLimit = "GF_RESPONSE_LIMIT"
AppClientSecret = "GF_PLUGIN_APP_CLIENT_SECRET" // nolint:gosec
)
type configKey struct{}
// GrafanaConfigFromContext returns Grafana config from context.
func GrafanaConfigFromContext(ctx context.Context) *GrafanaCfg {
v := ctx.Value(configKey{})
if v == nil {
return NewGrafanaCfg(nil)
}
cfg := v.(*GrafanaCfg)
if cfg == nil {
return NewGrafanaCfg(nil)
}
return cfg
}
// WithGrafanaConfig injects supplied Grafana config into context.
func WithGrafanaConfig(ctx context.Context, cfg *GrafanaCfg) context.Context {
ctx = context.WithValue(ctx, configKey{}, cfg)
return ctx
}
type GrafanaCfg struct {
config map[string]string
}
func NewGrafanaCfg(cfg map[string]string) *GrafanaCfg {
return &GrafanaCfg{config: cfg}
}
func (c *GrafanaCfg) Get(key string) string {
return c.config[key]
}
func (c *GrafanaCfg) FeatureToggles() FeatureToggles {
features, exists := c.config[featuretoggles.EnabledFeatures]
if !exists || features == "" {
return FeatureToggles{}
}
fs := strings.Split(features, ",")
enabledFeatures := make(map[string]struct{}, len(fs))
for _, f := range fs {
enabledFeatures[f] = struct{}{}
}
return FeatureToggles{
enabled: enabledFeatures,
}
}
func (c *GrafanaCfg) Equal(c2 *GrafanaCfg) bool {
if c == nil && c2 == nil {
return true
}
if c == nil || c2 == nil {
return false
}
if len(c.config) != len(c2.config) {
return false
}
for k, v1 := range c.config {
if v2, ok := c2.config[k]; !ok || v1 != v2 {
return false
}
}
return true
}
// ProxyHash returns the last four characters of the base64-encoded
// PDC client key contents, if present, for use in datasource instance
// caching. The contents should be PEM-encoded, so we try to PEM-decode
// them, and, if successful, return the base-64 encoding of the final three bytes,
// giving a four character hash.
func (c *GrafanaCfg) ProxyHash() string {
if c == nil {
return ""
}
contents := c.config[proxy.PluginSecureSocksProxyClientKeyContents]
if contents == "" {
return ""
}
block, _ := pem.Decode([]byte(contents))
if block == nil {
Logger.Warn("ProxyHash(): key contents are not PEM-encoded")
return ""
}
if block.Type != "PRIVATE KEY" {
Logger.Warn("ProxyHash(): key contents are not PEM-encoded private key")
return ""
}
bl := len(block.Bytes)
if bl < 3 {
Logger.Warn("ProxyHash(): key contents too short")
return ""
}
return base64.StdEncoding.EncodeToString(block.Bytes[bl-3:])
}
type FeatureToggles struct {
// enabled is a set-like map of feature flags that are enabled.
enabled map[string]struct{}
}
// IsEnabled returns true if feature f is contained in ft.enabled.
func (ft FeatureToggles) IsEnabled(f string) bool {
_, exists := ft.enabled[f]
return exists
}
type Proxy struct {
clientCfg *proxy.ClientCfg
}
func (c *GrafanaCfg) proxy() (Proxy, error) {
if v, exists := c.config[proxy.PluginSecureSocksProxyEnabled]; exists && v == strconv.FormatBool(true) {
var (
allowInsecure = false
err error
)
if v := c.Get(proxy.PluginSecureSocksProxyAllowInsecure); v != "" {
allowInsecure, err = strconv.ParseBool(c.Get(proxy.PluginSecureSocksProxyAllowInsecure))
if err != nil {
return Proxy{}, fmt.Errorf("parsing %s, value must be a boolean: %w", proxy.PluginSecureSocksProxyAllowInsecure, err)
}
}
var rootCaVals []string
if v = c.Get(proxy.PluginSecureSocksProxyRootCAsContents); v != "" {
rootCaVals = strings.Split(c.Get(proxy.PluginSecureSocksProxyRootCAsContents), ",")
}
return Proxy{
clientCfg: &proxy.ClientCfg{
ClientCert: c.Get(proxy.PluginSecureSocksProxyClientCert),
ClientCertVal: c.Get(proxy.PluginSecureSocksProxyClientCertContents),
ClientKey: c.Get(proxy.PluginSecureSocksProxyClientKey),
ClientKeyVal: c.Get(proxy.PluginSecureSocksProxyClientKeyContents),
RootCAs: strings.Split(c.Get(proxy.PluginSecureSocksProxyRootCAs), " "),
RootCAsVals: rootCaVals,
ProxyAddress: c.Get(proxy.PluginSecureSocksProxyProxyAddress),
ServerName: c.Get(proxy.PluginSecureSocksProxyServerName),
AllowInsecure: allowInsecure,
},
}, nil
}
return Proxy{}, nil
}
func (c *GrafanaCfg) AppURL() (string, error) {
url, ok := c.config[AppURL]
if !ok {
// Fallback to environment variable for backwards compatibility
url = os.Getenv(AppURL)
if url == "" {
return "", errors.New("app URL not set in config. A more recent version of Grafana may be required")
}
}
return url, nil
}
func (c *GrafanaCfg) ConcurrentQueryCount() (int, error) {
count, ok := c.config[ConcurrentQueryCount]
if !ok {
return 0, fmt.Errorf("ConcurrentQueryCount not set in config")
}
i, err := strconv.Atoi(count)
if err != nil {
return 0, fmt.Errorf("ConcurrentQueryCount cannot be converted to integer")
}
return i, nil
}
type SQLConfig struct {
RowLimit int64
DefaultMaxOpenConns int
DefaultMaxIdleConns int
DefaultMaxConnLifetimeSeconds int
}
func (c *GrafanaCfg) SQL() (SQLConfig, error) {
// max open connections
maxOpenString, ok := c.config[SQLMaxOpenConnsDefault]
if !ok {
return SQLConfig{}, errors.New("SQLDatasourceMaxOpenConnsDefault not set in config")
}
maxOpen, err := strconv.Atoi(maxOpenString)
if err != nil {
return SQLConfig{}, errors.New("SQLDatasourceMaxOpenConnsDefault config value is not a valid integer")
}
// max idle connections
maxIdleString, ok := c.config[SQLMaxIdleConnsDefault]
if !ok {
return SQLConfig{}, errors.New("SQLDatasourceMaxIdleConnsDefault not set in config")
}
maxIdle, err := strconv.Atoi(maxIdleString)
if err != nil {
return SQLConfig{}, errors.New("SQLDatasourceMaxIdleConnsDefault config value is not a valid integer")
}
// max connection lifetime
maxLifeString, ok := c.config[SQLMaxConnLifetimeSecondsDefault]
if !ok {
return SQLConfig{}, errors.New("SQLDatasourceMaxConnLifetimeDefault not set in config")
}
maxLife, err := strconv.Atoi(maxLifeString)
if err != nil {
return SQLConfig{}, errors.New("SQLDatasourceMaxConnLifetimeDefault config value is not a valid integer")
}
rowLimitString, ok := c.config[SQLRowLimit]
if !ok {
return SQLConfig{}, errors.New("RowLimit not set in config")
}
rowLimit, err := strconv.ParseInt(rowLimitString, 10, 64)
if err != nil {
return SQLConfig{}, errors.New("RowLimit in config is not a valid integer")
}
return SQLConfig{
RowLimit: rowLimit,
DefaultMaxOpenConns: maxOpen,
DefaultMaxIdleConns: maxIdle,
DefaultMaxConnLifetimeSeconds: maxLife,
}, nil
}
func (c *GrafanaCfg) UserFacingDefaultError() (string, error) {
value, ok := c.config[UserFacingDefaultError]
if !ok {
return "", errors.New("UserFacingDefaultError not set in config")
}
return value, nil
}
func (c *GrafanaCfg) ResponseLimit() int64 {
count, ok := c.config[ResponseLimit]
if !ok {
return 0
}
i, err := strconv.ParseInt(count, 10, 64)
if err != nil {
return 0
}
return i
}
func (c *GrafanaCfg) PluginAppClientSecret() (string, error) {
value, ok := c.config[AppClientSecret]
if !ok {
// Fallback to environment variable for backwards compatibility
value = os.Getenv(AppClientSecret)
if value == "" {
return "", errors.New("PluginAppClientSecret not set in config")
}
}
return value, nil
}
type userAgentKey struct{}
// UserAgentFromContext returns user agent from context.
func UserAgentFromContext(ctx context.Context) *useragent.UserAgent {
v := ctx.Value(userAgentKey{})
if v == nil {
return useragent.Empty()
}
ua := v.(*useragent.UserAgent)
if ua == nil {
return useragent.Empty()
}
return ua
}
// WithUserAgent injects supplied user agent into context.
func WithUserAgent(ctx context.Context, ua *useragent.UserAgent) context.Context {
ctx = context.WithValue(ctx, userAgentKey{}, ua)
return ctx
}