|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Wazuh-indexer securityadmin wrapper |
| 4 | +# Copyright (C) 2022, Wazuh Inc. |
| 5 | +# |
| 6 | +# This program is a free software; you can redistribute it |
| 7 | +# and/or modify it under the terms of the GNU General Public |
| 8 | +# License (version 2) as published by the FSF - Free Software |
| 9 | +# Foundation. |
| 10 | + |
| 11 | +CONFIG_PATH="/etc/wazuh-indexer" |
| 12 | + |
| 13 | +if [ ! -d "${CONFIG_PATH}" ]; then |
| 14 | + echo "ERROR: it was not possible to find ${CONFIG_PATH}" |
| 15 | + exit 1 |
| 16 | +fi |
| 17 | + |
| 18 | +CONFIG_FILE="${CONFIG_PATH}/opensearch.yml" |
| 19 | + |
| 20 | +if [ ! -f "${CONFIG_FILE}" ]; then |
| 21 | + echo "ERROR: it was not possible to find ${CONFIG_FILE}" |
| 22 | + exit 1 |
| 23 | +fi |
| 24 | + |
| 25 | +INSTALL_PATH="/usr/share/wazuh-indexer" |
| 26 | + |
| 27 | +if [ ! -d "${INSTALL_PATH}" ]; then |
| 28 | + echo "ERROR: it was not possible to find ${INSTALL_PATH}" |
| 29 | + exit 1 |
| 30 | +fi |
| 31 | + |
| 32 | +HOST="" |
| 33 | +OPTIONS="-icl -nhnv" |
| 34 | +WAZUH_INDEXER_ROOT_CA="$(cat ${CONFIG_FILE} 2>&1 | grep http.pemtrustedcas | sed 's/.*: //' | tr -d "[\"\']")" |
| 35 | +WAZUH_INDEXER_ADMIN_PATH="$(dirname "${WAZUH_INDEXER_ROOT_CA}" 2>&1)" |
| 36 | +SECURITY_PATH="${INSTALL_PATH}/plugins/opensearch-security" |
| 37 | +SECURITY_CONFIG_PATH="${CONFIG_PATH}/opensearch-security" |
| 38 | + |
| 39 | +# ----------------------------------------------------------------------------- |
| 40 | + |
| 41 | +trap ctrl_c INT |
| 42 | + |
| 43 | +clean(){ |
| 44 | + |
| 45 | + exit_code=$1 |
| 46 | + indexer_process_id=$(pgrep -f wazuh-indexer -c) |
| 47 | + if [ "${indexer_process_id}" -gt 1 ]; then |
| 48 | + pkill -n -f wazuh-indexer |
| 49 | + fi |
| 50 | + exit "${exit_code}" |
| 51 | + |
| 52 | +} |
| 53 | + |
| 54 | +ctrl_c() { |
| 55 | + clean 1 |
| 56 | +} |
| 57 | + |
| 58 | +# ----------------------------------------------------------------------------- |
| 59 | + |
| 60 | +getNetworkHost() { |
| 61 | + |
| 62 | + HOST=$(grep -hr "network.host:" "${CONFIG_FILE}" 2>&1) |
| 63 | + NH="network.host: " |
| 64 | + HOST="${HOST//$NH}" |
| 65 | + HOST=$(echo "${HOST}" | tr -d "[\"\']") |
| 66 | + |
| 67 | + isIP=$(echo "${HOST}" | grep -P "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$") |
| 68 | + isDNS=$(echo "${HOST}" | grep -P "^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$") |
| 69 | + |
| 70 | + # Allow to find ip with an interface |
| 71 | + if [ -z "${isIP}" ] && [ -z "${isDNS}" ]; then |
| 72 | + interface="${HOST//_}" |
| 73 | + HOST=$(ip -o -4 addr list "${interface}" | awk '{print $4}' | cut -d/ -f1) |
| 74 | + fi |
| 75 | + |
| 76 | + if [ "${HOST}" = "0.0.0.0" ]; then |
| 77 | + HOST="127.0.0.1" |
| 78 | + fi |
| 79 | + |
| 80 | + if [ -z "${HOST}" ]; then |
| 81 | + echo "ERROR: network host not valid, check ${CONFIG_FILE}" |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | + |
| 85 | +} |
| 86 | + |
| 87 | +# ----------------------------------------------------------------------------- |
| 88 | +getPort() { |
| 89 | + |
| 90 | + PORT=$(grep -hr 'transport.tcp.port' "${CONFIG_FILE}" 2>&1) |
| 91 | + if [ "${PORT}" ]; then |
| 92 | + PORT=$(echo "${PORT}" | cut -d' ' -f2 | cut -d'-' -f1) |
| 93 | + else |
| 94 | + PORT="9200" |
| 95 | + fi |
| 96 | + PORT=$(echo "${PORT}" | tr -d "[\"\']") |
| 97 | + |
| 98 | +} |
| 99 | +# ----------------------------------------------------------------------------- |
| 100 | + |
| 101 | +securityadmin() { |
| 102 | + |
| 103 | + if [ ! -d "${SECURITY_PATH}" ]; then |
| 104 | + echo "ERROR: it was not possible to find ${SECURITY_PATH}" |
| 105 | + exit 1 |
| 106 | + elif [ ! -d "${INSTALL_PATH}/jdk" ]; then |
| 107 | + echo "ERROR: it was not possible to find ${INSTALL_PATH}/jdk" |
| 108 | + exit 1 |
| 109 | + fi |
| 110 | + |
| 111 | + if [ -f "${WAZUH_INDEXER_ADMIN_PATH}/admin.pem" ] && [ -f "${WAZUH_INDEXER_ADMIN_PATH}/admin-key.pem" ] && [ -f "${WAZUH_INDEXER_ROOT_CA}" ]; then |
| 112 | + OPENSEARCH_CONF_DIR="${CONFIG_PATH}" JAVA_HOME="${INSTALL_PATH}/jdk" runuser wazuh-indexer --shell="/bin/bash" --command="${SECURITY_PATH}/tools/securityadmin.sh -cd ${SECURITY_CONFIG_PATH} -cacert ${WAZUH_INDEXER_ROOT_CA} -cert ${WAZUH_INDEXER_ADMIN_PATH}/admin.pem -key ${WAZUH_INDEXER_ADMIN_PATH}/admin-key.pem -h ${HOST} -p ${PORT} ${OPTIONS}" |
| 113 | + else |
| 114 | + echo "ERROR: this tool try to find admin.pem and admin-key.pem in ${WAZUH_INDEXER_ADMIN_PATH} but it couldn't. In this case, you must run manually the Indexer security initializer by running the command: JAVA_HOME="/usr/share/wazuh-indexer/jdk" runuser wazuh-indexer --shell="/bin/bash" --command="/usr/share/wazuh-indexer/plugins/opensearch-security/tools/securityadmin.sh -cd /etc/wazuh-indexer/opensearch-security -cacert /path/to/root-ca.pem -cert /path/to/admin.pem -key /path/to/admin-key.pem -h ${HOST} -p ${PORT} ${OPTIONS}" replacing /path/to/ by your certificates path." |
| 115 | + exit 1 |
| 116 | + fi |
| 117 | + |
| 118 | +} |
| 119 | + |
| 120 | +help() { |
| 121 | + echo |
| 122 | + echo "Usage: $0 [OPTIONS]" |
| 123 | + echo |
| 124 | + echo " -ho, --host <host> [Optional] Target IP or DNS to configure security." |
| 125 | + echo " --port <port> [Optional] wazuh-indexer security port." |
| 126 | + echo " --options <options> [Optional] Custom securityadmin options." |
| 127 | + echo " -h, --help Show this help." |
| 128 | + echo |
| 129 | + exit "$1" |
| 130 | +} |
| 131 | + |
| 132 | + |
| 133 | +main() { |
| 134 | + |
| 135 | + getNetworkHost |
| 136 | + getPort |
| 137 | + |
| 138 | + while [ -n "$1" ] |
| 139 | + do |
| 140 | + case "$1" in |
| 141 | + "-h"|"--help") |
| 142 | + help 0 |
| 143 | + ;; |
| 144 | + "-ho"|"--host") |
| 145 | + if [ -n "$2" ]; then |
| 146 | + HOST="$2" |
| 147 | + HOST=$(echo "${HOST}" | tr -d "[\"\']") |
| 148 | + isIP=$(echo "${2}" | grep -P "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$") |
| 149 | + isDNS=$(echo "${2}" | grep -P "^[a-zA-Z0-9][a-zA-Z0-9-]{1,61}[a-zA-Z0-9](?:\.[a-zA-Z]{2,})+$") |
| 150 | + if [[ -z "${isIP}" ]] && [[ -z "${isDNS}" ]]; then |
| 151 | + echo "The given information does not match with an IP address or a DNS." |
| 152 | + exit 1 |
| 153 | + fi |
| 154 | + shift 2 |
| 155 | + else |
| 156 | + help 1 |
| 157 | + fi |
| 158 | + ;; |
| 159 | + "--port") |
| 160 | + if [ -n "$2" ]; then |
| 161 | + PORT="$2" |
| 162 | + PORT=$(echo "${PORT}" | tr -d "[\"\']") |
| 163 | + if [[ -z $(echo "${2}" | grep -P "^([1-9][0-9]{0,3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$") ]]; then |
| 164 | + echo "The given information does not match with a valid PORT number." |
| 165 | + exit 1 |
| 166 | + fi |
| 167 | + shift 2 |
| 168 | + else |
| 169 | + help 1 |
| 170 | + fi |
| 171 | + ;; |
| 172 | + "--options") |
| 173 | + if [ -n "$2" ]; then |
| 174 | + OPTIONS="$2" |
| 175 | + shift 2 |
| 176 | + else |
| 177 | + help 1 |
| 178 | + fi |
| 179 | + ;; |
| 180 | + *) |
| 181 | + help 1 |
| 182 | + esac |
| 183 | + done |
| 184 | + |
| 185 | + securityadmin |
| 186 | + |
| 187 | +} |
| 188 | + |
| 189 | +main "$@" |
0 commit comments