SSH access and metadata in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When using SSH access with metadata in GCP, it's important to understand how the number of metadata entries affects the time it takes to establish access.
We want to know how the process scales as more SSH keys are added to the metadata.
Analyze the time complexity of updating and using SSH keys stored in instance metadata.
# Pseudocode for SSH access using metadata
metadata = getInstanceMetadata(instanceId)
for key in metadata.sshKeys:
if key matches user:
allow SSH access
else:
continue
This sequence checks each SSH key in the instance metadata to find a matching user key for access.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Iterating over each SSH key in the instance metadata to check for a match.
- How many times: Once per SSH key stored in the metadata.
As the number of SSH keys increases, the time to find a matching key grows because each key is checked one by one.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 key checks |
| 100 | 100 key checks |
| 1000 | 1000 key checks |
Pattern observation: The number of checks grows directly with the number of SSH keys.
Time Complexity: O(n)
This means the time to verify SSH access grows linearly with the number of SSH keys in metadata.
[X] Wrong: "Checking SSH access time stays the same no matter how many keys are stored."
[OK] Correct: Each key must be checked one by one, so more keys mean more checks and longer time.
Understanding how SSH access time grows with metadata size helps you design scalable and efficient access controls in cloud environments.
"What if SSH keys were stored in a separate database instead of metadata? How would the time complexity change?"
Practice
Solution
Step 1: Understand SSH access
SSH (Secure Shell) is a protocol used to securely connect to remote machines, such as virtual machines in GCP.Step 2: Identify SSH use in GCP
In GCP, SSH access allows users to securely log into VM instances to manage and operate them.Final Answer:
To securely connect to virtual machine instances -> Option AQuick Check:
SSH access = secure VM connection [OK]
- Confusing SSH with storage or monitoring services
- Thinking SSH creates VMs instead of connecting to them
Solution
Step 1: Understand where SSH keys are stored
SSH keys are stored in metadata, which is a place to keep configuration info for VMs.Step 2: Identify correct metadata key
The correct metadata key for SSH keys is 'ssh-keys' at the instance or project level.Final Answer:
Add the SSH key to the instance's metadata under the 'ssh-keys' key -> Option DQuick Check:
SSH keys stored in 'ssh-keys' metadata [OK]
- Adding SSH keys to firewall rules instead of metadata
- Trying to store SSH keys in disk storage or billing settings
{"ssh-keys": "user:ssh-rsa AAAAB3Nza... user@example.com"}What will happen when you try to SSH into this VM as 'user'?
Solution
Step 1: Analyze the metadata content
The metadata contains a valid SSH public key for user 'user' under 'ssh-keys'.Step 2: Understand SSH key usage
When connecting as 'user', the VM checks the 'ssh-keys' metadata and allows access if the matching private key is used.Final Answer:
SSH connection will succeed using the provided public key -> Option AQuick Check:
Valid SSH key in metadata = successful SSH login [OK]
- Assuming password prompt appears despite key presence
- Thinking VM restarts due to SSH metadata
Solution
Step 1: Understand project-wide SSH keys
Project-wide SSH keys apply to all instances unless blocked by instance settings.Step 2: Check instance metadata blocking
If the instance metadata has 'block-project-ssh-keys' set to true, it ignores project-wide keys.Final Answer:
The instance has block-project-ssh-keys set to true, blocking project keys -> Option CQuick Check:
block-project-ssh-keys=true blocks project keys [OK]
- Assuming firewall allows SSH means keys work
- Ignoring instance-level metadata blocking project keys
Solution
Step 1: Understand project-wide vs instance metadata
Project-wide SSH keys apply to all instances unless blocked by instance settings.Step 2: Control access per instance
Setting 'block-project-ssh-keys' to true on the instance disables project keys, allowing only instance metadata keys.Step 3: Add allowed users' keys to instance metadata
By adding only allowed users' keys to instance metadata, you restrict SSH access to them.Final Answer:
Set 'block-project-ssh-keys' to true on the instance and add allowed users' keys to instance metadata -> Option BQuick Check:
Block project keys + instance keys = controlled SSH access [OK]
- Relying only on firewall rules for SSH user control
- Removing project keys without adding instance keys
- Disabling SSH entirely when access is needed
