Skip to content

Commit 349caf9

Browse files
authored
feat(misconf): support for VPC resources for inbound/outbound rules (#6779)
1 parent 21114c9 commit 349caf9

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

pkg/iac/adapters/terraform/aws/ec2/vpc.go

+28
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,16 @@ func (a *sgAdapter) adaptSecurityGroup(resource *terraform.Block, module terrafo
134134
}
135135
}
136136

137+
for _, r := range module.GetReferencingResources(resource, "aws_vpc_security_group_ingress_rule", "security_group_id") {
138+
a.sgRuleIDs.Resolve(r.ID())
139+
ingressRules = append(ingressRules, adaptSingleSGRule(r))
140+
}
141+
142+
for _, r := range module.GetReferencingResources(resource, "aws_vpc_security_group_egress_rule", "security_group_id") {
143+
a.sgRuleIDs.Resolve(r.ID())
144+
egressRules = append(egressRules, adaptSingleSGRule(r))
145+
}
146+
137147
return ec2.SecurityGroup{
138148
Metadata: resource.GetMetadata(),
139149
Description: descriptionVal,
@@ -178,6 +188,24 @@ func adaptSGRule(resource *terraform.Block, modules terraform.Modules) ec2.Secur
178188
}
179189
}
180190

191+
func adaptSingleSGRule(resource *terraform.Block) ec2.SecurityGroupRule {
192+
description := resource.GetAttribute("description").AsStringValueOrDefault("", resource)
193+
194+
var cidrs []iacTypes.StringValue
195+
if ipv4 := resource.GetAttribute("cidr_ipv4"); ipv4.IsNotNil() {
196+
cidrs = append(cidrs, ipv4.AsStringValueOrDefault("", resource))
197+
}
198+
if ipv6 := resource.GetAttribute("cidr_ipv6"); ipv6.IsNotNil() {
199+
cidrs = append(cidrs, ipv6.AsStringValueOrDefault("", resource))
200+
}
201+
202+
return ec2.SecurityGroupRule{
203+
Metadata: resource.GetMetadata(),
204+
Description: description,
205+
CIDRs: cidrs,
206+
}
207+
}
208+
181209
func (a *naclAdapter) adaptNetworkACL(resource *terraform.Block, module *terraform.Module) ec2.NetworkACL {
182210
var networkRules []ec2.NetworkACLRule
183211
rulesBlocks := module.GetReferencingResources(resource, "aws_network_acl_rule", "network_acl_id")

pkg/iac/adapters/terraform/aws/ec2/vpc_test.go

+44
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,50 @@ resource "aws_flow_log" "this" {
221221
},
222222
},
223223
},
224+
{
225+
name: "ingress and egress rules",
226+
terraform: `
227+
resource "aws_security_group" "example" {
228+
name = "example"
229+
description = "example"
230+
}
231+
232+
resource "aws_vpc_security_group_egress_rule" "test" {
233+
security_group_id = aws_security_group.example.id
234+
cidr_ipv4 = "0.0.0.0/0"
235+
ip_protocol = "-1" # semantically equivalent to all ports
236+
}
237+
238+
resource "aws_vpc_security_group_ingress_rule" "test" {
239+
security_group_id = aws_security_group.example.id
240+
cidr_ipv4 = "0.0.0.0/0"
241+
from_port = "22"
242+
to_port = "22"
243+
ip_protocol = "tcp"
244+
}
245+
`,
246+
expected: ec2.EC2{
247+
SecurityGroups: []ec2.SecurityGroup{
248+
{
249+
Description: iacTypes.StringTest("example"),
250+
IngressRules: []ec2.SecurityGroupRule{
251+
{
252+
CIDRs: []iacTypes.StringValue{
253+
iacTypes.StringTest("0.0.0.0/0"),
254+
},
255+
},
256+
},
257+
EgressRules: []ec2.SecurityGroupRule{
258+
{
259+
CIDRs: []iacTypes.StringValue{
260+
iacTypes.StringTest("0.0.0.0/0"),
261+
},
262+
},
263+
},
264+
},
265+
},
266+
},
267+
},
224268
}
225269

226270
for _, test := range tests {

0 commit comments

Comments
 (0)