Service account keys management in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When managing service account keys, it's important to understand how the number of keys affects the time it takes to list, create, or delete them.
We want to know how the work grows as we handle more keys.
Analyze the time complexity of the following operation sequence.
// List all keys for a service account
keys = iam.projects.serviceAccounts.keys.list({
name: 'projects/-/serviceAccounts/service-account-email'
})
// Delete each key one by one
for (const key of keys.keys) {
iam.projects.serviceAccounts.keys.delete({
name: key.name
})
}
// Create a new key
newKey = iam.projects.serviceAccounts.keys.create({
name: 'projects/-/serviceAccounts/service-account-email'
})
This sequence lists all keys, deletes them one by one, then creates a new key for a service account.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Deleting each service account key individually.
- How many times: Once per existing key, so the number grows with the number of keys.
As the number of keys increases, the number of delete calls grows directly with it.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | List (1) + Delete (10) + Create (1) = 12 |
| 100 | List (1) + Delete (100) + Create (1) = 102 |
| 1000 | List (1) + Delete (1000) + Create (1) = 1002 |
Pattern observation: The total operations increase roughly in direct proportion to the number of keys.
Time Complexity: O(n)
This means the time to manage keys grows linearly with the number of keys you have.
[X] Wrong: "Deleting all keys happens in one single API call regardless of how many keys exist."
[OK] Correct: Each key must be deleted individually, so the number of delete calls grows with the number of keys.
Understanding how operations scale with resource count shows you can plan and manage cloud resources efficiently, a key skill in real projects.
"What if we batch delete keys in groups instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand service account keys
Service account keys are used by programs, not humans, to access Google Cloud securely.Step 2: Identify the correct purpose
They provide credentials for applications to authenticate and interact with cloud services.Final Answer:
To allow programs to securely access Google Cloud resources -> Option CQuick Check:
Service account keys = secure program access [OK]
- Confusing keys with user passwords
- Thinking keys manage billing
- Believing keys create virtual machines
my-service-account@my-project.iam.gserviceaccount.com?Solution
Step 1: Identify correct gcloud command syntax
The correct command to create a key isgcloud iam service-accounts keys createwith the--iam-accountflag.Step 2: Match the command with the options
gcloud iam service-accounts keys create key.json --iam-account=my-service-account@my-project.iam.gserviceaccount.com matches the correct syntax exactly.Final Answer:
gcloud iam service-accounts keys create key.json --iam-account=my-service-account@my-project.iam.gserviceaccount.com -> Option AQuick Check:
Correct command syntax = gcloud iam service-accounts keys create key.json --iam-account=my-service-account@my-project.iam.gserviceaccount.com [OK]
- Using 'create' without 'keys'
- Wrong flag like --account instead of --iam-account
- Omitting 'iam' in the command
gcloud iam service-accounts keys list --iam-account=my-service-account@my-project.iam.gserviceaccount.comAssuming there are two active keys for this service account.
Solution
Step 1: Understand the command purpose
The command lists keys for the specified service account.Step 2: Interpret expected output
Since two active keys exist, the output will show their details like key IDs and creation dates.Final Answer:
A list showing details of the two active keys including key IDs and creation dates -> Option DQuick Check:
Listing keys shows active keys details [OK]
- Expecting an error if keys exist
- Confusing keys list with service accounts list
- Thinking it prompts for key creation
gcloud iam service-accounts keys delete 123abc --iam-account=my-service-account@my-project.iam.gserviceaccount.comBut get an error saying the key ID does not exist. What is the most likely cause?
Solution
Step 1: Analyze the error message
The error says the key ID does not exist, meaning the key ID is invalid or not linked to the service account.Step 2: Check command components
The service account email may be correct, and project ID is not required here if default is set. Creating a key before deleting is unnecessary.Final Answer:
The key ID is incorrect or does not belong to the specified service account -> Option AQuick Check:
Invalid key ID causes deletion error [OK]
- Assuming project ID is mandatory in this command
- Thinking you must create a key before deleting
- Ignoring key ID correctness
Solution
Step 1: Understand key rotation best practice
To avoid downtime, first create a new key and update applications to use it.Step 2: Remove old key after update
Once applications use the new key, delete the old key to reduce risk.Final Answer:
Create a new key, update your applications to use it, then delete the old key -> Option BQuick Check:
New key first, then delete old key [OK]
- Deleting old key before updating apps
- Using multiple keys unnecessarily
- Waiting for old key to expire before rotating
