-
Notifications
You must be signed in to change notification settings - Fork 524
/
Copy pathVariableTemplateParser.go
462 lines (421 loc) · 16.6 KB
/
VariableTemplateParser.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
/*
* Copyright (c) 2024. Devtron Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package parsers
import (
"encoding/json"
"errors"
"fmt"
"github.com/caarlos0/env"
"github.com/devtron-labs/devtron/pkg/variables/utils"
"github.com/hashicorp/hcl2/hcl"
"github.com/hashicorp/hcl2/hcl/hclsyntax"
_ "github.com/hashicorp/hcl2/hcl/hclsyntax"
"github.com/zclconf/go-cty/cty"
"github.com/zclconf/go-cty/cty/function"
"github.com/zclconf/go-cty/cty/function/stdlib"
ctyJson "github.com/zclconf/go-cty/cty/json"
"go.uber.org/zap"
"regexp"
"strconv"
"strings"
)
type VariableTemplateParser interface {
ExtractVariables(template string, templateType VariableTemplateType) ([]string, error)
//ParseTemplate(template string, values map[string]string) string
ParseTemplate(parserRequest VariableParserRequest) VariableParserResponse
}
type VariableTemplateParserImpl struct {
logger *zap.SugaredLogger
variableTemplateParserConfig *VariableTemplateParserConfig
}
func NewVariableTemplateParserImpl(logger *zap.SugaredLogger) (*VariableTemplateParserImpl, error) {
impl := &VariableTemplateParserImpl{logger: logger}
cfg, err := getVariableTemplateParserConfig()
if err != nil {
return nil, err
}
impl.variableTemplateParserConfig = cfg
return impl, nil
}
type VariableTemplateParserConfig struct {
ScopedVariableEnabled bool `env:"SCOPED_VARIABLE_ENABLED" envDefault:"false"`
ScopedVariableHandlePrimitives bool `env:"SCOPED_VARIABLE_HANDLE_PRIMITIVES" envDefault:"false"`
VariableExpressionRegex string `env:"VARIABLE_EXPRESSION_REGEX" envDefault:"@{{([^}]+)}}"`
}
func (cfg VariableTemplateParserConfig) isScopedVariablesDisabled() bool {
return !cfg.ScopedVariableEnabled
}
func getVariableTemplateParserConfig() (*VariableTemplateParserConfig, error) {
cfg := &VariableTemplateParserConfig{}
err := env.Parse(cfg)
return cfg, err
}
func getRegexSubMatches(regex string, input string) [][]string {
re := regexp.MustCompile(regex)
matches := re.FindAllStringSubmatch(input, -1)
return matches
}
const quote = "\""
const escapedQuote = `\\"`
func (impl *VariableTemplateParserImpl) preProcessPlaceholder(template string, variableValueMap map[string]interface{}) string {
variableSubRegexWithQuotes := quote + impl.variableTemplateParserConfig.VariableExpressionRegex + quote
variableSubRegexWithEscapedQuotes := escapedQuote + impl.variableTemplateParserConfig.VariableExpressionRegex + escapedQuote
matches := getRegexSubMatches(variableSubRegexWithQuotes, template)
matches = append(matches, getRegexSubMatches(variableSubRegexWithEscapedQuotes, template)...)
// Replace the surrounding quotes for variables whose value is known
// and type is primitive
for _, match := range matches {
if len(match) == 2 {
originalMatch := match[0]
innerContent := match[1]
if val, ok := variableValueMap[innerContent]; ok && utils.IsPrimitiveType(val) {
replacement := fmt.Sprintf("@{{%s}}", innerContent)
template = strings.Replace(template, originalMatch, replacement, 1)
}
}
}
return template
}
func (impl *VariableTemplateParserImpl) ParseTemplate(parserRequest VariableParserRequest) VariableParserResponse {
if impl.variableTemplateParserConfig.isScopedVariablesDisabled() {
return parserRequest.GetEmptyResponse()
}
request := parserRequest
if impl.handlePrimitivesForJson(parserRequest) {
variableToValue := parserRequest.GetOriginalValuesMap()
template := impl.preProcessPlaceholder(parserRequest.Template, variableToValue)
//overriding request to handle primitives in json request
request.TemplateType = StringVariableTemplate
request.Template = template
}
return impl.parseTemplate(request)
}
func (impl *VariableTemplateParserImpl) handlePrimitivesForJson(parserRequest VariableParserRequest) bool {
return impl.variableTemplateParserConfig.ScopedVariableHandlePrimitives && parserRequest.TemplateType == JsonVariableTemplate
}
func (impl *VariableTemplateParserImpl) ExtractVariables(template string, templateType VariableTemplateType) ([]string, error) {
var variables []string
if template == "" {
return variables, nil
}
if !impl.variableTemplateParserConfig.ScopedVariableEnabled {
return variables, nil
}
// preprocess existing template to comment
template, err := impl.convertToHclCompatible(templateType, template)
if err != nil {
return variables, err
}
hclExpression, diagnostics := hclsyntax.ParseExpression([]byte(template), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})
if diagnostics.HasErrors() {
impl.logger.Errorw("error occurred while extracting variables from template", "template", template, "error", diagnostics.Error())
return variables, errors.New(InvalidTemplate)
} else {
hclVariables := hclExpression.Variables()
variables = impl.extractVarNames(hclVariables)
}
impl.logger.Info("extracted variables from template", variables)
return variables, nil
}
func (impl *VariableTemplateParserImpl) extractVarNames(hclVariables []hcl.Traversal) []string {
var variables []string
for _, hclVariable := range hclVariables {
variables = append(variables, hclVariable.RootName())
}
return variables
}
func (impl *VariableTemplateParserImpl) parseTemplate(parserRequest VariableParserRequest) VariableParserResponse {
template := parserRequest.Template
response := VariableParserResponse{Request: parserRequest, ResolvedTemplate: template}
values := parserRequest.GetValuesMap()
templateType := parserRequest.TemplateType
template, err := impl.convertToHclCompatible(templateType, template)
if err != nil {
response.Error = err
return response
}
impl.logger.Debug("variable hcl template valueMap", values)
hclExpression, diagnostics := hclsyntax.ParseExpression([]byte(template), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})
containsError := impl.checkAndUpdateDiagnosticError(diagnostics, template, &response, InvalidTemplate)
if containsError {
return response
}
updatedHclExpression, template, containsError := impl.checkForDefaultedVariables(parserRequest, hclExpression.Variables(), template, &response)
if containsError {
return response
}
if updatedHclExpression != nil {
hclExpression = updatedHclExpression
}
hclVarValues := impl.getHclVarValues(values)
opValue, diagnostics := hclExpression.Value(&hcl.EvalContext{
Variables: hclVarValues,
Functions: impl.getDefaultMappedFunc(),
})
containsError = impl.checkAndUpdateDiagnosticError(diagnostics, template, &response, VariableParsingFailed)
if containsError {
return response
}
output, err := impl.extractResolvedTemplate(templateType, opValue)
if err != nil {
output = template
}
response.ResolvedTemplate = output
return response
}
func (impl *VariableTemplateParserImpl) checkForDefaultedVariables(parserRequest VariableParserRequest, variables []hcl.Traversal, template string, response *VariableParserResponse) (hclsyntax.Expression, string, bool) {
var hclExpression hclsyntax.Expression
var diagnostics hcl.Diagnostics
valuesMap := parserRequest.GetValuesMap()
defaultedVars := impl.getDefaultedVariables(variables, valuesMap)
ignoreDefaultedVariables := parserRequest.IgnoreUnknownVariables
if len(defaultedVars) > 0 {
if ignoreDefaultedVariables {
template = impl.ignoreDefaultedVars(template, defaultedVars)
hclExpression, diagnostics = hclsyntax.ParseExpression([]byte(template), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})
hasErrors := impl.checkAndUpdateDiagnosticError(diagnostics, template, response, InvalidTemplate)
if hasErrors {
return nil, template, true
}
} else {
impl.logger.Errorw("error occurred while parsing template, unknown variables found", "defaultedVars", defaultedVars)
response.Error = errors.New(UnknownVariableFound)
defaultsVarNames := impl.extractVarNames(defaultedVars)
response.DetailedError = fmt.Sprintf(UnknownVariableErrorMsg, strings.Join(defaultsVarNames, ","))
return nil, template, true
}
}
return hclExpression, template, false
}
func (impl *VariableTemplateParserImpl) extractResolvedTemplate(templateType VariableTemplateType, opValue cty.Value) (string, error) {
var output string
if templateType == StringVariableTemplate {
opValueMap := opValue.AsValueMap()
rootValue := opValueMap["root"]
output = rootValue.AsString()
} else {
simpleJSONValue := ctyJson.SimpleJSONValue{Value: opValue}
marshalJSON, err := simpleJSONValue.MarshalJSON()
if err == nil {
output = string(marshalJSON)
} else {
impl.logger.Errorw("error occurred while marshalling json value of parsed template", "err", err)
return "", err
}
}
return output, nil
}
func (impl *VariableTemplateParserImpl) checkAndUpdateDiagnosticError(diagnostics hcl.Diagnostics, template string, response *VariableParserResponse, errMsg string) bool {
if !diagnostics.HasErrors() {
return false
}
detailedError := diagnostics.Error()
impl.logger.Errorw("error occurred while extracting variables from template", "template", template, "error", detailedError)
response.Error = errors.New(errMsg)
response.DetailedError = detailedError
return true
}
//func (impl *VariableTemplateParserImpl) ParseTemplate(template string, values map[string]string) string {
// //TODO KB: in case of yaml, need to convert it into JSON structure
// output := template
// template, _ = impl.convertToHclCompatible(JsonVariableTemplate, template)
// ignoreDefaultedVariables := true
// impl.logger.Debug("variable hcl template valueMap", values)
// //TODO KB: need to check for variables whose value is not present in values map, throw error or ignore variable
// hclExpression, diagnostics := hclsyntax.ParseExpression([]byte(template), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})
// if !diagnostics.HasErrors() {
// hclVariables := hclExpression.Variables()
// //variables := impl.extractVarNames(hclVariables)
// hclVarValues := impl.getHclVarValues(values)
// defaultedVars := impl.getDefaultedVariables(hclVariables, values)
// if len(defaultedVars) > 0 && ignoreDefaultedVariables {
// template = impl.ignoreDefaultedVars(template, defaultedVars)
// hclExpression, diagnostics = hclsyntax.ParseExpression([]byte(template), "", hcl.Pos{Line: 1, Column: 1, Byte: 0})
// if diagnostics.HasErrors() {
// //TODO KB: throw error with proper variable names creating problem
// }
// }
// opValue, valueDiagnostics := hclExpression.Value(&hcl.EvalContext{
// Variables: hclVarValues,
// Functions: impl.getDefaultMappedFunc(),
// })
// if !valueDiagnostics.HasErrors() {
// //opValueMap := opValue.AsValueMap()
// //rootValue := opValueMap["root"]
// //output = rootValue.AsString()
// simpleJSONValue := ctyJson.SimpleJSONValue{Value: opValue}
// marshalJSON, err := simpleJSONValue.MarshalJSON()
// if err == nil {
// output = string(marshalJSON)
// }
// }
// } else {
// //TODO KB: handle this case
// }
// return output
//}
func (impl *VariableTemplateParserImpl) getDefaultMappedFunc() map[string]function.Function {
return map[string]function.Function{
"upper": stdlib.UpperFunc,
"toInt": stdlib.IntFunc,
"toBool": ParseBoolFunc,
"split": stdlib.SplitFunc,
}
}
func (impl *VariableTemplateParserImpl) convertToHclCompatible(templateType VariableTemplateType, template string) (string, error) {
if templateType == StringVariableTemplate {
jsonStringify, err := json.Marshal(template)
if err != nil {
impl.logger.Errorw("error occurred while marshalling template, but continuing with the template", "err", err, "templateType", templateType)
//return "", errors.New(InvalidTemplate)
} else {
template = string(jsonStringify)
}
template = fmt.Sprintf(`{"root":%s}`, template)
}
template = impl.diluteExistingHclVars(template, "\\$", "$")
template = impl.diluteExistingHclVars(template, "%", "%")
return impl.convertToHclExpression(template), nil
}
func (impl *VariableTemplateParserImpl) diluteExistingHclVars(template string, templateControlKeyword string, replaceKeyword string) string {
hclVarRegex := regexp.MustCompile(templateControlKeyword + `\{`)
indexesData := hclVarRegex.FindAllIndex([]byte(template), -1)
var strBuilder strings.Builder
strBuilder.Grow(len(template))
currentIndex := 0
for _, datum := range indexesData {
startIndex := datum[0]
endIndex := datum[1]
strBuilder.WriteString(template[currentIndex:startIndex] + replaceKeyword + template[startIndex:endIndex])
currentIndex = endIndex
}
if currentIndex <= len(template) {
strBuilder.WriteString(template[currentIndex:])
}
output := strBuilder.String()
return output
}
func (impl *VariableTemplateParserImpl) convertToHclExpression(template string) string {
var devtronRegexCompiledPattern = regexp.MustCompile(impl.variableTemplateParserConfig.VariableExpressionRegex)
indexesData := devtronRegexCompiledPattern.FindAllIndex([]byte(template), -1)
var strBuilder strings.Builder
strBuilder.Grow(len(template))
currentIndex := 0
for _, datum := range indexesData {
startIndex := datum[0]
endIndex := datum[1]
strBuilder.WriteString(template[currentIndex:startIndex])
initQuoteAdded := false
if startIndex > 0 && template[startIndex-1] == '"' { // if quotes are already present then ignore
strBuilder.WriteString("$")
} else {
initQuoteAdded = true
strBuilder.WriteString("$")
//strBuilder.WriteString("\"$")
}
strBuilder.WriteString(template[startIndex+2 : endIndex-1])
if initQuoteAdded { // adding closing quote
//strBuilder.WriteString("\"")
}
currentIndex = endIndex
}
if currentIndex <= len(template) {
strBuilder.WriteString(template[currentIndex:])
}
output := strBuilder.String()
return output
}
func (impl *VariableTemplateParserImpl) getHclVarValues(values map[string]string) map[string]cty.Value {
variables := map[string]cty.Value{}
for varName, varValue := range values {
variables[varName] = cty.StringVal(varValue)
}
return variables
}
func (impl *VariableTemplateParserImpl) getDefaultedVariables(variables []hcl.Traversal, varValues map[string]string) []hcl.Traversal {
var defaultedVars []hcl.Traversal
for _, traversal := range variables {
if _, ok := varValues[traversal.RootName()]; !ok {
defaultedVars = append(defaultedVars, traversal)
}
}
return defaultedVars
}
func (impl *VariableTemplateParserImpl) ignoreDefaultedVars(template string, hclVars []hcl.Traversal) string {
var processedDtBuilder strings.Builder
impl.logger.Info("ignoring defaulted vars", "vars", hclVars)
maxSize := len(template) + len(hclVars)
processedDtBuilder.Grow(maxSize)
currentIndex := 0
for _, hclVar := range hclVars {
startIndex := hclVar.SourceRange().Start.Column
endIndex := hclVar.SourceRange().End.Column
startIndex = impl.getVarStartIndex(template, startIndex-1)
if startIndex == -1 {
continue
}
endIndex = impl.getVarEndIndex(template, endIndex-1)
if endIndex == -1 {
continue
}
processedDtBuilder.WriteString(template[currentIndex:startIndex] + "@{" + template[startIndex+1:endIndex] + "}")
currentIndex = endIndex
}
if currentIndex <= maxSize {
processedDtBuilder.WriteString(template[currentIndex:])
}
return processedDtBuilder.String()
}
func (impl *VariableTemplateParserImpl) getVarStartIndex(template string, startIndex int) int {
currentIndex := startIndex - 1
for ; currentIndex > 0; currentIndex-- {
//fmt.Println("value", string(template[currentIndex]))
if template[currentIndex] == '{' && template[currentIndex-1] == '$' {
return currentIndex - 1
}
}
return -1
}
func (impl *VariableTemplateParserImpl) getVarEndIndex(template string, endIndex int) int {
currentIndex := endIndex
for ; currentIndex < len(template); currentIndex++ {
//fmt.Println("value", string(template[currentIndex]))
if template[currentIndex] == '}' {
return currentIndex
}
}
return -1
}
var ParseBoolFunc = function.New(&function.Spec{
Description: `convert to bool value`,
Params: []function.Parameter{
{
Name: "val",
Type: cty.String,
AllowDynamicType: true,
AllowMarked: true,
},
},
Type: function.StaticReturnType(cty.Bool),
RefineResult: refineNonNull,
Impl: func(args []cty.Value, retType cty.Type) (cty.Value, error) {
boolVal, err := strconv.ParseBool(args[0].AsString())
return cty.BoolVal(boolVal), err
},
})
func refineNonNull(b *cty.RefinementBuilder) *cty.RefinementBuilder {
return b.NotNull()
}