id: gcloud-pg-log-error-verbosity-flag-not-configured

info:
  name: Log Error Verbosity Flag Not Configured Properly for PostgreSQL Instances
  author: princechaddha
  severity: medium
  description: |
    Ensure that the "log_error_verbosity" database flag configured for your Google Cloud PostgreSQL database instances is set to DEFAULT or to a stricter value. The "log_error_verbosity" flag determines the level of detail recorded in the server log for logged messages. Valid values are TERSE, DEFAULT, and VERBOSE, with TERSE being the most restrictive and VERBOSE providing the most detail.
  impact: |
    An improperly configured "log_error_verbosity" flag can result in either insufficient logging, which hinders troubleshooting and auditing, or excessive logging, which can impact performance and increase storage costs.
  remediation: |
    Set the "log_error_verbosity" flag to DEFAULT or a stricter value (e.g., TERSE) to balance logging detail and performance, as per your organization's logging policy.
  reference:
    - https://cloud.google.com/sql/docs/postgres/flags
  tags: cloud,devops,gcp,gcloud,google-cloud-sql,postgresql,logging,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 -r '.settings.databaseFlags[]? | select(.name=="log_error_verbosity") | .value // "null"'

    matchers:
      - type: word
        words:
          - 'null'

    extractors:
      - type: dsl
        dsl:
          - '"The log_error_verbosity flag is not configured properly or is set to an inappropriate value for PostgreSQL instance " + sqlInstance + " in project " + projectId'
# digest: 4a0a0047304502207edde3b80fb30b8b2409b209fb1c71fe7c572717a47a5fb5b6c05e9eac06d1ec0221009e3f762f132986c43d9df05aafb12dc7c45522632fbed9819be10ad27f9effbb:922c64590222798bb761d5b6d8e72950