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
get
permissions, 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 secrets
will not work.If you only have
list
permissions for secrets, you can runkubectl get secrets
and 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
credentials
in theexample
namespace - Two service accounts. One called
service-account-1
and another calledservice-account-2
- Two Roles:
- A role named
get-secret
that allows users associated with this role to get a secret. - A role named
list-secrets
that allows users to list secrets.
- A role named
- Two RoleBindings:
- A RoleBinding named
get-secrets-binding
that will associate the service accountservice-account-1
with theget-secrets
role. - A RoleBinding named
list-secrets-binding
that will associate the service accountservice-account-2
with thelist-secrets
role.
- 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-1
hasget
permissions for secrets in theexample
namespace. This service account should not be able to list secrets but should be able to get an individual secret in theexample
namespace. - The service account
service-account-2
haslist
permissions for secrets in theexample
namespace. This service account should not be able to get an individual secret but should be able to list all secrets in theexample
namespace.
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!