Parsing an input file

It can be difficult to understand the internal representation of your input files as you write your Rego code. As you will see when you learn how to write a rule, the input value is a JSON-like object, but the input files could also be YAML, Terraform, or Terraform Plan JSON Output. To help you understand how these are translated into JSON, Snyk provides a parse command.

You will need an IaC file to use as an input file. This input file can also be used when testing the rules, which parses your files into JSON by default.

Parsing Terraform files

Take, for example, the following Terraform file:

example.tf
resource "aws_redshift_cluster" "example" {
  cluster_identifier = "tf-redshift-cluster"
  database_name      = "mydb"
  master_username    = "foo"
  master_password    = "Mustbe8characters"
  node_type          = "dc1.large"
  cluster_type       = "single-node"
}

To get the equivalent JSON format, run the parse command:

snyk-iac-rules parse example.tf --format hcl2

This prints out the JSON, which you can use as guidance for writing your rules:

{
	"resource": {
		"aws_redshift_cluster": {
			"example": {
				"cluster_identifier": "tf-redshift-cluster",
				"cluster_type": "single-node",
				"database_name": "mydb",
				"master_password": "Mustbe8characters",
				"master_username": "foo",
				"node_type": "dc1.large"
			}
		}
	}
}

In Rego, accessing the node_type field would look like this:

Parsing YAML files

Another example is the following YAML file, defining a Kubernetes resource:

To get the equivalent JSON format, run the parse command:

This prints out the JSON, which you can use as guidance for writing your rules:

In Rego, accessing the privileged field would look like this:

Parsing Terraform Plan JSON output files

Another example is the following Terraform Plan JSON Output file, returned by the terraform show -json ./plan/example.json.tfplan command:

To get the equivalent JSON format, run the parse command:

This prints out the JSON, which you can use as guidance for writing your rules:

In Rego, accessing the tags field would look like this:

Last updated

Was this helpful?