Skip to content

Commit 893d4a2

Browse files
Use an options loop in Elasticsearch startup script (#51547)
* Use loop to parse options rather than grep * Add test for --help flag with encrypted keystore
1 parent 06c5db2 commit 893d4a2

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

distribution/src/bin/elasticsearch

+15-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@
1616

1717
source "`dirname "$0"`"/elasticsearch-env
1818

19+
CHECK_KEYSTORE=true
20+
DAEMONIZE=false
21+
for option in "$@"; do
22+
case "$option" in
23+
-h|--help|-V|--version)
24+
CHECK_KEYSTORE=false
25+
;;
26+
-d|--daemonize)
27+
DAEMONIZE=true
28+
;;
29+
esac
30+
done
31+
1932
if [ -z "$ES_TMPDIR" ]; then
2033
ES_TMPDIR=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.TempDirectory`
2134
fi
@@ -24,7 +37,7 @@ fi
2437
# conflicting GC configurations for the keystore tools
2538
unset KEYSTORE_PASSWORD
2639
KEYSTORE_PASSWORD=
27-
if ! echo $* | grep -E -q '(^-h |-h$| -h |--help$|--help |^-V |-V$| -V |--version$|--version )' \
40+
if [[ $CHECK_KEYSTORE = true ]] \
2841
&& "`dirname "$0"`"/elasticsearch-keystore has-passwd --silent
2942
then
3043
if ! read -s -r -p "Elasticsearch keystore password: " KEYSTORE_PASSWORD ; then
@@ -37,7 +50,7 @@ ES_JVM_OPTIONS="$ES_PATH_CONF"/jvm.options
3750
ES_JAVA_OPTS=`export ES_TMPDIR; "$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.JvmOptionsParser "$ES_JVM_OPTIONS"`
3851

3952
# manual parsing to find out, if process should be detached
40-
if ! echo $* | grep -E '(^-d |-d$| -d |--daemonize$|--daemonize )' > /dev/null; then
53+
if [[ $DAEMONIZE = false ]]; then
4154
exec \
4255
"$JAVA" \
4356
$ES_JAVA_OPTS \

qa/os/src/test/java/org/elasticsearch/packaging/test/KeystoreManagementTests.java

+20
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import static org.hamcrest.CoreMatchers.containsString;
5757
import static org.hamcrest.CoreMatchers.is;
5858
import static org.hamcrest.CoreMatchers.notNullValue;
59+
import static org.hamcrest.Matchers.startsWith;
5960
import static org.junit.Assume.assumeThat;
6061
import static org.junit.Assume.assumeTrue;
6162

@@ -210,6 +211,25 @@ public void test43WrongKeystorePasswordOnTty() throws Exception {
210211
assertThat(result.stdout, containsString(ERROR_INCORRECT_PASSWORD));
211212
}
212213

214+
/**
215+
* If we have an encrypted keystore, we shouldn't require a password to
216+
* view help information.
217+
*/
218+
public void test44EncryptedKeystoreAllowsHelpMessage() throws Exception {
219+
assumeTrue("users call elasticsearch directly in archive case",
220+
distribution.isArchive());
221+
222+
String password = "keystorepass";
223+
224+
rmKeystoreIfExists();
225+
createKeystore();
226+
setKeystorePassword(password);
227+
228+
assertPasswordProtectedKeystore();
229+
Shell.Result r = installation.executables().elasticsearch.run("--help");
230+
assertThat(r.stdout, startsWith("Starts Elasticsearch"));
231+
}
232+
213233
public void test50KeystorePasswordFromFile() throws Exception {
214234
assumeTrue("only for systemd", Platforms.isSystemd() && distribution().isPackage());
215235
String password = "!@#$%^&*()|\\<>/?";

0 commit comments

Comments
 (0)