diff --git a/pkg/parser/terraform/converter/default.go b/pkg/parser/terraform/converter/default.go index 3528a30cfc4..788cfe5aab3 100644 --- a/pkg/parser/terraform/converter/default.go +++ b/pkg/parser/terraform/converter/default.go @@ -365,6 +365,8 @@ func (c *converter) convertStringPart(expr hclsyntax.Expression) (string, error) return c.convertTemplateConditional(v) case *hclsyntax.TemplateJoinExpr: return c.convertTemplateFor(v.Tuple.(*hclsyntax.ForExpr)) + case *hclsyntax.ParenthesesExpr: + return c.convertStringPart(v.Expression) default: // try to evaluate with variables valueConverted, _ := expr.Value(&hcl.EvalContext{ diff --git a/pkg/parser/terraform/terraform_test.go b/pkg/parser/terraform/terraform_test.go index 489e278a8a1..20dc285dc0b 100644 --- a/pkg/parser/terraform/terraform_test.go +++ b/pkg/parser/terraform/terraform_test.go @@ -1,13 +1,14 @@ package terraform import ( - "github.com/Checkmarx/kics/pkg/parser/terraform/converter" - "github.com/hashicorp/hcl/v2" "path/filepath" "reflect" "strings" "testing" + "github.com/Checkmarx/kics/pkg/parser/terraform/converter" + "github.com/hashicorp/hcl/v2" + "github.com/Checkmarx/kics/pkg/model" "github.com/stretchr/testify/require" ) @@ -45,6 +46,24 @@ resource "aws_s3_bucket" "b" { subnet_id = var.subnet_ids[count.index] }` + + parentheses = ` +variable "default" { + type = "string" + default = "default_var_file" +} + +data "aws_ami" "example" { + most_recent = true + + owners = ["self"] + tags = { + Name = "app-server" + Tested = "true" + ("Tag/${var.default}") = "test" + } +} + ` ) type fileTest struct { @@ -96,6 +115,18 @@ func Test_Count(t *testing.T) { require.NotContains(t, document[0]["resource"].(model.Document)["aws_instance"], "server") } +// Test_Parentheses_Expr tests if parentheses expr is well parsed +func Test_Parentheses_Expr(t *testing.T) { + parser := NewDefault() + getInputVariables(filepath.FromSlash("../../../test/fixtures/test-tf-parentheses")) + document, _, err := parser.Parse("parentheses.tf", []byte(parentheses)) + require.NoError(t, err) + require.Len(t, document, 1) + require.Contains(t, document[0], "data") + ami := document[0]["data"].(model.Document)["aws_ami"].(model.Document)["example"] + require.Contains(t, ami.(model.Document)["tags"], "Tag/default_var_file") +} + // Test_Resolve tests the functions [Resolve()] and all the methods called by them func Test_Resolve(t *testing.T) { parser := NewDefault() diff --git a/test/fixtures/test-tf-parentheses/parenteses.tf b/test/fixtures/test-tf-parentheses/parenteses.tf new file mode 100644 index 00000000000..dfc88e34064 --- /dev/null +++ b/test/fixtures/test-tf-parentheses/parenteses.tf @@ -0,0 +1,15 @@ +variable "default" { + type = "string" + default = "default_var_file" +} + +data "aws_ami" "example" { + most_recent = true + + owners = ["self"] + tags = { + Name = "app-server" + Tested = "true" + ("Tag/${var.default}") = "test" + } +}