Skip to content

Commit 3987162

Browse files
Merge pull request #9 from FZachlod/develop
Adding passwordfile option to securely read password for each vault f…
2 parents e186231 + 78f2d74 commit 3987162

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

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,31 @@ 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;
79+
80+
private boolean hasPasswordOrPasswordFile(Object vaultPath) {
81+
return vaultPasswords.containsKey(vaultPath) || vaultPasswordFiles.containsKey(vaultPath);
82+
}
6583

6684
public Args(CommandLine commandLine) throws ParseException {
6785
this.bindAddr = commandLine.getOptionValue("bind", "localhost");
6886
this.port = Integer.parseInt(commandLine.getOptionValue("port", "0"));
6987
this.vaultPaths = commandLine.getOptionProperties("vault");
7088
this.vaultPasswords = commandLine.getOptionProperties("password");
89+
this.vaultPasswordFiles = commandLine.getOptionProperties("passwordfile");
7190
}
7291

7392
public String getBindAddr() {
@@ -79,14 +98,29 @@ public int getPort() {
7998
}
8099

81100
public Set<String> getVaultNames() {
82-
return vaultPaths.keySet().stream().filter(vaultPasswords::containsKey).map(String.class::cast).collect(Collectors.toSet());
101+
return vaultPaths.keySet().stream().filter(this::hasPasswordOrPasswordFile).map(String.class::cast).collect(Collectors.toSet());
83102
}
84103

85104
public String getVaultPath(String vaultName) {
86105
return vaultPaths.getProperty(vaultName);
87106
}
88107

108+
public String getVaultPasswordPath(String vaultName) {
109+
return vaultPasswordFiles.getProperty(vaultName);
110+
}
111+
89112
public String getVaultPassword(String vaultName) {
113+
if (vaultPasswords.getProperty(vaultName) == null){
114+
Path vaultPasswordPath = Paths.get(vaultPasswordFiles.getProperty(vaultName));
115+
if (Files.isReadable(vaultPasswordPath) && Files.isRegularFile(vaultPasswordPath)){
116+
try (Stream<String> lines = Files.lines(vaultPasswordPath)) {
117+
return lines.findFirst().get().toString();
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: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ 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+
throw new IllegalArgumentException("Cannot read password from file: " + Paths.get(args.getVaultPasswordPath(vaultName)));
54+
}
5255
if (!Files.isDirectory(vaultPath)) {
5356
throw new IllegalArgumentException("Not a directory: " + vaultPath);
5457
}

0 commit comments

Comments
 (0)