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

feat(terraform): add expanded actions to Terraform AWS IAM resources #5590

Closed
wants to merge 6 commits into from

Conversation

gruebel
Copy link
Contributor

@gruebel gruebel commented Sep 24, 2023

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

Description

  • added a new metadata field extensions to support features, which modifies on how the graph checks work
  • added the extension IAM_ACTION_EXPANSION to expand wildcard IAM actions in AWS resources

Now it is possible to check for precise AWS IAM actions, even when the resource is using wildcards, ex.

check

metadata:
  name: "Ensure CreateRole and CreatePolicy are not used"
  id: "CKV2_CUSTOM_1"
  category: "IAM"
  extensions:
    - IAM_ACTION_EXPANSION
definition:
  or:
    - cond_type: "attribute"
      resource_types:
        - "data.aws_iam_policy_document"
      attribute: "statement[?(@.effect == Allow)].actions[*]"
      operator: "jsonpath_not_within"
      value:
       - "iam:CreatePolicy"
       - "iam:CreateRole"

resource

data "aws_iam_policy_document" "example" {
  statement {
    sid    = "Example"
    effect = "Allow"

    actions = [
      "iam:*",
      "ec2:*",
    ]
  }
}

result

Check: CKV2_CUSTOM_1: "Ensure something important"
	FAILED for resource: aws_iam_policy_document.example
	File: /main.tf:199-209

		199 | data "aws_iam_policy_document" "example" {
		200 |   statement {
		201 |     sid    = "Example"
		202 |     effect = "Allow"
		203 | 
		204 |     actions = [
		205 |       "iam:*",
		206 |       "ec2:*",
		207 |     ]
		208 |   }
		209 | }

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • I have added tests that prove my feature, policy, or fix is effective and works
  • New and existing tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@gruebel gruebel temporarily deployed to scan-security September 24, 2023 11:41 — with GitHub Actions Inactive
@gruebel gruebel temporarily deployed to scan-security September 24, 2023 13:04 — with GitHub Actions Inactive
@gruebel gruebel temporarily deployed to scan-security September 24, 2023 21:10 — with GitHub Actions Inactive
@gruebel gruebel temporarily deployed to scan-security September 25, 2023 08:40 — with GitHub Actions Inactive
@gruebel gruebel marked this pull request as ready for review September 25, 2023 08:43

## Using a jsonpath operator with IAM_ACTION_EXPANSION extension

The following policy will fail, when `iam:CreatePolicy` or `iam:CreateRole` is used in `aws_iam_policy_document` data block by exact name or via wildcards.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
The following policy will fail, when `iam:CreatePolicy` or `iam:CreateRole` is used in `aws_iam_policy_document` data block by exact name or via wildcards.
The following policy will fail if iam:CreatePolicy or iam:CreateRole are used directly or through wildcards (e.g., iam:* or iam:Create*) in the aws_iam_policy_document data block.

Comment on lines +492 to +493
extensions:
- IAM_ACTION_EXPANSION
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
extensions:
- IAM_ACTION_EXPANSION
extensions: # Optional as IAM_ACTION_EXPANSION is enabled by default
- IAM_ACTION_EXPANSION


Available extensions:

* IAM_ACTION_EXPANSION (enabled by default)
Copy link
Collaborator

Choose a reason for hiding this comment

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

WDYT about making it the inverse logic for this flag? IAM Action Expansion is always on unless you use extension: DISABLE_IAM_ACTION_EXPANSION?

Comment on lines +78 to +79
By enabling the IAM_ACTION_EXPANSION extension additional data is added to the `Action` field of AWS Terraform resources.
Every action with a `*` wildcard will be expanded to the actual actions to give the possibility to query easily for precise permission names.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
By enabling the IAM_ACTION_EXPANSION extension additional data is added to the `Action` field of AWS Terraform resources.
Every action with a `*` wildcard will be expanded to the actual actions to give the possibility to query easily for precise permission names.
By enabling the IAM_ACTION_EXPANSION extension, additional data is added to the `Action` field of AWS Terraform resources. Every action with a `*` wildcard will be expanded to the actual actions, making it easier to query for specific permission names.

# Conflicts:
#	Pipfile.lock
#	checkov/common/checks_infra/solvers/connections_solvers/base_connection_solver.py
#	checkov/common/graph/checks_infra/registry.py
@gruebel gruebel temporarily deployed to scan-security October 6, 2023 13:33 — with GitHub Actions Inactive
Copy link

stale bot commented Apr 11, 2024

Thanks for contributing to Checkov! We've automatically marked this issue as stale to keep our issues list tidy, because it has not had any activity for 6 months. It will be closed in 14 days if no further activity occurs. Commenting on this issue will remove the stale tag. If you want to talk through the issue or help us understand the priority and context, feel free to add a comment or join us in the Checkov slack channel at codifiedsecurity.slack.com
Thanks!

@stale stale bot added the stale label Apr 11, 2024
Copy link

stale bot commented Apr 27, 2024

Closing issue due to inactivity. If you feel this is in error, please re-open, or reach out to the community via slack: codifiedsecurity.slack.com Thanks!

@stale stale bot closed this Apr 27, 2024
@tsmithv11 tsmithv11 reopened this Apr 29, 2024
@stale stale bot removed the stale label Apr 29, 2024
@pazbechor
Copy link
Contributor

@gruebel
Closing this one as conflicts exists & inactivity :)
Open it in the future if it's still relevant!

@pazbechor pazbechor closed this Jul 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants