forked from grafana/grafana-plugin-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
269 lines (216 loc) · 8.13 KB
/
common.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
package backend
import (
"context"
"encoding/json"
"time"
"github.com/kosimas/grafana-plugin-sdk-go/backend/httpclient"
"github.com/kosimas/grafana-plugin-sdk-go/backend/proxy"
"github.com/kosimas/grafana-plugin-sdk-go/internal/tenant"
)
const dataCustomOptionsKey = "grafanaData"
const secureDataCustomOptionsKey = "grafanaSecureData"
// User represents a Grafana user.
type User struct {
Login string
Name string
Email string
Role string
}
// AppInstanceSettings represents settings for an app instance.
//
// In Grafana an app instance is an app plugin of certain
// type that have been configured and enabled in a Grafana organization.
type AppInstanceSettings struct {
// JSONData repeats the properties at this level of the object (excluding DataSourceConfig), and also includes any
// custom properties associated with the plugin config instance.
JSONData json.RawMessage
// DecryptedSecureJSONData contains key,value pairs where the encrypted configuration plugin instance in Grafana
// server have been decrypted before passing them to the plugin.
DecryptedSecureJSONData map[string]string
// Updated is the last time this plugin instance's configuration was updated.
Updated time.Time
}
// HTTPClientOptions creates httpclient.Options based on settings.
func (s *AppInstanceSettings) HTTPClientOptions() (httpclient.Options, error) {
httpSettings, err := parseHTTPSettings(s.JSONData, s.DecryptedSecureJSONData)
if err != nil {
return httpclient.Options{}, err
}
opts := httpSettings.HTTPClientOptions()
setCustomOptionsFromHTTPSettings(&opts, httpSettings)
return opts, nil
}
// DataSourceInstanceSettings represents settings for a data source instance.
//
// In Grafana a data source instance is a data source plugin of certain
// type that have been configured and created in a Grafana organization.
type DataSourceInstanceSettings struct {
// ID is the Grafana assigned numeric identifier of the the data source instance.
ID int64
// UID is the Grafana assigned string identifier of the the data source instance.
UID string
// Type is the unique identifier of the plugin that the request is for.
// This should be the same value as PluginContext.PluginId.
Type string
// Name is the configured name of the data source instance.
Name string
// URL is the configured URL of a data source instance (e.g. the URL of an API endpoint).
URL string
// User is a configured user for a data source instance. This is not a Grafana user, rather an arbitrary string.
User string
// Database is the configured database for a data source instance.
// Only used by Elasticsearch and Influxdb.
// Please use JSONData to store information related to database.
Database string
// BasicAuthEnabled indicates if this data source instance should use basic authentication.
BasicAuthEnabled bool
// BasicAuthUser is the configured user for basic authentication. (e.g. when a data source uses basic
// authentication to connect to whatever API it fetches data from).
BasicAuthUser string
// JSONData contains the raw DataSourceConfig as JSON as stored by Grafana server. It repeats the properties in
// this object and includes custom properties.
JSONData json.RawMessage
// DecryptedSecureJSONData contains key,value pairs where the encrypted configuration in Grafana server have been
// decrypted before passing them to the plugin.
DecryptedSecureJSONData map[string]string
// Updated is the last time the configuration for the data source instance was updated.
Updated time.Time
}
// HTTPClientOptions creates httpclient.Options based on settings.
func (s *DataSourceInstanceSettings) HTTPClientOptions() (httpclient.Options, error) {
httpSettings, err := parseHTTPSettings(s.JSONData, s.DecryptedSecureJSONData)
if err != nil {
return httpclient.Options{}, err
}
if s.BasicAuthEnabled {
httpSettings.BasicAuthEnabled = s.BasicAuthEnabled
httpSettings.BasicAuthUser = s.BasicAuthUser
httpSettings.BasicAuthPassword = s.DecryptedSecureJSONData["basicAuthPassword"]
} else if s.User != "" {
httpSettings.BasicAuthEnabled = true
httpSettings.BasicAuthUser = s.User
httpSettings.BasicAuthPassword = s.DecryptedSecureJSONData["password"]
}
opts := httpSettings.HTTPClientOptions()
opts.Labels["datasource_name"] = s.Name
opts.Labels["datasource_uid"] = s.UID
opts.Labels["datasource_type"] = s.Type
setCustomOptionsFromHTTPSettings(&opts, httpSettings)
opts.ProxyOptions, err = s.ProxyOptions()
if err != nil {
return opts, err
}
return opts, nil
}
// PluginContext holds contextual information about a plugin request, such as
// Grafana organization, user and plugin instance settings.
type PluginContext struct {
// OrgID is The Grafana organization identifier the request originating from.
OrgID int64
// PluginID is the unique identifier of the plugin that the request is for.
PluginID string
// User is the Grafana user making the request.
//
// Will not be provided if Grafana backend initiated the request,
// for example when request is coming from Grafana Alerting.
User *User
// AppInstanceSettings is the configured app instance settings.
//
// In Grafana an app instance is an app plugin of certain
// type that have been configured and enabled in a Grafana organization.
//
// Will only be set if request targeting an app instance.
AppInstanceSettings *AppInstanceSettings
// DataSourceConfig is the configured data source instance
// settings.
//
// In Grafana a data source instance is a data source plugin of certain
// type that have been configured and created in a Grafana organization.
//
// Will only be set if request targeting a data source instance.
DataSourceInstanceSettings *DataSourceInstanceSettings
}
func setCustomOptionsFromHTTPSettings(opts *httpclient.Options, httpSettings *HTTPSettings) {
opts.CustomOptions = map[string]interface{}{}
if httpSettings.JSONData != nil {
opts.CustomOptions[dataCustomOptionsKey] = httpSettings.JSONData
}
if httpSettings.SecureJSONData != nil {
opts.CustomOptions[secureDataCustomOptionsKey] = httpSettings.SecureJSONData
}
}
// JSONDataFromHTTPClientOptions extracts JSON data from CustomOptions of httpclient.Options.
func JSONDataFromHTTPClientOptions(opts httpclient.Options) (res map[string]interface{}) {
if opts.CustomOptions == nil {
return
}
val, exists := opts.CustomOptions[dataCustomOptionsKey]
if !exists {
return
}
jsonData, ok := val.(map[string]interface{})
if !ok {
return
}
return jsonData
}
// SecureJSONDataFromHTTPClientOptions extracts secure JSON data from CustomOptions of httpclient.Options.
func SecureJSONDataFromHTTPClientOptions(opts httpclient.Options) (res map[string]string) {
if opts.CustomOptions == nil {
return
}
val, exists := opts.CustomOptions[secureDataCustomOptionsKey]
if !exists {
return
}
secureJSONData, ok := val.(map[string]string)
if !ok {
return
}
return secureJSONData
}
func propagateTenantIDIfPresent(ctx context.Context) context.Context {
if tid, exists := tenant.IDFromIncomingGRPCContext(ctx); exists {
ctx = tenant.WithTenant(ctx, tid)
}
return ctx
}
func (s *DataSourceInstanceSettings) ProxyOptions() (*proxy.Options, error) {
opts := &proxy.Options{}
var dat map[string]interface{}
if s.JSONData != nil {
if err := json.Unmarshal(s.JSONData, &dat); err != nil {
return nil, err
}
}
opts.Enabled = proxy.SecureSocksProxyEnabledOnDS(dat)
if !opts.Enabled {
return nil, nil
}
opts.Auth = &proxy.AuthOptions{}
opts.Timeouts = &proxy.TimeoutOptions{}
if v, exists := dat["secureSocksProxyUsername"]; exists {
opts.Auth.Username = v.(string)
} else {
// default username is the datasource uid
opts.Auth.Username = s.UID
}
if v, exists := s.DecryptedSecureJSONData["secureSocksProxyPassword"]; exists {
opts.Auth.Password = v
}
if v, exists := dat["timeout"]; exists {
if iv, ok := v.(float64); ok {
opts.Timeouts.Timeout = time.Duration(iv) * time.Second
}
} else {
opts.Timeouts.Timeout = proxy.DefaultTimeoutOptions.Timeout
}
if v, exists := dat["keepAlive"]; exists {
if iv, ok := v.(float64); ok {
opts.Timeouts.KeepAlive = time.Duration(iv) * time.Second
}
} else {
opts.Timeouts.KeepAlive = proxy.DefaultTimeoutOptions.KeepAlive
}
return opts, nil
}