Skip to content

Commit df5120c

Browse files
committed
Add vault creation command with default values
1 parent 3a1c457 commit df5120c

3 files changed

Lines changed: 94 additions & 1 deletion

File tree

src/main/java/module-info.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
requires org.fusesource.jansi;
1111
requires ch.qos.logback.core;
1212
requires ch.qos.logback.classic;
13+
requires org.cryptomator.cryptolib;
1314

1415
provides Configurator with LogbackConfigurator;
1516
}
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package org.cryptomator.cli;
2+
3+
import org.cryptomator.cryptofs.CryptoFileSystemProperties;
4+
import org.cryptomator.cryptofs.CryptoFileSystemProvider;
5+
import org.cryptomator.cryptolib.api.CryptoException;
6+
import org.cryptomator.cryptolib.api.CryptorProvider;
7+
import org.cryptomator.cryptolib.api.Masterkey;
8+
import org.cryptomator.cryptolib.common.MasterkeyFileAccess;
9+
import org.slf4j.Logger;
10+
import org.slf4j.LoggerFactory;
11+
12+
import picocli.CommandLine.ArgGroup;
13+
import picocli.CommandLine.Command;
14+
import picocli.CommandLine.Mixin;
15+
import picocli.CommandLine.Model;
16+
import picocli.CommandLine.Parameters;
17+
import picocli.CommandLine.Spec;
18+
19+
import java.io.IOException;
20+
import java.net.URI;
21+
import java.nio.CharBuffer;
22+
import java.nio.file.Files;
23+
import java.nio.file.Path;
24+
import java.security.SecureRandom;
25+
import java.util.concurrent.Callable;
26+
27+
@Command(
28+
name = "create",
29+
header = "Creates a vault",
30+
description = "Creates a new cryptomator vault at the specified path.",
31+
parameterListHeading = "%nParameters:%n",
32+
headerHeading = "Usage:%n%n",
33+
synopsisHeading = "%n",
34+
descriptionHeading = "%nDescription:%n%n",
35+
optionListHeading = "%nOptions:%n",
36+
mixinStandardHelpOptions = true)
37+
public class Create implements Callable<Integer> {
38+
39+
private static final Logger LOG = LoggerFactory.getLogger(Create.class);
40+
private static final byte[] PEPPER = new byte[0];
41+
private static final String MASTERKEY_FILE_NAME = "masterkey.cryptomator";
42+
private static final URI DEFAULT_KEY_ID = URI.create("masterkeyfile:" + MASTERKEY_FILE_NAME);
43+
44+
@Spec Model.CommandSpec spec;
45+
@Mixin LoggingMixin loggingMixin;
46+
47+
@Parameters(
48+
index = "0",
49+
paramLabel = "/path/to/vaultDirectory",
50+
description = "Path to the vault directory")
51+
Path pathToVault;
52+
53+
@ArgGroup(multiplicity = "1")
54+
PasswordSource passwordSource;
55+
56+
private SecureRandom csprng = null;
57+
58+
@Override
59+
public Integer call() throws Exception {
60+
csprng = SecureRandom.getInstanceStrong();
61+
62+
createVault(pathToVault);
63+
64+
LOG.info("Vault created successfully in {}", pathToVault);
65+
return 0;
66+
}
67+
68+
private void createVault(Path path) throws IOException {
69+
// Throw exception if there's something already there.
70+
Files.createDirectory(path);
71+
72+
try (var passphraseContainer = passwordSource.readPassphrase();
73+
var masterkey = Masterkey.generate(csprng)) {
74+
75+
Path masterkeyFilePath = path.resolve(MASTERKEY_FILE_NAME);
76+
MasterkeyFileAccess masterkeyFileAccess = new MasterkeyFileAccess(PEPPER, csprng);
77+
masterkeyFileAccess.persist(
78+
masterkey, masterkeyFilePath, CharBuffer.wrap(passphraseContainer.content()));
79+
80+
try {
81+
CryptoFileSystemProperties fsProps =
82+
CryptoFileSystemProperties.cryptoFileSystemProperties()
83+
.withCipherCombo(CryptorProvider.Scheme.SIV_GCM)
84+
.withKeyLoader(ignored -> masterkey.copy())
85+
.build();
86+
CryptoFileSystemProvider.initialize(path, fsProps, DEFAULT_KEY_ID);
87+
} catch (CryptoException e) {
88+
throw new IOException("Vault initialization failed", e);
89+
}
90+
}
91+
}
92+
}

src/main/java/org/cryptomator/cli/CryptomatorCli.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
mixinStandardHelpOptions = true,
1313
version = "${org.cryptomator.cli.version}",
1414
description = "Unlocks a cryptomator vault and mounts it into the system.",
15-
subcommands = { Unlock.class, ListMounters.class, CommandLine.HelpCommand.class})
15+
subcommands = {Create.class, Unlock.class, ListMounters.class, CommandLine.HelpCommand.class})
1616
public class CryptomatorCli {
1717

1818
private static final Logger LOG = LoggerFactory.getLogger(CryptomatorCli.class);

0 commit comments

Comments
 (0)