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(github): bug fixes in CKV_GITHUB_6, CKV_GITHUB_7, CKV_GITHUB_9 #3605

Merged
merged 8 commits into from
Oct 3, 2022

Conversation

marynaKK
Copy link
Contributor

@marynaKK marynaKK commented Oct 3, 2022

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

Description

Fix of couple bugs in GitHub checks

Description

The bugs:

  1. The route for repo under org should be different from that with no org, that is repo under a repo owner.
  2. The reply for repo webhooks can be parsed also as an org webhook reply, causing the same webhook to appear under org webhook and repo webhook, which is double in 2 different checks.
  3. The github_conf files are created on demand, hence causing leftovers from the previous runs - and mixing check results between different runs.

The fix:

  1. Use the GITHUB_REPO_OWNER to access webhooks/collaborators for a repo, instead of GITHUB_ORG
  2. change JSON schema to parse repo webhook reply as only repo webhook reply and same for org webhook reply. (added 2 tests)
  3. remove all existing non-empty files that can be used in this run under the dir that is declared in CKV_GITHUB_CONF_DIR_NAME. (test changed to check that the files are actually empty in the initialization of the class)

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

@marynaKK marynaKK changed the title bug fixes fix(github): bug fixes in CKV_GITHUB_6, CKV_GITHUB_7, CKV_GITHUB_9 Oct 3, 2022
@@ -85,28 +82,34 @@ def get_organization_webhooks(self) -> dict[str, Any] | None:
return data

def get_repository_collaborators(self) -> dict[str, Any] | None:
data = self._request(endpoint="repos/{}/{}/collaborators".format(self.org, self.current_repository),
allowed_status_codes=[200])
if self.org:
Copy link
Contributor

Choose a reason for hiding this comment

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

This is not needed, org can exist (to retrieve org security info) but the repository collaborators should be queried with repo-related details, i.e. repo_owner.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed this too :)


def get_repository_webhooks(self) -> dict[str, Any] | None:
data = self._request(endpoint="repos/{}/{}/hooks".format(self.org, self.current_repository),
allowed_status_codes=[200])
if self.org:
Copy link
Contributor

Choose a reason for hiding this comment

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

Same here, it is not needed, org can exist (to retrieve org security info) but the repository webhooks should be queried with repo-related details, i.e. repo_owner.

Copy link
Contributor Author

@marynaKK marynaKK Oct 3, 2022

Choose a reason for hiding this comment

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

there could be hooks for org, for repo of org, and for repo not under an org.
hence to get the correct hooks of the repo - there must be a specification if it's under a user or an org.
These 2 URLs will retrieve different data:
https://api.github.com/repos/marynaKK/terragoat/hooks - The private repo under my account
https://api.github.com/repos/KiteFlyingInTheSky/terragoat/hooks - The repo under the org 'KiteFlyingInTheSky', that is different from the previous repo.

The get endpoint is: /repos/{owner}/{repo}/hooks, It is not mentioned in the documentation but I guess the owner could be the org or the user.

Copy link
Contributor

Choose a reason for hiding this comment

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

If the user's org is also repo owner and he's interested in repo hooks under that org, he can provide org name as repo_owner env var.
The current solution will fail if I do provide org env var, but my org is not listed as repo owner (i.e. I'm the sole repo owner).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Got it! - fixed.

report = runner.run(
root_folder=valid_dir_path,
runner_filter=RunnerFilter(checks=checks)
)
self.assertEqual(len(report.failed_checks), 1)
self.assertEqual(report.parsing_errors, [])
self.assertEqual(len(report.passed_checks), 3)
self.assertEqual(len(report.passed_checks), 2)
Copy link
Contributor

Choose a reason for hiding this comment

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

Did we lose a check here? I would expect that if we have less passing checks we'll have more failed checks or skipped checks.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We didn't lose a check. initially, there was an error - see bug 2 in my description.

Copy link
Contributor

@nimrodkor nimrodkor left a comment

Choose a reason for hiding this comment

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

Nice, please come to an understanding with @maxamel and his important comments @marynaKK

checkov/github/dal.py Outdated Show resolved Hide resolved
@marynaKK marynaKK merged commit 694e7c8 into master Oct 3, 2022
@marynaKK marynaKK deleted the fix-github-bugs branch October 3, 2022 12:54
pj991207 pushed a commit to init-cloud/checkov that referenced this pull request Oct 3, 2022
…ridgecrewio#3605)

* bug fixes

* fix test

* added test to differentiate repo & org webhooks

* fix lint, mypy

* changed to repo owner in hooks and collab endpoint

* removed unused var from test

* remove all files of github_conf dir tree

* fix lint
pj991207 pushed a commit to init-cloud/checkov that referenced this pull request Oct 3, 2022
…ridgecrewio#3605)

* bug fixes

* fix test

* added test to differentiate repo & org webhooks

* fix lint, mypy

* changed to repo owner in hooks and collab endpoint

* removed unused var from test

* remove all files of github_conf dir tree

* fix lint
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