Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add validation on var.hieradata #269

Closed
cmd-ntrf opened this issue Nov 16, 2023 · 3 comments · Fixed by #284
Closed

Add validation on var.hieradata #269

cmd-ntrf opened this issue Nov 16, 2023 · 3 comments · Fixed by #284
Assignees
Labels
enhancement New feature or request

Comments

@cmd-ntrf
Copy link
Member

If the yaml input provided in var.hieradata is invalid, Puppet will be unable to execute properly. We can prevent injecting invalid yaml by trying to first decode it using yamldecode in the validation block of var.hieradata.

  validation {
    condition     = can(yamldecode(var.hieradata))
    error_message = "Hieradata needs to be valid YAML"
  }
@cmd-ntrf cmd-ntrf added the enhancement New feature or request label Nov 16, 2023
@cmd-ntrf cmd-ntrf self-assigned this Nov 16, 2023
@cmd-ntrf
Copy link
Member Author

We can also validate the YAML content on the Puppet server before the user_data.yaml file is copied.
https://github.com/ComputeCanada/magic_castle/blob/main/common/provision/main.tf#L48

@etiennedub
Copy link
Contributor

@cmd-ntrf I try your first suggestion but I'm not sure that it's enough. Almost everything can be convert to yaml with yamlencode (string, random text file, json object). However, it will catch resources pass to the variable and empty string.

For example, this is valid:

> yamlencode(file("bad.yaml"))
<<EOT
|
  this is not a yaml file
  a=>2
  $i : 42

EOT

Do you think we should get a better validation?

@etiennedub
Copy link
Contributor

Forget my last comment, I realized that it was yamldecode instead of yamlencode. With yamldecode, it catches issues with invalid files. It's actually a good solution. I will send the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants