Skip to content

Commit

Permalink
CMP-2543: Implement whitelist for configure-network-policies-namespaces
Browse files Browse the repository at this point in the history
Added a new varible var_network_policies_namespaces_whitelist_regex, and updated the rule configure_network_policies_namespaces so user is able to excude namespaces by setting this variable to the regex of namespace they want to exclude for this rule
  • Loading branch information
Vincent056 committed May 7, 2024
1 parent 5582fc2 commit 6089f75
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<def-group>
{{% set networkpolicies_api_path = '/apis/networking.k8s.io/v1/networkpolicies' %}}
{{% set namespaces_api_path = '/api/v1/namespaces' %}}
{{% set networkpolicies_for_non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.namespace | startswith("openshift") | not) and (.metadata.namespace | startswith("kube-") | not) and .metadata.namespace != "default") | .metadata.namespace] | unique' %}}
{{% set non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.name | startswith("openshift") | not) and (.metadata.name | startswith("kube-") | not) and .metadata.name != "default")]' %}}
{{% set networkpolicies_for_non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.namespace | startswith("openshift") | not) and (.metadata.namespace | startswith("kube-") | not) and .metadata.namespace != "default" and ({{if ne .var_network_policies_namespaces_whitelist_regex "None"}}.metadata.namespace | test("{{.var_network_policies_namespaces_whitelist_regex}}") | not{{else}}true{{end}})) | .metadata.namespace] | unique' %}}
{{% set non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.name | startswith("openshift") | not) and (.metadata.name | startswith("kube-") | not) and .metadata.name != "default" and ({{if ne .var_network_policies_namespaces_whitelist_regex "None"}}.metadata.name | test("{{.var_network_policies_namespaces_whitelist_regex}}") | not{{else}}true{{end}}))]' %}}
<definition class="compliance" id="configure_network_policies_namespaces" version="1">
{{{ oval_metadata("Ensure that application Namespaces have Network Policies defined") }}}
<criteria>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@ references:

{{% set networkpolicies_api_path = '/apis/networking.k8s.io/v1/networkpolicies' %}}
{{% set namespaces_api_path = '/api/v1/namespaces' %}}
{{% set networkpolicies_for_non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.namespace | startswith("openshift") | not) and (.metadata.namespace | startswith("kube-") | not) and .metadata.namespace != "default") | .metadata.namespace] | unique' %}}
{{% set non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.name | startswith("openshift") | not) and (.metadata.name | startswith("kube-") | not) and .metadata.name != "default")]' %}}

{{% set networkpolicies_for_non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.namespace | startswith("openshift") | not) and (.metadata.namespace | startswith("kube-") | not) and .metadata.namespace != "default" and ({{if ne .var_network_policies_namespaces_whitelist_regex "None"}}.metadata.namespace | test("{{.var_network_policies_namespaces_whitelist_regex}}") | not{{else}}true{{end}})) | .metadata.namespace] | unique' %}}
{{% set non_ctlplane_namespaces_filter = '[.items[] | select((.metadata.name | startswith("openshift") | not) and (.metadata.name | startswith("kube-") | not) and .metadata.name != "default" and ({{if ne .var_network_policies_namespaces_whitelist_regex "None"}}.metadata.name | test("{{.var_network_policies_namespaces_whitelist_regex}}") | not{{else}}true{{end}}))]' %}}
platform: not ocp4-on-hypershift

ocil_clause: 'Namespaced Network Policies needs review'

# same as above except filters the names only. Used in OCIL only, not in the 'warnings attribute'
{{% set non_ctlplane_namespaces_filter_names = '[.items[] | select((.metadata.name | startswith("openshift") | not) and (.metadata.name | startswith("kube-") | not) and .metadata.name != "default") | .metadata.name ]' %}}
{{% set non_ctlplane_namespaces_filter_names = '[.items[] | select((.metadata.name | startswith("openshift") | not) and (.metadata.name | startswith("kube-") | not) and .metadata.name != "default" and ({{if ne .var_network_policies_namespaces_whitelist_regex "None"}}.metadata.name | test("{{.var_network_policies_namespaces_whitelist_regex}}") | not{{else}}true{{end}})) | .metadata.name ]' %}}

ocil: |-
Verify that the every non-control plane namespace has an appropriate
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
documentation_complete: true

title: 'Whitelist Namespaces for Network Policies'

description: |-
Namespaces regular expression explicitly allowed
through network policy filters, e.g. setting value to
"namespace1|namespace2" will whitelist namespace
"namespace1" and "namespace2" for network policies checks.

type: string

operator: equals

interactive: false

options:
default: "None"

0 comments on commit 6089f75

Please sign in to comment.