0

I'm trying to access deployments in another namespace, and have the following ServiceAccount, ClusterRole, and ClusterRoleBinding

apiVersion: v1
kind: ServiceAccount
metadata:
  name: tekton-triggers-example-sa
  namespace: tekton-pipelines
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
    name: tekton-pipeline-sa
rules:
    - apiGroups: [""]
      resources: ["secrets", "services", "deployments", "pods"]
      verbs: ["get", "watch", "list", "create", "delete"]
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
    name: tekton-pipeline-permissions
    namespace: tekton-pipelines
subjects:
    - kind: ServiceAccount
      name: tekton-triggers-example-sa
      namespace: tekton-pipelines
roleRef:
    apiGroup: rbac.authorization.k8s.io
    kind: ClusterRole
    name: tekton-pipeline-sa

With that I can get everything but deployments

kubectl auth can-i get secrets --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
yes
kubectl auth can-i get services --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
yes
kubectl auth can-i get deployments --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
no
kubectl auth can-i get pods --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
yes

If I delete the binding I can't get anything, which makes sense

kubectl delete -f rbac/binding.yaml 
clusterrolebinding.rbac.authorization.k8s.io "tekton-pipeline-permissions" deleted

kubectl auth can-i get secrets --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
no
kubectl auth can-i get services --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
no
kubectl auth can-i get deployments --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
no
kubectl auth can-i get pods --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
no

If I apply it again, I get access to everything but deployments

kubectl apply -f rbac/binding.yaml 
clusterrolebinding.rbac.authorization.k8s.io/tekton-pipeline-permissions created
kubectl auth can-i get secrets --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
yes
kubectl auth can-i get services --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
yes
kubectl auth can-i get deployments --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
no
kubectl auth can-i get pods --as=system:serviceaccount:tekton-pipelines:tekton-triggers-example-sa -n anxiety-app 
yes

So that seems to me like that bindings is working for all the resources except deployments.

Anyone know what I've got wrong, why I can't access deployments?

TMH
  • 6,096
  • 7
  • 51
  • 88

1 Answers1

1

Your ClusterRole should be:

apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
  name: tekton-pipeline-sa
rules:
- apiGroups: [""]
  resources: ["secrets", "services", "pods"]
  verbs: ["get", "watch", "list", "create", "delete"]
- apiGroups: ["apps"]
  resources: ["deployments"]
  verbs: ["get", "watch", "list", "create", "delete"]

A Deployment would have some apiVersion: apps/v1. Which belongs to apiGroups apps. Wherehas pods, secrets or services would have theirs set to v1, thus in appGroups "".

SYN
  • 4,476
  • 1
  • 20
  • 22
  • Abosolute legend! Thanks so much. It there any reference of which apiGroup a resource is in so I don't have this probably again in the future? – TMH Jul 30 '22 at 13:08
  • 1
    When in doubt, you can use `kubectl api-resources`, which lists the objects known to a cluster, with their apiVersions. – SYN Jul 30 '22 at 13:10