Skip to content

Commit 9efa5be

Browse files
williamrandolphlcawljkakavas
authored
Password-protected Keystore Feature Branch PR (#51123) (#51510)
* Reload secure settings with password (#43197) If a password is not set, we assume an empty string to be compatible with previous behavior. Only allow the reload to be broadcast to other nodes if TLS is enabled for the transport layer. * Add passphrase support to elasticsearch-keystore (#38498) This change adds support for keystore passphrases to all subcommands of the elasticsearch-keystore cli tool and adds a subcommand for changing the passphrase of an existing keystore. The work to read the passphrase in Elasticsearch when loading, which will be addressed in a different PR. Subcommands of elasticsearch-keystore can handle (open and create) passphrase protected keystores When reading a keystore, a user is only prompted for a passphrase only if the keystore is passphrase protected. When creating a keystore, a user is allowed (default behavior) to create one with an empty passphrase Passphrase can be set to be empty when changing/setting it for an existing keystore Relates to: #32691 Supersedes: #37472 * Restore behavior for force parameter (#44847) Turns out that the behavior of `-f` for the add and add-file sub commands where it would also forcibly create the keystore if it didn't exist, was by design - although undocumented. This change restores that behavior auto-creating a keystore that is not password protected if the force flag is used. The force OptionSpec is moved to the BaseKeyStoreCommand as we will presumably want to maintain the same behavior in any other command that takes a force option. * Handle pwd protected keystores in all CLI tools (#45289) This change ensures that `elasticsearch-setup-passwords` and `elasticsearch-saml-metadata` can handle a password protected elasticsearch.keystore. For setup passwords the user would be prompted to add the elasticsearch keystore password upon running the tool. There is no option to pass the password as a parameter as we assume the user is present in order to enter the desired passwords for the built-in users. For saml-metadata, we prompt for the keystore password at all times even though we'd only need to read something from the keystore when there is a signing or encryption configuration. * Modify docs for setup passwords and saml metadata cli (#45797) Adds a sentence in the documentation of `elasticsearch-setup-passwords` and `elasticsearch-saml-metadata` to describe that users would be prompted for the keystore's password when running these CLI tools, when the keystore is password protected. Co-Authored-By: Lisa Cawley <[email protected]> * Elasticsearch keystore passphrase for startup scripts (#44775) This commit allows a user to provide a keystore password on Elasticsearch startup, but only prompts when the keystore exists and is encrypted. The entrypoint in Java code is standard input. When the Bootstrap class is checking for secure keystore settings, it checks whether or not the keystore is encrypted. If so, we read one line from standard input and use this as the password. For simplicity's sake, we allow a maximum passphrase length of 128 characters. (This is an arbitrary limit and could be increased or eliminated. It is also enforced in the keystore tools, so that a user can't create a password that's too long to enter at startup.) In order to provide a password on standard input, we have to account for four different ways of starting Elasticsearch: the bash startup script, the Windows batch startup script, systemd startup, and docker startup. We use wrapper scripts to reduce systemd and docker to the bash case: in both cases, a wrapper script can read a passphrase from the filesystem and pass it to the bash script. In order to simplify testing the need for a passphrase, I have added a has-passwd command to the keystore tool. This command can run silently, and exit with status 0 when the keystore has a password. It exits with status 1 if the keystore doesn't exist or exists and is unencrypted. A good deal of the code-change in this commit has to do with refactoring packaging tests to cleanly use the same tests for both the "archive" and the "package" cases. This required not only moving tests around, but also adding some convenience methods for an abstraction layer over distribution-specific commands. * Adjust docs for password protected keystore (#45054) This commit adds relevant parts in the elasticsearch-keystore sub-commands reference docs and in the reload secure settings API doc. * Fix failing Keystore Passphrase test for feature branch (#50154) One problem with the passphrase-from-file tests, as written, is that they would leave a SystemD environment variable set when they failed, and this setting would cause elasticsearch startup to fail for other tests as well. By using a try-finally, I hope that these tests will fail more gracefully. It appears that our Fedora and Ubuntu environments may be configured to store journald information under /var rather than under /run, so that it will persist between boots. Our destructive tests that read from the journal need to account for this in order to avoid trying to limit the output we check in tests. * Run keystore management tests on docker distros (#50610) * Add Docker handling to PackagingTestCase Keystore tests need to be able to run in the Docker case. We can do this by using a DockerShell instead of a plain Shell when Docker is running. * Improve ES startup check for docker Previously we were checking truncated output for the packaged JDK as an indication that Elasticsearch had started. With new preliminary password checks, we might get a false positive from ES keystore commands, so we have to check specifically that the Elasticsearch class from the Bootstrap package is what's running. * Test password-protected keystore with Docker (#50803) This commit adds two tests for the case where we mount a password-protected keystore into a Docker container and provide a password via a Docker environment variable. We also fix a logging bug where we were logging the identifier for an array of strings rather than the contents of that array. * Add documentation for keystore startup prompting (#50821) When a keystore is password-protected, Elasticsearch will prompt at startup. This commit adds documentation for this prompt for the archive, systemd, and Docker cases. Co-authored-by: Lisa Cawley <[email protected]> * Warn when unable to upgrade keystore on debian (#51011) For Red Hat RPM upgrades, we warn if we can't upgrade the keystore. This commit brings the same logic to the code for Debian packages. See the posttrans file for gets executed for RPMs. * Restore handling of string input Adds tests that were mistakenly removed. One of these tests proved we were not handling the the stdin (-x) option correctly when no input was added. This commit restores the original approach of reading stdin one char at a time until there is no more (-1, \r, \n) instead of using readline() that might return null * Apply spotless reformatting * Use '--since' flag to get recent journal messages When we get Elasticsearch logs from journald, we want to fetch only log messages from the last run. There are two reasons for this. First, if there are many logs, we might get a string that's too large for our utility methods. Second, when we're looking for a specific message or error, we almost certainly want to look only at messages from the last execution. Previously, we've been trying to do this by clearing out the physical files under the journald process. But there seems to be some contention over these directories: if journald writes a log file in between when our deletion command deletes the file and when it deletes the log directory, the deletion will fail. It seems to me that we might be able to use journald's "--since" flag to retrieve only log messages from the last run, and that this might be less likely to fail due to race conditions in file deletion. Unfortunately, it looks as if the "--since" flag has a granularity of one-second. I've added a two-second sleep to make sure that there's a sufficient gap between the test that will read from journald and the test before it. * Use new journald wrapper pattern * Update version added in secure settings request Co-authored-by: Lisa Cawley <[email protected]> Co-authored-by: Ioannis Kakavas <[email protected]>
1 parent 2239ba8 commit 9efa5be

File tree

79 files changed

+2526
-584
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

79 files changed

+2526
-584
lines changed

Vagrantfile

+1
Original file line numberDiff line numberDiff line change
@@ -479,6 +479,7 @@ JAVA
479479
ensure curl
480480
ensure unzip
481481
ensure rsync
482+
ensure expect
482483
483484
installed bats || {
484485
# Bats lives in a git repository....

distribution/docker/docker-test-entrypoint.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22
cd /usr/share/elasticsearch/bin/
3-
./elasticsearch-users useradd x_pack_rest_user -p x-pack-test-password -r superuser || true
3+
./elasticsearch-users useradd x_pack_rest_user -p x-pack-test-password -r superuser || true
44
echo "testnode" > /tmp/password
55
cat /tmp/password | ./elasticsearch-keystore add -x -f -v 'xpack.security.transport.ssl.keystore.secure_password'
66
cat /tmp/password | ./elasticsearch-keystore add -x -f -v 'xpack.security.http.ssl.keystore.secure_password'

distribution/docker/src/docker/bin/docker-entrypoint.sh

+13-3
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,18 @@ if [[ -f bin/elasticsearch-users ]]; then
5757
# honor the variable if it's present.
5858
if [[ -n "$ELASTIC_PASSWORD" ]]; then
5959
[[ -f /usr/share/elasticsearch/config/elasticsearch.keystore ]] || (run_as_other_user_if_needed elasticsearch-keystore create)
60-
if ! (run_as_other_user_if_needed elasticsearch-keystore list | grep -q '^bootstrap.password$'); then
61-
(run_as_other_user_if_needed echo "$ELASTIC_PASSWORD" | elasticsearch-keystore add -x 'bootstrap.password')
60+
if ! (run_as_other_user_if_needed elasticsearch-keystore has-passwd --silent) ; then
61+
# keystore is unencrypted
62+
if ! (run_as_other_user_if_needed elasticsearch-keystore list | grep -q '^bootstrap.password$'); then
63+
(run_as_other_user_if_needed echo "$ELASTIC_PASSWORD" | elasticsearch-keystore add -x 'bootstrap.password')
64+
fi
65+
else
66+
# keystore requires password
67+
if ! (run_as_other_user_if_needed echo "$KEYSTORE_PASSWORD" \
68+
| elasticsearch-keystore list | grep -q '^bootstrap.password$') ; then
69+
COMMANDS="$(printf "%s\n%s" "$KEYSTORE_PASSWORD" "$ELASTIC_PASSWORD")"
70+
(run_as_other_user_if_needed echo "$COMMANDS" | elasticsearch-keystore add -x 'bootstrap.password')
71+
fi
6272
fi
6373
fi
6474
fi
@@ -70,4 +80,4 @@ if [[ "$(id -u)" == "0" ]]; then
7080
fi
7181
fi
7282

73-
run_as_other_user_if_needed /usr/share/elasticsearch/bin/elasticsearch
83+
run_as_other_user_if_needed /usr/share/elasticsearch/bin/elasticsearch <<<"$KEYSTORE_PASSWORD"

distribution/packages/build.gradle

+4
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,10 @@ Closure commonPackageConfig(String type, boolean oss, boolean jdk) {
231231
from "${packagingFiles}/systemd/sysctl/elasticsearch.conf"
232232
fileMode 0644
233233
}
234+
into('/usr/share/elasticsearch/bin') {
235+
from "${packagingFiles}/systemd/systemd-entrypoint"
236+
fileMode 0755
237+
}
234238

235239
// ========= sysV init =========
236240
configurationFile '/etc/init.d/elasticsearch'

distribution/packages/src/common/scripts/postinst

+6-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,12 @@ if [ "$PACKAGE" = "deb" ]; then
108108
chmod 660 "${ES_PATH_CONF}"/elasticsearch.keystore
109109
md5sum "${ES_PATH_CONF}"/elasticsearch.keystore > "${ES_PATH_CONF}"/.elasticsearch.keystore.initial_md5sum
110110
else
111-
/usr/share/elasticsearch/bin/elasticsearch-keystore upgrade
111+
if /usr/share/elasticsearch/bin/elasticsearch-keystore has-passwd --silent ; then
112+
echo "### Warning: unable to upgrade encrypted keystore" 1>&2
113+
echo " Please run elasticsearch-keystore upgrade and enter password" 1>&2
114+
else
115+
/usr/share/elasticsearch/bin/elasticsearch-keystore upgrade
116+
fi
112117
fi
113118
fi
114119

distribution/packages/src/common/scripts/posttrans

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@ if [ ! -f "${ES_PATH_CONF}"/elasticsearch.keystore ]; then
1111
chmod 660 "${ES_PATH_CONF}"/elasticsearch.keystore
1212
md5sum "${ES_PATH_CONF}"/elasticsearch.keystore > "${ES_PATH_CONF}"/.elasticsearch.keystore.initial_md5sum
1313
else
14-
/usr/share/elasticsearch/bin/elasticsearch-keystore upgrade
14+
if /usr/share/elasticsearch/bin/elasticsearch-keystore has-passwd --silent ; then
15+
echo "### Warning: unable to upgrade encrypted keystore" 1>&2
16+
echo " Please run elasticsearch-keystore upgrade and enter password" 1>&2
17+
else
18+
/usr/share/elasticsearch/bin/elasticsearch-keystore upgrade
19+
fi
1520
fi
1621

1722
${scripts.footer}

distribution/packages/src/common/systemd/elasticsearch.service

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ WorkingDirectory=/usr/share/elasticsearch
1919
User=elasticsearch
2020
Group=elasticsearch
2121

22-
ExecStart=/usr/share/elasticsearch/bin/elasticsearch -p ${PID_DIR}/elasticsearch.pid --quiet
22+
ExecStart=/usr/share/elasticsearch/bin/systemd-entrypoint -p ${PID_DIR}/elasticsearch.pid --quiet
2323

2424
# StandardOutput is configured to redirect to journalctl since
2525
# some error messages may be logged in standard output before
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#!/bin/sh
2+
3+
# This wrapper script allows SystemD to feed a file containing a passphrase into
4+
# the main Elasticsearch startup script
5+
6+
if [ -n "$ES_KEYSTORE_PASSPHRASE_FILE" ] ; then
7+
exec /usr/share/elasticsearch/bin/elasticsearch "$@" < "$ES_KEYSTORE_PASSPHRASE_FILE"
8+
else
9+
exec /usr/share/elasticsearch/bin/elasticsearch "$@"
10+
fi

distribution/src/bin/elasticsearch

+15-2
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,19 @@ if [ -z "$ES_TMPDIR" ]; then
2020
ES_TMPDIR=`"$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.TempDirectory`
2121
fi
2222

23+
# get keystore password before setting java options to avoid
24+
# conflicting GC configurations for the keystore tools
25+
unset KEYSTORE_PASSWORD
26+
KEYSTORE_PASSWORD=
27+
if ! echo $* | grep -E -q '(^-h |-h$| -h |--help$|--help |^-V |-V$| -V |--version$|--version )' \
28+
&& "`dirname "$0"`"/elasticsearch-keystore has-passwd --silent
29+
then
30+
if ! read -s -r -p "Elasticsearch keystore password: " KEYSTORE_PASSWORD ; then
31+
echo "Failed to read keystore password on console" 1>&2
32+
exit 1
33+
fi
34+
fi
35+
2336
ES_JVM_OPTIONS="$ES_PATH_CONF"/jvm.options
2437
ES_JAVA_OPTS=`export ES_TMPDIR; "$JAVA" -cp "$ES_CLASSPATH" org.elasticsearch.tools.launchers.JvmOptionsParser "$ES_JVM_OPTIONS"`
2538

@@ -35,7 +48,7 @@ if ! echo $* | grep -E '(^-d |-d$| -d |--daemonize$|--daemonize )' > /dev/null;
3548
-Des.bundled_jdk="$ES_BUNDLED_JDK" \
3649
-cp "$ES_CLASSPATH" \
3750
org.elasticsearch.bootstrap.Elasticsearch \
38-
"$@"
51+
"$@" <<<"$KEYSTORE_PASSWORD"
3952
else
4053
exec \
4154
"$JAVA" \
@@ -48,7 +61,7 @@ else
4861
-cp "$ES_CLASSPATH" \
4962
org.elasticsearch.bootstrap.Elasticsearch \
5063
"$@" \
51-
<&- &
64+
<<<"$KEYSTORE_PASSWORD" &
5265
retval=$?
5366
pid=$!
5467
[ $retval -eq 0 ] || exit $retval

distribution/src/bin/elasticsearch-cli.bat

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ set ES_JAVA_OPTS=-Xms4m -Xmx64m -XX:+UseSerialGC %ES_JAVA_OPTS%
2525
-cp "%ES_CLASSPATH%" ^
2626
"%ES_MAIN_CLASS%" ^
2727
%*
28-
28+
2929
exit /b %ERRORLEVEL%

distribution/src/bin/elasticsearch.bat

+41-1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ setlocal enabledelayedexpansion
44
setlocal enableextensions
55

66
SET params='%*'
7+
SET checkpassword=Y
78

89
:loop
910
FOR /F "usebackq tokens=1* delims= " %%A IN (!params!) DO (
@@ -18,6 +19,20 @@ FOR /F "usebackq tokens=1* delims= " %%A IN (!params!) DO (
1819
SET silent=Y
1920
)
2021

22+
IF "!current!" == "-h" (
23+
SET checkpassword=N
24+
)
25+
IF "!current!" == "--help" (
26+
SET checkpassword=N
27+
)
28+
29+
IF "!current!" == "-V" (
30+
SET checkpassword=N
31+
)
32+
IF "!current!" == "--version" (
33+
SET checkpassword=N
34+
)
35+
2136
IF "!silent!" == "Y" (
2237
SET nopauseonerror=Y
2338
) ELSE (
@@ -41,6 +56,18 @@ IF ERRORLEVEL 1 (
4156
EXIT /B %ERRORLEVEL%
4257
)
4358

59+
SET KEYSTORE_PASSWORD=
60+
IF "%checkpassword%"=="Y" (
61+
CALL "%~dp0elasticsearch-keystore.bat" has-passwd --silent
62+
IF !ERRORLEVEL! EQU 0 (
63+
SET /P KEYSTORE_PASSWORD=Elasticsearch keystore password:
64+
IF !ERRORLEVEL! NEQ 0 (
65+
ECHO Failed to read keystore password on standard input
66+
EXIT /B !ERRORLEVEL!
67+
)
68+
)
69+
)
70+
4471
if not defined ES_TMPDIR (
4572
for /f "tokens=* usebackq" %%a in (`CALL %JAVA% -cp "!ES_CLASSPATH!" "org.elasticsearch.tools.launchers.TempDirectory"`) do set ES_TMPDIR=%%a
4673
)
@@ -54,7 +81,20 @@ if "%MAYBE_JVM_OPTIONS_PARSER_FAILED%" == "jvm_options_parser_failed" (
5481
exit /b 1
5582
)
5683

57-
%JAVA% %ES_JAVA_OPTS% -Delasticsearch -Des.path.home="%ES_HOME%" -Des.path.conf="%ES_PATH_CONF%" -Des.distribution.flavor="%ES_DISTRIBUTION_FLAVOR%" -Des.distribution.type="%ES_DISTRIBUTION_TYPE%" -Des.bundled_jdk="%ES_BUNDLED_JDK%" -cp "%ES_CLASSPATH%" "org.elasticsearch.bootstrap.Elasticsearch" !newparams!
84+
rem windows batch pipe will choke on special characters in strings
85+
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^^=^^^^!
86+
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^&=^^^&!
87+
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^|=^^^|!
88+
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^<=^^^<!
89+
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^>=^^^>!
90+
SET KEYSTORE_PASSWORD=!KEYSTORE_PASSWORD:^\=^^^\!
91+
92+
ECHO.!KEYSTORE_PASSWORD!| %JAVA% %ES_JAVA_OPTS% -Delasticsearch ^
93+
-Des.path.home="%ES_HOME%" -Des.path.conf="%ES_PATH_CONF%" ^
94+
-Des.distribution.flavor="%ES_DISTRIBUTION_FLAVOR%" ^
95+
-Des.distribution.type="%ES_DISTRIBUTION_TYPE%" ^
96+
-Des.bundled_jdk="%ES_BUNDLED_JDK%" ^
97+
-cp "%ES_CLASSPATH%" "org.elasticsearch.bootstrap.Elasticsearch" !newparams!
5898

5999
endlocal
60100
endlocal

docs/reference/cluster.asciidoc

+2
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ include::cluster/nodes-hot-threads.asciidoc[]
9999

100100
include::cluster/nodes-info.asciidoc[]
101101

102+
include::cluster/nodes-reload-secure-settings.asciidoc[]
103+
102104
include::cluster/nodes-stats.asciidoc[]
103105

104106
include::cluster/pending.asciidoc[]

docs/reference/cluster/nodes-reload-secure-settings.asciidoc

+41-11
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
[[cluster-nodes-reload-secure-settings]]
2-
== Nodes Reload Secure Settings
2+
=== Nodes reload secure settings API
3+
++++
4+
<titleabbrev>Nodes reload secure settings</titleabbrev>
5+
++++
36

4-
The cluster nodes reload secure settings API is used to re-read the
5-
local node's encrypted keystore. Specifically, it will prompt the keystore
6-
decryption and reading across the cluster. The keystore's plain content is
7-
used to reinitialize all compatible plugins. A compatible plugin can be
8-
reinitialized without restarting the node. The operation is
9-
complete when all compatible plugins have finished reinitializing. Subsequently,
10-
the keystore is closed and any changes to it will not be reflected on the node.
7+
8+
The cluster nodes reload secure settings API is used to re-load the keystore on each node.
119

1210
[source,console]
1311
--------------------------------------------------
@@ -21,9 +19,41 @@ The first command reloads the keystore on each node. The seconds allows
2119
to selectively target `nodeId1` and `nodeId2`. The node selection options are
2220
detailed <<cluster-nodes,here>>.
2321

24-
Note: It is an error if secure settings are inconsistent across the cluster
25-
nodes, yet this consistency is not enforced whatsoever. Hence, reloading specific
26-
nodes is not standard. It is only justifiable when retrying failed reload operations.
22+
NOTE: {es} requires consistent secure settings across the cluster nodes, but this consistency is not enforced.
23+
Hence, reloading specific nodes is not standard. It is only justifiable when retrying failed reload operations.
24+
25+
==== Reload Password Protected Secure Settings
26+
27+
When the {es} keystore is password protected and not simply obfuscated, the password for the keystore needs
28+
to be provided in the request to reload the secure settings.
29+
Reloading the settings for the whole cluster assumes that all nodes' keystores are protected with the same password
30+
and is only allowed when {ref}/configuring-tls.html#tls-transport[node to node communications are encrypted]
31+
32+
[source,js]
33+
--------------------------------------------------
34+
POST _nodes/reload_secure_settings
35+
{
36+
"reload_secure_settings": "s3cr3t" <1>
37+
}
38+
--------------------------------------------------
39+
// NOTCONSOLE
40+
41+
<1> The common password that the {es} keystore is encrypted with in every node of the cluster.
42+
43+
Alternatively the secure settings can be reloaded on a per node basis, locally accessing the API and passing the
44+
node-specific {es} keystore password.
45+
46+
[source,js]
47+
--------------------------------------------------
48+
POST _nodes/_local/reload_secure_settings
49+
{
50+
"reload_secure_settings": "s3cr3t" <1>
51+
}
52+
--------------------------------------------------
53+
// NOTCONSOLE
54+
55+
<1> The password that the {es} keystore is encrypted with on the local node.
56+
2757

2858
[float]
2959
[[rest-reload-secure-settings]]

0 commit comments

Comments
 (0)