Containers
Policy management in Amazon EKS using jsPolicy
Introduction
jsPolicy is an open-source framework for managing validating or mutating admission control policies for Amazon Elastic Kubernetes Service (Amazon EKS) clusters using JavaScript (or TypeScript), which is similar to the way AWS Identity and Access Management (IAM) manages AWS accounts and resource access. It’s also possible to write the entire jsPolicy in a separate file and load the policy from there. jsPolicy offers built-in functions to reduce policy development effort, and testing frameworks like Mocha and Jest can be used to test a policy’s behavior.
This post will walk you through deploying jsPolicy into an Amazon EKS cluster and implementing two policies—one to deny deployments of resources into the default namespace and another to only allow container images from Amazon Elastic Container Registry (Amazon ECR) or the Amazon container image registries.
Prerequisites
For the walk through in this post, the following prerequisites are required:
- An AWS account—with a user account with adequate access to manage Amazon EKS clusters (See required IAM permissions here.)
- Kubernetes Kubectl tool
- AWS Command Line Interface (AWS CLI)—installed and configured (See Installing, updating, and uninstalling the AWS CLI in the AWS CLI documentation.)
- Amazon EKS command line tool
- Helm v3.0.0
- Docker CLI and Docker Engine
Solution overview
These are the steps presented in the following sections:
- Getting started
- Deploying the Amazon EKS cluster
- Configuring jsPolicy
- Creating and testing policies using jsPolicy
Getting started
Before we can get started, we need to set up an Amazon EKS cluster. We use eksctl with the cluster config file mechanism.
Deploy the Amazon EKS cluster
With the necessary tools installed, launch the Amazon EKS cluster. In the following example, the Amazon EKS cluster is deployed to the US East (Ohio) Region (us-east-2). However, the AWS_REGION
may be configured for any approved AWS Region where Amazon EKS is enabled.
Export the region and account ID:
Once the region is exported, create the ClusterConfig
as follows:
After the ClusterConfig
is created, create the cluster using the eksctl create cluster
command:
The eksctl tool needs approximately 15 minutes to build the Amazon EKS cluster and reach a ready state. In order to use jsPolicy, it must be installed on the Amazon EKS cluster using Helm, which must first be installed on the local machine.
Update kubeconfig
Once the Amazon EKS cluster is built and is in a ready state, update the kubeconfig file to access the cluster:
Set up jsPolicy
Install jsPolicy on the Amazon EKS cluster in its own namespace:
Validate if the jsPolicy pods are running:
Create and test policies
Create a policy to prevent deployment to the default namespace. The following policy prevents deployments to the default Kubernetes namespace:
Deploy the policy:
Test the policy to deny namespace deployments
Create an nginx
pod in the default namespace:
Run the nginx
pod to test it:
The cluster returns the following error, preventing deployment to the default namespace:
Create another namespace called web
:
Modify the namespace in the nginx.yaml
that was created prior to this from default
to web
and deploy it again:
Create the policy for the allowed list of Amazon ECR repositories
This policy denies pod images not originating from the allow list of Amazon ECR repositories. The first two entries in the policy belong to the account owner and that belonging to the Amazon EKS Amazon ECR repository:
Deploy the policy:
Test the image registry of the allowed list policy
Create an nginx
deployment in the web
namespace using the nginx
image from Docker Hub:
Create the nginx
deployment to test:
The cluster returns the following error, indicating that the image should only be pulled from 602401143452.dkr.ecr.us-east-2.amazonaws.com
(Amazon EKS Amazon ECR registry) or XXXXXXXX.dkr.ecr.us-east-2.amazonaws.com
, where XXXXXXXX is your AWS account number:
Now tag the nginx
image with our own registry, push the image, and try deploying from our own registry. For this repository, you need to create it in Amazon ECR:
Identify the name of the repository and the tag that the image uses:
Pull the nginx
image from Docker Hub locally so it can be appropriately retagged:
Retag latest
with the repository name and push this image to Amazon ECR. Before this can be done, login to Amazon ECR:
Now push the image with the latest
tag to Amazon ECR:
Now update the nginx deployment
manifest file to use the image from Amazon ECR and deploy it:
Now create the deployment again:
Cleanup
To avoid incurring future charges, delete all the resources that were deployed earlier:
Conclusion
If you have previous experience using JavaScript or TypeScript, then you can easily define and deploy custom validating or mutating admission control policies with jsPolicy. We have shown the process in this post. Review the Amazon EKS Best Practices Guide for Security to implement an optimized security strategy for your cluster. To learn more about jsPolicy, check out the jsPolicy documentation and get involved with the jsPolicy community.