-
-
Notifications
You must be signed in to change notification settings - Fork 190
/
datasource.go
384 lines (345 loc) · 9.75 KB
/
datasource.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
package data
import (
"context"
"fmt"
"mime"
"net/http"
"net/url"
"path/filepath"
"strings"
"github.com/spf13/afero"
"github.com/pkg/errors"
"github.com/hairyhenderson/gomplate/v3/internal/config"
"github.com/hairyhenderson/gomplate/v3/libkv"
"github.com/hairyhenderson/gomplate/v3/vault"
)
func regExtension(ext, typ string) {
err := mime.AddExtensionType(ext, typ)
if err != nil {
panic(err)
}
}
func init() {
// Add some types we want to be able to handle which can be missing by default
regExtension(".json", jsonMimetype)
regExtension(".yml", yamlMimetype)
regExtension(".yaml", yamlMimetype)
regExtension(".csv", csvMimetype)
regExtension(".toml", tomlMimetype)
regExtension(".env", envMimetype)
}
// registerReaders registers the source-reader functions
func (d *Data) registerReaders() {
d.sourceReaders = make(map[string]func(*Source, ...string) ([]byte, error))
d.sourceReaders["aws+smp"] = readAWSSMP
d.sourceReaders["aws+sm"] = readAWSSecretsManager
d.sourceReaders["boltdb"] = readBoltDB
d.sourceReaders["consul"] = readConsul
d.sourceReaders["consul+http"] = readConsul
d.sourceReaders["consul+https"] = readConsul
d.sourceReaders["env"] = readEnv
d.sourceReaders["file"] = readFile
d.sourceReaders["http"] = readHTTP
d.sourceReaders["https"] = readHTTP
d.sourceReaders["merge"] = d.readMerge
d.sourceReaders["stdin"] = readStdin
d.sourceReaders["vault"] = readVault
d.sourceReaders["vault+http"] = readVault
d.sourceReaders["vault+https"] = readVault
d.sourceReaders["s3"] = readBlob
d.sourceReaders["gs"] = readBlob
d.sourceReaders["git"] = readGit
d.sourceReaders["git+file"] = readGit
d.sourceReaders["git+http"] = readGit
d.sourceReaders["git+https"] = readGit
d.sourceReaders["git+ssh"] = readGit
}
// lookupReader - return the reader function for the given scheme
func (d *Data) lookupReader(scheme string) (func(*Source, ...string) ([]byte, error), error) {
if d.sourceReaders == nil {
d.registerReaders()
}
r, ok := d.sourceReaders[scheme]
if !ok {
return nil, errors.Errorf("scheme %s not registered", scheme)
}
return r, nil
}
// Data -
type Data struct {
ctx context.Context
Sources map[string]*Source
sourceReaders map[string]func(*Source, ...string) ([]byte, error)
cache map[string][]byte
// headers from the --datasource-header/-H option that don't reference datasources from the commandline
extraHeaders map[string]http.Header
}
// Cleanup - clean up datasources before shutting the process down - things
// like Logging out happen here
func (d *Data) Cleanup() {
for _, s := range d.Sources {
s.cleanup()
}
}
// NewData - constructor for Data
// Deprecated: will be replaced in future
func NewData(datasourceArgs, headerArgs []string) (*Data, error) {
cfg := &config.Config{}
err := cfg.ParseDataSourceFlags(datasourceArgs, nil, headerArgs)
if err != nil {
return nil, err
}
data := FromConfig(context.Background(), cfg)
return data, nil
}
// FromConfig - internal use only!
func FromConfig(ctx context.Context, cfg *config.Config) *Data {
// XXX: This is temporary, and will be replaced with something a bit cleaner
// when datasources are refactored
stdin = cfg.Stdin
sources := map[string]*Source{}
for alias, d := range cfg.DataSources {
sources[alias] = &Source{
Alias: alias,
URL: d.URL,
header: d.Header,
}
}
for alias, d := range cfg.Context {
sources[alias] = &Source{
Alias: alias,
URL: d.URL,
header: d.Header,
}
}
return &Data{
ctx: ctx,
Sources: sources,
extraHeaders: cfg.ExtraHeaders,
}
}
// Source - a data source
type Source struct {
URL *url.URL
fs afero.Fs // used for file: URLs, nil otherwise
hc *http.Client // used for http[s]: URLs, nil otherwise
vc *vault.Vault // used for vault: URLs, nil otherwise
kv *libkv.LibKV // used for consul:, etcd:, zookeeper: & boltdb: URLs, nil otherwise
asmpg awssmpGetter // used for aws+smp:, nil otherwise
awsSecretsManager awsSecretsManagerGetter // used for aws+sm, nil otherwise
header http.Header // used for http[s]: URLs, nil otherwise
Alias string
mediaType string
}
func (s *Source) inherit(parent *Source) {
s.fs = parent.fs
s.hc = parent.hc
s.vc = parent.vc
s.kv = parent.kv
s.asmpg = parent.asmpg
}
func (s *Source) cleanup() {
if s.vc != nil {
s.vc.Logout()
}
if s.kv != nil {
s.kv.Logout()
}
}
// mimeType returns the MIME type to use as a hint for parsing the datasource.
// It's expected that the datasource will have already been read before
// this function is called, and so the Source's Type property may be already set.
//
// The MIME type is determined by these rules:
// 1. the 'type' URL query parameter is used if present
// 2. otherwise, the Type property on the Source is used, if present
// 3. otherwise, a MIME type is calculated from the file extension, if the extension is registered
// 4. otherwise, the default type of 'text/plain' is used
func (s *Source) mimeType(arg string) (mimeType string, err error) {
if len(arg) > 0 {
if strings.HasPrefix(arg, "//") {
arg = arg[1:]
}
if !strings.HasPrefix(arg, "/") {
arg = "/" + arg
}
}
argURL, err := url.Parse(arg)
if err != nil {
return "", fmt.Errorf("mimeType: couldn't parse arg %q: %w", arg, err)
}
mediatype := argURL.Query().Get("type")
if mediatype == "" {
mediatype = s.URL.Query().Get("type")
}
if mediatype == "" {
mediatype = s.mediaType
}
// make it so + doesn't need to be escaped
mediatype = strings.ReplaceAll(mediatype, " ", "+")
if mediatype == "" {
ext := filepath.Ext(argURL.Path)
mediatype = mime.TypeByExtension(ext)
}
if mediatype == "" {
ext := filepath.Ext(s.URL.Path)
mediatype = mime.TypeByExtension(ext)
}
if mediatype != "" {
t, _, err := mime.ParseMediaType(mediatype)
if err != nil {
return "", errors.Wrapf(err, "MIME type was %q", mediatype)
}
mediatype = t
return mediatype, nil
}
return textMimetype, nil
}
// String is the method to format the flag's value, part of the flag.Value interface.
// The String method's output will be used in diagnostics.
func (s *Source) String() string {
return fmt.Sprintf("%s=%s (%s)", s.Alias, s.URL.String(), s.mediaType)
}
// DefineDatasource -
func (d *Data) DefineDatasource(alias, value string) (string, error) {
if alias == "" {
return "", errors.New("datasource alias must be provided")
}
if d.DatasourceExists(alias) {
return "", nil
}
srcURL, err := config.ParseSourceURL(value)
if err != nil {
return "", err
}
s := &Source{
Alias: alias,
URL: srcURL,
header: d.extraHeaders[alias],
}
if d.Sources == nil {
d.Sources = make(map[string]*Source)
}
d.Sources[alias] = s
return "", nil
}
// DatasourceExists -
func (d *Data) DatasourceExists(alias string) bool {
_, ok := d.Sources[alias]
return ok
}
func (d *Data) lookupSource(alias string) (*Source, error) {
source, ok := d.Sources[alias]
if !ok {
srcURL, err := url.Parse(alias)
if err != nil || !srcURL.IsAbs() {
return nil, errors.Errorf("Undefined datasource '%s'", alias)
}
source = &Source{
Alias: alias,
URL: srcURL,
header: d.extraHeaders[alias],
}
d.Sources[alias] = source
}
if source.Alias == "" {
source.Alias = alias
}
return source, nil
}
func (d *Data) readDataSource(alias string, args ...string) (data, mimeType string, err error) {
source, err := d.lookupSource(alias)
if err != nil {
return "", "", err
}
b, err := d.readSource(source, args...)
if err != nil {
return "", "", errors.Wrapf(err, "Couldn't read datasource '%s'", alias)
}
subpath := ""
if len(args) > 0 {
subpath = args[0]
}
mimeType, err = source.mimeType(subpath)
if err != nil {
return "", "", err
}
return string(b), mimeType, nil
}
// Include -
func (d *Data) Include(alias string, args ...string) (string, error) {
data, _, err := d.readDataSource(alias, args...)
return data, err
}
// Datasource -
func (d *Data) Datasource(alias string, args ...string) (interface{}, error) {
data, mimeType, err := d.readDataSource(alias, args...)
if err != nil {
return nil, err
}
return parseData(mimeType, data)
}
func parseData(mimeType, s string) (out interface{}, err error) {
switch mimeAlias(mimeType) {
case jsonMimetype:
out, err = JSON(s)
if err != nil {
// maybe it's a JSON array
out, err = JSONArray(s)
}
case jsonArrayMimetype:
out, err = JSONArray(s)
case yamlMimetype:
out, err = YAML(s)
if err != nil {
// maybe it's a YAML array
out, err = YAMLArray(s)
}
case csvMimetype:
out, err = CSV(s)
case tomlMimetype:
out, err = TOML(s)
case envMimetype:
out, err = dotEnv(s)
case textMimetype:
out = s
default:
return nil, errors.Errorf("Datasources of type %s not yet supported", mimeType)
}
return out, err
}
// DatasourceReachable - Determines if the named datasource is reachable with
// the given arguments. Reads from the datasource, and discards the returned data.
func (d *Data) DatasourceReachable(alias string, args ...string) bool {
source, ok := d.Sources[alias]
if !ok {
return false
}
_, err := d.readSource(source, args...)
return err == nil
}
// readSource returns the (possibly cached) data from the given source,
// as referenced by the given args
func (d *Data) readSource(source *Source, args ...string) ([]byte, error) {
if d.cache == nil {
d.cache = make(map[string][]byte)
}
cacheKey := source.Alias
for _, v := range args {
cacheKey += v
}
cached, ok := d.cache[cacheKey]
if ok {
return cached, nil
}
r, err := d.lookupReader(source.URL.Scheme)
if err != nil {
return nil, errors.Wrap(err, "Datasource not yet supported")
}
data, err := r(source, args...)
if err != nil {
return nil, err
}
d.cache[cacheKey] = data
return data, nil
}