bootstrap_password.bash 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #!/usr/bin/env bats
  2. # Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
  3. # or more contributor license agreements. Licensed under the Elastic License;
  4. # you may not use this file except in compliance with the Elastic License.
  5. load $BATS_UTILS/utils.bash
  6. load $BATS_UTILS/plugins.bash
  7. load $BATS_UTILS/xpack.bash
  8. setup() {
  9. if [ $BATS_TEST_NUMBER == 1 ]; then
  10. export PACKAGE_NAME="elasticsearch"
  11. clean_before_test
  12. install
  13. set_debug_logging
  14. generate_trial_license
  15. verify_xpack_installation
  16. fi
  17. }
  18. if [[ "$BATS_TEST_FILENAME" =~ 20_tar_bootstrap_password.bats$ ]]; then
  19. load $BATS_UTILS/tar.bash
  20. GROUP='TAR BOOTSTRAP PASSWORD'
  21. install() {
  22. install_archive
  23. verify_archive_installation
  24. }
  25. export ESHOME=/tmp/elasticsearch
  26. export_elasticsearch_paths
  27. export ESPLUGIN_COMMAND_USER=elasticsearch
  28. else
  29. load $BATS_UTILS/packages.bash
  30. if is_rpm; then
  31. GROUP='RPM BOOTSTRAP PASSWORD'
  32. elif is_dpkg; then
  33. GROUP='DEB BOOTSTRAP PASSWORD'
  34. fi
  35. export_elasticsearch_paths
  36. export ESPLUGIN_COMMAND_USER=root
  37. install() {
  38. install_package
  39. verify_package_installation
  40. }
  41. fi
  42. @test "[$GROUP] add bootstrap.password setting" {
  43. if [[ -f /tmp/bootstrap.password ]]; then
  44. sudo rm -f /tmp/bootstrap.password
  45. fi
  46. run sudo -E -u $ESPLUGIN_COMMAND_USER bash <<"NEW_PASS"
  47. if [[ ! -f $ESCONFIG/elasticsearch.keystore ]]; then
  48. $ESHOME/bin/elasticsearch-keystore create
  49. fi
  50. cat /dev/urandom | tr -dc "[a-zA-Z0-9]" | fold -w 20 | head -n 1 > /tmp/bootstrap.password
  51. cat /tmp/bootstrap.password | $ESHOME/bin/elasticsearch-keystore add --stdin bootstrap.password
  52. NEW_PASS
  53. [ "$status" -eq 0 ] || {
  54. echo "Expected elasticsearch-keystore tool exit code to be zero but got [$status]"
  55. echo "$output"
  56. false
  57. }
  58. assert_file_exist "/tmp/bootstrap.password"
  59. }
  60. @test "[$GROUP] test bootstrap.password is in setting list" {
  61. run sudo -E -u $ESPLUGIN_COMMAND_USER bash <<"NODE_SETTINGS"
  62. cat >> $ESCONFIG/elasticsearch.yml <<- EOF
  63. network.host: 127.0.0.1
  64. http.port: 9200
  65. EOF
  66. NODE_SETTINGS
  67. run_elasticsearch_service 0
  68. wait_for_xpack 127.0.0.1 9200
  69. sudo -E -u $ESPLUGIN_COMMAND_USER "$ESHOME/bin/elasticsearch-keystore" list | grep "bootstrap.password"
  70. password=$(cat /tmp/bootstrap.password)
  71. clusterHealth=$(sudo curl -u "elastic:$password" -H "Content-Type: application/json" \
  72. -XGET "http://127.0.0.1:9200/_cluster/health?wait_for_status=green&timeout=30s")
  73. echo "$clusterHealth" | grep '"status":"green"' || {
  74. echo "Expected cluster health to be green but got:"
  75. echo "$clusterHealth"
  76. false
  77. }
  78. }
  79. @test "[$GROUP] test auto generated passwords with modified bootstrap.password" {
  80. if [[ -f /tmp/setup-passwords-output-with-bootstrap ]]; then
  81. sudo rm -f /tmp/setup-passwords-output-with-bootstrap
  82. fi
  83. run sudo -E -u $ESPLUGIN_COMMAND_USER bash <<"SETUP_OK"
  84. echo 'y' | $ESHOME/bin/elasticsearch-setup-passwords auto
  85. SETUP_OK
  86. echo "$output" > /tmp/setup-passwords-output-with-bootstrap
  87. [ "$status" -eq 0 ] || {
  88. echo "Expected x-pack elasticsearch-setup-passwords tool exit code to be zero but got [$status]"
  89. cat /tmp/setup-passwords-output-with-bootstrap
  90. debug_collect_logs
  91. false
  92. }
  93. curl -s -XGET 'http://127.0.0.1:9200' | grep "missing authentication credentials for REST"
  94. # Disable bash history expansion because passwords can contain "!"
  95. set +H
  96. users=( elastic kibana logstash_system )
  97. for user in "${users[@]}"; do
  98. grep "Changed password for user $user" /tmp/setup-passwords-output-with-bootstrap || {
  99. echo "Expected x-pack elasticsearch-setup-passwords tool to change password for user [$user]:"
  100. cat /tmp/setup-passwords-output-with-bootstrap
  101. false
  102. }
  103. password=$(grep "PASSWORD $user = " /tmp/setup-passwords-output-with-bootstrap | sed "s/PASSWORD $user = //")
  104. curl -u "$user:$password" -XGET 'http://127.0.0.1:9200' | grep "You Know, for Search"
  105. basic=$(echo -n "$user:$password" | base64)
  106. curl -H "Authorization: Basic $basic" -XGET 'http://127.0.0.1:9200' | grep "You Know, for Search"
  107. done
  108. set -H
  109. }
  110. @test "[$GROUP] test elasticsearch-sql-cli" {
  111. password=$(grep "PASSWORD elastic = " /tmp/setup-passwords-output-with-bootstrap | sed "s/PASSWORD elastic = //")
  112. curl -s -u "elastic:$password" -H "Content-Type: application/json" -XPUT 'localhost:9200/library/_doc/1?refresh&pretty' -d'{
  113. "name": "Ender'"'"'s Game",
  114. "author": "Orson Scott Card",
  115. "release_date": "1985-06-01",
  116. "page_count": 324
  117. }'
  118. password=$(grep "PASSWORD elastic = " /tmp/setup-passwords-output-with-bootstrap | sed "s/PASSWORD elastic = //")
  119. run $ESHOME/bin/elasticsearch-sql-cli --debug "http://elastic@127.0.0.1:9200" <<SQL
  120. $password
  121. SELECT * FROM library;
  122. SQL
  123. [ "$status" -eq 0 ] || {
  124. echo "SQL cli failed:\n$output"
  125. false
  126. }
  127. [[ "$output" == *"Card"* ]] || {
  128. echo "Failed to find author [Card] in library:$output"
  129. false
  130. }
  131. }
  132. @test "[$GROUP] test elasticsearch-sql-cli when user refuses password" {
  133. # Run with empty stdin
  134. run $ESHOME/bin/elasticsearch-sql-cli --debug "http://elastic@127.0.0.1:9200" <<SQL
  135. SQL
  136. [ "$status" -eq 77 ] || { #NOPERM
  137. echo "SQL cli failed:\n$output"
  138. false
  139. }
  140. [[ "$output" == *"password required"* ]] || {
  141. echo "Failed to find author [password required] in error:$output"
  142. false
  143. }
  144. }
  145. @test "[$GROUP] stop Elasticsearch" {
  146. stop_elasticsearch_service
  147. }