Skip to content

Commit

Permalink
Merge branch 'master' into nik-length-fn
Browse files Browse the repository at this point in the history
  • Loading branch information
simar7 authored Jul 25, 2023
2 parents 8fbc628 + b18871d commit 914f526
Show file tree
Hide file tree
Showing 3 changed files with 121 additions and 1 deletion.
21 changes: 21 additions & 0 deletions pkg/scanners/cloudformation/parser/fn_condition.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package parser

func ResolveCondition(property *Property) (resolved *Property, success bool) {
if !property.isFunction() {
return property, true
}

refProp := property.AsMap()["Condition"]
if refProp.IsNotString() {
return nil, false
}
refValue := refProp.AsString()

for k, prop := range property.ctx.Conditions {
if k == refValue {
return prop.resolveValue()
}
}

return nil, false
}
98 changes: 98 additions & 0 deletions pkg/scanners/cloudformation/parser/fn_condition_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
package parser

import (
"testing"

"github.com/aquasecurity/defsec/pkg/scanners/cloudformation/cftypes"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func Test_resolve_condition_value(t *testing.T) {

fctx := new(FileContext)
fctx.Conditions = map[string]Property{
"SomeCondition": {
ctx: fctx,
Inner: PropertyInner{
Type: cftypes.Map,
Value: map[string]*Property{
"Fn::Equals": {
ctx: fctx,
Inner: PropertyInner{
Type: cftypes.List,
Value: []*Property{
{
Inner: PropertyInner{
Type: cftypes.String,
Value: "some val",
},
},
{
Inner: PropertyInner{
Type: cftypes.String,
Value: "some val",
},
},
},
},
},
},
},
},
"EnableVersioning": {
ctx: fctx,
Inner: PropertyInner{
Type: cftypes.Map,
Value: map[string]*Property{
"Condition": {
Inner: PropertyInner{
Type: cftypes.String,
Value: "SomeCondition",
},
},
},
},
},
}

property := &Property{
ctx: fctx,
Inner: PropertyInner{
Type: cftypes.Map,
Value: map[string]*Property{
"Fn::If": {
ctx: fctx,
Inner: PropertyInner{
Type: cftypes.List,
Value: []*Property{
{
Inner: PropertyInner{
Type: cftypes.String,
Value: "EnableVersioning",
},
},
{
Inner: PropertyInner{
Type: cftypes.String,
Value: "Enabled",
},
},
{
Inner: PropertyInner{
Type: cftypes.String,
Value: "Suspended",
},
},
},
},
},
},
},
}

resolvedProperty, success := ResolveIntrinsicFunc(property)
require.True(t, success)

assert.Equal(t, "Enabled", resolvedProperty.AsString())
}
3 changes: 2 additions & 1 deletion pkg/scanners/cloudformation/parser/intrinsics.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func init() {
"Fn::Or": ResolveOr,
"Fn::Not": ResolveNot,
"Fn::Length": ResolveLength,
"Condition": ResolveCondition,
}
}

Expand All @@ -46,7 +47,7 @@ func IsIntrinsicFunc(node *yaml.Node) bool {
}

nodeTag := strings.TrimPrefix(node.Tag, "!")
if nodeTag != "Ref" {
if nodeTag != "Ref" && nodeTag != "Condition" {
nodeTag = fmt.Sprintf("Fn::%s", nodeTag)
}
for tag := range intrinsicFuncs {
Expand Down

0 comments on commit 914f526

Please sign in to comment.