id: gcloud-sql-log-checkpoints-disabled

info:
  name: Log Checkpoints Disabled in PostgreSQL Database Instances
  author: princechaddha
  severity: medium
  description: |
    Ensure that the "log_checkpoints" database flag is enabled for your Google Cloud PostgreSQL database instances. The "log_checkpoints" flag allows checkpoints and restart points to be logged and included within the PostgreSQL server log.
  impact: |
    Without enabling "log_checkpoints", crucial log data related to checkpoints and restart points will not be captured, making it difficult to monitor and troubleshoot PostgreSQL database performance and issues.
  remediation: |
    Enable the "log_checkpoints" flag for PostgreSQL database instances to ensure logging of checkpoints and restart points for better observability.
  reference:
    - https://cloud.google.com/sql/docs/postgres/configure-instance-settings
  tags: cloud,devops,gcp,gcloud,google-cloud-sql,postgresql,gcp-cloud-config

flow: |
  code(1)
  for(let projectId of iterate(template.projectIds)){
    set("projectId", projectId)
    code(2)
    for(let sqlInstance of iterate(template.sqlInstances)){
      set("sqlInstance", sqlInstance)
      code(3)
    }
  }

self-contained: true

code:
  - engine:
      - sh
      - bash
    source: |
      gcloud projects list --format="json(projectId)"

    extractors:
      - type: json
        name: projectIds
        internal: true
        json:
          - '.[].projectId'

  - engine:
      - sh
      - bash
    source: |
      gcloud sql instances list --project $projectId --filter='DATABASE_VERSION:POSTGRES*' --format="json(name)"

    extractors:
      - type: json
        name: sqlInstances
        internal: true
        json:
          - '.[].name'

  - engine:
      - sh
      - bash
    source: |
      gcloud sql instances describe $sqlInstance --format=json | jq '.settings.databaseFlags // [] | map(select(.name == "log_checkpoints")) | .[0]?.value'

    matchers:
      - type: word
        words:
          - 'off'

    extractors:
      - type: dsl
        dsl:
          - '"Log Checkpoints flag is disabled in PostgreSQL instance: " + sqlInstance + " in project: " + projectId'
# digest: 4a0a004730450221008cf3769178c9c1f1a64439433e5084956ab37193ce4e548abbe7ec1926e96bc502202109edd86c07ad806b0231c89b7eb988692814620a613b4f4b01c5e69d0609a0:922c64590222798bb761d5b6d8e72950