Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/cloudformation/structure/cloudformation_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ func (p *CloudformationParser) ValidFile(filePath string) bool {
}

func goformationParse(file string) (*cloudformation.Template, error) {


var template *cloudformation.Template
var err error
defer func() {
Expand All @@ -112,7 +114,8 @@ func goformationParse(file string) (*cloudformation.Template, error) {
return template, err
}

func (p *CloudformationParser) ParseFile(filePath string) ([]structure.IBlock, error) {
func (p *CloudformationParser) ParseFile(filePath string) ([]structure.IBlock,[]string, error) {
skipArr:=make([]string,0)
goformationLock.Lock()
template, err := goformationParse(filePath)
goformationLock.Unlock()
Expand All @@ -121,12 +124,12 @@ func (p *CloudformationParser) ParseFile(filePath string) ([]structure.IBlock, e
if err == nil {
err = fmt.Errorf("failed to parse template %v", filePath)
}
return nil, err
return nil,nil,err
}

if template.Transform != nil {
logger.Info(fmt.Sprintf("Skipping CFN template %s as SAM templates are not yet supported", filePath))
return nil, nil
return nil,skipArr, nil
}

resourceNames := make([]string, 0)
Expand All @@ -138,13 +141,13 @@ func (p *CloudformationParser) ParseFile(filePath string) ([]structure.IBlock, e
var resourceNamesToLines map[string]*structure.Lines
switch utils.GetFileFormat(filePath) {
case common.YmlFileType.FileFormat, common.YamlFileType.FileFormat:
resourceNamesToLines = yaml.MapResourcesLineYAML(filePath, resourceNames, ResourcesStartToken)
resourceNamesToLines,skipArr = yaml.MapResourcesLineYAML(filePath, resourceNames, ResourcesStartToken)
case common.JSONFileType.FileFormat:
var fileBracketsMapping map[int]json.BracketPair
resourceNamesToLines, fileBracketsMapping = json.MapResourcesLineJSON(filePath, resourceNames)
p.FileToBracketMapping.Store(filePath, fileBracketsMapping)
default:
return nil, fmt.Errorf("unsupported file type %s", utils.GetFileFormat(filePath))
return nil, nil,fmt.Errorf("unsupported file type %s", utils.GetFileFormat(filePath))
}

minResourceLine := math.MaxInt8
Expand Down Expand Up @@ -181,9 +184,9 @@ func (p *CloudformationParser) ParseFile(filePath string) ([]structure.IBlock, e

p.FileToResourcesLines.Store(filePath, structure.Lines{Start: minResourceLine, End: maxResourceLine})

return parsedBlocks, nil
return parsedBlocks,skipArr, nil
}
return nil, err
return nil,skipArr, err
}

func (p *CloudformationParser) extractTagsAndLines(filePath string, lines *structure.Lines, tagsValue reflect.Value) (structure.Lines, []tags.ITag) {
Expand Down Expand Up @@ -237,7 +240,7 @@ func (p *CloudformationParser) WriteFile(readFilePath string, blocks []structure
return err
}

_, err = p.ParseFile(tempFile.Name())
_, _,err = p.ParseFile(tempFile.Name())
if err != nil {
return fmt.Errorf("editing file %v resulted in a malformed template, please open a github issue with the relevant details", readFilePath)
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ type IParser interface {
Init(rootDir string, args map[string]string)
Name() string
ValidFile(filePath string) bool
ParseFile(filePath string) ([]structure.IBlock, error)
ParseFile(filePath string) ([]structure.IBlock,[]string, error)
WriteFile(readFilePath string, blocks []structure.IBlock, writeFilePath string) error
GetSkippedDirs() []string
GetSupportedFileExtensions() []string
Expand Down
13 changes: 8 additions & 5 deletions src/common/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ import (
"github.com/bridgecrewio/yor/src/common/tagging/tags"
taggingUtils "github.com/bridgecrewio/yor/src/common/tagging/utils"
"github.com/bridgecrewio/yor/src/common/utils"
slsStructure "github.com/bridgecrewio/yor/src/serverless/structure"
tfStructure "github.com/bridgecrewio/yor/src/terraform/structure"
// slsStructure "github.com/bridgecrewio/yor/src/serverless/structure"
// tfStructure "github.com/bridgecrewio/yor/src/terraform/structure"
)

type Runner struct {
Expand Down Expand Up @@ -72,11 +72,11 @@ func (r *Runner) Init(commands *clioptions.TagOptions) error {
}
switch p {
case "Terraform":
r.parsers = append(r.parsers, &tfStructure.TerraformParser{})
// r.parsers = append(r.parsers, &tfStructure.TerraformParser{})
case "CloudFormation":
r.parsers = append(r.parsers, &cfnStructure.CloudformationParser{})
case "Serverless":
r.parsers = append(r.parsers, &slsStructure.ServerlessParser{})
// r.parsers = append(r.parsers, &slsStructure.ServerlessParser{})
default:
logger.Warning(fmt.Sprintf("ignoring unknown parser %#v", err))
}
Expand Down Expand Up @@ -176,11 +176,14 @@ func (r *Runner) TagFile(file string) {
continue
}
logger.Info(fmt.Sprintf("Tagging %v\n", file))
blocks, err := parser.ParseFile(file)
blocks,skipArr, err := parser.ParseFile(file)
if err != nil {
logger.Info(fmt.Sprintf("Failed to parse file %v with parser %v", file, reflect.TypeOf(parser)))
continue
}
for _,value :=range skipArr{
r.skippedResources = append(r.skippedResources, value)
}
isFileTaggable := false
for _, block := range blocks {
if r.isSkippedResourceType(block.GetResourceType()) {
Expand Down
13 changes: 9 additions & 4 deletions src/common/yaml/yaml_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,9 @@ func FindTagsLinesYAML(textLines []string, tagsAttributeName string) (structure.
return tagsLines, tagsExist
}

func MapResourcesLineYAML(filePath string, resourceNames []string, resourcesStartToken string) map[string]*structure.Lines {
func MapResourcesLineYAML(filePath string, resourceNames []string, resourcesStartToken string) (map[string]*structure.Lines,[] string) {
resourceToLines := make(map[string]*structure.Lines)
skipResource:=make([]string,0)
for _, resourceName := range resourceNames {
// initialize a map between resource name and its lines in file
resourceToLines[resourceName] = &structure.Lines{Start: -1, End: -1}
Expand All @@ -276,7 +277,7 @@ func MapResourcesLineYAML(filePath string, resourceNames []string, resourcesStar
file, err := os.ReadFile(filePath)
if err != nil {
logger.Warning(fmt.Sprintf("failed to read file %s", filePath))
return nil
return nil,skipResource
}

readResources := false
Expand All @@ -291,8 +292,12 @@ func MapResourcesLineYAML(filePath string, resourceNames []string, resourcesStar
resourcesIndent = countLeadingSpaces(line)
continue
}

if readResources {
if i>0{
if strings.TrimSpace(fileLines[i-1])=="# yor:skip" {
skipResource = append(skipResource,strings.Trim(strings.TrimSpace(line),":") )
}
}
lineIndent := countLeadingSpaces(line)
if lineIndent <= resourcesIndent && strings.TrimSpace(line) != "" && !strings.Contains(line, "#") {
// No longer inside resources block, get the last line of the previous resource if exists
Expand Down Expand Up @@ -325,7 +330,7 @@ func MapResourcesLineYAML(filePath string, resourceNames []string, resourcesStar
// Handle last line of resource is last line of file
resourceToLines[latestResourceName].End = findLastNonEmptyLine(fileLines, len(fileLines)-1)
}
return resourceToLines
return resourceToLines ,skipResource
}

func countLeadingSpaces(line string) int {
Expand Down