@@ -53,45 +53,68 @@ class AddFileKeyStoreCommand extends EnvironmentAwareCommand {
53
53
54
54
@ Override
55
55
protected void execute (Terminal terminal , OptionSet options , Environment env ) throws Exception {
56
- KeyStoreWrapper keystore = KeyStoreWrapper .load (env .configFile ());
57
- if (keystore == null ) {
58
- if (options .has (forceOption ) == false &&
59
- terminal .promptYesNo ("The elasticsearch keystore does not exist. Do you want to create it?" , false ) == false ) {
60
- terminal .println ("Exiting without creating keystore." );
61
- return ;
56
+ char [] password = null ;
57
+ char [] passwordVerification = null ;
58
+ try {
59
+ KeyStoreWrapper keystore = KeyStoreWrapper .load (env .configFile ());
60
+ if (keystore == null ) {
61
+ if (options .has (forceOption ) == false &&
62
+ terminal .promptYesNo ("The elasticsearch keystore does not exist. Do you want to create it?" , false ) == false ) {
63
+ terminal .println ("Exiting without creating keystore." );
64
+ return ;
65
+ }
66
+ password = terminal .readSecret ("Enter passphrase for the elasticsearch keystore (empty for no passphrase): " );
67
+ passwordVerification = terminal .readSecret ("Enter same passphrase again: " );
68
+ if (Arrays .equals (password , passwordVerification ) == false ) {
69
+ throw new UserException (ExitCodes .DATA_ERROR , "Passphrases are not equal, exiting." );
70
+ }
71
+ keystore = KeyStoreWrapper .create ();
72
+ keystore .save (env .configFile (), password );
73
+ terminal .println ("Created elasticsearch keystore in " + env .configFile ());
74
+ } else {
75
+ if (keystore .hasPassword ()) {
76
+ password = terminal .readSecret ("Enter passphrase for the elasticsearch keystore: " );
77
+ } else {
78
+ password = new char [0 ];
79
+ }
80
+ keystore .decrypt (password );
62
81
}
63
- keystore = KeyStoreWrapper .create ();
64
- keystore .save (env .configFile (), new char [0 ] /* always use empty passphrase for auto created keystore */ );
65
- terminal .println ("Created elasticsearch keystore in " + env .configFile ());
66
- } else {
67
- keystore .decrypt (new char [0 ] /* TODO: prompt for password when they are supported */ );
68
- }
69
82
70
- List <String > argumentValues = arguments .values (options );
71
- if (argumentValues .size () == 0 ) {
72
- throw new UserException (ExitCodes .USAGE , "Missing setting name" );
73
- }
74
- String setting = argumentValues .get (0 );
75
- if (keystore .getSettingNames ().contains (setting ) && options .has (forceOption ) == false ) {
76
- if (terminal .promptYesNo ("Setting " + setting + " already exists. Overwrite?" , false ) == false ) {
77
- terminal .println ("Exiting without modifying keystore." );
78
- return ;
83
+ List <String > argumentValues = arguments .values (options );
84
+ if (argumentValues .size () == 0 ) {
85
+ throw new UserException (ExitCodes .USAGE , "Missing setting name" );
86
+ }
87
+ String setting = argumentValues .get (0 );
88
+ if (keystore .getSettingNames ().contains (setting ) && options .has (forceOption ) == false ) {
89
+ if (terminal .promptYesNo ("Setting " + setting + " already exists. Overwrite?" , false ) == false ) {
90
+ terminal .println ("Exiting without modifying keystore." );
91
+ return ;
92
+ }
79
93
}
80
- }
81
94
82
- if (argumentValues .size () == 1 ) {
83
- throw new UserException (ExitCodes .USAGE , "Missing file name" );
84
- }
85
- Path file = getPath (argumentValues .get (1 ));
86
- if (Files .exists (file ) == false ) {
87
- throw new UserException (ExitCodes .IO_ERROR , "File [" + file .toString () + "] does not exist" );
88
- }
89
- if (argumentValues .size () > 2 ) {
90
- throw new UserException (ExitCodes .USAGE , "Unrecognized extra arguments [" +
91
- String .join (", " , argumentValues .subList (2 , argumentValues .size ())) + "] after filepath" );
95
+ if (argumentValues .size () == 1 ) {
96
+ throw new UserException (ExitCodes .USAGE , "Missing file name" );
97
+ }
98
+ Path file = getPath (argumentValues .get (1 ));
99
+ if (Files .exists (file ) == false ) {
100
+ throw new UserException (ExitCodes .IO_ERROR , "File [" + file .toString () + "] does not exist" );
101
+ }
102
+ if (argumentValues .size () > 2 ) {
103
+ throw new UserException (ExitCodes .USAGE , "Unrecognized extra arguments [" +
104
+ String .join (", " , argumentValues .subList (2 , argumentValues .size ())) + "] after filepath" );
105
+ }
106
+ keystore .setFile (setting , Files .readAllBytes (file ));
107
+ keystore .save (env .configFile (), password );
108
+ } catch (SecurityException e ) {
109
+ throw new UserException (ExitCodes .DATA_ERROR , "Failed to access the keystore. Please make sure the passphrase was correct." );
110
+ } finally {
111
+ if (null != password ) {
112
+ Arrays .fill (password , '\u0000' );
113
+ }
114
+ if (null != passwordVerification ) {
115
+ Arrays .fill (passwordVerification , '\u0000' );
116
+ }
92
117
}
93
- keystore .setFile (setting , Files .readAllBytes (file ));
94
- keystore .save (env .configFile (), new char [0 ]);
95
118
}
96
119
97
120
@ SuppressForbidden (reason ="file arg for cli" )
0 commit comments