cloudtools / troposphere

troposphere - Python library to create AWS CloudFormation descriptions
BSD 2-Clause "Simplified" License
4.93k stars 1.45k forks source link

TypeError: {'LabelMatchStatement': {'Key': 'foo', 'Scope': 'bar'}} is not a valid Statement #2122

Open delkopiso opened 1 year ago

delkopiso commented 1 year ago

Problem

The error TypeError: {'LabelMatchStatement': {'Key': 'foo', 'Scope': 'bar'}} is not a valid Statement is returned when ingesting a CloudFormation template with TemplateGenerator.

Expected behavior

Any template built with the troposphere Template should be digestible via TemplateGenerator.

Reproduction steps

import json

from troposphere import Template
from troposphere.template_generator import TemplateGenerator
from troposphere.wafv2 import (
    Statement,
    WebACLRule,
    RuleAction,
    BlockAction,
    VisibilityConfig,
    WebACL,
    AllowAction,
    DefaultAction,
    LabelMatchStatement,
)

def build_template() -> Template:
    t = Template()
    t.set_version()
    t.set_description("reproduction case.")

    t.add_resource(
        WebACL(
            "waf",
            DefaultAction=DefaultAction(Allow=AllowAction()),
            Description="description of waf",
            Rules=[
                WebACLRule(
                    Name="rule1",
                    Action=RuleAction(Block=BlockAction()),
                    Priority=0,
                    Statement=Statement(
                        LabelMatchStatement=LabelMatchStatement(
                            Key="foo", Scope="bar",
                        )
                    ),
                    VisibilityConfig=VisibilityConfig(
                        CloudWatchMetricsEnabled=True,
                        MetricName="metric",
                        SampledRequestsEnabled=False,
                    ),
                ),
            ],
            Scope="REGIONAL",
            VisibilityConfig=VisibilityConfig(
                CloudWatchMetricsEnabled=True,
                MetricName="metric",
                SampledRequestsEnabled=False,
            ),
        )
    )

    return t

if __name__ == '__main__':
    built_template = build_template()
    built_json = built_template.to_json()
    print(built_json)
    generated_template = TemplateGenerator(json.loads(built_json))
    generated_json = generated_template.to_json()
    print(generated_json)