forked from bridgecrewio/checkov
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into openapi_runner
- Loading branch information
Showing
37 changed files
with
3,437 additions
and
2,889 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
checkov==2.0.1051 | ||
checkov==2.0.1058 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
from checkov.kubernetes.checks.resource.base_spec_check import BaseK8Check | ||
from checkov.common.models.enums import CheckCategories, CheckResult | ||
from typing import Dict, Any, List | ||
|
||
|
||
class RbacOperation(): | ||
""" | ||
A collection of RBAC permissions that permit a certain operation within Kubernetes. | ||
For example, the RbacOperation below denotes a write operation on admission webhooks. | ||
control_webhooks = RbacOperation( | ||
apigroups=["admissionregistration.k8s.io"], | ||
verbs=["create", "update", "patch"], | ||
resources=["mutatingwebhookconfigurations", "validatingwebhookconfigurations"]) | ||
Rules matching an apiGroup, verb and resource should be able to perform the operation. | ||
""" | ||
def __init__(self, apigroups: List[str], verbs: List[str], | ||
resources: List[str]): | ||
self.apigroups = apigroups | ||
self.verbs = verbs | ||
self.resources = resources | ||
|
||
|
||
class BaseRbacK8sCheck(BaseK8Check): | ||
""" | ||
Base class for checks that evaluate RBAC permissions in Roles and ClusterRoles | ||
""" | ||
def __init__(self, name, id, supported_entities=None): | ||
if supported_entities is None: | ||
supported_entities = ["Role", "ClusterRole"] | ||
categories = [CheckCategories.KUBERNETES] | ||
super().__init__(name=name, id=id, categories=categories, supported_entities=supported_entities) | ||
# A role that grants *ALL* the RbacOperation in failing_operations fails this check | ||
self.failing_operations: RbacOperation = [] | ||
|
||
def scan_spec_conf(self, conf): | ||
rules = conf.get("rules") | ||
if rules and isinstance(rules, list): | ||
for operation in self.failing_operations: | ||
# if one operation can't be found, check passes | ||
if not any(self.rule_can(rule, operation) for rule in rules): | ||
return CheckResult.PASSED | ||
# all operations were found, therefore the check fails | ||
return CheckResult.FAILED | ||
|
||
return CheckResult.PASSED | ||
|
||
# Check if a rule has an apigroup, verb, and resource specified in @operation | ||
def rule_can(self, rule: Dict[str, Any], operation: RbacOperation) -> bool: | ||
return self.apigroup_or_wildcard(rule, operation.apigroups) and \ | ||
self.verb_or_wildcard(rule, operation.verbs) and \ | ||
self.resource_or_wildcard(rule, operation.resources) | ||
|
||
def apigroup_or_wildcard(self, rule: Dict[str, Any], apigroups: List[str]) -> bool: | ||
return self.value_or_wildcard(rule, "apiGroups", apigroups) | ||
|
||
def verb_or_wildcard(self, rule: Dict[str, Any], verbs: List[str]) -> bool: | ||
return self.value_or_wildcard(rule, "verbs", verbs) | ||
|
||
def resource_or_wildcard(self, rule: Dict[str, Any], resources: List[str]) -> bool: | ||
if "resources" in rule: | ||
for granted_resource in rule["resources"]: | ||
if self.is_wildcard(granted_resource): | ||
return True | ||
for failing_resource in resources: | ||
if granted_resource == failing_resource: | ||
return True | ||
# Check for '*/subresource' syntax | ||
if "/" in failing_resource and "/" in granted_resource: | ||
if granted_resource == "*/" + failing_resource.split("/")[1]: | ||
return True | ||
return False | ||
|
||
# Check if rule has a key with a wildcard or a value from @value_list | ||
def value_or_wildcard(self, rule: Dict[str, Any], key: str, value_list: List[str]) -> bool: | ||
if key in rule: | ||
for value in rule[key]: | ||
if self.is_wildcard(value) or value in value_list: | ||
return True | ||
return False | ||
|
||
# Check if value is a K8s RBAC wildcard | ||
def is_wildcard(self, value: str) -> bool: | ||
return value == "*" or value == "*/*" |
26 changes: 26 additions & 0 deletions
26
checkov/kubernetes/checks/resource/k8s/RbacApproveCertificateSigningRequests.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from checkov.kubernetes.checks.resource.base_rbac_check import BaseRbacK8sCheck, RbacOperation | ||
|
||
|
||
class RbacApproveCertificateSigningRequests(BaseRbacK8sCheck): | ||
def __init__(self): | ||
name = "Minimize ClusterRoles that grant permissions to approve CertificateSigningRequests" | ||
id = "CKV_K8S_156" | ||
supported_entities = ["ClusterRole"] | ||
super().__init__(name=name, id=id, supported_entities=supported_entities) | ||
|
||
# See https://kubernetes.io/docs/reference/access-authn-authz/certificate-signing-requests/ | ||
self.failing_operations = [ | ||
RbacOperation( | ||
apigroups=["certificates.k8s.io"], | ||
verbs=["update", "patch"], | ||
resources=["certificatesigningrequests/approval"] | ||
), | ||
RbacOperation( | ||
apigroups=["certificates.k8s.io"], | ||
verbs=["approve"], | ||
resources=["signers"] | ||
), | ||
] | ||
|
||
|
||
check = RbacApproveCertificateSigningRequests() |
20 changes: 20 additions & 0 deletions
20
checkov/kubernetes/checks/resource/k8s/RbacControlWebhooks.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
from checkov.kubernetes.checks.resource.base_rbac_check import BaseRbacK8sCheck, RbacOperation | ||
|
||
|
||
class RbacControlWebhooks(BaseRbacK8sCheck): | ||
def __init__(self): | ||
name = "Minimize ClusterRoles that grant control over validating or mutating admission webhook configurations" | ||
id = "CKV_K8S_155" | ||
supported_entities = ["ClusterRole"] | ||
super().__init__(name=name, id=id, supported_entities=supported_entities) | ||
|
||
self.failing_operations = [ | ||
RbacOperation( | ||
apigroups=["admissionregistration.k8s.io"], | ||
verbs=["create", "update", "patch"], | ||
resources=["mutatingwebhookconfigurations", "validatingwebhookconfigurations"] | ||
), | ||
] | ||
|
||
|
||
check = RbacControlWebhooks() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.