|
| 1 | +#!/usr/bin/env bash |
| 2 | +# Wazuh Docker Copyright (C) 2017, Wazuh Inc. (License GPLv2) |
| 3 | +set -e |
| 4 | + |
| 5 | +umask 0002 |
| 6 | + |
| 7 | +# Constants |
| 8 | +INDEXER_HOME=/usr/share/wazuh-indexer |
| 9 | +OPENSEARCH_PATH_CONF=${INDEXER_HOME}/config |
| 10 | +JAVA_HOME=${INDEXER_HOME}/jdk |
| 11 | + |
| 12 | +# DISCOVERY=$(grep -oP "(?<=discovery.type: ).*" ${OPENSEARCH_PATH_CONF}/opensearch.yml) |
| 13 | + |
| 14 | +# Export variables to environment |
| 15 | +export INDEXER_HOME |
| 16 | +export OPENSEARCH_PATH_CONF |
| 17 | +export JAVA_HOME |
| 18 | + |
| 19 | +run_as_other_user_if_needed() { |
| 20 | + if [[ "$(id -u)" == "0" ]]; then |
| 21 | + # If running as root, drop to specified UID and run command |
| 22 | + exec chroot --userspec=1000:0 / "${@}" |
| 23 | + else |
| 24 | + # Either we are running in Openshift with random uid and are a member of the root group |
| 25 | + # or with a custom --user |
| 26 | + exec "${@}" |
| 27 | + fi |
| 28 | +} |
| 29 | + |
| 30 | +# Allow user specify custom CMD, maybe bin/opensearch itself |
| 31 | +# for example to directly specify `-E` style parameters for opensearch on k8s |
| 32 | +# or simply to run /bin/bash to check the image |
| 33 | +if [[ "$1" != "opensearchwrapper" ]]; then |
| 34 | + if [[ "$(id -u)" == "0" && $(basename "$1") == "opensearch" ]]; then |
| 35 | + # Rewrite CMD args to replace $1 with `opensearch` explicitly, |
| 36 | + # Without this, user could specify `opensearch -E x.y=z` but |
| 37 | + # `bin/opensearch -E x.y=z` would not work. |
| 38 | + set -- "opensearch" "${@:2}" |
| 39 | + # Use chroot to switch to UID 1000 / GID 0 |
| 40 | + exec chroot --userspec=1000:0 / "$@" |
| 41 | + else |
| 42 | + # User probably wants to run something else, like /bin/bash, with another uid forced (Openshift?) |
| 43 | + exec "$@" |
| 44 | + fi |
| 45 | +fi |
| 46 | + |
| 47 | +# Allow environment variables to be set by creating a file with the |
| 48 | +# contents, and setting an environment variable with the suffix _FILE to |
| 49 | +# point to it. This can be used to provide secrets to a container, without |
| 50 | +# the values being specified explicitly when running the container. |
| 51 | +# |
| 52 | +# This is also sourced in opensearch-env, and is only needed here |
| 53 | +# as well because we use INDEXER_PASSWORD below. Sourcing this script |
| 54 | +# is idempotent. |
| 55 | +source /usr/share/wazuh-indexer/bin/opensearch-env-from-file |
| 56 | + |
| 57 | +if [[ -f bin/opensearch-users ]]; then |
| 58 | + # Check for the INDEXER_PASSWORD environment variable to set the |
| 59 | + # bootstrap password for Security. |
| 60 | + # |
| 61 | + # This is only required for the first node in a cluster with Security |
| 62 | + # enabled, but we have no way of knowing which node we are yet. We'll just |
| 63 | + # honor the variable if it's present. |
| 64 | + if [[ -n "$INDEXER_PASSWORD" ]]; then |
| 65 | + [[ -f /usr/share/wazuh-indexer/opensearch.keystore ]] || (run_as_other_user_if_needed opensearch-keystore create) |
| 66 | + if ! (run_as_other_user_if_needed opensearch-keystore has-passwd --silent); then |
| 67 | + # keystore is unencrypted |
| 68 | + if ! (run_as_other_user_if_needed opensearch-keystore list | grep -q '^bootstrap.password$'); then |
| 69 | + (run_as_other_user_if_needed echo "$INDEXER_PASSWORD" | opensearch-keystore add -x 'bootstrap.password') |
| 70 | + fi |
| 71 | + else |
| 72 | + # keystore requires password |
| 73 | + if ! (run_as_other_user_if_needed echo "$KEYSTORE_PASSWORD" | |
| 74 | + opensearch-keystore list | grep -q '^bootstrap.password$'); then |
| 75 | + COMMANDS="$(printf "%s\n%s" "$KEYSTORE_PASSWORD" "$INDEXER_PASSWORD")" |
| 76 | + (run_as_other_user_if_needed echo "$COMMANDS" | opensearch-keystore add -x 'bootstrap.password') |
| 77 | + fi |
| 78 | + fi |
| 79 | + fi |
| 80 | +fi |
| 81 | + |
| 82 | +if [[ "$(id -u)" == "0" ]]; then |
| 83 | + # If requested and running as root, mutate the ownership of bind-mounts |
| 84 | + if [[ -n "$TAKE_FILE_OWNERSHIP" ]]; then |
| 85 | + chown -R 1000:0 /usr/share/wazuh-indexer/{data,logs} |
| 86 | + fi |
| 87 | +fi |
| 88 | + |
| 89 | +# Initialize security |
| 90 | +nohup /securityadmin.sh & |
| 91 | + |
| 92 | +#if [[ "$DISCOVERY" == "single-node" ]] && [[ ! -f "/var/lib/wazuh-indexer/.flag" ]]; then |
| 93 | +# run securityadmin.sh for single node with CACERT, CERT and KEY parameter |
| 94 | +# nohup /securityadmin.sh & |
| 95 | +# touch "/var/lib/wazuh-indexer/.flag" |
| 96 | +#fi |
| 97 | + |
| 98 | +run_as_other_user_if_needed /usr/share/wazuh-indexer/bin/opensearch <<<"$KEYSTORE_PASSWORD" |
0 commit comments