diff --git a/distribution/build.gradle b/distribution/build.gradle
index afee2e524c40..b56c75c71d7e 100644
--- a/distribution/build.gradle
+++ b/distribution/build.gradle
@@ -230,7 +230,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
* Properties to expand when copying packaging files *
*****************************************************************************/
configurations {
- ['libs', 'libsLaunchers', 'libsVersionChecker', 'libsPluginCli', 'libsKeystoreCli', 'libsSecurityCli', 'libsGeoIpCli', 'libsAnsiConsole'].each {
+ ['libs', 'libsLaunchers', 'libsVersionChecker', 'libsCliLauncher', 'libsPluginCli', 'libsKeystoreCli', 'libsSecurityCli', 'libsGeoIpCli', 'libsAnsiConsole'].each {
create(it) {
canBeConsumed = false
canBeResolved = true
@@ -251,6 +251,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
libs project(':server')
libsVersionChecker project(':distribution:tools:java-version-checker')
+ libsCliLauncher project(':distribution:tools:cli-launcher')
libsLaunchers project(':distribution:tools:launchers')
libsAnsiConsole project(':distribution:tools:ansi-console')
libsPluginCli project(':distribution:tools:plugin-cli')
@@ -274,6 +275,9 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
into('java-version-checker') {
from(configurations.libsVersionChecker)
}
+ into('cli-launcher') {
+ from(configurations.libsCliLauncher)
+ }
into('tools/geoip-cli') {
from(configurations.libsGeoIpCli)
}
@@ -370,12 +374,7 @@ configure(subprojects.findAll { ['archives', 'packages'].contains(it.name) }) {
// module provided bin files
with copySpec {
eachFile { it.setMode(0755) }
- if (testDistro == false) {
- from(defaultBinFiles)
- } else {
- from(defaultBinFiles)
- include 'x-pack-env', 'x-pack-security-env'
- }
+ from(defaultBinFiles)
if (distributionType != 'zip') {
exclude '*.bat'
}
diff --git a/distribution/packages/src/common/scripts/postinst b/distribution/packages/src/common/scripts/postinst
index 549a8753bab9..c97c2d85d54a 100644
--- a/distribution/packages/src/common/scripts/postinst
+++ b/distribution/packages/src/common/scripts/postinst
@@ -59,17 +59,15 @@ if [ "x$IS_UPGRADE" != "xtrue" ]; then
# Don't exit immediately on error, we want to hopefully print some helpful banners
set +e
# Attempt to auto-configure security, this seems to be an installation
- if ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
+ if CLI_NAME="auto-configure-node" \
+ CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
/usr/share/elasticsearch/bin/elasticsearch-cli <<< ""; then
# Above command runs as root and TLS keystores are created group-owned by root. It's simple to correct the ownership here
chown root:elasticsearch "${ES_PATH_CONF}"/certs/http.p12
chown root:elasticsearch "${ES_PATH_CONF}"/certs/http_ca.crt
chown root:elasticsearch "${ES_PATH_CONF}"/certs/transport.p12
- if INITIAL_PASSWORD=$(ES_MAIN_CLASS=org.elasticsearch.xpack.security.enrollment.tool.AutoConfigGenerateElasticPasswordHash \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
+ if INITIAL_PASSWORD=$(CLI_NAME=auto-config-gen-passwd \
+ CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
/usr/share/elasticsearch/bin/elasticsearch-cli); then
echo "--------------------------- Security autoconfiguration information ------------------------------"
echo
diff --git a/distribution/src/bin/elasticsearch b/distribution/src/bin/elasticsearch
index 81facb4de1bd..f8f6ae0b29b0 100755
--- a/distribution/src/bin/elasticsearch
+++ b/distribution/src/bin/elasticsearch
@@ -66,9 +66,8 @@ then
fi
if [[ $ENROLL_TO_CLUSTER = true ]]; then
- ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
+ CLI_NAME="auto-configure-node" \
+ CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
bin/elasticsearch-cli "${ARG_LIST[@]}" <<<"$KEYSTORE_PASSWORD"
elif [[ $ATTEMPT_SECURITY_AUTO_CONFIG = true ]]; then
# It is possible that an auto-conf failure prevents the node from starting, but this is only the exceptional case (exit code 1).
@@ -76,9 +75,8 @@ elif [[ $ATTEMPT_SECURITY_AUTO_CONFIG = true ]]; then
# if the error is uncommon or unexpected, but it should otherwise let the node to start as usual.
# It is passed in all the command line options in order to read the node settings ones (-E), while the other parameters are ignored
# (a small caveat is that it also inspects the -v option in order to provide more information on how auto config went)
- if ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
+ if CLI_NAME="auto-configure-node" \
+ CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli" \
bin/elasticsearch-cli "${ARG_LIST[@]}" <<<"$KEYSTORE_PASSWORD"; then
:
else
diff --git a/distribution/src/bin/elasticsearch-cli b/distribution/src/bin/elasticsearch-cli
index 35b3eddf12e0..8d7e65cc382d 100644
--- a/distribution/src/bin/elasticsearch-cli
+++ b/distribution/src/bin/elasticsearch-cli
@@ -4,28 +4,21 @@ set -e -o pipefail
source "`dirname "$0"`"/elasticsearch-env
-IFS=';' read -r -a additional_sources <<< "$ES_ADDITIONAL_SOURCES"
-for additional_source in "${additional_sources[@]}"
-do
- source "$ES_HOME"/bin/$additional_source
-done
-
-IFS=';' read -r -a additional_classpath_directories <<< "$ES_ADDITIONAL_CLASSPATH_DIRECTORIES"
-for additional_classpath_directory in "${additional_classpath_directories[@]}"
-do
- ES_CLASSPATH="$ES_CLASSPATH:$ES_HOME/$additional_classpath_directory/*"
-done
-
# use a small heap size for the CLI tools, and thus the serial collector to
-# avoid stealing many CPU cycles; a user can override by setting ES_JAVA_OPTS
-ES_JAVA_OPTS="-Xms4m -Xmx64m -XX:+UseSerialGC ${ES_JAVA_OPTS}"
+# avoid stealing many CPU cycles; a user can override by setting CLI_JAVA_OPTS
+CLI_JAVA_OPTS="-Xms4m -Xmx64m -XX:+UseSerialGC ${CLI_JAVA_OPTS}"
+
+LAUNCHER_CLASSPATH=$ES_HOME/lib/*:$ES_HOME/lib/cli-launcher/*
exec \
"$JAVA" \
- $ES_JAVA_OPTS \
+ $CLI_JAVA_OPTS \
+ -Dcli.name="$CLI_NAME" \
+ -Dcli.script="$0" \
+ -Dcli.libs="$CLI_LIBS" \
-Des.path.home="$ES_HOME" \
-Des.path.conf="$ES_PATH_CONF" \
-Des.distribution.type="$ES_DISTRIBUTION_TYPE" \
- -cp "$ES_CLASSPATH" \
- "$ES_MAIN_CLASS" \
+ -cp "$LAUNCHER_CLASSPATH" \
+ org.elasticsearch.launcher.CliToolLauncher \
"$@"
diff --git a/distribution/src/bin/elasticsearch-cli.bat b/distribution/src/bin/elasticsearch-cli.bat
index a69a02da2b8a..0a037110fde9 100644
--- a/distribution/src/bin/elasticsearch-cli.bat
+++ b/distribution/src/bin/elasticsearch-cli.bat
@@ -1,28 +1,27 @@
+@echo off
+
+setlocal enabledelayedexpansion
+
call "%~dp0elasticsearch-env.bat" || exit /b 1
-if defined ES_ADDITIONAL_SOURCES (
- for %%a in ("%ES_ADDITIONAL_SOURCES:;=","%") do (
- call "%~dp0%%a"
- )
-)
-
-if defined ES_ADDITIONAL_CLASSPATH_DIRECTORIES (
- for %%a in ("%ES_ADDITIONAL_CLASSPATH_DIRECTORIES:;=","%") do (
- set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/%%a/*
- )
-)
-
rem use a small heap size for the CLI tools, and thus the serial collector to
rem avoid stealing many CPU cycles; a user can override by setting ES_JAVA_OPTS
set ES_JAVA_OPTS=-Xms4m -Xmx64m -XX:+UseSerialGC %ES_JAVA_OPTS%
+set LAUNCHER_CLASSPATH=%ES_HOME%/lib/*;%ES_HOME%/lib/cli-launcher/*
+
%JAVA% ^
- %ES_JAVA_OPTS% ^
+ %CLI_JAVA_OPTS% ^
+ -Dcli.name="%CLI_NAME%" ^
+ -Dcli.script="%CLI_SCRIPT%" ^
+ -Dcli.libs="%CLI_LIBS%" ^
-Des.path.home="%ES_HOME%" ^
-Des.path.conf="%ES_PATH_CONF%" ^
-Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
- -cp "%ES_CLASSPATH%" ^
- "%ES_MAIN_CLASS%" ^
+ -cp "%LAUNCHER_CLASSPATH%" ^
+ org.elasticsearch.launcher.CliToolLauncher ^
%*
exit /b %ERRORLEVEL%
+
+endlocal
diff --git a/distribution/src/bin/elasticsearch-geoip b/distribution/src/bin/elasticsearch-geoip
index 47cf3adfa75d..162b504a4e6d 100755
--- a/distribution/src/bin/elasticsearch-geoip
+++ b/distribution/src/bin/elasticsearch-geoip
@@ -1,6 +1,4 @@
#!/bin/bash
-ES_MAIN_CLASS=org.elasticsearch.geoip.GeoIpCli \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/geoip-cli \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS=lib/tools/geoip-cli
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/distribution/src/bin/elasticsearch-geoip.bat b/distribution/src/bin/elasticsearch-geoip.bat
index a1d82ba16c57..bd167c6a65cb 100644
--- a/distribution/src/bin/elasticsearch-geoip.bat
+++ b/distribution/src/bin/elasticsearch-geoip.bat
@@ -3,8 +3,8 @@
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.geoip.GeoIpCli
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/geoip-cli
+set CLI_SCRIPT=%~0
+set CLI_LIBS=lib/tools/geoip-cli
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/distribution/src/bin/elasticsearch-keystore b/distribution/src/bin/elasticsearch-keystore
index c24eb47536e2..6bc966a6efdc 100755
--- a/distribution/src/bin/elasticsearch-keystore
+++ b/distribution/src/bin/elasticsearch-keystore
@@ -1,6 +1,4 @@
#!/bin/bash
-ES_MAIN_CLASS=org.elasticsearch.cli.keystore.KeyStoreCli \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS=lib/tools/keystore-cli
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/distribution/src/bin/elasticsearch-keystore.bat b/distribution/src/bin/elasticsearch-keystore.bat
index 59ca167ab519..d5397ad300d7 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.cli.keystore.KeyStoreCli
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/keystore-cli
+set CLI_SCRIPT=%~0
+set CLI_LIBS=lib/tools/keystore-cli
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/distribution/src/bin/elasticsearch-node b/distribution/src/bin/elasticsearch-node
index 29949486b552..dc3137628675 100755
--- a/distribution/src/bin/elasticsearch-node
+++ b/distribution/src/bin/elasticsearch-node
@@ -1,5 +1,3 @@
#!/bin/bash
-ES_MAIN_CLASS=org.elasticsearch.cluster.coordination.NodeToolCli \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/distribution/src/bin/elasticsearch-node.bat b/distribution/src/bin/elasticsearch-node.bat
index b152331d5ef8..14741fcd9ac6 100644
--- a/distribution/src/bin/elasticsearch-node.bat
+++ b/distribution/src/bin/elasticsearch-node.bat
@@ -3,7 +3,7 @@
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.cluster.coordination.NodeToolCli
+set CLI_SCRIPT=%~0
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/distribution/src/bin/elasticsearch-plugin b/distribution/src/bin/elasticsearch-plugin
index a5fa482e2288..7ef36fad4fe0 100755
--- a/distribution/src/bin/elasticsearch-plugin
+++ b/distribution/src/bin/elasticsearch-plugin
@@ -1,7 +1,5 @@
#!/bin/bash
-ES_JAVA_OPTS="--add-opens java.base/sun.security.provider=ALL-UNNAMED $ES_JAVA_OPTS" \
- ES_MAIN_CLASS=org.elasticsearch.plugins.cli.PluginCli \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/plugin-cli \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_JAVA_OPTS="--add-opens java.base/sun.security.provider=ALL-UNNAMED $CLI_JAVA_OPTS"
+CLI_LIBS=lib/tools/plugin-cli
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/distribution/src/bin/elasticsearch-plugin.bat b/distribution/src/bin/elasticsearch-plugin.bat
index 2da1d49b9b8a..75a100a56bc4 100644
--- a/distribution/src/bin/elasticsearch-plugin.bat
+++ b/distribution/src/bin/elasticsearch-plugin.bat
@@ -3,13 +3,13 @@
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_JAVA_OPTS=--add-opens java.base/sun.security.provider=ALL-UNNAMED %ES_JAVA_OPTS%
-set ES_MAIN_CLASS=org.elasticsearch.plugins.cli.PluginCli
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/plugin-cli
+set CLI_JAVA_OPTS=--add-opens java.base/sun.security.provider=ALL-UNNAMED %CLI_JAVA_OPTS%
+set CLI_SCRIPT=%~0
+set CLI_LIBS=lib/tools/plugin-cli
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
-
+
endlocal
endlocal
diff --git a/distribution/src/bin/elasticsearch-shard b/distribution/src/bin/elasticsearch-shard
index 4c14a0434175..dc3137628675 100755
--- a/distribution/src/bin/elasticsearch-shard
+++ b/distribution/src/bin/elasticsearch-shard
@@ -1,5 +1,3 @@
#!/bin/bash
-ES_MAIN_CLASS=org.elasticsearch.index.shard.ShardToolCli \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/distribution/src/bin/elasticsearch-shard.bat b/distribution/src/bin/elasticsearch-shard.bat
index 4db48f141fd6..14741fcd9ac6 100644
--- a/distribution/src/bin/elasticsearch-shard.bat
+++ b/distribution/src/bin/elasticsearch-shard.bat
@@ -3,7 +3,7 @@
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.index.shard.ShardToolCli
+set CLI_SCRIPT=%~0
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/distribution/src/bin/elasticsearch.bat b/distribution/src/bin/elasticsearch.bat
index ef18f0ce91dc..d76be6e67a87 100644
--- a/distribution/src/bin/elasticsearch.bat
+++ b/distribution/src/bin/elasticsearch.bat
@@ -102,11 +102,9 @@ SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^>=^^^>!
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^\=^^^\!
IF "%attemptautoconfig%"=="Y" (
- ECHO.!KEYSTORE_PASSWORD!| %JAVA% %ES_JAVA_OPTS% ^
- -Des.path.home="%ES_HOME%" ^
- -Des.path.conf="%ES_PATH_CONF%" ^
- -Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
- -cp "!ES_CLASSPATH!;!ES_HOME!/lib/tools/security-cli/*;!ES_HOME!/modules/x-pack-core/*;!ES_HOME!/modules/x-pack-security/*" "org.elasticsearch.xpack.security.cli.AutoConfigureNode" !newparams!
+ SET CLI_NAME=auto-configure-node
+ SET CLI_LIBS=modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli
+ ECHO.!KEYSTORE_PASSWORD!|call "%~dp0elasticsearch-cli.bat" !newparams!
SET SHOULDEXIT=Y
IF !ERRORLEVEL! EQU 0 SET SHOULDEXIT=N
IF !ERRORLEVEL! EQU 73 SET SHOULDEXIT=N
@@ -118,12 +116,9 @@ IF "%attemptautoconfig%"=="Y" (
)
IF "!enrolltocluster!"=="Y" (
- ECHO.!KEYSTORE_PASSWORD!| %JAVA% %ES_JAVA_OPTS% ^
- -Des.path.home="%ES_HOME%" ^
- -Des.path.conf="%ES_PATH_CONF%" ^
- -Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
- -cp "!ES_CLASSPATH!;!ES_HOME!/lib/tools/security-cli/*;!ES_HOME!/modules/x-pack-core/*;!ES_HOME!/modules/x-pack-security/*" "org.elasticsearch.xpack.security.cli.AutoConfigureNode" ^
- !newparams! --enrollment-token %enrollmenttoken%
+ SET CLI_NAME=auto-configure-node
+ SET CLI_LIBS=modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli
+ ECHO.!KEYSTORE_PASSWORD!|call "%~dp0elasticsearch-cli.bat" !newparams! --enrollment-token %enrollmenttoken%
IF !ERRORLEVEL! NEQ 0 (
exit /b !ERRORLEVEL!
)
diff --git a/distribution/tools/cli-launcher/build.gradle b/distribution/tools/cli-launcher/build.gradle
new file mode 100644
index 000000000000..17eaf3e018a0
--- /dev/null
+++ b/distribution/tools/cli-launcher/build.gradle
@@ -0,0 +1,9 @@
+apply plugin: 'elasticsearch.java'
+
+dependencies {
+ compileOnly project(':server')
+
+ testImplementation project(":test:framework")
+}
+
+tasks.named("javadoc").configure { enabled = false }
diff --git a/distribution/tools/cli-launcher/src/main/java/org/elasticsearch/launcher/CliToolLauncher.java b/distribution/tools/cli-launcher/src/main/java/org/elasticsearch/launcher/CliToolLauncher.java
new file mode 100644
index 000000000000..5952def15ad8
--- /dev/null
+++ b/distribution/tools/cli-launcher/src/main/java/org/elasticsearch/launcher/CliToolLauncher.java
@@ -0,0 +1,79 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.launcher;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+import org.elasticsearch.cli.Terminal;
+import org.elasticsearch.core.SuppressForbidden;
+
+import java.util.Map;
+import java.util.Properties;
+import java.util.stream.Collectors;
+
+/**
+ * A unified main method for Elasticsearch tools.
+ *
+ * This class should only be called by the elasticsearch-cli script.
+ */
+class CliToolLauncher {
+ private static final String SCRIPT_PREFIX = "elasticsearch-";
+
+ /**
+ * Runs a CLI tool.
+ *
+ *
+ * The following system properties may be provided:
+ *
+ * cli.name
- the name of the tool to run
+ * cli.script
- the path to the script that is being run
+ * cli.libs
- optional, comma separated list of additional lib directories
+ * that should be loaded to find the tool
+ *
+ * One of either cli.name
or cli.script
must be provided.
+ *
+ * @param args args to the tool
+ * @throws Exception if the tool fails with an unknown error
+ */
+ public static void main(String[] args) throws Exception {
+ Map sysprops = getSystemProperties();
+ String toolname = getToolName(sysprops);
+ String libs = sysprops.getOrDefault("cli.libs", "");
+
+ Command command = CliToolProvider.load(toolname, libs).create();
+ exit(command.main(args, Terminal.DEFAULT));
+ }
+
+ // package private for tests
+ static String getToolName(Map sysprops) {
+ String toolname = sysprops.getOrDefault("cli.name", "");
+ if (toolname.isBlank()) {
+ String script = sysprops.get("cli.script");
+ int nameStart = script.lastIndexOf(SCRIPT_PREFIX) + SCRIPT_PREFIX.length();
+ toolname = script.substring(nameStart);
+
+ if (sysprops.get("os.name").startsWith("Windows")) {
+ int dotIndex = toolname.indexOf(".bat"); // strip off .bat
+ toolname = toolname.substring(0, dotIndex);
+ }
+ }
+ return toolname;
+ }
+
+ @SuppressForbidden(reason = "collect system properties")
+ private static Map getSystemProperties() {
+ Properties props = System.getProperties();
+ return props.entrySet().stream().collect(Collectors.toMap(e -> e.getKey().toString(), e -> e.getValue().toString()));
+ }
+
+ @SuppressForbidden(reason = "System#exit")
+ private static void exit(int status) {
+ System.exit(status);
+ }
+}
diff --git a/distribution/tools/cli-launcher/src/test/java/org/elasticsearch/launcher/CliToolLauncherTests.java b/distribution/tools/cli-launcher/src/test/java/org/elasticsearch/launcher/CliToolLauncherTests.java
new file mode 100644
index 000000000000..b3042a78f088
--- /dev/null
+++ b/distribution/tools/cli-launcher/src/test/java/org/elasticsearch/launcher/CliToolLauncherTests.java
@@ -0,0 +1,33 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.launcher;
+
+import org.elasticsearch.test.ESTestCase;
+
+import java.util.Map;
+
+import static org.elasticsearch.launcher.CliToolLauncher.getToolName;
+import static org.hamcrest.Matchers.equalTo;
+
+public class CliToolLauncherTests extends ESTestCase {
+
+ public void testCliNameSysprop() {
+ assertThat(getToolName(Map.of("cli.name", "mycli")), equalTo("mycli"));
+ }
+
+ public void testScriptNameSysprop() {
+ var sysprops = Map.of("cli.name", "", "cli.script", "/foo/bar/elasticsearch-mycli", "os.name", "Linux");
+ assertThat(getToolName(sysprops), equalTo("mycli"));
+ }
+
+ public void testScriptNameSyspropWindows() {
+ var sysprops = Map.of("cli.name", "", "cli.script", "C:\\foo\\bar\\elasticsearch-mycli.bat", "os.name", "Windows XP");
+ assertThat(getToolName(sysprops), equalTo("mycli"));
+ }
+}
diff --git a/distribution/tools/geoip-cli/src/main/java/org/elasticsearch/geoip/GeoIpCli.java b/distribution/tools/geoip-cli/src/main/java/org/elasticsearch/geoip/GeoIpCli.java
index 6eb3aa01fc71..6c6a2b0b03ca 100644
--- a/distribution/tools/geoip-cli/src/main/java/org/elasticsearch/geoip/GeoIpCli.java
+++ b/distribution/tools/geoip-cli/src/main/java/org/elasticsearch/geoip/GeoIpCli.java
@@ -154,8 +154,4 @@ public class GeoIpCli extends Command {
return buf;
}
-
- public static void main(String[] args) throws Exception {
- exit(new GeoIpCli().main(args, Terminal.DEFAULT));
- }
}
diff --git a/distribution/tools/geoip-cli/src/main/java/org/elasticsearch/geoip/GeoIpCliProvider.java b/distribution/tools/geoip-cli/src/main/java/org/elasticsearch/geoip/GeoIpCliProvider.java
new file mode 100644
index 000000000000..2b6b0559a289
--- /dev/null
+++ b/distribution/tools/geoip-cli/src/main/java/org/elasticsearch/geoip/GeoIpCliProvider.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.geoip;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class GeoIpCliProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "geoip";
+ }
+
+ @Override
+ public Command create() {
+ return new GeoIpCli();
+ }
+}
diff --git a/distribution/tools/geoip-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider b/distribution/tools/geoip-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
new file mode 100644
index 000000000000..bfbc25e9cad2
--- /dev/null
+++ b/distribution/tools/geoip-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
@@ -0,0 +1 @@
+org.elasticsearch.geoip.GeoIpCliProvider
diff --git a/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/KeyStoreCli.java b/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/KeyStoreCli.java
index 710531a1999a..81acf200d784 100644
--- a/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/KeyStoreCli.java
+++ b/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/KeyStoreCli.java
@@ -8,15 +8,14 @@
package org.elasticsearch.cli.keystore;
-import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.cli.LoggingAwareMultiCommand;
/**
* A cli tool for managing secrets in the elasticsearch keystore.
*/
-public class KeyStoreCli extends LoggingAwareMultiCommand {
+class KeyStoreCli extends LoggingAwareMultiCommand {
- private KeyStoreCli() {
+ KeyStoreCli() {
super("A tool for managing settings stored in the elasticsearch keystore");
subcommands.put("create", new CreateKeyStoreCommand());
subcommands.put("list", new ListKeyStoreCommand());
@@ -28,9 +27,4 @@ public class KeyStoreCli extends LoggingAwareMultiCommand {
subcommands.put("passwd", new ChangeKeyStorePasswordCommand());
subcommands.put("has-passwd", new HasPasswordKeyStoreCommand());
}
-
- public static void main(String[] args) throws Exception {
- exit(new KeyStoreCli().main(args, Terminal.DEFAULT));
- }
-
}
diff --git a/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/KeyStoreCliProvider.java b/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/KeyStoreCliProvider.java
new file mode 100644
index 000000000000..5d64bc21015a
--- /dev/null
+++ b/distribution/tools/keystore-cli/src/main/java/org/elasticsearch/cli/keystore/KeyStoreCliProvider.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.cli.keystore;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class KeyStoreCliProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "keystore";
+ }
+
+ @Override
+ public Command create() {
+ return new KeyStoreCli();
+ }
+}
diff --git a/distribution/tools/keystore-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider b/distribution/tools/keystore-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
new file mode 100644
index 000000000000..6d546090b563
--- /dev/null
+++ b/distribution/tools/keystore-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
@@ -0,0 +1 @@
+org.elasticsearch.cli.keystore.KeyStoreCliProvider
diff --git a/distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/cli/PluginCli.java b/distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/cli/PluginCli.java
index f5e5b6136a5b..77bc80b85135 100644
--- a/distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/cli/PluginCli.java
+++ b/distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/cli/PluginCli.java
@@ -9,7 +9,6 @@
package org.elasticsearch.plugins.cli;
import org.elasticsearch.cli.Command;
-import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.cli.LoggingAwareMultiCommand;
import org.elasticsearch.core.internal.io.IOUtils;
@@ -20,11 +19,11 @@ import java.util.Collections;
/**
* A cli tool for adding, removing and listing plugins for elasticsearch.
*/
-public class PluginCli extends LoggingAwareMultiCommand {
+class PluginCli extends LoggingAwareMultiCommand {
private final Collection commands;
- private PluginCli() {
+ PluginCli() {
super("A tool for managing installed elasticsearch plugins");
subcommands.put("list", new ListPluginsCommand());
subcommands.put("install", new InstallPluginCommand());
@@ -32,10 +31,6 @@ public class PluginCli extends LoggingAwareMultiCommand {
commands = Collections.unmodifiableCollection(subcommands.values());
}
- public static void main(String[] args) throws Exception {
- exit(new PluginCli().main(args, Terminal.DEFAULT));
- }
-
@Override
public void close() throws IOException {
IOUtils.close(commands);
diff --git a/distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/cli/PluginCliProvider.java b/distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/cli/PluginCliProvider.java
new file mode 100644
index 000000000000..de87d1a5d086
--- /dev/null
+++ b/distribution/tools/plugin-cli/src/main/java/org/elasticsearch/plugins/cli/PluginCliProvider.java
@@ -0,0 +1,25 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.plugins.cli;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class PluginCliProvider implements CliToolProvider {
+
+ @Override
+ public String name() {
+ return "plugin";
+ }
+
+ @Override
+ public Command create() {
+ return new PluginCli();
+ }
+}
diff --git a/distribution/tools/plugin-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider b/distribution/tools/plugin-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
new file mode 100644
index 000000000000..473a3cb9456d
--- /dev/null
+++ b/distribution/tools/plugin-cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
@@ -0,0 +1 @@
+org.elasticsearch.plugins.cli.PluginCliProvider
diff --git a/libs/cli/src/main/java/org/elasticsearch/cli/CliToolProvider.java b/libs/cli/src/main/java/org/elasticsearch/cli/CliToolProvider.java
new file mode 100644
index 000000000000..d0523fadb0e2
--- /dev/null
+++ b/libs/cli/src/main/java/org/elasticsearch/cli/CliToolProvider.java
@@ -0,0 +1,91 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.cli;
+
+import java.io.IOException;
+import java.io.UncheckedIOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.ServiceLoader;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import java.util.stream.StreamSupport;
+
+/**
+ * An interface for command line tools to be instantiated and run.
+ */
+public interface CliToolProvider {
+
+ /**
+ * Returns the name of the tool provider.
+ * This must be unique across all tool providers.
+ */
+ String name();
+
+ /**
+ * Constructs the CLI instance to be run.
+ */
+ Command create();
+
+ /**
+ * Loads a tool provider from the Elasticsearch distribution.
+ *
+ * @param toolname the name of the tool to load
+ * @param libs the library directories to load, relative to the Elasticsearch homedir
+ * @return the instance of the loaded tool
+ * @throws AssertionError if the given toolname cannot be found or there are more than one tools found with the same name
+ */
+ static CliToolProvider load(String toolname, String libs) {
+ // the ES homedir is always our working dir
+ Path homeDir = Paths.get("").toAbsolutePath();
+ final ClassLoader cliLoader;
+ if (libs.isBlank()) {
+ cliLoader = ClassLoader.getSystemClassLoader();
+ } else {
+ List libsToLoad = Stream.of(libs.split(",")).map(homeDir::resolve).toList();
+ cliLoader = loadJars(libsToLoad);
+ }
+
+ ServiceLoader toolFinder = ServiceLoader.load(CliToolProvider.class, cliLoader);
+ List tools = StreamSupport.stream(toolFinder.spliterator(), false).filter(p -> p.name().equals(toolname)).toList();
+ if (tools.size() > 1) {
+ String names = tools.stream().map(t -> t.getClass().getName()).collect(Collectors.joining(", "));
+ throw new AssertionError("Multiple ToolProviders found with name [" + toolname + "]: " + names);
+ }
+ if (tools.size() == 0) {
+ var names = StreamSupport.stream(toolFinder.spliterator(), false).map(CliToolProvider::name).toList();
+ throw new AssertionError("CliToolProvider [" + toolname + "] not found, available names are " + names);
+ }
+ return tools.get(0);
+ }
+
+ private static ClassLoader loadJars(List dirs) {
+ final List urls = new ArrayList<>();
+ for (var dir : dirs) {
+ try (Stream jarFiles = Files.list(dir)) {
+ jarFiles.filter(p -> p.getFileName().toString().endsWith(".jar")).map(p -> {
+ try {
+ return p.toUri().toURL();
+ } catch (MalformedURLException e) {
+ throw new AssertionError(e);
+ }
+ }).forEach(urls::add);
+ } catch (IOException e) {
+ throw new UncheckedIOException(e);
+ }
+ }
+ return URLClassLoader.newInstance(urls.toArray(URL[]::new));
+ }
+}
diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/test/DockerTests.java b/qa/os/src/test/java/org/elasticsearch/packaging/test/DockerTests.java
index cddc069d4279..92cf181cec90 100644
--- a/qa/os/src/test/java/org/elasticsearch/packaging/test/DockerTests.java
+++ b/qa/os/src/test/java/org/elasticsearch/packaging/test/DockerTests.java
@@ -202,7 +202,10 @@ public class DockerTests extends PackagingTestCase {
// Stuff the proxy settings with garbage, so any attempt to go out to the internet would fail
sh.getEnv()
- .put("ES_JAVA_OPTS", "-Dhttp.proxyHost=example.org -Dhttp.proxyPort=9999 -Dhttps.proxyHost=example.org -Dhttps.proxyPort=9999");
+ .put(
+ "CLI_JAVA_OPTS",
+ "-Dhttp.proxyHost=example.org -Dhttp.proxyPort=9999 -Dhttps.proxyHost=example.org -Dhttps.proxyPort=9999"
+ );
sh.run(bin.pluginTool + " install --batch analysis-icu");
assertThat("Expected " + plugin + " to be installed", listPlugins(), hasItems(plugin));
@@ -228,7 +231,7 @@ public class DockerTests extends PackagingTestCase {
.volume(Path.of(EXAMPLE_PLUGIN_PATH), "/analysis-icu.zip")
.envVar("ELASTIC_PASSWORD", PASSWORD)
.envVar(
- "ES_JAVA_OPTS",
+ "CLI_JAVA_OPTS",
"-Dhttp.proxyHost=example.org -Dhttp.proxyPort=9999 -Dhttps.proxyHost=example.org -Dhttps.proxyPort=9999"
)
);
@@ -260,7 +263,7 @@ public class DockerTests extends PackagingTestCase {
builder().volume(tempDir.resolve(filename), installation.config.resolve(filename))
.envVar("ELASTIC_PASSWORD", PASSWORD)
.envVar(
- "ES_JAVA_OPTS",
+ "CLI_JAVA_OPTS",
"-Dhttp.proxyHost=example.org -Dhttp.proxyPort=9999 -Dhttps.proxyHost=example.org -Dhttps.proxyPort=9999"
)
);
@@ -298,7 +301,7 @@ public class DockerTests extends PackagingTestCase {
.extraArgs("--link " + mockServer.getContainerId() + ":mockserver");
if (useConfigFile == false) {
- builder.envVar("ES_JAVA_OPTS", "-Dhttp.proxyHost=mockserver -Dhttp.proxyPort=" + mockServer.getPort());
+ builder.envVar("CLI_JAVA_OPTS", "-Dhttp.proxyHost=mockserver -Dhttp.proxyPort=" + mockServer.getPort());
}
// Restart the container. This will sync plugins automatically, which will fail because
@@ -355,7 +358,7 @@ public class DockerTests extends PackagingTestCase {
builder().volume(tempDir.resolve(filename), installation.config.resolve(filename))
.envVar("ELASTIC_PASSWORD", PASSWORD)
.envVar(
- "ES_JAVA_OPTS",
+ "CLI_JAVA_OPTS",
"-Dhttp.proxyHost=example.org -Dhttp.proxyPort=9999 -Dhttps.proxyHost=example.org -Dhttps.proxyPort=9999"
)
);
diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/test/PluginCliTests.java b/qa/os/src/test/java/org/elasticsearch/packaging/test/PluginCliTests.java
index ff67733a7633..0aff5feed9dd 100644
--- a/qa/os/src/test/java/org/elasticsearch/packaging/test/PluginCliTests.java
+++ b/qa/os/src/test/java/org/elasticsearch/packaging/test/PluginCliTests.java
@@ -111,7 +111,7 @@ public class PluginCliTests extends PackagingTestCase {
}
public void test24JavaOpts() throws Exception {
- sh.getEnv().put("ES_JAVA_OPTS", "-XX:+PrintFlagsFinal");
+ sh.getEnv().put("CLI_JAVA_OPTS", "-XX:+PrintFlagsFinal");
assertWithExamplePlugin(installResult -> assertThat(installResult.stdout(), containsString("MaxHeapSize")));
}
diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/util/Archives.java b/qa/os/src/test/java/org/elasticsearch/packaging/util/Archives.java
index 11854ccfe517..c1f9118639b9 100644
--- a/qa/os/src/test/java/org/elasticsearch/packaging/util/Archives.java
+++ b/qa/os/src/test/java/org/elasticsearch/packaging/util/Archives.java
@@ -211,10 +211,7 @@ public class Archives {
"elasticsearch-sql-cli",
"elasticsearch-syskeygen",
"elasticsearch-users",
- "elasticsearch-service-tokens",
- "x-pack-env",
- "x-pack-security-env",
- "x-pack-watcher-env"
+ "elasticsearch-service-tokens"
).forEach(executable -> {
assertThat(es.bin(executable), file(File, owner, owner, p755));
diff --git a/qa/os/src/test/java/org/elasticsearch/packaging/util/Packages.java b/qa/os/src/test/java/org/elasticsearch/packaging/util/Packages.java
index 6d9cba73aa8f..e42cc67f41ad 100644
--- a/qa/os/src/test/java/org/elasticsearch/packaging/util/Packages.java
+++ b/qa/os/src/test/java/org/elasticsearch/packaging/util/Packages.java
@@ -246,10 +246,7 @@ public class Packages {
"elasticsearch-sql-cli",
"elasticsearch-syskeygen",
"elasticsearch-users",
- "elasticsearch-service-tokens",
- "x-pack-env",
- "x-pack-security-env",
- "x-pack-watcher-env"
+ "elasticsearch-service-tokens"
).forEach(executable -> assertThat(es.bin(executable), file(File, "root", "root", p755)));
// at this time we only install the current version of archive distributions, but if that changes we'll need to pass
diff --git a/server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCli.java b/server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCli.java
index 89b2fde4ab38..31aa49018aad 100644
--- a/server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCli.java
+++ b/server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCli.java
@@ -8,7 +8,6 @@
package org.elasticsearch.cluster.coordination;
import org.elasticsearch.cli.MultiCommand;
-import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.cli.CommandLoggingConfigurator;
import org.elasticsearch.env.NodeRepurposeCommand;
import org.elasticsearch.env.OverrideNodeVersionCommand;
@@ -20,9 +19,9 @@ import org.elasticsearch.env.OverrideNodeVersionCommand;
// Even if we avoid making a static reference to Logger class, there is no nice way to avoid declaring
// UNSAFE_BOOTSTRAP, which depends on ClusterService, which in turn has static Logger.
// TODO execute CommandLoggingConfigurator.configureLoggingWithoutConfig() in the constructor of commands, not in beforeMain
-public class NodeToolCli extends MultiCommand {
+class NodeToolCli extends MultiCommand {
- public NodeToolCli() {
+ NodeToolCli() {
super("A CLI tool to do unsafe cluster and index manipulations on current node", () -> {});
CommandLoggingConfigurator.configureLoggingWithoutConfig();
subcommands.put("repurpose", new NodeRepurposeCommand());
@@ -32,9 +31,4 @@ public class NodeToolCli extends MultiCommand {
subcommands.put("remove-settings", new RemoveSettingsCommand());
subcommands.put("remove-customs", new RemoveCustomsCommand());
}
-
- public static void main(String[] args) throws Exception {
- exit(new NodeToolCli().main(args, Terminal.DEFAULT));
- }
-
}
diff --git a/server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCliProvider.java b/server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCliProvider.java
new file mode 100644
index 000000000000..fa3dca5bc44d
--- /dev/null
+++ b/server/src/main/java/org/elasticsearch/cluster/coordination/NodeToolCliProvider.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.cluster.coordination;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class NodeToolCliProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "node";
+ }
+
+ @Override
+ public Command create() {
+ return new NodeToolCli();
+ }
+}
diff --git a/server/src/main/java/org/elasticsearch/index/shard/ShardToolCli.java b/server/src/main/java/org/elasticsearch/index/shard/ShardToolCli.java
index 306db0025c6e..488077d11c31 100644
--- a/server/src/main/java/org/elasticsearch/index/shard/ShardToolCli.java
+++ b/server/src/main/java/org/elasticsearch/index/shard/ShardToolCli.java
@@ -7,21 +7,15 @@
*/
package org.elasticsearch.index.shard;
-import org.elasticsearch.cli.Terminal;
import org.elasticsearch.common.cli.LoggingAwareMultiCommand;
/**
* Class encapsulating and dispatching commands from the {@code elasticsearch-shard} command line tool
*/
-public class ShardToolCli extends LoggingAwareMultiCommand {
+class ShardToolCli extends LoggingAwareMultiCommand {
- private ShardToolCli() {
+ ShardToolCli() {
super("A CLI tool to remove corrupted parts of unrecoverable shards");
subcommands.put("remove-corrupted-data", new RemoveCorruptedShardDataCommand());
}
-
- public static void main(String[] args) throws Exception {
- exit(new ShardToolCli().main(args, Terminal.DEFAULT));
- }
-
}
diff --git a/server/src/main/java/org/elasticsearch/index/shard/ShardToolCliProvider.java b/server/src/main/java/org/elasticsearch/index/shard/ShardToolCliProvider.java
new file mode 100644
index 000000000000..e530085ee016
--- /dev/null
+++ b/server/src/main/java/org/elasticsearch/index/shard/ShardToolCliProvider.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0 and the Server Side Public License, v 1; you may not use this file except
+ * in compliance with, at your election, the Elastic License 2.0 or the Server
+ * Side Public License, v 1.
+ */
+
+package org.elasticsearch.index.shard;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class ShardToolCliProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "shard";
+ }
+
+ @Override
+ public Command create() {
+ return new ShardToolCli();
+ }
+}
diff --git a/server/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider b/server/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
new file mode 100644
index 000000000000..fbc978252dae
--- /dev/null
+++ b/server/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
@@ -0,0 +1,2 @@
+org.elasticsearch.cluster.coordination.NodeToolCliProvider
+org.elasticsearch.index.shard.ShardToolCliProvider
diff --git a/settings.gradle b/settings.gradle
index f28f7ec51eb5..1e428828d5fa 100644
--- a/settings.gradle
+++ b/settings.gradle
@@ -53,6 +53,7 @@ List projects = [
'distribution:bwc:minor',
'distribution:bwc:staged',
'distribution:tools:java-version-checker',
+ 'distribution:tools:cli-launcher',
'distribution:tools:launchers',
'distribution:tools:plugin-cli',
'distribution:tools:keystore-cli',
diff --git a/x-pack/plugin/core/src/main/bin/x-pack-env b/x-pack/plugin/core/src/main/bin/x-pack-env
deleted file mode 100644
index 1500d00a1b9b..000000000000
--- a/x-pack/plugin/core/src/main/bin/x-pack-env
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/bash
-
-# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
-# or more contributor license agreements. Licensed under the Elastic License
-# 2.0; you may not use this file except in compliance with the Elastic License
-# 2.0.
-
-# include x-pack-core jars in classpath
-ES_CLASSPATH="$ES_CLASSPATH:$ES_HOME/modules/x-pack-core/*"
diff --git a/x-pack/plugin/core/src/main/bin/x-pack-env.bat b/x-pack/plugin/core/src/main/bin/x-pack-env.bat
deleted file mode 100644
index f6b5f8848d24..000000000000
--- a/x-pack/plugin/core/src/main/bin/x-pack-env.bat
+++ /dev/null
@@ -1,6 +0,0 @@
-rem Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
-rem or more contributor license agreements. Licensed under the Elastic License
-rem 2.0; you may not use this file except in compliance with the Elastic License
-rem 2.0.
-
-set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/modules/x-pack-core/*
diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java
index a6b8f2d2adef..a3c1d887ab9c 100644
--- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java
+++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNode.java
@@ -138,29 +138,25 @@ public class AutoConfigureNode extends EnvironmentAwareCommand {
private final OptionSpec enrollmentTokenParam = parser.accepts("enrollment-token", "The enrollment token to use")
.withRequiredArg();
- private final OptionSpec reconfigure = parser.accepts("reconfigure");
+ private final boolean inReconfigureMode;
private final BiFunction clientFunction;
- public AutoConfigureNode(BiFunction clientFunction) {
+ public AutoConfigureNode(boolean reconfigure, BiFunction clientFunction) {
super("Generates all the necessary security configuration for a node in a secured cluster");
// This "cli utility" is invoked from the node startup script, where it is passed all the
// node startup options unfiltered. It cannot consume most of them, but it does need to inspect the `-E` ones.
parser.allowsUnrecognizedOptions();
+ this.inReconfigureMode = reconfigure;
this.clientFunction = clientFunction;
}
- public AutoConfigureNode() {
- this(CommandLineHttpClient::new);
- }
-
- public static void main(String[] args) throws Exception {
- exit(new AutoConfigureNode().main(args, Terminal.DEFAULT));
+ public AutoConfigureNode(boolean reconfigure) {
+ this(reconfigure, CommandLineHttpClient::new);
}
@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
final boolean inEnrollmentMode = options.has(enrollmentTokenParam);
- final boolean inReconfigureMode = options.has(reconfigure);
// skipping security auto-configuration because node considered as restarting.
for (Path dataPath : env.dataFiles()) {
diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNodeToolProvider.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNodeToolProvider.java
new file mode 100644
index 000000000000..0530c8074f77
--- /dev/null
+++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/AutoConfigureNodeToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.cli;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class AutoConfigureNodeToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "auto-configure-node";
+ }
+
+ @Override
+ public Command create() {
+ return new AutoConfigureNode(false);
+ }
+}
diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java
index d51d2032617f..b8eb87ba2117 100644
--- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java
+++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateTool.java
@@ -75,7 +75,7 @@ import javax.security.auth.x500.X500Principal;
* @deprecated Replaced by {@link CertificateTool}
*/
@Deprecated
-public class CertificateGenerateTool extends EnvironmentAwareCommand {
+class CertificateGenerateTool extends EnvironmentAwareCommand {
private static final String AUTO_GEN_CA_DN = "CN=Elastic Certificate Tool Autogenerated CA";
private static final String DESCRIPTION = "Simplifies certificate creation for use with the Elastic Stack";
@@ -159,10 +159,6 @@ public class CertificateGenerateTool extends EnvironmentAwareCommand {
.withOptionalArg();
}
- public static void main(String[] args) throws Exception {
- exit(new CertificateGenerateTool().main(args, Terminal.DEFAULT));
- }
-
@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
final boolean csrOnly = options.has(csrSpec);
diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolProvider.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolProvider.java
new file mode 100644
index 000000000000..70865cb69f0d
--- /dev/null
+++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateGenerateToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.cli;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class CertificateGenerateToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "certgen";
+ }
+
+ @Override
+ public Command create() {
+ return new CertificateGenerateTool();
+ }
+}
diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java
index c6d6f71b2569..60c89c38ac88 100644
--- a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java
+++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateTool.java
@@ -84,7 +84,7 @@ import javax.security.auth.x500.X500Principal;
/**
* CLI tool to make generation of certificates or certificate requests easier for users
*/
-public class CertificateTool extends LoggingAwareMultiCommand {
+class CertificateTool extends LoggingAwareMultiCommand {
private static final String AUTO_GEN_CA_DN = "CN=Elastic Certificate Tool Autogenerated CA";
private static final String DESCRIPTION = "Simplifies certificate creation for use with the Elastic Stack";
@@ -145,10 +145,6 @@ public class CertificateTool extends LoggingAwareMultiCommand {
}
}
- public static void main(String[] args) throws Exception {
- exit(new CertificateTool().main(args, Terminal.DEFAULT));
- }
-
CertificateTool() {
super(DESCRIPTION);
subcommands.put("csr", new SigningRequestCommand());
diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateToolProvider.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateToolProvider.java
new file mode 100644
index 000000000000..38ceef4dfdf1
--- /dev/null
+++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/CertificateToolProvider.java
@@ -0,0 +1,24 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.cli;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class CertificateToolProvider implements CliToolProvider {
+
+ @Override
+ public String name() {
+ return "certutil";
+ }
+
+ @Override
+ public Command create() {
+ return new CertificateTool();
+ }
+}
diff --git a/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/ReconfigureNodeToolProvider.java b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/ReconfigureNodeToolProvider.java
new file mode 100644
index 000000000000..30cb8126bb62
--- /dev/null
+++ b/x-pack/plugin/security/cli/src/main/java/org/elasticsearch/xpack/security/cli/ReconfigureNodeToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.cli;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class ReconfigureNodeToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "reconfigure-node";
+ }
+
+ @Override
+ public Command create() {
+ return new AutoConfigureNode(true);
+ }
+}
diff --git a/x-pack/plugin/security/cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider b/x-pack/plugin/security/cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
new file mode 100644
index 000000000000..087e4da126a1
--- /dev/null
+++ b/x-pack/plugin/security/cli/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
@@ -0,0 +1,4 @@
+org.elasticsearch.xpack.security.cli.CertificateToolProvider
+org.elasticsearch.xpack.security.cli.CertificateGenerateToolProvider
+org.elasticsearch.xpack.security.cli.ReconfigureNodeToolProvider
+org.elasticsearch.xpack.security.cli.AutoConfigureNodeToolProvider
diff --git a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/AutoConfigureNodeTests.java b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/AutoConfigureNodeTests.java
index 8f3c1c7f6bce..06848d3d555f 100644
--- a/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/AutoConfigureNodeTests.java
+++ b/x-pack/plugin/security/cli/src/test/java/org/elasticsearch/xpack/security/cli/AutoConfigureNodeTests.java
@@ -262,7 +262,7 @@ public class AutoConfigureNodeTests extends ESTestCase {
private X509Certificate runAutoConfigAndReturnHTTPCertificate(Path configDir, Settings settings) throws Exception {
final Environment env = TestEnvironment.newEnvironment(Settings.builder().put("path.home", configDir).put(settings).build());
// runs the command to auto-generate the config files and the keystore
- new AutoConfigureNode().execute(new MockTerminal(), new OptionParser().parse(), env);
+ new AutoConfigureNode(false).execute(new MockTerminal(), new OptionParser().parse(), env);
KeyStoreWrapper nodeKeystore = KeyStoreWrapper.load(configDir.resolve("config"));
nodeKeystore.decrypt(new char[0]); // the keystore is always bootstrapped with an empty password
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-certgen b/x-pack/plugin/security/src/main/bin/elasticsearch-certgen
index a99515c1e847..fc5ee22e2eec 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-certgen
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-certgen
@@ -5,8 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.CertificateGenerateTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli"
+source "`dirname "$0"`"/elasticsearch-cli
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 fd851fd21ebf..e27333c6dff3 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-certgen.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-certgen.bat
@@ -8,9 +8,8 @@ rem 2.0.
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.CertificateGenerateTool
-set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-certutil b/x-pack/plugin/security/src/main/bin/elasticsearch-certutil
index 4e1e19462687..fc5ee22e2eec 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-certutil
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-certutil
@@ -5,8 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.CertificateTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli"
+source "`dirname "$0"`"/elasticsearch-cli
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 16740fe455ae..e27333c6dff3 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-certutil.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-certutil.bat
@@ -8,9 +8,8 @@ rem 2.0.
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.CertificateTool
-set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token b/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token
index ce17ab6022c6..d4d864b85da4 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.enrollment.tool.CreateEnrollmentTokenTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security"
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token.bat
index 39027ef69772..8fae41144d82 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-create-enrollment-token.bat
@@ -8,9 +8,8 @@ rem 2.0.
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.xpack.security.enrollment.tool.CreateEnrollmentTokenTool
-set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node b/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node
index 5204adf7e5b6..fc5ee22e2eec 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node
@@ -5,8 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
- "`dirname "$0"`"/elasticsearch-cli \
- --reconfigure "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli"
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node.bat
index 386812a663be..e27333c6dff3 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-reconfigure-node.bat
@@ -8,10 +8,9 @@ rem 2.0.
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigureNode
-set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli
-call "%~dp0elasticsearch-cli.bat" "--reconfigure" ^
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security,lib/tools/security-cli
+call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password b/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password
index 6f50bc54440d..d4d864b85da4 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.tool.ResetPasswordTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security"
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password.bat
index ec081b6548db..8fae41144d82 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-reset-password.bat
@@ -8,9 +8,8 @@ rem 2.0.
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.tool.ResetPasswordTool
-set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
-set ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata b/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata
index a09bd6be3b22..d4d864b85da4 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-saml-metadata
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.saml.SamlMetadataCommand \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security"
+source "`dirname "$0"`"/elasticsearch-cli
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 0143b1b53c50..8fae41144d82 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
@@ -8,8 +8,8 @@ rem 2.0.
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
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-security-config.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-security-config.bat
deleted file mode 100644
index e69de29bb2d1..000000000000
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens b/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens
index c9cdbd78bc8f..d4d864b85da4 100755
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.service.FileTokensTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security"
+source "`dirname "$0"`"/elasticsearch-cli
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens.bat b/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens.bat
index 6ca1260c2a6a..8fae41144d82 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-service-tokens.bat
@@ -8,8 +8,8 @@ rem 2.0.
setlocal enabledelayedexpansion
setlocal enableextensions
-set ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.service.FileTokensTool
-set ES_ADDITIONAL_SOURCES=x-pack-env;x-pack-security-env
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords b/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords
index 16b482389298..d4d864b85da4 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-setup-passwords
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security"
+source "`dirname "$0"`"/elasticsearch-cli
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 54641bc49ba7..8fae41144d82 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
@@ -8,8 +8,8 @@ rem 2.0.
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
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen b/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen
index 9e0c3ccf5d19..d4d864b85da4 100755
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.crypto.tool.SystemKeyTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security"
+source "`dirname "$0"`"/elasticsearch-cli
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 dceff2294c4a..8fae41144d82 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-syskeygen.bat
@@ -8,8 +8,8 @@ rem 2.0.
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
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/elasticsearch-users b/x-pack/plugin/security/src/main/bin/elasticsearch-users
index bf4a4b339523..d4d864b85da4 100755
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-users
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-users
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.security.authc.file.tool.UsersTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-security"
+source "`dirname "$0"`"/elasticsearch-cli
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 e2e553ba41c2..8fae41144d82 100644
--- a/x-pack/plugin/security/src/main/bin/elasticsearch-users.bat
+++ b/x-pack/plugin/security/src/main/bin/elasticsearch-users.bat
@@ -8,8 +8,8 @@ rem 2.0.
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
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-security
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/security/src/main/bin/x-pack-security-env b/x-pack/plugin/security/src/main/bin/x-pack-security-env
deleted file mode 100644
index 37d7f569a469..000000000000
--- a/x-pack/plugin/security/src/main/bin/x-pack-security-env
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/bash
-
-# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
-# or more contributor license agreements. Licensed under the Elastic License
-# 2.0; you may not use this file except in compliance with the Elastic License
-# 2.0.
-
-# include x-pack-security jars in classpath
-ES_CLASSPATH="$ES_CLASSPATH:$ES_HOME/modules/x-pack-security/*"
diff --git a/x-pack/plugin/security/src/main/bin/x-pack-security-env.bat b/x-pack/plugin/security/src/main/bin/x-pack-security-env.bat
deleted file mode 100644
index 1fbfe656da7e..000000000000
--- a/x-pack/plugin/security/src/main/bin/x-pack-security-env.bat
+++ /dev/null
@@ -1,6 +0,0 @@
-rem Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
-rem or more contributor license agreements. Licensed under the Elastic License
-rem 2.0; you may not use this file except in compliance with the Elastic License
-rem 2.0.
-
-set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/modules/x-pack-security/*
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java
index f52d8f8add03..b4e766fabf04 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordTool.java
@@ -34,7 +34,7 @@ import java.util.function.Function;
import static org.elasticsearch.xpack.core.security.CommandLineHttpClient.createURL;
import static org.elasticsearch.xpack.security.tool.CommandUtils.generatePassword;
-public class ResetPasswordTool extends BaseRunAsSuperuserCommand {
+class ResetPasswordTool extends BaseRunAsSuperuserCommand {
private final Function clientFunction;
private final OptionSpecBuilder interactive;
@@ -42,14 +42,10 @@ public class ResetPasswordTool extends BaseRunAsSuperuserCommand {
private final OptionSpecBuilder batch;
private final OptionSpec usernameOption;
- public ResetPasswordTool() {
+ ResetPasswordTool() {
this(CommandLineHttpClient::new, environment -> KeyStoreWrapper.load(environment.configFile()));
}
- public static void main(String[] args) throws Exception {
- exit(new ResetPasswordTool().main(args, Terminal.DEFAULT));
- }
-
protected ResetPasswordTool(
Function clientFunction,
CheckedFunction keyStoreFunction
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolProvider.java
new file mode 100644
index 000000000000..4df5bf85da79
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/ResetPasswordToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.authc.esnative.tool;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class ResetPasswordToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "reset-password";
+ }
+
+ @Override
+ public Command create() {
+ return new ResetPasswordTool();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java
index 566ac5d5389d..06ec37daac41 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordTool.java
@@ -72,7 +72,7 @@ import static java.util.Arrays.asList;
* elastic user and the ChangePassword API for setting the password of the rest of the built-in users when needed.
*/
@Deprecated
-public class SetupPasswordTool extends LoggingAwareMultiCommand {
+class SetupPasswordTool extends LoggingAwareMultiCommand {
private static final char[] CHARS = ("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789").toCharArray();
public static final List USERS = asList(
@@ -124,10 +124,6 @@ public class SetupPasswordTool extends LoggingAwareMultiCommand {
return new InteractiveSetup();
}
- public static void main(String[] args) throws Exception {
- exit(new SetupPasswordTool().main(args, Terminal.DEFAULT));
- }
-
// Visible for testing
OptionParser getParser() {
return this.parser;
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolProvider.java
new file mode 100644
index 000000000000..54abca2b900e
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/esnative/tool/SetupPasswordToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.authc.esnative.tool;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class SetupPasswordToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "setup-passwords";
+ }
+
+ @Override
+ public Command create() {
+ return new SetupPasswordTool();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java
index 11a61894bde4..09f542a8b2cd 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersTool.java
@@ -43,11 +43,7 @@ import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
-public class UsersTool extends LoggingAwareMultiCommand {
-
- public static void main(String[] args) throws Exception {
- exit(new UsersTool().main(args, Terminal.DEFAULT));
- }
+class UsersTool extends LoggingAwareMultiCommand {
UsersTool() {
super("Manages elasticsearch file users");
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersToolProvider.java
new file mode 100644
index 000000000000..b13621ddf237
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/file/tool/UsersToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.authc.file.tool;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class UsersToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "users";
+ }
+
+ @Override
+ public Command create() {
+ return new UsersTool();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java
index 15ed84168ae9..9506fbce3408 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataCommand.java
@@ -69,7 +69,7 @@ import java.util.stream.Collectors;
/**
* CLI tool to generate SAML Metadata for a Service Provider (realm)
*/
-public class SamlMetadataCommand extends KeyStoreAwareCommand {
+class SamlMetadataCommand extends KeyStoreAwareCommand {
static final String METADATA_SCHEMA = "saml-schema-metadata-2.0.xsd";
@@ -90,18 +90,14 @@ public class SamlMetadataCommand extends KeyStoreAwareCommand {
private final CheckedFunction keyStoreFunction;
private KeyStoreWrapper keyStoreWrapper;
- public static void main(String[] args) throws Exception {
- exit(new SamlMetadataCommand().main(args, Terminal.DEFAULT));
- }
-
- public SamlMetadataCommand() {
+ SamlMetadataCommand() {
this((environment) -> {
KeyStoreWrapper ksWrapper = KeyStoreWrapper.load(environment.configFile());
return ksWrapper;
});
}
- public SamlMetadataCommand(CheckedFunction keyStoreFunction) {
+ SamlMetadataCommand(CheckedFunction keyStoreFunction) {
super("Generate Service Provider Metadata for a SAML realm");
outputPathSpec = parser.accepts("out", "path of the xml file that should be generated").withRequiredArg();
batchSpec = parser.accepts("batch", "Do not prompt");
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataToolProvider.java
new file mode 100644
index 000000000000..9d4a67635465
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/saml/SamlMetadataToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.authc.saml;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class SamlMetadataToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "saml-metadata";
+ }
+
+ @Override
+ public Command create() {
+ return new SamlMetadataCommand();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java
index 79e5f74c39b7..77d294b105a3 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensTool.java
@@ -31,13 +31,9 @@ import java.util.Map;
import java.util.TreeMap;
import java.util.function.Predicate;
-public class FileTokensTool extends LoggingAwareMultiCommand {
+class FileTokensTool extends LoggingAwareMultiCommand {
- public static void main(String[] args) throws Exception {
- exit(new FileTokensTool().main(args, Terminal.DEFAULT));
- }
-
- public FileTokensTool() {
+ FileTokensTool() {
super("Manages elasticsearch service account file-tokens");
subcommands.put("create", newCreateFileTokenCommand());
subcommands.put("delete", newDeleteFileTokenCommand());
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolProvider.java
new file mode 100644
index 000000000000..13303227cc22
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/authc/service/FileTokensToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.authc.service;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class FileTokensToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "service-tokens";
+ }
+
+ @Override
+ public Command create() {
+ return new FileTokensTool();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java
index 3da100aa82e1..8583a71e8715 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyTool.java
@@ -32,7 +32,7 @@ import java.util.Set;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
-public class SystemKeyTool extends EnvironmentAwareCommand {
+class SystemKeyTool extends EnvironmentAwareCommand {
static final String KEY_ALGO = "HmacSHA512";
static final int KEY_SIZE = 1024;
@@ -49,18 +49,6 @@ public class SystemKeyTool extends EnvironmentAwareCommand {
PosixFilePermission.OWNER_WRITE
);
- public static void main(String[] args) throws Exception {
- final SystemKeyTool tool = new SystemKeyTool();
- int status = main(tool, args, Terminal.DEFAULT);
- if (status != ExitCodes.OK) {
- exit(status);
- }
- }
-
- static int main(SystemKeyTool tool, String[] args, Terminal terminal) throws Exception {
- return tool.main(args, terminal);
- }
-
@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
final Path keyPath;
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyToolProvider.java
new file mode 100644
index 000000000000..634596f7598f
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/crypto/tool/SystemKeyToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.crypto.tool;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class SystemKeyToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "syskeygen";
+ }
+
+ @Override
+ public Command create() {
+ return new SystemKeyTool();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHash.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHash.java
index cfe126205788..534987ac25d8 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHash.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHash.java
@@ -36,16 +36,12 @@ import static org.elasticsearch.xpack.security.tool.CommandUtils.generatePasswor
*
* The generated password is written to stdout upon success. Error messages are printed to stderr.
*/
-public class AutoConfigGenerateElasticPasswordHash extends KeyStoreAwareCommand {
+class AutoConfigGenerateElasticPasswordHash extends KeyStoreAwareCommand {
- public AutoConfigGenerateElasticPasswordHash() {
+ AutoConfigGenerateElasticPasswordHash() {
super("Generates a password hash for for the elastic user and stores it in elasticsearch.keystore");
}
- public static void main(String[] args) throws Exception {
- exit(new AutoConfigGenerateElasticPasswordHash().main(args, Terminal.DEFAULT));
- }
-
@Override
protected void execute(Terminal terminal, OptionSet options, Environment env) throws Exception {
final Hasher hasher = Hasher.resolve(XPackSettings.PASSWORD_HASHING_ALGORITHM.get(env.settings()));
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHashToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHashToolProvider.java
new file mode 100644
index 000000000000..33e9332817c8
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/AutoConfigGenerateElasticPasswordHashToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.enrollment.tool;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class AutoConfigGenerateElasticPasswordHashToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "auto-config-gen-passwd";
+ }
+
+ @Override
+ public Command create() {
+ return new AutoConfigGenerateElasticPasswordHash();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java
index 954badc86e47..919f4531734f 100644
--- a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenTool.java
@@ -26,7 +26,7 @@ import java.net.URL;
import java.util.List;
import java.util.function.Function;
-public class CreateEnrollmentTokenTool extends BaseRunAsSuperuserCommand {
+class CreateEnrollmentTokenTool extends BaseRunAsSuperuserCommand {
private final OptionSpec scope;
private final Function clientFunction;
@@ -34,7 +34,6 @@ public class CreateEnrollmentTokenTool extends BaseRunAsSuperuserCommand {
static final List ALLOWED_SCOPES = List.of("node", "kibana");
CreateEnrollmentTokenTool() {
-
this(
environment -> new CommandLineHttpClient(environment),
environment -> KeyStoreWrapper.load(environment.configFile()),
@@ -55,10 +54,6 @@ public class CreateEnrollmentTokenTool extends BaseRunAsSuperuserCommand {
.required();
}
- public static void main(String[] args) throws Exception {
- exit(new CreateEnrollmentTokenTool().main(args, Terminal.DEFAULT));
- }
-
@Override
protected void validate(Terminal terminal, OptionSet options, Environment env) throws Exception {
if (XPackSettings.ENROLLMENT_ENABLED.get(env.settings()) == false) {
diff --git a/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenToolProvider.java b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenToolProvider.java
new file mode 100644
index 000000000000..1c5cee5daf72
--- /dev/null
+++ b/x-pack/plugin/security/src/main/java/org/elasticsearch/xpack/security/enrollment/tool/CreateEnrollmentTokenToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.security.enrollment.tool;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class CreateEnrollmentTokenToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "create-enrollment-token";
+ }
+
+ @Override
+ public Command create() {
+ return new CreateEnrollmentTokenTool();
+ }
+}
diff --git a/x-pack/plugin/security/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider b/x-pack/plugin/security/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
new file mode 100644
index 000000000000..1ff00bb7edc9
--- /dev/null
+++ b/x-pack/plugin/security/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
@@ -0,0 +1,8 @@
+org.elasticsearch.xpack.security.enrollment.tool.CreateEnrollmentTokenToolProvider
+org.elasticsearch.xpack.security.authc.esnative.tool.ResetPasswordToolProvider
+org.elasticsearch.xpack.security.authc.esnative.tool.SetupPasswordToolProvider
+org.elasticsearch.xpack.security.authc.saml.SamlMetadataToolProvider
+org.elasticsearch.xpack.security.authc.service.FileTokensToolProvider
+org.elasticsearch.xpack.security.crypto.tool.SystemKeyToolProvider
+org.elasticsearch.xpack.security.authc.file.tool.UsersToolProvider
+org.elasticsearch.xpack.security.enrollment.tool.AutoConfigGenerateElasticPasswordHashToolProvider
diff --git a/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli b/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli
index 5942934283b4..0e192f8a5d94 100755
--- a/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli
+++ b/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli
@@ -9,8 +9,6 @@ CLI_PARAMETERS="$@"
source "`dirname "$0"`"/elasticsearch-env
-source "$ES_HOME"/bin/x-pack-env
-
CLI_JAR=$(ls "$ES_HOME"/bin/elasticsearch-sql-cli-*.jar)
exec \
diff --git a/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli.bat b/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli.bat
index 53a87c2b371a..f1a3237045eb 100644
--- a/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli.bat
+++ b/x-pack/plugin/sql/src/main/bin/elasticsearch-sql-cli.bat
@@ -10,9 +10,7 @@ setlocal enableextensions
call "%~dp0elasticsearch-env.bat" || exit /b 1
-call "%ES_HOME%/bin/x-pack-env.bat" || exit /b 1
-
-set CLI_JAR=%ES_HOME%/bin/*
+for %%a in (!ES_HOME!\bin\elasticsearch-sql-cli-*.jar) do set CLI_JAR=%%a
%JAVA% ^
-cp "%CLI_JAR%" ^
diff --git a/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval b/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval
index c2e7195428e1..7758c507dc65 100755
--- a/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval
+++ b/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval
@@ -5,7 +5,5 @@
# 2.0; you may not use this file except in compliance with the Elastic License
# 2.0.
-ES_MAIN_CLASS=org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalTool \
- ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-watcher-env" \
- "`dirname "$0"`"/elasticsearch-cli \
- "$@"
+CLI_LIBS="modules/x-pack-core,modules/x-pack-watcher"
+source "`dirname "$0"`"/elasticsearch-cli
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 bbc8f8d03e82..006fd00ec5ee 100644
--- a/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval.bat
+++ b/x-pack/plugin/watcher/src/main/bin/elasticsearch-croneval.bat
@@ -8,8 +8,8 @@ rem 2.0.
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
+set CLI_SCRIPT=%~0
+set CLI_LIBS=modules/x-pack-core,modules/x-pack-watcher
call "%~dp0elasticsearch-cli.bat" ^
%%* ^
|| goto exit
diff --git a/x-pack/plugin/watcher/src/main/bin/x-pack-watcher-env b/x-pack/plugin/watcher/src/main/bin/x-pack-watcher-env
deleted file mode 100644
index 9b8ad2ef7bab..000000000000
--- a/x-pack/plugin/watcher/src/main/bin/x-pack-watcher-env
+++ /dev/null
@@ -1,9 +0,0 @@
-#!/bin/bash
-
-# Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
-# or more contributor license agreements. Licensed under the Elastic License
-# 2.0; you may not use this file except in compliance with the Elastic License
-# 2.0.
-
-# include x-pack-security jars in classpath
-ES_CLASSPATH="$ES_CLASSPATH:$ES_HOME/modules/x-pack-watcher/*"
diff --git a/x-pack/plugin/watcher/src/main/bin/x-pack-watcher-env.bat b/x-pack/plugin/watcher/src/main/bin/x-pack-watcher-env.bat
deleted file mode 100644
index 9aece14f049b..000000000000
--- a/x-pack/plugin/watcher/src/main/bin/x-pack-watcher-env.bat
+++ /dev/null
@@ -1,6 +0,0 @@
-rem Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
-rem or more contributor license agreements. Licensed under the Elastic License
-rem 2.0; you may not use this file except in compliance with the Elastic License
-rem 2.0.
-
-set ES_CLASSPATH=!ES_CLASSPATH!;!ES_HOME!/modules/x-pack-watcher/*
diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java
index 45832d418ec9..87711de6bc2a 100644
--- a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java
+++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalTool.java
@@ -24,11 +24,7 @@ import java.util.Arrays;
import java.util.List;
import java.util.Locale;
-public class CronEvalTool extends LoggingAwareCommand {
-
- public static void main(String[] args) throws Exception {
- exit(new CronEvalTool().main(args, Terminal.DEFAULT));
- }
+class CronEvalTool extends LoggingAwareCommand {
private static final DateFormatter UTC_FORMATTER = DateFormatter.forPattern("EEE, d MMM yyyy HH:mm:ss")
.withZone(ZoneOffset.UTC)
diff --git a/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolProvider.java b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolProvider.java
new file mode 100644
index 000000000000..f19b10928261
--- /dev/null
+++ b/x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/trigger/schedule/tool/CronEvalToolProvider.java
@@ -0,0 +1,23 @@
+/*
+ * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
+ * or more contributor license agreements. Licensed under the Elastic License
+ * 2.0; you may not use this file except in compliance with the Elastic License
+ * 2.0.
+ */
+
+package org.elasticsearch.xpack.watcher.trigger.schedule.tool;
+
+import org.elasticsearch.cli.CliToolProvider;
+import org.elasticsearch.cli.Command;
+
+public class CronEvalToolProvider implements CliToolProvider {
+ @Override
+ public String name() {
+ return "croneval";
+ }
+
+ @Override
+ public Command create() {
+ return new CronEvalTool();
+ }
+}
diff --git a/x-pack/plugin/watcher/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider b/x-pack/plugin/watcher/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
new file mode 100644
index 000000000000..4ed62b4daa8e
--- /dev/null
+++ b/x-pack/plugin/watcher/src/main/resources/META-INF/services/org.elasticsearch.cli.CliToolProvider
@@ -0,0 +1 @@
+org.elasticsearch.xpack.watcher.trigger.schedule.tool.CronEvalToolProvider