TL;DR
Having get permissions to a Kubernetes object does not imply you have list premissions and vice versa.
This was not immediately obvious to me.
I figured that if you can list all objects of a certain kind (like secrets) you can also get individual objects and vice versa, but that’s not the case.
Here’s how it works using Kubernetes Secrets as an example object:
If you only have
getpermissions, you will have to know the exact name of the secret you are trying to read beforehand. You can run a command likekubectl get secret <secret-name>successfully, but runningkubectl get secretswill not work.If you only have
listpermissions for secrets, you can runkubectl get secretsand see a list of secrets. But runningkubectl get secret <secret-name>will not work.
Example with Minikube
I’ll be demonstrating the conclusion from above using
Minikube. I’ll be creating the following in K8s: a Secret, two Roles, two Service Accounts, and two RoleBindings to assign get and list RBAC permissions to each service account for testing.
Here is a very long manifest that creates the following objects:
- A namespace called
example - A
basic authentication secret called
credentialsin theexamplenamespace - Two service accounts. One called
service-account-1and another calledservice-account-2 - Two Roles:
- A role named
get-secretthat allows users associated with this role to get a secret. - A role named
list-secretsthat allows users to list secrets.
- A role named
- Two RoleBindings:
- A RoleBinding named
get-secrets-bindingthat will associate the service accountservice-account-1with theget-secretsrole. - A RoleBinding named
list-secrets-bindingthat will associate the service accountservice-account-2with thelist-secretsrole.
- A RoleBinding named
| |
This manifest can be saved to a YAML file and applied like so:
| |
Testing It All Out
Now we can test the get and list RBAC verbs with our service accounts.
To summarize:
- The service account
service-account-1hasgetpermissions for secrets in theexamplenamespace. This service account should not be able to list secrets but should be able to get an individual secret in theexamplenamespace. - The service account
service-account-2haslistpermissions for secrets in theexamplenamespace. This service account should not be able to get an individual secret but should be able to list all secrets in theexamplenamespace.
We can use the following command formula to test the permissions set to both service accounts:
| |
First lets try getting a secret under both service accounts. We expect service-account-1 to be able to get a secret, but not service-account-2:
| |
| |
Works as expected!
Now lets try getting the credentials secret we created earlier just to double check. We should get the same results as above.
| |
| |
Works as expected.
Next lets try listing secrets under both service accounts. This time we expect service-account-2 to be able to list secrets, but not service-account-1:
| |
| |
Works as expected once again!