Parcourir la source

Improve error handling:
* Vault only returns the confiCache
* cache has safe and unsafe method to get config

Armin Schrenk il y a 3 ans
Parent
commit
76811f92e6

+ 8 - 14
src/main/java/org/cryptomator/common/vaults/Vault.java

@@ -17,7 +17,6 @@ import org.cryptomator.cryptofs.CryptoFileSystem;
 import org.cryptomator.cryptofs.CryptoFileSystemProperties;
 import org.cryptomator.cryptofs.CryptoFileSystemProperties.FileSystemFlags;
 import org.cryptomator.cryptofs.CryptoFileSystemProvider;
-import org.cryptomator.cryptofs.VaultConfig.UnverifiedVaultConfig;
 import org.cryptomator.cryptofs.common.FileSystemCapabilityChecker;
 import org.cryptomator.cryptolib.api.CryptoException;
 import org.cryptomator.cryptolib.api.MasterkeyLoader;
@@ -58,7 +57,7 @@ public class Vault {
 	private final AtomicReference<CryptoFileSystem> cryptoFileSystem;
 	private final VaultState state;
 	private final ObjectProperty<Exception> lastKnownException;
-	private final VaultConfigCache configWrapper;
+	private final VaultConfigCache configCache;
 	private final VaultStats stats;
 	private final StringBinding displayName;
 	private final StringBinding displayablePath;
@@ -75,9 +74,9 @@ public class Vault {
 	private volatile Volume volume;
 
 	@Inject
-	Vault(VaultSettings vaultSettings, VaultConfigCache configWrapper, Provider<Volume> volumeProvider, @DefaultMountFlags StringBinding defaultMountFlags, AtomicReference<CryptoFileSystem> cryptoFileSystem, VaultState state, @Named("lastKnownException") ObjectProperty<Exception> lastKnownException, VaultStats stats) {
+	Vault(VaultSettings vaultSettings, VaultConfigCache configCache, Provider<Volume> volumeProvider, @DefaultMountFlags StringBinding defaultMountFlags, AtomicReference<CryptoFileSystem> cryptoFileSystem, VaultState state, @Named("lastKnownException") ObjectProperty<Exception> lastKnownException, VaultStats stats) {
 		this.vaultSettings = vaultSettings;
-		this.configWrapper = configWrapper;
+		this.configCache = configCache;
 		this.volumeProvider = volumeProvider;
 		this.defaultMountFlags = defaultMountFlags;
 		this.cryptoFileSystem = cryptoFileSystem;
@@ -105,10 +104,10 @@ public class Vault {
 		Set<FileSystemFlags> flags = EnumSet.noneOf(FileSystemFlags.class);
 		if (vaultSettings.usesReadOnlyMode().get()) {
 			flags.add(FileSystemFlags.READONLY);
-		} else if(vaultSettings.maxCleartextFilenameLength().get() == -1) {
+		} else if (vaultSettings.maxCleartextFilenameLength().get() == -1) {
 			LOG.debug("Determining cleartext filename length limitations...");
 			var checker = new FileSystemCapabilityChecker();
-			int shorteningThreshold = getUnverifiedVaultConfig().allegedShorteningThreshold();
+			int shorteningThreshold = configCache.get().allegedShorteningThreshold();
 			int ciphertextLimit = checker.determineSupportedCiphertextFileNameLength(getPath());
 			if (ciphertextLimit < shorteningThreshold) {
 				int cleartextLimit = checker.determineSupportedCleartextFileNameLength(getPath());
@@ -194,7 +193,7 @@ public class Vault {
 	}
 
 	public void reloadConfig() throws IOException {
-		configWrapper.reloadConfig();
+		configCache.reloadConfig();
 	}
 
 	// ******************************************************************************
@@ -364,13 +363,8 @@ public class Vault {
 		}
 	}
 
-	public UnverifiedVaultConfig getUnverifiedVaultConfig() {
-		try {
-			return configWrapper.getConfig();
-		} catch (IOException e) {
-			throw new IllegalStateException("Vault Config not present.");
-		}
-
+	public VaultConfigCache getVaultConfigCache() {
+		return configCache;
 	}
 
 	public void setCustomMountFlags(String mountFlags) {

+ 10 - 1
src/main/java/org/cryptomator/common/vaults/VaultConfigCache.java

@@ -8,6 +8,7 @@ import org.cryptomator.cryptofs.VaultConfigLoadException;
 import javafx.beans.property.ObjectProperty;
 import javafx.beans.property.SimpleObjectProperty;
 import java.io.IOException;
+import java.io.UncheckedIOException;
 import java.nio.charset.StandardCharsets;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -35,13 +36,21 @@ public class VaultConfigCache {
 		}
 	}
 
-	VaultConfig.UnverifiedVaultConfig getConfig() throws IOException {
+	public VaultConfig.UnverifiedVaultConfig get() throws IOException {
 		if (Objects.isNull(config.get())) {
 			reloadConfig();
 		}
 		return config.get();
 	}
 
+	public VaultConfig.UnverifiedVaultConfig getUnchecked() {
+		try {
+			return get();
+		} catch (IOException e) {
+			throw new UncheckedIOException(e);
+		}
+	}
+
 
 	/**
 	 * Attempts to read the vault config file and parse it without verifying its integrity.

+ 4 - 4
src/main/java/org/cryptomator/ui/health/StartController.java

@@ -2,6 +2,7 @@ package org.cryptomator.ui.health;
 
 import dagger.Lazy;
 import org.cryptomator.common.vaults.Vault;
+import org.cryptomator.common.vaults.VaultConfigCache;
 import org.cryptomator.cryptofs.VaultConfig;
 import org.cryptomator.cryptofs.VaultConfigLoadException;
 import org.cryptomator.cryptofs.VaultKeyInvalidException;
@@ -21,7 +22,6 @@ import javafx.application.Platform;
 import javafx.fxml.FXML;
 import javafx.scene.Scene;
 import javafx.stage.Stage;
-import java.io.IOException;
 import java.util.concurrent.CompletableFuture;
 import java.util.concurrent.CompletionException;
 import java.util.concurrent.ExecutorService;
@@ -34,7 +34,7 @@ public class StartController implements FxController {
 
 	private final Stage window;
 	private final Stage unlockWindow;
-	private final Vault vault;
+	private final VaultConfigCache vaultConfig;
 	private final KeyLoadingStrategy keyLoadingStrategy;
 	private final ExecutorService executor;
 	private final AtomicReference<Masterkey> masterkeyRef;
@@ -46,7 +46,7 @@ public class StartController implements FxController {
 	public StartController(@HealthCheckWindow Stage window, @HealthCheckWindow Vault vault, @HealthCheckWindow KeyLoadingStrategy keyLoadingStrategy, ExecutorService executor, AtomicReference<Masterkey> masterkeyRef, AtomicReference<VaultConfig> vaultConfigRef, @FxmlScene(FxmlFile.HEALTH_CHECK_LIST) Lazy<Scene> checkScene, Lazy<ErrorComponent.Builder> errorComponent, @Named("unlockWindow") Stage unlockWindow) {
 		this.window = window;
 		this.unlockWindow = unlockWindow;
-		this.vault = vault;
+		this.vaultConfig = vault.getVaultConfigCache();
 		this.keyLoadingStrategy = keyLoadingStrategy;
 		this.executor = executor;
 		this.masterkeyRef = masterkeyRef;
@@ -77,7 +77,7 @@ public class StartController implements FxController {
 	}
 
 	private void verifyVaultConfig(KeyLoadingStrategy keyLoadingStrategy) throws VaultConfigLoadException {
-		var unverifiedCfg = vault.getUnverifiedVaultConfig();
+		var unverifiedCfg = vaultConfig.getUnchecked();
 		try (var masterkey = keyLoadingStrategy.loadKey(unverifiedCfg.getKeyId())) {
 			var verifiedCfg = unverifiedCfg.verify(masterkey.getEncoded(), unverifiedCfg.allegedVaultVersion());
 			vaultConfigRef.set(verifiedCfg);

+ 7 - 3
src/main/java/org/cryptomator/ui/keyloading/KeyLoadingModule.java

@@ -27,9 +27,13 @@ abstract class KeyLoadingModule {
 	@KeyLoading
 	@KeyLoadingScoped
 	static KeyLoadingStrategy provideKeyLoaderProvider(@KeyLoading Vault vault, Map<String, Provider<KeyLoadingStrategy>> strategies) {
-		String scheme = vault.getUnverifiedVaultConfig().getKeyId().getScheme();
-		var fallback = KeyLoadingStrategy.failed(new IllegalArgumentException("Unsupported key id " + scheme));
-		return strategies.getOrDefault(scheme, () -> fallback).get();
+		try {
+			String scheme = vault.getVaultConfigCache().get().getKeyId().getScheme();
+			var fallback = KeyLoadingStrategy.failed(new IllegalArgumentException("Unsupported key id " + scheme));
+			return strategies.getOrDefault(scheme, () -> fallback).get();
+		} catch (IOException e) {
+			return KeyLoadingStrategy.failed(e);
+		}
 	}
 
 }