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

fix(tf parser): added parentheses expr to convertStringPart #5695

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions pkg/parser/terraform/converter/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down
35 changes: 33 additions & 2 deletions pkg/parser/terraform/terraform_test.go
Original file line number Diff line number Diff line change
@@ -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"
)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
Expand Down
15 changes: 15 additions & 0 deletions test/fixtures/test-tf-parentheses/parenteses.tf
Original file line number Diff line number Diff line change
@@ -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"
}
}