From 1ade59ff3f8741c786f4c892d2be9bd500c758e7 Mon Sep 17 00:00:00 2001 From: rafaela-soares Date: Fri, 12 Aug 2022 12:52:29 +0100 Subject: [PATCH 1/2] fixed hclsyntax parentheses expr panic --- pkg/parser/terraform/converter/default.go | 2 + pkg/parser/terraform/terraform_test.go | 37 ++++++++++++++++++- .../test-tf-parentheses/parenteses.tf | 15 ++++++++ 3 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 test/fixtures/test-tf-parentheses/parenteses.tf 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..17a704d819e 100644 --- a/pkg/parser/terraform/terraform_test.go +++ b/pkg/parser/terraform/terraform_test.go @@ -1,13 +1,15 @@ package terraform import ( - "github.com/Checkmarx/kics/pkg/parser/terraform/converter" - "github.com/hashicorp/hcl/v2" + "fmt" "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 +47,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 +116,19 @@ 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)) + fmt.Println(document) + 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" + } +} From 46f12fbc98fcde0ebb7ea78d55d77d76639d1fbf Mon Sep 17 00:00:00 2001 From: rafaela-soares Date: Fri, 12 Aug 2022 12:56:12 +0100 Subject: [PATCH 2/2] remove print --- pkg/parser/terraform/terraform_test.go | 2 -- 1 file changed, 2 deletions(-) diff --git a/pkg/parser/terraform/terraform_test.go b/pkg/parser/terraform/terraform_test.go index 17a704d819e..20dc285dc0b 100644 --- a/pkg/parser/terraform/terraform_test.go +++ b/pkg/parser/terraform/terraform_test.go @@ -1,7 +1,6 @@ package terraform import ( - "fmt" "path/filepath" "reflect" "strings" @@ -121,7 +120,6 @@ 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)) - fmt.Println(document) require.NoError(t, err) require.Len(t, document, 1) require.Contains(t, document[0], "data")