Open simar7 opened 4 months ago
@simar7 I'll divide this into 3 tasks.
count
and each
objects, but we decided not to add that. It would be easy to add it. https://github.com/aquasecurity/trivy/pull/6302/commits/835351defb6280f93cae61386d616525123f328efoo
and bar
, which have resources with the same logical path. Maybe we should add some kind of entrypoint field? The path field for a resource makes no sense if it is in a remote module. Only the logical path is enough.The first point has already been implemented. What about 2 and 3?
Maybe we should add some kind of entrypoint field?
This sounds like it needs uniqueness right? What if we add the root module itself as the entry point? What other options do we have?
In order to display the expanded block in the report, we need to modify the HCL file, overwrite the file in the file system, and update the finding metadata to refer to the new location.
Actually as I thought of this more, expanding HCL and displaying has another problem of shifting indexes. This may cause the output to be indeterministic. I see two ways here that we can go:
count
or for_each
types.count
and for_each
types. This is similar to the idea we had about "similar results" but a little less involved as only results in a single loop are labeled together. We can think of a way to output this meaningfully. Maybe something like the following:CRITICAL: Network ACL rule allows ingress from public internet.
Opening up ACLs to the public internet is potentially dangerous. You should restrict access to IP addresses or ranges that explicitly require it where possible.
See https://avd.aquasec.com/misconfig/avd-aws-0105
terraform-aws-modules/vpc/aws/.terraform/modules/vpc/main.tf:204
via terraform-aws-modules/vpc/aws/.terraform/modules/vpc/main.tf:191-206 (aws_network_acl_rule.public_inbound[13])
via vpc.tf:55-250 (module.vpc)
+ and 6 similar results originating from
191 resource "aws_network_acl_rule" "public_inbound" {
...
204 [ cidr_block = lookup(var.public_inbound_acl_rules[count.index], "cidr_block", null)
...
206 }
Just an idea, I'm open to other ideas as well.
I think the 3rd point is not about grouping results (we already have an issue for that). Right now, the user needs to evaluate the attribute values themselves to determine which parameters caused the misconfiguration in one of the resource instances created with for-each or count.
For example, in the following expression cidr_block = lookup(var.public_inbound_acl_rules[count.index], “cidr_block”, null)
, you must first define an index, then look up the rule at that index to find out which specific CIDR
caused the incorrect setting. If the attribute expression is more complex, this complicates the process of analyzing and identifying the cause.
If we handle the order of indexes to be deterministic always (sort), what if we build the HCL in memory? We can omit line numbers from such a case (or use the ones from original file). The only difference in this output compared to what we have will be the populated indexes and values, rest should remain the same.
We could limit this to a certain extent maybe by only expanding for a limited set of indexes so we don't run into memory issues since we will have to do this all in memory.
Will this be displayed next to the code snippet?
...
terraform-aws-modules/vpc/aws/.terraform/modules/vpc/main.tf:204
via terraform-aws-modules/vpc/aws/.terraform/modules/vpc/main.tf:191-206 (aws_network_acl_rule.public_inbound[13])
via vpc.tf:55-250 (module.vpc)
191 resource "aws_network_acl_rule" "public_inbound" {
...
204 [ cidr_block = lookup(var.public_inbound_acl_rules[count.index], "cidr_block", null)
...
206 }
Rendered:
resource "aws_network_acl_rule" "public_inbound" {
...
[ cidr_block = "172.16.0.0/24"
...
}
If so, should there be a flag to disable this? I don't think it will affect memory if we only do it for findings. We can use the hclwrite package to do this.
Will this be displayed next to the code snippet?
... terraform-aws-modules/vpc/aws/.terraform/modules/vpc/main.tf:204 via terraform-aws-modules/vpc/aws/.terraform/modules/vpc/main.tf:191-206 (aws_network_acl_rule.public_inbound[13]) via vpc.tf:55-250 (module.vpc) 191 resource "aws_network_acl_rule" "public_inbound" { ... 204 [ cidr_block = lookup(var.public_inbound_acl_rules[count.index], "cidr_block", null) ... 206 } Rendered: resource "aws_network_acl_rule" "public_inbound" { ... [ cidr_block = "172.16.0.0/24" ... }
If so, should there be a flag to disable this? I don't think it will affect memory if we only do it for findings. We can use the hclwrite package to do this.
Yes, that looks good to me. If memory isn't an issue, why should we have the need to disable this? What if we only display the rendered output and not the actual code snippet? Like I said, we could omit line numbers or omit them altogether if they don't line up well with the rendered output.
We could keep it under a feature flag (disabled by default). It could be a similar to the --trace
flag we have today since it really only helps users debug the origin of the misconfiguration.
I think the code snippet is still needed to match between the finding and the source code. It doesn't make sense for the rendered output to have line numbers, since the expression may take several lines (e.g. function call or grouping with parentheses), but the value will have one line.
TODO:
for_each
orcount
variables. This would allow to target specific resources such as described in the example below.Discussed in https://github.com/aquasecurity/trivy/discussions/7157