-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
38 changed files
with
2,539 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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 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
20 changes: 20 additions & 0 deletions
20
src/main/java/org/cryptomator/cryptofs/health/api/CheckFailed.java
This file contains 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,20 @@ | ||
package org.cryptomator.cryptofs.health.api; | ||
|
||
public class CheckFailed implements DiagnosticResult { | ||
|
||
private final String message; | ||
|
||
public CheckFailed(String message) { | ||
this.message = message; | ||
} | ||
|
||
@Override | ||
public Severity getSeverity() { | ||
return Severity.CRITICAL; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("Check failed: %s", message); | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/java/org/cryptomator/cryptofs/health/api/CommonDetailKeys.java
This file contains 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,11 @@ | ||
package org.cryptomator.cryptofs.health.api; | ||
|
||
public class CommonDetailKeys { | ||
|
||
public static final String ENCRYPTED_PATH = "Encrypted Path"; | ||
public static final String DIR_ID = "Directory ID"; | ||
public static final String DIR_ID_FILE = "Directory ID File"; | ||
|
||
private CommonDetailKeys() {}; | ||
|
||
} |
55 changes: 55 additions & 0 deletions
55
src/main/java/org/cryptomator/cryptofs/health/api/DiagnosticResult.java
This file contains 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,55 @@ | ||
package org.cryptomator.cryptofs.health.api; | ||
|
||
import org.cryptomator.cryptofs.VaultConfig; | ||
import org.cryptomator.cryptolib.api.Cryptor; | ||
import org.cryptomator.cryptolib.api.Masterkey; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Path; | ||
import java.util.Map; | ||
|
||
public interface DiagnosticResult { | ||
|
||
enum Severity { | ||
/** | ||
* No complains | ||
*/ | ||
GOOD, | ||
|
||
/** | ||
* Unexpected, but nothing to worry about. May be worth logging | ||
*/ | ||
INFO, | ||
|
||
/** | ||
* Compromises vault structure, can and should be fixed. | ||
*/ | ||
WARN, | ||
|
||
/** | ||
* Irreversible damage, no automated way of fixing this issue. | ||
*/ | ||
CRITICAL; | ||
} | ||
|
||
Severity getSeverity(); | ||
|
||
/** | ||
* @return A short, human-readable summary of the result. | ||
*/ | ||
@Override | ||
String toString(); | ||
|
||
default void fix(Path pathToVault, VaultConfig config, Masterkey masterkey, Cryptor cryptor) throws IOException { | ||
throw new UnsupportedOperationException("Fix for result" + this.getClass() + " not implemented"); | ||
} | ||
|
||
/** | ||
* Get more specific info about the result like names of affected resources. | ||
* | ||
* @return A map of strings containing result specific information | ||
*/ | ||
default Map<String, String> details() { | ||
return Map.of(); | ||
} | ||
} |
74 changes: 74 additions & 0 deletions
74
src/main/java/org/cryptomator/cryptofs/health/api/HealthCheck.java
This file contains 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,74 @@ | ||
package org.cryptomator.cryptofs.health.api; | ||
|
||
import org.cryptomator.cryptofs.VaultConfig; | ||
import org.cryptomator.cryptolib.api.Cryptor; | ||
import org.cryptomator.cryptolib.api.Masterkey; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.nio.file.Path; | ||
import java.util.Collection; | ||
import java.util.ServiceLoader; | ||
import java.util.concurrent.ExecutorService; | ||
import java.util.function.Consumer; | ||
import java.util.stream.Stream; | ||
import java.util.stream.StreamSupport; | ||
|
||
public interface HealthCheck { | ||
|
||
/** | ||
* @return All known health checks | ||
*/ | ||
static Collection<HealthCheck> allChecks() { | ||
return ServiceLoader.load(HealthCheck.class).stream().map(ServiceLoader.Provider::get).toList(); | ||
} | ||
|
||
/** | ||
* @return A human readable name for this check | ||
*/ | ||
default String name() { | ||
var canonicalName = getClass().getCanonicalName(); | ||
return canonicalName.substring(canonicalName.lastIndexOf('.')+1); | ||
} | ||
|
||
/** | ||
* Checks the vault at the given path. | ||
* | ||
* @param pathToVault Path to the vault's root directory | ||
* @param config The parsed and verified vault config | ||
* @param masterkey The masterkey | ||
* @param cryptor A cryptor initialized for this vault | ||
* @param resultCollector Callback called for each result. | ||
*/ | ||
void check(Path pathToVault, VaultConfig config, Masterkey masterkey, Cryptor cryptor, Consumer<DiagnosticResult> resultCollector); | ||
|
||
/** | ||
* Invokes the health check on a background thread scheduled using the given executor service. The results will be | ||
* streamed. If the stream gets {@link Stream#close() closed} before it terminates, an attempt is made to cancel | ||
* the health check. | ||
* <p> | ||
* The check blocks if the stream is not consumed | ||
* | ||
* @param pathToVault Path to the vault's root directory | ||
* @param config The parsed and verified vault config | ||
* @param masterkey The masterkey | ||
* @param cryptor A cryptor initialized for this vault | ||
* @param executor An executor service to run the health check | ||
* @return A lazily filled stream of diagnostic results. | ||
*/ | ||
default Stream<DiagnosticResult> check(Path pathToVault, VaultConfig config, Masterkey masterkey, Cryptor cryptor, ExecutorService executor) { | ||
var resultSpliterator = new TransferSpliterator<DiagnosticResult>(new PoisonResult()); | ||
|
||
var task = executor.submit(() -> { | ||
try { | ||
check(pathToVault, config, masterkey, cryptor, resultSpliterator); | ||
} catch (TransferSpliterator.TransferClosedException e) { | ||
LoggerFactory.getLogger(HealthCheck.class).debug("{} cancelled.", name()); | ||
} finally { | ||
resultSpliterator.close(); | ||
} | ||
}); | ||
|
||
return StreamSupport.stream(resultSpliterator, false).onClose(() -> task.cancel(true)); | ||
} | ||
|
||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/org/cryptomator/cryptofs/health/api/PoisonResult.java
This file contains 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,8 @@ | ||
package org.cryptomator.cryptofs.health.api; | ||
|
||
record PoisonResult() implements DiagnosticResult { | ||
@Override | ||
public Severity getSeverity() { | ||
return null; | ||
} | ||
} |
Oops, something went wrong.