Secure Google Cloud authentication for local tools: service account impersonation
When you use the gcloud CLI on your PC — for example to call Vertex AI and Gemini models from Cline inside Visual Studio Code — you need credentials that the local tool can use. The quickest path is Application Default Credentials, but the way most people create them gives far more power to the local machine than the task actually requires. This article explains the risk and shows a safer pattern based on service account impersonation, which keeps the blast radius small while remaining just as convenient.
How Application Default Credentials usually get created
The classic workflow is two commands: you authorize the CLI with your personal Google account, then you generate the default credential file. The result is a file named application_default_credentials.json stored in your local configuration directory.
gcloud auth application-default loginThat file is a refresh token tied to your user identity. If your account has broad roles — Editor, Owner, or project-level permissions on many services — the JSON file inherits all of them. Anyone who copies that file can act as you across the whole Google Cloud console account until the token is revoked. Committing it to a repository by mistake, or leaving it on a shared laptop, is a realistic and serious incident.
A better method: impersonating a service account
Instead of exporting your own identity, you can create a dedicated service account, grant it only the permissions the workload needs, and let your user impersonate it. The local file then contains only the information required to request short-lived access tokens for that service account.
gcloud auth application-default login --impersonate-service-account=SERVICE_ACCOUNT_EMAILThe setup has four steps. First, create a service account in the Google Cloud console and assign the narrowest role that works — for Vertex AI, the Vertex AI User role is usually enough. Second, enable the IAM Service Credentials API on the project, otherwise token creation fails. Third, grant your own email address the Service Account Token Creator role on that specific service account; this is the entity-level binding that allows impersonation. Fourth, run the login command with the impersonation flag, sign in through the browser, and the CLI writes a small JSON file to disk.
Comparison at a glance
| Aspect | User ADC (login) | Service account impersonation |
|---|---|---|
| Credential scope | All permissions of your user account | Only the roles granted to the service account |
| Token lifetime | Long-lived refresh token | Short-lived access token, refreshed hourly |
| Impact if the file leaks | Broad access to the whole project or organization | Limited to the service account permissions |
| Revocation | Revoke the user OAuth grant | Remove the IAM binding or the token creator role |
| Setup effort | Two commands | Four steps, one time |
Why the risk is smaller
Three properties make this pattern resilient. First, the service account only has the permissions you explicitly granted, so a stolen file cannot touch resources outside that scope. Second, you can always disable billing on the project, which immediately stops any costly usage even if a token is still valid. Third, and most conveniently, you can remove the OAuth token permissions from your Google account page; that single action breaks the link between your identity and the token generation for the service account, and every future refresh fails.
There is also an operational benefit: the credential file is disposable. If you suspect it was exposed, delete it, revoke the binding, create a new file, and continue working. No password rotation, no shared secret to redistribute.
Practical notes for Cline and Vertex AI
When you configure Cline in VS Code to use Google Vertex AI, point it at the impersonation-based credential file and select the project and region explicitly. Keep the file outside the repository, add it to .gitignore, and prefer a per-developer service account so that revoking one person's access does not disturb the rest of the team. Audit the service account role bindings periodically; least privilege drifts over time as projects grow.
Conclusion
Application Default Credentials created from your user login are convenient but hand your entire cloud identity to a local file. Service account impersonation costs a few minutes of setup and gives you scoped permissions, hourly tokens, and a clean revocation path. For everyday work with Gemini models through Cline, it is the option worth standardizing on.
Let's work together
Do you need more info, help with your project, or to develop an idea?
Whether it's an easy question, a quick doubt, or just a 5-minute chat, send me a message—it costs nothing and I'm always ready to help. I love discussing a problem to understand it, getting creative with solutions, and focusing on simple, reliable, and straightforward ideas that we can actuate quickly.
Contact me →