################################
# Istio RBAC
################################
# Permissions and roles for istio
# To debug: start the cluster with -vmodule=rbac,3 to enable verbose logging on RBAC DENY
# Also helps to enable logging on apiserver 'wrap' to see the URLs.
# Each RBAC deny needs to be mapped into a rule for the role.
# If using minikube, start with '--extra-config=apiserver.Authorization.Mode=RBAC'
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-pilot-{ISTIO_NAMESPACE}
rules:
- apiGroups: ["config.istio.io"]
  resources: ["*"]
  verbs: ["*"]
- apiGroups: ["networking.istio.io"]
  resources: ["*"]
  verbs: ["*"]
- apiGroups: ["authentication.istio.io"]
  resources: ["*"]
  verbs: ["*"]
- apiGroups: ["apiextensions.k8s.io"]
  resources: ["customresourcedefinitions"]
  verbs: ["*"]
- apiGroups: ["extensions"]
  resources: ["thirdpartyresources", "thirdpartyresources.extensions", "ingresses", "ingresses/status"]
  verbs: ["*"]
- apiGroups: [""]
  resources: ["configmaps"]
  verbs: ["create", "get", "list", "watch", "update"]
- apiGroups: [""]
  resources: ["endpoints", "pods", "services"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["namespaces", "nodes", "secrets"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["admissionregistration.k8s.io"]
  resources: ["validatingwebhookconfigurations"]
  verbs: ["create", "update", "delete"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-sidecar-injector-{ISTIO_NAMESPACE}
rules:
- apiGroups: ["*"]
  resources: ["configmaps"]
  verbs: ["get", "list", "watch"]
---
# Mixer CRD needs to watch and list CRDs
# It also uses discovery API to discover Kinds of config.istio.io
# K8s adapter needs to list pods, services etc.
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-mixer-{ISTIO_NAMESPACE}
rules:
- apiGroups: ["config.istio.io"] # Istio CRD watcher
  resources: ["*"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apiextensions.k8s.io"]
  resources: ["customresourcedefinitions"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["configmaps", "endpoints", "pods", "services", "namespaces", "secrets"]
  verbs: ["get", "list", "watch"]
---
# Mixer validator: CRD, configmaps, and secrets need to be watched.
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-mixer-validator-{ISTIO_NAMESPACE}
rules:
- apiGroups: ["config.istio.io"] # Istio CRD watcher
  resources: ["*"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["apiextensions.k8s.io"]
  resources: ["customresourcedefinitions"]
  verbs: ["get", "list", "watch"]
- apiGroups: [""]
  resources: ["configmaps", "secrets"]
  verbs: ["get", "list", "watch"]
- apiGroups: ["admissionregistration.k8s.io"]
  resources: ["validatingwebhookconfigurations"]
  verbs: ["create", "update", "delete"]
---
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-ca-{ISTIO_NAMESPACE}
rules:
- apiGroups: [""]
  resources: ["secrets"]
  verbs: ["create", "get", "watch", "list", "update", "delete"]
- apiGroups: [""]
  resources: ["serviceaccounts"]
  verbs: ["get", "watch", "list"]
- apiGroups: [""]
  resources: ["services"]
  verbs: ["get", "watch", "list"]
---
# Permissions for the sidecar proxy.
kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-sidecar-{ISTIO_NAMESPACE}
rules:
- apiGroups: ["extensions"]
  resources: ["thirdpartyresources", "ingresses"]
  verbs: ["get", "watch", "list", "update"]
- apiGroups: [""]
  resources: ["configmaps", "pods", "endpoints", "services"]
  verbs: ["get", "watch", "list"]
---
# Grant permissions to the Pilot/discovery.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-pilot-admin-role-binding-{ISTIO_NAMESPACE}
subjects:
- kind: ServiceAccount
  name: istio-pilot-service-account
  namespace: {ISTIO_NAMESPACE}
roleRef:
  kind: ClusterRole
  name: istio-pilot-{ISTIO_NAMESPACE}
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to the Sidecar sidecar-injector
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-sidecar-injector-admin-role-binding-{ISTIO_NAMESPACE}
subjects:
- kind: ServiceAccount
  name: istio-sidecar-injector-service-account
  namespace: {ISTIO_NAMESPACE}
roleRef:
  kind: ClusterRole
  name: istio-sidecar-injector-{ISTIO_NAMESPACE}
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to the CA.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-ca-role-binding-{ISTIO_NAMESPACE}
subjects:
- kind: ServiceAccount
  name: istio-ca-service-account
  namespace: {ISTIO_NAMESPACE}
roleRef:
  kind: ClusterRole
  name: istio-ca-{ISTIO_NAMESPACE}
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to the Ingress controller.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-ingress-admin-role-binding-{ISTIO_NAMESPACE}
subjects:
- kind: ServiceAccount
  name: istio-ingress-service-account
  namespace: {ISTIO_NAMESPACE}
roleRef:
  kind: ClusterRole
  name: istio-pilot-{ISTIO_NAMESPACE}
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to the sidecar.
# TEMPORARY: the istioctl should generate a separate service account for the proxy, and permission
# granted only to that account !
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-sidecar-role-binding-{ISTIO_NAMESPACE}
subjects:
- kind: ServiceAccount
  name: default
  namespace: {ISTIO_NAMESPACE}
roleRef:
  kind: ClusterRole
  name: istio-sidecar-{ISTIO_NAMESPACE}
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to Mixer.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-mixer-admin-role-binding-{ISTIO_NAMESPACE}
subjects:
- kind: ServiceAccount
  name: istio-mixer-service-account
  namespace: {ISTIO_NAMESPACE}
roleRef:
  kind: ClusterRole
  name: istio-mixer-{ISTIO_NAMESPACE}
  apiGroup: rbac.authorization.k8s.io
---
# Grant permissions to Mixer validator.
kind: ClusterRoleBinding
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
  name: istio-mixer-validator-admin-role-binding-{ISTIO_NAMESPACE}
subjects:
- kind: ServiceAccount
  name: istio-mixer-validator-service-account
  namespace: {ISTIO_NAMESPACE}
roleRef:
  kind: ClusterRole
  name: istio-mixer-validator-{ISTIO_NAMESPACE}
  apiGroup: rbac.authorization.k8s.io
---
