8
8
*******************************************************************************/
9
9
package org .cryptomator .cli ;
10
10
11
+ import java .io .IOException ;
12
+ import java .nio .file .Files ;
13
+ import java .nio .file .Path ;
14
+ import java .nio .file .Paths ;
11
15
import java .util .Properties ;
12
16
import java .util .Set ;
13
17
import java .util .stream .Collectors ;
18
+ import java .util .stream .Stream ;
14
19
15
20
import org .apache .commons .cli .CommandLine ;
16
21
import org .apache .commons .cli .DefaultParser ;
@@ -27,7 +32,8 @@ public class Args {
27
32
private static final String USAGE = "java -jar cryptomator-cli.jar" //
28
33
+ " --bind localhost --port 8080" //
29
34
+ " --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" ;
31
37
private static final Options OPTIONS = new Options ();
32
38
static {
33
39
OPTIONS .addOption (Option .builder () //
@@ -56,18 +62,31 @@ public class Args {
56
62
.valueSeparator () //
57
63
.hasArgs () //
58
64
.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 ());
59
72
}
60
73
61
74
private final String bindAddr ;
62
75
private final int port ;
63
76
private final Properties vaultPaths ;
64
77
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
+ }
65
83
66
84
public Args (CommandLine commandLine ) throws ParseException {
67
85
this .bindAddr = commandLine .getOptionValue ("bind" , "localhost" );
68
86
this .port = Integer .parseInt (commandLine .getOptionValue ("port" , "0" ));
69
87
this .vaultPaths = commandLine .getOptionProperties ("vault" );
70
88
this .vaultPasswords = commandLine .getOptionProperties ("password" );
89
+ this .vaultPasswordFiles = commandLine .getOptionProperties ("passwordfile" );
71
90
}
72
91
73
92
public String getBindAddr () {
@@ -79,14 +98,29 @@ public int getPort() {
79
98
}
80
99
81
100
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 ());
83
102
}
84
103
85
104
public String getVaultPath (String vaultName ) {
86
105
return vaultPaths .getProperty (vaultName );
87
106
}
88
107
108
+ public String getVaultPasswordPath (String vaultName ) {
109
+ return vaultPasswordFiles .getProperty (vaultName );
110
+ }
111
+
89
112
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
+ }
90
124
return vaultPasswords .getProperty (vaultName );
91
125
}
92
126
0 commit comments