@@ -58,11 +58,11 @@ public class KeystoreManagementTests extends PackagingTestCase {
58
58
public void test10InstallArchiveDistribution () throws Exception {
59
59
assumeTrue (distribution ().isArchive ());
60
60
61
- installation = installArchive (distribution );
61
+ installation = installArchive (sh , distribution );
62
62
verifyArchiveInstallation (installation , distribution ());
63
63
64
64
final Installation .Executables bin = installation .executables ();
65
- Shell .Result r = sh .runIgnoreExitCode (bin .elasticsearchKeystore + " has-passwd" );
65
+ Shell .Result r = sh .runIgnoreExitCode (bin .keystoreTool . toString () + " has-passwd" );
66
66
assertThat ("has-passwd should fail" , r .exitCode , not (is (0 )));
67
67
assertThat ("has-passwd should fail" , r .stderr , containsString ("ERROR: Elasticsearch keystore not found" ));
68
68
}
@@ -72,12 +72,12 @@ public void test11InstallPackageDistribution() throws Exception {
72
72
assumeTrue (distribution ().isPackage ());
73
73
74
74
assertRemoved (distribution );
75
- installation = installPackage (distribution );
75
+ installation = installPackage (sh , distribution );
76
76
assertInstalled (distribution );
77
77
verifyPackageInstallation (installation , distribution , sh );
78
78
79
79
final Installation .Executables bin = installation .executables ();
80
- Shell .Result r = sh .runIgnoreExitCode (bin .elasticsearchKeystore + " has-passwd" );
80
+ Shell .Result r = sh .runIgnoreExitCode (bin .keystoreTool . toString () + " has-passwd" );
81
81
assertThat ("has-passwd should fail" , r .exitCode , not (is (0 )));
82
82
assertThat ("has-passwd should fail" , r .stderr , containsString ("ERROR: Keystore is not password-protected" ));
83
83
}
@@ -90,10 +90,7 @@ public void test20CreateKeystoreManually() throws Exception {
90
90
final Installation .Executables bin = installation .executables ();
91
91
verifyKeystorePermissions ();
92
92
93
- String possibleSudo = distribution ().isArchive () && Platforms .LINUX
94
- ? "sudo -u " + ARCHIVE_OWNER + " "
95
- : "" ;
96
- Shell .Result r = sh .run (possibleSudo + bin .elasticsearchKeystore + " list" );
93
+ Shell .Result r = bin .keystoreTool .run ("list" );
97
94
assertThat (r .stdout , containsString ("keystore.seed" ));
98
95
}
99
96
@@ -109,10 +106,7 @@ public void test30AutoCreateKeystore() throws Exception {
109
106
verifyKeystorePermissions ();
110
107
111
108
final Installation .Executables bin = installation .executables ();
112
- String possibleSudo = distribution ().isArchive () && Platforms .LINUX
113
- ? "sudo -u " + ARCHIVE_OWNER + " "
114
- : "" ;
115
- Shell .Result r = sh .run (possibleSudo + bin .elasticsearchKeystore + " list" );
109
+ Shell .Result r = bin .keystoreTool .run ("list" );
116
110
assertThat (r .stdout , containsString ("keystore.seed" ));
117
111
}
118
112
@@ -192,87 +186,57 @@ public void test50KeystorePasswordFromFile() throws Exception {
192
186
193
187
assertPasswordProtectedKeystore ();
194
188
195
- sh .getEnv ().put ("ES_KEYSTORE_PASSPHRASE_FILE" , esKeystorePassphraseFile .toString ());
196
- distribution ().packagingConditional ()
197
- .forPackage (
198
- () -> sh .run ("sudo systemctl set-environment ES_KEYSTORE_PASSPHRASE_FILE=$ES_KEYSTORE_PASSPHRASE_FILE" )
199
- )
200
- .forArchive (Platforms .NO_ACTION )
201
- .forDocker (/* TODO */ Platforms .NO_ACTION )
202
- .run ();
189
+ try {
190
+ sh .run ("sudo systemctl set-environment ES_KEYSTORE_PASSPHRASE_FILE=" + esKeystorePassphraseFile );
203
191
204
- Files .createFile (esKeystorePassphraseFile );
205
- Files .write (esKeystorePassphraseFile ,
206
- (password + System .lineSeparator ()).getBytes (StandardCharsets .UTF_8 ),
207
- StandardOpenOption .WRITE );
192
+ Files .createFile (esKeystorePassphraseFile );
193
+ Files .write (esKeystorePassphraseFile ,
194
+ (password + System .lineSeparator ()).getBytes (StandardCharsets .UTF_8 ),
195
+ StandardOpenOption .WRITE );
208
196
209
- startElasticsearch ();
210
- ServerUtils .runElasticsearchTests ();
211
- stopElasticsearch ();
212
-
213
- distribution ().packagingConditional ()
214
- .forPackage (
215
- () -> sh .run ("sudo systemctl unset-environment ES_KEYSTORE_PASSPHRASE_FILE" )
216
- )
217
- .forArchive (Platforms .NO_ACTION )
218
- .forDocker (/* TODO */ Platforms .NO_ACTION )
219
- .run ();
197
+ startElasticsearch ();
198
+ ServerUtils .runElasticsearchTests ();
199
+ stopElasticsearch ();
200
+ } finally {
201
+ sh .run ("sudo systemctl unset-environment ES_KEYSTORE_PASSPHRASE_FILE" );
202
+ }
220
203
}
221
204
222
- @ Ignore /* Ignored for feature branch, awaits fix: https://github.com/elastic/elasticsearch/issues/50079 */
223
205
public void test51WrongKeystorePasswordFromFile () throws Exception {
224
206
assumeTrue ("only for systemd" , Platforms .isSystemd () && distribution ().isPackage ());
225
207
Path esKeystorePassphraseFile = installation .config .resolve ("eks" );
226
208
227
209
assertPasswordProtectedKeystore ();
228
210
229
- sh .getEnv ().put ("ES_KEYSTORE_PASSPHRASE_FILE" , esKeystorePassphraseFile .toString ());
230
- distribution ().packagingConditional ()
231
- .forPackage (
232
- () -> sh .run ("sudo systemctl set-environment ES_KEYSTORE_PASSPHRASE_FILE=$ES_KEYSTORE_PASSPHRASE_FILE" )
233
- )
234
- .forArchive (Platforms .NO_ACTION )
235
- .forDocker (/* TODO */ Platforms .NO_ACTION )
236
- .run ();
237
-
238
- if (Files .exists (esKeystorePassphraseFile )) {
239
- rm (esKeystorePassphraseFile );
240
- }
211
+ try {
212
+ sh .run ("sudo systemctl set-environment ES_KEYSTORE_PASSPHRASE_FILE=" + esKeystorePassphraseFile );
241
213
242
- Files .createFile (esKeystorePassphraseFile );
243
- Files .write (esKeystorePassphraseFile ,
244
- ("wrongpassword" + System .lineSeparator ()).getBytes (StandardCharsets .UTF_8 ),
245
- StandardOpenOption .WRITE );
214
+ if (Files .exists (esKeystorePassphraseFile )) {
215
+ rm (esKeystorePassphraseFile );
216
+ }
246
217
247
- Shell .Result result = runElasticsearchStartCommand ();
248
- assertElasticsearchFailure (result , PASSWORD_ERROR_MESSAGE );
218
+ Files .createFile (esKeystorePassphraseFile );
219
+ Files .write (esKeystorePassphraseFile ,
220
+ ("wrongpassword" + System .lineSeparator ()).getBytes (StandardCharsets .UTF_8 ),
221
+ StandardOpenOption .WRITE );
249
222
250
- distribution ().packagingConditional ()
251
- .forPackage (
252
- () -> sh .run ("sudo systemctl unset-environment ES_KEYSTORE_PASSPHRASE_FILE" )
253
- )
254
- .forArchive (Platforms .NO_ACTION )
255
- .forDocker (/* TODO */ Platforms .NO_ACTION )
256
- .run ();
223
+ Shell .Result result = runElasticsearchStartCommand ();
224
+ assertElasticsearchFailure (result , PASSWORD_ERROR_MESSAGE );
225
+ } finally {
226
+ sh .run ("sudo systemctl unset-environment ES_KEYSTORE_PASSPHRASE_FILE" );
227
+ }
257
228
}
258
229
259
230
private void createKeystore () throws Exception {
260
231
Path keystore = installation .config ("elasticsearch.keystore" );
261
232
final Installation .Executables bin = installation .executables ();
262
- Platforms .onLinux (() -> {
263
- distribution ().packagingConditional ()
264
- .forPackage (() -> sh .run (bin .elasticsearchKeystore + " create" ))
265
- .forArchive (() -> sh .run ("sudo -u " + ARCHIVE_OWNER + " " + bin .elasticsearchKeystore + " create" ))
266
- .forDocker (/* TODO */ Platforms .NO_ACTION )
267
- .run ();
268
- });
233
+ bin .keystoreTool .run ("create" );
269
234
270
235
// this is a hack around the fact that we can't run a command in the same session as the same user but not as administrator.
271
236
// the keystore ends up being owned by the Administrators group, so we manually set it to be owned by the vagrant user here.
272
237
// from the server's perspective the permissions aren't really different, this is just to reflect what we'd expect in the tests.
273
238
// when we run these commands as a role user we won't have to do this
274
239
Platforms .onWindows (() -> {
275
- sh .run (bin .elasticsearchKeystore + " create" );
276
240
sh .chown (keystore );
277
241
});
278
242
}
@@ -288,31 +252,37 @@ private void setKeystorePassword(String password) throws Exception {
288
252
final Installation .Executables bin = installation .executables ();
289
253
290
254
// set the password by passing it to stdin twice
291
- Platforms .onLinux (() -> distribution ().packagingConditional ()
292
- .forPackage (() -> sh .run ("( echo \' " + password + "\' ; echo \' " + password + "\' ) | " +
293
- bin .elasticsearchKeystore + " passwd" ))
294
- .forArchive (() -> sh .run ("( echo \' " + password + "\' ; echo \' " + password + "\' ) | " +
295
- "sudo -u " + ARCHIVE_OWNER + " " + bin .elasticsearchKeystore + " passwd" ))
296
- .forDocker (/* TODO */ Platforms .NO_ACTION )
297
- .run ()
298
- );
255
+ Platforms .onLinux (() -> {
256
+ bin .keystoreTool .run ("passwd" , password + "\n " + password + "\n " );
257
+ });
258
+
299
259
Platforms .onWindows (() -> {
300
260
sh .run ("Invoke-Command -ScriptBlock {echo \' " + password + "\' ; echo \' " + password + "\' } | "
301
- + bin .elasticsearchKeystore + " passwd" );
261
+ + bin .keystoreTool + " passwd" );
302
262
});
303
263
}
304
264
305
265
private void assertPasswordProtectedKeystore () {
306
- Shell .Result r = sh . runIgnoreExitCode ( installation .executables ().elasticsearchKeystore . toString () + " has-passwd" );
266
+ Shell .Result r = installation .executables ().keystoreTool . run ( " has-passwd" );
307
267
assertThat ("keystore should be password protected" , r .exitCode , is (0 ));
308
268
}
309
269
310
270
private void verifyKeystorePermissions () throws Exception {
311
271
Path keystore = installation .config ("elasticsearch.keystore" );
312
- distribution ().packagingConditional ()
313
- .forPackage (() -> assertThat (keystore , file (File , "root" , "elasticsearch" , p660 )))
314
- .forArchive (() -> assertThat (keystore , file (File , ARCHIVE_OWNER , ARCHIVE_OWNER , p660 )))
315
- .forDocker (/* TODO */ Platforms .NO_ACTION )
316
- .run ();
272
+ switch (distribution .packaging ) {
273
+ case TAR :
274
+ case ZIP :
275
+ assertThat (keystore , file (File , ARCHIVE_OWNER , ARCHIVE_OWNER , p660 ));
276
+ break ;
277
+ case DEB :
278
+ case RPM :
279
+ assertThat (keystore , file (File , "root" , "elasticsearch" , p660 ));
280
+ break ;
281
+ case DOCKER :
282
+ // TODO #49469
283
+ break ;
284
+ default :
285
+ throw new IllegalStateException ("Unknown Elasticsearch packaging type." );
286
+ }
317
287
}
318
288
}
0 commit comments