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
|
package datafs
import (
"context"
"encoding/json"
"fmt"
"io"
"io/fs"
"net/http"
"net/url"
"os"
"path"
"runtime"
"strings"
"github.com/hairyhenderson/go-fsimpl"
"github.com/hairyhenderson/gomplate/v4/internal/config"
"github.com/hairyhenderson/gomplate/v4/internal/iohelpers"
)
const osWindows = "windows"
// typeOverrideParam gets the query parameter used to override the content type
// used to parse a given datasource - use GOMPLATE_TYPE_PARAM to use a different
// parameter name.
func typeOverrideParam() string {
if v := os.Getenv("GOMPLATE_TYPE_PARAM"); v != "" {
return v
}
return "type"
}
// DataSourceReader reads content from a datasource
type DataSourceReader interface {
// ReadSource reads the content of a datasource, given an alias and optional
// arguments. If the datasource is not found, the alias is interpreted as a
// URL. If the alias is not a valid URL, an error is returned.
//
// Returned content is cached, so subsequent calls with the same alias and
// arguments will return the same content.
ReadSource(ctx context.Context, alias string, args ...string) (string, []byte, error)
// contains registry
Registry
}
type dsReader struct {
cache map[string]*content
Registry
}
// content type mainly for caching
type content struct {
contentType string
b []byte
}
func NewSourceReader(reg Registry) DataSourceReader {
return &dsReader{Registry: reg}
}
func (d *dsReader) ReadSource(ctx context.Context, alias string, args ...string) (string, []byte, error) {
source, ok := d.Lookup(alias)
if !ok {
srcURL, err := url.Parse(alias)
if err != nil || !srcURL.IsAbs() {
return "", nil, fmt.Errorf("undefined datasource '%s': %w", alias, err)
}
d.Register(alias, config.DataSource{URL: srcURL})
// repeat the lookup now that it's registered - we shouldn't just use
// it directly because registration may include extra headers
source, _ = d.Lookup(alias)
}
if d.cache == nil {
d.cache = make(map[string]*content)
}
cacheKey := alias
for _, v := range args {
cacheKey += v
}
cached, ok := d.cache[cacheKey]
if ok {
return cached.contentType, cached.b, nil
}
arg := ""
if len(args) > 0 {
arg = args[0]
}
u, err := resolveURL(*source.URL, arg)
if err != nil {
return "", nil, err
}
fc, err := d.readFileContent(ctx, u, source.Header)
if err != nil {
return "", nil, fmt.Errorf("couldn't read datasource '%s' (%s): %w", alias, u, err)
}
d.cache[cacheKey] = fc
return fc.contentType, fc.b, nil
}
func removeQueryParam(u *url.URL, key string) *url.URL {
q := u.Query()
q.Del(key)
u.RawQuery = q.Encode()
return u
}
func (d *dsReader) readFileContent(ctx context.Context, u *url.URL, hdr http.Header) (*content, error) {
// possible type hint in the type query param. Contrary to spec, we allow
// unescaped '+' characters to make it simpler to provide types like
// "application/array+json"
overrideType := typeOverrideParam()
mimeType := u.Query().Get(overrideType)
mimeType = strings.ReplaceAll(mimeType, " ", "+")
// now that we have the hint, remove it from the URL - we can't have it
// leaking into the filesystem layer
u = removeQueryParam(u, overrideType)
u, fname := SplitFSMuxURL(u)
fsys, err := FSysForPath(ctx, u.String())
if err != nil {
return nil, fmt.Errorf("fsys for path %v: %w", u, err)
}
// need to support absolute paths on local filesystem too
// TODO: this is a hack, probably fix this?
if u.Scheme == "file" && runtime.GOOS != osWindows {
fname = u.Path + fname
}
fsys = fsimpl.WithContextFS(ctx, fsys)
fsys = fsimpl.WithHeaderFS(hdr, fsys)
fsys = WithDataSourceRegistryFS(d.Registry, fsys)
f, err := fsys.Open(fname)
if err != nil {
return nil, fmt.Errorf("open (url: %q, name: %q): %w", u, fname, err)
}
defer f.Close()
fi, err := f.Stat()
if err != nil {
return nil, fmt.Errorf("stat (url: %q, name: %q): %w", u, fname, err)
}
if mimeType == "" {
mimeType = fsimpl.ContentType(fi)
}
var data []byte
if fi.IsDir() {
var dirents []fs.DirEntry
dirents, err = fs.ReadDir(fsys, fname)
if err != nil {
return nil, fmt.Errorf("readDir (url: %q, name: %s): %w", u, fname, err)
}
entries := make([]string, len(dirents))
for i, e := range dirents {
entries[i] = e.Name()
}
data, err = json.Marshal(entries)
if err != nil {
return nil, fmt.Errorf("json.Marshal: %w", err)
}
mimeType = iohelpers.JSONArrayMimetype
} else {
data, err = io.ReadAll(f)
if err != nil {
return nil, fmt.Errorf("read (url: %q, name: %s): %w", u, fname, err)
}
}
if mimeType == "" {
// default to text/plain
mimeType = iohelpers.TextMimetype
}
return &content{contentType: mimeType, b: data}, nil
}
// resolveURL parses the relative URL rel against base, and returns the
// resolved URL. Differs from url.ResolveReference in that query parameters are
// added. In case of duplicates, params from rel are used.
func resolveURL(base url.URL, rel string) (*url.URL, error) {
if rel == "" {
return &base, nil
}
// git URLs are special - they have double-slashes that separate a repo
// from a path in the repo. A missing double-slash means the path is the
// root.
switch base.Scheme {
case "git", "git+file", "git+http", "git+https", "git+ssh":
if strings.Contains(base.Path, "//") && strings.Contains(rel, "//") {
return nil, fmt.Errorf("both base URL and subpath contain '//', which is not allowed in git URLs")
}
// If there's a subpath, the base path must end with '/'. This behaviour
// is unique to git URLs - other schemes would instead drop the last
// path element and replace with the subpath.
if rel != "" && !strings.HasSuffix(base.Path, "/") {
base.Path += "/"
}
// If subpath starts with '//', make it relative by prefixing a '.',
// otherwise it'll be treated as a schemeless URI and the first part
// will be interpreted as a hostname.
if strings.HasPrefix(rel, "//") {
rel = "." + rel
}
case "aws+sm":
// aws+sm URLs may be opaque, so resolution needs to be handled
// differently
if base.Opaque != "" {
// if it's opaque and we have a relative path we'll append it to
// the opaque part
if rel != "" {
base.Opaque = path.Join(base.Opaque, rel)
}
return &base, nil
} else if base.Path == "" && !strings.HasPrefix(rel, "/") {
// if the base has no path and the relative URL doesn't start with
// a slash, we treat it as opaque
base.Opaque = rel
}
}
// if there's still an opaque part, there's no resolving to do - just return
// the base URL
if base.Opaque != "" {
return &base, nil
}
relURL, err := url.Parse(rel)
if err != nil {
return nil, err
}
// URL.ResolveReference requires (or assumes, at least) that the base is
// absolute. We want to support relative URLs too though, so we need to
// correct for that.
var out *url.URL
switch {
case base.IsAbs():
out = base.ResolveReference(relURL)
case base.Scheme == "" && base.Path[0] == '/':
// absolute path, no scheme or volume
out = base.ResolveReference(relURL)
out.Path = out.Path[1:]
default:
out = resolveRelativeURL(&base, relURL)
}
if base.RawQuery != "" {
bq := base.Query()
rq := relURL.Query()
for k := range rq {
bq.Set(k, rq.Get(k))
}
out.RawQuery = bq.Encode()
}
return out, nil
}
// relative path, might even involve .. or . in the path
func resolveRelativeURL(base, relURL *url.URL) *url.URL {
// first find the difference between base and what base would be if it
// were absolute
emptyURL, _ := url.Parse("")
absBase := base.ResolveReference(emptyURL)
absBase.Path = strings.TrimPrefix(absBase.Path, "/")
diff := strings.TrimSuffix(base.Path, absBase.Path)
diff = strings.TrimSuffix(diff, "/")
out := base.ResolveReference(relURL)
// now correct the path by adding the prefix back in
out.Path = diff + out.Path
return out
}
|