Skip to content

Commit

Permalink
add option to set maxCleartextNameLimit
Browse files Browse the repository at this point in the history
  • Loading branch information
infeo committed Sep 25, 2024
1 parent 883359c commit 2c6a42a
Showing 1 changed file with 38 additions and 5 deletions.
43 changes: 38 additions & 5 deletions src/main/java/org/cryptomator/cli/CryptomatorCli.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.cryptomator.cryptofs.CryptoFileSystemProperties;
import org.cryptomator.cryptofs.CryptoFileSystemProvider;
import org.cryptomator.cryptofs.VaultConfig;
import org.cryptomator.cryptolib.api.Masterkey;
import org.cryptomator.cryptolib.common.MasterkeyFileAccess;
import org.cryptomator.integrations.mount.UnmountFailedException;
Expand All @@ -14,6 +15,8 @@
import java.io.IOException;
import java.net.URI;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.SecureRandom;
import java.util.concurrent.Callable;
Expand All @@ -26,7 +29,10 @@ public class CryptomatorCli implements Callable<Integer> {

private static final Logger LOG = LoggerFactory.getLogger(CryptomatorCli.class);
private static final byte[] PEPPER = new byte[0];
private static final String CONFIG_FILE_NAME = "vault.cryptomator";

@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

@Parameters(index = "0", paramLabel = "/path/to/vaultDirectory", description = "Path to the vault directory")
Path pathToVault;
Expand All @@ -37,17 +43,33 @@ public class CryptomatorCli implements Callable<Integer> {
@CommandLine.ArgGroup(exclusive = false, multiplicity = "1")
MountOptions mountOptions;

@CommandLine.Option(names = {"--maxCleartextNameLength"}, description = "Maximum cleartext filename length limit of created files. Remark: If this limit is greater than the shortening threshold, it does not have any effect.")
void setMaxCleartextNameLength(int input) {
if (input <= 0) {
throw new CommandLine.ParameterException(spec.commandLine(),
String.format("Invalid value '%d' for option '--maxCleartextNameLength': " +
"value must be a positive Number between 1 and %d.", input, Integer.MAX_VALUE));
}
maxCleartextNameLength = input;
}

private int maxCleartextNameLength = 0;

private SecureRandom csrpg = null;

@Override
public Integer call() throws Exception {
csrpg = SecureRandom.getInstanceStrong();
CryptoFileSystemProperties fsProps = CryptoFileSystemProperties.cryptoFileSystemProperties() //

var unverifiedConfig = readConfigFromStorage(pathToVault);
var fsPropsBuilder = CryptoFileSystemProperties.cryptoFileSystemProperties() //
.withKeyLoader(this::loadMasterkey) //
//TODO: shortening Threshold
//TODO: maxCleartextname
.build();
try (var fs = CryptoFileSystemProvider.newFileSystem(pathToVault, fsProps);
.withShorteningThreshold(unverifiedConfig.allegedShorteningThreshold()); //cryptofs checks, if config is signed with masterkey
if (maxCleartextNameLength > 0) {
fsPropsBuilder.withMaxCleartextNameLength(maxCleartextNameLength);
}

try (var fs = CryptoFileSystemProvider.newFileSystem(pathToVault, fsPropsBuilder.build());
var mount = mountOptions.mount(fs)) {
System.out.println(mount.getMountpoint().uri());
while (true) {
Expand All @@ -74,6 +96,17 @@ private Masterkey loadMasterkey(URI keyId) {
}
}

/**
* Attempts to read the vault config file and parse it without verifying its integrity.
*
* @throws IOException if reading the file fails
*/
static VaultConfig.UnverifiedVaultConfig readConfigFromStorage(Path vaultPath) throws IOException {
Path configPath = vaultPath.resolve(CONFIG_FILE_NAME);
String token = Files.readString(configPath, StandardCharsets.US_ASCII);
return VaultConfig.decode(token);
}

public static void main(String... args) {
int exitCode = new CommandLine(new CryptomatorCli())
.setPosixClusteredShortOptionsAllowed(false)
Expand Down

0 comments on commit 2c6a42a

Please sign in to comment.