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

AttributeError when testing protocol/port range for ingress that uses security_groups instead of cidr_blocks #615

Open
pontinjx opened this issue Apr 21, 2022 · 0 comments
Assignees
Labels

Comments

@pontinjx
Copy link

Description

When creating a security group with an ingress that references another security group in the plan, Terraform Compliance fails with AttributeError

To Reproduce

Feature File:

Feature: SSH or RDP Port Open
    Scenario Outline: SSH or RDP Port Open
        Given I have aws_security_group defined
        When it has ingress
        Then it must contain ingress
        Then it must not have <proto> protocol and port <portRange> for "([0-9]{1,3}\.){3}[0-9]{1,3}\/*"

        Examples:
            |proto|portRange|
            |tcp|22|
            |tcp|3389|

Plan File:

{
    "format_version": "1.0",
    "terraform_version": "1.1.9",
    "planned_values": {
        "root_module": {
            "resources": [
                {
                    "address": "aws_security_group.ec2_sg",
                    "mode": "managed",
                    "type": "aws_security_group",
                    "name": "ec2_sg",
                    "provider_name": "registry.terraform.io/hashicorp/aws",
                    "schema_version": 1,
                    "values": {
                        "description": "Managed by Terraform",
                        "ingress": [
                            {
                                "cidr_blocks": [],
                                "description": "",
                                "from_port": 3389,
                                "ipv6_cidr_blocks": [],
                                "prefix_list_ids": [],
                                "protocol": "tcp",
                                "self": false,
                                "to_port": 3389
                            }
                        ],
                        "name": "ec2_sg",
                        "revoke_rules_on_delete": false,
                        "tags": null,
                        "timeouts": null,
                        "vpc_id": "vpc-12345678"
                    },
                    "sensitive_values": {
                        "egress": [],
                        "ingress": [
                            {
                                "cidr_blocks": [],
                                "ipv6_cidr_blocks": [],
                                "prefix_list_ids": [],
                                "security_groups": []
                            }
                        ],
                        "tags_all": {}
                    }
                },
                {
                    "address": "aws_security_group.elb_sg",
                    "mode": "managed",
                    "type": "aws_security_group",
                    "name": "elb_sg",
                    "provider_name": "registry.terraform.io/hashicorp/aws",
                    "schema_version": 1,
                    "values": {
                        "description": "Managed by Terraform",
                        "egress": [
                            {
                                "cidr_blocks": [
                                    "0.0.0.0/0"
                                ],
                                "description": "",
                                "from_port": 0,
                                "ipv6_cidr_blocks": [],
                                "prefix_list_ids": [],
                                "protocol": "-1",
                                "security_groups": [],
                                "self": false,
                                "to_port": 0
                            }
                        ],
                        "name": "elb_sg",
                        "revoke_rules_on_delete": false,
                        "tags": null,
                        "timeouts": null,
                        "vpc_id": "vpc-12345678"
                    },
                    "sensitive_values": {
                        "egress": [
                            {
                                "cidr_blocks": [
                                    false
                                ],
                                "ipv6_cidr_blocks": [],
                                "prefix_list_ids": [],
                                "security_groups": []
                            }
                        ],
                        "ingress": [],
                        "tags_all": {}
                    }
                }
            ]
        }
    },
    "resource_changes": [
        {
            "address": "aws_security_group.ec2_sg",
            "mode": "managed",
            "type": "aws_security_group",
            "name": "ec2_sg",
            "provider_name": "registry.terraform.io/hashicorp/aws",
            "change": {
                "actions": [
                    "create"
                ],
                "before": null,
                "after": {
                    "description": "Managed by Terraform",
                    "ingress": [
                        {
                            "cidr_blocks": [],
                            "description": "",
                            "from_port": 3389,
                            "ipv6_cidr_blocks": [],
                            "prefix_list_ids": [],
                            "protocol": "tcp",
                            "self": false,
                            "to_port": 3389
                        }
                    ],
                    "name": "ec2_sg",
                    "revoke_rules_on_delete": false,
                    "tags": null,
                    "timeouts": null,
                    "vpc_id": "vpc-12345678"
                },
                "after_unknown": {
                    "arn": true,
                    "egress": true,
                    "id": true,
                    "ingress": [
                        {
                            "cidr_blocks": [],
                            "ipv6_cidr_blocks": [],
                            "prefix_list_ids": [],
                            "security_groups": true
                        }
                    ],
                    "name_prefix": true,
                    "owner_id": true,
                    "tags_all": true
                },
                "before_sensitive": false,
                "after_sensitive": {
                    "egress": [],
                    "ingress": [
                        {
                            "cidr_blocks": [],
                            "ipv6_cidr_blocks": [],
                            "prefix_list_ids": [],
                            "security_groups": []
                        }
                    ],
                    "tags_all": {}
                }
            }
        },
        {
            "address": "aws_security_group.elb_sg",
            "mode": "managed",
            "type": "aws_security_group",
            "name": "elb_sg",
            "provider_name": "registry.terraform.io/hashicorp/aws",
            "change": {
                "actions": [
                    "create"
                ],
                "before": null,
                "after": {
                    "description": "Managed by Terraform",
                    "egress": [
                        {
                            "cidr_blocks": [
                                "0.0.0.0/0"
                            ],
                            "description": "",
                            "from_port": 0,
                            "ipv6_cidr_blocks": [],
                            "prefix_list_ids": [],
                            "protocol": "-1",
                            "security_groups": [],
                            "self": false,
                            "to_port": 0
                        }
                    ],
                    "name": "elb_sg",
                    "revoke_rules_on_delete": false,
                    "tags": null,
                    "timeouts": null,
                    "vpc_id": "vpc-12345678"
                },
                "after_unknown": {
                    "arn": true,
                    "egress": [
                        {
                            "cidr_blocks": [
                                false
                            ],
                            "ipv6_cidr_blocks": [],
                            "prefix_list_ids": [],
                            "security_groups": []
                        }
                    ],
                    "id": true,
                    "ingress": true,
                    "name_prefix": true,
                    "owner_id": true,
                    "tags_all": true
                },
                "before_sensitive": false,
                "after_sensitive": {
                    "egress": [
                        {
                            "cidr_blocks": [
                                false
                            ],
                            "ipv6_cidr_blocks": [],
                            "prefix_list_ids": [],
                            "security_groups": []
                        }
                    ],
                    "ingress": [],
                    "tags_all": {}
                }
            }
        }
    ],
    "configuration": {
        "provider_config": {
            "aws": {
                "name": "aws",
                "version_constraint": "~> 3.0",
                "expressions": {
                    "profile": {
                        "constant_value": "devops"
                    },
                    "region": {
                        "constant_value": "eu-west-1"
                    }
                }
            }
        },
        "root_module": {
            "resources": [
                {
                    "address": "aws_security_group.ec2_sg",
                    "mode": "managed",
                    "type": "aws_security_group",
                    "name": "ec2_sg",
                    "provider_config_key": "aws",
                    "expressions": {
                        "ingress": {
                            "references": [
                                "aws_security_group.elb_sg.id",
                                "aws_security_group.elb_sg"
                            ]
                        },
                        "name": {
                            "constant_value": "ec2_sg"
                        },
                        "vpc_id": {
                            "constant_value": "vpc-12345678"
                        }
                    },
                    "schema_version": 1
                },
                {
                    "address": "aws_security_group.elb_sg",
                    "mode": "managed",
                    "type": "aws_security_group",
                    "name": "elb_sg",
                    "provider_config_key": "aws",
                    "expressions": {
                        "egress": {
                            "constant_value": [
                                {
                                    "cidr_blocks": [
                                        "0.0.0.0/0"
                                    ],
                                    "description": null,
                                    "from_port": 0,
                                    "ipv6_cidr_blocks": null,
                                    "prefix_list_ids": null,
                                    "protocol": "-1",
                                    "security_groups": null,
                                    "self": null,
                                    "to_port": 0
                                }
                            ]
                        },
                        "name": {
                            "constant_value": "elb_sg"
                        },
                        "vpc_id": {
                            "constant_value": "vpc-12345678"
                        }
                    },
                    "schema_version": 1
                }
            ]
        }
    }
}

Sample Terraform Code:

resource "aws_security_group" "ec2_sg" {
  name   = "ec2_sg"
  vpc_id = "vpc-12345678"

  ingress {
    from_port       = 6252
    to_port         = 6252
    protocol        = "tcp"
    security_groups = [aws_security_group.elb_sg.id]
  }
}

resource "aws_security_group" "elb_sg" {
  name   = "elb_sg"
  vpc_id = "vpc-12345678"

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


}

Used terraform-compliance Parameters:

N/A

Error Output:

Feature: SSH or RDP Port Open  # /Users/XXXXXX/WSSHRDPPortOpen.feature

    Scenario Outline: SSH or RDP Port Open
        Given I have aws_security_group defined
        When it has ingress
        Then it must contain ingress
        Then it must not have <proto> protocol and port <portRange> for "([0-9]{1,3}\.){3}[0-9]{1,3}\/*"

    Examples:
        | proto | portRange |
        | tcp   | 22        |
          AttributeError: 'str' object has no attribute 'get'
        | tcp   | 3389      |
          AttributeError: 'str' object has no attribute 'get'

or with -S

ERROR: Hook 'silent_formatter_after_each_feature' from /Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/terraform_compliance/extensions/silent_formatter.py:23 raised: 'AttributeError: 'Context' object has no attribute 'failure_msg''

Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/radish/hookregistry.py", line 132, in call
    func(model, *args, **kwargs)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/terraform_compliance/extensions/silent_formatter.py", line 36, in silent_formatter_after_each_feature
    self.silent_formatter_after_each_scenario(scenario)
  File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/terraform_compliance/extensions/silent_formatter.py", line 57, in silent_formatter_after_each_scenario
    if len(step.context.failure_msg) == 2:
AttributeError: 'Context' object has no attribute 'failure_msg'

Expected Behavior:
The test should pass

Tested Versions:

  • terraform-compliance version: v1.3.32
  • terraform version: v1.1.9
  • python version: Python 3.8.1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants