21
21
import java .io .PrintStream ;
22
22
import java .lang .reflect .Field ;
23
23
import java .nio .charset .StandardCharsets ;
24
+ import java .nio .file .Files ;
25
+ import java .nio .file .Path ;
26
+ import java .nio .file .Paths ;
27
+ import java .util .Arrays ;
24
28
import java .util .Collections ;
25
29
import java .util .List ;
26
30
import java .util .Map ;
27
31
32
+ import org .apache .commons .lang3 .RandomStringUtils ;
28
33
import org .junit .After ;
29
34
import org .junit .Before ;
30
35
import org .junit .Test ;
31
36
import org .junit .runner .RunWith ;
32
37
33
38
import org .opensearch .security .support .ConfigConstants ;
39
+ import org .opensearch .security .tools .Hasher ;
34
40
import org .opensearch .security .tools .democonfig .util .NoExitSecurityManager ;
35
41
36
42
import static org .hamcrest .MatcherAssert .assertThat ;
39
45
import static org .hamcrest .Matchers .is ;
40
46
import static org .opensearch .security .dlic .rest .validation .RequestContentValidator .ValidationError .INVALID_PASSWORD_INVALID_REGEX ;
41
47
import static org .opensearch .security .dlic .rest .validation .RequestContentValidator .ValidationError .INVALID_PASSWORD_TOO_SHORT ;
48
+ import static org .opensearch .security .tools .democonfig .SecuritySettingsConfigurer .DEFAULT_ADMIN_PASSWORD ;
42
49
import static org .opensearch .security .tools .democonfig .SecuritySettingsConfigurer .DEFAULT_PASSWORD_MIN_LENGTH ;
43
50
import static org .opensearch .security .tools .democonfig .SecuritySettingsConfigurer .REST_ENABLED_ROLES ;
44
51
import static org .opensearch .security .tools .democonfig .SecuritySettingsConfigurer .SYSTEM_INDICES ;
@@ -66,13 +73,14 @@ public class SecuritySettingsConfigurerTests {
66
73
private static Installer installer ;
67
74
68
75
@ Before
69
- public void setUp () {
76
+ public void setUp () throws IOException {
70
77
System .setOut (new PrintStream (outContent ));
71
78
System .setErr (new PrintStream (outContent ));
72
79
installer = Installer .getInstance ();
73
80
installer .buildOptions ();
74
81
securitySettingsConfigurer = new SecuritySettingsConfigurer (installer );
75
82
setUpConf ();
83
+ setUpInternalUsersYML ();
76
84
}
77
85
78
86
@ After
@@ -87,7 +95,7 @@ public void tearDown() throws NoSuchFieldException, IllegalAccessException {
87
95
}
88
96
89
97
@ Test
90
- public void testUpdateAdminPasswordWithCustomPassword () throws NoSuchFieldException , IllegalAccessException {
98
+ public void testUpdateAdminPasswordWithCustomPassword () throws NoSuchFieldException , IllegalAccessException , IOException {
91
99
String customPassword = "myStrongPassword123" ;
92
100
setEnv (adminPasswordKey , customPassword );
93
101
@@ -104,7 +112,7 @@ public void testUpdateAdminPassword_noPasswordSupplied() {
104
112
try {
105
113
System .setSecurityManager (new NoExitSecurityManager ());
106
114
securitySettingsConfigurer .updateAdminPassword ();
107
- } catch (SecurityException e ) {
115
+ } catch (SecurityException | IOException e ) {
108
116
assertThat (e .getMessage (), equalTo ("System.exit(-1) blocked to allow print statement testing." ));
109
117
} finally {
110
118
System .setSecurityManager (null );
@@ -125,7 +133,7 @@ public void testUpdateAdminPasswordWithWeakPassword() throws NoSuchFieldExceptio
125
133
try {
126
134
System .setSecurityManager (new NoExitSecurityManager ());
127
135
securitySettingsConfigurer .updateAdminPassword ();
128
- } catch (SecurityException e ) {
136
+ } catch (SecurityException | IOException e ) {
129
137
assertThat (e .getMessage (), equalTo ("System.exit(-1) blocked to allow print statement testing." ));
130
138
} finally {
131
139
System .setSecurityManager (null );
@@ -148,7 +156,7 @@ public void testUpdateAdminPasswordWithShortPassword() throws NoSuchFieldExcepti
148
156
try {
149
157
System .setSecurityManager (new NoExitSecurityManager ());
150
158
securitySettingsConfigurer .updateAdminPassword ();
151
- } catch (SecurityException e ) {
159
+ } catch (SecurityException | IOException e ) {
152
160
assertThat (e .getMessage (), equalTo ("System.exit(-1) blocked to allow print statement testing." ));
153
161
} finally {
154
162
System .setSecurityManager (null );
@@ -160,7 +168,8 @@ public void testUpdateAdminPasswordWithShortPassword() throws NoSuchFieldExcepti
160
168
}
161
169
162
170
@ Test
163
- public void testUpdateAdminPasswordWithWeakPassword_skipPasswordValidation () throws NoSuchFieldException , IllegalAccessException {
171
+ public void testUpdateAdminPasswordWithWeakPassword_skipPasswordValidation () throws NoSuchFieldException , IllegalAccessException ,
172
+ IOException {
164
173
setEnv (adminPasswordKey , "weakpassword" );
165
174
installer .environment = ExecutionEnvironment .TEST ;
166
175
securitySettingsConfigurer .updateAdminPassword ();
@@ -170,6 +179,49 @@ public void testUpdateAdminPasswordWithWeakPassword_skipPasswordValidation() thr
170
179
verifyStdOutContainsString ("Admin password set successfully." );
171
180
}
172
181
182
+ @ Test
183
+ public void testUpdateAdminPasswordWithCustomInternalUsersYML () throws IOException {
184
+ String internalUsersFile = installer .OPENSEARCH_CONF_DIR + "opensearch-security" + File .separator + "internal_users.yml" ;
185
+ Path internalUsersFilePath = Paths .get (internalUsersFile );
186
+
187
+ List <String > newContent = Arrays .asList (
188
+ "_meta:" ,
189
+ " type: \" internalusers\" " ,
190
+ " config_version: 2" ,
191
+ "admin:" ,
192
+ " hash: " + Hasher .hash (RandomStringUtils .randomAlphanumeric (16 ).toCharArray ()),
193
+ " backend_roles:" ,
194
+ " - \" admin\" "
195
+ );
196
+ // overwriting existing content
197
+ Files .write (internalUsersFilePath , newContent , StandardCharsets .UTF_8 );
198
+
199
+ securitySettingsConfigurer .updateAdminPassword ();
200
+
201
+ verifyStdOutContainsString ("Admin password seems to be custom configured. Skipping update to admin password." );
202
+ }
203
+
204
+ @ Test
205
+ public void testUpdateAdminPasswordWithDefaultInternalUsersYml () {
206
+
207
+ SecuritySettingsConfigurer .ADMIN_PASSWORD = "" ; // to ensure 0 flaky-ness
208
+ try {
209
+ System .setSecurityManager (new NoExitSecurityManager ());
210
+ securitySettingsConfigurer .updateAdminPassword ();
211
+ } catch (SecurityException | IOException e ) {
212
+ assertThat (e .getMessage (), equalTo ("System.exit(-1) blocked to allow print statement testing." ));
213
+ } finally {
214
+ System .setSecurityManager (null );
215
+ }
216
+
217
+ verifyStdOutContainsString (
218
+ String .format (
219
+ "No custom admin password found. Please provide a password via the environment variable %s." ,
220
+ ConfigConstants .OPENSEARCH_INITIAL_ADMIN_PASSWORD
221
+ )
222
+ );
223
+ }
224
+
173
225
@ Test
174
226
public void testSecurityPluginAlreadyConfigured () {
175
227
securitySettingsConfigurer .writeSecurityConfigToOpenSearchYML ();
@@ -353,4 +405,21 @@ void setUpConf() {
353
405
private void verifyStdOutContainsString (String s ) {
354
406
assertThat (outContent .toString (), containsString (s ));
355
407
}
408
+
409
+ private void setUpInternalUsersYML () throws IOException {
410
+ String internalUsersFile = installer .OPENSEARCH_CONF_DIR + "opensearch-security" + File .separator + "internal_users.yml" ;
411
+ Path internalUsersFilePath = Paths .get (internalUsersFile );
412
+ List <String > defaultContent = Arrays .asList (
413
+ "_meta:" ,
414
+ " type: \" internalusers\" " ,
415
+ " config_version: 2" ,
416
+ "admin:" ,
417
+ " hash: " + Hasher .hash (DEFAULT_ADMIN_PASSWORD .toCharArray ()),
418
+ " reserved: " + true ,
419
+ " backend_roles:" ,
420
+ " - \" admin\" " ,
421
+ " description: Demo admin user"
422
+ );
423
+ Files .write (internalUsersFilePath , defaultContent , StandardCharsets .UTF_8 );
424
+ }
356
425
}
0 commit comments