# Copyright 2022 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.
######
# Logs Bucket writer IAM permissions for security log sink
# Binds the generated writer identity from the LoggingLogSink to the logging project
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
  name: security-log-bucket-writer-permissions
  namespace: projects
  annotations:
    config.kubernetes.io/depends-on: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id} # kpt-set: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id}
spec:
  resourceRef:
    kind: Project
    name: logging-prj-id # kpt-set: ${logging-prj-id}
    namespace: projects
  # AC-3(7) ACCESS ENFORCEMENT | ROLE-BASED ACCESS CONTROL
  bindings:
    - role: roles/logging.bucketWriter
      members:
        - memberFrom:
            logSinkRef:
              name: security-log-sink
              namespace: logging
---
# Logs Bucket writer IAM permissions for the platform and component log sink
# Binds the generated writer identity from the LoggingLogSink to the logging project
# AC-3(7) - Write access to the logging buckets is limited by IAM to just the identities of the log sinks configured to send logs to the buckets (set at the logging project level)
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
  name: platform-and-component-log-bucket-writer-permissions
  namespace: projects
  annotations:
    config.kubernetes.io/depends-on: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id} # kpt-set: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id}
spec:
  resourceRef:
    kind: Project
    name: logging-prj-id # kpt-set: ${logging-prj-id}
    namespace: projects
  # AC-3(7) ACCESS ENFORCEMENT | ROLE-BASED ACCESS CONTROL
  bindings:
    - role: roles/logging.bucketWriter
      members:
        - memberFrom:
            logSinkRef:
              name: platform-and-component-log-sink
              namespace: logging
---
# Logs Bucket writer IAM permissions for the platform and component log sink
# Binds the generated writer identity from the LoggingLogSink to the logging project
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMPartialPolicy
metadata:
  name: gke-kcc-cluster-platform-and-component-log-bucket-writer-permissions
  namespace: projects
  annotations:
    config.kubernetes.io/depends-on: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id} # kpt-set: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id}
spec:
  resourceRef:
    kind: Project
    name: logging-prj-id # kpt-set: ${logging-prj-id}
    namespace: projects
  # AC-3(7)
  bindings:
    - role: roles/logging.bucketWriter
      members:
        - memberFrom:
            logSinkRef:
              name: gke-kcc-cluster-platform-and-component-log-sink
              namespace: logging
---
# Enable data access log configuration on the logging project
apiVersion: iam.cnrm.cloud.google.com/v1beta1
kind: IAMAuditConfig
metadata:
  name: logging-project-data-access-log-config
  namespace: projects
  annotations:
    config.kubernetes.io/depends-on: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id} # kpt-set: resourcemanager.cnrm.cloud.google.com/namespaces/projects/Project/${logging-prj-id}
spec:
  service: allServices
  auditLogConfigs:
    - logType: ADMIN_READ
    - logType: DATA_READ
    - logType: DATA_WRITE
  resourceRef:
    kind: Project
    name: logging-prj-id # kpt-set: ${logging-prj-id}
    namespace: projects
---
# TODO
# IAMPolicyMember
# IAM role to enable Access Transparency Admin (roles/axt.admin) through the logging project
# https://cloud.google.com/cloud-provider-access-management/access-transparency/docs/enable
