4
4
# Variables
5
5
logfile=" /var/log/wazuh-ami-customizer.log"
6
6
debug=" | tee -a ${logfile} "
7
+
8
+ # ##########################################
9
+ # Utility Functions
10
+ # ##########################################
7
11
function logger(){
8
12
now=$( date +' %d/%m/%Y %H:%M:%S' )
9
13
mtype=" INFO:"
@@ -28,20 +32,26 @@ function logger(){
28
32
printf " %s\n" " ${now} ${mtype} ${message} " | tee -a " ${logfile} "
29
33
}
30
34
31
- logger " Starting Wazuh AMI Customizer"
32
-
33
- logger " Stopping SSH service to avoid connections during the configuration"
34
-
35
- eval " systemctl stop sshd.service"
35
+ function create_certificates() {
36
+ logger " Creating certificates"
37
+ eval " bash /etc/.wazuh-certs-tool.sh -A ${debug} "
38
+ }
36
39
37
- logger " Waiting for Wazuh indexer to be ready"
40
+ function systemctl_execution(){
41
+ eval " systemctl $1 $2 $3 "
42
+ }
38
43
39
- until $( curl -XGET https://localhost:9200/ -uadmin:admin -k --max-time 120 --silent --output /dev/null) ; do
40
- logger -w " Wazuh indexer is not ready yet, waiting 10 seconds"
41
- sleep 10
42
- done
44
+ # ##########################################
45
+ # Configuration Functions
46
+ # ##########################################
43
47
44
48
function configure_indexer(){
49
+ logger " Stopping all services"
50
+ systemctl_execution " stop" " filebeat" " ${debug} "
51
+ systemctl_execution " stop" " wazuh-dashboard" " ${debug} "
52
+ systemctl_execution " stop" " wazuh-manager" " ${debug} "
53
+ systemctl_execution " stop" " wazuh-indexer" " ${debug} "
54
+ eval " sleep 5"
45
55
logger " Configuring Wazuh Indexer"
46
56
eval " rm -f /etc/wazuh-indexer/certs/* ${debug} "
47
57
eval " cp /etc/wazuh-certificates/wazuh-indexer.pem /etc/wazuh-indexer/certs/wazuh-indexer.pem ${debug} "
@@ -52,10 +62,28 @@ function configure_indexer(){
52
62
eval " chmod 500 /etc/wazuh-indexer/certs ${debug} "
53
63
eval " chmod 400 /etc/wazuh-indexer/certs/* ${debug} "
54
64
eval " chown -R wazuh-indexer:wazuh-indexer /etc/wazuh-indexer/certs ${debug} "
55
- eval " systemctl restart wazuh-indexer ${debug} "
65
+ systemctl_execution " start " " wazuh-indexer" " ${debug} "
56
66
eval " /usr/share/wazuh-indexer/bin/indexer-security-init.sh ${debug} "
57
67
}
58
68
69
+ function verify_indexer() {
70
+ logger " Waiting for Wazuh indexer to be ready"
71
+ indexer_security_admin_comm=" curl -XGET https://localhost:9200/ -uadmin:admin -k --max-time 120 --silent -w \" %{http_code}\" --output /dev/null"
72
+ http_status=$( eval " ${indexer_security_admin_comm} " )
73
+ retries=0
74
+ max_retries=5
75
+ while [ " ${http_status} " -ne 200 ]; do
76
+ logger -w " Wazuh indexer is not ready yet, waiting 5 seconds"
77
+ sleep 5
78
+ retries=$(( retries+ 1 ))
79
+ if [ " ${retries} " -eq " ${max_retries} " ]; then
80
+ logger -e " Wazuh indexer is not ready yet, trying to configure it again"
81
+ configure_indexer
82
+ fi
83
+ http_status=$( eval " ${indexer_security_admin_comm} " )
84
+ done
85
+ }
86
+
59
87
function configure_filebeat(){
60
88
logger " Configuring Filebeat"
61
89
eval " rm -f /etc/filebeat/certs/* ${debug} "
@@ -65,14 +93,23 @@ function configure_filebeat(){
65
93
eval " chmod 500 /etc/filebeat/certs ${debug} "
66
94
eval " chmod 400 /etc/filebeat/certs/* ${debug} "
67
95
eval " chown -R root:root /etc/filebeat/certs ${debug} "
68
- eval " systemctl restart filebeat ${debug} "
96
+ systemctl_execution " start" " filebeat" " ${debug} "
97
+ }
98
+
99
+ function verify_filebeat() {
100
+ logger " Waiting for Filebeat to be ready"
101
+ if filebeat test output | grep -q -i -w " ERROR" ; then
102
+ logger -e " Filebeat is not ready yet, trying to configure it again"
103
+ eval " filebeat test output x ${debug} "
104
+ configure_filebeat
105
+ fi
69
106
}
70
107
71
108
function configure_manager(){
72
109
logger " Configuring Wazuh Manager"
73
110
eval " rm /var/ossec/api/configuration/security/*_key.pem ${debug} "
74
111
eval " rm /var/ossec/api/configuration/ssl/server.* ${debug} "
75
- eval " systemctl restart wazuh-manager ${debug} "
112
+ systemctl_execution " start " " wazuh-manager" " ${debug} "
76
113
}
77
114
78
115
function configure_dashboard(){
@@ -84,9 +121,31 @@ function configure_dashboard(){
84
121
eval " chmod 500 /etc/wazuh-dashboard/certs ${debug} "
85
122
eval " chmod 400 /etc/wazuh-dashboard/certs/* ${debug} "
86
123
eval " chown -R wazuh-dashboard:wazuh-dashboard /etc/wazuh-dashboard/certs ${debug} "
87
- eval " systemctl restart wazuh-dashboard ${debug} "
124
+ systemctl_execution " start " " wazuh-dashboard" " ${debug} "
88
125
}
89
126
127
+ function verify_dashboard() {
128
+ logger " Waiting for Wazuh dashboard to be ready"
129
+ dashboard_check_comm=" curl -XGET https://localhost:443/status -uadmin:admin -k -w \" %{http_code}\" -s -o /dev/null"
130
+ http_code=$( eval " ${dashboard_check_comm} " )
131
+ retries=0
132
+ max_dashboard_initialize_retries=20
133
+ while [ " ${http_code} " -ne " 200" ]; do
134
+ logger -w " Wazuh dashboard is not ready yet, waiting 15 seconds"
135
+ retries=$(( retries+ 1 ))
136
+ sleep 15
137
+ if [ " ${retries} " -eq " ${max_dashboard_initialize_retries} " ]; then
138
+ logger -e " Wazuh dashboard is not ready yet, trying to configure it again"
139
+ configure_dashboard
140
+ fi
141
+ http_code=$( eval " ${dashboard_check_comm} " )
142
+ done
143
+ }
144
+
145
+ # ##########################################
146
+ # Cleanup and Finalization Functions
147
+ # ##########################################
148
+
90
149
function clean_configuration(){
91
150
logger " Cleaning configuration files"
92
151
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"
@@ -100,63 +159,43 @@ function change_passwords(){
100
159
new_password=$( ec2-metadata | grep " instance-id" | cut -d" :" -f2 | tr -d " " | awk ' {print toupper(substr($0,1,1)) substr($0,2)}' )
101
160
eval " sed -i 's/password:.*/password: ${new_password} /g' /etc/.wazuh-install-files/wazuh-passwords.txt ${debug} "
102
161
eval " bash /etc/.wazuh-passwords-tool.sh -a -A -au wazuh -ap wazuh -f /etc/.wazuh-install-files/wazuh-passwords.txt >> /dev/null"
103
- eval " systemctl restart wazuh-dashboard ${debug} "
104
162
}
105
163
106
164
function restart_ssh_service(){
107
165
logger " Starting SSH service"
108
166
eval " systemctl start sshd.service"
109
167
}
110
168
111
- logger " Creating certificates"
112
- eval " bash /etc/.wazuh-certs-tool.sh -A ${debug} "
169
+ # ##########################################
170
+ # Main
171
+ # ##########################################
113
172
114
- configure_indexer
173
+ logger " Starting Wazuh AMI Customizer"
174
+
175
+ logger " Stopping SSH service to avoid connections during the configuration"
176
+ eval " systemctl stop sshd.service"
115
177
116
178
logger " Waiting for Wazuh indexer to be ready"
117
- indexer_security_admin_comm=" curl -XGET https://localhost:9200/ -uadmin:admin -k --max-time 120 --silent -w \" %{http_code}\" --output /dev/null"
118
- http_status=$( eval " ${indexer_security_admin_comm} " )
119
- retries=0
120
- max_retries=5
121
- while [ " ${http_status} " -ne 200 ]; do
122
- logger -w " Wazuh indexer is not ready yet, waiting 5 seconds"
123
- sleep 5
124
- retries=$(( retries+ 1 ))
125
- if [ " ${retries} " -eq " ${max_retries} " ]; then
126
- logger -e " Wazuh indexer is not ready yet, trying to configure it again"
127
- configure_indexer
128
- fi
129
- http_status=$( eval " ${indexer_security_admin_comm} " )
179
+ until $( curl -XGET https://localhost:9200/ -uadmin:admin -k --max-time 120 --silent --output /dev/null) ; do
180
+ logger -w " Wazuh indexer is not ready yet, waiting 10 seconds"
181
+ sleep 10
130
182
done
131
183
132
- configure_filebeat
184
+ create_certificates
185
+
186
+ configure_indexer
187
+ verify_indexer
133
188
134
- logger " Waiting for Filebeat to be ready"
135
- if filebeat test output | grep -q -i -w " ERROR" ; then
136
- logger -e " Filebeat is not ready yet, trying to configure it again"
137
- eval " filebeat test output x ${debug} "
138
- configure_filebeat
139
- fi
189
+ configure_filebeat
190
+ verify_filebeat
140
191
141
192
configure_manager
142
193
143
194
configure_dashboard
195
+ verify_dashboard
144
196
145
- logger " Waiting for Wazuh dashboard to be ready"
146
- dashboard_check_comm=" curl -XGET https://localhost:443/status -uadmin:admin -k -w \" %{http_code}\" -s -o /dev/null"
147
- http_code=$( eval " ${dashboard_check_comm} " )
148
- retries=0
149
- max_dashboard_initialize_retries=20
150
- while [ " ${http_code} " -ne " 200" ]; do
151
- logger -w " Wazuh dashboard is not ready yet, waiting 15 seconds"
152
- retries=$(( retries+ 1 ))
153
- sleep 15
154
- if [ " ${retries} " -eq " ${max_dashboard_initialize_retries} " ]; then
155
- logger -e " Wazuh dashboard is not ready yet, trying to configure it again"
156
- configure_dashboard
157
- fi
158
- http_code=$( eval " ${dashboard_check_comm} " )
159
- done
197
+ systemctl_execution " stop" " wazuh-dashboard" " ${debug} "
198
+ eval " sleep 5"
160
199
161
200
change_passwords
162
201
@@ -165,6 +204,9 @@ until $(curl -XGET https://localhost:9200/ -uadmin:${new_password} -k --max-time
165
204
sleep 10
166
205
done
167
206
207
+ systemctl_execution " start" " wazuh-dashboard" " ${debug} "
208
+ systemctl_execution " enable" " wazuh-dashboard" " ${debug} "
209
+
168
210
restart_ssh_service
169
211
170
212
clean_configuration
0 commit comments