-
Notifications
You must be signed in to change notification settings - Fork 25k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow node to enroll to cluster on startup #77718
Changes from all commits
aeec13d
4c17742
f9a14b8
226e965
a15b6bc
99afe27
ca0836c
068e45d
be92ca1
ba11466
5c0c1f0
062cb85
2f3c337
74ef40b
140d2c4
fb8686c
b480b4e
7941370
8ca65fa
2d8b395
f631474
1efbc06
0c1fee9
345329e
36fc590
fd14936
6dbb3fa
5dbfba3
2daabc5
caa5880
d6018d3
14b679a
881d001
1758026
026341d
c6f123e
bfbfee0
4222f7c
a3cae59
7331c71
2d04043
aebba35
3643d72
4198cc8
573add3
38623c6
0ced955
9efae09
48b7569
d0e9e6c
a3c9a21
18ed565
f09cba0
347e4b9
d8729ce
7693119
1138134
ee03466
0ca60d1
e00aa95
491dbf5
d7ba414
5859676
10fec73
631d22c
b635b96
eaeafe8
5b796df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,16 +18,29 @@ source "`dirname "$0"`"/elasticsearch-env | |
CHECK_KEYSTORE=true | ||
ATTEMPT_SECURITY_AUTO_CONFIG=true | ||
DAEMONIZE=false | ||
for option in "$@"; do | ||
case "$option" in | ||
-h|--help|-V|--version) | ||
CHECK_KEYSTORE=false | ||
ATTEMPT_SECURITY_AUTO_CONFIG=false | ||
;; | ||
-d|--daemonize) | ||
DAEMONIZE=true | ||
;; | ||
esac | ||
ENROLL_TO_CLUSTER=false | ||
# Store original arg array as we will be shifting through it below | ||
ARG_LIST=("$@") | ||
|
||
while [ $# -gt 0 ]; do | ||
if [[ $1 == "--enrollment-token" ]]; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Personally, I would have stuck with a |
||
if [ $ENROLL_TO_CLUSTER = true ]; then | ||
echo "Multiple --enrollment-token parameters are not allowed" 1>&2 | ||
exit 1 | ||
fi | ||
ENROLL_TO_CLUSTER=true | ||
ATTEMPT_SECURITY_AUTO_CONFIG=false | ||
ENROLLMENT_TOKEN="$2" | ||
jkakavas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
shift | ||
elif [[ $1 == "-h" || $1 == "--help" || $1 == "-V" || $1 == "--version" ]]; then | ||
CHECK_KEYSTORE=false | ||
ATTEMPT_SECURITY_AUTO_CONFIG=false | ||
elif [[ $1 == "-d" || $1 == "--daemonize" ]]; then | ||
DAEMONIZE=true | ||
fi | ||
if [[ $# -gt 0 ]]; then | ||
shift | ||
fi | ||
done | ||
|
||
if [ -z "$ES_TMPDIR" ]; then | ||
|
@@ -47,16 +60,21 @@ then | |
fi | ||
fi | ||
|
||
if [[ $ATTEMPT_SECURITY_AUTO_CONFIG = true ]]; then | ||
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 \ | ||
bin/elasticsearch-cli "${ARG_LIST[@]}" <<<"$KEYSTORE_PASSWORD" | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have shifted through $@ so we need to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't need to check the return value here. Any ExitCode should make us stop because we cannot satisfy an explicit request of the user ( to enroll this node to an existing cluster) . |
||
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). | ||
# Most likely an auto-conf failure will leave the configuration untouched (exit codes 73, 78 and 80), optionally printing a message | ||
# 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.ConfigInitialNode \ | ||
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 \ | ||
bin/elasticsearch-cli "$@" <<<"$KEYSTORE_PASSWORD"; then | ||
bin/elasticsearch-cli "${ARG_LIST[@]}" <<<"$KEYSTORE_PASSWORD"; then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we have shifted through $@ so we need to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Am I missing something or are these two scenarios identical? What's the difference between ENROLL_TO_CLUSTER being true and ATTEMPT_SECURITY_AUTO_CONFIG? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes this is slightly confusing. Both scenarios call the same class passing that arguments of the user, but :
|
||
: | ||
else | ||
retval=$? | ||
|
@@ -77,6 +95,13 @@ fi | |
# - fourth, ergonomic JVM options are applied | ||
ES_JAVA_OPTS=`export ES_TMPDIR; "$JAVA" "$XSHARE" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.JvmOptionsParser "$ES_PATH_CONF" "$ES_HOME/plugins"` | ||
|
||
# Remove enrollment related parameters before passing the arg list to Elasticsearch | ||
for i in "${!ARG_LIST[@]}"; do | ||
if [[ ${ARG_LIST[i]} = "--enrollment-token" || ${ARG_LIST[i]} = "$ENROLLMENT_TOKEN" ]]; then | ||
unset 'ARG_LIST[i]' | ||
fi | ||
done | ||
|
||
# manual parsing to find out, if process should be detached | ||
if [[ $DAEMONIZE = false ]]; then | ||
exec \ | ||
|
@@ -90,7 +115,7 @@ if [[ $DAEMONIZE = false ]]; then | |
-Des.bundled_jdk="$ES_BUNDLED_JDK" \ | ||
-cp "$ES_CLASSPATH" \ | ||
org.elasticsearch.bootstrap.Elasticsearch \ | ||
"$@" <<<"$KEYSTORE_PASSWORD" | ||
"${ARG_LIST[@]}" <<<"$KEYSTORE_PASSWORD" | ||
else | ||
exec \ | ||
"$JAVA" \ | ||
|
@@ -103,7 +128,7 @@ else | |
-Des.bundled_jdk="$ES_BUNDLED_JDK" \ | ||
-cp "$ES_CLASSPATH" \ | ||
org.elasticsearch.bootstrap.Elasticsearch \ | ||
"$@" \ | ||
"${ARG_LIST[@]}" \ | ||
<<<"$KEYSTORE_PASSWORD" & | ||
retval=$? | ||
pid=$! | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,14 +5,15 @@ setlocal enableextensions | |
|
||
SET params='%*' | ||
SET checkpassword=Y | ||
SET enrolltocluster=N | ||
SET attemptautoconfig=Y | ||
|
||
:loop | ||
FOR /F "usebackq tokens=1* delims= " %%A IN (!params!) DO ( | ||
SET previous=!current! | ||
SET current=%%A | ||
SET params='%%B' | ||
SET silent=N | ||
|
||
IF "!current!" == "-s" ( | ||
SET silent=Y | ||
) | ||
|
@@ -38,14 +39,33 @@ FOR /F "usebackq tokens=1* delims= " %%A IN (!params!) DO ( | |
SET attemptautoconfig=N | ||
) | ||
|
||
IF "!current!" == "--enrollment-token" ( | ||
IF "!enrolltocluster!" == "Y" ( | ||
ECHO "Multiple --enrollment-token parameters are not allowed" 1>&2 | ||
goto exitwithone | ||
) | ||
SET enrolltocluster=Y | ||
SET attemptautoconfig=N | ||
) | ||
|
||
IF "!previous!" == "--enrollment-token" ( | ||
SET enrollmenttoken="!current!" | ||
) | ||
|
||
IF "!silent!" == "Y" ( | ||
SET nopauseonerror=Y | ||
) ELSE ( | ||
IF "x!newparams!" NEQ "x" ( | ||
SET newparams=!newparams! !current! | ||
) ELSE ( | ||
SET newparams=!current! | ||
) | ||
SET SHOULD_SKIP=false | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. skip adding |
||
IF "!previous!" == "--enrollment-token" SET SHOULD_SKIP=true | ||
IF "!current!" == "--enrollment-token" SET SHOULD_SKIP=true | ||
IF "!SHOULD_SKIP!" == "false" ( | ||
IF "x!newparams!" NEQ "x" ( | ||
SET newparams=!newparams! !current! | ||
) ELSE ( | ||
SET newparams=!current! | ||
) | ||
) | ||
|
||
) | ||
|
||
IF "x!params!" NEQ "x" ( | ||
|
@@ -73,13 +93,21 @@ IF "%checkpassword%"=="Y" ( | |
) | ||
) | ||
|
||
rem windows batch pipe will choke on special characters in strings | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^^=^^^^! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^&=^^^&! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^|=^^^|! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^<=^^^<! | ||
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.flavor="%ES_DISTRIBUTION_FLAVOR%" ^ | ||
-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.ConfigInitialNode" !newparams! | ||
-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 SHOULDEXIT=Y | ||
IF !ERRORLEVEL! EQU 0 SET SHOULDEXIT=N | ||
IF !ERRORLEVEL! EQU 73 SET SHOULDEXIT=N | ||
|
@@ -90,6 +118,19 @@ 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.flavor="%ES_DISTRIBUTION_FLAVOR%" ^ | ||
-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% | ||
IF !ERRORLEVEL! NEQ 0 ( | ||
exit /b !ERRORLEVEL! | ||
) | ||
) | ||
|
||
if not defined ES_TMPDIR ( | ||
for /f "tokens=* usebackq" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory"`) do set ES_TMPDIR=%%a | ||
) | ||
|
@@ -111,14 +152,6 @@ if "%MAYBE_JVM_OPTIONS_PARSER_FAILED%" == "jvm_options_parser_failed" ( | |
exit /b 1 | ||
) | ||
|
||
rem windows batch pipe will choke on special characters in strings | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^^=^^^^! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^&=^^^&! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^|=^^^|! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^<=^^^<! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^>=^^^>! | ||
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^\=^^^\! | ||
|
||
ECHO.!KEYSTORE_PASSWORD!| %JAVA% %ES_JAVA_OPTS% -Delasticsearch ^ | ||
-Des.path.home="%ES_HOME%" -Des.path.conf="%ES_PATH_CONF%" ^ | ||
-Des.distribution.flavor="%ES_DISTRIBUTION_FLAVOR%" ^ | ||
|
@@ -129,3 +162,8 @@ ECHO.!KEYSTORE_PASSWORD!| %JAVA% %ES_JAVA_OPTS% -Delasticsearch ^ | |
endlocal | ||
endlocal | ||
exit /b %ERRORLEVEL% | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE: I'm more than happy to remove this ugliness if we don't care about exiting with 0 (instead of an 1) when a user passes multiple |
||
rem this hack is ugly but necessary because we can't exit with /b X from within the argument parsing loop. | ||
rem exit 1 (without /b) would work for powershell but it will terminate the cmd process when run in cmd | ||
:exitwithone | ||
exit /b 1 |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ plugins { | |
dependencies { | ||
testImplementation project(':server') | ||
testImplementation project(':libs:elasticsearch-core') | ||
testImplementation(testArtifact(project(':x-pack:plugin:core'))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So that we can use |
||
testImplementation "junit:junit:${versions.junit}" | ||
testImplementation "org.hamcrest:hamcrest:${versions.hamcrest}" | ||
testImplementation "com.carrotsearch.randomizedtesting:randomizedtesting-runner:${versions.randomizedrunner}" | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I changed the way we are iterating over the args, so that we can capture the value of the enrollment token. We need this so that we can remove it from the args array that we end up passing to elasticserach process to start in the end, as elasticsearch will not allow for unrecognized options.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm curious about why you iterate through
$@
here (effectively), rather than$ARG_LIST
.If you just iterated through ARG_LIST you could remove the enrollment token in a single pass rather than needing to iterate again later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using SHIFT in this iteration so that I can recognize the named arg that comes after
--enrollment-token
. If Iwas to iterate over ARG_LIST , I'd be removing args from it while iterating so I wouldn't be able to use it below.Or, I am missing your point, let me know :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's not important, but you could do something like this: