Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(cloudformation): set context for conditions #1389

Merged
merged 2 commits into from
Jul 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions pkg/scanners/cloudformation/parser/fn_ref.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@ func ResolveReference(property *Property) (resolved *Property, success bool) {
return property.deriveResolved(cftypes.String, pseudo.(string)), true
}

if property.ctx == nil {
return property, false
}

var param *Parameter
for k := range property.ctx.Parameters {
if k == refValue {
Expand Down
5 changes: 5 additions & 0 deletions pkg/scanners/cloudformation/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ func (p *Parser) ParseFile(ctx context.Context, fs fs.FS, path string) (context

p.debug.Log("Context loaded from source %s", path)

// the context must be set to conditions before resources
for _, c := range context.Conditions {
c.setContext(context)
}

for name, r := range context.Resources {
r.ConfigureResource(name, fs, path, context)
}
Expand Down
51 changes: 51 additions & 0 deletions pkg/scanners/cloudformation/parser/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,3 +176,54 @@ func createTestFileContext(t *testing.T, source string) *FileContext {
require.Len(t, contexts, 1)
return contexts[0]
}

func Test_parse_yaml_use_condition_in_resource(t *testing.T) {
source := `---
AWSTemplateFormatVersion: "2010-09-09"
Description: some description
Parameters:
ServiceName:
Type: String
Description: The service name
EnvName:
Type: String
Description: Optional environment name to prefix all resources with
Default: ""

Conditions:
SuffixResources: !Not [!Equals [!Ref EnvName, ""]]

Resources:
ErrorTimedOutMetricFilter:
Type: AWS::Logs::MetricFilter
Properties:
FilterPattern: '?ERROR ?error ?Error ?"timed out"' # If log contains one of these error words or timed out
LogGroupName:
!If [
SuffixResources,
!Sub "/aws/lambda/${ServiceName}-${EnvName}",
!Sub "/aws/lambda/${ServiceName}",
]
MetricTransformations:
- MetricName: !Sub "${ServiceName}-ErrorLogCount"
MetricNamespace: market-LogMetrics
MetricValue: 1
DefaultValue: 0
`

files, err := parseFile(t, source, "cf.yaml")
require.NoError(t, err)
assert.Len(t, files, 1)
ctx := files[0]

assert.Len(t, ctx.Parameters, 2)
assert.Len(t, ctx.Conditions, 1)
assert.Len(t, ctx.Resources, 1)

res := ctx.GetResourceByLogicalID("ErrorTimedOutMetricFilter")
assert.NotNil(t, res)

refProp := res.GetProperty("LogGroupName")
assert.False(t, refProp.IsNil())
assert.Equal(t, "/aws/lambda/${ServiceName}", refProp.AsString())
}