hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
41.68k stars 9.41k forks source link

parse error in resource declaration header leads to crash #19327

Closed apparentlymart closed 5 years ago

apparentlymart commented 5 years ago

If the header of a resource declaration block contains a syntax error, the HCL parser attempts recovery in order to parse the rest of the file and find any other potential errors. The result in this case is an incomplete parse tree and some diagnostic messages.

Terraform is currently then still attempting to decode such incomplete blocks during its later validation pass, again attempting to collect as many errors as possible at once before returning.

Unfortunately, Terraform is not being defensive enough about the possibility of an incomplete data structure, leading to a crash.

For example:

resource "null_resource" "foo_${interpolations_invalid_here}" {
}

This leads to a panic during config loading:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x802350]

goroutine 1 [running]:
github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax.(*Body).PartialContent(0x0, 0x61e8c00, 0xc0002b5588, 0x8, 0xc0004422c8, 0x1, 0x0, 0x0)
    /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/hashicorp/hcl2/hcl/hclsyntax/structure.go:135 +0x80
github.com/hashicorp/terraform/configs.decodeResourceBlock(0xc000286680, 0x8, 0x3467d95, 0x8, 0x0)
    /opt/gopath/src/github.com/hashicorp/terraform/configs/resource.go:88 +0x181
github.com/hashicorp/terraform/configs.(*Parser).loadConfigFile(0xc0004c87e0, 0xc0002db1c0, 0x1d, 0x0, 0x0, 0x37b4640, 0x1d, 0x2)
    /opt/gopath/src/github.com/hashicorp/terraform/configs/parser_config.go:115 +0xe27
github.com/hashicorp/terraform/configs.(*Parser).LoadConfigFile(0xc0004c87e0, 0xc0002db1c0, 0x1d, 0x0, 0x0, 0x1, 0xc0002db142)
    /opt/gopath/src/github.com/hashicorp/terraform/configs/parser_config.go:22 +0x44
github.com/hashicorp/terraform/configs.(*Parser).loadFiles(0xc0004c87e0, 0xc000467140, 0x1, 0x1, 0x0, 0x1, 0x0, 0x0, 0x0, 0x0, ...)
    /opt/gopath/src/github.com/hashicorp/terraform/configs/parser_config_dir.go:76 +0x340
github.com/hashicorp/terraform/configs.(*Parser).LoadConfigDir(0xc0004c87e0, 0x3460b75, 0x1, 0x0, 0x1, 0xc00041d588, 0xb2d1e0)
    /opt/gopath/src/github.com/hashicorp/terraform/configs/parser_config_dir.go:36 +0x124
github.com/hashicorp/terraform/command.(*Meta).loadSingleModule(0xc0003008c0, 0x3460b75, 0x1, 0x0, 0x0, 0x0, 0xe)
    /opt/gopath/src/github.com/hashicorp/terraform/command/meta_config.go:85 +0x158
github.com/hashicorp/terraform/command.(*InitCommand).Run(0xc0003008c0, 0xc00000c090, 0x0, 0x0, 0xc000466bb0)
    /opt/gopath/src/github.com/hashicorp/terraform/command/init.go:169 +0xa07
github.com/hashicorp/terraform/vendor/github.com/mitchellh/cli.(*CLI).Run(0xc0002e9e00, 0xc0002e9e00, 0xc00041dd20, 0x1)
    /opt/gopath/src/github.com/hashicorp/terraform/vendor/github.com/mitchellh/cli/cli.go:255 +0x207
main.wrappedMain(0x0)
    /opt/gopath/src/github.com/hashicorp/terraform/main.go:222 +0xacc
main.realMain(0x0)
    /opt/gopath/src/github.com/hashicorp/terraform/main.go:99 +0xb7
main.main()
    /opt/gopath/src/github.com/hashicorp/terraform/main.go:35 +0x2e

We addressed a previous error of this type by paired fixes in both HCL and Terraform: HCL ought to produce a valid but incomplete block in this case, rather than an invalid one (with a nil body), which then allows callers to do careful partial analysis without tripping over this nil. However, some additional care within Terraform itself may also be warranted here. We'll use this issue to track the Terraform-facing problem, though the bulk of the fix here will probably be over in the HCL2 repository.

(The above stack trace came from Terraform v0.12.0-alpha2.)

ghost commented 4 years ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.