From 7e1b0850f1986b9841900549a18f2cfa2eb617d4 Mon Sep 17 00:00:00 2001 From: MIRIAM-SLOV Date: Tue, 16 Jul 2024 15:08:02 +0300 Subject: [PATCH 1/2] fix tags with tag-prefix --- src/common/tagging/tag_group.go | 2 +- tests/integration/integration_test.go | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/common/tagging/tag_group.go b/src/common/tagging/tag_group.go index bc9a67cf..3509e377 100644 --- a/src/common/tagging/tag_group.go +++ b/src/common/tagging/tag_group.go @@ -49,7 +49,7 @@ func (t *TagGroup) SetTags(tags []tags.ITag) { for _, tag := range tags { tag.Init() tag.SetTagPrefix(t.Options.TagPrefix) - if !t.IsTagSkipped(tag) && (t.SpecifiedTags == nil || len(t.SpecifiedTags) == 0 || utils.InSlice(t.SpecifiedTags, tag.GetKey())) { + if !t.IsTagSkipped(tag) && (t.SpecifiedTags == nil || len(t.SpecifiedTags) == 0 || utils.InSlice(t.SpecifiedTags, strings.TrimPrefix(tag.GetKey(), t.Options.TagPrefix))) { t.tags = append(t.tags, tag) } } diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 41f6d5b8..359eb386 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -201,7 +201,7 @@ func TestRunResults(t *testing.T) { } }) - t.Run("Test terraform-aws-bridgecrew-read-only tagging specified tags", func(t *testing.T) { + t.Run("Test terraform-aws-bridgecrew-read-only tagging specified tags with tag-prefix", func(t *testing.T) { repoPath := utils.CloneRepo("https://github.com/bridgecrewio/terraform-aws-bridgecrew-read-only.git", "a8686215642fd47a38bf8615d91d0d40630ab989") defer os.RemoveAll(repoPath) @@ -210,6 +210,7 @@ func TestRunResults(t *testing.T) { Directory: repoPath, TagGroups: getTagGroups(), Tag: []string{"yor_trace"}, + TagPrefix: "prefix_", Parsers: []string{"Terraform"}, }) failIfErr(t, err) @@ -220,10 +221,11 @@ func TestRunResults(t *testing.T) { report := reportService.GetReport() assert.LessOrEqual(t, 18, report.Summary.Scanned) assert.Greater(t, report.Summary.Scanned, 0) + assert.NotEqual(t, 0, len(report.NewResourceTags)) for _, newTag := range report.NewResourceTags { - if strings.HasPrefix(repoPath, newTag.File) { - assert.Equal(t, "yor_trace", newTag.TagKey) + if strings.HasPrefix(newTag.File, repoPath) { + assert.Equal(t, "prefix_yor_trace", newTag.TagKey) assert.Equal(t, "aws_iam_role.bridgecrew_account_role", newTag.ResourceID) } } From d838efa7bdc3878eeb33fbc2e8fb11c7b097e4a2 Mon Sep 17 00:00:00 2001 From: MIRIAM-SLOV Date: Tue, 16 Jul 2024 18:23:08 +0300 Subject: [PATCH 2/2] new UT --- src/common/tagging/tag_group_test.go | 16 ++++++++++++++++ tests/integration/integration_test.go | 8 +++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/common/tagging/tag_group_test.go b/src/common/tagging/tag_group_test.go index f2723aa3..c8f6f486 100644 --- a/src/common/tagging/tag_group_test.go +++ b/src/common/tagging/tag_group_test.go @@ -52,4 +52,20 @@ func TestTagGroup(t *testing.T) { tgs := tagGroup.GetTags() assert.Equal(t, 0, len(tgs)) }) + + t.Run("Test tag prefix not broke tags", func(t *testing.T) { + tagGroup := TagGroup{Options: InitTagGroupOptions{ + TagPrefix: "prefix_", + }, + SpecifiedTags: []string{"yor_trace"}, + } + tagGroup.SetTags([]tags.ITag{ + &tags.Tag{Key: "yor_trace"}, + &tags.Tag{Key: "git_modifiers"}, + }) + tgs := tagGroup.GetTags() + assert.Equal(t, 1, len(tgs)) + assert.Equal(t, string("prefix_yor_trace"), tgs[0].GetKey()) + + }) } diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 359eb386..41f6d5b8 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -201,7 +201,7 @@ func TestRunResults(t *testing.T) { } }) - t.Run("Test terraform-aws-bridgecrew-read-only tagging specified tags with tag-prefix", func(t *testing.T) { + t.Run("Test terraform-aws-bridgecrew-read-only tagging specified tags", func(t *testing.T) { repoPath := utils.CloneRepo("https://github.com/bridgecrewio/terraform-aws-bridgecrew-read-only.git", "a8686215642fd47a38bf8615d91d0d40630ab989") defer os.RemoveAll(repoPath) @@ -210,7 +210,6 @@ func TestRunResults(t *testing.T) { Directory: repoPath, TagGroups: getTagGroups(), Tag: []string{"yor_trace"}, - TagPrefix: "prefix_", Parsers: []string{"Terraform"}, }) failIfErr(t, err) @@ -221,11 +220,10 @@ func TestRunResults(t *testing.T) { report := reportService.GetReport() assert.LessOrEqual(t, 18, report.Summary.Scanned) assert.Greater(t, report.Summary.Scanned, 0) - assert.NotEqual(t, 0, len(report.NewResourceTags)) for _, newTag := range report.NewResourceTags { - if strings.HasPrefix(newTag.File, repoPath) { - assert.Equal(t, "prefix_yor_trace", newTag.TagKey) + if strings.HasPrefix(repoPath, newTag.File) { + assert.Equal(t, "yor_trace", newTag.TagKey) assert.Equal(t, "aws_iam_role.bridgecrew_account_role", newTag.ResourceID) } }