Skip to content

Commit

Permalink
Create yor_name tag (#367)
Browse files Browse the repository at this point in the history
* added function to sls block type

* created the yor_name tag

* fixed test

* added cfngoat.yaml

* added sls file

* fixed sls file

* added tf file

* added tests and adjusted integration test

* create new instance of 	ChangeAccumulator

* moved all files to single dir

* one test

* final test fix
  • Loading branch information
rotemavni authored May 18, 2023
1 parent 146b043 commit 165347c
Show file tree
Hide file tree
Showing 11 changed files with 1,555 additions and 5 deletions.
34 changes: 34 additions & 0 deletions src/common/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package runner

import (
"fmt"
"github.com/bridgecrewio/yor/src/common/tagging/tags"
"os"
"path/filepath"
"strings"
Expand Down Expand Up @@ -274,3 +275,36 @@ func initMockGitTagGroup(rootDir string, filesToBlames map[string]string) *gitta
gitTagGroup.GitService = gitService
return &gitTagGroup
}

func Test_YorNameTag(t *testing.T) {
t.Run("tag code2cloud", func(t *testing.T) {
options := clioptions.TagOptions{
Directory: "../../../tests/resource_name",
TagGroups: []string{string(taggingUtils.Code2Cloud)},
Parsers: []string{"Terraform", "CloudFormation", "Serverless"},
}

runner := Runner{}
err := runner.Init(&options)
reportService, err := runner.TagDirectory()
if err != nil {
t.Error(err)
}
reportService.CreateReport()
report := reportService.GetReport()

yorNameCounter := 0
for _, newTag := range report.NewResourceTags {
if newTag.TagKey == tags.YorNameTagKey {
yorNameCounter += 1
resourceIdParts := strings.Split(newTag.ResourceID, ".")
resourceName := resourceIdParts[0]
if len(resourceIdParts) > 1 {
resourceName = resourceIdParts[1]
}
assert.Equal(t, resourceName, newTag.UpdatedValue)
}
}
assert.True(t, yorNameCounter > 0)
})
}
5 changes: 5 additions & 0 deletions src/common/structure/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ type IBlock interface {
GetTagsAttributeName() string
IsGCPBlock() bool
GetResourceType() string
GetResourceName() string
}

type Block struct {
Expand Down Expand Up @@ -211,3 +212,7 @@ func (b *Block) GetTagsAttributeName() string {
func (b *Block) IsGCPBlock() bool {
return false
}

func (b *Block) GetResourceName() string {
return b.GetResourceID()
}
24 changes: 22 additions & 2 deletions src/common/tagging/code2cloud/tag_group.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package code2cloud

import (
"fmt"
"github.com/bridgecrewio/yor/src/common/logger"
"github.com/bridgecrewio/yor/src/common/structure"
"github.com/bridgecrewio/yor/src/common/tagging"
"github.com/bridgecrewio/yor/src/common/tagging/tags"
Expand All @@ -16,15 +18,33 @@ func (t *TagGroup) InitTagGroup(_ string, skippedTags []string, explicitlySpecif
}
t.SkippedTags = skippedTags
t.SpecifiedTags = explicitlySpecifiedTags
t.SetTags([]tags.ITag{&YorTraceTag{}})
t.SetTags([]tags.ITag{&YorTraceTag{}, &YorNameTag{}})
}

func (t *TagGroup) GetDefaultTags() []tags.ITag {
return []tags.ITag{
&YorTraceTag{},
&YorNameTag{},
}
}

func (t *TagGroup) CreateTagsForBlock(block structure.IBlock) error {
return t.UpdateBlockTags(block, struct{}{})
return t.UpdateBlockTags(block)
}

func (t *TagGroup) UpdateBlockTags(block structure.IBlock) error {
var newTags []tags.ITag
var err error
var tagVal tags.ITag
for _, tag := range t.GetTags() {
tagVal, err = tag.CalculateValue(block)
if err != nil {
logger.Error(fmt.Sprintf("Failed to create %v tag for block %v", tag.GetKey(), block.GetResourceID()))
}
if tagVal != nil && tagVal.GetValue() != "" {
newTags = append(newTags, tagVal)
}
}
block.AddNewTags(newTags)
return err
}
14 changes: 13 additions & 1 deletion src/common/tagging/code2cloud/tag_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package code2cloud

import (
"regexp"
"strings"
"testing"

"github.com/bridgecrewio/yor/src/common/tagging"
Expand Down Expand Up @@ -66,15 +67,26 @@ func TestCode2CloudTagGroup(t *testing.T) {
tagGroup := TagGroup{}
tagGroup.InitTagGroup("", nil, nil, tagging.WithTagPrefix("prefix"))

blockName := "my_bucket"

block := &MockTestBlock{
Block: structure.Block{
FilePath: path,
IsTaggable: true,
Name: blockName,
},
}

_ = tagGroup.CreateTagsForBlock(block)
assert.Equal(t, 1, len(block.NewTags))
assert.Equal(t, 2, len(block.NewTags))
foundYorName := false
for _, newTag := range block.NewTags {
if strings.Contains(newTag.GetKey(), tags.YorNameTagKey) {
foundYorName = true
assert.Equal(t, blockName, newTag.GetValue())
}
}
assert.True(t, foundYorName)
})
}

Expand Down
29 changes: 29 additions & 0 deletions src/common/tagging/code2cloud/yor_name.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package code2cloud

import (
"fmt"
"github.com/bridgecrewio/yor/src/common/structure"
"github.com/bridgecrewio/yor/src/common/tagging/tags"
"reflect"
)

type YorNameTag struct {
tags.Tag
}

func (t *YorNameTag) Init() {
t.Key = tags.YorNameTagKey
}

func (t *YorNameTag) CalculateValue(block interface{}) (tags.ITag, error) {
blockVal, ok := block.(structure.IBlock)
if !ok {
return nil, fmt.Errorf("failed to convert data to IBlock, which is required to calculte tag value. Type of data: %s", reflect.TypeOf(block))
}

return &tags.Tag{Key: t.Key, Value: blockVal.GetResourceName()}, nil
}

func (t *YorNameTag) GetDescription() string {
return "A tag that states the resource name in the IaC config file"
}
1 change: 1 addition & 0 deletions src/common/tagging/tags/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const GitModifiersTagKey = "git_modifiers"
const GitLastModifiedAtTagKey = "git_last_modified_at"
const GitLastModifiedByTagKey = "git_last_modified_by"
const GitRepoTagKey = "git_repo"
const YorNameTagKey = "yor_name"

type ITag interface {
Init()
Expand Down
6 changes: 6 additions & 0 deletions src/terraform/structure/terraform_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,9 @@ func (b *TerraformBlock) GetSeparator() string {
func (b *TerraformBlock) IsGCPBlock() bool {
return strings.HasPrefix(b.GetResourceID(), "google_") || b.GetTagsAttributeName() == ProviderToTagAttribute["google"]
}

func (b *TerraformBlock) GetResourceName() string {
resourceId := b.GetResourceID()
resourceType := b.GetResourceType()
return strings.ReplaceAll(resourceId, strings.Join([]string{resourceType, ""}, "."), "")
}
4 changes: 2 additions & 2 deletions tests/integration/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ func TestTagUncommittedResults(t *testing.T) {
rawTags := defaultInstanceBlock.HclSyntaxBlock.Body.Attributes["tags"]
rawTagsExpr := rawTags.Expr.(*hclsyntax.ObjectConsExpr)
assert.Equal(t, "tags", rawTags.Name)
assert.Equal(t, 10, len(rawTagsExpr.Items))
assert.Equal(t, 11, len(rawTagsExpr.Items))

currentTags := defaultInstanceBlock.ExitingTags

Expand Down Expand Up @@ -352,7 +352,7 @@ func TestTagUncommittedResults(t *testing.T) {
rawTags := defaultInstanceBlock.HclSyntaxBlock.Body.Attributes["tags"]
rawTagsExpr := rawTags.Expr.(*hclsyntax.ObjectConsExpr)
assert.Equal(t, "tags", rawTags.Name)
assert.Equal(t, 10, len(rawTagsExpr.Items))
assert.Equal(t, 11, len(rawTagsExpr.Items))

currentTags := defaultInstanceBlock.ExitingTags

Expand Down
Loading

0 comments on commit 165347c

Please sign in to comment.