Skip to content

Commit

Permalink
feat: add OnlyContains helper function (#953)
Browse files Browse the repository at this point in the history
Signed-off-by: Owen Rumney <[email protected]>
  • Loading branch information
Owen Rumney authored Sep 21, 2022
1 parent f08667f commit b3b0414
Showing 1 changed file with 55 additions and 2 deletions.
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 {
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

0 comments on commit b3b0414

Please sign in to comment.