Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 2 additions & 2 deletions pkg/parser/terraform/data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,9 +122,9 @@ func getDataSourcePolicy(ctx context.Context, currentPath string, inputVariables

func decodeDataSourcePolicy(ctx context.Context, value cty.Value) dataSourcePolicy {
contextLogger := logger.FromContext(ctx)
jsonified, err := ctyjson.Marshal(value, cty.DynamicPseudoType)
jsonified, err := ctyjson.Marshal(cty.UnknownAsNull(value), cty.DynamicPseudoType)
if err != nil {
contextLogger.Error().Msgf("Error trying to decode data source block: %s", err)
contextLogger.Warn().Msgf("Error trying to decode data source block: %s", err)
return dataSourcePolicy{}
}
var data dataSource
Expand Down
9 changes: 9 additions & 0 deletions pkg/parser/terraform/data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,15 @@ func Test_getDataSourcePolicy(t *testing.T) {
resourceName: "test_example",
},
want: `{"Id":"lala","Statement":[{"Actions":["s3:ListAllMyBuckets","s3:GetBucketLocation"],"Resources":["arn:aws:s3:::*"],"Sid":"1"},{"Actions":["s3:ListBucket"],"Condition":{"StringLike":{"s3:prefix":["","home/","home/&{aws:username}/"]}},"Resources":["arn:aws:s3:::test"]},{"Actions":["s3:*"],"Resources":["arn:aws:s3:::test/home/&{aws:username}","arn:aws:s3:::test/home/&{aws:username}/*"]}]}
`,
},
{
name: "should not drop policy when scalar fields reference unknown variables",

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exercise declared variables through the parser path

This regression case passes an empty VariableMap to getDataSourcePolicy in the test, so the new variable "sid_value" {} fixture is never loaded. In production Parser.Resolve calls getInputVariables first, which installs a var object even when the variable has no default; with that real input, sid = var.sid_value produces an Unsupported attribute diagnostic and parseDataSourceBody returns "" before UnknownAsNull runs. Please drive this case through Resolve/getInputVariables or pass the same map so the scanner actually preserves policies with defaultless variables.

Useful? React with 👍 / 👎.

args: args{
currentPath: filepath.Join("..", "..", "..", "test", "fixtures", "test_terraform_data_source_unknown_vars"),
resourceName: "partial_unknowns",
},
want: `{"Statement":[{"Actions":["s3:GetObject"],"Effect":"Allow","Resources":["arn:aws:s3:::my-bucket/*"]}]}
`,
},
}
Expand Down
16 changes: 16 additions & 0 deletions test/fixtures/test_terraform_data_source_unknown_vars/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
variable "sid_value" {}

data "aws_iam_policy_document" "partial_unknowns" {
statement {
effect = "Allow"
sid = var.sid_value

actions = [
"s3:GetObject",
]

resources = [
"arn:aws:s3:::my-bucket/*",
]
}
}
Loading