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

fix(terraform_plan): Add provisioners to TF Plan parser #5622

Merged
merged 6 commits into from
Oct 22, 2023
Merged

Conversation

tsmithv11
Copy link
Collaborator

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

Description

Add provisioners to TF Plan parser to support local-exec policies

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

@tsmithv11 tsmithv11 temporarily deployed to scan-security October 5, 2023 19:41 — with GitHub Actions Inactive
@tsmithv11 tsmithv11 requested a review from gruebel October 5, 2023 19:50
Comment on lines 169 to 171
provisioners = conf.get(TF_PLAN_RESOURCE_PROVISIONERS) if conf else None
if provisioners:
resource_conf[TF_PLAN_RESOURCE_PROVISIONERS] = provisioners
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the problem with this approach is, that the provisioners in HCL looks different than in the plan file. So, a custom policy would need to cover both cases.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@gruebel good callout. I've updated it to match as best I could, but note that Terraform transforms the command and removes some of it, so it won't match completely. Now this policy:

metadata:
 name: "Test"
 id: "CKV2_CUSTOM_1"
 guidelines: "Policy description"
 category: "GENERAL_SECURITY"
scope:
  provider: "aws"
definition:
  cond_type: attribute
  resource_types:
    - aws_instance
  attribute: "provisioner/local-exec"
  operator: not_exists

Works for both TF and Plan files using all methods documented here: https://developer.hashicorp.com/terraform/language/resources/provisioners/syntax

Screenshot 2023-10-19 at 1 23 08 AM
Screenshot 2023-10-19 at 1 22 32 AM

Copy link
Contributor

Choose a reason for hiding this comment

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

sounds good

@tsmithv11 tsmithv11 temporarily deployed to scan-security October 19, 2023 08:45 — with GitHub Actions Inactive
@tsmithv11 tsmithv11 requested a review from gruebel October 19, 2023 08:53
Copy link
Contributor

@gruebel gruebel left a comment

Choose a reason for hiding this comment

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

🍻

@gruebel gruebel merged commit 59c62b7 into main Oct 22, 2023
29 checks passed
@gruebel gruebel deleted the add-provisioners branch October 22, 2023 11:35
@junhu73
Copy link

junhu73 commented Jul 26, 2024

@tsmithv11 , do you think there is bug here with item['expressions']['command'] since type is not checked and remote and file provisioners (or future provisioners) does not have the key "command" in the Terraform plan file. We are seeing this error with checkov (where checkov just exits silently and indicates nothing scanned.

Traceback (most recent call last):
File "/usr/local/lib/python3.9/dist-packages/checkov/common/parallelizer/parallel_runner.py", line 72, in func_wrapper
result = original_func(*item)
File "/usr/local/lib/python3.9/dist-packages/checkov/common/runners/runner_registry.py", line 835, in _parallel_run
report = runner.run(
File "/usr/local/lib/python3.9/dist-packages/checkov/terraform/plan_runner.py", line 118, in run
self.definitions, definitions_raw = create_definitions(root_folder, files, runner_filter, parsing_errors)
File "/usr/local/lib/python3.9/dist-packages/checkov/terraform/plan_utils.py", line 54, in create_definitions
current_tf_definitions, current_definitions_raw = parse_tf_plan(file, out_parsing_errors)
File "/usr/local/lib/python3.9/dist-packages/checkov/terraform/plan_parser.py", line 395, in parse_tf_plan
module_blocks = _find_child_modules(
File "/usr/local/lib/python3.9/dist-packages/checkov/terraform/plan_parser.py", line 258, in _find_child_modules
resource_block, block_type, prepared = _prepare_resource_block(
File "/usr/local/lib/python3.9/dist-packages/checkov/terraform/plan_parser.py", line 209, in _prepare_resource_block
resource_conf["provisioner"] = _get_provisioner(provisioners)
File "/usr/local/lib/python3.9/dist-packages/checkov/terraform/plan_parser.py", line 425, in _get_provisioner
command_value = item['expressions']['command']
KeyError: 'command'

def _get_provisioner(input_data: List[Any]) -> List[Any]:
result = []
for item in input_data:
key = item['type']
command_value = item['expressions']['command']
if not isinstance(command_value, list):
command_value = [command_value]
transformed_item = {key: {'command': command_value}}
result.append(transformed_item)
return result

@tsmithv11
Copy link
Collaborator Author

Thanks, @junhu73. Fixed here: #6606

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.

4 participants