Cloudogu GmbH
A "firewall" for communication between pods.
kubectl describe netpol <name>
In every namespace except kube-system
:
kube-system
โ Might stop the apps in your cluster from working
Don't forget to:
egress
more recent than ingress
rules and less sophisticated
My recommendations:
kube-system
namespaceskube-system
egress
whitelisting for cluster-external traffic apiVersion: v1
kind: Pod
metadata:
annotations:
seccomp.security.alpha.kubernetes.io/pod: runtime/default
spec:
containers:
- name: restricted
securityContext:
runAsNonRoot: true
runAsUser: 100000
runAsGroup: 100000
readOnlyRootFilesystem: true
allowPrivilegeEscalation: false
capabilities:
drop:
- ALL
enableServiceLinks: false
automountServiceAccountToken: false # When not communicating with API Server
runAsNonRoot: true
runAsUser
and runAsGroup
> 10000 runc
(used by Docker among others)sudo
, setuid
, Kernel vulnerabilitiesCapNetRaw
attack - DNS Spoofing on Kubernetes Clusterscurl --cacert /var/run/secrets/kubernetes.io/serviceaccount/ca.crt \
-H "Authorization: Bearer $(cat /var/run/secrets/kubernetes.io/serviceaccount/token)" \
https://${KUBERNETES_SERVICE_HOST}/api/v1/
Application might need temp folder to write to
docker diff <containerName>
emptyDir
volumes in podSome images require capabilities
docker run --rm --cap-drop ALL <image>
# Check error
docker run --rm --cap-drop ALL --cap-add CAP_CHOWN <image>
# Keep adding caps until no more error
nginxinc/nginx-unprivileged
runAsUser: 100000
in securityContext
of pod or USER 100000
in Dockerfile
of image.chmod
/chown
in Dockerfile
GID 0
Find out if your cluster adheres to these and other good security practices:
โก๏ธ Be prepared for a lot of findings
โก๏ธ Create your own good practices
My recommendations:
โก๏ธ Still highly recommended!
privileged
containers,apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
annotations:
seccomp.security.alpha.kubernetes.io/defaultProfileName: runtime/default
seccomp.security.alpha.kubernetes.io/allowedProfileNames: runtime/default
spec:
requiredDropCapabilities:
- ALL
allowedCapabilities: []
defaultAllowPrivilegeEscalation: false
allowPrivilegeEscalation: false
readOnlyRootFilesystem: true
runAsUser: # Same for runAsGroup, supplementalGroups, fsGroup
rule: MustRunAs
ranges:
- min: 100000
max: 999999
apiVersion: policy/v1beta1
kind: PodSecurityPolicy
metadata:
annotations:
apparmor.security.beta.kubernetes.io/defaultProfileName: runtime/default
apparmor.security.beta.kubernetes.io/allowedProfileNames: runtime/default
spec:
hostIPC: false
hostPID: false
hostNetwork: false
hostPorts: []
privileged: false
allowedHostPaths: []
volumes:
- configMap
- emptyDir
- projected
- secret
- downwardAPI
- persistentVolumeClaim
(Cluster)Role
apiGroups [ extensions ]
apiGroups [ policy ]
# Query active PSP
kubectl get pod <POD> -o jsonpath='{.metadata.annotations.kubernetes\.io/psp}'
# Check authorization
kubectl auth can-i use psp/privileged --as=system:serviceaccount:<NS>:<SA>
# Show which SA's are authorized (kubectl plugin)
kubectl who-can use psp/<PSP>
# Show roles of a SA (kubectl plugin)
kubectl rbac-lookup <SA> # e.g. subject = sa name
enableServiceLinks: false
automountServiceAccountToken: false
"Whitelisting" via RBAC.
Cloudogu GmbH
See also ๐ cloudogu.com/blog/tag/k8s-security
@cloudogu