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

info:
  name: Log Statement Stats Enabled in PostgreSQL Database Instances
  author: princechaddha
  severity: medium
  description: |
    Ensure that the "log_statement_stats" database flag is disabled for your Google Cloud PostgreSQL database instances in order to avoid performance issues caused by excessive logging. The "log_statement_stats" configuration flag controls the inclusion of end-to-end performance statistics within PostgreSQL logs for each SQL query.
  impact: |
    Enabling the "log_statement_stats" flag may lead to significant performance degradation due to excessive logging, impacting the database's overall efficiency.
  remediation: |
    Disable the "log_statement_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_statement_stats")) | .[].value'

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

    extractors:
      - type: dsl
        dsl:
          - '"The log_statement_stats flag is enabled for PostgreSQL instance " + sqlInstance + " in project " + projectId'
# digest: 4a0a00473045022055c2c14e86a7777aaba1fb27d2459d0f92041436277842bff389fc8fc390305402210099d9fb11c2e2c5b5c970f71ec0f1948fdf448fa6374c0c4799db539b29676b67:922c64590222798bb761d5b6d8e72950