From 01b5a46c24d24895aa3a4eb78917534161fb76d4 Mon Sep 17 00:00:00 2001 From: Jason Tedor Date: Wed, 6 Jun 2018 21:57:58 -0400 Subject: [PATCH] Pass main class by environment variable on Windows (#31156) A previous refactoring of the CLI scripts migrated all of the CLI tools to shell to a common script, elasticsearch-cli. This approach is fine in Bash where it is easy to tear arguments apart but it doesn't work so well on Windows where quoting is insane. To avoid having to tear the arguments apart to separate the first argument to elasticsearch-cli from the remaining arguments, we instead choose a strategy where we can avoid tearing the arguments apart. To do this, we will instead pass the main class by an environment variable and then we can pass the arguments straight through. This will let us avoid awful quoting issues on Windows. This is the Windows side of that effort and the Bash side was in a previous commit. --- distribution/src/bin/elasticsearch-cli.bat | 9 ++------- distribution/src/bin/elasticsearch-keystore.bat | 2 +- distribution/src/bin/elasticsearch-plugin.bat | 2 +- distribution/src/bin/elasticsearch-translog.bat | 2 +- .../security/src/main/bin/elasticsearch-certgen.bat | 2 +- .../security/src/main/bin/elasticsearch-certutil.bat | 2 +- .../security/src/main/bin/elasticsearch-migrate.bat | 2 +- .../src/main/bin/elasticsearch-saml-metadata.bat | 2 +- .../src/main/bin/elasticsearch-setup-passwords.bat | 2 +- .../security/src/main/bin/elasticsearch-syskeygen.bat | 2 +- .../plugin/security/src/main/bin/elasticsearch-users.bat | 2 +- .../watcher/src/main/bin/elasticsearch-croneval.bat | 2 +- 12 files changed, 13 insertions(+), 18 deletions(-) diff --git a/distribution/src/bin/elasticsearch-cli.bat b/distribution/src/bin/elasticsearch-cli.bat index e85abdee4487f..b668a7c06c272 100644 --- a/distribution/src/bin/elasticsearch-cli.bat +++ b/distribution/src/bin/elasticsearch-cli.bat @@ -6,11 +6,6 @@ if defined ES_ADDITIONAL_SOURCES ( ) ) -for /f "tokens=1*" %%a in ("%*") do ( - set main_class=%%a - set arguments=%%b -) - if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES ( for %%a in ("%ES_ADDITIONAL_CLASSPATH_DIRECTORIES:;=","%") do ( set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/%%a/* @@ -24,5 +19,5 @@ if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES ( -Des.distribution.flavor="%ES_DISTRIBUTION_FLAVOR%" ^ -Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^ -cp "%ES_CLASSPATH%" ^ - %main_class% ^ - %arguments% + "%ES_MAIN_CLASS%" ^ + %* diff --git a/distribution/src/bin/elasticsearch-keystore.bat b/distribution/src/bin/elasticsearch-keystore.bat index 380a3e501d57e..b43182a273f6b 100644 --- a/distribution/src/bin/elasticsearch-keystore.bat +++ b/distribution/src/bin/elasticsearch-keystore.bat @@ -3,8 +3,8 @@ setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.common.settings.KeyStoreCli call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.common.settings.KeyStoreCli ^ %%* ^ || exit /b 1 diff --git a/distribution/src/bin/elasticsearch-plugin.bat b/distribution/src/bin/elasticsearch-plugin.bat index 5d7b1d7a8283d..7e71de790f03e 100644 --- a/distribution/src/bin/elasticsearch-plugin.bat +++ b/distribution/src/bin/elasticsearch-plugin.bat @@ -3,9 +3,9 @@ setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.plugins.PluginCli set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/plugin-cli call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.plugins.PluginCli ^ %%* ^ || exit /b 1 diff --git a/distribution/src/bin/elasticsearch-translog.bat b/distribution/src/bin/elasticsearch-translog.bat index 9c4cefcf2fe64..6a2e3046205fe 100644 --- a/distribution/src/bin/elasticsearch-translog.bat +++ b/distribution/src/bin/elasticsearch-translog.bat @@ -3,8 +3,8 @@ setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.index.translog.TranslogToolCli call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.index.translog.TranslogToolCli ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-certgen.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-certgen.bat index 01f3c0f21cd77..b5842b57b168c 100644 --- a/x-pack/plugin/security/src/main/bin/elasticsearch-certgen.bat +++ b/x-pack/plugin/security/src/main/bin/elasticsearch-certgen.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.core.ssl.CertificateGenerateTool set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.core.ssl.CertificateGenerateTool ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-certutil.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-certutil.bat index f8a5fd9880a62..2e397190f23a9 100644 --- a/x-pack/plugin/security/src/main/bin/elasticsearch-certutil.bat +++ b/x-pack/plugin/security/src/main/bin/elasticsearch-certutil.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.core.ssl.CertificateTool set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.core.ssl.CertificateTool ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-migrate.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-migrate.bat index 67faf2ea66afc..4b8e4f926d797 100644 --- a/x-pack/plugin/security/src/main/bin/elasticsearch-migrate.bat +++ b/x-pack/plugin/security/src/main/bin/elasticsearch-migrate.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.ESNativeRealmMigrateTool set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.security.authc.esnative.ESNativeRealmMigrateTool ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata.bat index 6cdd539a81da3..64a272dfbb5a6 100644 --- a/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata.bat +++ b/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.saml.SamlMetadataCommand set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.security.authc.saml.SamlMetadataCommand ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords.bat index e3ea134ae43b3..3c956ca47ba26 100644 --- a/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords.bat +++ b/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordTool set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordTool ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen.bat index 570eef619ec28..11414872d073b 100644 --- a/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen.bat +++ b/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.security.crypto.tool.SystemKeyTool set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.security.crypto.tool.SystemKeyTool ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-users.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-users.bat index 2975fbe87b976..9b35895ed86c1 100644 --- a/x-pack/plugin/security/src/main/bin/elasticsearch-users.bat +++ b/x-pack/plugin/security/src/main/bin/elasticsearch-users.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.file.tool.UsersTool set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.security.authc.file.tool.UsersTool ^ %%* ^ || exit /b 1 diff --git a/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval.bat b/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval.bat index 281b06cf77b17..2b4a33c9f9e75 100644 --- a/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval.bat +++ b/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval.bat @@ -7,9 +7,9 @@ rem you may not use this file except in compliance with the Elastic License. setlocal enabledelayedexpansion setlocal enableextensions +set ES_MAIN_CLASS=org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalTool set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-watcher-env call "%~dp0elasticsearch-cli.bat" ^ - org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalTool ^ %%* ^ || exit /b 1