gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.01k stars 971 forks source link

Type error with sops_decrypt_file and yamldecode #2103

Open antoinerrr opened 2 years ago

antoinerrr commented 2 years ago

Hi Terraform v1.1.8 Terragrunt v0.36.6

I have a strange error where yamldecode is not using the good type for a data decrypted with sops_decrypt_file (data is a str, but try to decode int):

ERRO[0000]    8:     yamldecode(sops_decrypt_file(find_in_parent_folders("variables/secrets.enc.yml")))
ERRO[0000]
ERRO[0000] Call to function "yamldecode" failed: on line 2, column 27: cannot parse
"0xeeeeeeeee" as
tag:yaml.org,2002:int.

0xeeeeeeeee is in my case an hexa string (I think the 0x at the beginning is causing the issue)

File before sops encode:

secrets:
  SOME SECRET: "0xeeeeeee" 

File after sops encode:

secrets:
    SECRET: ENC[AES256_GCM,data:MY_ENCODED_DATA=,tag:71+xxxxx==,type:str]
sops:
   *sops config stuff*

As you can see the data is tagged as a string.

So I don't know if the issue is with the terra grunt function sops_decrypt_file who maybe transform my data to an int or if the issue is with the terraform function yamldecode who just do not understand string starting with 0x

Thank you

denis256 commented 2 years ago

Hi, does this issue occur only when terragrunt tries to decrypt files? the file will be decrypted successfully with the sops command?

antoinerrr commented 2 years ago

Hi, Yes the sops -d command work as expected and return a proper yaml.

denis256 commented 2 years ago

Hi, I tested on v0.36.6 and on v0.37.1 and didn't get mentioned error, yaml files got decrypted and passed as variables

Full example in: https://github.com/denis256/terragrunt-tests/tree/master/issue-2103

antoinerrr commented 2 years ago

Hi @denis256 Sorry if my exemple wasn't clear, "0xeee" was just a place holder for the real value. You can test it again but with any blockchain contract/address you want to have the error, for exemple you can try with the value: "0x629A673A8242c2AC4B7B8C5D8735fbeac21A6205"

Thank you

denis256 commented 2 years ago

Hi, this helped to get the same error :+1:

after more tests I found that the issue is with value returned from sops decryption function which didn't return value in quotes, this can be also observed using CLI sops:

$ cat file.yaml 
secrets:
  SOME SECRET: "0x629A673A8242c2AC4B7B8C5D8735fbeac21A6205"

$ sops --encrypt ... file.yaml > file.enc.yaml
$ sops -d file.enc.yaml

secrets:
    SOME SECRET: 0x629A673A8242c2AC4B7B8C5D8735fbeac21A6205

(quotes got removed...)

This is a known issue in sops and go-yaml:

https://github.com/mozilla/sops/issues/1003 https://github.com/go-yaml/yaml/issues/435

To overcome this issue in terragrunt, I was thinking about:

antoinerrr commented 2 years ago

Thanks for the help, option 1 is what I ended up doing, not super clean but do the job.