chriskuchin / terraform-provider-hiera5

MIT License
10 stars 3 forks source link

Terraform run fails when an empty yaml file is included #56

Open fdammeke opened 10 months ago

fdammeke commented 10 months ago

Hi,

first off: thanks for this awesome implementation!

Issue: Sometimes I declare 'empty' .yaml files in my gitops project as a placeholder, the provider panics when reading those. example yaml file content:

---

Environment:

Terraform v1.5.7
on darwin_amd64
+ provider registry.terraform.io/chriskuchin/hiera5 v0.5.4

Solution: Would it be possible not to crash on an empty yaml file, even though it contains valid syntax?

Reproducable:

Stack trace from the terraform-provider-hiera5_v0.5.4 plugin:

panic: file 'hiera-data/environment/dev/keyvault.yaml' does not contain a YAML hash

goroutine 29 [running]:
github.com/lyraproj/hiera/provider.YamlData({0x48e2f968?, 0xc000438140?})
        github.com/lyraproj/hiera@v0.4.6/provider/yamldata.go:36 +0x133
... <snip> ...
github.com/lyraproj/hiera/hiera.DoWithParent({0x19cde80?, 0x1f5d5c0?}, 0xc0004227c0?, {0x189a720?, 0xc000326a40?}, 0xc000191020)
        github.com/lyraproj/hiera@v0.4.6/hiera/hiera.go:204 +0x8f
github.com/chriskuchin/terraform-provider-hiera5/hiera5/helper.Lookup({0x19cdda0, 0xc00050f3b0}, {0xc00017a1b6, 0xa}, {0xc00017a1d8, 0x4}, {0xc000312570, 0x4}, {0x0, 0x0}, ...)
        github.com/chriskuchin/terraform-provider-hiera5/hiera5/helper/lyraproj.go:65 +0x7cb
github.com/chriskuchin/terraform-provider-hiera5/hiera5.(*hiera5).lookup(0xc00013db00?, {0x19cdda0?, 0xc00050f3b0?}, {0xc000312570, 0x4}, {0x0?, 0xc00019e210?})
        github.com/chriskuchin/terraform-provider-hiera5/hiera5/hiera5.go:55 +0x7b
github.com/chriskuchin/terraform-provider-hiera5/hiera5.(*hiera5).json(0xc00050f440, {0x19cdda0, 0xc00050f3b0}, {0xc000312570, 0x4}, {0xc000191308, 0x1, 0xc00013db00?})
        github.com/chriskuchin/terraform-provider-hiera5/hiera5/hiera5.go:143 +0xb0
github.com/chriskuchin/terraform-provider-hiera5/hiera5.(*Hiera5JSONDataSource).Read(0xc00050f440, {0x19cdda0, 0xc00050f3b0}, {{{{0x19d3078, 0xc00019e210}, {0x17a5d20, 0xc00019e150}}, {0x19d6bc0, 0xc00013db00}}, {{{0x0, ...}, ...}, ...}}, ...)
        github.com/chriskuchin/terraform-provider-hiera5/hiera5/data_source_hiera5_json.go:76 +0x26a
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).ReadDataSource(0xc000103080, {0x19cdda0, 0xc00050f3b0}, 0xc00050f470, 0xc000191650)
        github.com/hashicorp/terraform-plugin-framework@v1.4.0/internal/fwserver/server_readdatasource.go:79 +0x433

Error: The terraform-provider-hiera5_v0.5.4 plugin crashed!
chriskuchin commented 10 months ago

Unfortunately I actually bumped into this a long time ago and the error is coming from the underlying hiera library.

https://github.com/lyraproj/hiera/blob/master/provider/yamldata.go

I am not sure why the system panics and that library hasn't been updated in years....

From what I can see patching it will require forking that library. I may look at it sometime cause it annoys me too but honestly no promises.

You can work around it by putting just a random key value in the file. Doesn't even have to be something you use.

hiera: true

Or other to prevent the panic