-
Notifications
You must be signed in to change notification settings - Fork 3
/
context.go
312 lines (288 loc) · 8.22 KB
/
context.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
package main
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"time"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/gohcl"
"github.com/hashicorp/hcl/v2/hclsyntax"
"github.com/hashicorp/hcl/v2/json"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
func decodeFile(filenames []string, ctx *hcl.EvalContext, target interface{}) error {
var srcs = make([][]byte, len(filenames))
for i, filename := range filenames {
src, err := ioutil.ReadFile(filename)
if err != nil {
if os.IsNotExist(err) {
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Configuration file not found",
Detail: fmt.Sprintf("The configuration file %s does not exist.", filename),
},
}
}
return hcl.Diagnostics{
{
Severity: hcl.DiagError,
Summary: "Failed to read configuration",
Detail: fmt.Sprintf("Can't read %s: %s.", filename, err),
},
}
}
srcs[i] = src
}
return decode(filenames, srcs, ctx, target)
}
func decode(filenames []string, srcs [][]byte, ctx *hcl.EvalContext, target interface{}) error {
var file *hcl.File
var files []*hcl.File
var diags hcl.Diagnostics
for i, filename := range filenames {
switch suffix := strings.ToLower(filepath.Ext(filename)); suffix {
case ".hcl":
file, diags = hclsyntax.ParseConfig(srcs[i], filename, hcl.Pos{Line: 1, Column: 1})
case ".json":
file, diags = json.Parse(srcs[i], filename)
default:
diags = diags.Append(&hcl.Diagnostic{
Severity: hcl.DiagError,
Summary: "Unsupported file format",
Detail: fmt.Sprintf("Cannot read from %s: unrecognized file format suffix %q.", filename, suffix),
})
return diags
}
if diags.HasErrors() {
return diags
}
files = append(files, file)
}
diags = gohcl.DecodeBody(hcl.MergeFiles(files), ctx, target)
if diags.HasErrors() {
return diags
}
return nil
}
// _context returns the basic context that will be used to initially
// decode HCL documents.
func _context() *hcl.EvalContext {
var paramImpl = func(args []cty.Value, retType cty.Type) (cty.Value, error) {
str := args[0].AsString()
if len(args) != 2 {
str = strings.ToLower(str)
}
return cty.StringVal(fmt.Sprintf("{{ .%s }}", strings.Title(str))), nil
}
var ctx = &hcl.EvalContext{
Functions: map[string]function.Function{
"env": function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "var",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return cty.StringVal(os.Getenv(args[0].AsString())), nil
},
}),
"param": function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "key",
Type: cty.String,
},
},
VarParam: &function.Parameter{
Name: "raw",
Type: cty.Bool,
},
Type: function.StaticReturnType(cty.String),
Impl: paramImpl,
}),
"raw": function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "key",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.String),
Impl: paramImpl,
}),
},
}
return ctx
}
// fileEvalCtx returns a context that should be used when
// a HCL Attribute can only use file functions
var fileEvalCtx = hcl.EvalContext{
Variables: map[string]cty.Value{},
Functions: map[string]function.Function{
"file": FileToStr("file", "ctx"),
},
}
// bodyEvalCtx returns a context that should be used
// for body strings. This gives them all of the same
// features for consiancy.
var bodyEvalCtx = hcl.EvalContext{
Variables: map[string]cty.Value{},
Functions: map[string]function.Function{
"file": FileToStr("body", "ctx"),
},
}
// jwtEvalCtx returns a context that should be used
// with JWT values that can have properties other
// than strings.
func jwtEvalCtx(name, key string, vars map[string]map[string]cty.Value) *hcl.EvalContext {
var ctx = &hcl.EvalContext{
Variables: map[string]cty.Value{},
Functions: map[string]function.Function{
"now": NowToStr,
"file": FileToStr(name, key),
"unix": UnixTsToStr,
"duration": DurToStr,
},
}
for k, v := range vars {
ctx.Variables[k] = cty.ObjectVal(v)
}
return ctx
}
// TextBlockToStr takes in a textblock and return the
// data with args filled in
func TextBlockToStr(texts []TextBlock) function.Function {
return function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "name",
Type: cty.String,
},
},
VarParam: &function.Parameter{
Type: cty.String,
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
var argVals = make(map[string]cty.Value)
for i, argVal := range args {
argVals[fmt.Sprintf("%d", i)] = argVal
}
ctx := &hcl.EvalContext{
Variables: map[string]cty.Value{
"arg": cty.ObjectVal(argVals),
},
}
for _, txt := range texts {
if txt.Name == args[0].AsString() {
val, dia := txt.Data.Expr.Value(ctx)
var err error
if dia.HasErrors() {
err = fmt.Errorf("text block: %v", dia)
}
switch val.Type() {
case cty.Number:
i, _ := val.AsBigFloat().Int64()
val = cty.StringVal(fmt.Sprintf("%d", i))
case cty.String:
return val, err
default:
b, err := ctyjson.SimpleJSONValue{Value: val}.MarshalJSON()
return cty.StringVal(string(b)), err
}
}
}
return cty.StringVal(""), nil
},
})
}
// DurToStr takes a tiem duration and returns a
// unix timestamp of the duration from time.Now()
var DurToStr = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "duration",
Type: cty.String,
},
},
Type: function.StaticReturnType(cty.Number),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
dur, err := time.ParseDuration(args[0].AsString())
if err != nil {
return cty.NumberIntVal(0), ErrParseDuration.F(err)
}
return cty.NumberIntVal(time.Now().Add(dur).Unix()), nil
},
})
// fileToStrOpen allows the function to be overridden so we
// can add our own filesystem to open files, use the name, key
// to allow parallel tests based on those values.
var fileToStrOpen = func(_, _, filepath string) (io.ReadCloser, error) { return os.Open(filepath) }
// FileToStr takes in a name, key (used during testing) and returns a HCL
// function that will return the contents of a file as a string
func FileToStr(name string, key string) function.Function {
return function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "filename",
Type: cty.String,
AllowDynamicType: true,
AllowMarked: true,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
// trim leading dots and slashes so we can't do some bad things.
filepath := filepath.Join(_runtimePath, strings.TrimLeft(args[0].AsString(), `.`+string(filepath.Separator)))
f, err := fileToStrOpen(name, key, filepath)
if err != nil {
log.Fatal("expr file open:", err)
}
defer f.Close()
b, err := ioutil.ReadAll(f)
if err != nil {
log.Fatal("expr file read:", err)
}
return cty.StringVal(string(bytes.TrimSpace(b))), nil
},
})
}
// NowToStr returns the current time as a int64 unix time
var NowToStr = function.New(&function.Spec{
Params: []function.Parameter{},
Type: function.StaticReturnType(cty.Number),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
return cty.NumberIntVal(time.Now().Unix()), nil
},
})
// UnixTsToStr takes in a RFC822 formatted string and
// returns the unix timestamp as a int64 number
//
// RFC822: Sat, 20 Feb 2021 00:00:00 UTC
var UnixTsToStr = function.New(&function.Spec{
Params: []function.Parameter{
{
Name: "unix",
Type: cty.String,
AllowDynamicType: true,
AllowMarked: true,
},
},
Type: function.StaticReturnType(cty.String),
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
a, err := time.Parse(time.RFC822, args[0].AsString())
if err != nil {
return cty.StringVal("0"), ErrParseTimeFmt.F(err)
}
return cty.StringVal(fmt.Sprintf("%d", a.Unix())), nil
},
})