7
7
8
8
import org .apache .logging .log4j .Logger ;
9
9
import org .elasticsearch .SpecialPermission ;
10
+ import org .elasticsearch .common .settings .SecureSetting ;
11
+ import org .elasticsearch .common .settings .SecureString ;
12
+ import org .elasticsearch .common .settings .Setting ;
10
13
import org .elasticsearch .common .settings .Settings ;
11
14
import org .elasticsearch .common .settings .SettingsException ;
12
15
import org .elasticsearch .common .unit .TimeValue ;
24
27
import java .security .PrivilegedActionException ;
25
28
import java .security .PrivilegedExceptionAction ;
26
29
import java .util .Properties ;
30
+ import java .util .Set ;
27
31
28
32
public class Account {
29
33
30
34
static final String SMTP_PROTOCOL = "smtp" ;
35
+ private static final String SMTP_PASSWORD = "password" ;
36
+ private static final Setting <SecureString > SECURE_PASSWORD_SETTING = SecureSetting .secureString ("secure_" + SMTP_PASSWORD , null );
31
37
32
38
static {
33
39
SecurityManager sm = System .getSecurityManager ();
@@ -101,7 +107,7 @@ public Email send(Email email, Authentication auth, Profile profile) throws Mess
101
107
if (auth != null && auth .password () != null ) {
102
108
password = new String (auth .password ().text (cryptoService ));
103
109
} else if (config .smtp .password != null ) {
104
- password = new String (config .smtp .password );
110
+ password = new String (config .smtp .password . getChars () );
105
111
}
106
112
107
113
if (profile == null ) {
@@ -199,18 +205,40 @@ static class Smtp {
199
205
final String host ;
200
206
final int port ;
201
207
final String user ;
202
- final char [] password ;
208
+ final SecureString password ;
203
209
final Properties properties ;
204
210
205
211
Smtp (Settings settings ) {
206
212
host = settings .get ("host" , settings .get ("localaddress" , settings .get ("local_address" )));
213
+
207
214
port = settings .getAsInt ("port" , settings .getAsInt ("localport" , settings .getAsInt ("local_port" , 25 )));
208
215
user = settings .get ("user" , settings .get ("from" , null ));
209
- String passStr = settings . get ( "password" , null );
210
- password = passStr != null ? passStr .toCharArray () : null ;
216
+ password = getSecureSetting ( SMTP_PASSWORD , settings , SECURE_PASSWORD_SETTING );
217
+ // password = passStr != null ? passStr.toCharArray() : null;
211
218
properties = loadSmtpProperties (settings );
212
219
}
213
220
221
+ /**
222
+ * Finds a setting, and then a secure setting if the setting is null, or returns null if one does not exist. This differs
223
+ * from other getSetting calls in that it allows for null whereas the other methods throw an exception.
224
+ *
225
+ * Note: if your setting was not previously secure, than the string reference that is in the setting object is still
226
+ * insecure. This is only constructing a new SecureString with the char[] of the insecure setting.
227
+ */
228
+ private static SecureString getSecureSetting (String settingName , Settings settings , Setting <SecureString > secureSetting ) {
229
+ String value = settings .get (settingName );
230
+ if (value == null ) {
231
+ SecureString secureString = secureSetting .get (settings );
232
+ if (secureString != null && secureString .length () > 0 ) {
233
+ return secureString ;
234
+ } else {
235
+ return null ;
236
+ }
237
+ } else {
238
+ return new SecureString (value .toCharArray ());
239
+ }
240
+ }
241
+
214
242
/**
215
243
* loads the standard Java Mail properties as settings from the given account settings.
216
244
* The standard settings are not that readable, therefore we enabled the user to configure
@@ -231,7 +259,9 @@ static Properties loadSmtpProperties(Settings settings) {
231
259
232
260
settings = builder .build ();
233
261
Properties props = new Properties ();
234
- for (String key : settings .keySet ()) {
262
+ // Secure strings can not be retreived out of a settings object and should be handled differently
263
+ Set <String > insecureSettings = settings .filter (s -> s .startsWith ("secure_" ) == false ).keySet ();
264
+ for (String key : insecureSettings ) {
235
265
props .setProperty (SMTP_SETTINGS_PREFIX + key , settings .get (key ));
236
266
}
237
267
return props ;
0 commit comments