<p>I have successfully implemented PodSecurityPolicies(PSP) in my local minikube and am having trouble porting it into GKE. My aim for now is simple -->  <strong>Dont Allow pods with UID 0 or with privileged access.</strong></p>

<p>My PSP is simple:</p>

<pre><code>apiVersion: extensions/v1beta1
kind: PodSecurityPolicy
metadata:
  name: default-psp
spec:
  privileged: false
  runAsUser:
    rule: MustRunAsNonRoot
</code></pre>

<p>And Ive setup RBAC ClusterRoleBinding allowing ALL serviceaccounts to USE the PSP.</p>

<pre><code>kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: restrict-root-clusterRole
rules:
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - default-psp
---
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: restrict-root-clusterRoleBinding
roleRef:
  kind: ClusterRole
  name: restrict-root-clusterRole
  apiGroup: rbac.authorization.k8s.io
subjects:
- apiGroup: rbac.authorization.k8s.io
  kind: Group
  name: system:serviceaccounts
</code></pre>

<p>Now I enable PSP in GKE using <code>gcloud beta container clusters update psp-demo --enable-pod-security-policy</code></p>

<p>And then I notice that GKE create the following PSPs</p>

<pre><code>$ k get psp
NAME                           PRIV    CAPS   SELINUX    RUNASUSER   FSGROUP    SUPGROUP   READONLYROOTFS   VOLUMES
gce.event-exporter             false          RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            hostPath,secret
gce.fluentd-gcp                false          RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            configMap,hostPath,secret
gce.persistent-volume-binder   false          RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            nfs,secret
gce.privileged                 true    *      RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            *
gce.unprivileged-addon         false          RunAsAny   RunAsAny    RunAsAny   RunAsAny   false            emptyDir,configMap,secret
</code></pre>

<p>I then create my PSP and RBAC rules.</p>

<pre><code>k get psp
NAME                           PRIV    CAPS   SELINUX    RUNASUSER          FSGROUP     SUPGROUP    READONLYROOTFS   VOLUMES
default-psp                    false          RunAsAny   MustRunAsNonRoot   RunAsAny   RunAsAny   false            *
gce.event-exporter             false          RunAsAny   RunAsAny           RunAsAny    RunAsAny    false            hostPath,secret
gce.fluentd-gcp                false          RunAsAny   RunAsAny           RunAsAny    RunAsAny    false            configMap,hostPath,secret
gce.persistent-volume-binder   false          RunAsAny   RunAsAny           RunAsAny    RunAsAny    false            nfs,secret
gce.privileged                 true    *      RunAsAny   RunAsAny           RunAsAny    RunAsAny    false            *
gce.unprivileged-addon         false          RunAsAny   RunAsAny           RunAsAny    RunAsAny    false            emptyDir,configMap,secret
</code></pre>

<p>I then spin up a root user pod</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  name: root-user-pod
spec:
  containers:
  - name: root-user-pod
    image: nginx
    ports:
    - containerPort: 80
</code></pre>

<p>And it goes into Running state and looking at the annotation, I see :</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubernetes.io/limit-ranger: 'LimitRanger plugin set: cpu request for container
      root-user-pod'
    kubernetes.io/psp: gce.privileged
</code></pre>

<p>So clearly my default PSP is not being used.</p>

<p>I tried to edit the <code>gce.privileged</code> PSP but GKE automatically reverts it back to default <code>privileged</code> status.</p>

<p>Then what I did was to create a Pod in a particular namespace as a particular ServiceAccouunt. My new RBAC rules are :</p>

<pre><code>apiVersion: v1
kind: ServiceAccount
metadata:
  name: test-user
  namespace: test
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  namespace: test
  name: test-psp-role
rules:
- apiGroups: ['policy']
  resources: ['podsecuritypolicies']
  verbs:     ['use']
  resourceNames:
  - default-psp
---
kind: RoleBinding
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: test-psp-roleBinding
  namespace: test
subjects:
- kind: ServiceAccount
  name: test-user
  namespace: test
roleRef:
  kind: Role
  name: test-psp-role
  apiGroup: rbac.authorization.k8s.io
</code></pre>

<p>And I add <code>serviceAccountName: test-user</code> to my Pod manifest and then deploy the pod in the <code>test</code> namespace and it too going into Running state.</p>

<pre><code>k get po -n test
NAME            READY   STATUS    RESTARTS   AGE
root-user-pod   1/1     Running   0          7s
</code></pre>

<p>With the annotation :</p>

<pre><code>apiVersion: v1
kind: Pod
metadata:
  annotations:
    kubernetes.io/psp: gce.privileged
  creationTimestamp: "2019-03-12T15:48:11Z"
  name: root-user-pod
  namespace: test
</code></pre>

<p>So im not sure what to do next. How can I over ride the default PSPs that GKE creates ? </p>

<p>TL;DR - The privileged policy is required for running privileged system pods that provide critical cluster functionality. Your user account has access to use all PodSecurityPolicies.</p>

<p>Use of a specific PodSecurityPolicy can be authorized in 2 different ways:</p>

<ol>
<li>The <strong>pod's</strong> service account has the <code>use</code> permission on the specific PodSecurityPolicy.</li>
<li>The <strong>creating user</strong> has the <code>use</code> permission on the specific PodSecurityPolicy.</li>
</ol>

<p>You can test if your user account has access to use the policy with:</p>

<pre><code>kubectl auth can-i use podsecuritypolicy/${PSP_NAME}
</code></pre>

<p>You can test whether the pod's service account has access to a PSP with:</p>

<pre><code>kubectl --as=system:serviceaccount:${NAMESPACE:-default}:${SERVICE_ACCOUNT:-default} auth can-i use podsecuritypolicy/${PSP_NAME}
</code></pre>

<p>If you create the pod through a deployment, replicaset, or some other indirect mechanism, then it is the controller that creates the pod, not the user. In those cases the controller should not have access to any privileged pod security policies, and you should see the behavior you want.</p>

<p>Alternatively, make sure the users creating unprivileged pods do not have access to the cluster-admin role binding.</p>

<p>There are known issues with this approach, and the Kubernetes community is working on resolving these before PodSecurityPolicy is promoted from beta to general availability.</p>
