open-policy-agent / conftest

Write tests against structured configuration data using the Open Policy Agent Rego query language
https://conftest.dev
Other
2.84k stars 301 forks source link

Error parsing HCL2 labels property #249

Closed philjhale closed 4 years ago

philjhale commented 4 years ago

Here is a snippet of Terraform HCL2 code.

variable "project_name" {
  type        = string
}

module "project_factory" {
  source  = "terraform-google-modules/project-factory/google"
  version = "~> 6.0"

  name  = var.project_name

  labels = {
      env = "dev"
  }
}

Executing conftest parse [filename] -i hcl2 using Conftest 0.17 results in the following output.

{
    "module": {
        "project_factory": {
            "labels": "${{\n      env = \"dev\"\n  }}",
            "name": "${var.project_name}",
            "source": "terraform-google-modules/project-factory/google",
            "version": "~> 6.0"
        }
    },
    "variable": {
        "project_name": {
            "type": "${string}"
        }
    }
}

It seems like there has been a problem parsing the labels property. I was expecting output something like this:

...
"labels": {
  "env": "dev"
}
...
boranx commented 4 years ago

Hi @philjhale Thanks for issuing this out I made a PR(#253 ) that fixes that, after it gets merged we can obtain the output

{
        "module": {
                "project_factory": {
                        "labels": {
                                "env": "dev"
                        },
                        "name": "${var.project_name}",
                        "source": "terraform-google-modules/project-factory/google",
                        "version": "~\u003e 6.0"
                }
        },
        "variable": {
                "project_name": {
                        "type": "${string}"
                }
        }
}

But I noticed while hacking that there is also an encoding problem in the output. "version": "~\u003e 6.0" should be "version": "~> 6.0" However, this behaviour does not relate to this issue. I confirm the same issue happens when using v0.17 as well