-
Notifications
You must be signed in to change notification settings - Fork 819
fix(auth): auto-recover from credential decryption failures after upgrade #398
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
Open
zerone0x
wants to merge
1
commit into
googleworkspace:main
Choose a base branch
from
zerone0x:fix/auth-decryption-recovery
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+81
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -207,8 +207,32 @@ async fn load_credentials_inner( | |
|
|
||
| // 2. Encrypted credentials (always AuthorizedUser for now) | ||
| if enc_path.exists() { | ||
| let json_str = credential_store::load_encrypted_from_path(enc_path) | ||
| .context("Failed to decrypt credentials")?; | ||
| let json_str = match credential_store::load_encrypted_from_path(enc_path) { | ||
| Ok(s) => s, | ||
| Err(e) => { | ||
| // Decryption failed — the encryption key likely changed (e.g. after | ||
| // an upgrade that migrated keys between keyring and file storage). | ||
| // Remove the stale file so the next `gws auth login` starts fresh, | ||
| // and fall through to other credential sources. | ||
| eprintln!( | ||
| "warning: removing undecryptable credentials file ({}): {e:#}", | ||
| enc_path.display() | ||
| ); | ||
| let _ = std::fs::remove_file(enc_path); | ||
| // Also remove any stale token cache that used the old key. | ||
| let token_cache = enc_path.with_file_name("token_cache.json"); | ||
| let _ = std::fs::remove_file(&token_cache); | ||
| let sa_token_cache = enc_path.with_file_name("sa_token_cache.json"); | ||
| let _ = std::fs::remove_file(&sa_token_cache); | ||
|
|
||
| // Fall through to remaining credential sources below. | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| anyhow::bail!( | ||
| "Encrypted credentials could not be decrypted (key may have changed \ | ||
| after an upgrade). The stale file has been removed automatically. \ | ||
| Please run `gws auth login` to re-authenticate." | ||
| ); | ||
| } | ||
| }; | ||
|
|
||
| let creds: serde_json::Value = | ||
| serde_json::from_str(&json_str).context("Failed to parse decrypted credentials")?; | ||
|
|
@@ -614,6 +638,37 @@ mod tests { | |
| } | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[serial_test::serial] | ||
| async fn test_load_credentials_corrupt_encrypted_file_is_removed() { | ||
| // When credentials.enc cannot be decrypted, the file should be removed | ||
| // automatically so that a subsequent `gws auth login` starts fresh. | ||
| let tmp = tempfile::tempdir().unwrap(); | ||
| let _home_guard = EnvVarGuard::set("HOME", tmp.path()); | ||
| let _adc_guard = EnvVarGuard::remove("GOOGLE_APPLICATION_CREDENTIALS"); | ||
|
|
||
| let dir = tempfile::tempdir().unwrap(); | ||
| let enc_path = dir.path().join("credentials.enc"); | ||
|
|
||
| // Write garbage data that cannot be decrypted | ||
| std::fs::write(&enc_path, b"not-valid-encrypted-data-at-all-1234567890").unwrap(); | ||
| assert!(enc_path.exists()); | ||
|
|
||
| let result = load_credentials_inner(None, &enc_path, &PathBuf::from("/does/not/exist")) | ||
| .await; | ||
|
|
||
| assert!(result.is_err()); | ||
| let msg = result.unwrap_err().to_string(); | ||
| assert!( | ||
| msg.contains("could not be decrypted"), | ||
| "Error should mention decryption failure, got: {msg}" | ||
| ); | ||
| assert!( | ||
| !enc_path.exists(), | ||
| "Stale credentials.enc must be removed after decryption failure" | ||
| ); | ||
| } | ||
|
|
||
| #[tokio::test] | ||
| #[serial_test::serial] | ||
| async fn test_get_token_env_var_empty_falls_through() { | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment on line 216 is misleading. It states that the code will "fall through to other credential sources", but the
anyhow::bail!macro on line 229 causes the function to return an error immediately, preventing any fall-through. This makes the code's control flow difficult to understand. The code's behavior (bailing out) is correct, but the comment should be updated to reflect this.