scottwinkler / terraform-provider-shell

Terraform provider for executing shell commands and saving output to state file
Mozilla Public License 2.0
279 stars 61 forks source link

Provider crashes when script returns null as value in JSON state object #23

Closed pacon-vib closed 4 years ago

pacon-vib commented 4 years ago

Thankyou for this great provider!

When I'm running some command-line tools that return JSON objects, but some fields are sometimes filled with null, the null causes the shell provider to panic.

Here's a minimal Terraform config that will cause a panic:

resource "shell_script" "demo" {
  lifecycle_commands {
    create = "echo '{\"baz\": null}' >&3"
    delete = "true"
  }
}

Here's an extract from crash.log; I can submit more if need be:

2020-02-18T10:23:58.730+1100 [DEBUG] plugin.terraform-provider-shell_v0.1.3: panic: interface conversion: interface {} is nil, not string
2020-02-18T10:23:58.730+1100 [DEBUG] plugin.terraform-provider-shell_v0.1.3: 
2020-02-18T10:23:58.730+1100 [DEBUG] plugin.terraform-provider-shell_v0.1.3: goroutine 131 [running]:
2020-02-18T10:23:58.730+1100 [DEBUG] plugin.terraform-provider-shell_v0.1.3: github.com/scottwinkler/terraform-provider-shell/shell.parseJSON(0xc000290000, 0x2000, 0x2000, 0x1, 0x1, 0x2000)
2020-02-18T10:23:58.730+1100 [DEBUG] plugin.terraform-provider-shell_v0.1.3:    /Users/swinkler/go/src/github.com/scottwinkler/terraform-provider-shell/shell/utility.go:53 +0x2b7

Shell scripts are only supposed to return maps of strings to strings in the state, so returning null is naughty, but I would like to propose changing the behaviour to turn those nulls into empty strings, instead of panicking.

For now I'm raising an issue but my colleague is working on a pull request as well.