Kubernetes Clusters Should Disable Automounting API Credentials
Table of Contents
Core Concepts
What are API Credentials in Kubernetes?
In Kubernetes, API credentials are used to authenticate and authorize access to the Kubernetes API server. These credentials are typically stored in a service account token, which is a JSON Web Token (JWT). When a pod is created, Kubernetes can automatically mount the service account token into the pod, allowing the containers within the pod to interact with the API server.
Automounting API Credentials
Automounting API credentials means that Kubernetes will automatically mount the service account token into every pod by default. This feature is enabled by default in Kubernetes, and it can be convenient for developers who need to interact with the API server from within their pods. However, it also means that every pod has access to the API server, which can be a security risk if not properly managed.
Security Risks of Automounting API Credentials
- Privilege Escalation: If an attacker gains access to a pod with an automatically mounted service account token, they can use this token to interact with the API server and potentially escalate their privileges within the cluster.
- Data Exposure: The service account token can be used to access sensitive information stored in the Kubernetes API, such as secrets and configuration data. If a pod is compromised, this information could be exposed.
- Denial of Service: An attacker could use the service account token to launch a denial-of-service attack against the API server, disrupting the normal operation of the cluster.
Typical Usage Example
Let’s consider a simple example of a pod with automounted API credentials.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
containers:
- name: example-container
image: nginx:1.14.2
ports:
- containerPort: 80
In this example, since we have not explicitly disabled automounting, Kubernetes will automatically mount the service account token into the pod. The token will be available at /var/run/secrets/kubernetes.io/serviceaccount/token inside the container.
kubectl exec -it example-pod -- cat /var/run/secrets/kubernetes.io/serviceaccount/token
This command will display the service account token, which can be used to authenticate with the Kubernetes API server.
Common Practices
Enabling and Disabling Automounting
To disable automounting of API credentials for a specific service account, you can set the automountServiceAccountToken field to false in the service account definition.
apiVersion: v1
kind: ServiceAccount
metadata:
name: my-service-account
automountServiceAccountToken: false
To use this service account in a pod, you can specify it in the pod’s spec section.
apiVersion: v1
kind: Pod
metadata:
name: example-pod
spec:
serviceAccountName: my-service-account
containers:
- name: example-container
image: nginx:1.14.2
ports:
- containerPort: 80
Global Configuration
You can also configure the default behavior for all service accounts in a namespace or the entire cluster. To do this, you can set the automountServiceAccountToken field in the namespace or cluster-level service account controller configuration.
Best Practices
Principle of Least Privilege
Only grant the minimum necessary permissions to each service account. Instead of using a single service account with broad permissions, create multiple service accounts with specific permissions for different types of pods.
Regularly Rotate Service Account Tokens
Regularly rotate the service account tokens to reduce the risk of a compromised token being used for an extended period of time.
Monitor and Audit API Access
Use Kubernetes audit logging and monitoring tools to track API access and detect any suspicious activity.
Use RBAC Effectively
Implement Role-Based Access Control (RBAC) to define who can access the Kubernetes API and what actions they can perform.
Conclusion
Automounting API credentials in Kubernetes clusters can be a convenient feature, but it also introduces significant security risks. By disabling automounting, following best practices, and implementing proper security controls, you can reduce the risk of a security breach and protect your Kubernetes cluster from potential threats.