-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathas-install-collectors.sh
638 lines (578 loc) · 20.7 KB
/
as-install-collectors.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
#!/bin/bash
#Folders
local_content_folder=customer_data
certificates_folder=certificates
configs_folder=configs
packages_folder=packages
ssl_folder="/etc/ssl/as-collectors"
current_version="7.17.4"
current_dir=`pwd`
#Check permissions
if [ "$EUID" -ne 0 ]
then echo "Please run the script as root"
exit
fi
if [[ "$#" == 0 ]]
then
echo "No parameters given, please provide at least -i(nstall) or -u(ninstall)"
exit 1
fi
print_help()
{
echo '
-c|--customerid Provide the customer id for the machine
-H|--host Provide the host to send data to
-i|--install Install or Update the collectors
-u|--uninstall Remove the installed collectors
-t|--installer-type If the OS is not recognized specify the package manager manually [rpm,dpkg]
-a|--all Automatically install all collectors
-b|--startonboot Predetermine that autostart should be enabled
-r|--reinstall Predetermine that packages should be reinstalled
-s|--skip-install Predetermine to skip installs
-O|--overwrite Predetermine to overwrite configuration files
-S|--skip Predetermine to skip configuration file overwrites
'
}
#Handle input parameters
while [[ "$#" > 0 ]]
do
echo "checking: $1"
declare -A inputparms
case $1 in
-c|--customerid) inputparms['customer_id']="$2"; shift;;
-H|--host) inputparms['host']="$2"; shift;;
-i|--install) install=1;;
-u|--uninstall) uninstall=1;;
-t|--installer-type) inputparms['installer_type']="$2"; shift;;
-a|--all) inputparms['select_collector']="a";;
-b|--startonboot) inputparms['enable_autostart']="y";;
-r|--reinstall) inputparms['package_setup_choice']="r";;
-s|--skip-install) inputparms['package_setup_choice']="s";;
-O|--overwrite) inputparms['overwrite_config']="o";;
-S|--skip) inputparms['overwrite_config']="s";;
-h|--help) print_help;;
*) echo "Unknown parameter passed: $1"; exit 1;;
esac
shift
done
#Error handler
error_handler()
{
if [ "$?" != "0" ]; then
echo "$1"
exit
fi
}
read_question()
{
question=$1
silent_parm=$2
#empty validation on every question read
validation=''
validation=$3
answer=""
#Check if the silent parm is empty
if [ -z ${inputparms[$silent_parm]} ]
then
read -p "$question" answer
#If there was any answer proceed
if ([ -n "$answer" ])
then
#Check if a validation pattern is present and validate if so
if [ -z "$validation" ] || [[ "$answer" =~ $validation ]]
then
return
else
echo "The question was not answered correctly, as the input did not match. Please try again.."
read_question "$question" "$silent_parm" "$validation"
fi
return
else
echo "The question was not answered. Please try again.."
read_question "$question" "$silent_parm" "$validation"
fi
else
answer=${inputparms[$2]}
#If there was any answer proceed
if ([ -n "$answer" ])
then
#Check if a validation pattern is present and validate if so
if [ -z "$validation" ] || [[ "$answer" =~ $validation ]]
then
return
else
echo "The parameter given for $silent_parm was not correct, as the input did not match. Exiting.."
exit
fi
return
else
echo "The parameter given for $silent_parm was not provided. Please try again.."
exit
fi
fi
}
#Check if configuration file exists
copy_item()
{
source=$1
destination=$2
#If it is a directory, loop through all files and resend them through the function as files
if [ -d $source ]
then
echo "Looping through files in $source"
directory=$destination
for file in $(find $source -mindepth 1)
do
echo "Found file $file"
#Grab file name
file_name=`basename $file`
copy_item $file $directory/$file_name
done
return
fi
if [ -f $destination ]
then
#If so, ask to overwrite, create a new file or skip the copy
read_question "The target $destination already exists, What do you want to do? [O]verwrite, [K]eep both, or [S]kip: " "overwrite_config"
config_choice="$answer"
if [[ $config_choice =~ o|O ]]
then
echo "Overwriting $destination"
if [ -f $destination ]
then
echo "Creating bak file for $destination"
cp -rf $destination $destination.bak
fi
cp -rf $source $destination
sed -i -e "s/<customer_id>/$customer_id/g" $destination
sed -i -e "s/<host>/$host/g" $destination
elif [[ $config_choice =~ k|K ]]
then
echo "Copying file to $destination.new"
cp -rf $source $destination.new
sed -i -e "s/<customer_id>/$customer_id/g" $destination
sed -i -e "s/<host>/$host/g" $destination
elif [[ $config_choice =~ s|S ]]
then
echo "Skipping $source"
else
echo "An invalid option was provided, please try again"
copy_item $source $destination
fi
else
echo "Copying $source to $destination"
cp -rf $source $destination
sed -i -e "s/<customer_id>/$customer_id/g" $destination
sed -i -e "s/<host>/$host/g" $destination
fi
}
check_package() {
echo "checking if required version is present"
#cd $packages_folder
ls $package-$current_version-$file_extention
if [ $? -ne 0 ]
then
echo "Downloading version $current_version for $package"
wget https://artifacts.elastic.co/downloads/beats/$package/$package-$current_version-$file_extention
error_handler "Could not download file"
#latest_version=`ls -lhS *.rpm | head -n 1 | awk '{print $9}'`
fi
echo "Checking package integrity, downloading sha 512 checksum from Elastic..."
rm -f $package-$current_version-$file_extention.sha512
wget https://artifacts.elastic.co/downloads/beats/$package/$package-$current_version-$file_extention.sha512
shasum -c $package-$current_version-$file_extention.sha512
error_handler "There was a checksum mismatch for $package. Please determine if this is a configuration or a possible security issue"
#cd $current_dir
}
install_package()
{
package=$1
package_installed=false
cd $current_dir/$packages_folder
#Check if package is already installed
if [ "$installer" == "dpkg" ]; then dpkg -l $package &> /dev/null
elif [ "$installer" == "rpm" ]; then yum list $package &> /dev/null; fi
if [ $? -eq 0 ]
then
#If installed, ask to reinstall or skip
read_question "$package is already installed, What do you want to do? [R]einstall, [U]pgrade or [S]kip: " "package_setup_choice"
package_setup_choice="$answer"
if [[ $package_setup_choice =~ r|R ]]
then
echo "Reinstalling $package with $installer"
check_package
if [ "$installer" == "dpkg" ]; then dpkg -r $package
elif [ "$installer" == "rpm" ]; then yum erase $package -y -q; fi
error_handler "Could not remove $package"
if [ "$installer" == "dpkg" ]; then dpkg -i $package-$current_version-$file_extention
elif [ "$installer" == "rpm" ]; then rpm -i $package-$current_version-$file_extention; fi
error_handler "Could not install $package"
package_installed=true
elif [[ $package_setup_choice =~ s|S ]]
then
echo "Skipping the installation of $package"
elif [[ $package_setup_choice =~ u|U ]]
then
upgrade_package
else
echo "An invalid option was provided, please try again"
install_package $package
fi
else
echo "Installing $package with $installer"
check_package
if [ "$installer" == "dpkg" ]; then dpkg -i $package-$current_version-$file_extention
elif [ "$installer" == "rpm" ]; then rpm -i $package-$current_version-$file_extention; fi
error_handler "Could not install $package package"
package_installed=true
fi
cd $current_dir
#Check config files to see if they are the default configs. If so, delete them
if [ "$package_installed" == true ] && [ -f /etc/$package/$package.yml ]
then
echo "Checking if default configuration is present after installation"
if [ -f /etc/$package/$package.reference.yml ]
then
if [ `date -r /etc/$package/$package.yml +%s` == `date -r /etc/$package/$package.reference.yml +%s` ]
then
rm -f /etc/$package/$package.yml
error_handler "Could not remove /etc/$package/$package.yml"
# if [ "$package" == "filebeat" ]
# then
# rm -f /etc/$package/modules.d/*
# error_handler "Could not remove files in /etc/$package/modules.d"
# fi
fi
fi
fi
}
upgrade_package()
{
echo "Upgrading $package with $installer to $current_version"
check_package
if [ `$package version | awk '{print $3}'` != $current_version ]
then
if [ "$installer" == "dpkg" ]; then dpkg -i $package-$current_version-$file_extention
elif [ "$installer" == "rpm" ]; then rpm -U $package-$current_version-$file_extention; fi
error_handler "Could not install $package package"
echo "restarting $package"
systemctl restart $package
else
echo "The version is equal, no upgrade required"
fi
}
start_on_boot()
{
package=$1
echo "Enabling $package..."
if [ "$installer" == "dpkg" ]
then
systemctl enable $package
error_handler "Could not configure $package to start on boot"
elif [ "$installer" == "rpm" ]
then
chkconfig $service on
error_handler "Could not configure $package to start on boot"
fi
}
start_now()
{
package=$1
if [ "$installer" == "dpkg" ]
then
echo "starting $package..."
systemctl start $package
error_handler "Could not start $package"
elif [ "$installer" == "rpm" ]
then
service $service start
error_handler "Could not get $package to start, check the configuration manually"
fi
}
uninstall_package()
{
package=$1
package_installed=false
#Check if package is already installed
if [ "$installer" == "dpkg" ]; then dpkg -l $package &> /dev/null
elif [ "$installer" == "rpm" ]; then yum list $package &> /dev/null; fi
if [ $? -eq 0 ]
then
echo "Uninstalling $package"
if [ "$installer" == "dpkg" ]; then dpkg -P $package
elif [ "$installer" == "rpm" ]; then yum erase $package -y -q; fi
error_handler "Could not uninstall $package"
else
echo "$package was not found on the system"
fi
}
################### Prerequisites ###################
#Check the OS on which the collectors are going to be installed
if [ -f /etc/debian_version ]
then
os_version=`cat /etc/debian_version`
installer="dpkg"
file_extention="amd64.deb"
fi
if [ -f /etc/redhat-release ]
then
os_version=`cat /etc/redhat-release`
installer="rpm"
os="redhat"
file_extention="x86_64.rpm"
fi
if [ -f /etc/centos-release ]
then
os_version=`cat /etc/centos-release`
installer="rpm"
os="centos"
file_extention="x86_64.rpm"
fi
#If Debian / Ubuntu, RedHat / Centos continue the installation otherwise ask to provide installer type
if [ -z $installer ]
then
echo "No supported OS was found"
#If not recognized, prompt option to choose for DPKG or RPM install on own risk, or exit
read_question "If you want to try manually, please enter 'dpkg' or 'rpm'" "installer_type"
installer="$answer"
fi
#Check if an installer type has been found and is an expected value
if [[ $installer =~ dpkg|rpm ]]
then
echo "A valid installer type has been provided"
else
echo "There was no valid installer type found, exiting"
exit 1
fi
#Ask for API key that was provided when adding the machine on the dashboard
#Disabled until API is present
if [ $install ]
then
#Ask for the customer_id
read_question "What is your customer ID?: " "customer_id" 'as[0-9]{11}'
customer_id=$answer
#Ask for the host
read_question "Provide the host to send data to: " "host"
host=$answer
#Ask which packages the user would like to install
supported_collectors=(filebeat packetbeat auditbeat metricbeat)
counter=-
for i in ${!supported_collectors[@]} ; do
echo "$i: ${supported_collectors[$i]}"
let counter=$counter+1
done
echo "Select a to enable all collectors"
#Input customer name for folder definition
read_question "Select a collector from the list to mark it for install (type a number from the list and press enter): " "select_collector"
select_collector=$answer
#collector_install_list=""
while true
do
if [[ $select_collector =~ a|A ]]
then
collector_install_list=("filebeat" "packetbeat" "auditbeat" "metricbeat")
break
elif [ ${collector_install_list[$select_collector]+exists} ]
then
echo "Collector is already added to the list"
else
if [ ${supported_collectors[$select_collector]+exists} ]
then
echo "Adding collector to the list"
collector_install_list[$select_collector]=${supported_collectors[$select_collector]}
select_collector=""
else
echo "You have selected an invalid option"
fi
fi
read -p "Optionally select another collector, otherwise press Enter: " select_collector
if [ -z "$select_collector" ]
then
break
fi
done
echo "You have selected: ${collector_install_list[@]}"
#Check $local_content_folder for local content
if [ -d $local_content_folder ]
then
if [ -d $certificates_folder ]
then
echo "Locally present certificates found, using these..."
certificates_local=true
#TO DO: Validate certificates present
fi
if [ -d $packages_folder ]
then
echo "Locally present collector packages found, using these..."
collectors_local=true
#TO DO: Validate certificates present
fi
if [ -d $configs_folder ]
then
echo "Locally present configs found, using these..."
collectors_local=true
#TO DO: Validate certificates present
fi
fi
#Download packages based upon install type (dpkg or rpm)
if [ "$certificates_local" == false ]
then
echo "No local certificates found, downloading is not yet supported. Exiting..."
exit
fi
#Download certificates from AntSec with API key
if [ "$collectors_local" == false ]
then
echo "No local collector packages found, downloading is not yet supported. Exiting..."
exit
fi
#Download configuration from AntSec with API key
if [ "$config_local" == false ]
then
echo "No local config found, downloading is not yet supported. Exiting..."
exit
fi
################### Install Phase ###################
#Check if $ssl_folder exists
if [ ! -d $ssl_folder ]
then
mkdir $ssl_folder
error_handler "Could not create folder"
fi
#Check if certificates are already present
#if [ -f $ssl_folder ]
#Check if certificates are different than the ones that should be
#If present, ask to overwrite
#Extract certificates to the required location
cp $certificates_folder/* $ssl_folder/
error_handler "Could not copy certificate files"
#Install the selected packages
for collector in "${collector_install_list[@]}"
do
install_package $collector
done
#Copy the configuration to the /etc folders
for collector in "${collector_install_list[@]}"
do
#Check if configuration folder exists
if [ ! -d "/etc/$collector" ]
then
error_handler "$collector package is not installed correctly"
else
# Loop through files to check if they exists and ask what to do with them
#/etc/$collector/$collector.yml
#$configs_folder/$collector.yml
copy_item $configs_folder/$collector/$collector.yml /etc/$collector/$collector.yml
if [ "$collector" == "filebeat" ]
then
copy_item $configs_folder/$collector/modules.d /etc/$collector/modules.d
fi
fi
done
#Set all required permissions
echo "Checking permissions on the private key(s)"
for key in `ls $ssl_folder/*.key`
do
if chmod 600 $key
then
echo "Changed permission of $key to 600"
fi
done
echo "The AntSec Collectors have been successfully installed"
read_question "Would you like to automatically start the services upon boot? [Y/n]: " "enable_autostart"
enable_autostart="$answer"
if [[ $enable_autostart =~ y|Y ]]
then
for collector in "${collector_install_list[@]}"
do
start_on_boot $collector
done
else
echo "Did not receive 'yes', skipping autostart configuration for $package"
fi
read -p "Would you like to start the services now? [Y/n]: " -i "Y" -e start_services
if [[ $start_services =~ y|Y ]]
then
for collector in "${collector_install_list[@]}"
do
start_now $collector
done
else
echo "Did not receive 'yes', skipping autostart configuration for $package"
fi
exit
elif [ $uninstall ]
then
#Ask which packages the user would like to install
supported_collectors=(filebeat packetbeat auditbeat metricbeat)
counter=-
for i in ${!supported_collectors[@]} ; do
echo "$i: ${supported_collectors[$i]}"
let counter=$counter+1
done
echo "Select 'a' to uninstall all collectors"
#Input customer name for folder definition
read_question "Select a collector from the list to mark it for uninstall (type a number from the list and press enter): " "select_collector"
select_collector="$answer"
#collector_install_list=""
while true
do
if [[ $select_collector =~ a|A ]]
then
collector_install_list=("filebeat" "packetbeat" "auditbeat" "metricbeat")
break
elif [ ${collector_install_list[$select_collector]+exists} ]
then
echo "Collector is already added to the list"
else
if [ ${supported_collectors[$select_collector]+exists} ]
then
echo "Adding collector to the list"
collector_install_list[$select_collector]=${supported_collectors[$select_collector]}
select_collector=""
else
echo "You have selected an invalid option"
fi
fi
read -p "Optionally select another collector, otherwise press Enter: " select_collector
if [ -z "$select_collector" ]
then
break
fi
done
echo "You have selected: ${collector_install_list[@]}"
################### Uninstall Phase ###################
#Check if $ssl_folder exists
if [ -d $ssl_folder ]
then
read -p "Do you want to remove the certificates from '$ssl_folder'? [Y/n] : " -i "Y" -e remove_certificates
if [[ $remove_certificates =~ y|Y ]]
then
rm -r $ssl_folder
error_handler "Could not remove folder"
else
echo "Did not receive 'yes', keeping certificate directory"
fi
fi
#Uninstall the selected packages
for collector in "${collector_install_list[@]}"
do
uninstall_package $collector
done
#Ask to clean remaining files
for collector in "${collector_install_list[@]}"
do
if [ -d /etc/$collector ]
then
read -p "Some configuration files remained in /etc/$collector. Do you want to remove these? [Y/n] : " -i "Y" -e remove_remainders
if [[ $remove_remainders =~ y|Y ]]
then
rm -r /etc/$collector
error_handler "Could not remove folder"
else
echo "Did not receive 'yes', keeping remainders in /etc/$collector"
fi
fi
done
echo "The AntSec Collectors have been successfully uninstalled"
fi