Skip to content

Commit

Permalink
Merge branch '4.10.1' of github.com:wazuh/wazuh-virtual-machines into…
Browse files Browse the repository at this point in the history
… merge2-4.10.0-into-4.10.1
  • Loading branch information
c-bordon committed Nov 4, 2024
2 parents ed02849 + 5a682d6 commit adc57af
Show file tree
Hide file tree
Showing 6 changed files with 203 additions and 74 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/builder_OVA.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ on:
WAZUH_VIRTUAL_MACHINES_REFERENCE:
description: 'Branch or tag of the wazuh-virtual-machines repository'
required: true
default: '4.10.0'
default: '4.10.1'
WAZUH_INSTALLATION_ASSISTANT_REFERENCE:
description: 'Branch or tag of the wazuh-installation-assistant repository'
required: true
default: '4.10.0'
default: '4.10.1'
WAZUH_PACKAGE_REPOSITORY:
type: choice
description: 'Wazuh package repository from which to download the packages'
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/packages_builder_ami.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ on:
WAZUH_VIRTUAL_MACHINES_REFERENCE:
description: 'Branch or tag of the wazuh-virtual-machines repository'
required: true
default: '4.10.0'
default: '4.10.1'
WAZUH_AUTOMATION_REFERENCE:
description: 'Branch or tag of the wazuh-automation repository'
required: true
default: '4.10.0'
default: '4.10.1'
WAZUH_INSTALLATION_ASSISTANT_REFERENCE:
description: 'Branch or tag of the wazuh-installation-assistant repository'
required: true
default: '4.10.0'
default: '4.10.1'
VERBOSITY:
description: 'Verbosity level on playbooks execution'
required: true
Expand Down
18 changes: 18 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
# Change Log
All notable changes to this project will be documented in this file.

## [4.10.1]

### Added

- None

### Changed

- Improvements to AMI customization script. ([#98](https://github.com/wazuh/wazuh-virtual-machines/pull/98))

### Fixed

- None

### Deleted

- None

## [4.10.0]

### Added
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.10.0
4.10.1
77 changes: 9 additions & 68 deletions ami/playbooks/build_ami_packages.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -244,75 +244,15 @@
- wazuh-indexer
- wazuh-dashboard

- name: Create changePassword script file
file:
dest: "/etc/.changePasswords.sh"
state: touch
mode: +x

- name: Add script to change passwords on boot
blockinfile:
path: "/etc/.changePasswords.sh"
block: |
#!/bin/bash
mv /home/{{ username }}/.ssh/authorized_keys /home/{{ username }}/.ssh/.authorized_keys_ori
until $(sudo curl -XGET https://localhost:9200/ -uadmin:admin -k --max-time 120 --silent --output /dev/null); do
sleep 10
done
bash /etc/.wazuh-certs-tool.sh -A
mv -f /etc/wazuh-certificates/wazuh-indexer.pem /etc/wazuh-indexer/certs/wazuh-indexer.pem
mv -f /etc/wazuh-certificates/wazuh-indexer-key.pem /etc/wazuh-indexer/certs/wazuh-indexer-key.pem
mv -f /etc/wazuh-certificates/admin.pem /etc/wazuh-indexer/certs/admin.pem
mv -f /etc/wazuh-certificates/admin-key.pem /etc/wazuh-indexer/certs/admin-key.pem
cp /etc/wazuh-certificates/root-ca.pem /etc/wazuh-indexer/certs/root-ca.pem
chmod 500 /etc/wazuh-indexer/certs
chmod 400 /etc/wazuh-indexer/certs/*
chown -R wazuh-indexer:wazuh-indexer /etc/wazuh-indexer/certs
systemctl restart wazuh-indexer
/usr/share/wazuh-indexer/bin/indexer-security-init.sh
mv -f /etc/wazuh-certificates/wazuh-server.pem /etc/filebeat/certs/wazuh-server.pem
mv -f /etc/wazuh-certificates/wazuh-server-key.pem /etc/filebeat/certs/wazuh-server-key.pem
cp /etc/wazuh-certificates/root-ca.pem /etc/filebeat/certs/root-ca.pem
chmod 500 /etc/filebeat/certs
chmod 400 /etc/filebeat/certs/*
chown -R root:root /etc/filebeat/certs
systemctl restart filebeat
rm -f /var/ossec/api/configuration/security/*_key.pem
rm -f /var/ossec/api/configuration/ssl/server.*
systemctl restart wazuh-manager
mv -f /etc/wazuh-certificates/wazuh-dashboard.pem /etc/wazuh-dashboard/certs/wazuh-dashboard.pem
mv -f /etc/wazuh-certificates/wazuh-dashboard-key.pem /etc/wazuh-dashboard/certs/wazuh-dashboard-key.pem
cp /etc/wazuh-certificates/root-ca.pem /etc/wazuh-dashboard/certs/root-ca.pem
chmod 500 /etc/wazuh-dashboard/certs
chmod 400 /etc/wazuh-dashboard/certs/*
chown -R wazuh-dashboard:wazuh-dashboard /etc/wazuh-dashboard/certs
systemctl restart wazuh-dashboard
rm -rf /etc/wazuh-certificates /etc/.wazuh-certs-tool.sh /etc/config.yml /etc/wazuh-certificates-tool.log
new_password=$(ec2-metadata | grep "instance-id" | cut -d":" -f2 | tr -d " "| awk '{print toupper(substr($0,1,1)) substr($0,2)}')
sed -i "s/password:.*/password: ${new_password}/g" /etc/.wazuh-install-files/{{ passwords_file_name }}
bash /etc/.wazuh-passwords-tool.sh -a -A -au wazuh -ap wazuh -f /etc/.wazuh-install-files/{{ passwords_file_name }}
systemctl restart wazuh-dashboard
rm -f /etc/.changePasswords.sh /etc/.wazuh-passwords-tool.sh /etc/.wazuh-install-files/{{ passwords_file_name }} /var/log/wazuh-passwords-tool.log
rmdir /etc/.wazuh-install-files
until $(sudo curl -XGET https://localhost:9200/ -uadmin:${new_password} -k --max-time 120 --silent --output /dev/null); do
sleep 10
done
sed -i "/#Ansible: Change Passwords/,//d" /var/spool/cron/root
mv /home/{{ username }}/.ssh/.authorized_keys_ori /home/{{ username }}/.ssh/authorized_keys
- name: Add change password to crontab
- name: Copy wazuh-ami-customizer.sh script
copy:
src: "../wazuh-ami-customizer.sh"
dest: "/etc/.wazuh-ami-customizer.sh"

- name: Add Wazuh AMI Customizer to crontab
cron:
name: Change Passwords
job: ". /etc/.changePasswords.sh"
name: Wazuh AMI Customizer
job: ". /etc/.wazuh-ami-customizer.sh"
special_time: reboot

- name: Change SSH port to 22
Expand Down Expand Up @@ -386,6 +326,7 @@

- name: Clean logs
shell: |
rm -rf /var/log/wazuh-install.log
find /var/log/ -type f -exec bash -c 'cat /dev/null > {}' \;
find /var/ossec/logs -type f -execdir sh -c 'cat /dev/null > "$1"' _ {} \;
find /var/log/wazuh-indexer -type f -execdir sh -c 'cat /dev/null > "$1"' _ {} \;
Expand Down
170 changes: 170 additions & 0 deletions ami/wazuh-ami-customizer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
#!/bin/bash
# This script is used to configure the Wazuh environment after the installation

# Variables
logfile="/var/log/wazuh-ami-customizer.log"
debug="| tee -a ${logfile}"
function logger(){
now=$(date +'%d/%m/%Y %H:%M:%S')
mtype="INFO:"
if [ -n "${1}" ]; then
while [ -n "${1}" ]; do
case ${1} in
"-e")
mtype="ERROR:"
shift 1
;;
"-w")
mtype="WARNING:"
shift 1
;;
*)
message="${1}"
shift 1
;;
esac
done
fi
printf "%s\n" "${now} ${mtype} ${message}" | tee -a "${logfile}"
}

logger "Starting Wazuh AMI Customizer"

logger "Moving authorized_keys file to a temporary location"

mv /home/wazuh-user/.ssh/authorized_keys /home/wazuh-user/.ssh/.authorized_keys_ori

logger "Waiting for Wazuh indexer to be ready"

until $(sudo curl -XGET https://localhost:9200/ -uadmin:admin -k --max-time 120 --silent --output /dev/null); do
logger -w "Wazuh indexer is not ready yet, waiting 10 seconds"
sleep 10
done

function configure_indexer(){
logger "Configuring Wazuh Indexer"
eval "rm -f /etc/wazuh-indexer/certs/* ${debug}"
eval "cp /etc/wazuh-certificates/wazuh-indexer.pem /etc/wazuh-indexer/certs/wazuh-indexer.pem ${debug}"
eval "cp /etc/wazuh-certificates/wazuh-indexer-key.pem /etc/wazuh-indexer/certs/wazuh-indexer-key.pem ${debug}"
eval "cp /etc/wazuh-certificates/admin.pem /etc/wazuh-indexer/certs/admin.pem ${debug}"
eval "cp /etc/wazuh-certificates/admin-key.pem /etc/wazuh-indexer/certs/admin-key.pem ${debug}"
eval "cp /etc/wazuh-certificates/root-ca.pem /etc/wazuh-indexer/certs/root-ca.pem ${debug}"
eval "chmod 500 /etc/wazuh-indexer/certs ${debug}"
eval "chmod 400 /etc/wazuh-indexer/certs/* ${debug}"
eval "chown -R wazuh-indexer:wazuh-indexer /etc/wazuh-indexer/certs ${debug}"
eval "systemctl restart wazuh-indexer ${debug}"
eval "/usr/share/wazuh-indexer/bin/indexer-security-init.sh ${debug}"
}

function configure_filebeat(){
logger "Configuring Filebeat"
eval "rm -f /etc/filebeat/certs/* ${debug}"
eval "cp /etc/wazuh-certificates/wazuh-server.pem /etc/filebeat/certs/wazuh-server.pem ${debug}"
eval "cp /etc/wazuh-certificates/wazuh-server-key.pem /etc/filebeat/certs/wazuh-server-key.pem ${debug}"
eval "cp /etc/wazuh-certificates/root-ca.pem /etc/filebeat/certs/root-ca.pem ${debug}"
eval "chmod 500 /etc/filebeat/certs ${debug}"
eval "chmod 400 /etc/filebeat/certs/* ${debug}"
eval "chown -R root:root /etc/filebeat/certs ${debug}"
eval "systemctl restart filebeat ${debug}"
}

function configure_manager(){
logger "Configuring Wazuh Manager"
eval "rm /var/ossec/api/configuration/security/*_key.pem ${debug}"
eval "rm /var/ossec/api/configuration/ssl/server.* ${debug}"
eval "systemctl restart wazuh-manager ${debug}"
}

function configure_dashboard(){
logger "Configuring Wazuh Dashboard"
eval "rm -f /etc/wazuh-dashboard/certs/* ${debug}"
eval "cp /etc/wazuh-certificates/wazuh-dashboard.pem /etc/wazuh-dashboard/certs/wazuh-dashboard.pem ${debug}"
eval "cp /etc/wazuh-certificates/wazuh-dashboard-key.pem /etc/wazuh-dashboard/certs/wazuh-dashboard-key.pem ${debug}"
eval "cp /etc/wazuh-certificates/root-ca.pem /etc/wazuh-dashboard/certs/root-ca.pem ${debug}"
eval "chmod 500 /etc/wazuh-dashboard/certs ${debug}"
eval "chmod 400 /etc/wazuh-dashboard/certs/* ${debug}"
eval "chown -R wazuh-dashboard:wazuh-dashboard /etc/wazuh-dashboard/certs ${debug}"
eval "systemctl restart wazuh-dashboard ${debug}"
}

function clean_configuration(){
logger "Cleaning configuration files"
eval "rm -rf /etc/wazuh-certificates /etc/.wazuh-certs-tool.sh /etc/config.yml /etc/wazuh-certificates-tool.log /var/log/wazuh-ami-customizer.log"
eval "rm -f /etc/.changePasswords.sh /etc/.wazuh-passwords-tool.sh /etc/.wazuh-install-files/wazuh-passwords.txt /var/log/wazuh-passwords-tool.log"
eval "rmdir /etc/.wazuh-install-files"
eval "sed -i '/#Ansible: Change Passwords/,//d' /var/spool/cron/root"
}

function change_passwords(){
logger "Changing passwords"
new_password=$(ec2-metadata | grep "instance-id" | cut -d":" -f2 | tr -d " "| awk '{print toupper(substr($0,1,1)) substr($0,2)}')
eval "sed -i 's/password:.*/password: ${new_password}/g' /etc/.wazuh-install-files/wazuh-passwords.txt ${debug}"
eval "bash /etc/.wazuh-passwords-tool.sh -a -A -au wazuh -ap wazuh -f /etc/.wazuh-install-files/wazuh-passwords.txt ${debug}"
eval "systemctl restart wazuh-dashboard ${debug}"
}

function restore_authorized_keys(){
logger "Restoring authorized_keys file"
eval "mv /home/wazuh-user/.ssh/.authorized_keys_ori /home/wazuh-user/.ssh/authorized_keys ${debug}"
}

logger "Creating certificates"
eval "bash /etc/.wazuh-certs-tool.sh -A ${debug}"

configure_indexer

logger "Waiting for Wazuh indexer to be ready"
indexer_security_admin_comm="sudo curl -XGET https://localhost:9200/ -uadmin:admin -k --max-time 120 --silent -w \"%{http_code}\" --output /dev/null"
http_status=$(eval "${indexer_security_admin_comm}")
retries=0
max_retries=5
while [ "${http_status}" -ne 200 ]; do
logger -w "Wazuh indexer is not ready yet, waiting 5 seconds"
sleep 5
retries=$((retries+1))
if [ "${retries}" -eq "${max_retries}" ]; then
logger -e "Wazuh indexer is not ready yet, trying to configure it again"
configure_indexer
fi
http_status=$(eval "${indexer_security_admin_comm}")
done

configure_filebeat

logger "Waiting for Filebeat to be ready"
if filebeat test output | grep -q -i -w "ERROR"; then
logger -e "Filebeat is not ready yet, trying to configure it again"
eval "filebeat test output x ${debug}"
configure_filebeat
fi

configure_manager

configure_dashboard

logger "Waiting for Wazuh dashboard to be ready"
dashboard_check_comm="curl -XGET https://localhost:443/status -uadmin:admin -k -w \"%{http_code}\" -s -o /dev/null"
http_code=$(eval "${dashboard_check_comm}")
retries=0
max_dashboard_initialize_retries=20
while [ "${http_code}" -ne "200" ];do
logger -w "Wazuh dashboard is not ready yet, waiting 15 seconds"
retries=$((retries+1))
sleep 15
if [ "${retries}" -eq "${max_dashboard_initialize_retries}" ]; then
logger -e "Wazuh dashboard is not ready yet, trying to configure it again"
configure_dashboard
fi
http_code=$(eval "${dashboard_check_comm}")
done

change_passwords

logger "Waiting for Wazuh indexer to be ready with new password"
until $(sudo curl -XGET https://localhost:9200/ -uadmin:${new_password} -k --max-time 120 --silent --output /dev/null); do
sleep 10
done

restore_authorized_keys

clean_configuration

0 comments on commit adc57af

Please sign in to comment.