Description
Allow TCP 22 only from approved administration sources to the VMs that need SSH. With 0.0.0.0/0, any IPv4 source with a route can attempt a connection to a listening SSH service.
Potential impact
A reachable SSH service can receive login attempts using compromised keys from outside the administration network.
Remediation
Test the VPN, bastion or IAP path before removing broad IPv4 or IPv6 allowances. Check overlapping rules and priorities, consider OS Login and multi-factor authentication, and test both allowed and denied connections.
Examples
Replace 10.10.0.0/24 with the actual management range. Neither rule specifies targets, so select the required VMs to avoid applying the rule to the whole VPC.
Before
resource "google_compute_firewall" "ssh" {
name = "public-ssh"
network = google_compute_network.default.name
direction = "INGRESS"
source_ranges = ["0.0.0.0/0"]
allow {
protocol = "tcp"
ports = ["22"]
}
}
After
resource "google_compute_firewall" "ssh" {
name = "restricted-ssh"
network = google_compute_network.default.name
direction = "INGRESS"
source_ranges = ["10.10.0.0/24"]
allow {
protocol = "tcp"
ports = ["22"]
}
}
The example also renames the firewall rule. Keep its existing name when changing only the allowed source.