{"id":7416,"date":"2023-09-27T08:00:28","date_gmt":"2023-09-27T02:30:28","guid":{"rendered":"https:\/\/www.upnxtblog.com\/?p=7416"},"modified":"2023-09-23T13:13:46","modified_gmt":"2023-09-23T07:43:46","slug":"enforcing-policies-with-kubewarden-on-amazon-eks","status":"publish","type":"post","link":"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/","title":{"rendered":"Enforcing policies with Kubewarden on Amazon EKS"},"content":{"rendered":"<div class='booster-block booster-read-block'><\/div><p>According to <a href=\"https:\/\/www.redhat.com\/en\/resources\/state-kubernetes-security-report\" target=\"_blank\" rel=\"noopener\">Red Hat&#8217;s 2022 State of Kubernetes Security Report<\/a>, respondents stated that exposures due to misconfigurations in their container and Kubernetes environments (46%) is nearly three times the level of concern over attacks (16%), with vulnerabilities as the second-leading cause of worry (28%). Important settings, such as role-based access control (RBAC) and security contexts, are critical to the security posture of a cluster. One of the most important mis configuration is pod that lacks correct security configurations. Kubernetes offered <a href=\"https:\/\/kubernetes.io\/docs\/concepts\/policy\/pod-security-policy\/\" target=\"_blank\" rel=\"noopener\">Pod Security Policy (PSP)<\/a> mechanism to regulate pod security. PSPs define a set of security parameters that pods must fulfil before being created or updated in a cluster. However, PSPs have been deprecated as of Kubernetes version 1.21 due to serious usability problems, and have been removed in Kubernetes version 1.25.<\/p>\n<p>PSPs are decommissioned in favor of Pod Security Admission (PSA), a built-in admission controller that implements the security measures described in the Pod Security Standards (PSS). There are also several Policy-as-Code (PaC) solutions available for Kubernetes that are more flexible than PSS and also provide guardrails to guide cluster users, prevent unwanted behaviors, through prescribed and automated controls. Following are some of the examples<\/p>\n<table border=\"1\">\n<tbody>\n<tr>\n<td width=\"208\"><strong>PaC Solution<\/strong><\/td>\n<td width=\"225\"><strong>Link<\/strong><\/td>\n<td width=\"208\"><strong>CNCF Project Status<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"208\">Open Policy Agent<\/td>\n<td width=\"225\">https:\/\/www.openpolicyagent.org\/<\/td>\n<td width=\"208\"><strong>Graduated<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"208\">OPA\/gatekeeper<\/td>\n<td width=\"225\">https:\/\/github.com\/open-policy-agent\/gatekeeper<\/td>\n<td width=\"208\"><strong>Graduated<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"208\">Kyverno<\/td>\n<td width=\"225\">https:\/\/kyverno.io\/<\/td>\n<td width=\"208\"><strong>Incubating<\/strong><\/td>\n<\/tr>\n<tr>\n<td width=\"208\">Kubewarden<\/td>\n<td width=\"225\">https:\/\/www.kubewarden.io\/<\/td>\n<td width=\"208\"><strong>Sandbox<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>PaC solutions uses <a href=\"https:\/\/kubernetes.io\/docs\/reference\/access-authn-authz\/admission-controllers\/\" target=\"_blank\" rel=\"noopener\">Kubernetes Dynamic Admission Controllers<\/a> to intercept the Kubernetes API server request flow, via a webhook call, and mutate and validate request payloads, based on policies written and stored as code. Mutation and validation happen before the API server request results in a change to the cluster.<\/p>\n<p>To implement <a href=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/20\/securing-kubernetes-workloads-with-opa-opa-gatekeeper-in-amazon-eks\/\">Pod security<\/a>, Kubernetes users can choose between PSA or PaC, both solutions can coexist with PSP in the same cluster. Considering PaC solutions are more flexible and more granular and it is not just focused on pods but can also be used against different resources and actions. It can further be used to implement behaviors that are not necessarily security related, such as best practices, organizational standards, etc.<\/p>\n<p>In this post, we are going to look at PaC solution &#8211; <a href=\"https:\/\/www.kubewarden.io\/\" target=\"_blank\" rel=\"noopener\">Kubewarden<\/a>, a policy engine for Kubernetes. It doesn\u2019t require users to learn new Domain Specific Language or a query language instead policies can be authored in your favorite programming language. Kubewarden policies can also be distributed using container registries and could be integrated to your existing infrastructure and processes via CI\/CD pipelines.<\/p>\n\n<h2>Use Case<\/h2>\n<p>Kubewarden installs as an <a href=\"https:\/\/kubernetes.io\/docs\/reference\/access-authn-authz\/extensible-admission-controllers\/\" target=\"_blank\" rel=\"noopener\">Kubernetes Dynamic Admission Controller<\/a>, which receives webhook events when an API object changes. It validates incoming requests using policies written in WebAssembly. By using WebAssembly, users can write Kubernetes policies using their favorite programming language, as long as the language can produce Wasm binaries.<\/p>\n<p>Kubewarden has three main components which you will interact with:<\/p>\n<ol>\n<li><strong>PolicyServer<\/strong> &#8211; component which executes the Kubewarden policies when requests arrive and validates them.<\/li>\n<li><strong>ClusterAdmissionPolicy<\/strong> &#8211; defines how policies evaluate requests.<\/li>\n<li><strong>AdmissionPolicy<\/strong> &#8211; policy will process only the requests that are targeting the Namespace<\/li>\n<\/ol>\n<p>We will show you in this article how Kubernetes cluster administrators can validate and mutate configurations.<\/p>\n<h2>Prerequisites<\/h2>\n<p>We will assume that you already have an EKS cluster up and running. If you don\u2019t have the cluster, please refer this <a href=\"https:\/\/www.eksworkshop.com\/030_eksctl\/\" target=\"_blank\" rel=\"noopener\">link<\/a> to get started with Amazon EKS. Please note: Your k8s cluster version must be above v1.14.<\/p>\n<h2>Install Kubewarden on your cluster<\/h2>\n<p>Kubewarden installation is easy and the output from the install process is as shown below.<\/p>\n<h3>Install cert-manager<\/h3>\n<p><code>kubectl apply -f <a href=\"https:\/\/github.com\/jetstack\/cert-manager\/releases\/latest\/download\/cert-manager.yaml\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/jetstack\/cert-manager\/releases\/latest\/download\/cert-manager.yaml<\/a><\/code><\/p>\n<figure id=\"attachment_7418\" aria-describedby=\"caption-attachment-7418\" style=\"width: 1495px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7418\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube1-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube1.png?fit=1495%2C291&amp;ssl=1\" data-orig-size=\"1495,291\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube1\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Install cert-manager&lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube1.png?fit=1024%2C199&amp;ssl=1\" class=\"size-full wp-image-7418 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube1.png?resize=1140%2C222&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"222\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube1.png?w=1495&amp;ssl=1 1495w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube1.png?resize=300%2C58&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube1.png?resize=1024%2C199&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube1.png?resize=768%2C149&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/222;\" \/><figcaption id=\"caption-attachment-7418\" class=\"wp-caption-text\">Image &#8211; Install cert-manager<\/figcaption><\/figure>\n<h3>Check if <code>cert-manager<\/code> is up and running<\/h3>\n<p><code>kubectl wait --for=condition=Available deployment --timeout=2m -n cert-manager \u2013all<\/code><\/p>\n<figure id=\"attachment_7419\" aria-describedby=\"caption-attachment-7419\" style=\"width: 1428px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7419\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube2-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube2.png?fit=1428%2C177&amp;ssl=1\" data-orig-size=\"1428,177\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube2\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Check if cert-manager is up and running&lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube2.png?fit=1024%2C127&amp;ssl=1\" class=\"size-full wp-image-7419 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube2.png?resize=1140%2C141&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"141\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube2.png?w=1428&amp;ssl=1 1428w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube2.png?resize=300%2C37&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube2.png?resize=1024%2C127&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube2.png?resize=768%2C95&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/141;\" \/><figcaption id=\"caption-attachment-7419\" class=\"wp-caption-text\">Image &#8211; Check if cert-manager is up and running<\/figcaption><\/figure>\n<h3>Deploy Kubewarden stack using helm charts<\/h3>\n<p><code>helm repo add kubewarden <a href=\"https:\/\/charts.kubewarden.io\" target=\"_blank\" rel=\"noopener\">https:\/\/charts.kubewarden.io<\/a><\/code><\/p>\n<figure id=\"attachment_7420\" aria-describedby=\"caption-attachment-7420\" style=\"width: 1093px\" class=\"wp-caption alignleft\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7420\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube3-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube3.png?fit=1093%2C70&amp;ssl=1\" data-orig-size=\"1093,70\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube3\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Deploy Kubewarden stack &lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube3.png?fit=1024%2C66&amp;ssl=1\" class=\"wp-image-7420 size-full lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube3.png?resize=1093%2C70&#038;ssl=1\" alt=\"\" width=\"1093\" height=\"70\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube3.png?w=1093&amp;ssl=1 1093w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube3.png?resize=300%2C19&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube3.png?resize=1024%2C66&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube3.png?resize=768%2C49&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1093px) 100vw, 1093px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1093px; --smush-placeholder-aspect-ratio: 1093\/70;\" \/><figcaption id=\"caption-attachment-7420\" class=\"wp-caption-text\">Image &#8211; Deploy Kubewarden stack<\/figcaption><\/figure>\n<p>&nbsp;<\/p>\n<p><code>helm install --wait -n kubewarden --create-namespace kubewarden-crds kubewarden\/kubewarden-crds<\/code><\/p>\n<figure id=\"attachment_7421\" aria-describedby=\"caption-attachment-7421\" style=\"width: 1487px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7421\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube4-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube4.png?fit=1487%2C284&amp;ssl=1\" data-orig-size=\"1487,284\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube4\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Deploy Kubewarden stack &lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube4.png?fit=1024%2C196&amp;ssl=1\" class=\"size-full wp-image-7421 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube4.png?resize=1140%2C218&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"218\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube4.png?w=1487&amp;ssl=1 1487w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube4.png?resize=300%2C57&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube4.png?resize=1024%2C196&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube4.png?resize=768%2C147&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/218;\" \/><figcaption id=\"caption-attachment-7421\" class=\"wp-caption-text\">Image &#8211; Deploy Kubewarden stack<\/figcaption><\/figure>\n<p><code>helm install --wait -n kubewarden kubewarden-controller kubewarden\/kubewarden-controller<\/code><\/p>\n<figure id=\"attachment_7422\" aria-describedby=\"caption-attachment-7422\" style=\"width: 1470px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7422\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube5-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube5.png?fit=1470%2C473&amp;ssl=1\" data-orig-size=\"1470,473\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube5\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Deploy Kubewarden stack &lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube5.png?fit=1024%2C329&amp;ssl=1\" class=\"size-full wp-image-7422 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube5.png?resize=1140%2C367&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"367\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube5.png?w=1470&amp;ssl=1 1470w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube5.png?resize=300%2C97&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube5.png?resize=1024%2C329&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube5.png?resize=768%2C247&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/367;\" \/><figcaption id=\"caption-attachment-7422\" class=\"wp-caption-text\">Image &#8211; Deploy Kubewarden stack<\/figcaption><\/figure>\n<p><code>helm install --wait -n kubewarden kubewarden-defaults kubewarden\/kubewarden-defaults<\/code><\/p>\n<figure id=\"attachment_7423\" aria-describedby=\"caption-attachment-7423\" style=\"width: 1417px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7423\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube6-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube6.png?fit=1417%2C514&amp;ssl=1\" data-orig-size=\"1417,514\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube6\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Deploy Kubewarden stack &lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube6.png?fit=1024%2C371&amp;ssl=1\" class=\"size-full wp-image-7423 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube6.png?resize=1140%2C414&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"414\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube6.png?w=1417&amp;ssl=1 1417w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube6.png?resize=300%2C109&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube6.png?resize=1024%2C371&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube6.png?resize=768%2C279&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/414;\" \/><figcaption id=\"caption-attachment-7423\" class=\"wp-caption-text\">Image &#8211; Deploy Kubewarden stack<\/figcaption><\/figure>\n<p>Now that we have deployed Kubewarden, lets enforce our first policy in the next section.<\/p>\n<h2>Demonstration on validating privileged containers<\/h2>\n<p>The use of privileged containers is not a good security practice. By using privileged containers, it gives the container access to all of the capabilities that a host has. Compromised containers with privileged access can affect other containers operating on the host as well. In this demonstration, we will show you how to establish a policy that prevents pods that require privileged capabilities from running.<\/p>\n<p>First, let\u2019s create the policy below using <code>kubectl apply<\/code> command as below. This policy below will not allow a process to run in a privileged mode.<\/p>\n<pre>kubectl apply -f - &lt;&lt;EOF\r\napiVersion: policies.kubewarden.io\/v1alpha2\r\nkind: ClusterAdmissionPolicy\r\nmetadata:\r\n\u00a0 name: psp-allowprivilegeescalation\r\nspec:\r\n\u00a0 module: registry:\/\/ghcr.io\/kubewarden\/policies\/allow-privilege-escalation-psp:v0.1.11\r\n\u00a0 rules:\r\n\u00a0\u00a0\u00a0 - apiGroups:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - \"\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 apiVersions:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - v1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 resources:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - pods\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 operations:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - CREATE\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - UPDATE\r\n\u00a0 mutating: false\r\n\u00a0 settings:\r\n\u00a0\u00a0\u00a0 default_allow_privilege_escalation: false\r\nEOF<\/pre>\n<figure id=\"attachment_7424\" aria-describedby=\"caption-attachment-7424\" style=\"width: 1198px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7424\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube7-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube7.png?fit=1198%2C715&amp;ssl=1\" data-orig-size=\"1198,715\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube7\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Create the policy &lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube7.png?fit=1024%2C611&amp;ssl=1\" class=\"size-full wp-image-7424 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube7.png?resize=1140%2C680&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"680\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube7.png?w=1198&amp;ssl=1 1198w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube7.png?resize=300%2C179&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube7.png?resize=1024%2C611&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube7.png?resize=768%2C458&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/680;\" \/><figcaption id=\"caption-attachment-7424\" class=\"wp-caption-text\">Image &#8211; Create the policy<\/figcaption><\/figure>\n<p>Now that policy has been created, let\u2019s create a NGINX pod with privileged access, as shown in the YAML block below.<\/p>\n<pre>kubectl apply -f - &lt;&lt;EOF\r\napiVersion: v1\r\nkind: Pod\r\nmetadata:\r\n\u00a0 name: nginx\r\nspec:\r\n\u00a0 containers:\r\n\u00a0 - name: nginx\r\n\u00a0\u00a0\u00a0 image: nginx\r\n\u00a0\u00a0\u00a0 securityContext:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 allowPrivilegeEscalation: true\r\n\u00a0 - name: sidecar\r\n\u00a0\u00a0\u00a0 image: sidecar\r\nEOF<\/pre>\n<figure id=\"attachment_7425\" aria-describedby=\"caption-attachment-7425\" style=\"width: 1486px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7425\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube8-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube8.png?fit=1486%2C510&amp;ssl=1\" data-orig-size=\"1486,510\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube8\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Create a NGINX pod with privileged access&lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube8.png?fit=1024%2C351&amp;ssl=1\" class=\"size-full wp-image-7425 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube8.png?resize=1140%2C391&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"391\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube8.png?w=1486&amp;ssl=1 1486w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube8.png?resize=300%2C103&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube8.png?resize=1024%2C351&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube8.png?resize=768%2C264&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/391;\" \/><figcaption id=\"caption-attachment-7425\" class=\"wp-caption-text\">Image &#8211; Create a NGINX pod with privileged access<\/figcaption><\/figure>\n<p>As shown above, the pod creation fails as it contains the <code>allowPrivilegeEscalation: true<\/code> in the above code snippet. This is one of the best security practices where privilege escalations on pods should not be allowed.<\/p>\n<p>Let&#8217;s move to next demo which is on blocking pods running as root<\/p>\n<h2>Demonstration on blocking pods running as root<\/h2>\n<p>Running containers as \u2018root\u2019 is not good security practice. This gives the container access to all of the capabilities that a host has and compromised containers with root access can affect other containers operating on the host. In this demonstration, we will show you how to establish a policy that prevents pods that require &#8220;root&#8221; capabilities from running.<\/p>\n<p>First, let\u2019s create the policy below using <code>kubectl apply<\/code> command as below. This policy below will not allow containers to run in root mode.<\/p>\n<pre>kubectl apply -f - &lt;&lt;EOF\r\napiVersion: policies.kubewarden.io\/v1alpha2\r\nkind: ClusterAdmissionPolicy\r\nmetadata:\r\n\u00a0 name: psp-usergroup\r\nspec:\r\n\u00a0 module: registry:\/\/ghcr.io\/kubewarden\/policies\/user-group-psp:v0.2.0\r\n\u00a0 rules:\r\n\u00a0\u00a0\u00a0 - apiGroups:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - \"\"\r\n\u00a0 \u00a0\u00a0\u00a0\u00a0apiVersions:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - v1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 resources:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - pods\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 operations:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - CREATE\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - UPDATE\r\n\u00a0 mutating: true\r\n\u00a0 settings:\r\n\u00a0\u00a0\u00a0 run_as_user:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 rule: MustRunAsNonRoot\r\n\u00a0\u00a0\u00a0 supplemental_groups:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 rule: MustRunAs\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 ranges:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0- min: 1000\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 max: 65535\r\nEOF<\/pre>\n<figure id=\"attachment_7426\" aria-describedby=\"caption-attachment-7426\" style=\"width: 1124px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7426\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube9-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?fit=1124%2C785&amp;ssl=1\" data-orig-size=\"1124,785\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube9\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Create the policy &lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?fit=1024%2C715&amp;ssl=1\" class=\"size-full wp-image-7426 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?resize=1124%2C785&#038;ssl=1\" alt=\"\" width=\"1124\" height=\"785\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?w=1124&amp;ssl=1 1124w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?resize=300%2C210&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?resize=1024%2C715&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?resize=768%2C536&amp;ssl=1 768w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube9.png?resize=400%2C280&amp;ssl=1 400w\" data-sizes=\"auto, (max-width: 1124px) 100vw, 1124px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1124px; --smush-placeholder-aspect-ratio: 1124\/785;\" \/><figcaption id=\"caption-attachment-7426\" class=\"wp-caption-text\">Image &#8211; Create the policy<\/figcaption><\/figure>\n<p>Now that policy has been created, let\u2019s create a NGINX pod running as root user, as shown in the YAML block below.<\/p>\n<pre>kubectl apply -f - &lt;&lt;EOF\r\napiVersion: v1\r\nkind: Pod\r\nmetadata:\r\n\u00a0 name: nginx\r\nspec:\r\n\u00a0 containers:\r\n\u00a0 - name: nginx\r\n\u00a0\u00a0\u00a0 image: nginx\r\n\u00a0\u00a0\u00a0 securityContext:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 runAsNonRoot: false\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 runAsUser: 0\r\nEOF<\/pre>\n<figure id=\"attachment_7427\" aria-describedby=\"caption-attachment-7427\" style=\"width: 1501px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7427\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube10-3\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube10.png?fit=1501%2C489&amp;ssl=1\" data-orig-size=\"1501,489\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube10\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Create a NGINX pod running as root user&lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube10.png?fit=1024%2C334&amp;ssl=1\" class=\"size-full wp-image-7427 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube10.png?resize=1140%2C371&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"371\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube10.png?w=1501&amp;ssl=1 1501w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube10.png?resize=300%2C98&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube10.png?resize=1024%2C334&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube10.png?resize=768%2C250&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/371;\" \/><figcaption id=\"caption-attachment-7427\" class=\"wp-caption-text\">Image &#8211; Create a NGINX pod running as root user<\/figcaption><\/figure>\n<p>As shown above, the pod creation fails as it contains the <code>runAsUser:0<\/code> in the above code snippet. This is one of the best security practices where running pods as root should not be allowed.<\/p>\n<p>Now that we have seen how to block root access, let&#8217;s go to next demo on allowing pod to use the specific port#<\/p>\n<h2>Demonstration on allowing pod to use the port 443 only<\/h2>\n<p>Using admission controllers, a Kubewarden rule can be enforced to use specific port only. Below sample policy requires applications to run only on 443 port.<\/p>\n<pre>kubectl apply -f - &lt;&lt;EOF\r\napiVersion: policies.kubewarden.io\/v1alpha2\r\nkind: ClusterAdmissionPolicy\r\nmetadata:\r\n\u00a0 name: psp-hostnamespaces\r\nspec:\r\n\u00a0 module: registry:\/\/ghcr.io\/kubewarden\/policies\/host-namespaces-psp:v0.1.2\r\n\u00a0 rules:\r\n\u00a0\u00a0\u00a0 - apiGroups:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - \"\"\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 apiVersions:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - v1\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 resources:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - pods\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 operations:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - CREATE\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 - UPDATE\r\n\u00a0 mutating: false\r\n\u00a0 settings:\r\n\u00a0\u00a0\u00a0 allow_host_ipc: false\r\n\u00a0\u00a0\u00a0 allow_host_pid: false\r\n\u00a0\u00a0\u00a0 allow_host_ports:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - min: 443\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 max: 443\r\n\u00a0\u00a0\u00a0 allow_host_network: false\r\nEOF<\/pre>\n<p>Let\u2019s create the following policy using kubectl apply command<\/p>\n<figure id=\"attachment_7428\" aria-describedby=\"caption-attachment-7428\" style=\"width: 840px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7428\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube11\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube11.png?fit=840%2C688&amp;ssl=1\" data-orig-size=\"840,688\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube11\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Create the policy&lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube11.png?fit=840%2C688&amp;ssl=1\" class=\"size-full wp-image-7428 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube11.png?resize=840%2C688&#038;ssl=1\" alt=\"\" width=\"840\" height=\"688\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube11.png?w=840&amp;ssl=1 840w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube11.png?resize=300%2C246&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube11.png?resize=768%2C629&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 840px) 100vw, 840px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 840px; --smush-placeholder-aspect-ratio: 840\/688;\" \/><figcaption id=\"caption-attachment-7428\" class=\"wp-caption-text\">Image &#8211; Create the policy<\/figcaption><\/figure>\n<p>Now that policy has been created, let\u2019s create a NGINX pod running on port# 80, as shown in the YAML block below.<\/p>\n<pre>kubectl apply -f - &lt;&lt;EOF\r\napiVersion: v1\r\nkind: Pod\r\nmetadata:\r\n\u00a0 name: nginx\r\nspec:\r\n\u00a0 containers:\r\n\u00a0 - name: nginx\r\n\u00a0\u00a0\u00a0 image: nginx\r\n\u00a0\u00a0\u00a0 imagePullPolicy: IfNotPresent\r\n\u00a0\u00a0\u00a0 ports:\r\n\u00a0\u00a0\u00a0\u00a0\u00a0 - containerPort: 80\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 hostPort: 80\r\n\u00a0 - name: sleeping-sidecar\r\n\u00a0\u00a0\u00a0 image: alpine\r\n\u00a0\u00a0\u00a0 command: [\"sleep\", \"1h\"]\r\nEOF<\/pre>\n<figure id=\"attachment_7429\" aria-describedby=\"caption-attachment-7429\" style=\"width: 1484px\" class=\"wp-caption aligncenter\"><img data-recalc-dims=\"1\" decoding=\"async\" data-attachment-id=\"7429\" data-permalink=\"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/27\/enforcing-policies-with-kubewarden-on-amazon-eks\/kube12\/\" data-orig-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube12.png?fit=1484%2C616&amp;ssl=1\" data-orig-size=\"1484,616\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"kube12\" data-image-description=\"\" data-image-caption=\"&lt;p&gt;Image &amp;#8211; Create a NGINX pod running on port# 80&lt;\/p&gt;\n\" data-large-file=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube12.png?fit=1024%2C425&amp;ssl=1\" class=\"size-full wp-image-7429 lazyload\" data-src=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube12.png?resize=1140%2C473&#038;ssl=1\" alt=\"\" width=\"1140\" height=\"473\" data-srcset=\"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube12.png?w=1484&amp;ssl=1 1484w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube12.png?resize=300%2C125&amp;ssl=1 300w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube12.png?resize=1024%2C425&amp;ssl=1 1024w, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/09\/kube12.png?resize=768%2C319&amp;ssl=1 768w\" data-sizes=\"auto, (max-width: 1140px) 100vw, 1140px\" src=\"data:image\/svg+xml;base64,PHN2ZyB3aWR0aD0iMSIgaGVpZ2h0PSIxIiB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciPjwvc3ZnPg==\" style=\"--smush-placeholder-width: 1140px; --smush-placeholder-aspect-ratio: 1140\/473;\" \/><figcaption id=\"caption-attachment-7429\" class=\"wp-caption-text\">Image &#8211; Create a NGINX pod running on port# 80<\/figcaption><\/figure>\n<p>As shown above, the pod creation fails as it is running on <code>port# 80<\/code>. You can customize this policy to allow other or different ports that you trust.<\/p>\n<h2>Cleanup<\/h2>\n<p>Remove the remove the Kubewarden resources created by uninstalling the helm charts as follow:<\/p>\n<pre>helm uninstall --namespace kubewarden kubewarden-defaults\r\n\r\nhelm uninstall --namespace kubewarden kubewarden-controller\r\n\r\nhelm uninstall --namespace kubewarden kubewarden-crds<\/pre>\n<p>Once the helm charts have been uninstalled, you can remove the Kubernetes namespace that was used to deploy the Kubewarden stack:<\/p>\n<p><code>kubectl delete namespace kubewarden<\/code><\/p>\n<h2>Conclusion<\/h2>\n<p>PaC solutions such as Kubewarden makes it simple and easy to do policy management on your EKS cluster, more flexible than PSS and also provide guardrails to guide cluster users, prevent unwanted behaviors, through prescribed and automated controls.<\/p>\n<p><strong>References<\/strong><\/p>\n<p><a href=\"https:\/\/docs.kubewarden.io\" target=\"_blank\" rel=\"noopener\">https:\/\/docs.kubewarden.io<\/a><\/p>\n<p><a href=\"https:\/\/github.com\/kubewarden\/\" target=\"_blank\" rel=\"noopener\">https:\/\/github.com\/kubewarden\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>According to Red Hat&#8217;s 2022 State of Kubernetes Security Report, respondents stated that exposures due to misconfigurations in their container and Kubernetes environments (46%) is nearly three times the level of concern over attacks (16%), with vulnerabilities as the second-leading cause of worry (28%). Important settings, such as role-based access control (RBAC) and security contexts, [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":1031,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"nf_dc_page":"","_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[311],"tags":[627,628],"class_list":["post-7416","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-kubernetes","tag-kubewarden","tag-pod-security-policies"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p9fbQS-1VC","jetpack-related-posts":[{"id":5757,"url":"https:\/\/www.upnxtblog.com\/index.php\/2020\/10\/13\/10-best-practices-worth-implementing-to-adopt-kubernetes\/","url_meta":{"origin":7416,"position":0},"title":"Kubernetes Adoption in 2024: Key Statistics","author":"Karthik","date":"October 13, 2020","format":false,"excerpt":"We already know that\u00a0Kubernetes is the No. 1 orchestration platform for container-based applications, automating the deployment and scaling of these apps, and streamlining maintenance operations. However, Kubernetes comes with its own complexity challenges. So how can an enterprise take advantage of containerization to tackle complexity and not end up with\u2026","rel":"","context":"In &quot;Kubernetes Guides&quot;","block_context":{"text":"Kubernetes Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/kubernetes\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/10\/Big-Idea.jpg?fit=770%2C330&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/10\/Big-Idea.jpg?fit=770%2C330&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/10\/Big-Idea.jpg?fit=770%2C330&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2020\/10\/Big-Idea.jpg?fit=770%2C330&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":4547,"url":"https:\/\/www.upnxtblog.com\/index.php\/2019\/12\/09\/implementing-policies-in-kubernetes\/","url_meta":{"origin":7416,"position":1},"title":"Implementing Policies in Kubernetes","author":"Karthik","date":"December 9, 2019","format":false,"excerpt":"Kubernetes, as we know, coordinates a highly available cluster of computers that are connected to work as a single unit. Kubernetes contains a number of abstractions that allow deployment of containerized applications to the cluster without attaching them to individual machines. In short, Kubernetes is - Portable: public, private, hybrid,\u2026","rel":"","context":"In &quot;Kubernetes Guides&quot;","block_context":{"text":"Kubernetes Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/kubernetes\/"},"img":{"alt_text":"kubernetes logo","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":7300,"url":"https:\/\/www.upnxtblog.com\/index.php\/2023\/09\/20\/securing-kubernetes-workloads-with-opa-opa-gatekeeper-in-amazon-eks\/","url_meta":{"origin":7416,"position":2},"title":"Top Kubernetes Security Best Practices: Securing Kubernetes Workloads with OPA &#038; OPA Gatekeeper in Amazon EKS","author":"Karthik","date":"September 20, 2023","format":false,"excerpt":"Introduction As the adoption of container orchestration platforms like Kubernetes increases, so does the need for robust security measures. Open Policy Agent (OPA) and OPA Gatekeeper are powerful tools that help enforce policy-based security and governance in Kubernetes clusters. In this blog post, we will explore what OPA and OPA\u2026","rel":"","context":"In &quot;Kubernetes Guides&quot;","block_context":{"text":"Kubernetes Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/kubernetes\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/08\/opa-gatekeeper-k8s.png?fit=840%2C480&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/08\/opa-gatekeeper-k8s.png?fit=840%2C480&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/08\/opa-gatekeeper-k8s.png?fit=840%2C480&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2023\/08\/opa-gatekeeper-k8s.png?fit=840%2C480&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3719,"url":"https:\/\/www.upnxtblog.com\/index.php\/2019\/04\/12\/cloud-native-computing-foundation-adopts-cri-o-container-runtimetutorial\/","url_meta":{"origin":7416,"position":3},"title":"Cloud Native Computing Foundation adopts CRI-O container runtime+tutorial","author":"Karthik","date":"April 12, 2019","format":false,"excerpt":"CNCF team has voted to accept CRI-O as an incubation-level hosted project. CRI-O was created by Red Hat and it is an implementation of the Kubernetes Container Runtime Interface\u00a0(CRI) designed to enable the use of\u00a0Open Container Initiative (OCI) compatible runtime. In this article, let us look at key features\/components, how\u2026","rel":"","context":"In &quot;Kubernetes Guides&quot;","block_context":{"text":"Kubernetes Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/kubernetes\/"},"img":{"alt_text":"CRI-O container runtime","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/04\/cri-0.jpg?fit=606%2C247&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/04\/cri-0.jpg?fit=606%2C247&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2019\/04\/cri-0.jpg?fit=606%2C247&ssl=1&resize=525%2C300 1.5x"},"classes":[]},{"id":4962,"url":"https:\/\/www.upnxtblog.com\/index.php\/2020\/04\/15\/what-are-the-key-kubernetes-metrics-that-you-have-to-monitor\/","url_meta":{"origin":7416,"position":4},"title":"What are the key Kubernetes metrics that you have to monitor ?","author":"Karthik","date":"April 15, 2020","format":false,"excerpt":"We have already looked at BEST Kubernetes monitoring tools, with the increasing adoption of containers and microservices in the enterprises, monitoring utilities have to handle more services and server instances than ever before. Kubernetes environments vary from deployment to deployment, but they generally have a handful of key components, resources,\u2026","rel":"","context":"In &quot;Kubernetes Guides&quot;","block_context":{"text":"Kubernetes Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/kubernetes\/"},"img":{"alt_text":"kubernetes logo","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=700%2C400 2x"},"classes":[]},{"id":3061,"url":"https:\/\/www.upnxtblog.com\/index.php\/2018\/10\/24\/3-alternative-kubernetes-container-runtimes\/","url_meta":{"origin":7416,"position":5},"title":"3 Alternative Kubernetes container runtimes","author":"Karthik","date":"October 24, 2018","format":false,"excerpt":"Container runtime is the software that is responsible for running containers. To understand better, let us look at the typical Kubernetes cluster, its comprised of a master node and a set of slave nodes. If you're looking for quickstart on basic understanding of Kubernetes concepts, please refer earlier posts for\u2026","rel":"","context":"In &quot;Kubernetes Guides&quot;","block_context":{"text":"Kubernetes Guides","link":"https:\/\/www.upnxtblog.com\/index.php\/category\/kubernetes\/"},"img":{"alt_text":"kubernetes logo","src":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200","width":350,"height":200,"srcset":"https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=350%2C200 1x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=525%2C300 1.5x, https:\/\/i0.wp.com\/www.upnxtblog.com\/wp-content\/uploads\/2017\/11\/kubernetes.jpg?fit=722%2C612&ssl=1&resize=700%2C400 2x"},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/7416","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/comments?post=7416"}],"version-history":[{"count":7,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/7416\/revisions"}],"predecessor-version":[{"id":7436,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/posts\/7416\/revisions\/7436"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/media\/1031"}],"wp:attachment":[{"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/media?parent=7416"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/categories?post=7416"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.upnxtblog.com\/index.php\/wp-json\/wp\/v2\/tags?post=7416"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}