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(kustomize): Add origin annotations to calculate bases of kustomize checks #5298

Merged
merged 29 commits into from
Jul 10, 2023

Conversation

bo156
Copy link
Contributor

@bo156 bo156 commented Jul 6, 2023

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

Description

This PR allows adding buildMetadata: [originAnnotations] to kustomization.yaml files, which enables calculating the origin base which created the resource when running kustomize checks.
This feature edits the kustomization.yaml files for the user, so it is hidden behind the env variable ALLOW_KUSTOMIZE_FILE_EDITS

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

@bo156 bo156 temporarily deployed to scan-security July 6, 2023 11:29 — with GitHub Actions Inactive
@bo156 bo156 force-pushed the feature/kustomize-origin-annotations branch from 10e9411 to c572d37 Compare July 6, 2023 11:34
@bo156 bo156 temporarily deployed to scan-security July 6, 2023 11:38 — with GitHub Actions Inactive
@bo156 bo156 temporarily deployed to scan-security July 6, 2023 12:41 — with GitHub Actions Inactive
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.

the 2 _get_caller_... methods are messy, but we have to do some messy path manipulation stuff 😢

checkov/common/util/env_vars_config.py Outdated Show resolved Hide resolved
checkov/kustomize/runner.py Outdated Show resolved Hide resolved
checkov/kustomize/runner.py Outdated Show resolved Hide resolved
checkov/kustomize/runner.py Outdated Show resolved Hide resolved
Comment on lines 521 to 527
proc = subprocess.Popen(full_command.split(' '), cwd=filePath, stdout=subprocess.PIPE, stderr=subprocess.PIPE) # nosec
output, _ = proc.communicate()

if env_vars_config.ALLOW_KUSTOMIZE_FILE_EDITS and add_origin_annotations_return_code == 0:
# If the return code is not 0, we didn't add the new buildmetadata field, so we shouldn't remove it
remove_origin_annotaions = 'kustomize edit remove buildmetadata originAnnotations'
subprocess.Popen(remove_origin_annotaions.split(' '), cwd=filePath).wait() # nosec
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure, why we use Popen better to use run instead as recommend by Python, at least for the second call we can switch to run

Copy link
Contributor Author

Choose a reason for hiding this comment

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

tbh I just used popen as that's what the function already used. will do it now

Co-authored-by: Anton Grübel <[email protected]>
@bo156 bo156 temporarily deployed to scan-security July 9, 2023 16:16 — with GitHub Actions Inactive
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.

🙂

@bo156 bo156 temporarily deployed to scan-security July 10, 2023 07:54 — with GitHub Actions Inactive
resource=kustomizeResourceID, evaluations=variable_evaluations,
check_class=check.__class__.__module__, file_abs_path=absolute_file_path, severity=check.severity)
record.set_guideline(check.guideline)
report.add_record(record=record)

return report

def _get_caller_file_info(self, entity_context: _EntityContext, k8_file: str, k8_file_path: str, resource_id: str,
root_folder: str | None) -> tuple[tuple[int, int] | None, str | None]:
origin_relative_path = entity_context['origin_relative_path']
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
origin_relative_path = entity_context['origin_relative_path']
origin_relative_path = entity_context.get('origin_relative_path')

@bo156 bo156 force-pushed the feature/kustomize-origin-annotations branch from b9c8999 to e5c71a5 Compare July 10, 2023 11:42
@bo156 bo156 temporarily deployed to scan-security July 10, 2023 11:42 — with GitHub Actions Inactive
@bo156 bo156 merged commit 40e3626 into main Jul 10, 2023
32 checks passed
@bo156 bo156 deleted the feature/kustomize-origin-annotations branch July 10, 2023 11:57
bo156 added a commit that referenced this pull request Jul 10, 2023
…ases of… (#5312)

Revert "feat(kustomize): Add origin annotations to calculate bases of kustomize checks (#5298)"

This reverts commit 40e3626.
gruebel added a commit that referenced this pull request Jul 10, 2023
…uild github action to use github runners (#5316)

* feat(kustomize): Add origin annotations to calculate bases of kustomize checks (#5298)

* Added basic implementation of adding and removing buildmetadata originAnnotations

* Only remove the new buildmetadata if it wasn't added successfully by checkov

* middle of debugging

* more improvements

* Successfully updated the file path

* middle of adding correct base code lines

* Fixed calculation of caller file path

* another debug way

* Another addition

* Fixed bug where we calculate the base for overlay incorrectly, causing us to copy overlays to wrong directory

* Calculated relative directory to new base

* Allowed empty caller file lines and fixed all kustomize tests

* Added tests for caller file paths

* Added support for caller file path in graph reports and updated tests accordingly

* Fixed parsing of kustomization files with resources which actually represent overlays

* Added env variable to allow kustomize file edits

* Removed comment

* Updated _get_caller_file_path signature

* Linters

* nosec on subprocess.Popen as we are incharge of the command

* Added condition that annotations are not None

* CR

* find correct parent without loop

Co-authored-by: Anton Grübel <[email protected]>

* don't check len

Co-authored-by: Anton Grübel <[email protected]>

* Used subprocess.run

* moved env var to another place in the instance

* Fixed all tests:

* Check for origin_relative_path before concat

* Added documentation

---------

Co-authored-by: Anton Grübel <[email protected]>

* Moved build job unit tests to use public images instead of self-hosted, as a bug exist in the hosted version

---------

Co-authored-by: Anton Grübel <[email protected]>
Teko012 pushed a commit to Teko012/checkov_fork_bridgecrewio that referenced this pull request Jul 11, 2023
…ases of… (bridgecrewio#5312)

Revert "feat(kustomize): Add origin annotations to calculate bases of kustomize checks (bridgecrewio#5298)"

This reverts commit 40e3626.
Teko012 pushed a commit to Teko012/checkov_fork_bridgecrewio that referenced this pull request Jul 11, 2023
…uild github action to use github runners (bridgecrewio#5316)

* feat(kustomize): Add origin annotations to calculate bases of kustomize checks (bridgecrewio#5298)

* Added basic implementation of adding and removing buildmetadata originAnnotations

* Only remove the new buildmetadata if it wasn't added successfully by checkov

* middle of debugging

* more improvements

* Successfully updated the file path

* middle of adding correct base code lines

* Fixed calculation of caller file path

* another debug way

* Another addition

* Fixed bug where we calculate the base for overlay incorrectly, causing us to copy overlays to wrong directory

* Calculated relative directory to new base

* Allowed empty caller file lines and fixed all kustomize tests

* Added tests for caller file paths

* Added support for caller file path in graph reports and updated tests accordingly

* Fixed parsing of kustomization files with resources which actually represent overlays

* Added env variable to allow kustomize file edits

* Removed comment

* Updated _get_caller_file_path signature

* Linters

* nosec on subprocess.Popen as we are incharge of the command

* Added condition that annotations are not None

* CR

* find correct parent without loop

Co-authored-by: Anton Grübel <[email protected]>

* don't check len

Co-authored-by: Anton Grübel <[email protected]>

* Used subprocess.run

* moved env var to another place in the instance

* Fixed all tests:

* Check for origin_relative_path before concat

* Added documentation

---------

Co-authored-by: Anton Grübel <[email protected]>

* Moved build job unit tests to use public images instead of self-hosted, as a bug exist in the hosted version

---------

Co-authored-by: Anton Grübel <[email protected]>
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