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): Add to nested resources on k8s graph inherit namespace #6912

Merged
merged 1 commit into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions checkov/kubernetes/graph_builder/local_graph.py
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,12 @@ def _extract_nested_resources_recursive(conf: Dict[str, Any], all_resources: Lis
template['apiVersion'] = conf.get('apiVersion')

template_metadata = template.get('metadata')

talazuri marked this conversation as resolved.
Show resolved Hide resolved
template_namespace = template_metadata.get('namespace')
metadata_namespace = metadata.get('namespace')
if template_namespace is None and metadata_namespace is not None:
template_metadata['namespace'] = metadata_namespace

annotations = metadata.get('annotations')
if annotations is not None and template_metadata is not None and 'annotations' not in template_metadata:
# Updates annotations to template as well to handle metadata added to the parent resource
Expand Down
40 changes: 40 additions & 0 deletions tests/kubernetes/checks/example_NoDefaultNamespace/Dev-PASSED.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: v1
kind: Namespace
metadata:
name: dev

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: dev
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pass:
- 'Pod.default.nginx-ingress-controller-2.app.kubernetes.io/name-ingress-nginx.app.kubernetes.io/part-of-ingress-nginx'
- 'Pod.example-ns.nginx-ingress-controller-2.app.kubernetes.io/name-ingress-nginx.app.kubernetes.io/part-of-ingress-nginx'
fail:
- 'Pod.default.nginx-ingress-controller.app.kubernetes.io/name-ingress-nginx.app.kubernetes.io/part-of-ingress-nginx'
- 'Pod.example-ns.nginx-ingress-controller.app.kubernetes.io/name-ingress-nginx.app.kubernetes.io/part-of-ingress-nginx'
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
pass:
- 'Pod.dev.nginx-deployment.app-nginx'
- 'Deployment.dev.nginx-deployment'
- 'Service.dev.nginx-service'
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
apiVersion: v1
kind: Namespace
metadata:
name: dev

---
apiVersion: apps/v1
kind: Deployment
metadata:
name: nginx-deployment
namespace: dev
spec:
replicas: 3
selector:
matchLabels:
app: nginx
template:
metadata:
labels:
app: nginx
spec:
containers:
- name: nginx
image: nginx:latest
ports:
- containerPort: 80
---
apiVersion: v1
kind: Service
metadata:
name: nginx-service
namespace: dev
spec:
selector:
app: nginx
ports:
- protocol: TCP
port: 80
targetPort: 80
type: LoadBalancer
61 changes: 61 additions & 0 deletions tests/kubernetes/graph/checks/test_checks/NoDefaultNamespace.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
metadata:
id: "CKV_K8S_160"
name: "Ensure resources in k8s not in default namespace"
category: "KUBERNETES"
definition:
and:
- cond_type: "attribute"
resource_types:
- "Deployment"
- "StatefulSet"
- "DaemonSet"
- "Job"
- "CronJob"
- "Pod"
- "Service"
- "ConfigMap"
- "Secret"
attribute: "metadata.namespace"
operator: "exists"
- cond_type: "attribute"
resource_types:
- "Deployment"
- "StatefulSet"
- "DaemonSet"
- "Job"
- "CronJob"
- "Pod"
- "Service"
- "ConfigMap"
- "Secret"
attribute: "metadata.namespace"
operator: "not_equals"
value: "default"
- cond_type: "attribute"
resource_types:
- "Deployment"
- "StatefulSet"
- "DaemonSet"
- "Job"
- "CronJob"
- "Pod"
- "Service"
- "ConfigMap"
- "Secret"
attribute: "metadata.namespace"
operator: "not_equals"
value: "kube-system"
- cond_type: "attribute"
resource_types:
- "Deployment"
- "StatefulSet"
- "DaemonSet"
- "Job"
- "CronJob"
- "Pod"
- "Service"
- "ConfigMap"
- "Secret"
attribute: "metadata.namespace"
operator: "not_regex_match"
value: "^kube-.*"
3 changes: 3 additions & 0 deletions tests/kubernetes/graph/checks/test_yaml_policies.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ def test_PodIsPubliclyAccessibleExample(self) -> None:
def test_RequireAllPodsToHaveNetworkPolicy(self) -> None:
self.go('RequireAllPodsToHaveNetworkPolicy')

def test_NoDefaultNamespace(self):
self.go('NoDefaultNamespace')

def create_report_from_graph_checks_results(self, checks_results, check):
report = Report("kubernetes")
first_results_key = list(checks_results.keys())[0]
Expand Down
Loading