BREAKING CHANGE: Dropping support for Terraform plans in JSON input #5950
Replies: 3 comments 3 replies
-
@nikpivkin as I was further researching this, I thought of the idea to convert the input JSON back into HCL and then to parse it. For example: func toHCL() error {
input, err := ioutil.ReadAll(os.Stdin)
if err != nil {
return fmt.Errorf("unable to read from stdin: %s", err)
}
ast, err := jsonParser.Parse([]byte(input))
if err != nil {
return fmt.Errorf("unable to parse JSON: %s", err)
}
err = printer.Fprint(os.Stdout, ast)
if err != nil {
return fmt.Errorf("unable to print HCL: %s", err)
}
return nil
} Taken from: https://github.com/kvz/json2hcl/blob/master/main.go#L62C1-L79C2 Seems to be evaluated fine as we convert back into HCL and can use You can use the below as input for testing:
resource "aws_athena_database" "good_example" {
name = "database_name"
bucket = aws_s3_bucket.hoge.bucket
encryption_configuration {
encryption_option = "SSE_KMS"
kms_key_arn = aws_kms_key.example.arn
}
} Equivalent {
"resource": {
"aws_athena_database": {
"good_example": [
{
"bucket": "${aws_s3_bucket.hoge.bucket}",
"encryption_configuration": [
{
"encryption_option": "SSE_KMS",
"kms_key_arn": "${aws_kms_key.example.arn}"
}
],
"name": "database_name"
}
]
}
}
} |
Beta Was this translation helpful? Give feedback.
-
Here is an example of a broken configuration that generates json2html: "//" "metadata" {
"backend" = "local"
"stackName" = "cdktf-eval"
"version" = "0.18.0"
}
"//" "outputs" "cdktf-eval" "tfBackend" {
"bucket" = "tfBackend_bucket_4FB869C0"
"key_id" = "tfBackend_key_id_057987E4"
"table" = "tfBackend_table_289B9B8E"
}
"output" "tfBackend_bucket_4FB869C0" {
"value" = "${aws_s3_bucket.tfBackend_TerraformBackendBucket_CC1A7B64.id}"
}
"output" "tfBackend_key_id_057987E4" {
"value" = "${aws_kms_key.tfBackend_TerraformBackendKey_F8E2509B.id}"
}
"output" "tfBackend_table_289B9B8E" {
"value" = "${aws_dynamodb_table.tfBackend_TerraformBackendLock_D9C1475E.name}"
}
"provider" = {
"aws" = {
"region" = "us-east-1"
}
}
"resource" "aws_dynamodb_table" "tfBackend_TerraformBackendLock_D9C1475E" {
"//" "metadata" {
"path" = "cdktf-eval/tfBackend/TerraformBackendLock"
"uniqueId" = "tfBackend_TerraformBackendLock_D9C1475E"
}
"attribute" = {
"name" = "LockID"
"type" = "S"
}
"billing_mode" = "PAY_PER_REQUEST"
"hash_key" = "LockID"
"name" = "terraform-backend-lock"
}
"resource" "aws_kms_alias" "tfBackend_TerraformBackendAlias_7688F893" {
"//" "metadata" {
"path" = "cdktf-eval/tfBackend/TerraformBackendAlias"
"uniqueId" = "tfBackend_TerraformBackendAlias_7688F893"
}
"name" = "alias/terraform-backend-key"
"target_key_id" = "${aws_kms_key.tfBackend_TerraformBackendKey_F8E2509B.id}"
}
"resource" "aws_kms_key" "tfBackend_TerraformBackendKey_F8E2509B" {
"//" "metadata" {
"path" = "cdktf-eval/tfBackend/TerraformBackendKey"
"uniqueId" = "tfBackend_TerraformBackendKey_F8E2509B"
}
"deletion_window_in_days" = 7
"description" = "Terraform Backend Key"
"enable_key_rotation" = true
}
"resource" "aws_s3_bucket" "tfBackend_TerraformBackendBucket_CC1A7B64" {
"//" "metadata" {
"path" = "cdktf-eval/tfBackend/TerraformBackendBucket"
"uniqueId" = "tfBackend_TerraformBackendBucket_CC1A7B64"
}
"acl" = "private"
"server_side_encryption_configuration" "rule" "apply_server_side_encryption_by_default" {
"kms_master_key_id" = "${aws_kms_key.tfBackend_TerraformBackendKey_F8E2509B.arn}"
"sse_algorithm" = "aws:kms"
}
"versioning" = {
"enabled" = true
"mfa_delete" = false
}
}
"resource" "aws_s3_bucket_public_access_block" "tfBackend_TerraformBackendBucketPublicAccessBlock_E076B36D" {
"//" "metadata" {
"path" = "cdktf-eval/tfBackend/TerraformBackendBucketPublicAccessBlock"
"uniqueId" = "tfBackend_TerraformBackendBucketPublicAccessBlock_E076B36D"
}
"block_public_acls" = true
"block_public_policy" = true
"bucket" = "${aws_s3_bucket.tfBackend_TerraformBackendBucket_CC1A7B64.id}"
"ignore_public_acls" = true
"restrict_public_buckets" = true
}
"terraform" "backend" "local" {
"path" = "/Users/tososomaru/projects/trivy-test/issue-5080/terraform.cdktf-eval.tfstate"
}
"terraform" "required_providers" "aws" {
"source" = "aws"
"version" = "5.17.0"
} What the provider configuration block should look like: provider "google" {
project = "acme-app"
region = "us-central1"
}
Perhaps we can pre-clear the json of comments and unnecessary blocks like terrafrom. |
Beta Was this translation helpful? Give feedback.
-
As explained here we have found a way to solve this problem, without deprecating the support for terraform plan. |
Beta Was this translation helpful? Give feedback.
-
This is a breaking change.
Description
As highlighted in the issue, we will be dropping support for the timebeing for terraform plan scanning that's passed in via JSON.
Motivation
We've run into several occasions (see linked issues below) where we incorrectly flag (false positive) misconfigurations in Terraform scanning when the input is the Terraform plan in JSON. This issue takes place as we're unable to parse nested blocks and attributes past the first stage as currently there's no way to "walk" the JSON input. See the more on this hashicorp/hcl#543
In other words, we currently are unable to parse any nested attributes that are actually defined within a Terraform Plan in JSON input.
Action items
Drop support for Terraform JSON until we have a proper way to walk the input. Flagging false positives creates misinformation.
Timeline
As for the timeline, we will commence this change in version v0.50.0 of Trivy at this time. That's roughly 2 months from the time of posting this discussion.
We welcome any questions that you may have on this. Please share your feedback, questions, concerns or suggestions regarding this change. We're open to hearing from you all and we apologize for the inconvenience that this may cause.
Affected issues
#5080
#5894
#5081
#5099
References
Beta Was this translation helpful? Give feedback.
All reactions