Skip to content

Commit 0d9865f

Browse files
authored
feat(misconf): adapt AWS::EC2::VPC (#8534)
Signed-off-by: nikpivkin <[email protected]>
1 parent 9bedd98 commit 0d9865f

File tree

3 files changed

+77
-1
lines changed

3 files changed

+77
-1
lines changed

pkg/iac/adapters/cloudformation/aws/ec2/adapt_test.go

+43
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,17 @@ Resources:
9898
MetadataOptions:
9999
HttpTokens: required
100100
HttpEndpoint: disabled
101+
MyVPC:
102+
Type: AWS::EC2::VPC
103+
Properties:
104+
CidrBlock: 10.0.0.0/16
105+
MyFlowLog:
106+
Type: AWS::EC2::FlowLog
107+
Properties:
108+
LogGroupName: FlowLogsGroup
109+
ResourceId: !Ref MyVPC
110+
ResourceType: VPC
111+
TrafficType: ALL
101112
`,
102113
expected: ec2.EC2{
103114
Instances: []ec2.Instance{
@@ -193,6 +204,11 @@ Resources:
193204
},
194205
},
195206
},
207+
VPCs: []ec2.VPC{
208+
{
209+
FlowLogsEnabled: types.BoolTest(true),
210+
},
211+
},
196212
},
197213
},
198214
{
@@ -237,6 +253,7 @@ Resources:
237253
},
238254
},
239255
},
256+
VPCs: []ec2.VPC{},
240257
},
241258
},
242259
{
@@ -281,6 +298,7 @@ Resources:
281298
},
282299
},
283300
},
301+
VPCs: []ec2.VPC{},
284302
},
285303
},
286304
{
@@ -336,6 +354,7 @@ Resources:
336354
},
337355
},
338356
},
357+
VPCs: []ec2.VPC{},
339358
},
340359
},
341360
{
@@ -364,6 +383,30 @@ Resources:
364383
},
365384
},
366385
},
386+
VPCs: []ec2.VPC{},
387+
},
388+
},
389+
{
390+
name: "VPC flow log ref to other VPC",
391+
source: `AWSTemplateFormatVersion: 2010-09-09
392+
Resources:
393+
MyVPC:
394+
Type: AWS::EC2::VPC
395+
Properties:
396+
CidrBlock: 10.0.0.0/16
397+
MyFlowLog:
398+
Type: AWS::EC2::FlowLog
399+
Properties:
400+
LogGroupName: FlowLogsGroup
401+
ResourceId: !Ref MyOtherVPC
402+
ResourceType: VPC
403+
TrafficType: ALL`,
404+
expected: ec2.EC2{
405+
VPCs: []ec2.VPC{
406+
{
407+
FlowLogsEnabled: types.BoolTest(false),
408+
},
409+
},
367410
},
368411
},
369412
}

pkg/iac/adapters/cloudformation/aws/ec2/ec2.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func Adapt(cfFile parser.FileContext) ec2.EC2 {
1111
LaunchConfigurations: getLaunchConfigurations(cfFile),
1212
LaunchTemplates: getLaunchTemplates(cfFile),
1313
Instances: getInstances(cfFile),
14-
VPCs: nil,
14+
VPCs: getVPCs(cfFile),
1515
NetworkACLs: getNetworkACLs(cfFile),
1616
SecurityGroups: getSecurityGroups(cfFile),
1717
Subnets: getSubnets(cfFile),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package ec2
2+
3+
import (
4+
"github.com/samber/lo"
5+
6+
"github.com/aquasecurity/trivy/pkg/iac/providers/aws/ec2"
7+
"github.com/aquasecurity/trivy/pkg/iac/scanners/cloudformation/parser"
8+
"github.com/aquasecurity/trivy/pkg/iac/types"
9+
"github.com/aquasecurity/trivy/pkg/set"
10+
)
11+
12+
func getVPCs(fctx parser.FileContext) []ec2.VPC {
13+
vpcFlowLogs := getVpcGlowLogs(fctx)
14+
return lo.Map(fctx.GetResourcesByType("AWS::EC2::VPC"),
15+
func(resource *parser.Resource, _ int) ec2.VPC {
16+
return ec2.VPC{
17+
Metadata: resource.Metadata(),
18+
// CloudFormation does not provide direct management for the default VPC
19+
IsDefault: types.BoolUnresolvable(resource.Metadata()),
20+
FlowLogsEnabled: types.Bool(vpcFlowLogs.Contains(resource.ID()), resource.Metadata()),
21+
}
22+
})
23+
}
24+
25+
func getVpcGlowLogs(fctx parser.FileContext) set.Set[string] {
26+
ids := set.New[string]()
27+
for _, resource := range fctx.GetResourcesByType("AWS::EC2::FlowLog") {
28+
if resource.GetStringProperty("ResourceType").EqualTo("VPC") {
29+
ids.Append(resource.GetStringProperty("ResourceId").Value())
30+
}
31+
}
32+
return ids
33+
}

0 commit comments

Comments
 (0)