fixing server switch handling and oidc metadata fetch#1991
Conversation
Signed-off-by: Jan Jeliga <jeliga.jan@gmail.com>
Summary of ChangesHello @jjeliga, 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 focuses on enhancing the robustness and reliability of the CLI's authentication process. It includes improvements to how OIDC metadata is retrieved and refines the state management when switching between different authentication servers during the login flow. These changes aim to provide a more stable and predictable user experience when interacting with various server configurations. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Using Gemini Code AssistThe 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 by creating a comment using either
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 Limitations & Feedback Gemini Code Assist 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. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. 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
|
There was a problem hiding this comment.
Code Review
This pull request fixes server switching logic and how OIDC metadata is fetched. The change to use httpx.AsyncClient for fetching OIDC metadata is a good improvement. However, the refactoring of the server switching logic in server.py introduces a potential issue by eagerly saving configuration changes before validation, which could lead to an inconsistent state if the process is interrupted. I've added a detailed comment with a suggestion to fix this while maintaining the goal of improving the code structure.
| # Validate that the token is still valid by attempting to load it | ||
|
|
||
| # Temporarily set the server as active to test the token, this does not save the change yet | ||
| # Keep the original active server/auth server in case of failure | ||
| previous_server = config.auth_manager.active_server | ||
| previous_auth_server = config.auth_manager.active_auth_server | ||
| config.auth_manager._auth.active_server = server | ||
| config.auth_manager._auth.active_auth_server = auth_server | ||
|
|
||
| config.auth_manager.active_server = server | ||
| config.auth_manager.active_auth_server = auth_server | ||
|
|
||
| try: | ||
| token = await config.auth_manager.load_auth_token() | ||
| if not token: | ||
| # No token available, need to log in | ||
| # Restore previous state until login completes | ||
| config.auth_manager._auth.active_server = previous_server | ||
| config.auth_manager._auth.active_auth_server = previous_auth_server | ||
| config.auth_manager.active_server = previous_server | ||
| config.auth_manager.active_auth_server = previous_auth_server | ||
| # Fall through to login flow below | ||
| else: | ||
| # Token is valid, switch to this server (setters handle saving) | ||
| config.auth_manager.active_server = server | ||
| config.auth_manager.active_auth_server = auth_server | ||
| console.success(f"Logged in to [cyan]{server}[/cyan].") | ||
| return | ||
| except InvalidGrantError: | ||
| # Token refresh failed due to invalid/expired refresh token | ||
| log_in_message = "Your session has expired. Please log in again." | ||
| # Restore previous state until login completes | ||
| config.auth_manager._auth.active_server = previous_server | ||
| config.auth_manager._auth.active_auth_server = previous_auth_server | ||
| config.auth_manager.active_server = previous_server | ||
| config.auth_manager.active_auth_server = previous_auth_server | ||
| # Fall through to login flow below | ||
| except OAuth2Error as e: | ||
| # Other OAuth2 protocol errors - report but don't continue | ||
| console.error(f"OAuth2 error: {e.description}") | ||
| config.auth_manager._auth.active_server = previous_server | ||
| config.auth_manager._auth.active_auth_server = previous_auth_server | ||
| config.auth_manager.active_server = previous_server | ||
| config.auth_manager.active_auth_server = previous_auth_server | ||
| sys.exit(1) | ||
| except RuntimeError as e: | ||
| # Network or OIDC discovery errors - report but don't continue | ||
| console.error(f"Failed to validate authentication: {e}") | ||
| console.hint("Check your network connection and try again.") | ||
| config.auth_manager._auth.active_server = previous_server | ||
| config.auth_manager._auth.active_auth_server = previous_auth_server | ||
| config.auth_manager.active_server = previous_server | ||
| config.auth_manager.active_auth_server = previous_auth_server | ||
| sys.exit(1) |
There was a problem hiding this comment.
While replacing direct access to the private _auth attribute with the public property setters is a good refactoring goal, the current implementation introduces a regression in behavior. The property setters immediately save the configuration to disk.
This changes the logic to an "optimistic" update:
- The new server is set as active and saved to disk before validation.
- If validation fails, an attempt is made to revert the change and save again.
This creates a race condition. If the process is terminated during token validation (e.g., by Ctrl+C or a crash), the configuration file will be left in an inconsistent state, with the new server marked as active even though the switch was not successful.
The previous logic was safer as it only modified the configuration in memory and saved it to disk once, upon successful validation. To restore this safer transactional behavior, the temporary switch should not be persisted.
A long-term solution would be to refactor AuthManager to better support this pattern, but for now, it's best to revert to the previous logic for this block to avoid potential configuration corruption.
# Validate that the token is still valid by attempting to load it
# Temporarily set the server as active to test the token, this does not save the change yet
previous_server = config.auth_manager.active_server
previous_auth_server = config.auth_manager.active_auth_server
config.auth_manager._auth.active_server = server
config.auth_manager._auth.active_auth_server = auth_server
try:
token = await config.auth_manager.load_auth_token()
if not token:
# No token available, need to log in
# Restore previous state until login completes
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
# Fall through to login flow below
else:
# Token is valid, switch to this server (setters handle saving)
config.auth_manager.active_server = server
config.auth_manager.active_auth_server = auth_server
console.success(f"Logged in to [cyan]{server}[/cyan].")
return
except InvalidGrantError:
# Token refresh failed due to invalid/expired refresh token
log_in_message = "Your session has expired. Please log in again."
# Restore previous state until login completes
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
# Fall through to login flow below
except OAuth2Error as e:
# Other OAuth2 protocol errors - report but don't continue
console.error(f"OAuth2 error: {e.description}")
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
sys.exit(1)
except RuntimeError as e:
# Network or OIDC discovery errors - report but don't continue
console.error(f"Failed to validate authentication: {e}")
console.hint("Check your network connection and try again.")
config.auth_manager._auth.active_server = previous_server
config.auth_manager._auth.active_auth_server = previous_auth_server
sys.exit(1)
Summary
Fixing some CLI login logic
Linked Issues
None
Documentation
If this PR adds new feature or changes existing. Make sure documentation is adjusted accordingly. If the docs is not needed, please explain why.