<p>I'm experiencing a strange behavior from newly created Kubernetes service accounts. It appears that their tokens provide limitless access permissions in our cluster.</p>

<p>If I create a new namespace, a new service account inside that namespace, and then use the service account's token in a new kube config, I am able to perform all actions in the cluster.</p>

<pre><code># SERVER is the only variable you'll need to change to replicate on your own cluster
SERVER=https://k8s-api.example.com
NAMESPACE=test-namespace
SERVICE_ACCOUNT=test-sa

# Create a new namespace and service account
kubectl create namespace "${NAMESPACE}"
kubectl create serviceaccount -n "${NAMESPACE}" "${SERVICE_ACCOUNT}"

SECRET_NAME=$(kubectl get serviceaccount "${SERVICE_ACCOUNT}" --namespace=test-namespace -o jsonpath='{.secrets[*].name}')
CA=$(kubectl get secret -n "${NAMESPACE}" "${SECRET_NAME}" -o jsonpath='{.data.ca\.crt}')
TOKEN=$(kubectl get secret -n "${NAMESPACE}" "${SECRET_NAME}" -o jsonpath='{.data.token}' | base64 --decode)

# Create the config file using the certificate authority and token from the newly created
# service account
echo "
apiVersion: v1
kind: Config
clusters:
- name: test-cluster
  cluster:
    certificate-authority-data: ${CA}
    server: ${SERVER}
contexts:
- name: test-context
  context:
    cluster: test-cluster
    namespace: ${NAMESPACE}
    user: ${SERVICE_ACCOUNT}
current-context: test-context
users:
- name: ${SERVICE_ACCOUNT}
  user:
    token: ${TOKEN}
" &gt; config

</code></pre>

<p>Running that ^ as a shell script yields a <code>config</code> in the current directory. The problem is, using that file, I'm able to read and edit all resources in the cluster. I'd like the newly created service account to have no permissions unless I explicitly grant them via RBAC.</p>

<pre><code># All pods are shown, including kube-system pods
KUBECONFIG=./config kubectl get pods --all-namespaces

# And I can edit any of them
KUBECONFIG=./config kubectl edit pods -n kube-system some-pod
</code></pre>

<p>I haven't added any role bindings to the newly created service account, so I would expect it to receive access denied responses for all <code>kubectl</code> queries using the newly generated config.</p>

<p>Below is an example of the test-sa service account's JWT that's embedded in <code>config</code>: </p>

<pre><code>{
  "iss": "kubernetes/serviceaccount",
  "kubernetes.io/serviceaccount/namespace": "test-namespace",
  "kubernetes.io/serviceaccount/secret.name": "test-sa-token-fpfb4",
  "kubernetes.io/serviceaccount/service-account.name": "test-sa",
  "kubernetes.io/serviceaccount/service-account.uid": "7d2ecd36-b709-4299-9ec9-b3a0d754c770",
  "sub": "system:serviceaccount:test-namespace:test-sa"

}
</code></pre>

<p>Things to consider...</p>

<ul>
<li>RBAC seems to be enabled in the cluster as I see <code>rbac.authorization.k8s.io/v1</code> and 
<code>rbac.authorization.k8s.io/v1beta1</code> in the output of <code>kubectl api-versions | grep rbac</code> as suggested in <a href="https://stackoverflow.com/questions/51238988/how-to-check-whether-rbac-is-enabled-using-kubectl#51239531">this post</a>. It is notable that <code>kubectl cluster-info dump | grep authorization-mode</code>, as suggested in another answer to the same question, doesn't show output. Could this suggest RBAC isn't actually enabled?</li>
<li>My user has <code>cluster-admin</code> role privileges, but I would not expect those to carry over to service accounts created with it.</li>
<li>We're running our cluster on GKE.</li>
<li>As far as I'm aware, we don't have any unorthodox RBAC roles or bindings in the cluster that would cause this. I could be missing something or am generally unaware of K8s RBAC configurations that would cause this.</li>
</ul>

<p>Am I correct in my assumption that newly created service accounts should have extremely limited cluster access, and the above scenario shouldn't be possible without permissive role bindings being attached to the new service account? Any thoughts on what's going on here, or ways I can restrict the access of test-sa?</p>

Brannon
<p>You can check the permission of the service account by running command</p>

<pre><code>kubectl auth can-i --list --as=system:serviceaccount:test-namespace:test-sa
</code></pre>

<p>If you see below output that's the very limited permission by default a service account gets.</p>

<pre><code>Resources                                       Non-Resource URLs   Resource Names   Verbs
selfsubjectaccessreviews.authorization.k8s.io   []                  []               [create]
selfsubjectrulesreviews.authorization.k8s.io    []                  []               [create]
                                                [/api/*]            []               [get]
                                                [/api]              []               [get]
                                                [/apis/*]           []               [get]
                                                [/apis]             []               [get]
                                                [/healthz]          []               [get]
                                                [/healthz]          []               [get]
                                                [/livez]            []               [get]
                                                [/livez]            []               [get]
                                                [/openapi/*]        []               [get]
                                                [/openapi]          []               [get]
                                                [/readyz]           []               [get]
                                                [/readyz]           []               [get]
                                                [/version/]         []               [get]
                                                [/version/]         []               [get]
                                                [/version]          []               [get]
                                                [/version]          []               [get]
</code></pre>
