File tree 3 files changed +77
-1
lines changed
pkg/iac/adapters/cloudformation/aws/ec2
3 files changed +77
-1
lines changed Original file line number Diff line number Diff line change @@ -98,6 +98,17 @@ Resources:
98
98
MetadataOptions:
99
99
HttpTokens: required
100
100
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
101
112
` ,
102
113
expected : ec2.EC2 {
103
114
Instances : []ec2.Instance {
@@ -193,6 +204,11 @@ Resources:
193
204
},
194
205
},
195
206
},
207
+ VPCs : []ec2.VPC {
208
+ {
209
+ FlowLogsEnabled : types .BoolTest (true ),
210
+ },
211
+ },
196
212
},
197
213
},
198
214
{
@@ -237,6 +253,7 @@ Resources:
237
253
},
238
254
},
239
255
},
256
+ VPCs : []ec2.VPC {},
240
257
},
241
258
},
242
259
{
@@ -281,6 +298,7 @@ Resources:
281
298
},
282
299
},
283
300
},
301
+ VPCs : []ec2.VPC {},
284
302
},
285
303
},
286
304
{
@@ -336,6 +354,7 @@ Resources:
336
354
},
337
355
},
338
356
},
357
+ VPCs : []ec2.VPC {},
339
358
},
340
359
},
341
360
{
@@ -364,6 +383,30 @@ Resources:
364
383
},
365
384
},
366
385
},
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
+ },
367
410
},
368
411
},
369
412
}
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ func Adapt(cfFile parser.FileContext) ec2.EC2 {
11
11
LaunchConfigurations : getLaunchConfigurations (cfFile ),
12
12
LaunchTemplates : getLaunchTemplates (cfFile ),
13
13
Instances : getInstances (cfFile ),
14
- VPCs : nil ,
14
+ VPCs : getVPCs ( cfFile ) ,
15
15
NetworkACLs : getNetworkACLs (cfFile ),
16
16
SecurityGroups : getSecurityGroups (cfFile ),
17
17
Subnets : getSubnets (cfFile ),
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments