Other Articles

VPC Firewall – Restrict Public Database Access

Secure Configuration Checks > GCP

This check ensures that no VPC firewall rule allows common database ports to be reached from the entire internet. The ports are MySQL (3306), PostgreSQL (5432), SQL Server (1433), MongoDB (27017), Redis (6379) and Elasticsearch (9200, 9300).

Check Details

  • Resource: VPC Network (firewall rules)
  • Check: Restrict public database access
  • Risk: Exposed databases are a leading cause of data theft and ransom attacks. Attackers can read, change or delete the data.

Remediation via Google Cloud Console

  1. Log in to the Google Cloud Console and navigate to VPC network > Firewall. VPC Firewall Rules with database ports
  2. Find enabled ingress allow rules that expose database ports to 0.0.0.0/0 or ::/0. Also check rules that allow all TCP ports or all protocols.
  3. Click the firewall rule name and then click Edit. Edit VPC Firewall Rule
  4. Under Source filter, replace the public source range with trusted application IP ranges or appropriate source tags or service accounts. Check the IPv4 and IPv6 source ranges separately where applicable. Restrict VPC Firewall Source Ranges
  5. Review Protocols and ports to ensure that only the required database ports are allowed. Click Save.
  6. If the firewall rule is not required, select it and click Delete instead.
  7. Repeat these steps for every affected firewall rule.

Remediation via Google Cloud CLI

  1. Open the Google Cloud Console and launch Cloud Shell. GCP Cloud Shell
  2. List the enabled ingress rules that allow a database port from the internet:

    PORTS="3306 5432 1433 27017 6379 9200 9300"
    
    gcloud compute firewall-rules list --format=json | \
    jq -r --arg ports "$PORTS" '
      ($ports | split(" ") | map(tonumber)) as $p
      | .[]
      | select(.direction == "INGRESS" and (.disabled | not))
      | select((.sourceRanges // []) | any(. == "0.0.0.0/0" or . == "::/0"))
      | select(any(.allowed[]?;
          .IPProtocol == "all"
          or (.IPProtocol == "tcp"
              and (.ports == null
                   or any((.ports[] | split("-") | map(tonumber)) as $r | $p[];
                          . >= $r[0] and . <= $r[-1])))))
      | [.name, (.network | split("/") | last)]
      | @tsv'
    

    The output shows the rule name and its network. Cloud SQL instances with a public IP are not covered by firewall rules. See Cloud SQL – Protect From Internet Access.

  3. Limit the source of the rule to the range of your application subnet:

    gcloud compute firewall-rules update <RULE_NAME> --source-ranges=<TRUSTED_CIDR>
    

    Replace <RULE_NAME> with the rule name and <TRUSTED_CIDR> with an internal range, for example 10.0.1.0/24. If the rule also allows other ports, create a separate rule for them first.

  4. Or delete the rule if it is not needed:

    gcloud compute firewall-rules delete <RULE_NAME>
    
  5. Verify the rule:

    gcloud compute firewall-rules describe <RULE_NAME> \
    --format="yaml(name,sourceRanges,allowed)"
    
    Verify VPC firewall rule for database ports

Default Value

By default, no database port is open to the internet. Ingress traffic is denied unless a rule allows it.

Updated on 09 November, 2026