Service accounts for applications in GCP - Time & Space Complexity
Start learning this pattern below
Jump into concepts and practice - no test required
When applications use service accounts, they often request tokens to access cloud resources. Understanding how the number of token requests grows helps us see how the system performs as usage increases.
We want to know: how does the number of token requests change as more applications or services use the service account?
Analyze the time complexity of the following operation sequence.
// For each application instance
for (int i = 0; i < n; i++) {
// Request an access token from the service account
token = requestAccessToken(serviceAccount);
// Use the token to call a cloud API
callCloudAPI(token);
}
This sequence shows multiple application instances each requesting a token from the same service account and then calling a cloud API.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Requesting an access token from the service account.
- How many times: Once per application instance (n times).
Each new application instance makes its own token request, so the total number of token requests grows directly with the number of instances.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 token requests |
| 100 | 100 token requests |
| 1000 | 1000 token requests |
Pattern observation: The number of token requests increases one-to-one with the number of application instances.
Time Complexity: O(n)
This means that as you add more application instances, the total token requests grow in direct proportion.
[X] Wrong: "Requesting one token can serve all application instances, so the number of requests stays the same no matter how many instances run."
[OK] Correct: Each instance usually needs its own token to authenticate separately, so requests increase with instances.
Understanding how token requests scale helps you design applications that handle authentication efficiently and avoid bottlenecks as usage grows.
"What if multiple application instances shared a cached token instead of requesting new ones each time? How would the time complexity change?"
Practice
service account in Google Cloud Platform (GCP)?Solution
Step 1: Understand service account role
A service account is a special account used by applications or virtual machines to authenticate and access Google Cloud resources securely without user intervention.Step 2: Differentiate from user accounts
User accounts are for people to log in, while service accounts are for applications or services to act on behalf of users or themselves.Final Answer:
To allow applications to authenticate and access GCP resources securely -> Option AQuick Check:
Service account = app authentication [OK]
- Confusing service accounts with user accounts
- Thinking service accounts store data
- Assuming service accounts monitor network
Solution
Step 1: Identify how to assign service accounts to VMs
Thegcloud compute instances createcommand supports a--service-accountflag to specify which service account the VM should use.Step 2: Eliminate incorrect options
Firewall rules do not assign service accounts, startup scripts do not assign service accounts, and user accounts are unrelated to service account assignment.Final Answer:
Use the--service-accountflag withgcloud compute instances create-> Option AQuick Check:
Assign service account with --service-account flag [OK]
- Trying to assign service account via firewall
- Using startup scripts to assign service accounts
- Confusing user accounts with service accounts
from google.cloud import storage client = storage.Client() buckets = list(client.list_buckets()) print(len(buckets))
What must be true for this code to successfully list buckets?
Solution
Step 1: Understand authentication requirement
Google Cloud client libraries require authentication, usually via a service account or user credentials, to access resources like buckets.Step 2: Identify required permissions
To list buckets, the service account or user must have at least the Storage Viewer role to read bucket metadata.Final Answer:
The environment must have a service account with Storage Viewer role configured -> Option BQuick Check:
Service account with Storage Viewer role needed [OK]
- Assuming user login in browser is enough
- Running code without any service account
- Thinking no auth is needed for bucket listing
Solution
Step 1: Check service account permissions
If the application cannot access Cloud Storage, the most common reason is missing IAM permissions on the service account assigned to the VM.Step 2: Rule out other causes
Lack of external IP does not block access if using private Google access; missing import causes code errors but not permission failures; service account email unrelated to VM name.Final Answer:
The service account lacks the necessary IAM permissions for Cloud Storage -> Option DQuick Check:
Missing IAM permissions cause access failure [OK]
- Assuming external IP is required for access
- Blaming code imports without error evidence
- Confusing service account email with VM name
Solution
Step 1: Identify least privilege principle
Grant only the permissions needed. For Cloud SQL access, the Cloud SQL Client role is sufficient.Step 2: Assign correct service account to Cloud Run
Create a dedicated service account with Cloud SQL Client role and assign it to the Cloud Run service to avoid over-permission.Step 3: Eliminate insecure or excessive options
Using default service account with Owner role is too broad; Storage Admin role is unrelated; embedding user credentials is insecure.Final Answer:
Create a service account with only Cloud SQL Client role and assign it to the Cloud Run service -> Option CQuick Check:
Least privilege: Cloud SQL Client role on service account [OK]
- Using overly broad Owner role
- Assigning unrelated roles like Storage Admin
- Embedding user credentials in app code
