id: gcloud-pg-log-planner-stats-enabled

info:
  name: Log Planner Stats Enabled in PostgreSQL Database Instances
  author: princechaddha
  severity: medium
  description: |
    Ensure that the log_planner_stats database flag is disabled for your Google Cloud PostgreSQL database instances in order to avoid performance issues caused by excessive logging. The log_planner_stats flag controls the inclusion of PostgreSQL planner performance statistics in the PostgreSQL logs for each query.
  impact: |
    Enabling the log_planner_stats flag may lead to significant performance degradation due to excessive logging, impacting the database's overall efficiency.
  remediation: |
    Disable the log_planner_stats flag in your PostgreSQL database instance configuration to prevent performance issues caused by excessive logging.
  reference:
    - https://cloud.google.com/sql/docs/postgres/flags
  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 --project $projectId --format=json | jq '.settings.databaseFlags // [] | .[] | select(.name=="log_planner_stats") | .value'

    matchers:
      - type: word
        words:
          - 'on'

    extractors:
      - type: dsl
        dsl:
          - '"The log_planner_stats flag is enabled for PostgreSQL instance " + sqlInstance + " in project " + projectId'
# digest: 490a004630440220781d7f2de970ecb796d59a50d7c39cc04de36071d57aada59996d4ab24c8aca602206bf965fc0978cab7609b57e03d83d4c5995ebf492e221f4473a5a2fe7cd01afc:922c64590222798bb761d5b6d8e72950