【发布时间】:2019-07-30 20:59:12
【问题描述】:
我正在创建 Pod 安全策略以阻止所有用户将 Pod 创建为 Root 用户。我的集群在 GKE 上。 到目前为止,我执行的步骤是
1) 在我的集群中启用 PodSecurityPolicy
gcloud beta container clusters update standard-cluster-11 --enable-pod-security-policy
2) 定义策略。该策略很简单,并且限制了 root 用户。
apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
name: a-restrict-root
spec:
privileged: false
runAsUser:
rule: MustRunAsNonRoot # <------ Root user restricted.
seLinux:
rule: RunAsAny
fsGroup:
rule: RunAsAny
supplementalGroups:
rule: RunAsAny
volumes:
- '*'
3) 然后当然要实施正确的 RBAC 规则,以便可以为所有用户实施。
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: gce:podsecuritypolicy:a-restrict-root
labels:
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/cluster-service: "true"
rules:
- apiGroups:
- policy
resourceNames:
- a-restrict-root
resources:
- podsecuritypolicies
verbs:
- use
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: gce:podsecuritypolicy:a-restrict-root
labels:
addonmanager.kubernetes.io/mode: Reconcile
kubernetes.io/cluster-service: "true"
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: gce:podsecuritypolicy:a-restrict-root
subjects:
- kind: Group
apiGroup: rbac.authorization.k8s.io
name: system:serviceaccounts
现在是我尝试启动 Pod 的部分。 pod 定义如下所示:
apiVersion: v1
kind: Pod
metadata:
name: security-context-demo
spec:
securityContext:
runAsUser: 0
fsGroup: 0
volumes:
- name: sec-ctx-vol
emptyDir: {}
containers:
- name: sec-ctx-demo
image: gcr.io/google-samples/node-hello:1.0
volumeMounts:
- name: sec-ctx-vol
mountPath: /data/demo
securityContext:
allowPrivilegeEscalation: false
如您所见,runAsUser 设置为 0,意思是 root。
当我运行kubectl create -f pod.yaml 时,pod 正在创建并进入Running 状态。
当我执行到 Pod 中时,我可以看到所有进程都以 root 身份运行
$ kubectl exec -it security-context-demo -- sh
# ps aux
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.0 4336 812 ? Ss 19:25 0:00 /bin/sh -c node server.js
root 6 0.4 0.5 772124 22656 ? Sl 19:25 0:00 node server.js
root 11 0.0 0.0 4336 724 ? Ss 19:26 0:00 sh
root 16 0.0 0.0 17500 2072 ? R+ 19:26 0:00 ps aux
但根据我的 PodSecurityPolicy,这不应该被允许。有什么我错过的吗?
更新:
我启动了一个默认的 nginx pod,我知道它总是以 root 用户身份启动。它的清单是:
apiVersion: v1
kind: Pod
metadata:
name: nginx
spec:
containers:
- name: nginx
image: nginx
当我创建它时,它也成功启动了。
$ kubectl get po
NAME READY STATUS RESTARTS AGE
nginx 1/1 Running 0 2m
由于 PSP 的原因,它不应该启动。
【问题讨论】:
标签: kubernetes google-kubernetes-engine