id: gcloud-postgresql-log-hostname-disabled

info:
  name: Log Hostname Flag Disabled for PostgreSQL Database Instances
  author: princechaddha
  severity: medium
  description: |
    Ensure that the "log_hostname" database flag is enabled for your Google Cloud PostgreSQL database instances in order to assist with incident response and tracking usage in an environment utilizing dynamic IP addresses. There is a potential cost to server performance caused by hostname logging.
  impact: |
    If the "log_hostname" flag is disabled, tracking and responding to incidents in dynamic IP address environments becomes more challenging, reducing the visibility of database activity and increasing the risk of undetected malicious activity.
  remediation: |
    Enable the "log_hostname" database flag for all PostgreSQL database instances in your Google Cloud environment to ensure proper logging and traceability.
  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 --project=$projectId --format=json | jq '.settings.databaseFlags // [] | map(select(.name=="log_hostname")) | .[].value'

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

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