GCP firewall allows SSH from all IPv4 addresses

Restrict SSH administration to approved sources and target VMs, and retain strong authentication.

Description

Allowing TCP 22 from every IPv4 source permits connections to reachable SSH services. In the Compute Engine API, traffic can match either a source range or a source tag.

Potential impact

A client outside the management network could attempt to sign in with a compromised SSH key.

Remediation

Test an approved IAP or VPN path first. Limit TCP 22 to the required management sources and target VMs. Check overlapping rules and priorities, OS Login, keys and IAM permissions, then test both allowed and denied connections.

Examples

Omitting the network selects the default VPC. Supply your project and service-account JSON file. The first task is historical. google.cloud 1.14.0 rejects its combination of source_tags, source_ranges and target_tags.

Before

yaml
- name: ssh_unrestricted
  google.cloud.gcp_compute_firewall:
    name: test-object
    allowed:
      - ip_protocol: tcp
        ports:
          - "22"
    target_tags:
      - test-ssh-server
      - staging-ssh-server
    source_tags:
      - test-ssh-clients
    project: "{{ project_id }}"
    auth_kind: serviceaccount
    service_account_file: "/tmp/auth.pem"
    state: present
    source_ranges:
      - "0.0.0.0/0"

In the API rule illustrated by the first task, a source tag cannot narrow 0.0.0.0/0. The target tags select which VMs the rule applies to.

After

yaml
- name: ssh_restricted
  google.cloud.gcp_compute_firewall:
    name: test-object
    allowed:
      - ip_protocol: tcp
        ports:
          - "22"
    target_tags:
      - test-ssh-server
      - staging-ssh-server
    project: "{{ project_id }}"
    auth_kind: serviceaccount
    service_account_file: /tmp/auth.pem
    state: present
    source_ranges:
      - "203.0.113.10/32"

For a direct connection from one fixed management host, replace 203.0.113.10/32 with that host’s IPv4 address using /32. For IAP TCP forwarding, use IAP’s IPv4 source range, 35.235.240.0/20.

References