Skip to content

Commit 660d22c

Browse files
authored
cli: automatically use file storage if storage in keychain fails (microsoft#189336)
Fixes microsoft#187380
1 parent 150d819 commit 660d22c

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

cli/src/auth.rs

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ impl StoredCredential {
161161

162162
struct StorageWithLastRead {
163163
storage: Box<dyn StorageImplementation>,
164+
fallback_storage: Option<FileStorage>,
164165
last_read: Cell<Result<Option<StoredCredential>, WrappedError>>,
165166
}
166167

@@ -392,14 +393,18 @@ impl Auth {
392393
let mut keyring_storage = ThreadKeyringStorage::default();
393394
let mut file_storage = FileStorage(PersistedState::new(self.file_storage_path.clone()));
394395

395-
let keyring_storage_result = match std::env::var("VSCODE_CLI_USE_FILE_KEYCHAIN") {
396-
Ok(_) => Err(wrap("", "user prefers file storage").into()),
397-
_ => keyring_storage.read(),
396+
let native_storage_result = if std::env::var("VSCODE_CLI_USE_FILE_KEYCHAIN").is_ok()
397+
|| self.file_storage_path.exists()
398+
{
399+
Err(wrap("", "user prefers file storage").into())
400+
} else {
401+
keyring_storage.read()
398402
};
399403

400-
let mut storage = match keyring_storage_result {
404+
let mut storage = match native_storage_result {
401405
Ok(v) => StorageWithLastRead {
402406
last_read: Cell::new(Ok(v)),
407+
fallback_storage: Some(file_storage),
403408
storage: Box::new(keyring_storage),
404409
},
405410
Err(e) => {
@@ -410,6 +415,7 @@ impl Auth {
410415
.read()
411416
.map_err(|e| wrap(e, "could not read from file storage")),
412417
),
418+
fallback_storage: None,
413419
storage: Box::new(file_storage),
414420
}
415421
}
@@ -531,7 +537,18 @@ impl Auth {
531537
"Failed to update keyring with new credentials: {}",
532538
e
533539
);
540+
541+
if let Some(fb) = storage.fallback_storage.take() {
542+
storage.storage = Box::new(fb);
543+
match storage.storage.store(creds.clone()) {
544+
Err(e) => {
545+
warning!(self.log, "Also failed to update fallback storage: {}", e)
546+
}
547+
Ok(_) => debug!(self.log, "Updated fallback storage successfully"),
548+
}
549+
}
534550
}
551+
535552
storage.last_read.set(Ok(Some(creds)));
536553
})
537554
}

0 commit comments

Comments
 (0)