diff --git a/src/terraform/structure/terraform_parser.go b/src/terraform/structure/terraform_parser.go index fc9c3d63..5d2c1e6c 100644 --- a/src/terraform/structure/terraform_parser.go +++ b/src/terraform/structure/terraform_parser.go @@ -3,7 +3,6 @@ package structure import ( "encoding/json" "fmt" - "github.com/genelet/determined/dethcl" "os" "path/filepath" "reflect" @@ -16,6 +15,7 @@ import ( "github.com/bridgecrewio/yor/src/common/structure" "github.com/bridgecrewio/yor/src/common/tagging/tags" "github.com/bridgecrewio/yor/src/common/utils" + "github.com/genelet/determined/dethcl" "github.com/hashicorp/go-hclog" "github.com/hashicorp/hcl/v2" "github.com/hashicorp/hcl/v2/hclsyntax" @@ -305,7 +305,7 @@ func (p *TerraformParser) modifyBlockTags(rawBlock *hclwrite.Block, parsedBlock isMergeOpExists := false isForOpExists := false isRenderedAttribute := false - existingParsedTags, _ := p.parseTagAttribute(rawTagsTokens) + existingParsedTags := p.parseTagAttribute(rawTagsTokens) for i, rawTagsToken := range rawTagsTokens { tokenStr := string(rawTagsToken.Bytes) if tokenStr == "merge" { @@ -681,7 +681,7 @@ func (p *TerraformParser) getExistingTags(hclBlock *hclwrite.Block, tagsAttribut // if tags exists in resource isTaggable, _ = p.isBlockTaggable(hclBlock) tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{}) - parsedTags, _ := p.parseTagAttribute(tagsTokens) + parsedTags := p.parseTagAttribute(tagsTokens) for key := range parsedTags { iTag := tags.Init(key, parsedTags[key]) existingTags = append(existingTags, iTag) @@ -815,7 +815,7 @@ func getUncloseBracketsCount(bracketsCounters map[hclsyntax.TokenType]int) int { return sum } -func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) (map[string]string, error) { +func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) map[string]string { isForOpExists := false for _, rawTagsToken := range tokens { tokenStr := string(rawTagsToken.Bytes) @@ -828,21 +828,9 @@ func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) (map[string] hclData := new(Resource) hclBytes := tokens.Bytes() hclBytes = []byte(strings.Replace(string(hclBytes), "{", " tags= {", 1)) - - err := dethcl.Unmarshal([]byte(hclBytes), hclData) - if err != nil { - return nil, fmt.Errorf("failed to unmarshel data because %s", err) - } - - tempHclData, err := dethcl.Marshal(hclData) - if err != nil { - return nil, fmt.Errorf("failed to marshel data because %s", err) - - } - hclFile, diagnostics := hclwrite.ParseConfig(tempHclData, "", hcl.InitialPos) - if diagnostics != nil && diagnostics.HasErrors() { - return nil, fmt.Errorf("failed to convert to hclFile %s", diagnostics) - } + dethcl.Unmarshal((hclBytes), hclData) + tempHclData, _ := dethcl.Marshal(hclData) + hclFile, _ := hclwrite.ParseConfig(tempHclData, "", hcl.InitialPos) tagsAttribute := hclFile.Body().GetAttribute("tags") tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{}) tokens = tagsTokens @@ -871,7 +859,7 @@ func (p *TerraformParser) parseTagAttribute(tokens hclwrite.Tokens) (map[string] parsedTags[key] = value } - return parsedTags, nil + return parsedTags } func (p *TerraformParser) getClient(providerName string) tfschema.Client { @@ -925,7 +913,7 @@ func (p *TerraformParser) getModuleTags(hclBlock *hclwrite.Block, tagsAttributeN // if tags exists in module isTaggable = true tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{}) - parsedTags, _ := p.parseTagAttribute(tagsTokens) + parsedTags := p.parseTagAttribute(tagsTokens) for key := range parsedTags { iTag := tags.Init(key, parsedTags[key]) existingTags = append(existingTags, iTag) diff --git a/src/terraform/structure/terraform_parser_test.go b/src/terraform/structure/terraform_parser_test.go index 3184c29e..6d6ccbd4 100644 --- a/src/terraform/structure/terraform_parser_test.go +++ b/src/terraform/structure/terraform_parser_test.go @@ -49,6 +49,19 @@ func TestTerraformParser_SkipResourceByComment(t *testing.T) { } assert.Empty(t, p.GetSkipResourcesByComment()) }) + t.Run("One resource with skip comment, only that resource added to skipResourcesByComment slice", func(t *testing.T) { + // Initialize TerraformParser and parse file with one resource containing skip tag + p := &TerraformParser{} + p.Init("../../../tests/terraform/skipComment/", nil) + defer p.Close() + filePath := "../../../tests/terraform/skipComment/skipOne.tf" + _, err := p.ParseFile(filePath) + if err != nil { + t.Errorf("failed to read hcl file because %s", err) + } + exceptedSkipResources := []string{"aws_instance.example_instance"} + assert.Equal(t, exceptedSkipResources, p.GetSkipResourcesByComment()) + }) } @@ -68,7 +81,7 @@ func TestParseTagAttribute(t *testing.T) { tagsAttribute := hclBlock.Body().GetAttribute(tagsAttributeName) if tagsAttribute != nil { tagsTokens := tagsAttribute.Expr().BuildTokens(hclwrite.Tokens{}) - parsedTags, _ := parser.parseTagAttribute(tagsTokens) + parsedTags := parser.parseTagAttribute(tagsTokens) if block.GetResourceName() == "bucket_var_tags" { assert.Equal(t, parsedTags, expectedTags) }