这是indexloc提供的服务,不要输入任何密码
Skip to content

terraform: IAM resources using for_each are not correctly scanned #7337

@piax93

Description

@piax93

Describe the issue
Many checks use the Cloudsplaining library to parse IAM policies, and there is a caching mechanism to avoid parsing again the same policy multiple times, as that can be time consuming. The cache uses entity_path as the key, which is derived from the un-indexed name of each terraform resource.
This means that if two or more IAM resources are defined in with count or for_each, which will make them have the same name, only the first one will be loaded, and the rest will be skipped, with the scanner rerunning again on the same cached policy code.

Examples
Example configuration that would trigger the bug:

resource "aws_iam_policy" "policy" {
  for_each = toset(["policy1.json", "policy2.json"])

  name   = replace(each.value, ".json", "")
  path   = "/"
  policy = file(each.value)
}

as this would create two policies with addresses aws_iam_policy.policy["policy1.json"] and aws_iam_policy.policy["policy2.json"], but only the first one would be loaded and scanned since the entity path would only include aws_iam_policy.policy.

Version:

  • Checkov Version 3.2.483

Additional context
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    checksCheck additions or changes

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions