Grant a ServiceAccount Access to a Specific Secret Using RBAC
Scenario
All resources should be created in the finance namespace.
Create a Secret named api-key-v2 with key api-key and value payment-gateway-prod-key-12345. Then create a ServiceAccount named specific-secret-reader-sa.
Create a Role named single-secret-getter-role granting only the get verb on secrets, restricted to api-key-v2 via resourceNames. Bind it to the ServiceAccount using a RoleBinding named single-secret-getter-binding.
#This should return `yes`.
kubectl auth can-i get secret/api-key-v2 -n finance \
--as=system:serviceaccount:finance:specific-secret-reader-sa
#This should return `no`.
kubectl auth can-i get secret/other-secret -n finance \
--as=system:serviceaccount:finance:specific-secret-reader-sa
Hint 1
Use kubectl create role -h and kubectl create rolebinding -h to see available flags for creating a Role with specific verbs, resources, and resourceNames, then binding it to a ServiceAccount. See the official docs: Using RBAC Authorization