Skip to content
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

Security auto-configuration for packaged installations #75144

Merged
merged 293 commits into from
Oct 15, 2021
Merged
Show file tree
Hide file tree
Changes from 48 commits
Commits
Show all changes
293 commits
Select commit Hold shift + click to select a range
7d9b6f5
generate password on installation time and show it to the user. We se…
jkakavas Jul 13, 2021
2598dd3
More error handling
albertzaharovits Jul 13, 2021
9c29107
Merge branch 'master' into auto_conf_security
albertzaharovits Jul 14, 2021
e566c3e
Merge branch 'auto_conf_security' into packaged-auto-conf
jkakavas Jul 14, 2021
d104c9b
add undocumented parameter to the autoconfig CLI tool so that users d…
jkakavas Jul 14, 2021
38ba6b5
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 3, 2021
6fd7123
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 9, 2021
3ecbb0f
remove old version of file
jkakavas Aug 9, 2021
576b608
no need to all a CLI tool from postinst
jkakavas Aug 9, 2021
d566434
wip
jkakavas Aug 9, 2021
3aab03e
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 9, 2021
21fcc13
add class to generate and store password hash
jkakavas Aug 10, 2021
6d924e5
more wip
jkakavas Aug 10, 2021
6fd451e
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 20, 2021
0d4ad2f
more wip - commit to run some packaging tests
jkakavas Aug 21, 2021
1239ab7
spotless
jkakavas Aug 21, 2021
75c26e2
sudo -g doesn't work in all OS
jkakavas Aug 21, 2021
b88f1ec
create .elasticsearch.keystore.initial_md5sum
jkakavas Aug 21, 2021
0113694
fix tests?
jkakavas Aug 22, 2021
dcc40e3
fix filtering
jkakavas Aug 22, 2021
776a155
remove auto-config dir on purge
jkakavas Aug 23, 2021
882c191
fix filtering
jkakavas Aug 23, 2021
f0b1a38
spotless
jkakavas Aug 23, 2021
fd4e4f9
further test fixes
jkakavas Aug 23, 2021
ea8dfb9
fix purging
jkakavas Aug 23, 2021
26ec0e3
more
jkakavas Aug 23, 2021
072a672
fix purging?
jkakavas Aug 23, 2021
b58fe80
create first
jkakavas Aug 23, 2021
6cc80bf
correct updated permissions in tests
jkakavas Aug 23, 2021
f6a313d
d
jkakavas Aug 23, 2021
c1fa865
one of these days, packaging tests... one of these days...
jkakavas Aug 23, 2021
776de7c
remove keystore on purge
jkakavas Aug 23, 2021
3a37699
fix group owner
jkakavas Aug 23, 2021
f6ecd0d
Fix test matcher
jkakavas Aug 23, 2021
5c732e2
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 23, 2021
680503f
Revert unecessary changes
jkakavas Aug 23, 2021
5fb7152
fix
jkakavas Aug 23, 2021
bc1c78a
revert unncessary change
jkakavas Aug 23, 2021
1e702a5
make banners shorter
jkakavas Aug 23, 2021
076f1f9
change upgrade test
jkakavas Aug 23, 2021
c88cbc6
hopefully make a meaningful test this time
jkakavas Aug 23, 2021
72fc1fd
one last time..
jkakavas Aug 23, 2021
a97c469
fix ownership
jkakavas Aug 23, 2021
7b70758
typos
jkakavas Aug 23, 2021
7a48d74
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 23, 2021
19df189
simplify postinst
jkakavas Aug 23, 2021
3bf3962
add check for package upgades
jkakavas Aug 23, 2021
9a2f6ef
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 23, 2021
7bbb58d
Possible disable security when upgrading to 8.0.0 (temporarily)
jkakavas Aug 23, 2021
99485ea
minor changes to postinst and some additional tests
jkakavas Aug 24, 2021
7db3187
null check
jkakavas Aug 24, 2021
9ed95b1
meh
jkakavas Aug 24, 2021
2ab4d37
meh^2
jkakavas Aug 24, 2021
9f81dab
one of these days, I'll figure out how to read docs
jkakavas Aug 24, 2021
5f65641
tighten test assertion
jkakavas Aug 24, 2021
423dbd6
Update docs/changelog/75144.yaml
jkakavas Aug 24, 2021
639f5fd
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Aug 24, 2021
f73aa25
Single area label!
jkakavas Aug 24, 2021
d7e8cf4
change changelog
jkakavas Sep 1, 2021
3c8416c
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Sep 1, 2021
5f173f9
Merge branch 'master' into packaged-auto-conf
elasticmachine Sep 2, 2021
15e1eff
Merge branch 'master' into packaged-auto-conf
elasticmachine Sep 2, 2021
2795c3a
Attemp TLS config
albertzaharovits Sep 3, 2021
bc28f81
Checkstyle
albertzaharovits Sep 3, 2021
d0a1122
Crazy typo
albertzaharovits Sep 3, 2021
48c7c80
move does not support attributes
albertzaharovits Sep 3, 2021
d0c9270
Archives
albertzaharovits Sep 3, 2021
d0ad3c1
Nit
albertzaharovits Sep 3, 2021
c75dda6
Spotless
albertzaharovits Sep 3, 2021
71bd34d
Facepalm
albertzaharovits Sep 3, 2021
fa4b93e
QA OS Make request with https
albertzaharovits Sep 3, 2021
83d4036
Merge branch 'master' into config_tls_when_node_starts
albertzaharovits Sep 3, 2021
cce8094
working on archive tests
albertzaharovits Sep 3, 2021
9c82a71
Archives tests still
albertzaharovits Sep 3, 2021
7afeee2
ArchiveTests test51
albertzaharovits Sep 3, 2021
169b3e7
ArchiveTests still
albertzaharovits Sep 3, 2021
a05bb85
Commons io FileUtils
albertzaharovits Sep 3, 2021
b66c345
Commons io precommit
albertzaharovits Sep 4, 2021
230a364
Verbose auto-conf pacakging tests
albertzaharovits Sep 4, 2021
26ad69a
More info about why the node doesn't start
albertzaharovits Sep 4, 2021
f9b74fd
Remove explicit CLI tool
jkakavas Sep 6, 2021
4360d08
Updates from feedback
jkakavas Sep 6, 2021
b0cbdfc
fix cert DN and remove references to deleted CLI tool
jkakavas Sep 6, 2021
1feac33
don't exit with 0 all the time
jkakavas Sep 6, 2021
5dc4258
more packaging
jkakavas Sep 6, 2021
c05732d
more packaging2
jkakavas Sep 6, 2021
4dcbc84
remove tripping assertion
jkakavas Sep 10, 2021
4f0a743
Enable nodes running TEST INTEG distribution to call ConfigInitialNod…
jkakavas Sep 10, 2021
3535b00
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 10, 2021
26e7811
fix keystore management tests
jkakavas Sep 10, 2021
7dcfd2f
test20KeystorePasswordOnStandardInput runs before we ever start the n…
jkakavas Sep 10, 2021
f86e6a8
Wait for enough time for ES in docker to complete auto-configuration
jkakavas Sep 10, 2021
11834f1
missing colon that took me 4 hours to spot
jkakavas Sep 10, 2021
bdcd267
more test fixes
jkakavas Sep 10, 2021
d83fd6c
take TLS autoconfiguration in consideration for ArchiveGenerateInitia…
jkakavas Sep 10, 2021
d90104b
guess what
jkakavas Sep 10, 2021
9fd5e77
:/ :(
jkakavas Sep 10, 2021
0ef54aa
Use autoconfigured TLS when needed in Docker, disable it otherwise
jkakavas Sep 11, 2021
da5e3de
fix docker (for good ?) and print debug to retain my sanity
jkakavas Sep 11, 2021
c29e3e7
what's another commit
jkakavas Sep 11, 2021
c8785c4
disable auto-configuration in unrelated docker tests, fix stupid bug …
jkakavas Sep 12, 2021
1498131
On linux too
jkakavas Sep 12, 2021
84046fd
Create user after we verify installation so that upon installation ve…
jkakavas Sep 12, 2021
cd8c285
We might not have an elasticsearch.yml file in Docker
jkakavas Sep 12, 2021
182c45d
revert previous unnecessary changes
jkakavas Sep 12, 2021
8ac2890
spotless
jkakavas Sep 12, 2021
1e439a6
more adjustments
jkakavas Sep 12, 2021
48da88c
more adjustments
jkakavas Sep 12, 2021
dd5f118
this should fix all in linux
jkakavas Sep 12, 2021
9791b77
plugin and configuration tests
jkakavas Sep 12, 2021
71c934e
some more
jkakavas Sep 13, 2021
bc68b30
some more
jkakavas Sep 13, 2021
9cd79e0
config tests
jkakavas Sep 13, 2021
69990d1
add temp debug info
jkakavas Sep 13, 2021
6221408
get the certificate from the right path
jkakavas Sep 13, 2021
a754c1e
add support for auto-configuration to windows batch file
jkakavas Sep 16, 2021
c150a08
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 16, 2021
3f1d58a
Docker packaging tests now use auto-configured TLS
jkakavas Sep 19, 2021
73be68f
CertGenCliTests do not need TLS auto-configuration
jkakavas Sep 19, 2021
a69986c
cleanup
jkakavas Sep 19, 2021
c55d9dd
spotless
jkakavas Sep 19, 2021
deafd2d
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 19, 2021
ec3c51f
fix certgen tests
jkakavas Sep 19, 2021
6ccf723
don't sstop on success
jkakavas Sep 20, 2021
c1771d8
minor updates
jkakavas Sep 20, 2021
749ba9b
fix
jkakavas Sep 21, 2021
084b2f9
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 21, 2021
a8f7c47
Catch Throwables and amend how we populate SANs for certificates
jkakavas Sep 21, 2021
a5baae0
add more tests
jkakavas Sep 21, 2021
28bca9e
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 21, 2021
90a9022
spotless
jkakavas Sep 21, 2021
90bd0f6
minor fixes
jkakavas Sep 21, 2021
7d2219c
cleanup data dir after test to avoid false positives
jkakavas Sep 22, 2021
4449fd5
test adjustments
jkakavas Sep 22, 2021
9ecc182
spotless
jkakavas Sep 22, 2021
9781eba
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 22, 2021
9145898
revert fix for local invocation
jkakavas Sep 22, 2021
ea5932d
spotless
jkakavas Sep 22, 2021
a781149
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 22, 2021
ae591d3
Use legacy MAC algorithm for PKCS12 in tests until we can bump minimu…
jkakavas Sep 22, 2021
10f3474
Main
albertzaharovits Sep 27, 2021
6076aab
Nit rename & javadoc
albertzaharovits Sep 27, 2021
674202d
ReservedRealm security index param remove fallout
albertzaharovits Sep 27, 2021
98e3ed9
Merge branch 'master' into set-auto-conf-password
albertzaharovits Sep 27, 2021
ccd1dbe
WIP
albertzaharovits Sep 27, 2021
c2092d8
ReservedRealm tests
albertzaharovits Sep 28, 2021
0b89640
Merge branch 'master' into set-auto-conf-password
albertzaharovits Sep 28, 2021
e8b390c
Exception serialization test
albertzaharovits Sep 28, 2021
93abfb2
Almost
albertzaharovits Sep 28, 2021
ed8646f
ReservedRealm tests
albertzaharovits Sep 28, 2021
1a3220d
call external class properly
jkakavas Sep 28, 2021
42d88e9
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 28, 2021
6ebcebc
fix elasticsearch-env
jkakavas Sep 29, 2021
405c60d
fix tests for windows
jkakavas Sep 29, 2021
c3124d2
typo
jkakavas Sep 29, 2021
53443bd
NativeUsersStoreTests
albertzaharovits Sep 29, 2021
5645b7d
isElasticUserAutoConfigured
albertzaharovits Sep 29, 2021
125480c
minor fix
jkakavas Sep 29, 2021
8decc71
fix for windows
jkakavas Sep 29, 2021
4a9f987
set read only with attrib on windows
jkakavas Sep 29, 2021
41d6067
undo read only before deletion
jkakavas Sep 29, 2021
1e4648f
test
jkakavas Sep 29, 2021
8ac813e
temp ugly debug
jkakavas Sep 29, 2021
a0cd8de
Address feedback related to packaging tests
jkakavas Sep 29, 2021
de20c38
adjust test after enabling auto-config
jkakavas Sep 29, 2021
ba6720c
windows fixes
jkakavas Sep 30, 2021
6cde022
windows
jkakavas Sep 30, 2021
5798743
add bugurl
jkakavas Sep 30, 2021
38df918
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 30, 2021
ff48940
spotless
jkakavas Sep 30, 2021
7b86021
more spotless
jkakavas Sep 30, 2021
820fd28
wrong password instead of empty
jkakavas Sep 30, 2021
b59c55f
Mute on windows the test that I've been trying all morning to fix on …
jkakavas Sep 30, 2021
b070178
windows debugging
jkakavas Sep 30, 2021
b25cc0a
guess what spotless didn't like
jkakavas Sep 30, 2021
f4e784f
deBUG
jkakavas Sep 30, 2021
8fb6362
debugging windows packaging tests
jkakavas Sep 30, 2021
14e1f36
Negative integ tests
albertzaharovits Sep 30, 2021
a7e4c49
Merge branch 'master' into set-auto-conf-password
albertzaharovits Sep 30, 2021
4128d61
Merge fallout
albertzaharovits Sep 30, 2021
59c2542
Checkstyle
albertzaharovits Sep 30, 2021
299c710
Nit
albertzaharovits Sep 30, 2021
1b2eed3
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Sep 30, 2021
f908c25
path fix
jkakavas Sep 30, 2021
c5eeb4f
temp debug
jkakavas Sep 30, 2021
d646a3b
test fixes
jkakavas Sep 30, 2021
dcb6ee5
spotless
jkakavas Sep 30, 2021
b0377e1
moar spotless
jkakavas Sep 30, 2021
a33a1da
windows file ownership
jkakavas Sep 30, 2021
6f2be87
test fixes
jkakavas Oct 1, 2021
c83160d
Merge remote-tracking branch 'origin/master' into config_tls_when_nod…
jkakavas Oct 1, 2021
4cf3243
remove default from env
jkakavas Oct 1, 2021
4a7e626
Merge remote-tracking branch 'albertzaharovits/config_tls_when_node_s…
jkakavas Oct 1, 2021
0d75418
Merge branch 'packaged-auto-conf' of github.com:jkakavas/elasticsearc…
jkakavas Oct 1, 2021
53e6a2d
merge woes
jkakavas Oct 1, 2021
b28bd52
spotless
jkakavas Oct 1, 2021
be9eefa
test fixes
jkakavas Oct 1, 2021
bc8d9c5
selective mute to check the rest of the tests on windows
jkakavas Oct 1, 2021
1acd0cc
more fixes
jkakavas Oct 1, 2021
8df3cbf
unused import
jkakavas Oct 1, 2021
35b9845
fix autoconfig check for packages
jkakavas Oct 1, 2021
6f752ab
fix certgen
jkakavas Oct 1, 2021
ea66082
Merge remote-tracking branch 'albertzaharovits/set-auto-conf-password…
jkakavas Oct 1, 2021
a8337c2
...
jkakavas Oct 1, 2021
622dffd
fix package upgrade tests
jkakavas Oct 1, 2021
507b3f6
fix package upgrade tests for real now
jkakavas Oct 1, 2021
df561e8
adjustments
jkakavas Oct 2, 2021
514a240
bump leniency
jkakavas Oct 2, 2021
0ca655b
Merge remote-tracking branch 'albertzaharovits/config_tls_when_node_s…
jkakavas Oct 2, 2021
ba30af4
temp debugging
jkakavas Oct 2, 2021
4d1658b
fix docker issues on startup
jkakavas Oct 2, 2021
dd2f567
additional windows debug temp logging
jkakavas Oct 2, 2021
e175b5c
unused imports
jkakavas Oct 2, 2021
99b649c
spotless
jkakavas Oct 2, 2021
178580b
check if FileUtils.deleteDirectory from apache commons does the job o…
jkakavas Oct 3, 2021
99b4ad0
spotless
jkakavas Oct 3, 2021
5d793a3
test fixes
jkakavas Oct 3, 2021
dbf7250
Revert "check if FileUtils.deleteDirectory from apache commons does t…
jkakavas Oct 3, 2021
8bda57f
attempt to explicitly own the dir before removing it
jkakavas Oct 3, 2021
c3547ec
Merge remote-tracking branch 'albertzaharovits/config_tls_when_node_s…
jkakavas Oct 3, 2021
e3d0825
fix tests
jkakavas Oct 3, 2021
8abbf94
almost there
jkakavas Oct 3, 2021
1e981e0
almost, almost, there
jkakavas Oct 3, 2021
7e817d0
more
jkakavas Oct 3, 2021
ceca0f1
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Oct 9, 2021
cbf60bd
spotless
jkakavas Oct 9, 2021
af4d838
Can't use setup passwords for packaged installations
jkakavas Oct 9, 2021
2089d1c
fix tests
jkakavas Oct 9, 2021
6e72adb
add user when needed
jkakavas Oct 9, 2021
27a655c
remove temp dirs correctly in windows
jkakavas Oct 9, 2021
05c2efe
remove temp dirs correctly in windows2
jkakavas Oct 9, 2021
bae98d3
fix deletion
jkakavas Oct 9, 2021
738eb2e
fix autoformat
jkakavas Oct 9, 2021
c72b48d
hello spotless my old friend
jkakavas Oct 9, 2021
40a05af
proper windows cleaning
jkakavas Oct 10, 2021
5e6db81
spotless
jkakavas Oct 10, 2021
0f2d589
sort all first
jkakavas Oct 10, 2021
f721f19
print without deleting for troubleshooting
jkakavas Oct 11, 2021
a8eeff9
blind testing
jkakavas Oct 11, 2021
f0f7b65
spaces are important
jkakavas Oct 11, 2021
c3c6329
dbg
jkakavas Oct 11, 2021
3b0e7ff
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Oct 14, 2021
a77460f
unused import
jkakavas Oct 14, 2021
211391e
fixes
jkakavas Oct 14, 2021
bbb38b9
spotless
jkakavas Oct 14, 2021
ffc4f88
Merge remote-tracking branch 'origin/master' into packaged-auto-conf
jkakavas Oct 14, 2021
ffce3d3
revert unecessary changes and fix merge
jkakavas Oct 14, 2021
bda567b
unused imports
jkakavas Oct 14, 2021
7cdf1e9
Update docs/changelog/75144.yaml
jkakavas Oct 14, 2021
2e19810
fix test
jkakavas Oct 14, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ java.nio.file.Path#of(java.lang.String, java.lang.String[]) @ Use org.elasticsea
java.nio.file.FileSystems#getDefault() @ use org.elasticsearch.core.PathUtils.getDefaultFileSystem() instead.

java.nio.file.Files#getFileStore(java.nio.file.Path) @ Use org.elasticsearch.env.Environment.getFileStore() instead, impacted by JDK-8034057
java.nio.file.Files#isWritable(java.nio.file.Path) @ Use org.elasticsearch.env.Environment.isWritable() instead, impacted by JDK-8034057

@defaultMessage Use org.elasticsearch.common.Randomness#get for reproducible sources of randomness
java.util.Random#<init>()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -766,6 +766,7 @@ private Map<String, String> getESEnvironment() {
// If we are testing the current version of Elasticsearch, use the configured runtime Java, otherwise use the bundled JDK
if (getTestDistribution() == TestDistribution.INTEG_TEST || getVersion().equals(VersionProperties.getElasticsearchVersion())) {
defaultEnv.put("ES_JAVA_HOME", runtimeJava.get().getAbsolutePath());
defaultEnv.put("AUTO_CONFIG", "false");
}
defaultEnv.put("ES_PATH_CONF", configFile.getParent().toString());
String systemPropertiesString = "";
Expand Down
14 changes: 13 additions & 1 deletion distribution/packages/src/common/scripts/postinst
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,25 @@ case "$1" in
exit 1
;;
esac

# to pick up /usr/lib/sysctl.d/elasticsearch.conf
if command -v systemctl > /dev/null; then
systemctl restart systemd-sysctl.service || true
fi

if [ "x$IS_UPGRADE" != "xtrue" ]; then
# This runs on installation and since we do not set a password for the keystore by default, we can assume it's not password protected
jkakavas marked this conversation as resolved.
Show resolved Hide resolved
/usr/share/elasticsearch/bin/elasticsearch-autoconfig-security --explicitly-acknowledge-execution <<< ""
if [ -f "${ES_PATH_CONF}"/elasticsearch.keystore ]; then
INITIAL_PASSWORD=$(tr -dc 'A-Za-z0-9~!@#$%^&*-_=+?' </dev/urandom | head -c 14)
echo "$INITIAL_PASSWORD" | /usr/share/elasticsearch/bin/elasticsearch-keystore add -x 'bootstrap.password'
echo "########## Security autoconfiguration information ############"
echo "# #"
echo "# The password of the elastic superuser will be set to: ${INITIAL_PASSWORD} #"
echo "# #"
echo "# #"
echo "# #"
echo "#############################################################################################"
fi
if command -v systemctl >/dev/null; then
echo "### NOT starting on installation, please execute the following statements to configure elasticsearch service to start automatically using systemd"
echo " sudo systemctl daemon-reload"
Expand Down
18 changes: 18 additions & 0 deletions distribution/src/bin/elasticsearch
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,20 @@
source "`dirname "$0"`"/elasticsearch-env

CHECK_KEYSTORE=true
AUTO_CONFIG="${AUTO_CONFIG:-true}"
DAEMONIZE=false
for option in "$@"; do
case "$option" in
-h|--help|-V|--version)
CHECK_KEYSTORE=false
AUTO_CONFIG=false
;;
-d|--daemonize)
DAEMONIZE=true
;;
-e|--enroll)
AUTO_CONFIG=false
;;
esac
done

Expand All @@ -45,6 +50,19 @@ then
fi
fi

if [[ $AUTO_CONFIG = true ]]; then
# ignore failures in auto-configuration
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spoke with @colings86 about this today and we may want to revisit this decision.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Which decision is that ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That if we fail to perform auto-configuration (can't write to file) that we might want to fail instead of ignore. Something about "leniency" and being "abhorent", blah blah blah 😉

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like aphorisms as the next person but this is not about leniency. We ( and by we I mean mostly @albertzaharovits ) have spent a lot of time thinking about the cases where we should fail and when we should not. This is written in code in #74868 and Albert has the task to write this down to the design doc too. In short, we consider the case where we can't write to the config file because it is set as read-only, as a case where we have an explicit configuration decision by the user ( the file doesn't get to be read only by its own). As such we have elected to not treat this as an error and fail to start the node ( and in doing so introduce a breaking change as we allow this today ) but simply not attempt to auto-configure security for them. Authentication will be enabled but we won't enable and configure TLS. Happy to reconsider or discuss alternative approaches, @colings86 feel free to bring this up for discussion in the project or we can chat anytime

# it is possible that an auto-configuration failure can prevent the node from starting, but this is only the exceptional case
# most likely an auto-configuration failure will leave the configuration untouched, optionally printing a message
# if the error is unexpected, but the node will start as usual
ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigInitialNode \
ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
"$(dirname "$0")/elasticsearch-cli" \
"$@" \
<<<"$KEYSTORE_PASSWORD" || true
fi

# The JVM options parser produces the final JVM options to start Elasticsearch.
# It does this by incorporating JVM options in the following way:
# - first, system JVM options are applied (these are hardcoded options in the
Expand Down
7 changes: 7 additions & 0 deletions distribution/src/bin/elasticsearch-autoconfig-security
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash
jkakavas marked this conversation as resolved.
Show resolved Hide resolved

ES_MAIN_CLASS=org.elasticsearch.xpack.security.cli.AutoConfigInitialNode \
ES_ADDITIONAL_SOURCES="x-pack-env;x-pack-security-env" \
ES_ADDITIONAL_CLASSPATH_DIRECTORIES=lib/tools/security-cli \
"`dirname "$0"`"/elasticsearch-cli \
"$@"
15 changes: 13 additions & 2 deletions distribution/src/bin/elasticsearch-env
Original file line number Diff line number Diff line change
Expand Up @@ -119,16 +119,23 @@ if [[ "$ES_DISTRIBUTION_TYPE" == "docker" ]]; then

declare -a es_arg_array

containsElement () {
local e match="$1"
shift
for e; do [[ "$e" == "$match" ]] && return 0; done
return 1
}

while IFS='=' read -r envvar_key envvar_value
do
# Elasticsearch settings need to have at least two dot separated lowercase
# words, e.g. `cluster.name`, or uppercased with underscore separators and
# prefixed with `ES_`, e.g. `ES_CLUSTER_NAME`. Underscores in setting names
# are escaped by writing them as a double-underscore e.g. "__"
if [[ ! -z "$envvar_value" ]]; then
es_opt=""
if [[ "$envvar_key" =~ ^[a-z0-9_]+\.[a-z0-9_]+ ]]; then
es_opt="-E${envvar_key}=${envvar_value}"
es_arg_array+=("${es_opt}")
elif [[ "$envvar_key" =~ ^ES(_{1,2}[A-Z]+)+$ ]]; then
case "$envvar_key" in
# Do nothing for these. Not all of these are actually exported into the environment by our scripts,
Expand All @@ -148,10 +155,14 @@ if [[ "$ES_DISTRIBUTION_TYPE" == "docker" ]]; then
# The long-hand sed `y` command works in any sed variant.
envvar_key="$(echo "$envvar_key" | sed -e 's/^ES_//; s/_/./g ; s/\.\./_/g; y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' )"
es_opt="-E${envvar_key}=${envvar_value}"
es_arg_array+=("${es_opt}")
;;
esac
fi
if [[ ! -z "$es_opt" ]]; then
if ! containsElement "${es_opt}" "$@"; then
es_arg_array+=("${es_opt}")
fi
fi
fi
done <<< "$(env)"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
*/
public class ExitCodes {
public static final int OK = 0;
public static final int NOOP = 63; /* nothing to do */
jkakavas marked this conversation as resolved.
Show resolved Hide resolved
public static final int USAGE = 64; /* command line usage error */
public static final int DATA_ERROR = 65; /* data format error */
public static final int NO_INPUT = 66; /* cannot open input */
Expand Down
44 changes: 42 additions & 2 deletions qa/os/src/test/java/org/elasticsearch/packaging/util/Packages.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

Expand Down Expand Up @@ -153,7 +154,7 @@ public static void remove(Distribution distribution) throws Exception {
});
}

public static void verifyPackageInstallation(Installation installation, Distribution distribution, Shell sh) {
public static void verifyPackageInstallation(Installation installation, Distribution distribution, Shell sh) throws IOException {
verifyOssInstallation(installation, distribution, sh);
verifyDefaultInstallation(installation, distribution);
}
Expand Down Expand Up @@ -217,7 +218,7 @@ private static void verifyOssInstallation(Installation es, Distribution distribu
}
}

private static void verifyDefaultInstallation(Installation es, Distribution distribution) {
private static void verifyDefaultInstallation(Installation es, Distribution distribution) throws IOException {

Stream.of(
"elasticsearch-certgen",
Expand All @@ -240,6 +241,45 @@ private static void verifyDefaultInstallation(Installation es, Distribution dist

Stream.of("users", "users_roles", "roles.yml", "role_mapping.yml", "log4j2.properties")
.forEach(configFile -> assertThat(es.config(configFile), file(File, "root", "elasticsearch", p660)));
verifySecurityAutoConfigured(es, distribution);
}

private static void verifySecurityAutoConfigured(Installation es, Distribution distribution) throws IOException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd really rather not continue to add test coverage as assertions to verifyInstallation(). We should implement this as a new distinct test suite. We also need to add a lot more coverage here for all the other scenarios as this only covers the "happy path". For example:

  • Verify the correct behavior (still under debate) when elasticsearch.yml file is non-writable
  • Verify we don't auto config security if security settings already exist
  • Verify we don't auto config security on an upgrade

There are likely other more specific scenarios that probably are better implemented as unit tests for the CLI tool for which no tests currently yet exist.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to add a lot more coverage here for all the other scenarios

Thanks for the feedback, I'll make sure I'll add test coverage before I raise this for review.

Verify the correct behavior (still under debate) when elasticsearch.yml file is non-writable

What is under debate regarding this ?

There are likely other more specific scenarios that probably are better implemented as unit tests for the CLI tool for which no tests currently yet exist.

Makes sense, I think it's better if we keep comments targeted in relevant PRs so that we don't miss anything.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is under debate regarding this ?

Like I mentioned, @colings86 and I briefly talked about possibly erroring on startup if we fail to auto-config security rather than pressing forward. No decisions were made but we might want to reconfirm that this is indeed the behavior we want.

assertThat(es.config("auto_generated_certs"), file(Directory, "root", "elasticsearch", p755));
Stream.of("http_keystore.p12", "http_truststore.p12", "transport_keystore_all_nodes.p12", "transport_truststore_all_nodes.p12")
.forEach(
keystore -> assertThat(es.config("auto_generated_certs").resolve(keystore), file(File, "root", "elasticsearch", p660))
);
List<String> configLines = Files.readAllLines(es.config("elasticsearch.yml"));
assertThat(configLines, contains("xpack.security.enabled: true"));
assertThat(configLines, contains("xpack.security.enrollment.enabled: true"));
assertThat(configLines, contains("xpack.security.authc.realms.file.auto_generated_1625753263.order: 0"));
jkakavas marked this conversation as resolved.
Show resolved Hide resolved
assertThat(configLines, contains("xpack.security.transport.ssl.enabled: true"));
assertThat(configLines, contains("xpack.security.transport.ssl.verification_mode: certificate"));
assertThat(configLines, contains("xpack.security.transport.ssl.client_authentication: required"));
assertThat(
configLines,
contains(
"xpack.security.transport.ssl.keystore.path: " + "/etc/elasticsearch/auto_generated_certs/transport_keystore_all_nodes.p12"
)
);
assertThat(
configLines,
contains(
"xpack.security.transport.ssl.truststore.path: "
+ "/etc/elasticsearch/auto_generated_certs/transport_truststore_all_nodes.p12"
)
);
assertThat(configLines, contains("xpack.security.http.ssl.enabled: true"));
assertThat(
configLines,
contains("xpack.security.http.ssl.keystore.path: " + "/etc/elasticsearch/auto_generated_certs/http_keystore.p12")
);
assertThat(
configLines,
contains("xpack.security.http.ssl.truststore.path: " + "/etc/elasticsearch/auto_generated_certs/http_truststore.p12")
);
assertThat(configLines, contains("http.host: [_local_, _site_]"));
}

/**
Expand Down
29 changes: 1 addition & 28 deletions server/src/main/java/org/elasticsearch/bootstrap/Bootstrap.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import org.apache.lucene.util.StringHelper;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.Version;
import org.elasticsearch.cli.KeyStoreAwareCommand;
import org.elasticsearch.cli.Terminal;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.PidFile;
Expand Down Expand Up @@ -238,37 +237,11 @@ static SecureSettings loadSecureSettings(Environment initialEnv) throws Bootstra
}

static SecureSettings loadSecureSettings(Environment initialEnv, InputStream stdin) throws BootstrapException {
final KeyStoreWrapper keystore;
try {
keystore = KeyStoreWrapper.load(initialEnv.configFile());
} catch (IOException e) {
throw new BootstrapException(e);
}

SecureString password;
try {
if (keystore != null && keystore.hasPassword()) {
password = readPassphrase(stdin, KeyStoreAwareCommand.MAX_PASSPHRASE_LENGTH);
} else {
password = new SecureString(new char[0]);
}
} catch (IOException e) {
throw new BootstrapException(e);
}

try (password) {
if (keystore == null) {
final KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.create();
keyStoreWrapper.save(initialEnv.configFile(), new char[0]);
return keyStoreWrapper;
} else {
keystore.decrypt(password.getChars());
KeyStoreWrapper.upgrade(keystore, initialEnv.configFile(), password.getChars());
}
return KeyStoreWrapper.bootstrap(initialEnv.configFile(), () -> readPassphrase(stdin, KeyStoreWrapper.MAX_PASSPHRASE_LENGTH));
} catch (Exception e) {
throw new BootstrapException(e);
}
return keystore;
}

// visible for tests
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@ public KeyStoreAwareCommand(String description) {
super(description);
}

/** Arbitrarily chosen maximum passphrase length */
public static final int MAX_PASSPHRASE_LENGTH = 128;

/**
* Reads the keystore password from the {@link Terminal}, prompting for verification where applicable and returns it as a
* {@link SecureString}.
Expand All @@ -42,9 +39,9 @@ protected static SecureString readPassword(Terminal terminal, boolean withVerifi
final char[] passwordArray;
if (withVerification) {
passwordArray = terminal.readSecret("Enter new password for the elasticsearch keystore (empty for no password): ",
MAX_PASSPHRASE_LENGTH);
KeyStoreWrapper.MAX_PASSPHRASE_LENGTH);
char[] passwordVerification = terminal.readSecret("Enter same password again: ",
MAX_PASSPHRASE_LENGTH);
KeyStoreWrapper.MAX_PASSPHRASE_LENGTH);
if (Arrays.equals(passwordArray, passwordVerification) == false) {
throw new UserException(ExitCodes.DATA_ERROR, "Passwords are not equal, exiting.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.apache.lucene.util.SetOnce;
import org.elasticsearch.cli.ExitCodes;
import org.elasticsearch.cli.UserException;
import org.elasticsearch.common.CheckedSupplier;
import org.elasticsearch.common.Randomness;
import org.elasticsearch.common.hash.MessageDigests;

Expand All @@ -45,6 +46,7 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.AccessDeniedException;
import java.nio.file.Files;
import java.nio.file.LinkOption;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.nio.file.attribute.PosixFileAttributeView;
Expand Down Expand Up @@ -72,6 +74,9 @@
*/
public class KeyStoreWrapper implements SecureSettings {

/** Arbitrarily chosen maximum passphrase length */
public static final int MAX_PASSPHRASE_LENGTH = 128;

/** An identifier for the type of data that may be stored in a keystore entry. */
private enum EntryType {
STRING,
Expand Down Expand Up @@ -101,7 +106,7 @@ private static class Entry {
"~!@#$%^&*-_=+?").toCharArray();

/** The name of the keystore file to read and write. */
private static final String KEYSTORE_FILENAME = "elasticsearch.keystore";
public static final String KEYSTORE_FILENAME = "elasticsearch.keystore";

/** The version of the metadata written before the keystore data. */
static final int FORMAT_VERSION = 4;
Expand Down Expand Up @@ -194,6 +199,29 @@ public static void addBootstrapSeed(KeyStoreWrapper wrapper) {
Arrays.fill(characters, (char)0);
}

public static KeyStoreWrapper bootstrap(Path configDir, CheckedSupplier<SecureString, Exception> passwordSupplier) throws Exception {
KeyStoreWrapper keystore = KeyStoreWrapper.load(configDir);

SecureString password;
if (keystore != null && keystore.hasPassword()) {
password = passwordSupplier.get();
} else {
password = new SecureString(new char[0]);
}

try (password) {
if (keystore == null) {
final KeyStoreWrapper keyStoreWrapper = KeyStoreWrapper.create();
keyStoreWrapper.save(configDir, new char[0]);
return keyStoreWrapper;
} else {
keystore.decrypt(password.getChars());
KeyStoreWrapper.upgrade(keystore, configDir, password.getChars());
}
}
return keystore;
}

/**
* Loads information about the Elasticsearch keystore from the provided config directory.
*
Expand Down Expand Up @@ -482,6 +510,7 @@ public synchronized void save(Path configDir, char[] password) throws Exception
Directory directory = new NIOFSDirectory(configDir);
// write to tmp file first, then overwrite
String tmpFile = KEYSTORE_FILENAME + ".tmp";
Path keystoreTempFile = configDir.resolve(tmpFile);
try (IndexOutput output = directory.createOutput(tmpFile, IOContext.DEFAULT)) {
CodecUtil.writeHeader(output, KEYSTORE_FILENAME, FORMAT_VERSION);
output.writeByte(password.length == 0 ? (byte)0 : (byte)1);
Expand Down Expand Up @@ -515,18 +544,31 @@ public synchronized void save(Path configDir, char[] password) throws Exception
final String message = String.format(
Locale.ROOT,
"unable to create temporary keystore at [%s], write permissions required for [%s] or run [elasticsearch-keystore upgrade]",
configDir.resolve(tmpFile),
keystoreTempFile,
configDir);
Files.deleteIfExists(keystoreTempFile);
throw new UserException(ExitCodes.CONFIG, message, e);
}

Path keystoreFile = keystorePath(configDir);
Files.move(configDir.resolve(tmpFile), keystoreFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
PosixFileAttributeView attrs = Files.getFileAttributeView(keystoreFile, PosixFileAttributeView.class);
// check that replace doesn't change the owner
if (Files.exists(keystoreFile, LinkOption.NOFOLLOW_LINKS) &&
false == Files.getOwner(keystoreTempFile, LinkOption.NOFOLLOW_LINKS).equals(Files.getOwner(keystoreFile,
LinkOption.NOFOLLOW_LINKS))) {
Files.deleteIfExists(keystoreTempFile);
final String message = String.format(
Locale.ROOT,
"will not overwrite keystore at [%s], because this incurs changing the file owner",
keystoreFile,
configDir);
throw new UserException(ExitCodes.CONFIG, message);
}
PosixFileAttributeView attrs = Files.getFileAttributeView(keystoreTempFile, PosixFileAttributeView.class);
if (attrs != null) {
// don't rely on umask: ensure the keystore has minimal permissions
attrs.setPermissions(PosixFilePermissions.fromString("rw-rw----"));
}
Files.move(keystoreTempFile, keystoreFile, StandardCopyOption.REPLACE_EXISTING, StandardCopyOption.ATOMIC_MOVE);
}

/**
Expand Down Expand Up @@ -584,7 +626,7 @@ public static void validateSettingName(String setting) {
/**
* Set a string setting.
*/
synchronized void setString(String setting, char[] value) {
public synchronized void setString(String setting, char[] value) {
ensureOpen();
validateSettingName(setting);

Expand Down
Loading