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(kubernetes): handled missing key in k8s templates #3640

Merged
merged 2 commits into from
Oct 11, 2022

Conversation

PelegLi
Copy link
Contributor

@PelegLi PelegLi commented Oct 11, 2022

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

Description

handled empty field "spec" in k8s templates.
No UT for this specific case wasn't added since a YAML with null value is not parsed in checkov (considered as a parsing error)

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

Comment on lines +38 to 39
if "spec" in conf and "jobTemplate" in conf["spec"] and "spec" in conf["spec"]["jobTemplate"] and conf["spec"]["jobTemplate"]["spec"] and "template" in conf["spec"]["jobTemplate"]["spec"] and "spec" in conf["spec"]["jobTemplate"]["spec"]["template"]:
spec = conf["spec"]["jobTemplate"]["spec"]["template"]["spec"]
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
if "spec" in conf and "jobTemplate" in conf["spec"] and "spec" in conf["spec"]["jobTemplate"] and conf["spec"]["jobTemplate"]["spec"] and "template" in conf["spec"]["jobTemplate"]["spec"] and "spec" in conf["spec"]["jobTemplate"]["spec"]["template"]:
spec = conf["spec"]["jobTemplate"]["spec"]["template"]["spec"]
cron_spec = find_in_dict(input_dict=resource, key_path="spec/jobTemplate/spec/template/spec")
if isinstance(cron_spec, dict):
spec = cron_spec

you can import this function from

from checkov.common.util.data_structures_utils import find_in_dict

this will take care of any weird None value along the line

@PelegLi PelegLi merged commit e676473 into master Oct 11, 2022
@PelegLi PelegLi deleted the none-key-in-k8s-templates branch October 11, 2022 15:19
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