Skip to content
This repository has been archived by the owner on Jan 23, 2025. It is now read-only.

feat: add OnlyContains helper function #953

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Changes from 1 commit
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
57 changes: 55 additions & 2 deletions pkg/terraform/attribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ import (
"strconv"
"strings"

defsecTypes "github.com/aquasecurity/defsec/pkg/types"

"github.com/aquasecurity/defsec/pkg/scanners/terraform/context"
defsecTypes "github.com/aquasecurity/defsec/pkg/types"

"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/hcl/v2/hclsyntax"
Expand Down Expand Up @@ -483,6 +482,60 @@ func (a *Attribute) Contains(checkValue interface{}, equalityOptions ...Equality
return strings.Contains(val.AsString(), stringToLookFor)
}

func (a *Attribute) OnlyContains(checkValue interface{}) bool {
owenrumney marked this conversation as resolved.
Show resolved Hide resolved
if a == nil {
return false
}
val := a.Value()
if val.IsNull() {
return false
}

checkSlice, ok := checkValue.([]interface{})
if !ok {
return false
}

if val.Type().IsListType() || val.Type().IsTupleType() {
for _, value := range val.AsValueSlice() {
found := false
for _, cVal := range checkSlice {
switch t := cVal.(type) {
case string:
if t == value.AsString() {
found = true
break
}
case bool:
if t == value.True() {
found = true
break
}
case int, int8, int16, int32, int64:
i, _ := value.AsBigFloat().Int64()
if t == i {
found = true
break
}
case float32, float64:
f, _ := value.AsBigFloat().Float64()
if t == f {
found = true
break
}
}

}
if !found {
return false
}
}
return true
}

return false
}

func containsIgnoreCase(left, substring string) bool {
return strings.Contains(strings.ToLower(left), strings.ToLower(substring))
}
Expand Down