暫無描述

0-wazuh-init 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/with-contenv bash
  2. # Wazuh App Copyright (C) 2017, Wazuh Inc. (License GPLv2)
  3. WAZUH_INSTALL_PATH=/var/ossec
  4. WAZUH_CONFIG_MOUNT=/wazuh-config-mount
  5. WAZUH_MANAGER_SERVER=$WAZUH_MANAGER_SERVER
  6. WAZUH_MANAGER_PORT=${WAZUH_MANAGER_PORT:-"1514"}
  7. WAZUH_REGISTRATION_SERVER=${WAZUH_REGISTRATION_SERVER:-$WAZUH_MANAGER_SERVER}
  8. WAZUH_REGISTRATION_PORT=${WAZUH_REGISTRATION_PORT:-"1515"}
  9. WAZUH_REGISTRATION_PASSWORD=$WAZUH_REGISTRATION_PASSWORD
  10. WAZUH_AGENT_NAME=${WAZUH_AGENT_NAME:-"wazuh-agent-$HOSTNAME"}
  11. WAZUH_AGENT_GROUPS=${WAZUH_AGENT_GROUPS:-"default"}
  12. ##############################################################################
  13. # Aux functions
  14. ##############################################################################
  15. print() {
  16. echo -e $1
  17. }
  18. error_and_exit() {
  19. echo "Error executing command: '$1'."
  20. echo 'Exiting.'
  21. exit 1
  22. }
  23. exec_cmd() {
  24. eval $1 > /dev/null 2>&1 || error_and_exit "$1"
  25. }
  26. exec_cmd_stdout() {
  27. eval $1 2>&1 || error_and_exit "$1"
  28. }
  29. ##############################################################################
  30. # Copy all files from $WAZUH_CONFIG_MOUNT to $WAZUH_INSTALL_PATH and respect
  31. # destination files permissions
  32. #
  33. # For example, to mount the file /var/ossec/data/etc/ossec.conf, mount it at
  34. # $WAZUH_CONFIG_MOUNT/etc/ossec.conf in your container and this code will
  35. # replace the ossec.conf file in /var/ossec/data/etc with yours.
  36. ##############################################################################
  37. mount_files() {
  38. if [ -e "$WAZUH_CONFIG_MOUNT" ]
  39. then
  40. print "Identified Wazuh configuration files to mount..."
  41. exec_cmd_stdout "cp --verbose -r $WAZUH_CONFIG_MOUNT/* $WAZUH_INSTALL_PATH"
  42. else
  43. print "No Wazuh configuration files to mount..."
  44. fi
  45. }
  46. ##############################################################################
  47. # Allow users to set the manager ip and port, enrollment ip and port and
  48. # enroll dynamically on container start.
  49. #
  50. # To use this:
  51. # 1. Create your own ossec.conf file
  52. # 2. In your ossec.conf file, use the <agent> configuration
  53. # 3. Mount your custom ossec.conf file at $WAZUH_CONFIG_MOUNT/etc/ossec.conf
  54. ##############################################################################
  55. set_manager_conn() {
  56. echo "ossec.conf configuration"
  57. sed -i "s#<address>CHANGE_MANAGER_IP</address>#<address>$WAZUH_MANAGER_SERVER</address>#g" ${WAZUH_INSTALL_PATH}/etc/ossec.conf
  58. sed -i "s#<port>CHANGE_MANAGER_PORT</port>#<port>$WAZUH_MANAGER_PORT</port>#g" ${WAZUH_INSTALL_PATH}/etc/ossec.conf
  59. sed -i "s#<manager_address>CHANGE_ENROLL_IP</manager_address>#<manager_address>$WAZUH_REGISTRATION_SERVER</manager_address>#g" ${WAZUH_INSTALL_PATH}/etc/ossec.conf
  60. sed -i "s#<port>CHANGE_ENROLL_PORT</port>#<port>$WAZUH_REGISTRATION_PORT</port>#g" ${WAZUH_INSTALL_PATH}/etc/ossec.conf
  61. sed -i "s#<agent_name>CHANGE_AGENT_NAME</agent_name>#<agent_name>$WAZUH_AGENT_NAME</agent_name>#g" ${WAZUH_INSTALL_PATH}/etc/ossec.conf
  62. sed -i "s#<groups>CHANGE_AGENT_GROUPS</groups>#<groups>$WAZUH_AGENT_GROUPS</groups>#g" ${WAZUH_INSTALL_PATH}/etc/ossec.conf
  63. [ -n "$WAZUH_REGISTRATION_PASSWORD" ] && \
  64. echo "$WAZUH_REGISTRATION_PASSWORD" > ${WAZUH_INSTALL_PATH}/etc/authd.pass && \
  65. chown root:wazuh ${WAZUH_INSTALL_PATH}/etc/authd.pass && \
  66. chmod 640 ${WAZUH_INSTALL_PATH}/etc/authd.pass
  67. }
  68. ##############################################################################
  69. # Main function
  70. ##############################################################################
  71. main() {
  72. # Mount selected files (WAZUH_CONFIG_MOUNT) to container
  73. mount_files
  74. # Configure agent variables
  75. set_manager_conn
  76. }
  77. main