Repository navigation
Evade DBusException - #1346
Evade DBusException#1346
Conversation
|
While I like this change (as it avoids unnecessary reflection), I wonder why a DBusException didn't get caught here: |
That's a very good question. Thanks for pointing that out. I changed the wrong piece of code. Looking at the code again shows that this is not an uncaught exception, but a printed stack trace. Changing the right piece of code leads to this: I think that's much better. |
overheadhunter
left a comment
There was a problem hiding this comment.
On a different note: Change private final Logger log to private static final Logger LOG 😉 While you're at it anyway.
|
No problem. |
| connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION); | ||
| } catch (DBusException e) { | ||
| e.printStackTrace(); | ||
| LOG.error(e.toString(), e.getCause()); |
There was a problem hiding this comment.
e.toString() most of the time is not going to give you everything that happened with that exception. If there are no security constraints on what's being thrown, just print it all over: LOG.error("Connection couldn’t be acquired because:", e); — and yes, no space after : and no need to parametrize that field either 😉
There was a problem hiding this comment.
Your logging statement produces:
06:46:52.599 [main] INFO org.cryptomator.launcher.Cryptomator - Starting Cryptomator SNAPSHOT on Mac OS X 10.15.6 (x86_64)
06:46:52.926 [JavaFX Application Thread] INFO o.c.ui.launcher.FxApplicationStarter - JavaFX Runtime started.
06:46:53.006 [JavaFX Application Thread] ERROR o.f.secret.simple.SimpleCollection - org.freedesktop.dbus.exceptions.DBusException: Cannot Resolve Session Bus Address: MachineId file can not be found
06:46:53.009 [JavaFX Application Thread] ERROR o.c.k.LinuxKDEWalletKeychainAccessImpl - Connection couldn’t be acquired because:
org.freedesktop.dbus.exceptions.DBusException: Cannot Resolve Session Bus Address: MachineId file can not be found
at org.freedesktop.dbus.connections.impl.DBusConnection.lambda$determineMachineIdFile$5(DBusConnection.java:320)
at java.base/java.util.Optional.orElseThrow(Optional.java:401)
at org.freedesktop.dbus.connections.impl.DBusConnection.determineMachineIdFile(DBusConnection.java:320)
at org.freedesktop.dbus.connections.impl.DBusConnection.getDbusMachineId(DBusConnection.java:303)
at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:149)
at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:169)
at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:228)
at org.freedesktop.dbus.connections.impl.DBusConnection.getConnection(DBusConnection.java:184)
at org.cryptomator.keychain.LinuxKDEWalletKeychainAccessImpl.<init>(LinuxKDEWalletKeychainAccessImpl.java:23)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500)
at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481)
at org.cryptomator.keychain.LinuxSystemKeychainAccess.constructKeychainAccess(LinuxSystemKeychainAccess.java:31)
at org.cryptomator.keychain.LinuxSystemKeychainAccess.<init>(LinuxSystemKeychainAccess.java:22)
at org.cryptomator.keychain.LinuxSystemKeychainAccess_Factory.get(LinuxSystemKeychainAccess_Factory.java:16)
at org.cryptomator.keychain.LinuxSystemKeychainAccess_Factory.get(LinuxSystemKeychainAccess_Factory.java:6)
at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
at dagger.internal.SetFactory.get(SetFactory.java:126)
at dagger.internal.SetFactory.get(SetFactory.java:37)
at org.cryptomator.keychain.KeychainModule_ProvideSupportedKeychainFactory.get(KeychainModule_ProvideSupportedKeychainFactory.java:25)
at org.cryptomator.keychain.KeychainModule_ProvideSupportedKeychainFactory.get(KeychainModule_ProvideSupportedKeychainFactory.java:10)
at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
at org.cryptomator.keychain.KeychainModule_ProvideKeychainManagerFactory.get(KeychainModule_ProvideKeychainManagerFactory.java:24)
at org.cryptomator.keychain.KeychainModule_ProvideKeychainManagerFactory.get(KeychainModule_ProvideKeychainManagerFactory.java:9)
at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
at org.cryptomator.ui.common.VaultService_Factory.get(VaultService_Factory.java:28)
at org.cryptomator.ui.common.VaultService_Factory.get(VaultService_Factory.java:10)
at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
at org.cryptomator.ui.fxapp.FxApplication_Factory.get(FxApplication_Factory.java:73)
at org.cryptomator.ui.fxapp.FxApplication_Factory.get(FxApplication_Factory.java:20)
at dagger.internal.DoubleCheck.get(DoubleCheck.java:47)
at org.cryptomator.launcher.DaggerCryptomatorComponent$FxApplicationComponentImpl.application(DaggerCryptomatorComponent.java:1055)
at org.cryptomator.ui.launcher.FxApplicationStarter.lambda$start$0(FxApplicationStarter.java:47)
at com.sun.javafx.application.PlatformImpl.lambda$startup$5(PlatformImpl.java:271)
at com.sun.glass.ui.Application.invokeAndWait(Application.java:464)
at com.sun.javafx.tk.quantum.QuantumToolkit.runToolkit(QuantumToolkit.java:366)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$startup$10(QuantumToolkit.java:280)
at com.sun.glass.ui.Application.lambda$run$1(Application.java:153)
06:46:59.004 [JavaFX Application Thread] INFO o.cryptomator.ui.fxapp.UpdateChecker - Current version: null, lastest version: 1.5.8
In this part of the code we basically get noticed, whether we are on a system with D-bus (Linux) or not (Windows, Mac). On an system without D-Bus, the full exception won't give us any more useful information.
So here, I'd stick with a log message that is one line long.
There was a problem hiding this comment.
In this part of the code we basically get noticed, whether we are on a system with D-bus (Linux) or not (Windows, Mac). On an system without D-Bus, the full exception won't give us any more useful information.
Not entirely true. If the running OS is windows, this method does not throw a DBusException but a RuntimeException and the failure is not logged at all (good!). This would also desireable on MacOS.
On the other hand, if we are on a system with linux kernel, the stacktrace might be of interest. Also, if the connection to DBus fails, we still create the object since the exception in the constructor is not rethrown. This would lead to several other exceptions where calls on a null reference are made.
My suggestion is to change this to something similar as in LinuxSecretServiceKeychainAccessImpl with default, empty constructor and the isSupported() function acutally init the object. This is not the best solution (i would prefer making the construtor private and create a factory method to prevent reflection), but for the PR sufficient.
There was a problem hiding this comment.
[...]
RuntimeExceptionand the failure is not logged at all (good!) [...]
Whether this is "good" is debatable (see #1276).
There was a problem hiding this comment.
On the other hand, if we are on a system with linux kernel, the stacktrace might be of interest. Also, if the connection to DBus fails, we still create the object since the exception in the constructor is not rethrown. This would lead to several other exceptions where calls on a null reference are made.
I don't think so. On a Linux system no one ever configures or installs D-Bus. It's just there as a very basic part of the Linux system that get's configured by the Linux package maintainer. Most people even don't know what D-Bus is. So why having more information on a D-Bus fail? This just tells us that D-Bus not there and so we aren't on Linux.
My suggestion is to change this to something similar as in LinuxSecretServiceKeychainAccessImpl with default, empty constructor and the isSupported() function acutally init the object. This is not the best solution (i would prefer making the construtor private and create a factory method to prevent reflection), but for the PR sufficient.
The kdewallet lib does not provide a default, empty constructor, because you cannot suppose that kdewallet is the only D-Bus functionality you'll use in your app and cannot create a new D-Bus connection for every D-Bus functionality that is required. A DBusConnection represents a connection to an application.
There was a problem hiding this comment.
On a Linux system no one ever configures or installs D-Bus.It's just there
Putting the stacktrace aside: I'm just wondering if there are cases, where a connection to the D-Bus system fails initially (for whatever reason). If this is possible (looking at the code it seems possible), then imho this object should not be constructed but throwing an exception in the constructor rather catching it and pretending to be something one can work with.
Of course, throwing just the exception could be dangerous, but it will be catched in the calling factory method LinuxSystemKeyChainAccess.constructKeychainAccess()
There was a problem hiding this comment.
Putting the stacktrace aside: I'm just wondering if there are cases, where a connection to the D-Bus system fails initially (for whatever reason). If this is possible (looking at the code it seems possible), then imho this object should not be constructed but throwing an exception in the constructor rather catching it and pretending to be something one can work with.
Of course, throwing just the exception could be dangerous, but it will be catched in the calling factory method
LinuxSystemKeyChainAccess.constructKeychainAccess()
Ok, since we are talking about software, a connection to the D-Bus system could certainly fail initially. I agree that an exception should be thrown in the catch clause after a DBus exception happened. Just as a side note: the code wasn't written to pretend to have something one can work with although an exception occurred. When you look at KeychainModule and KeychainManager you'll see, that all keychain implementations are only made available to Cryptomator if isSupported() returns true.
infeo
left a comment
There was a problem hiding this comment.
An instance of this class should not be created, if we cannot establish a DBus connection, but in the current implementation we allow this.
| connection = DBusConnection.getConnection(DBusConnection.DBusBusType.SESSION); | ||
| } catch (DBusException e) { | ||
| e.printStackTrace(); | ||
| LOG.error(e.toString(), e.getCause()); |
There was a problem hiding this comment.
In this part of the code we basically get noticed, whether we are on a system with D-bus (Linux) or not (Windows, Mac). On an system without D-Bus, the full exception won't give us any more useful information.
Not entirely true. If the running OS is windows, this method does not throw a DBusException but a RuntimeException and the failure is not logged at all (good!). This would also desireable on MacOS.
On the other hand, if we are on a system with linux kernel, the stacktrace might be of interest. Also, if the connection to DBus fails, we still create the object since the exception in the constructor is not rethrown. This would lead to several other exceptions where calls on a null reference are made.
My suggestion is to change this to something similar as in LinuxSecretServiceKeychainAccessImpl with default, empty constructor and the isSupported() function acutally init the object. This is not the best solution (i would prefer making the construtor private and create a factory method to prevent reflection), but for the PR sufficient.
| } | ||
| } catch (Exception e) { | ||
| log.error(e.toString(), e.getCause()); | ||
| LOG.error(e.toString(), e.getCause()); |
There was a problem hiding this comment.
See my comment above. If the object is set up correctly, also here the stacktrace can be interesting.
Reconsider, if this should be changed to LOG.error("Some msg:", e)
|
I also added some more log statements, as this is strongly encouraged by devs here and hope this fits your needs. |
infeo
left a comment
There was a problem hiding this comment.
As long as the messages are only debug level, everything is good 😉
|
Sorry, big fingers on my smartphone GitHub app |
Fixes #1345