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

info:
  name: Log Parser Stats Enabled in PostgreSQL Database Instances
  author: princechaddha
  severity: medium
  description: |
    Ensure that the "log_parser_stats" database flag is turned off for your Google Cloud PostgreSQL database instances in order to avoid performance issues caused by excessive logging. The PostgreSQL planner (PostgreSQL optimizer) is responsible for parsing and verifying the syntax of each query received by the database server. If the syntax is correct, a parse tree is built up; otherwise, an error is generated. The "log_parser_stats" flag controls the inclusion of parser performance statistics within PostgreSQL logs for each query made to the database.
  impact: |
    Enabling the "log_parser_stats" flag may lead to significant performance degradation due to excessive logging, impacting the database's overall efficiency.
  remediation: |
    Disable the "log_parser_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 // [] | map(select(.name=="log_parser_stats")) | .[].value'

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

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