Enable Gemini CLI to reuse user's auth in Cloud Shell by mboshernitsan Β· Pull Request #3070 Β· google-gemini/gemini-cli Β· GitHub | Latest TMZ Celebrity News & Gossip | Watch TMZ Live
Skip to content

Enable Gemini CLI to reuse user's auth in Cloud Shell #3070

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mboshernitsan
Copy link
Collaborator

TLDR

Dive Deeper

Reviewer Test Plan

Testing Matrix

🍏 πŸͺŸ 🐧
npm run ❓ ❓ ❓
npx ❓ ❓ ❓
Docker ❓ ❓ ❓
Podman ❓ - -
Seatbelt ❓ - -

Linked issues / bugs

Introduces a non-interactive authentication flow for the Gemini CLI when running in a Cloud Shell environment. This change streamlines the user experience by automatically selecting an authentication method based on the environment, rather than prompting the user.

The new authentication logic is as follows:
1.  If a user has explicitly set an 'selectedAuthType' in their 'settings.json', that setting will be respected.
2.  If no 'selectedAuthType' is set, the CLI will attempt to authenticate using the following methods in order:
 1.  Gemini API key, if the 'GEMINI_API_KEY' environment variable is set.
 2.  Vertex AI, if the 'GOOGLE_GENAI_USE_VERTEXAI' environment variable is set to 'true'.
 3.  Application Default Credentials (ADC) as a fallback.  Cloud Shell has special logic to provide ADC for the logged in user identity via its metadata server; this enables users to authenticate using the same credentials with which they logged into Cloud Shell without an extra login step.
3.  The selected authentication type is not persisted to 'settings.json' when using the automatic flow, but OAuth credentials are still cached to 'oauth_creds.json' to allow for token refreshes.
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary of Changes

Hello @mboshernitsan, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly improves the authentication experience for the Gemini CLI when operating within Google Cloud Shell. It introduces logic to automatically detect the Cloud Shell environment and leverage Application Default Credentials (ADC) for seamless authentication, prioritizing existing user credentials. This change aims to reduce friction for users by allowing them to utilize their Cloud Shell login without additional configuration, while also refining the default authentication type selection and enhancing test coverage for these new flows.

Highlights

  • Enhanced Cloud Shell Authentication: The Gemini CLI now intelligently detects if it's running in Google Cloud Shell. If so, it prioritizes authentication methods by attempting to leverage Application Default Credentials (ADC) first, allowing users to reuse their existing Cloud Shell login without manual intervention or explicit API keys.
  • Prioritized Auth Type Selection: Within the Cloud Shell environment, the CLI now cascades through GEMINI_API_KEY, Vertex AI, and Google Login as default authentication types if no explicit type is set. Crucially, these default choices are not persisted to the user's settings file when in Cloud Shell.
  • Application Default Credentials (ADC) Integration: The core oauth2 module has been updated to integrate with the Compute client from google-auth-library. This enables the CLI to acquire credentials seamlessly via ADC in environments like Cloud Shell, providing a more native and secure authentication experience.
  • Improved Error Handling and Test Coverage: Error handling for retrieving Google Account IDs from existing credentials has been made non-fatal, allowing the application to continue. Additionally, comprehensive new tests have been added to validate the Cloud Shell authentication flow, covering scenarios for cached credentials, successful ADC usage, and ADC failures.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with πŸ‘ and πŸ‘Ž on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution. ↩

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request enables the Gemini CLI to use a user's existing authentication within Cloud Shell via Application Default Credentials (ADC). A high-priority suggestion is to cache credentials obtained through ADC when they are refreshable to improve performance.

Comment on lines +99 to +101

// Do not cache creds in this case; note that Compute client will handle its own refresh
return computeClient;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The current implementation doesn't cache credentials obtained via Application Default Credentials (ADC). ADC can resolve to different credential types. While credentials from a metadata server are short-lived and shouldn't be cached, ADC can also use user credentials from gcloud auth application-default login, which do contain a refresh token and are long-lived.

Not caching these refreshable credentials is a missed optimization. It forces the CLI to run the full ADC flow on every execution, which can be slow. Caching credentials when a refresh_token is present would align with user expectations and improve performance.

      // If ADC provides refreshable credentials (e.g. from gcloud user), cache them for efficiency.
      if (computeClient.credentials?.refresh_token) {
        await cacheCredentials(computeClient.credentials);
      }
      return computeClient;

@gemini-cli gemini-cli bot added the status/need-issue Pull request needs to be associated with an issue label Jul 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
status/need-issue Pull request needs to be associated with an issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant

TMZ Celebrity News – Breaking Stories, Videos & Gossip

Looking for the latest TMZ celebrity news? You've come to the right place. From shocking Hollywood scandals to exclusive videos, TMZ delivers it all in real time.

Whether it’s a red carpet slip-up, a viral paparazzi moment, or a legal drama involving your favorite stars, TMZ news is always first to break the story. Stay in the loop with daily updates, insider tips, and jaw-dropping photos.

πŸŽ₯ Watch TMZ Live

TMZ Live brings you daily celebrity news and interviews straight from the TMZ newsroom. Don’t miss a beatβ€”watch now and see what’s trending in Hollywood.