VM instance allows project-wide SSH keys

For VMs using metadata-based SSH, review the need for project-wide keys and allow only required instance access.

Description

A VM that uses metadata-based SSH can accept project keys without a separate entry on the instance. OS Login ignores SSH keys in both project and instance metadata.

Potential impact

An obsolete or exposed project key can grant access to several VMs.

Remediation

Before blocking project keys, verify that operators and automation can connect with approved instance keys or OS Login. On a VM that will keep metadata-based SSH, set block-project-ssh-keys: "TRUE". In google.cloud 1.14.0, gcp_compute_instance cannot update existing VM metadata. Use the Google Cloud console, gcloud or the API for an existing VM, then test a new connection.

Examples

These are metadata excerpts for creating a VM. Supply the name, project, machine type, disk and service account credentials.

Before

yaml
- name: ssh_keys_unblocked
  google.cloud.gcp_compute_instance:
    metadata:
      block-project-ssh-keys: "FALSE"
    zone: us-central1-a
    auth_kind: serviceaccount

- name: no_metadata
  google.cloud.gcp_compute_instance:
    zone: us-central1-a
    auth_kind: serviceaccount

Neither "FALSE" nor omission explicitly blocks project keys. Actual access depends on the login method and network path.

After

yaml
- name: ssh_keys_blocked
  google.cloud.gcp_compute_instance:
    metadata:
      block-project-ssh-keys: "TRUE"
    zone: us-central1-a
    auth_kind: serviceaccount

With "TRUE", instance keys still work for metadata-based SSH, and the keys stored on the project remain available to other VMs.

References