SSH Keys Should Be Instance Specific
More Info:
Instances should not be configured to allow project-wide SSH keys. To support the principle of least privilege and prevent potential privilege escalation, instances should not be given access to project-wide SSH keys.
Risk Level
Medium
Address
Security
Compliance Standards
- CIS GCP
- Cloudanix Best Practice
Triage and Remediation
- Remediation
Remediation
Using Console
To remediate the misconfiguration "SSH Keys Should Be Instance Specific" for GCP using GCP console, you can follow the below steps:
- Login to the GCP console and select the project where the instance is running.
- In the left navigation menu, select "Compute Engine" and then click on "VM instances".
- Select the instance for which you want to remediate the misconfiguration.
- Click on "Edit" button at the top of the page.
- Scroll down to the "SSH Keys" section and click on "Show and edit".
- Remove any public SSH keys that are not specific to the instance.
- Add new SSH keys that are specific to the instance by clicking on "Add item" and pasting the public key in the text box.
- Click on "Save" to save the changes.
By following these steps, you have successfully remediated the misconfiguration "SSH Keys Should Be Instance Specific" for GCP using GCP console.
Using CLI
To remediate the misconfiguration of SSH keys not being instance-specific in GCP using GCP CLI, follow these steps:
-
Open the Cloud Shell in the GCP console.
-
Check the current SSH keys in your project using the following command:
gcloud compute os-login ssh-keys list
- Identify the SSH keys that are not instance-specific and remove them using the following command:
gcloud compute os-login ssh-keys remove --key-file=<path-to-key-file>
- Create a new instance-specific SSH key using the following command:
ssh-keygen -t rsa -f ~/.ssh/<key-file-name> -C <comment>
- Add the new SSH key to your project using the following command:
gcloud compute os-login ssh-keys add --key-file=<path-to-key-file>
-
Create a new instance in GCP and specify the new SSH key as the metadata for the instance.
-
Connect to the new instance using the new instance-specific SSH key.
By following these steps, you can remediate the misconfiguration of SSH keys not being instance-specific in GCP using GCP CLI.
Using Python
To remediate the misconfiguration "SSH Keys Should Be Instance Specific" for GCP using Python, you can follow the below steps:
-
First, you need to create a new SSH key pair for the instance. You can use the
paramikolibrary in Python to generate an SSH key pair.import paramikossh = paramiko.SSHClient()ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())ssh.connect('instance-ip-address', username='username', password='password')ssh.exec_command('ssh-keygen -t rsa') -
Once you have generated the SSH key pair, you need to add the public key to the instance's metadata. You can use the
google-authandgoogle-api-python-clientlibraries in Python to interact with GCP APIs.from google.oauth2 import service_accountfrom googleapiclient.discovery import buildcredentials = service_account.Credentials.from_service_account_file('path/to/credentials.json')compute = build('compute', 'v1', credentials=credentials)project_id = 'your-project-id'zone = 'instance-zone'instance_name = 'instance-name'instance = compute.instances().get(project=project_id, zone=zone, instance=instance_name).execute()metadata = instance['metadata']items = metadata.get('items', [])# Remove any existing SSH keysitems = [item for item in items if item['key'] != 'ssh-keys']# Add the new SSH keyssh_key = 'ssh-rsa <public-key> instance-specific-key'items.append({'key': 'ssh-keys', 'value': ssh_key})# Update the instance metadatametadata['items'] = itemscompute.instances().setMetadata(project=project_id, zone=zone, instance=instance_name, body={'metadata': metadata}).execute() -
Finally, you can test the new SSH key by connecting to the instance using the private key.
ssh.connect('instance-ip-address', username='username', key_filename='/path/to/private/key')
By following these steps, you can remediate the misconfiguration "SSH Keys Should Be Instance Specific" for GCP using Python.
Using Terraform
resource "google_compute_instance" "INSTANCE_NAME" {
name = "INSTANCE_NAME" # replace with your instance name
machine_type = "MACHINE_TYPE" # e.g. "e2-medium"
zone = "ZONE" # e.g. "us-central1-a"
boot_disk {
initialize_params {
image = "BOOT_DISK_IMAGE" # e.g. "debian-cloud/debian-12"
}
}
network_interface {
network = "NETWORK_SELF_LINK_OR_NAME"
}
# Ensure project-wide SSH keys are blocked on this instance
metadata = {
block-project-ssh-keys = "true"
# add any other existing metadata keys here
}
}
This change does not force replacement of the instance; Terraform will update instance metadata in place.
For verification, terraform plan should show an in-place update to google_compute_instance.INSTANCE_NAME with metadata.block-project-ssh-keys changing from false (or null) to "true".