Skip to content

Commit

Permalink
fix input CFT templates based on goformation structs (#1105)
Browse files Browse the repository at this point in the history
* sanitize cft template based on goformation structs
  • Loading branch information
patilpankaj212 authored Jan 5, 2022
1 parent c8344ba commit 83afdb7
Show file tree
Hide file tree
Showing 6 changed files with 2,606 additions and 5 deletions.
20 changes: 16 additions & 4 deletions pkg/iac-providers/cft/v1/load-file.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,16 +93,28 @@ func (a *CFTV1) extractTemplate(file string, data *[]byte) (*cloudformation.Temp

switch fileExt {
case YAMLExtension, YAMLExtension2:
template, err := goformation.ParseYAML(*data)
zap.S().Debug("sanitizing cft template file", zap.String("file", file))
sanitized, err := a.sanitizeCftTemplate(*data, true)
if err != nil {
zap.S().Debug("failed to parse file", zap.String("file", file))
zap.S().Debug("failed to sanitize cft template file", zap.String("file", file), zap.Error(err))
return nil, err
}
template, err := goformation.ParseYAML(sanitized)
if err != nil {
zap.S().Debug("failed to parse file", zap.String("file", file), zap.Error(err))
return nil, err
}
return template, nil
case JSONExtension:
template, err := goformation.ParseJSON(*data)
zap.S().Debug("sanitizing cft template file", zap.String("file", file))
sanitized, err := a.sanitizeCftTemplate(*data, false)
if err != nil {
zap.S().Debug("failed to sanitize cft template file", zap.String("file", file), zap.Error(err))
return nil, err
}
template, err := goformation.ParseJSON(sanitized)
if err != nil {
zap.S().Debug("failed to parse file", zap.String("file", file))
zap.S().Debug("failed to parse file", zap.String("file", file), zap.Error(err))
return nil, err
}
return template, nil
Expand Down
2 changes: 1 addition & 1 deletion pkg/iac-providers/cft/v1/load-file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func TestLoadIacFile(t *testing.T) {
filePath: "nonexistent.txt",
typeOnly: false,
}, {
wantErr: fmt.Errorf(testErrString3),
wantErr: fmt.Errorf("error while resolving intrinsic functions, error %w", fmt.Errorf(testErrString3)),
want: output.AllResourceConfigs{},
cftv1: CFTV1{},
name: "invalid file",
Expand Down
Loading

0 comments on commit 83afdb7

Please sign in to comment.