Skip to content

Commit 8c6f577

Browse files
authored
Merge pull request #1501 from snyk/fix/driftignore
fix: driftignore that ignore type
2 parents 4c4a333 + 0986e63 commit 8c6f577

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

pkg/filter/driftignore.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (r *DriftIgnore) parseIgnorePattern(line string, patterns *[]gitignore.Patt
9090
func (r *DriftIgnore) isAnyOfChildrenTypesNotIgnored(ty resource.ResourceType) bool {
9191
childrenTypes := resource.GetMeta(ty).GetChildrenTypes()
9292
for _, childrenType := range childrenTypes {
93-
if !r.match(fmt.Sprintf("%s.*", childrenType)) {
93+
if !r.shouldIgnoreType(childrenType) {
9494
return true
9595
}
9696
if r.isAnyOfChildrenTypesNotIgnored(childrenType) {
@@ -107,6 +107,17 @@ func (r *DriftIgnore) IsTypeIgnored(ty resource.ResourceType) bool {
107107
return false
108108
}
109109

110+
return r.shouldIgnoreType(ty)
111+
}
112+
113+
func (r *DriftIgnore) shouldIgnoreType(ty resource.ResourceType) bool {
114+
for _, pattern := range r.ignorePatterns {
115+
// If a line start with a `!` and if the type match, we should not ignore it
116+
if strings.HasPrefix(pattern, fmt.Sprintf("!%s.", ty)) {
117+
return false
118+
}
119+
}
120+
110121
return r.match(fmt.Sprintf("%s.*", ty))
111122
}
112123

pkg/filter/driftignore_test.go

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,50 @@ func TestDriftIgnore_IsTypeIgnored(t *testing.T) {
712712
path: "testdata/drift_ignore_all/.driftignore",
713713
ignores: []string{"*", "!aws_s3*", "!aws_route53*"},
714714
},
715+
{
716+
name: "do not ignore type when one inclusion rule with resource ID exist",
717+
resources: []*resource.Resource{
718+
// This type should not be ignored because of `!aws_iam_policy_attachment.foo*` expression
719+
{
720+
Type: "aws_iam_policy_attachment",
721+
Id: "foobar",
722+
},
723+
// This type should not be ignored because `azurerm_route` type is not ignored and is a child of `azurerm_route_table`
724+
{
725+
Type: "azurerm_route_table",
726+
Id: "uselessId",
727+
},
728+
// This type should not be ignored because of `!azurerm_route.barfoo` expression
729+
{
730+
Type: "azurerm_route",
731+
Id: "barfoo",
732+
},
733+
},
734+
want: []bool{
735+
false,
736+
false,
737+
false,
738+
},
739+
path: "",
740+
ignores: []string{"*", "!aws_iam_policy_attachment.foobar", "!azurerm_route.barfoo"},
741+
},
742+
{
743+
name: "ignore type wildcard while excluding one",
744+
resources: []*resource.Resource{
745+
{
746+
Type: "type_ignored",
747+
},
748+
{
749+
Type: "type_not_ignored",
750+
},
751+
},
752+
want: []bool{
753+
true,
754+
false,
755+
},
756+
path: "",
757+
ignores: []string{"type_*", "!type_not_ignored"},
758+
},
715759
}
716760
for _, tt := range tests {
717761
t.Run(tt.name, func(t *testing.T) {

0 commit comments

Comments
 (0)