A misconfigured self-hosted CI/CD setup in Kubernetes can be a vector for attacks by allowing unauthorized access to your cluster through poorly configured runners or job pods. This can lead to data leakage or app disruptions. Properly securing your CI/CD configuration is essential to prevent these risks.
First, you must ensure that your runners have the appropriate privileges. Then, you must implement the runners in separate nodes as your main app nodes for each environment. You must also add CI/CD scanning to your code and docker images. Finally, you should create multiple runners adapted to each environment's needs.
Recommendation: Separation of Privileges
One fundamental practice in Kubernetes CI/CD is to limit the privileges your runners and jobs pods will have on the cluster.
Running privileged pods may allow compromised dev machines to gain access to the node clusters, for example.
According to the principle of least privileges, each runner must also have only the permissions needed for its stage.
Usually, your runners do not need many privileges. You can separate the rights for your runners and the rights of the jobs launched by your runners for CI/CD. Usually, the jobs need really specific rights, whereas the runners mostly need to be able to run those jobs.
Risks Addressed:
Recommendation: Implement a robust node isolation strategy
To enhance security and resource management, adopt a solid node isolation strategy. Isolate CI/CD resources from application workloads by using separate node groups for your runners and jobs.
Risks Addressed:
Recommendation: Limit the content of the images you use for your job to what you need.
CI/CD jobs are launched using a base Docker image. This image must be chosen wisely and contain only what is strictly needed for your pipelines to work. Depending on the job, you can use different images.
Risks Addressed:
Recommendation: Implement tools that check misconfiguration for your CI/CD runners setup.
Ensure that your CI/CD runners setup is checked by tools such as Checkov or Kyverno to identify and prevent deploying insecure resources.
Chekov can check your Kubernetes yaml files and detect security issues, misconfigurations, or compliance violations before deployment.
Kyverno can prevent privileged runner or job pods from being deployed using its strong policy as a code engine.
This can help check that step 1 (having low privileges) is well applied.
Risks Addressed:
Recommendation: Use secure parameters when configuring your runners on your git provider.
This part may need a proper article. However, it is essential to make sure your runner configuration is correctly configured on your git provider to address strictly your needs.
Taking the example of gitlab CI :
The configuration is significant to take care of and should not be set randomly.
Risks Addressed:
By following the best practices presented in this article, you can significantly reduce the risk of Kubernetes compromission or data cluster leakage and improve the overall security of your infrastructure when using self-hosted runners.