# Copyright 2021 Google LLC
#
# 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
#
#      http://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.
# ACM GCP ServiceAccount (GSA)
# This GSA can be used to grant ACM additional permissions with IAMPartialPolicy
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMServiceAccount
metadata: # kpt-merge: platform-namespace/sa-acm-gke-cluster
  name: sa-acm-gke-cluster # kpt-set: sa-acm-${cluster-name}
  namespace: platform-namespace # kpt-set: ${platform-namespace}
  annotations:
    cnrm.cloud.google.com/blueprint: cnrm/acm/v0.2.0
    cnrm.cloud.google.com/project-id: project-id # kpt-set: ${project-id}
spec:
  displayName: sa-acm-gke-cluster # kpt-set: sa-acm-${cluster-name}
  description: For use with ACM to provide read access to Cloud Source Repositories
---
# Allow ACM Config Sync Kubernetes ServiceAccount (KSA) for root-reconciler to use the ACM GSA
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata: # kpt-merge: platform-namespace/sa-acm-gke-cluster
  name: sa-acm-gke-cluster # kpt-set: sa-acm-${cluster-name}
  namespace: platform-namespace # kpt-set: ${platform-namespace}
  annotations:
    cnrm.cloud.google.com/blueprint: cnrm/acm/v0.2.0
    cnrm.cloud.google.com/project-id: project-id # kpt-set: ${project-id}
spec:
  resourceRef:
    name: sa-acm-gke-cluster # kpt-set: sa-acm-${cluster-name}
    apiVersion: iam.cnrm.cloud.google.com/v1beta1
    kind: IAMServiceAccount
  bindings:
    - role: roles/iam.workloadIdentityUser
      members:
        - member: serviceAccount:project-id.svc.id.goog[config-management-system/root-reconciler] # kpt-set: serviceAccount:${project-id}.svc.id.goog[config-management-system/root-reconciler]
---
# Allow ACM GSA to read from CSR repos in the CSR project
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata: # kpt-merge: platform-namespace/source-reader-sync-cluster-name-project-id
  name: source-reader-sync-cluster-name-project-id # kpt-set: source-reader-sync-${cluster-name}-${project-id}
  namespace: platform-namespace # kpt-set: ${platform-namespace}
  annotations:
    cnrm.cloud.google.com/blueprint: cnrm/acm/v0.2.0
    cnrm.cloud.google.com/project-id: project-id # kpt-set: ${project-id}
spec:
  resourceRef:
    apiVersion: sourcerepo.cnrm.cloud.google.com/v1beta1
    kind: SourceRepoRepository
    external: projects/project-id/repos/name # kpt-set: ${sync-repo-ref}
  bindings:
    - role: roles/source.reader
      members:
        - member: serviceAccount:sync-gke-cluster@project-id.iam.gserviceaccount.com # kpt-set: serviceAccount:sa-acm-${cluster-name}@${project-id}.iam.gserviceaccount.com
