Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
handle edge cases of key ring unavailability
  • Loading branch information
g2vinay committed Aug 8, 2025
commit 01ef26901b766efd3563f698ba9dc003883a4add
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import com.azure.core.exception.ClientAuthenticationException;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.implementation.util.IdentityUtil;
import com.microsoft.aad.msal4j.ITokenCacheAccessAspect;
import com.microsoft.aad.msal4j.ITokenCacheAccessContext;
import com.microsoft.aad.msal4jextensions.PersistenceSettings;
Expand Down Expand Up @@ -87,11 +88,13 @@ private PersistenceSettings getPersistenceSettings() {
getCacheName(name != null ? name : DEFAULT_KEYRING_ITEM_NAME), DEFAULT_KEYRING_ATTR_NAME,
DEFAULT_KEYRING_ATTR_VALUE, null, null);
return persistenceSettingsBuilder.build();
} catch (KeyRingAccessException e) {
if (!allowUnencryptedStorage) {
throw LOGGER.logExceptionAsError(e);
}
persistenceSettingsBuilder.setLinuxUseUnprotectedFileAsCacheStorage(true);
} catch (Exception e) {
if (e instanceof KeyRingAccessException || !IdentityUtil.isKeyRingAccessible()) {
if (!allowUnencryptedStorage) {
throw LOGGER.logExceptionAsError(e instanceof KeyRingAccessException ? ((KeyRingAccessException) e) : new RuntimeException(e));
}
persistenceSettingsBuilder.setLinuxUseUnprotectedFileAsCacheStorage(true);
}
return persistenceSettingsBuilder.build();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,4 +205,28 @@ public static AuthenticationRecord loadVSCodeAuthRecord() throws IOException {
// Deserialize to AuthenticationRecord
return AuthenticationRecord.deserialize(json);
}

/**
* Checks if the GNOME Keyring is accessible.
* @return true if accessible, false otherwise.
*/
public static boolean isKeyRingAccessible() {
try {
ProcessBuilder processBuilder = new ProcessBuilder("secret-tool", "lookup", "test", "test");
processBuilder.redirectErrorStream(true);
Process process = processBuilder.start();

int exitCode = process.waitFor();
if (exitCode == 0) {
return true; // Keyring is accessible
} else {
LOGGER.verbose("GNOME Keyring is unavailable or inaccessible.");
return false;
}
} catch (IOException | InterruptedException e) {
Thread.currentThread().interrupt(); // Restore interrupted state if InterruptedException occurs
LOGGER.verbose("Error while checking GNOME Keyring availability: " + e.getMessage());
return false;
}
}
}
Loading