-
Notifications
You must be signed in to change notification settings - Fork 5
New secret service #125
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
Merged
Merged
New secret service #125
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
a8a57c2
Replace GnomeKeyringKeychainAccess, KDEWalletKeychainAccess by Secret…
purejava 13e3132
Encrypted session is needed for tests to work
purejava 5163d8b
Ensure unlocking
purejava 9f227d0
Set default alias when missing
purejava c22f813
Migrate KDE wallet entries
purejava 161ea15
Service 1.0.0 was released 🎉
purejava b9c6eb1
Get back GnomeKeyringKeychainAccess, KDEWalletKeychainAccess
purejava 1c54f97
Remove added line by IDE
purejava 17a4f69
Keep migrated items
purejava ca6b4d8
Merge branch 'develop' into new-secret-service
purejava 857c812
Merge branch 'develop' into new-secret-service
purejava 3067f3b
Merge branch 'develop' into new-secret-service
purejava 163ad9a
Service 1.1.0 was released 🎉
purejava d17d556
Check wether DBus is available on the system
purejava b8bb5bb
Deprecate KDE Wallet and GNOME keyring
purejava e8b8bd5
Migration is done with GeneralPreferencesController#migrateKeychainEn…
purejava e3bae1d
Fix logic
purejava 1f5b1b4
Annotate the actual classes, not the test classes
purejava 4c7b490
Have Cryptomator as the label for a keychain entry
purejava 85d3de9
remove unused imports
infeo 8222287
replace withAttributes method
infeo e0b9a57
Do not throw exception on delete if item does not exist
infeo aaaa6fc
Merge branch 'develop' into new-secret-service
infeo 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
Some comments aren't visible on the classic Files Changed page.
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
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
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
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
169 changes: 169 additions & 0 deletions
169
src/main/java/org/cryptomator/linux/keychain/SecretServiceKeychainAccess.java
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 |
|---|---|---|
| @@ -0,0 +1,169 @@ | ||
| package org.cryptomator.linux.keychain; | ||
|
|
||
| import org.cryptomator.integrations.common.DisplayName; | ||
| import org.cryptomator.integrations.common.OperatingSystem; | ||
| import org.cryptomator.integrations.common.Priority; | ||
| import org.cryptomator.integrations.keychain.KeychainAccessException; | ||
| import org.cryptomator.integrations.keychain.KeychainAccessProvider; | ||
| import org.freedesktop.dbus.DBusPath; | ||
| import org.purejava.secret.api.Collection; | ||
| import org.purejava.secret.api.EncryptedSession; | ||
| import org.purejava.secret.api.Item; | ||
| import org.purejava.secret.api.Static; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Objects; | ||
|
|
||
| @Priority(1100) | ||
| @OperatingSystem(OperatingSystem.Value.LINUX) | ||
| @DisplayName("Secret Service") | ||
| public class SecretServiceKeychainAccess implements KeychainAccessProvider { | ||
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(SecretServiceKeychainAccess.class); | ||
| private static final String LABEL_FOR_SECRET_IN_KEYRING = "Cryptomator"; | ||
| private static final String ID_KEY = "Vault"; | ||
| private static final String NAME_KEY = "Name"; | ||
| private final EncryptedSession session = new EncryptedSession(); | ||
| private final Collection collection = new Collection(new DBusPath(Static.DBusPath.DEFAULT_COLLECTION)); | ||
|
|
||
| public SecretServiceKeychainAccess() { | ||
| session.getService().addCollectionChangedHandler(collection -> LOG.debug("Collection {} changed", collection.getPath())); | ||
| session.getService().addCollectionCreatedHandler(collection -> LOG.debug("Collection {} created", collection.getPath())); | ||
| session.getService().addCollectionDeletedHandler(collection -> LOG.debug("Collection {} deleted", collection.getPath())); | ||
| var getAlias = session.getService().readAlias("default"); | ||
| if (getAlias.isSuccess() && "/".equals(getAlias.value().getPath())) { | ||
| // default alias is not set; set it to the login keyring | ||
| session.getService().setAlias("default", new DBusPath(Static.DBusPath.LOGIN_COLLECTION)); | ||
| } | ||
| collection.addItemChangedHandler(item -> LOG.debug("Item {} changed", item.getPath())); | ||
| collection.addItemCreatedHandler(item -> LOG.debug("Item {} created", item.getPath())); | ||
| collection.addItemDeletedHandler(item -> LOG.debug("Item {} deleted", item.getPath())); | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public void storePassphrase(String key, String displayName, CharSequence passphrase) throws KeychainAccessException { | ||
| try { | ||
| var call = collection.searchItems(withKey(key)); | ||
| if (call.isSuccess()) { | ||
| if (call.value().isEmpty()) { | ||
| List<DBusPath> lockable = new ArrayList<>(); | ||
| lockable.add(new DBusPath(collection.getDBusPath())); | ||
| session.getService().unlock(lockable); | ||
| var itemProps = Item.createProperties(LABEL_FOR_SECRET_IN_KEYRING, withKeyAndName(key, displayName)); | ||
| var secret = session.encrypt(passphrase); | ||
| var created = collection.createItem(itemProps, secret, false); | ||
| if (!created.isSuccess()) { | ||
| throw new KeychainAccessException("Storing password failed", created.error()); | ||
| } | ||
| } else { | ||
| changePassphrase(key, displayName, passphrase); | ||
| } | ||
| } else { | ||
| throw new KeychainAccessException("Storing password failed", call.error()); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new KeychainAccessException("Storing password failed.", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public char[] loadPassphrase(String key) throws KeychainAccessException { | ||
| try { | ||
| var call = collection.searchItems(withKey(key)); | ||
| if (call.isSuccess()) { | ||
| if (!call.value().isEmpty()) { | ||
| var path = call.value().getFirst(); | ||
| session.getService().ensureUnlocked(path); | ||
| var secret = new Item(path).getSecret(session.getSession()); | ||
| return session.decrypt(secret); | ||
| } else { | ||
| return null; | ||
| } | ||
| } else { | ||
| throw new KeychainAccessException("Loading password failed", call.error()); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new KeychainAccessException("Loading password failed.", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void deletePassphrase(String key) throws KeychainAccessException { | ||
| try { | ||
| var call = collection.searchItems(withKey(key)); | ||
| if (call.isSuccess()) { | ||
| if (!call.value().isEmpty()) { | ||
| var path = call.value().getFirst(); | ||
| session.getService().ensureUnlocked(path); | ||
| var item = new Item(path); | ||
| var deleted = item.delete(); | ||
| if (!deleted.isSuccess()) { | ||
| throw new KeychainAccessException("Deleting password failed", deleted.error()); | ||
| } | ||
| } else { | ||
| LOG.debug("Deleting entry with {}={} failed: No such item found", ID_KEY, key); | ||
| } | ||
| } else { | ||
| throw new KeychainAccessException("Deleting password failed", call.error()); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new KeychainAccessException("Deleting password failed", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public void changePassphrase(String key, String displayName, CharSequence passphrase) throws KeychainAccessException { | ||
| try { | ||
| var call = collection.searchItems(withKey(key)); | ||
| if (call.isSuccess()) { | ||
| if (!call.value().isEmpty()) { | ||
| session.getService().ensureUnlocked(call.value().getFirst()); | ||
| var secret = session.encrypt(passphrase); | ||
| var itemProps = Item.createProperties(LABEL_FOR_SECRET_IN_KEYRING, withKeyAndName(key, displayName)); | ||
| var updated = collection.createItem(itemProps, secret, true); | ||
| if (!updated.isSuccess()) { | ||
| throw new KeychainAccessException("Updating password failed", updated.error()); | ||
| } | ||
| } else { | ||
| var msg = "Vault " + key + " not found, updating failed"; | ||
| throw new KeychainAccessException(msg); | ||
| } | ||
| } else { | ||
| throw new KeychainAccessException("Updating password failed", call.error()); | ||
| } | ||
| } catch (Exception e) { | ||
| throw new KeychainAccessException("Updating password failed", e); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSupported() { | ||
| return session.setupEncryptedSession() && | ||
| session.getService().hasDefaultCollection(); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isLocked() { | ||
| var call = collection.isLocked(); | ||
| return !call.isSuccess() || call.value(); | ||
| } | ||
|
|
||
| private Map<String, String> withKey(String key) { | ||
| if (key == null) { | ||
| throw new IllegalArgumentException("Arguments must not be null"); | ||
| } | ||
| return Map.of(ID_KEY, key); | ||
| } | ||
|
|
||
| private Map<String, String> withKeyAndName(String key, String name) { | ||
| if (key == null) { | ||
| throw new IllegalArgumentException("Arguments must not be null"); | ||
| } | ||
| return Map.of(ID_KEY, key, NAME_KEY, Objects.requireNonNullElse(name, "")); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
.../resources/META-INF/services/org.cryptomator.integrations.keychain.KeychainAccessProvider
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 |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| org.cryptomator.linux.keychain.SecretServiceKeychainAccess | ||
| org.cryptomator.linux.keychain.KDEWalletKeychainAccess | ||
| org.cryptomator.linux.keychain.GnomeKeyringKeychainAccess |
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 |
|---|---|---|
|
|
@@ -87,4 +87,4 @@ public static boolean gnomeKeyringAvailableAndUnlocked() { | |
| } | ||
| } | ||
|
|
||
| } | ||
| } | ||
91 changes: 91 additions & 0 deletions
91
src/test/java/org/cryptomator/linux/keychain/SecretServiceKeychainAccessTest.java
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 |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| package org.cryptomator.linux.keychain; | ||
|
|
||
| import org.cryptomator.integrations.keychain.KeychainAccessException; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeAll; | ||
| import org.junit.jupiter.api.MethodOrderer; | ||
| import org.junit.jupiter.api.Nested; | ||
| import org.junit.jupiter.api.Order; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.TestMethodOrder; | ||
| import org.junit.jupiter.api.condition.EnabledIf; | ||
| import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; | ||
|
|
||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * Unit tests for Secret Service access via Dbus. | ||
| */ | ||
| @EnabledIfEnvironmentVariable(named = "DBUS_SESSION_BUS_ADDRESS", matches = ".*") | ||
| public class SecretServiceKeychainAccessTest { | ||
|
|
||
| private static boolean isInstalled; | ||
|
|
||
| @BeforeAll | ||
| public static void checkSystemAndSetup() throws IOException { | ||
| ProcessBuilder dbusSend = new ProcessBuilder("dbus-send", "--print-reply", "--dest=org.freedesktop.DBus", "/org/freedesktop/DBus", "org.freedesktop.DBus.ListNames"); | ||
| ProcessBuilder grep = new ProcessBuilder("grep", "-q", "org.freedesktop.secrets"); | ||
| try { | ||
| Process end = ProcessBuilder.startPipeline(List.of(dbusSend, grep)).get(1); | ||
| if (end.waitFor(1000, TimeUnit.MILLISECONDS)) { | ||
| isInstalled = end.exitValue() == 0; | ||
| } else { | ||
| isInstalled = false; | ||
| } | ||
infeo marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch (InterruptedException e) { | ||
| Thread.currentThread().interrupt(); | ||
| } | ||
| } | ||
|
|
||
| @Test | ||
| public void testIsSupported() { | ||
| var service = new SecretServiceKeychainAccess(); | ||
| Assertions.assertEquals(isInstalled, service.isSupported()); | ||
| } | ||
|
|
||
| @Nested | ||
| @TestMethodOrder(MethodOrderer.OrderAnnotation.class) | ||
| @EnabledIf("serviceAvailableAndUnlocked") | ||
| class FunctionalTests { | ||
|
|
||
| static final String KEY_ID = "cryptomator-test-" + UUID.randomUUID(); | ||
| final static SecretServiceKeychainAccess KEYRING = new SecretServiceKeychainAccess(); | ||
|
|
||
| @Test | ||
| @Order(1) | ||
| public void testStore() throws KeychainAccessException { | ||
| KEYRING.isSupported(); // ensure encrypted session | ||
| KEYRING.storePassphrase(KEY_ID, "cryptomator-test", "p0ssw0rd"); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(2) | ||
| public void testLoad() throws KeychainAccessException { | ||
| var passphrase = KEYRING.loadPassphrase(KEY_ID); | ||
| Assertions.assertNotNull(passphrase); | ||
| Assertions.assertEquals("p0ssw0rd", String.copyValueOf(passphrase)); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(3) | ||
| public void testDelete() throws KeychainAccessException { | ||
| KEYRING.deletePassphrase(KEY_ID); | ||
| } | ||
|
|
||
| @Test | ||
| @Order(4) | ||
| public void testLoadNotExisting() throws KeychainAccessException { | ||
| var result = KEYRING.loadPassphrase(KEY_ID); | ||
| Assertions.assertNull(result); | ||
| } | ||
|
|
||
| public static boolean serviceAvailableAndUnlocked() { | ||
| var service = new SecretServiceKeychainAccess(); | ||
| return service.isSupported() && !service.isLocked(); | ||
| } | ||
| } | ||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.