Fix problems with .tfvars value referencing
Fixes:
MODIFY
.gitignore
.gitignore
@@ -9,7 +9,6 @@ __pycache__/
# Terraform
*.tfstate*
*.terraform*
-*.tfvars
*.tfbackend
# git

MODIFY
checkov/terraform/parser.py
checkov/terraform/parser.py
@@ -211,7 +211,7 @@ class Parser:
if hcl_tfvars: # terraform.tfvars
data = _load_or_die_quietly(hcl_tfvars, self.out_parsing_errors)
if data:
- var_value_and_file_map.update({k: (v, hcl_tfvars.path) for k, v in data.items()})
+ var_value_and_file_map.update({k: (v[0], hcl_tfvars.path) for k, v in data.items()})
if json_tfvars: # terraform.tfvars.json
data = _load_or_die_quietly(json_tfvars, self.out_parsing_errors)
if data:
@@ -730,7 +730,8 @@ Load JSON or HCL, depending on filename.
def _clean_bad_definitions(tf_definition_list):
return {
block_type: list(filter(lambda definition_list: block_type == 'locals' or
- len(definition_list.keys()) == 1, tf_definition_list[block_type]))
+ not isinstance(definition_list, dict)
+ or len(definition_list.keys()) == 1, tf_definition_list[block_type]))
for block_type in tf_definition_list.keys()
}

ADD
tests/common/runner_registry/example_multi_iac/tf/terraform.tfvars
+VERSIONING = true
\ No newline at end of file

ADD
tests/terraform/parser/resources/parser_scenarios/tfvars/main.tf
+variable "foo" {}
+
+resource "aws_s3_bucket" "my_bucket" {
+ bucket = var.foo
+}
\ No newline at end of file

MODIFY
tests/terraform/parser/test_hcl2_load_assumptions.py
tests/terraform/parser/test_hcl2_load_assumptions.py
@@ -7,6 +7,17 @@ import hcl2
# This group of tests is used to confirm assumptions about how the hcl2 library parses into json.
# We want to make sure important assumptions are caught if behavior changes.
class TestHCL2LoadAssumptions(unittest.TestCase):
+ def test_tfvars(self):
+ tf = '''
+ VERSIONING = true
+ CHECKOV = "awesome"
+ '''
+ expect = {
+ "VERSIONING": [True],
+ "CHECKOV": ["awesome"]
+ }
+ self.go(tf, expect)
+
def test_multiline_function(self):
tf = '''
locals {

MODIFY
tests/terraform/parser/test_parser_scenarios.py
tests/terraform/parser/test_parser_scenarios.py
@@ -105,6 +105,9 @@ class TestParserScenarios(unittest.TestCase):
def test_json_807(self):
self.go("json_807")
+ def test_tfvars(self):
+ self.go("tfvars")
+
@staticmethod
def go(dir_name):
dir_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),

Fix problems with .tfvars value referencing

Fixes: #865