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,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
0 commit comments