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

bug(misconf): Fix rendering of map slices in Terraform resource block #5172

Closed
simar7 opened this issue Sep 13, 2023 · 0 comments · Fixed by aquasecurity/defsec#1450
Closed
Assignees
Labels
kind/bug Categorizes issue or PR as related to a bug. scan/misconfiguration Issues relating to misconfiguration scanning
Milestone

Comments

@simar7
Copy link
Member

simar7 commented Sep 13, 2023

Motivation

When processing a tfplan that contains the following Terraform resource, the hcl file is not output correctly.

resource "aws_imagebuilder_distribution_configuration" "example" {
  name = "example"

  distribution {
    ami_distribution_configuration {
      ami_tags = {
        CostCenter = "IT"
      }

      name = "example-{{ imagebuilder:buildDate }}"

      launch_permission {
        user_ids = ["123456789012"]
      }
    }

    launch_template_configuration {
      launch_template_id = "lt-0aaa1bcde2ff3456"
    }

    region = "us-east-1"
  }
}

resource "aws_security_group" "allow_tls" {
  name        = "allow_tls"
  description = "Allow TLS inbound traffic"
  vpc_id      = "bar"

  ingress {
    description      = "TLS from VPC"
    from_port        = 443
    to_port          = 443
    protocol         = "tcp"
    cidr_blocks      = ["0.0.0.0/0"]
  }

  egress {
    from_port        = 0
    to_port          = 0
    protocol         = "-1"
    cidr_blocks      = ["0.0.0.0/0"]
    ipv6_cidr_blocks = ["::/0"]
  }

  tags = {
    Name = "allow_tls"
  }
}

expected

resource "aws_imagebuilder_distribution_configuration" "example" {
	name = "example"
	distribution {
	ami_distribution_configuration {
	ami_tags = {
	CostCenter = "IT"
}
	description = ""
	kms_key_id = ""
	launch_permission = {
	organization_arns = []
	organizational_unit_arns = []
	user_groups = []
	user_ids = [
	"123456789012",
]
}
	name = "example-{{ imagebuilder:buildDate }}"
	target_account_ids = []
}
	launch_template_configuration {
	account_id = ""
	default = true
	launch_template_id = "lt-0aaa1bcde2ff3456"
}
	region = "us-east-1"
	}
}

resource "aws_security_group" "allow_tls" {
	description = "Allow TLS inbound traffic"
	egress {
	protocol = "-1"
	cidr_blocks = [
	"0.0.0.0/0",
]
	from_port = 0
	ipv6_cidr_blocks = [
	"::/0",
]
	to_port = 0
}
	ingress {
	cidr_blocks = [
	"0.0.0.0/0",
]
	description = "TLS from VPC"
	protocol = "tcp"
	from_port = 443
	to_port = 443
}
	name = "allow_tls"
	tags = {
	Name = "allow_tls"
}
	tags_all = {
	Name = "allow_tls"
}
	vpc_id = "bar"
	egress {
	cidr_blocks = [
	"0.0.0.0/0",
]
	ipv6_cidr_blocks = [
	"::/0",
]
	protocol = "-1"
	}
ingress {
	cidr_blocks = [
	"0.0.0.0/0",
]
	description = "TLS from VPC"
	from_port = 443
	protocol = "tcp"
	to_port = 443
	}
}

actual

resource "aws_imagebuilder_distribution_configuration" "example" {
	name = "example"
	distribution {
	ami_distribution_configuration {
	description = ""
	kms_key_id = ""
	launch_permission = {
	user_groups = []
	user_ids =  = [
	"123456789012",
]
	organization_arns = []
	organizational_unit_arns = []
}
	name = "example-{{ imagebuilder:buildDate }}"
	target_account_ids = []
	ami_tags = map[string]interface {}{"CostCenter":"IT"}
}
	launch_template_configuration {
	account_id = ""
	default = true
	launch_template_id = "lt-0aaa1bcde2ff3456"
}
	region = "us-east-1"
	}
}

resource "aws_security_group" "allow_tls" {
	description = "Allow TLS inbound traffic"
	egress {
	from_port = 0
	ipv6_cidr_blocks =  = [
	"::/0",
]
	to_port = 0
	cidr_blocks =  = [
	"0.0.0.0/0",
]
	protocol = "-1"
}
	ingress {
	protocol = "tcp"
	to_port = 443
	cidr_blocks =  = [
	"0.0.0.0/0",
]
	description = "TLS from VPC"
	from_port = 443
}
	name = "allow_tls"
	tags = {
	Name = "allow_tls"
}
	tags_all = {
	Name = "allow_tls"
}
	vpc_id = "bar"
	egress {
	cidr_blocks  = [
	"0.0.0.0/0",
]
	ipv6_cidr_blocks  = [
	"::/0",
]
	protocol = "-1"
	}
ingress {
	cidr_blocks  = [
	"0.0.0.0/0",
]
	description = "TLS from VPC"
	from_port = 443
	protocol = "tcp"
	to_port = 443
	}
}

wrong points

  • line 17: ami_tags = map[string]interface {}{"CostCenter":"IT"}
    • directly print map[string]{} objects
  • line 36: cidr_blocks = = [
  • line 44: cidr_blocks = = [

These problems prevent trivy config . /tfplan.json does not work correctly and I would like to fix it.

JSON file for reproduction

{"format_version":"1.2","terraform_version":"1.5.7","planned_values":{"root_module":{"resources":[{"address":"aws_imagebuilder_distribution_configuration.example","mode":"managed","type":"aws_imagebuilder_distribution_configuration","name":"example","provider_name":"registry.terraform.io/hashicorp/aws","schema_version":0,"values":{"description":null,"distribution":[{"ami_distribution_configuration":[{"ami_tags":{"CostCenter":"IT"},"description":"","kms_key_id":"","launch_permission":[{"organization_arns":[],"organizational_unit_arns":[],"user_groups":[],"user_ids":["123456789012"]}],"name":"example-{{ imagebuilder:buildDate }}","target_account_ids":[]}],"container_distribution_configuration":[],"fast_launch_configuration":[],"launch_template_configuration":[{"account_id":"","default":true,"launch_template_id":"lt-0aaa1bcde2ff3456"}],"license_configuration_arns":[],"region":"us-east-1"}],"name":"example","tags":null},"sensitive_values":{"distribution":[{"ami_distribution_configuration":[{"ami_tags":{},"launch_permission":[{"organization_arns":[],"organizational_unit_arns":[],"user_groups":[],"user_ids":[false]}],"target_account_ids":[]}],"container_distribution_configuration":[],"fast_launch_configuration":[],"launch_template_configuration":[{}],"license_configuration_arns":[]}],"tags_all":{}}},{"address":"aws_security_group.allow_tls","mode":"managed","type":"aws_security_group","name":"allow_tls","provider_name":"registry.terraform.io/hashicorp/aws","schema_version":1,"values":{"description":"Allow TLS inbound traffic","egress":[{"cidr_blocks":["0.0.0.0/0"],"description":"","from_port":0,"ipv6_cidr_blocks":["::/0"],"prefix_list_ids":[],"protocol":"-1","security_groups":[],"self":false,"to_port":0}],"ingress":[{"cidr_blocks":["0.0.0.0/0"],"description":"TLS from VPC","from_port":443,"ipv6_cidr_blocks":[],"prefix_list_ids":[],"protocol":"tcp","security_groups":[],"self":false,"to_port":443}],"name":"allow_tls","revoke_rules_on_delete":false,"tags":{"Name":"allow_tls"},"tags_all":{"Name":"allow_tls"},"timeouts":null,"vpc_id":"bar"},"sensitive_values":{"egress":[{"cidr_blocks":[false],"ipv6_cidr_blocks":[false],"prefix_list_ids":[],"security_groups":[]}],"ingress":[{"cidr_blocks":[false],"ipv6_cidr_blocks":[],"prefix_list_ids":[],"security_groups":[]}],"tags":{},"tags_all":{}}}]}},"resource_changes":[{"address":"aws_imagebuilder_distribution_configuration.example","mode":"managed","type":"aws_imagebuilder_distribution_configuration","name":"example","provider_name":"registry.terraform.io/hashicorp/aws","change":{"actions":["create"],"before":null,"after":{"description":null,"distribution":[{"ami_distribution_configuration":[{"ami_tags":{"CostCenter":"IT"},"description":"","kms_key_id":"","launch_permission":[{"organization_arns":[],"organizational_unit_arns":[],"user_groups":[],"user_ids":["123456789012"]}],"name":"example-{{ imagebuilder:buildDate }}","target_account_ids":[]}],"container_distribution_configuration":[],"fast_launch_configuration":[],"launch_template_configuration":[{"account_id":"","default":true,"launch_template_id":"lt-0aaa1bcde2ff3456"}],"license_configuration_arns":[],"region":"us-east-1"}],"name":"example","tags":null},"after_unknown":{"arn":true,"date_created":true,"date_updated":true,"distribution":[{"ami_distribution_configuration":[{"ami_tags":{},"launch_permission":[{"organization_arns":[],"organizational_unit_arns":[],"user_groups":[],"user_ids":[false]}],"target_account_ids":[]}],"container_distribution_configuration":[],"fast_launch_configuration":[],"launch_template_configuration":[{}],"license_configuration_arns":[]}],"id":true,"tags_all":true},"before_sensitive":false,"after_sensitive":{"distribution":[{"ami_distribution_configuration":[{"ami_tags":{},"launch_permission":[{"organization_arns":[],"organizational_unit_arns":[],"user_groups":[],"user_ids":[false]}],"target_account_ids":[]}],"container_distribution_configuration":[],"fast_launch_configuration":[],"launch_template_configuration":[{}],"license_configuration_arns":[]}],"tags_all":{}}}},{"address":"aws_security_group.allow_tls","mode":"managed","type":"aws_security_group","name":"allow_tls","provider_name":"registry.terraform.io/hashicorp/aws","change":{"actions":["create"],"before":null,"after":{"description":"Allow TLS inbound traffic","egress":[{"cidr_blocks":["0.0.0.0/0"],"description":"","from_port":0,"ipv6_cidr_blocks":["::/0"],"prefix_list_ids":[],"protocol":"-1","security_groups":[],"self":false,"to_port":0}],"ingress":[{"cidr_blocks":["0.0.0.0/0"],"description":"TLS from VPC","from_port":443,"ipv6_cidr_blocks":[],"prefix_list_ids":[],"protocol":"tcp","security_groups":[],"self":false,"to_port":443}],"name":"allow_tls","revoke_rules_on_delete":false,"tags":{"Name":"allow_tls"},"tags_all":{"Name":"allow_tls"},"timeouts":null,"vpc_id":"bar"},"after_unknown":{"arn":true,"egress":[{"cidr_blocks":[false],"ipv6_cidr_blocks":[false],"prefix_list_ids":[],"security_groups":[]}],"id":true,"ingress":[{"cidr_blocks":[false],"ipv6_cidr_blocks":[],"prefix_list_ids":[],"security_groups":[]}],"name_prefix":true,"owner_id":true,"tags":{},"tags_all":{}},"before_sensitive":false,"after_sensitive":{"egress":[{"cidr_blocks":[false],"ipv6_cidr_blocks":[false],"prefix_list_ids":[],"security_groups":[]}],"ingress":[{"cidr_blocks":[false],"ipv6_cidr_blocks":[],"prefix_list_ids":[],"security_groups":[]}],"tags":{},"tags_all":{}}}}],"configuration":{"provider_config":{"aws":{"name":"aws","full_name":"registry.terraform.io/hashicorp/aws","version_constraint":"~\u003e 5.16","expressions":{"region":{"constant_value":"ap-northeast-1"}}}},"root_module":{"resources":[{"address":"aws_imagebuilder_distribution_configuration.example","mode":"managed","type":"aws_imagebuilder_distribution_configuration","name":"example","provider_config_key":"aws","expressions":{"distribution":[{"ami_distribution_configuration":[{"ami_tags":{"constant_value":{"CostCenter":"IT"}},"launch_permission":[{"user_ids":{"constant_value":["123456789012"]}}],"name":{"constant_value":"example-{{ imagebuilder:buildDate }}"}}],"launch_template_configuration":[{"launch_template_id":{"constant_value":"lt-0aaa1bcde2ff3456"}}],"region":{"constant_value":"us-east-1"}}],"name":{"constant_value":"example"}},"schema_version":0},{"address":"aws_security_group.allow_tls","mode":"managed","type":"aws_security_group","name":"allow_tls","provider_config_key":"aws","expressions":{"description":{"constant_value":"Allow TLS inbound traffic"},"egress":{"constant_value":[{"cidr_blocks":["0.0.0.0/0"],"description":null,"from_port":0,"ipv6_cidr_blocks":["::/0"],"prefix_list_ids":null,"protocol":"-1","security_groups":null,"self":null,"to_port":0}]},"ingress":{"constant_value":[{"cidr_blocks":["0.0.0.0/0"],"description":"TLS from VPC","from_port":443,"ipv6_cidr_blocks":null,"prefix_list_ids":null,"protocol":"tcp","security_groups":null,"self":null,"to_port":443}]},"name":{"constant_value":"allow_tls"},"tags":{"constant_value":{"Name":"allow_tls"}},"vpc_id":{"constant_value":"bar"}},"schema_version":1}]}},"timestamp":"2023-09-09T00:07:06Z"}
@simar7 simar7 added kind/bug Categorizes issue or PR as related to a bug. scan/misconfiguration Issues relating to misconfiguration scanning labels Sep 13, 2023
@simar7 simar7 self-assigned this Sep 13, 2023
@simar7 simar7 added this to the v0.46.0 milestone Sep 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug Categorizes issue or PR as related to a bug. scan/misconfiguration Issues relating to misconfiguration scanning
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

1 participant