# Copyright 2021 The Sigstore Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     https://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

kind: ClusterRole
apiVersion: rbac.authorization.k8s.io/v1
metadata:
  name: cosigned-rbac
rules:
  - apiGroups: [""]
    resources: ["events"]
    verbs: ["create"]

  # Allow the reconciliation of exactly our validating and mutating webhooks.
  - apiGroups: ["admissionregistration.k8s.io"]
    resources: ["validatingwebhookconfigurations", "mutatingwebhookconfigurations"]
    verbs: ["list", "watch"]
  - apiGroups: ["admissionregistration.k8s.io"]
    resources: ["validatingwebhookconfigurations", "mutatingwebhookconfigurations"]
    verbs: ["get", "update"]
    resourceNames: ["cosigned.sigstore.dev", "validating.clusterimagepolicy.sigstore.dev", "defaulting.clusterimagepolicy.sigstore.dev"]

  - apiGroups: [""]
    resources: ["namespaces"]
    verbs: ["get"]
    # The webhook configured the namespace as the OwnerRef on various cluster-scoped resources,
    # which requires we can Get the system namespace.
    resourceNames: ["cosign-system"]

  # TODO: We will need this once we have conversion webhooks.
  # # Allow the reconciliation of exactly our CRDs.
  # # This is needed for us to patch in conversion webhook information.
  # - apiGroups: ["apiextensions.k8s.io"]
  #   resources: ["customresourcedefinitions"]
  #   verbs: ["list", "watch"]
  # - apiGroups: ["apiextensions.k8s.io"]
  #   resources: ["customresourcedefinitions"]
  #   verbs: ["get", "update"]
  #   resourceNames: ["clusterimagepolicies.cosigned.sigstore.dev"]

  # Allow reconciliation of the ClusterImagePolic CRDs.
  - apiGroups: ["cosigned.sigstore.dev", "validating.clusterimagepolicy.sigstore.dev", "defaulting.clusterimagepolicy.sigstore.dev"]
    resources: ["clusterimagepolicies"]
    verbs: ["get", "list", "update", "watch", "patch"]

  # This is needed by k8schain to support fetching pull secrets attached to pod specs
  # or their service accounts.  If pull secrets aren't used, the "secrets" below can
  # be safely dropped, but the logic will fetch the service account to check for pull
  # secrets.
  - apiGroups: [""]
    resources: ["serviceaccounts", "secrets"]
    verbs: ["get"]
