From b4ee5678968edb5133f024ad2528ce2f315bbcf3 Mon Sep 17 00:00:00 2001 From: sapnasingh4852-source Date: Wed, 2 Dec 2026 21:56:25 +0530 Subject: [PATCH 1/2] fix: match regolibrary memory defaults in C-0004, C-0269 and C-0271 The bundle set memoryRequestMin 0.125Gi, memoryLimitMin 0.25Gi and 4Gi maxima, while regolibrary defaults the minima to 0 and leaves the maxima empty. The Rego rules therefore pass a 64Mi request or a 128Mi limit that the CEL policies rejected. Relax the shipped parameters to the Rego defaults and treat an empty maximum as no upper bound. Keep the bounded values in a separate test template so the range checks stay covered. Signed-off-by: sapnasingh4852-source --- .../basic-control-configuration.yaml | 10 +- controls/C-0004/policy.yaml | 4 +- controls/C-0004/tests.json | 22 +++ controls/C-0269/policy.yaml | 2 +- controls/C-0269/tests.json | 11 ++ controls/C-0271/policy.yaml | 2 +- controls/C-0271/tests.json | 12 ++ .../default-control-configuration.yaml | 10 +- .../memory-range-control-configuration.yaml | 137 ++++++++++++++++++ 9 files changed, 198 insertions(+), 12 deletions(-) create mode 100644 test-resources/memory-range-control-configuration.yaml diff --git a/configuration/basic-control-configuration.yaml b/configuration/basic-control-configuration.yaml index c1bf55f..e6fd1bc 100644 --- a/configuration/basic-control-configuration.yaml +++ b/configuration/basic-control-configuration.yaml @@ -50,10 +50,12 @@ settings: - 5 maxHighVulnerabilities: - 10 - memoryLimitMax: 4Gi - memoryLimitMin: 0.25Gi - memoryRequestMax: 4Gi - memoryRequestMin: 0.125Gi + # Match regolibrary's default-config-inputs.json: a minimum of 0 keeps only + # the "value is set" check, and an empty maximum means no upper bound. + memoryLimitMax: "" + memoryLimitMin: "0" + memoryRequestMax: "" + memoryRequestMin: "0" publicRegistries: - docker.io - gcr.io diff --git a/controls/C-0004/policy.yaml b/controls/C-0004/policy.yaml index f405a52..0452311 100644 --- a/controls/C-0004/policy.yaml +++ b/controls/C-0004/policy.yaml @@ -41,12 +41,12 @@ spec: variables.containers.all(container, (!(!(has(container.resources)) || !(has(container.resources.requests)) || !(has(container.resources.requests.memory))) && quantity(string(params.settings.memoryRequestMin)).compareTo(quantity(string(container.resources.requests.memory))) != 1 && - quantity(string(params.settings.memoryRequestMax)).compareTo(quantity(string(container.resources.requests.memory))) != -1 )) + (params.settings.memoryRequestMax == "" || quantity(string(params.settings.memoryRequestMax)).compareTo(quantity(string(container.resources.requests.memory))) != -1) )) messageExpression: 'object.kind + "/" + object.metadata.name + " contains container/s with memory request not set or they are not in the specified range! (see more at https://kubescape.io/docs/controls/c-0004/)"' - expression: > variables.containers.all(container, (!(!(has(container.resources)) || !(has(container.resources.limits)) || !(has(container.resources.limits.memory))) && quantity(string(params.settings.memoryLimitMin)).compareTo(quantity(string(container.resources.limits.memory))) != 1 && - quantity(string(params.settings.memoryLimitMax)).compareTo(quantity(string(container.resources.limits.memory))) != -1 )) + (params.settings.memoryLimitMax == "" || quantity(string(params.settings.memoryLimitMax)).compareTo(quantity(string(container.resources.limits.memory))) != -1) )) messageExpression: 'object.kind + "/" + object.metadata.name + " contains container/s with memory limit not set or they are not in the specified range! (see more at https://kubescape.io/docs/controls/c-0004/)"' \ No newline at end of file diff --git a/controls/C-0004/tests.json b/controls/C-0004/tests.json index 0968524..a9e1a5d 100644 --- a/controls/C-0004/tests.json +++ b/controls/C-0004/tests.json @@ -27,11 +27,30 @@ "name": "Pod with container having memory request and limits set and values out of the range is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.requests.memory=127Mi", "spec.containers.[0].resources.limits.memory=1Gi" ] }, + { + "name": "Pod with container having a 64Mi memory request and a 128Mi limit is allowed with the default unbounded range", + "template": "pod.yaml", + "expected": "pass", + "field_change_list": [ + "spec.containers.[0].resources.requests.memory=64Mi", + "spec.containers.[0].resources.limits.memory=128Mi" + ] + }, + { + "name": "Deployment with container having memory request and limit above the former maximum is allowed with the default unbounded range", + "template": "deployment.yaml", + "expected": "pass", + "field_change_list": [ + "spec.template.spec.containers.[0].resources.requests.memory=5Gi", + "spec.template.spec.containers.[0].resources.limits.memory=6Gi" + ] + }, { "name": "Deployment with container having memory-request and limits not set is blocked", "template": "deployment.yaml", @@ -169,6 +188,7 @@ "name": "Pod with container having memory request and limits set and init container having memory request and limits set and value not in the limit is blocked", "template": "pod-with-init-container.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].image=registry.k8s.io/e2e-test-images/busybox:1.36.1-1", "spec.containers.[0].resources.requests.memory=256Mi", @@ -182,6 +202,7 @@ "name": "Pod resized below the memory request minimum is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.requests.memory=256Mi", "spec.containers.[0].resources.limits.memory=512Mi" @@ -196,6 +217,7 @@ "name": "Pod resized above the memory limit maximum is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.requests.memory=256Mi", "spec.containers.[0].resources.limits.memory=512Mi" diff --git a/controls/C-0269/policy.yaml b/controls/C-0269/policy.yaml index 5ff8567..a65641f 100644 --- a/controls/C-0269/policy.yaml +++ b/controls/C-0269/policy.yaml @@ -41,5 +41,5 @@ spec: variables.containers.all(container, (!(!(has(container.resources)) || !(has(container.resources.requests)) || !(has(container.resources.requests.memory))) && quantity(string(params.settings.memoryRequestMin)).compareTo(quantity(string(container.resources.requests.memory))) != 1 && - quantity(string(params.settings.memoryRequestMax)).compareTo(quantity(string(container.resources.requests.memory))) != -1 )) + (params.settings.memoryRequestMax == "" || quantity(string(params.settings.memoryRequestMax)).compareTo(quantity(string(container.resources.requests.memory))) != -1) )) messageExpression: 'object.kind + "/" + object.metadata.name + " contains container/s with memory request not set or they are not in the specified range! (see more at https://kubescape.io/docs/controls/c-0269/)"' diff --git a/controls/C-0269/tests.json b/controls/C-0269/tests.json index e13f77e..b3bbc94 100644 --- a/controls/C-0269/tests.json +++ b/controls/C-0269/tests.json @@ -18,10 +18,19 @@ "name": "Pod with container having memory request set and value not in the limit is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.requests.memory=4.1Gi" ] }, + { + "name": "Pod with container having a 64Mi memory request is allowed with the default unbounded range", + "template": "pod.yaml", + "expected": "pass", + "field_change_list": [ + "spec.containers.[0].resources.requests.memory=64Mi" + ] + }, { "name": "Deployment with container having memory request not set is blocked", "template": "deployment.yaml", @@ -137,6 +146,7 @@ "name": "Pod with container having memory request set and init container having memory request set and value not in the limit is blocked", "template": "pod-with-init-container.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].image=registry.k8s.io/e2e-test-images/busybox:1.36.1-1", "spec.containers.[0].resources.requests.memory=256Mi", @@ -148,6 +158,7 @@ "name": "Pod resized below the memory request minimum is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.requests.memory=256Mi" ], diff --git a/controls/C-0271/policy.yaml b/controls/C-0271/policy.yaml index d7eaa26..365d202 100644 --- a/controls/C-0271/policy.yaml +++ b/controls/C-0271/policy.yaml @@ -41,5 +41,5 @@ spec: variables.containers.all(container, (!(!(has(container.resources)) || !(has(container.resources.limits)) || !(has(container.resources.limits.memory))) && quantity(string(params.settings.memoryLimitMin)).compareTo(quantity(string(container.resources.limits.memory))) != 1 && - quantity(string(params.settings.memoryLimitMax)).compareTo(quantity(string(container.resources.limits.memory))) != -1 )) + (params.settings.memoryLimitMax == "" || quantity(string(params.settings.memoryLimitMax)).compareTo(quantity(string(container.resources.limits.memory))) != -1) )) messageExpression: 'object.kind + "/" + object.metadata.name + " contains container/s with memory limit not set or they are not in the specified range! (see more at https://kubescape.io/docs/controls/c-0271/)"' \ No newline at end of file diff --git a/controls/C-0271/tests.json b/controls/C-0271/tests.json index 2975e71..e4cc968 100644 --- a/controls/C-0271/tests.json +++ b/controls/C-0271/tests.json @@ -18,10 +18,19 @@ "name": "Pod with container having memory limits set and value not in the limit is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.limits.memory=4.1Gi" ] }, + { + "name": "Pod with container having a 128Mi memory limit is allowed with the default unbounded range", + "template": "pod.yaml", + "expected": "pass", + "field_change_list": [ + "spec.containers.[0].resources.limits.memory=128Mi" + ] + }, { "name": "Deployment with container having memory limits not set is blocked", "template": "deployment.yaml", @@ -137,6 +146,7 @@ "name": "Pod with container having memory limits set and init container having memory limits set and value not in the limit is blocked", "template": "pod-with-init-container.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].image=registry.k8s.io/e2e-test-images/busybox:1.36.1-1", "spec.containers.[0].resources.limits.memory=512Mi", @@ -148,6 +158,7 @@ "name": "Pod resized below the memory limit minimum is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.requests.memory=64Mi", "spec.containers.[0].resources.limits.memory=512Mi" @@ -162,6 +173,7 @@ "name": "Pod resized above the memory limit maximum is blocked", "template": "pod.yaml", "expected": "fail", + "param_template": "memory-range-control-configuration.yaml", "field_change_list": [ "spec.containers.[0].resources.requests.memory=64Mi", "spec.containers.[0].resources.limits.memory=512Mi" diff --git a/test-resources/default-control-configuration.yaml b/test-resources/default-control-configuration.yaml index 75914e6..e92784f 100644 --- a/test-resources/default-control-configuration.yaml +++ b/test-resources/default-control-configuration.yaml @@ -48,10 +48,12 @@ settings: - 5 maxHighVulnerabilities: - 10 - memoryLimitMax: 4Gi - memoryLimitMin: 0.25Gi - memoryRequestMax: 4Gi - memoryRequestMin: 0.125Gi + # Match regolibrary's default-config-inputs.json: a minimum of 0 keeps only + # the "value is set" check, and an empty maximum means no upper bound. + memoryLimitMax: "" + memoryLimitMin: "0" + memoryRequestMax: "" + memoryRequestMin: "0" publicRegistries: - docker.io - gcr.io diff --git a/test-resources/memory-range-control-configuration.yaml b/test-resources/memory-range-control-configuration.yaml new file mode 100644 index 0000000..1c9a0a2 --- /dev/null +++ b/test-resources/memory-range-control-configuration.yaml @@ -0,0 +1,137 @@ +apiVersion: "kubescape.io/v1" +kind: ControlConfiguration +metadata: + name: placeholder +settings: + hardenedSandboxRuntimeClasses: + - gvisor + cpuLimitMax: 5 + cpuLimitMin: 1 + cpuRequestMax: 5 + cpuRequestMin: 0.1 + imageRepositoryAllowList: + - gcr.io + - docker.io # Not in Kubescape's initial configuration but added to test as this is an edge case. + insecureCapabilities: + - SETPCAP + - NET_ADMIN + - NET_RAW + - SYS_MODULE + - SYS_RAWIO + - SYS_PTRACE + - SYS_ADMIN + - SYS_BOOT + - MAC_OVERRIDE + - MAC_ADMIN + - PERFMON + - ALL + - BPF + k8sRecommendedLabels: + - app.kubernetes.io/name + - app.kubernetes.io/instance + - app.kubernetes.io/version + - app.kubernetes.io/component + - app.kubernetes.io/part-of + - app.kubernetes.io/managed-by + - app.kubernetes.io/created-by + listOfDangerousArtifacts: + - bin/bash + - sbin/sh + - bin/ksh + - bin/tcsh + - bin/zsh + - usr/bin/scsh + - bin/csh + - bin/busybox + - usr/bin/busybox + maxCriticalVulnerabilities: + - 5 + maxHighVulnerabilities: + - 10 + # Bounded values for tests that exercise the range checks. The shipped + # defaults in default-control-configuration.yaml are unbounded. + memoryLimitMax: 4Gi + memoryLimitMin: 0.25Gi + memoryRequestMax: 4Gi + memoryRequestMin: 0.125Gi + publicRegistries: + - docker.io + - gcr.io + - quay.io + - registry.hub.docker.com + recommendedLabels: + - app + - tier + - phase + - version + - owner + - env + sensitiveInterfaces: + - nifi + - argo-server + - weave-scope-app + - kubeflow + - kubernetes-dashboard + - jenkins + - prometheus-deployment + sensitiveKeyNames: + - aws_access_key_id + - aws_secret_access_key + - azure_batchai_storage_account + - azure_batchai_storage_key + - azure_batch_account + - azure_batch_key + - secret + - key + - password + - pwd + - token + - jwt + - bearer + - credential + sensitiveKeyNamesAllowed: + - top-secret-allowed + sensitiveValues: + - BEGIN \w+ PRIVATE KEY + - PRIVATE KEY + - eyJhbGciO + - JWT + - Bearer + - _key_ + - _secret_ + sensitiveValuesAllowed: + - test-value + - encrypted-password + servicesNames: + - nifi-service + - argo-server + - minio + - postgres + - workflow-controller-metrics + - weave-scope-app + - kubernetes-dashboard + untrustedRegistries: + - docker.io + - gcr.io + - quay.io + - registry.hub.docker.com + wlKnownNames: + - coredns + - kube-proxy + - event-exporter-gke + - kube-dns + - 17-default-backend + - metrics-server + - ca-audit + - ca-dashboard-aggregator + - ca-notification-server + - ca-ocimage + - ca-oracle + - ca-posture + - ca-rbac + - ca-vuln-scan + - ca-webhook + - ca-websocket + - clair-clair + + cloudProvider: aks From c6eb4180636cac25651f1d90c0fdca03ae7002e7 Mon Sep 17 00:00:00 2001 From: sapnasingh4852-source Date: Wed, 2 Dec 2026 21:56:27 +0530 Subject: [PATCH 2/2] docs: explain the default memory range for the memory controls An empty maximum means no upper bound. State the shipped 0 minimum and empty maximum so readers know that only field presence is enforced by default. Signed-off-by: sapnasingh4852-source --- ...-0004-deny-resources-with-memory-limit-or-request-not-set.md | 2 ++ ...bescape-c-0269-deny-resources-with-memory-request-not-set.md | 2 ++ ...kubescape-c-0271-deny-resources-with-memory-limit-not-set.md | 2 ++ 3 files changed, 6 insertions(+) diff --git a/docs/policies-based-on-kubescape-controls/kubescape-c-0004-deny-resources-with-memory-limit-or-request-not-set.md b/docs/policies-based-on-kubescape-controls/kubescape-c-0004-deny-resources-with-memory-limit-or-request-not-set.md index 1c53893..bd1d618 100644 --- a/docs/policies-based-on-kubescape-controls/kubescape-c-0004-deny-resources-with-memory-limit-or-request-not-set.md +++ b/docs/policies-based-on-kubescape-controls/kubescape-c-0004-deny-resources-with-memory-limit-or-request-not-set.md @@ -29,6 +29,8 @@ If any of the above checks fail, the resource is denied from being deployed in the cluster. +The shipped parameters set the minimum to `0` and leave the maximum empty, which matches the Rego defaults. An empty maximum means no upper bound, so by default only the presence of each field is enforced. Set the minimum and maximum in the `ControlConfiguration` to enforce a range. + The minimum values apply to init containers too, so a small init container that runs one short command still needs a memory request of at least [memoryRequestMin](https://kubescape.io/docs/frameworks-and-controls/configuring-controls/#memory_request_min) and a memory limit of at least [memoryLimitMin](https://kubescape.io/docs/frameworks-and-controls/configuring-controls/#memory_limit_min). Ephemeral containers (added with `kubectl debug`) are not checked, because the Kubernetes API does not allow `resources` on ephemeral containers. diff --git a/docs/policies-based-on-kubescape-controls/kubescape-c-0269-deny-resources-with-memory-request-not-set.md b/docs/policies-based-on-kubescape-controls/kubescape-c-0269-deny-resources-with-memory-request-not-set.md index f23a928..e2a5d8b 100644 --- a/docs/policies-based-on-kubescape-controls/kubescape-c-0269-deny-resources-with-memory-request-not-set.md +++ b/docs/policies-based-on-kubescape-controls/kubescape-c-0269-deny-resources-with-memory-request-not-set.md @@ -23,6 +23,8 @@ If any of the above checks fail, the resource is denied from being deployed in the cluster. +The shipped parameters set the minimum to `0` and leave the maximum empty, which matches the Rego defaults. An empty maximum means no upper bound, so by default only the presence of the field is enforced. Set the minimum and maximum in the `ControlConfiguration` to enforce a range. + The minimum values apply to init containers too, so a small init container that runs one short command still needs a memory request of at least [memoryRequestMin](https://kubescape.io/docs/frameworks-and-controls/configuring-controls/#memory_request_min). Ephemeral containers (added with `kubectl debug`) are not checked, because the Kubernetes API does not allow `resources` on ephemeral containers. diff --git a/docs/policies-based-on-kubescape-controls/kubescape-c-0271-deny-resources-with-memory-limit-not-set.md b/docs/policies-based-on-kubescape-controls/kubescape-c-0271-deny-resources-with-memory-limit-not-set.md index f5bc283..f8aeb7b 100644 --- a/docs/policies-based-on-kubescape-controls/kubescape-c-0271-deny-resources-with-memory-limit-not-set.md +++ b/docs/policies-based-on-kubescape-controls/kubescape-c-0271-deny-resources-with-memory-limit-not-set.md @@ -23,6 +23,8 @@ If any of the above checks fail, the resource is denied from being deployed in the cluster. +The shipped parameters set the minimum to `0` and leave the maximum empty, which matches the Rego defaults. An empty maximum means no upper bound, so by default only the presence of the field is enforced. Set the minimum and maximum in the `ControlConfiguration` to enforce a range. + The minimum values apply to init containers too, so a small init container that runs one short command still needs a memory limit of at least [memoryLimitMin](https://kubescape.io/docs/frameworks-and-controls/configuring-controls/#memory_limit_min). Ephemeral containers (added with `kubectl debug`) are not checked, because the Kubernetes API does not allow `resources` on ephemeral containers.