88 *******************************************************************************/
99package 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 ;
1115import java .util .Properties ;
1216import java .util .Set ;
1317import java .util .stream .Collectors ;
18+ import java .util .stream .Stream ;
1419
1520import org .apache .commons .cli .CommandLine ;
1621import 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
0 commit comments