-
Notifications
You must be signed in to change notification settings - Fork 1
/
UPDATE.sh
1224 lines (1074 loc) · 46 KB
/
UPDATE.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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# Color codes
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
RED='\033[0;31m'
VIOLET='\033[0;35m'
NC='\033[0m' # No Color
setVars(){
MISP_IMAGE_NAME=$(generateName "misp")
MYSQL_IMAGE_NAME=$(generateName "mysql")
REDIS_IMAGE_NAME=$(generateName "redis")
MODULES_IMAGE_NAME=$(generateName "modules")
PATH_TO_MISP="/var/www/MISP/"
SNAP_NAME=$(generateName "update")
NAMING_LOG_FILE="./logs/naming.log"
MISP_NEW_EQ_OLD=false
MYSQL_NEW_EQ_OLD=false
REDIS_NEW_EQ_OLD=false
MODULES_NEW_EQ_OLD=false
RENAMED_MISP=false
RENAMED_MYSQL=false
RENAMED_REDIS=false
RENAMED_MODULES=false
}
setDefaultArgs(){
default_misp="no"
default_misp_img=""
default_current_misp=""
default_new_misp=$(generateName "misp")
default_mysql="no"
default_mysql_img=""
default_new_mysql=$(generateName "mysql")
default_redis="no"
default_redis_img=""
default_new_redis=$(generateName "redis")
default_modules="no"
default_modules_img=""
default_new_modules=$(generateName "modules")
}
error() {
local msg=$1
echo -e "${RED}[$(date '+%Y-%m-%d %H:%M:%S')] Error: $msg${NC}" > /dev/tty
}
warn() {
local msg=$1
echo -e "${YELLOW}[$(date '+%Y-%m-%d %H:%M:%S')] Warning: $msg${NC}" > /dev/tty
}
okay() {
local msg=$1
echo -e "${GREEN}[$(date '+%Y-%m-%d %H:%M:%S')] Success: $msg${NC}" > /dev/tty
}
info() {
local msg=$1
echo -e "${BLUE}[$(date '+%Y-%m-%d %H:%M:%S')] Info: $msg${NC}" > /dev/tty
}
generateName(){
local name="$1"
echo "${name}-$(date +%Y-%m-%d-%H-%M-%S)"
}
checkResourceExists() {
local resource_type="$1"
local resource_name="$2"
case "$resource_type" in
"container")
lxc info "$resource_name" &>/dev/null
;;
"image")
lxc image list --format=json | jq -e --arg alias "$resource_name" '.[] | select(.aliases[].name == $alias) | .fingerprint' &>/dev/null
;;
"project")
lxc project list --format=json | jq -e --arg name "$resource_name" '.[] | select(.name == $name) | .name' &>/dev/null
;;
"storage")
lxc storage list --format=json | jq -e --arg name "$resource_name" '.[] | select(.name == $name) | .name' &>/dev/null
;;
"network")
lxc network list --format=json | jq -e --arg name "$resource_name" '.[] | select(.name == $name) | .name' &>/dev/null
;;
"profile")
lxc profile list --format=json | jq -e --arg name "$resource_name" '.[] | select(.name == $name) | .name' &>/dev/null
;;
esac
return $?
}
checkSoftwareDependencies() {
local dependencies=("jq" "yq")
for dep in "${dependencies[@]}"; do
if ! command -v "$dep" &> /dev/null; then
error "$dep is not installed."
exit 1
fi
done
}
usage() {
echo "Usage: $0 [options]"
echo
echo "Options:"
echo " -h, --help Show this help message and exit."
echo " -p, --passwd <password> Set the MySQL root password."
echo " --update-misp Update MISP container."
echo " --misp-image <image> Specify the MISP image."
echo " --current-misp <name> Specify the current MISP container name (Mandatory)."
echo " --new-misp <name> Specify the new MISP container name."
echo " -a, --all Apply updates to all components."
echo " --update-mysql Update MySQL container."
echo " --mysql-image <image> Specify the MySQL image."
echo " --new-mysql <name> Specify the new MySQL container name."
echo " --update-redis Update Redis container."
echo " --redis-image <image> Specify the Redis image."
echo " --new-redis <name> Specify the new Redis container name."
echo " --update-modules Update modules container."
echo " --modules-image <image> Specify the modules image."
echo " --new-modules <name> Specify the new modules container name."
echo
echo "Examples:"
echo " $0 --update-misp --misp-image new_misp_image.tar --current-misp current_misp_container --new-misp new_misp_container -p password"
echo " $0 --update-mysql --current-misp current_misp_container --mysql-image new_mysql_image.tar --new-mysql new_mysql_container -p password"
echo
echo "Note:"
echo " - The script updates specified containers to new versions using provided images."
echo " - Mandatory fields must be specified for each component you wish to update."
echo " - Use only alphanumeric characters and hyphens for container names."
}
nonInteractiveConfig(){
VALID_ARGS=$(getopt -o hap: --long help,passwd:,update-misp,misp-image:,current-misp:,new-misp:,all,update-mysql,mysql-image:,new-mysql:,update-redis,redis-image:,new-redis:,update-modules,modules-image:,new-modules: -- "$@")
if [[ $? -ne 0 ]]; then
exit 1;
fi
eval set -- "$VALID_ARGS"
while [ $# -gt 0 ]; do
case "$1" in
-h | --help)
usage
exit 0
;;
-p | --passswd)
MYSQL_ROOT_PASSWORD=$2
shift 2
;;
--update-misp)
misp="y"
shift
;;
--misp-image)
misp_img=$2
shift 2
;;
--current-misp)
current_misp=$2
shift 2
;;
--new-misp)
new_misp=$2
shift 2
;;
-a | --all)
misp="y"
mysql="y"
redis="y"
modules="y"
shift
;;
--update-mysql)
mysql="y"
shift
;;
--mysql-image)
mysql_img=$2
shift 2
;;
--new-mysql)
new_mysql=$2
shift 2
;;
--update-redis)
redis="y"
shift
;;
--redis-image)
redis_img=$2
shift 2
;;
--new-redis)
new_redis=$2
shift 2
;;
--update-modules)
modules="y"
shift
;;
--modules-image)
modules_img=$2
shift 2
;;
--new-modules)
new_modules=$2
shift 2
;;
*)
break
;;
esac
done
# Set global values
misp=${misp:-$default_misp}
MISP=$(echo "$misp" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
MISP_IMAGE=${misp_img:-$default_misp_img}
CURRENT_MISP=${current_misp:-$default_current_misp}
NEW_MISP=${new_misp:-$default_new_misp}
mysql=${mysql:-$default_mysql}
MYSQL=$(echo "$mysql" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
MYSQL_IMAGE=${mysql_img:-$default_mysql_img}
NEW_MYSQL=${new_mysql:-$default_new_mysql}
redis=${redis:-$default_redis}
REDIS=$(echo "$redis" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
REDIS_IMAGE=${redis_img:-$default_redis_img}
NEW_REDIS=${new_redis:-$default_new_redis}
modules=${modules:-$default_modules}
MODULES=$(echo "$modules" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
MODULES_IMAGE=${modules_img:-$default_modules_img}
NEW_MODULES=${new_modules:-$default_new_modules}
}
checkNamingConvention(){
local input="$1"
local pattern="^[a-zA-Z0-9-]+$"
if ! [[ "$input" =~ $pattern ]]; then
return 1
fi
return 0
}
validateArgs(){
local mandatory=("CURRENT_MISP")
local image=()
# Check images and if args set
if ! checkResourceExists "container" "$CURRENT_MISP"; then
error "Container $CURRENT_MISP could not be found!"
exit 1
fi
if $MISP; then
mandatory+=("MISP_IMAGE" "MYSQL_ROOT_PASSWORD")
image+=("MISP_IMAGE")
fi
if $MYSQL; then
mandatory+=("MYSQL_IMAGE" "MYSQL_ROOT_PASSWORD")
image+=("MYSQL_IMAGE")
fi
if $REDIS; then
mandatory+=("REDIS_IMAGE")
image+=("REDIS_IMAGE")
fi
if $MODULES; then
mandatory+=("MODULES_IMAGE")
image+=("MODULES_IMAGE")
fi
for arg in "${mandatory[@]}"; do
if [ -z "${!arg}" ]; then
error "$arg is not set!"
exit 1
fi
done
for img in "${image[@]}"; do
if [ ! -f "${!img}" ]; then
error "Image ${!img} is not a file or could not be found!"
exit 1
fi
done
# Check if update is set
if ! $MISP && ! $MYSQL && ! $REDIS && ! $MODULES; then
error "No update set for any component. Cancel update ..."
exit 1
fi
# Check if components are running
if ! checkLXDContainerRunning "$CURRENT_MISP"; then
error "Container $CURRENT_MISP is not running!"
exit 1
fi
validateNewContainerSetup "$NEW_MISP" "$MISP" "MISP" "MISP_IMAGE"
validateNewContainerSetup "$NEW_MYSQL" "$MYSQL" "MySQL" "MYSQL_IMAGE"
validateNewContainerSetup "$NEW_REDIS" "$REDIS" "Redis" "REDIS_IMAGE"
validateNewContainerSetup "$NEW_MODULES" "$MODULES" "Modules" "MODULES_IMAGE"
# Check for duplicate names
declare -A name_counts
if $MISP; then
((name_counts["$NEW_MISP"]++))
fi
if $MYSQL;then
((name_counts["$NEW_MYSQL"]++))
fi
if $REDIS;then
((name_counts["$NEW_REDIS"]++))
fi
if $MODULES;then
((name_counts["$NEW_MODULES"]++))
fi
for name in "${!name_counts[@]}"; do
if ((name_counts["$name"] >= 2)); then
error "At least two container have the same new name: $name"
exit 1
fi
done
}
checkLXDContainerRunning() {
local container_name=$1
local state
state=$(lxc list "^${container_name}$" --format csv -c s)
if [ "$state" == "RUNNING" ]; then
return 0
fi
return 1
}
validateNewContainerSetup() {
local name=$1
local flag=$2
local type=$3
local image_var_name=$4
if [ "$flag" = true ]; then
if ! checkNamingConvention "$name"; then
error "Name $name for $type container is not valid. Please use only alphanumeric characters and hyphens."
exit 1
fi
if [ ! -f "${!image_var_name}" ]; then
if [ -z "${!image_var_name}" ]; then
error "No update image for $type specified!"
exit 1
fi
error "The specified image ${!image_var_name} for $type does not exist"
exit 1
fi
fi
}
err() {
local parent_lineno="$1"
local message="$2"
local code="${3:-1}"
if [[ -n "$message" ]] ; then
error "Line ${parent_lineno}: ${message}: exiting with status ${code}"
else
error "Line ${parent_lineno}: exiting with status ${code}"
fi
reset
exit "${code}"
}
interrupt() {
warn "Script interrupted by user!"
reset
exit 130
}
reset(){
local new_containers=()
local backup_container=("$BACKUP_MISP" "$BACKUP_MYSQL" "$BACKUP_REDIS" "$BACKUP_MODULES")
local images=("$MISP_IMAGE_NAME" "$MYSQL_IMAGE_NAME" "$REDIS_IMAGE_NAME" "$MODULES_IMAGE_NAME")
if $RENAMED_MISP || ! $MISP_NEW_EQ_OLD; then
new_containers+=("$NEW_MISP")
fi
if $RENAMED_MYSQL || ! $MYSQL_NEW_EQ_OLD; then
new_containers+=("$NEW_MYSQL")
fi
if $RENAMED_REDIS || ! $REDIS_NEW_EQ_OLD; then
new_containers+=("$NEW_REDIS")
fi
if $RENAMED_MODULES || ! $MODULES_NEW_EQ_OLD; then
new_containers+=("$NEW_MODULES")
fi
warn "Reset to state before updating ..."
sleep 5
for container in "${new_containers[@]}"; do
for i in $(lxc list --format=json | jq -r '.[].name'); do
if [ "$i" == "$container" ]; then
lxc delete "$container" --force
fi
done
done
for image in "${images[@]}";do
for i in $(lxc image list --format=json | jq -r '.[].aliases[].name'); do
if [ "$i" == "$image" ]; then
lxc image delete "$image"
fi
done
done
if $RENAMED_MISP; then
renameContainer "$CURRENT_MISP" "$ORIGINAL_MISP"
fi
if $RENAMED_MYSQL; then
renameContainer "$CURRENT_MYSQL" "$ORIGINAL_MYSQL"
fi
if $RENAMED_REDIS; then
renameContainer "$CURRENT_REDIS" "$ORIGINAL_REDIS"
fi
if $RENAMED_MODULES; then
renameContainer "$CURRENT_MODULES" "$ORIGINAL_MODULES"
fi
warn "Restore snapshots ..."
lxc restore "$BACKUP_MISP" "$SNAP_NAME"
lxc restore "$BACKUP_MYSQL" "$SNAP_NAME"
lxc restore "$BACKUP_REDIS" "$SNAP_NAME"
if $CUSTOM_MODULES; then
lxc restore "$BACKUP_MODULES" "$SNAP_NAME"
fi
for container in "${backup_container[@]}"; do
local status
status=$(lxc list --format=json | jq -e --arg name "$container" '.[] | select(.name == $name) | .status')
if [ -n "$container" ] && [ "$status" == "\"Stopped\"" ]; then
lxc start "$container"
fi
done
# Delete snapshots
lxc delete "$BACKUP_MISP"/"$SNAP_NAME"
lxc delete "$BACKUP_MYSQL"/"$SNAP_NAME"
lxc delete "$BACKUP_REDIS"/"$SNAP_NAME"
if $CUSTOM_MODULES; then
lxc delete "$BACKUP_MODULES"/"$SNAP_NAME"
fi
}
restoreBackup(){
local container=$1
local backup=$2
if ! checkResourceExists "container" "$container"; then
error "Container $container could not be found!"
exit 1
fi
if lxc info "$container" | grep -q "$backup"; then
error "Backup $backup for container $container does not exist!"
exit 1
fi
lxc restore "$container" "$backup"
}
getAdditionalContainers(){
CURRENT_MYSQL=$(lxc exec "$CURRENT_MISP" -- bash -c "grep 'host' /var/www/MISP/app/Config/database.php | awk '{print \$3}' | sed "s/'.lxd'//g" | sed 's/[^a-zA-Z0-9-]//g'")
if ! checkLXDContainerRunning "$CURRENT_MYSQL"; then
error "Container $CURRENT_MYSQL is not running!"
fi
# Check root pwd
if $MISP || $MYSQL; then
if ! lxc exec "$CURRENT_MYSQL" -- mysql -u "root" -p"$MYSQL_ROOT_PASSWORD" -e "quit" > /dev/null 2>&1; then
error "MySQL root pwd is invalid!"
exit 1
fi
fi
local redis_host
redis_host=$(lxc exec "$CURRENT_MISP" -- bash -c 'sudo -u "www-data" -H sh -c "/var/www/MISP/app/Console/cake Admin getSetting MISP.redis_host"')
CURRENT_REDIS=$(echo "$redis_host" | jq -r '.value' | sed "s/\\.lxd$//")
if ! checkLXDContainerRunning "$CURRENT_REDIS"; then
error "Container $CURRENT_REDIS is not running!"
fi
CUSTOM_MODULES=true
local modules_host
modules_host=$(lxc exec "$CURRENT_MISP" -- bash -c 'sudo -u "www-data" -H sh -c "/var/www/MISP/app/Console/cake Admin getSetting Plugin.Enrichment_services_url"')
CURRENT_MODULES=$(echo "$modules_host" | jq -r '.value' | sed "s/\\.lxd$//")
if [ "$CURRENT_MODULES" == "http://127.0.0.1" ]; then
CUSTOM_MODULES=false
fi
if $CUSTOM_MODULES; then
if ! checkLXDContainerRunning "$CURRENT_MODULES"; then
error "Container $CURRENT_MODULES is not running!"
fi
fi
# Save original names
ORIGINAL_MISP=$CURRENT_MISP
ORIGINAL_MYSQL=$CURRENT_MYSQL
ORIGINAL_REDIS=$CURRENT_REDIS
if $CUSTOM_MODULES; then
ORIGINAL_MODULES=$CURRENT_MODULES
fi
}
cleanupMISP(){
lxc image delete "$MISP_IMAGE_NAME"
rm -r "$TEMP"
}
createRedisSocket(){
trap 'err ${LINENO}' ERR
local file_path="/etc/redis/redis.conf"
local lines_to_add="# create a unix domain socket to listen on\nunixsocket /var/run/redis/redis.sock\n# set permissions for the socket\nunixsocketperm 775"
lxc exec "$NEW_MISP" -- usermod -g www-data redis
lxc exec "$NEW_MISP" -- mkdir -p /var/run/redis/
lxc exec "$NEW_MISP" -- chown -R redis:www-data /var/run/redis
lxc exec "$NEW_MISP" -- cp "$file_path" "$file_path.bak"
lxc exec "$NEW_MISP" -- bash -c "echo -e \"$lines_to_add\" | cat - \"$file_path\" >tempfile && mv tempfile \"$file_path\""
lxc exec "$NEW_MISP" -- usermod -aG redis www-data
lxc exec "$NEW_MISP" -- service redis-server restart
# Modify php.ini
local php_ini_path="/etc/php/$PHP_VERSION/apache2/php.ini"
local socket_path="/var/run/redis/redis.sock"
lxc exec "$NEW_MISP" -- sed -i "s|;session.save_path = \"/var/lib/php/sessions\"|session.save_path = \"$socket_path\"|; s|session.save_handler = files|session.save_handler = redis|" "$php_ini_path"
lxc exec "$NEW_MISP" -- sudo service apache2 restart
}
updateMISP(){
trap 'err ${LINENO}' ERR
info "Update MISP..."
TEMP=$(mktemp -d)
# Check if the directory was created successfully
if [ -z "$TEMP" ]; then
error "Creating temporary directory."
exit 1
fi
okay "Created temporary directory $TEMP."
# Pull config
info "Extract files from current MISP..."
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/app/files "$TEMP" -v
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/app/tmp "$TEMP" -v
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/app/Config "$TEMP" -v
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/app/webroot/img "$TEMP"/webroot/ -v
lxc file pull "$CURRENT_MISP"/var/www/MISP/app/webroot/gpg.asc "$TEMP"/webroot/ -v
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/app/View/Emails/html/Custom "$TEMP"/View/Emails/html/ -v
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/app/View/Emails/text/Custom "$TEMP"/View/Emails/text/ -v
lxc file pull "$CURRENT_MISP"/var/www/MISP/app/Plugin/CakeResque/Config/config.php "$TEMP"/Plugin/CakeResque/Config/ -v
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/.gnupg/openpgp-revocs.d "$TEMP"/.gnupg/ -v
lxc file pull -r "$CURRENT_MISP"/var/www/MISP/.gnupg/private-keys-v1.d "$TEMP"/.gnupg/ -v
lxc file pull "$CURRENT_MISP"/var/www/MISP/.gnupg/pubring.kbx "$TEMP"/.gnupg/ -v
lxc file pull "$CURRENT_MISP"/var/www/MISP/.gnupg/pubring.kbx~ "$TEMP"/.gnupg/ -v
lxc file pull "$CURRENT_MISP"/var/www/MISP/.gnupg/trustdb.gpg "$TEMP"/.gnupg/ -v
info "Get additional config..."
MYSQL_USER=$(lxc exec "$CURRENT_MISP" -- bash -c "grep 'login' /var/www/MISP/app/Config/database.php | awk '{print \$3}' | sed 's/[^a-zA-Z0-9]//g'")
PHP_VERSION=$(lxc exec "$CURRENT_MISP" -- bash -c "php -v | head -n 1 | awk '{print \$2}' | cut -d '.' -f 1,2")
PHP_MEMORY_LIMIT=$(lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- grep "memory_limit" /etc/php/"$PHP_VERSION"/apache2/php.ini | awk -F' = ' '{print $2}')
PHP_MAX_EXECUTION_TIME=$(lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- grep "max_execution_time" /etc/php/"$PHP_VERSION"/apache2/php.ini | awk -F' = ' '{print $2}')
PHP_UPLOAD_MAX_FILESIZE=$(lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- grep "upload_max_filesize" /etc/php/"$PHP_VERSION"/apache2/php.ini | awk -F' = ' '{print $2}')
PHP_POST_MAX_SIZE=$(lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- grep "post_max_size" /etc/php/"$PHP_VERSION"/apache2/php.ini | awk -F' = ' '{print $2}')
# Import new image
info "Import image..."
lxc image import "$MISP_IMAGE" --alias "$MISP_IMAGE_NAME"
# Create new instance
info "Create new MISP instance..."
local profile
profile=$(lxc config show "$CURRENT_MISP" | yq eval '.profiles | join(" ")' -)
lxc launch "$MISP_IMAGE_NAME" "$NEW_MISP" --profile="$profile"
# Transfer files to new instance
echo "Push files to new MISP instance"
lxc file push -r "$TEMP"/files "$NEW_MISP"/var/www/MISP/app/ -v
lxc file push -r "$TEMP"/tmp "$NEW_MISP"/var/www/MISP/app/ -v
lxc file push -r "$TEMP"/Config "$NEW_MISP"/var/www/MISP/app/ -v
lxc file push -r "$TEMP"/webroot/img "$NEW_MISP"/var/www/MISP/app/webroot/ -v
lxc file push "$TEMP"/webroot/gpg.asc "$NEW_MISP"/var/www/MISP/app/webroot/ -v
lxc file push -r "$TEMP"/View/Emails/html/Custom "$NEW_MISP"/var/www/MISP/app/View/Emails/html/ -v
lxc file push -r "$TEMP"/View/Emails/text/Custom "$NEW_MISP"/var/www/MISP/app/View/Emails/text/ -v
lxc file push "$TEMP"/Plugin/CakeResque/Config/config.php "$NEW_MISP"/var/www/MISP/app/Plugin/CakeResque/Config/ -v
lxc file push -r "$TEMP"/.gnupg "$NEW_MISP"/var/www/MISP/ -v
# Set permissions
lxc exec "$NEW_MISP" -- sudo chown -R www-data:www-data $PATH_TO_MISP
lxc exec "$NEW_MISP" -- sudo chmod -R 750 $PATH_TO_MISP
lxc exec "$NEW_MISP" -- sudo chmod -R g+ws ${PATH_TO_MISP}/app/tmp
lxc exec "$NEW_MISP" -- sudo chmod -R g+ws ${PATH_TO_MISP}/app/files
lxc exec "$NEW_MISP" -- sudo chmod -R g+ws ${PATH_TO_MISP}/app/files/scripts/tmp
# Change host address on MySQL
if ! $MISP_NEW_EQ_OLD; then
lxc exec "$CURRENT_MYSQL" -- mysql -u root -p"$MYSQL_ROOT_PASSWORD" -e "RENAME USER '$MYSQL_USER'@'$CURRENT_MISP.lxd' TO '$MYSQL_USER'@'$NEW_MISP.lxd';"
fi
# Apply php.ini config
lxc exec "$NEW_MISP" -- bash -c "grep -q '^memory_limit' /etc/php/$PHP_VERSION/apache2/php.ini && sed -i 's/^memory_limit.*/memory_limit = $PHP_MEMORY_LIMIT/' /etc/php/$PHP_VERSION/apache2/php.ini || echo 'memory_limit = $PHP_MEMORY_LIMIT' >> /etc/php/$PHP_VERSION/apache2/php.ini"
lxc exec "$NEW_MISP" -- bash -c "grep -q '^max_execution_time' /etc/php/$PHP_VERSION/apache2/php.ini && sed -i 's/^max_execution_time.*/max_execution_time = $PHP_MAX_EXECUTION_TIME/' /etc/php/$PHP_VERSION/apache2/php.ini || echo 'max_execution_time = $PHP_MAX_EXECUTION_TIME' >> /etc/php/$PHP_VERSION/apache2/php.ini"
lxc exec "$NEW_MISP" -- bash -c "grep -q '^upload_max_filesize' /etc/php/$PHP_VERSION/apache2/php.ini && sed -i 's/^upload_max_filesize.*/upload_max_filesize = $PHP_UPLOAD_MAX_FILESIZE/' /etc/php/$PHP_VERSION/apache2/php.ini || echo 'upload_max_filesize = $PHP_UPLOAD_MAX_FILESIZE' >> /etc/php/$PHP_VERSION/apache2/php.ini"
lxc exec "$NEW_MISP" -- bash -c "grep -q '^mempost_max_sizeory_limit' /etc/php/$PHP_VERSION/apache2/php.ini && sed -i 's/^post_max_size.*/post_max_size = $PHP_POST_MAX_SIZE/' /etc/php/$PHP_VERSION/apache2/php.ini || echo 'post_max_size = $PHP_POST_MAX_SIZE' >> /etc/php/$PHP_VERSION/apache2/php.ini"
lxc exec "$NEW_MISP" -- sudo service apache2 restart
createRedisSocket
# Update
lxc exec "$NEW_MISP" -- sudo -u www-data bash -c "$PATH_TO_MISP/app/Console/cake Admin runUpdates"
info "Stopping current MISP instance..."
lxc stop "$CURRENT_MISP"
cleanupMISP
}
cleanupMySQL(){
lxc image delete "$MYSQL_IMAGE_NAME"
rm -r "$TEMP"
}
editMySQLConf(){
local key=$1
local value=$2
local container=$3
lxc exec "$container" -- bash -c "\
if grep -q '^$key' /etc/mysql/mariadb.conf.d/50-server.cnf; then \
sed -i 's/^$key.*/$key = $value/' /etc/mysql/mariadb.conf.d/50-server.cnf; \
else \
awk -v $key='$key = $value' \
'/^\[mysqld\]/ {print; print $key; next} {print}' \
/etc/mysql/mariadb.conf.d/50-server.cnf > /tmp/php.ini.modified && \
mv /tmp/php.ini.modified /etc/mysql/mariadb.conf.d/50-server.cnf; \
fi"
}
updateMySQL(){
trap 'err ${LINENO}' ERR
info "Update MySQL..."
local profile
profile=$(lxc config show "$CURRENT_MYSQL" | yq eval '.profiles | join(" ")' -)
lxc image import "$MYSQL_IMAGE" --alias "$MYSQL_IMAGE_NAME"
lxc launch "$MYSQL_IMAGE_NAME" "$NEW_MYSQL" --profile="$profile"
sleep 2
# Apply config
lxc exec "$NEW_MYSQL" -- sed -i 's/bind-address = 127.0.0.1/bind-address = 0.0.0.0/' "/etc/mysql/mariadb.conf.d/50-server.cnf"
INNODB_BUFFER_POOL_SIZE=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_buffer_pool_size" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_buffer_pool_size=/ {print $2}')
INNODB_CHANGE_BUFFERING=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_change_buffering" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_change_buffering=/ {print $2}')
INNODB_IO_CAPACITY=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_io_capacity" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_io_capacity=/ {print $2}')
INNODB_IO_CAPACITY_MAX=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_io_capacity_max" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_io_capacity_max=/ {print $2}')
INNODB_LOG_FILE_SIZE=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_log_file_size" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_log_file_size=/ {print $2}')
INNODB_LOG_FILES_IN_GROUP=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_log_files_in_group" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_log_files_in_group=/ {print $2}')
INNODB_READ_IO_THREADS=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_read_io_threads" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_read_io_threads=/ {print $2}')
INNODB_STATS_PERISTENT=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_stats_persistent" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_stats_persistent=/ {print $2}')
INNODB_WRITE_IO_THREADS=$(lxc exec "$CURRENT_MYSQL" -- grep "innodb_write_io_threads" /etc/mysql/mariadb.conf.d/50-server.cnf | awk -F '=' '/^innodb_write_io_threads=/ {print $2}')
# Modify the configuration file in the new container
if [ -n "$INNODB_WRITE_IO_THREADS" ]; then
editMySQLConf "innodb_write_io_threads" "$INNODB_WRITE_IO_THREADS" "$NEW_MYSQL"
fi
if [ -n "$INNODB_STATS_PERISTENT" ]; then
editMySQLConf "innodb_stats_persistent" "$INNODB_STATS_PERISTENT" "$NEW_MYSQL"
fi
if [ -n "$INNODB_READ_IO_THREADS" ]; then
editMySQLConf "innodb_read_io_threads" "$INNODB_READ_IO_THREADS" "$NEW_MYSQL"
fi
if [ -n "$INNODB_LOG_FILES_IN_GROUP" ]; then
editMySQLConf "innodb_log_files_in_group" "$INNODB_LOG_FILES_IN_GROUP" "$NEW_MYSQL"
fi
if [ -n "$INNODB_LOG_FILE_SIZE" ]; then
editMySQLConf "innodb_log_file_size" "$INNODB_LOG_FILE_SIZE" "$NEW_MYSQL"
fi
if [ -n "$INNODB_IO_CAPACITY_MAX" ]; then
editMySQLConf "innodb_io_capacity_max" "$INNODB_IO_CAPACITY_MAX" "$NEW_MYSQL"
fi
if [ -n "$INNODB_IO_CAPACITY" ]; then
editMySQLConf "innodb_io_capacity" "$INNODB_IO_CAPACITY" "$NEW_MYSQL"
fi
if [ -n "$INNODB_CHANGE_BUFFERING" ]; then
editMySQLConf "innodb_change_buffering" "$INNODB_CHANGE_BUFFERING" "$NEW_MYSQL"
fi
if [ -n "$INNODB_BUFFER_POOL_SIZE" ]; then
editMySQLConf "innodb_buffer_pool_size" "$INNODB_BUFFER_POOL_SIZE" "$NEW_MYSQL"
fi
# Move data
info "Copying data to new database ..."
TEMP=$(mktemp -d)
lxc exec "$CURRENT_MYSQL" -- mysqldump -u root -p"$MYSQL_ROOT_PASSWORD" --all-databases > "$TEMP"/backup.sql
lxc exec "$NEW_MYSQL" -- mysql -u root -p"$MYSQL_ROOT_PASSWORD" < "$TEMP"/backup.sql
lxc exec "$NEW_MYSQL" -- sudo systemctl restart mysql
# Configure MISP
lxc exec "$CURRENT_MISP" -- sed -i "s|$CURRENT_MYSQL.lxd|$NEW_MYSQL.lxd|" "$PATH_TO_MISP/app/Config/database.php"
lxc stop "$CURRENT_MYSQL"
cleanupMySQL
}
cleanupRedis(){
lxc image delete "$REDIS_IMAGE_NAME"
}
updateRedis(){
trap 'err ${LINENO}' ERR
info "Update Redis..."
local profile
profile=$(lxc config show "$CURRENT_REDIS" | yq eval '.profiles | join(" ")' -)
lxc image import "$REDIS_IMAGE" --alias "$REDIS_IMAGE_NAME"
lxc launch "$REDIS_IMAGE_NAME" "$NEW_REDIS" --profile="$profile"
# Configure new Redis
local port
port=$(lxc exec "$CURRENT_REDIS" -- grep "port" /etc/redis/redis.conf | awk '/^port/ {print $2}')
lxc exec "$NEW_REDIS" -- sed -i "s/^bind .*/bind 0.0.0.0/" "/etc/redis/redis.conf"
lxc exec "$NEW_REDIS" -- sed -i "s/^port .*/port $port/" "/etc/redis/redis.conf"
lxc exec "$NEW_REDIS" -- systemctl restart redis-server
# Configure MISP
lxc exec "$CURRENT_MISP" -- sed -i "s/'host' => '$CURRENT_REDIS.lxd'/'host' => '$NEW_REDIS.lxd'/; s/'port' => 6379/'port' => $port/" /var/www/MISP/app/Plugin/CakeResque/Config/config.php
lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- /var/www/MISP/app/Console/cake Admin setSetting "Plugin.ZeroMQ_redis_host" "$NEW_REDIS.lxd"
lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- /var/www/MISP/app/Console/cake Admin setSetting "MISP.redis_host" "$NEW_REDIS.lxd"
lxc stop "$CURRENT_REDIS"
cleanupRedis
}
cleanupModules(){
lxc image delete "$MODULES_IMAGE_NAME"
}
checkModules(){
local instance=$1
if [ "$(lxc exec "$instance" -- systemctl is-active misp-modules)" = "active" ]; then
okay "Service misp-modules is running."
else
error "Service misp-modules is not running."
lxc stop "$instance"
return 1
fi
}
updateModules(){
trap 'err ${LINENO}' ERR
info "Update Modules..."
local profile
profile=$(lxc config show "$CURRENT_MODULES" | yq eval '.profiles | join(" ")' -)
lxc image import "$MODULES_IMAGE" --alias "$MODULES_IMAGE_NAME"
lxc launch "$MODULES_IMAGE_NAME" "$NEW_MODULES" --profile="$profile"
sleep 5
checkModules "$NEW_MODULES"
# configure MISP
lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- /var/www/MISP/app/Console/cake Admin setSetting "Plugin.Export_services_url" "$NEW_MODULES.lxd"
lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- /var/www/MISP/app/Console/cake Admin setSetting "Plugin.Import_services_url" "$NEW_MODULES.lxd"
lxc exec "$CURRENT_MISP" -- sudo -H -u www-data -- /var/www/MISP/app/Console/cake Admin setSetting "Plugin.Enrichment_services_url" "$NEW_MODULES.lxd"
lxc stop "$CURRENT_MODULES"
cleanupModules
}
renameContainer(){
local container=$1
local new_name=$2
if checkLXDContainerRunning "$container"; then
lxc stop "$container"
lxc mv "$container" "$new_name"
lxc start "$new_name"
else
lxc mv "$container" "$new_name"
fi
}
checkLXDContainerRunning() {
local container_name=$1
local state
state=$(lxc list "^${container_name}$" --format csv -c s)
if [ "$state" == "RUNNING" ]; then
return 0
fi
return 1
}
log_to_file() {
local log_message=$1
local log_file=$2
# Create dir and log file if it does not exist
if [ ! -d "$(dirname "$log_file")" ]; then
mkdir -p "$(dirname "$log_file")"
fi
local current_time
current_time=$(date "+%Y-%m-%d %H:%M:%S")
echo "[$current_time] $log_message" >> "$log_file"
}
interactiveConfig(){
echo
echo "################################################################################"
echo -e "# Welcome to the ${BLUE}MISP-airgap${NC} Update Script #"
echo "#------------------------------------------------------------------------------#"
echo -e "# This update script will guide you through the process of updating your #"
echo -e "# ${BLUE}MISP${NC} installation within an LXD environment. #"
echo -e "# #"
echo -e "# ${VIOLET}Attention:${NC} #"
echo -e "# ${VIOLET}It is recommended to back up your current configuration and data before ${NC}#"
echo -e "# ${VIOLET}proceeding with the update to prevent any potential data loss. ${NC} #"
echo -e "# #"
echo -e "# ${VIOLET}The update process will try to retain your settings, but it is advisable to ${NC} #"
echo -e "# ${VIOLET}review them post-update to ensure everything is configured as expected.${NC} #"
echo -e "# #"
echo "################################################################################"
echo
declare -A nameCheckArray
# Ask for current MISP
while true; do
read -r -p "Name of the current misp container: " current_misp
CURRENT_MISP=${current_misp:-$default_current_misp}
if ! checkResourceExists "container" "$CURRENT_MISP"; then
error "Container '$CURRENT_MISP' does not exist."
continue
fi
break
done
# Ask for MISP update
read -r -p "Do you want to update MISP (y/n, default: $default_misp): " misp
misp=${misp:-$default_misp}
MISP=$(echo "$misp" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
if $MISP; then
# Ask for MISP image
while true; do
read -r -e -p "What is the path to the MISP image (default: $default_misp_img): " misp_img
misp_img=${misp_img:-$default_misp_img}
if [ ! -f "$misp_img" ]; then
error "The specified file does not exist."
continue
fi
MISP_IMAGE=$misp_img
break
done
# Ask for new MISP container name
while true; do
read -r -p "Name of the new MISP container (default: $default_new_misp): " new_misp
NEW_MISP=${new_misp:-$default_new_misp}
if [[ ${nameCheckArray[$NEW_MISP]+_} ]]; then
error "Name '$NEW_MISP' has already been used. Please choose a different name."
continue
fi
if ! checkNamingConvention "$NEW_MISP"; then
continue
fi
if checkResourceExists "container" "$NEW_MISP"; then
warn "Container '$NEW_MISP' already exists."
read -r -p "Do you want to proceed anyways (y/n, default: n): " rename_misp
RENAME=$(echo "$rename_misp" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
if $RENAME; then
nameCheckArray[$NEW_MISP]=1
break
fi
continue
fi
nameCheckArray[$NEW_MISP]=1
break
done
while true; do
read -r -p "Please enter your MySQL root pwd: " root_pwd
MYSQL_ROOT_PASSWORD=${root_pwd}
# Add check?
break
done
fi
# Ask for MySQL update
read -r -p "Do you want to update MySQL (y/n, default: $default_mysql): " mysql
mysql=${mysql:-$default_mysql}
MYSQL=$(echo "$mysql" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
if $MYSQL; then
# Ask for MySQL image
while true; do
read -r -e -p "What is the path to the MySQL image (default: $default_mysql_img): " mysql_img
mysql_img=${mysql_img:-$default_mysql_img}
if [ ! -f "$mysql_img" ]; then
error "The specified file does not exist."
continue
fi
MYSQL_IMAGE=$mysql_img
break
done
# Ask for new MySQL container name
while true; do
read -r -p "Name of the new MySQL container (default: $default_new_mysql): " new_mysql
NEW_MYSQL=${new_mysql:-$default_new_mysql}
if [[ ${nameCheckArray[$NEW_MYSQL]+_} ]]; then
error "Name '$NEW_MYSQL' has already been used. Please choose a different name."
continue
fi
if ! checkNamingConvention "$NEW_MYSQL"; then
continue
fi
if checkResourceExists "container" "$NEW_MYSQL"; then
warn "Container '$NEW_MYSQL' already exists."
read -r -p "Do you want to proceed anyways (y/n, default: n): " rename_mysql
RENAME=$(echo "$rename_mysql" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
if $RENAME; then
nameCheckArray[$NEW_MYSQL]=1
break
fi
continue
fi
nameCheckArray[$NEW_MYSQL]=1
break
done
if ! $MISP; then
while true; do
read -r -p "Please enter your MySQL root pwd: " root_pwd
MYSQL_ROOT_PASSWORD=${root_pwd}
# Add check?
break
done
fi
fi
read -r -p "Do you want to update Redis (y/n, default: $default_redis): " redis
redis=${redis:-$default_redis}
REDIS=$(echo "$redis" | grep -iE '^y(es)?$' > /dev/null && echo true || echo false)
if $REDIS; then
# Ask for Redis image
while true; do
read -r -e -p "What is the path to the Redis image (default: $default_redis_img): " redis_img
redis_img=${redis_img:-$default_redis_img}
if [ ! -f "$redis_img" ]; then
error "The specified file does not exist."
continue
fi
REDIS_IMAGE=$redis_img
break
done
# Ask for new Redis container name
while true; do
read -r -p "Name of the new Redis container (default: $default_new_redis): " new_redis
NEW_REDIS=${new_redis:-$default_new_redis}
if [[ ${nameCheckArray[$NEW_REDIS]+_} ]]; then
error "Name '$NEW_REDIS' has already been used. Please choose a different name."
continue
fi
if ! checkNamingConvention "$NEW_REDIS"; then
continue