Skip to content

Commit 409c69a

Browse files
committed
Adding passwordfile option to securely read password for each vault from a corresponding password file. Works the same way as the password option does. Just uses a file instead to read the password from the first line. password trumps passwordfile.
1 parent e186231 commit 409c69a

2 files changed

Lines changed: 40 additions & 2 deletions

File tree

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

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,14 @@
88
*******************************************************************************/
99
package org.cryptomator.cli;
1010

11+
import java.io.IOException;
12+
import java.nio.file.Files;
13+
import java.nio.file.Path;
14+
import java.nio.file.Paths;
1115
import java.util.Properties;
1216
import java.util.Set;
1317
import java.util.stream.Collectors;
18+
import java.util.stream.Stream;
1419

1520
import org.apache.commons.cli.CommandLine;
1621
import org.apache.commons.cli.DefaultParser;
@@ -27,7 +32,8 @@ public class Args {
2732
private static final String USAGE = "java -jar cryptomator-cli.jar" //
2833
+ " --bind localhost --port 8080" //
2934
+ " --vault mySecretVault=/path/to/vault --password mySecretVault=FooBar3000" //
30-
+ " --vault myOtherVault=/path/to/other/vault --password myOtherVault=BarFoo4000";
35+
+ " --vault myOtherVault=/path/to/other/vault --password myOtherVault=BarFoo4000"
36+
+ " --vault myThirdVault=/path/to/third/vault --passwordfile myThirdVault=/path/to/passwordfile";
3137
private static final Options OPTIONS = new Options();
3238
static {
3339
OPTIONS.addOption(Option.builder() //
@@ -56,18 +62,27 @@ public class Args {
5662
.valueSeparator() //
5763
.hasArgs() //
5864
.build());
65+
OPTIONS.addOption(Option.builder() //
66+
.longOpt("passwordfile") //
67+
.argName("Passwordfile for a vault") //
68+
.desc("Format must be vaultName=passwordfile") //
69+
.valueSeparator() //
70+
.hasArgs() //
71+
.build());
5972
}
6073

6174
private final String bindAddr;
6275
private final int port;
6376
private final Properties vaultPaths;
6477
private final Properties vaultPasswords;
78+
private final Properties vaultPasswordfiles;
6579

6680
public Args(CommandLine commandLine) throws ParseException {
6781
this.bindAddr = commandLine.getOptionValue("bind", "localhost");
6882
this.port = Integer.parseInt(commandLine.getOptionValue("port", "0"));
6983
this.vaultPaths = commandLine.getOptionProperties("vault");
7084
this.vaultPasswords = commandLine.getOptionProperties("password");
85+
this.vaultPasswordfiles = commandLine.getOptionProperties("passwordfile");
7186
}
7287

7388
public String getBindAddr() {
@@ -79,14 +94,33 @@ public int getPort() {
7994
}
8095

8196
public Set<String> getVaultNames() {
82-
return vaultPaths.keySet().stream().filter(vaultPasswords::containsKey).map(String.class::cast).collect(Collectors.toSet());
97+
Set<String> filteredVaults = vaultPaths.keySet().stream().filter(vaultPasswords::containsKey).map(String.class::cast).collect(Collectors.toSet());
98+
filteredVaults.addAll(vaultPaths.keySet().stream().filter(vaultPasswordfiles::containsKey).map(String.class::cast).collect(Collectors.toSet()));
99+
return filteredVaults;
83100
}
84101

85102
public String getVaultPath(String vaultName) {
86103
return vaultPaths.getProperty(vaultName);
87104
}
88105

106+
public String getVaultPasswordPath(String vaultName) { return vaultPasswordfiles.getProperty(vaultName); }
107+
89108
public String getVaultPassword(String vaultName) {
109+
if (vaultPasswords.getProperty(vaultName) == null){
110+
Path vaultPasswordPath = Paths.get(vaultPasswordfiles.getProperty(vaultName));
111+
if (Files.isReadable(vaultPasswordPath) && Files.isRegularFile(vaultPasswordPath)){
112+
try (Stream<String> lines = Files.lines(vaultPasswordPath)) {
113+
String vaultPassword = lines.findFirst().get().toString();
114+
if (vaultPassword != "") {
115+
return vaultPassword;
116+
}
117+
return null;
118+
} catch (IOException e) {
119+
return null;
120+
}
121+
}
122+
return null;
123+
}
90124
return vaultPasswords.getProperty(vaultName);
91125
}
92126

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ private static void validate(Args args) throws IllegalArgumentException {
4949

5050
for (String vaultName : args.getVaultNames()) {
5151
Path vaultPath = Paths.get(args.getVaultPath(vaultName));
52+
if ((args.getVaultPasswordPath(vaultName) != null) && args.getVaultPassword(vaultName) == null)
53+
{
54+
throw new IllegalArgumentException("Cannot read password from file: " + Paths.get(args.getVaultPasswordPath(vaultName)));
55+
}
5256
if (!Files.isDirectory(vaultPath)) {
5357
throw new IllegalArgumentException("Not a directory: " + vaultPath);
5458
}

0 commit comments

Comments
 (0)