From 349caf96bc3dd81551d488044f1adfdb947f39fb Mon Sep 17 00:00:00 2001 From: Nikita Pivkin Date: Tue, 28 May 2024 04:40:45 +0700 Subject: [PATCH] feat(misconf): support for VPC resources for inbound/outbound rules (#6779) --- pkg/iac/adapters/terraform/aws/ec2/vpc.go | 28 ++++++++++++ .../adapters/terraform/aws/ec2/vpc_test.go | 44 +++++++++++++++++++ 2 files changed, 72 insertions(+) diff --git a/pkg/iac/adapters/terraform/aws/ec2/vpc.go b/pkg/iac/adapters/terraform/aws/ec2/vpc.go index 440de4619e74..92ee730e0947 100644 --- a/pkg/iac/adapters/terraform/aws/ec2/vpc.go +++ b/pkg/iac/adapters/terraform/aws/ec2/vpc.go @@ -134,6 +134,16 @@ func (a *sgAdapter) adaptSecurityGroup(resource *terraform.Block, module terrafo } } + for _, r := range module.GetReferencingResources(resource, "aws_vpc_security_group_ingress_rule", "security_group_id") { + a.sgRuleIDs.Resolve(r.ID()) + ingressRules = append(ingressRules, adaptSingleSGRule(r)) + } + + for _, r := range module.GetReferencingResources(resource, "aws_vpc_security_group_egress_rule", "security_group_id") { + a.sgRuleIDs.Resolve(r.ID()) + egressRules = append(egressRules, adaptSingleSGRule(r)) + } + return ec2.SecurityGroup{ Metadata: resource.GetMetadata(), Description: descriptionVal, @@ -178,6 +188,24 @@ func adaptSGRule(resource *terraform.Block, modules terraform.Modules) ec2.Secur } } +func adaptSingleSGRule(resource *terraform.Block) ec2.SecurityGroupRule { + description := resource.GetAttribute("description").AsStringValueOrDefault("", resource) + + var cidrs []iacTypes.StringValue + if ipv4 := resource.GetAttribute("cidr_ipv4"); ipv4.IsNotNil() { + cidrs = append(cidrs, ipv4.AsStringValueOrDefault("", resource)) + } + if ipv6 := resource.GetAttribute("cidr_ipv6"); ipv6.IsNotNil() { + cidrs = append(cidrs, ipv6.AsStringValueOrDefault("", resource)) + } + + return ec2.SecurityGroupRule{ + Metadata: resource.GetMetadata(), + Description: description, + CIDRs: cidrs, + } +} + func (a *naclAdapter) adaptNetworkACL(resource *terraform.Block, module *terraform.Module) ec2.NetworkACL { var networkRules []ec2.NetworkACLRule rulesBlocks := module.GetReferencingResources(resource, "aws_network_acl_rule", "network_acl_id") diff --git a/pkg/iac/adapters/terraform/aws/ec2/vpc_test.go b/pkg/iac/adapters/terraform/aws/ec2/vpc_test.go index 72005e5b8559..611f8ca8527d 100644 --- a/pkg/iac/adapters/terraform/aws/ec2/vpc_test.go +++ b/pkg/iac/adapters/terraform/aws/ec2/vpc_test.go @@ -221,6 +221,50 @@ resource "aws_flow_log" "this" { }, }, }, + { + name: "ingress and egress rules", + terraform: ` +resource "aws_security_group" "example" { + name = "example" + description = "example" +} + +resource "aws_vpc_security_group_egress_rule" "test" { + security_group_id = aws_security_group.example.id + cidr_ipv4 = "0.0.0.0/0" + ip_protocol = "-1" # semantically equivalent to all ports +} + +resource "aws_vpc_security_group_ingress_rule" "test" { + security_group_id = aws_security_group.example.id + cidr_ipv4 = "0.0.0.0/0" + from_port = "22" + to_port = "22" + ip_protocol = "tcp" +} +`, + expected: ec2.EC2{ + SecurityGroups: []ec2.SecurityGroup{ + { + Description: iacTypes.StringTest("example"), + IngressRules: []ec2.SecurityGroupRule{ + { + CIDRs: []iacTypes.StringValue{ + iacTypes.StringTest("0.0.0.0/0"), + }, + }, + }, + EgressRules: []ec2.SecurityGroupRule{ + { + CIDRs: []iacTypes.StringValue{ + iacTypes.StringTest("0.0.0.0/0"), + }, + }, + }, + }, + }, + }, + }, } for _, test := range tests {