Written by Praful Tamrakar Senior Cloud Engineer, Powerupcloud Technologies
Security in Cloud and Infra level
- Ensure the worker nodes AMI meet the CIS benchmark.
- For K8s benchmark :
- Below is a list of tools and resources that can be used to automate the validation of an instance of Kubernetes against the CIS Kubernetes Benchmark:
- Verify that the Security Groups and NACL do not allow all traffic access and the rules allow access to ports and Protocol needed only for Application and ssh purposes.
- Make sure that you have encryption of data at rest. Amazon KMS can be used for encryption of data at rest. For Example :
- EBS volumes for ControlPlane nodes and worker nodes can be encrypted via KMS.
- You can encrypt the Logs Data either in Cloudwatch Logs or in S3 using KMS.
- If Instance(s) are behind the ELB, make sure you have configured HTTPS encryption and decryption process (generally known as SSL termination) handled by an Elastic Load Balancer.
- Make sure the worker nodes and RDS are provisioned in Private Subnets.
- It’s always best practise to have a Separate Kubernetes(EKS) cluster for each Environment( Dev/UAT/Prod).
- Ensure to use AWS Shield/WAF to prevent DDOS attacks.
- Ensure to use a minimal base image ( Eg: Alpine image to run the App)
- Ensure that the docker image registry you are using is a trusted, authorized and private registry. EG: Amazon ECR.
- Make sure you remove all the unnecessary files in your docker image. Eg: In tomcat server, you need to remove:
- Ensure to disable the display of the app-server version or server information. For example, below in the Tomcat server, we can see the server information is displayed. This can be mitigated using the procedure below.
Update an empty value to server.info(server.info=””) in the file,$CATALINA_HOME/lib/org/apache/catalina/util/ServerInfo.properties
- Ensure not to copy or add any sensitive file/data in the Docker image, it’s always recommended to use Secrets ( K8s secrets are encrypted at rest by default onwards Kubernetes v1.13 ) You may also use another secret management tool of choice such as AWS Secret Manager/Hashicorp Vault.
- Eg: do not enter Database Endpoints, username, passwords in the docker file. Use K8s secrets and these secrets can be used as an Environmental variables
apiVersion: v1 kind: Pod metadata: name: secret-env-pod spec: containers: - name: myapp image: myapp env: - name: DB_USERNAME valueFrom: secretKeyRef: name: dbsecret key: username - name: DB_PASSWORD valueFrom: secretKeyRef: name: dbsecret key: password - name: DB_ENDPOINT valueFrom: secretKeyRef: name: dbsecret key: endpoint
5. Ensure to disable Bash from the container images.
6. Endorse Multi-Stage build for smaller, cleaner and secure images.
To understand how can you leverage multi-stage can be found on :
7. Verify that the container images are scanned for vulnerability assessment before it is pushed to the registry. The AWS ECR has the feature that you can scan Repository to Scan on Push. Eg : CLAIR/AQUA/etc assessment tools can be used to scan images. These tools can be embedded in the CI/CD pipeline making sure if there is any vulnerability, the docker image push can be rejected/terminated. Find sample implementation – https://www.powerupcloud.com/email-va-report-of-docker-images-in-ecr/
- Make sure to use or upgrade Kubernetes to the latest stable version.
- It’s recommended not to use default namespace. Instead, create a namespace for each application, i.e separate Namespaces for separate sensitive workloads.
- Make sure to enable Role-Based Access Control (RBAC) for Clients( Service Accounts / Users) for restricted privileges.
- Subjects: The set of users and processes that want to access the Kubernetes API.
- Resources: The set of Kubernetes API Objects available in the cluster. Examples include Pods, Deployments, Services, Nodes, and PersistentVolumes, among others.
- Verbs: The set of operations that can be executed to the resources above. Different verbs are available (examples: get, watch, create, delete, etc.), but ultimately all of them are Create, Read, Update or Delete (CRUD) operations.
Let’s see RBAC meant for seeing Kubernetes as a production-ready platform.
- Have multiple users with different properties, establishing a proper authentication mechanism.
- Have full control over which operations each user or group of users can execute.
- Have full control over which operations each process inside a pod can execute.
- Limit the visibility of certain resources of namespaces.
4. Make sure to standardize the naming and labeling Convention of the Pod, Deployment, and service. This will ease the operational burden for security management ( Pod Network Policy ).
5. Ensure to use Kubernetes network policy which will restrict the Pods communication, i.e how groups of pods are allowed to communicate with each other and other network endpoints. Please find how to implement the network policy in Amazon EKS https://blog.powerupcloud.com/restricting-k8s-services-access-on-amazon-eks-part-ix-7d75c97c9f3e
6. AWS Single Sign-On (SSO), AWS Managed Microsoft Active Directory Service, and the AWS IAM authenticator can be used to control access to your Amazon EKS cluster running on the AWS cloud.
7. Corroborate to use Pod Security Context.
- Ensure to disable root access. the docker image should be accessible from a non-root user
- Make sure to configure read-only root file system
- Security-Enhanced Linux (SELinux): You can assign SELinuxOptions objects using the seLinuxOptions field. Note that the SELinux module needs to be loaded on the underlying Linux nodes for these policies to take effect.
- Make sure Linux capabilities and/or add non-default Linux capabilities are used if it’s required.
- Make sure not to run pods/containers as privileged unless you will require access to all devices on the host. Permission to access an object, like a file, is based on user ID (UID) and group ID (GID).
Please Find the Snippet for Pod Security Context :
... spec: securityContext: runAsUser: 1000 runAsGroup: 3000 fsGroup: 2000 readOnlyRootFilesystem: true allowPrivilegeEscalation: false seLinuxOptions: level: "s0:c123,c456" capabilities: drop: - NET_RAW - CHOWN add: ["NET_ADMIN", "SYS_TIME"] ...
Note : Pod Security content can be used in pod as well as container level.
apiVersion: v1 kind: Pod metadata: name: security-context-demo-2 spec: #Pod level securityContext: runAsUser: 1000 containers: - name: sec-ctx-demo-2 image: gcr.io/google-samples/node-hello:1.0 #container level securityContext: runAsUser: 2000 allowPrivilegeEscalation: false
8. Make sure to embed these Kubernetes Admission Controllers in all possible ways.
- AlwaysPullImages – modifies every new Pod to force the image pull policy to Always. This is useful in a multitenant cluster so that users can be assured that their private images can only be used by those who have the credentials to pull them.
- DenyEscalatingExec – will deny exec and attach commands to pods that run with escalated privileges that allow host access. This includes pods that run as privileged, have access to the host IPC namespace or have access to the host PID namespace.
- ResourceQuota – will observe the incoming request and ensure that it does not violate any of the constraints enumerated in the ResourceQuota object in a Namespace.
- LimitRanger- will observe the incoming request and ensure that it does not violate any of the constraints enumerated in the LimitRange object in a Namespace. Eg: CPU and Memory
10. Ensure to scan Manifest files (yaml/json) for which any credentials are passed in objects ( deployment, charts ) Palo Alto Prisma / Alcide Kubernetes Advisor.
11. Ensure to use TLS authentication for Tiller when Helm is being used.
12. It’s always recommended not to use a default Service account
- The default service account has a very wide range of permissions in the cluster and should, therefore be disabled.
13. Do not create a Service Account or a User which has full cluster-admin privileges unless necessary, Always follow Least Privilege rule.
14. Make sure to disable anonymous access and send Unauthorized responses to unauthenticated requests. Verify the following Kubernetes security settings when configuring kubelet parameters:
- anonymous-auth is set to false to disable anonymous access (it will send 401 Unauthorized responses to unauthenticated requests).
- kubelet has a `–client-ca-file flag, providing a CA bundle to verify client certificates.
- –authorization-mode is not set to AlwaysAllow, as the more secure Webhook mode will delegate authorization decisions to the Kubernetes API server.
- –read-only-port is set to 0 to avoid unauthorized connections to the read-only endpoint (optional).
15. Ensure to put restricted access to etcd from only the API server and nodes that need that access. This can be restricted in the Security Group attached to ControlPlane.
K8s API call level
- Ensure that all the communication from the client(Pod/EndUser) to the K8s(API SERVER) should be TLS encrypted
- May experience throttle if huge API calls happen
- Corroborate that all the communication from k8s API server to ETCD/Kube Control Manager/Kubelet/worker node/Kube-proxy/Kube Scheduler should be TLS encrypted
- Enable Control Plane API to call logging and Auditing. ( EG: EKS Control Plane Logging)
- If you are using Managed Services for K8s such as Amazon EKS, GKE, Azure Kubernetes Service (AKS), these all things are taken care
EKS Security Considerations
- EKS does not support Kubernetes Network Policies or any other way to create firewall rules for Kubernetes deployment workloads apart from Security Groups on the Worker node, since it uses VPC CNI plugin by default, which does not support network policy. Fortunately, this has a simple fix. The Calico CNI can be deployed in EKS to run alongside the VPC CNI, providing Kubernetes Network Policies support.
- Ensure to Protect EC2 Instance Role Credentials and Manage AWS IAM Permissions for Pods. These can be configured by using below tools:
- By using the IAM roles for the service accounts feature, we no longer need to provide extended permissions to the worker node’s IAM role so that pods on that node can call AWS APIs. We can scope IAM permissions to a service account, and only pods that use that service account have access to those permissions. This feature also eliminates the need for third-party solutions such as kiam or kube2iam.
- Make sure to block pod access to the EC2 metadata API (not needed if you use kube2iam or kiam)
Security Monitoring of K8s
Sysdig Falco is an open-source, container security monitor designed to detect anomalous activity in your containers. Sysdig Falco taps into your host’s (or Node’s in the case Kubernetes) system calls to generate an event stream of all system activity. Falco’s rules engine then allows you to create rules based on this event stream, allowing you to alert on system events that seem abnormal. Since containers should have a very limited scope in what they run, you can easily create rules to alert on abnormal behavior inside a container.
The Alcide Advisor is a Continuous Kubernetes and Istio hygiene checks tool that provides a single-pane view for all your K8s-related issues, including audits, compliance, topology, networks, policies, and threats. This ensures that you get a better understanding and control of distributed and complex Kubernetes projects with a continuous and dynamic analysis. A partial list of the checks we run includes:
- Kubernetes vulnerability scanning
- Hunting misplaced secrets, or excessive secret access
- Workload hardening from Pod Security to network policies
- Istio security configuration and best practices
- Ingress controllers for security best practices.
- Kubernetes API server access privileges.
- Kubernetes operators security best practices.