-
Notifications
You must be signed in to change notification settings - Fork 3
/
build.sh
executable file
·890 lines (827 loc) · 29.6 KB
/
build.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
#!/usr/bin/env bash -u
#
# Prepare OpenSSL FIPS object module source code and build Nginx Docker image.
trap ctrl_c INT # Handle interrupts
printf "\n"
##############################################################################
# DEPENDENCY CHECK
##############################################################################
# Ensure that Docker is installed
command -v docker >/dev/null 2>&1 || {
echo >&2 "\nYou need to install Docker first."
exit 1
}
##############################################################################
# GLOBAL VARIABLES
##############################################################################
# Pretty terminal colors
BOLD=$( tput bold )
RED=$( tput setaf 1 )
GREEN=$( tput setaf 2 )
WHITE=$( tput setaf 7 )
YELLOW=$(tput setaf 3)
NC=$( tput sgr0 )
# Global default configuration variables
DEFAULT_CONTAINER_NAME="api-gateway"
DEFAULT_EXPOSE_PORTS="-p 8080:80 -p 4443:443"
DEFAULT_IMAGE_TAG="$USER/nginx-fips"
DEFAULT_OPENSSL_FIPS_DIR="openssl-fips-2.0.12"
DEFAULT_OPENSSL_FIPS_VERSION="2.0.10"
DEFAULT_SRC_PATH="src"
DEFAULT_LIB_PATH="lib"
DEFAULT_NGINX_PATH="${PWD}/$DEFAULT_SRC_PATH/nginx-1.11.2"
DEFAULT_NGINX_CONF_PATH="${PWD}/$DEFAULT_LIB_PATH/conf"
DEFAULT_CERTIFICATE_PATH="${PWD}/$DEFAULT_LIB_PATH/certs"
DEFAULT_CERTIFICATE_KEY_PATH="${PWD}/$DEFAULT_LIB_PATH/private"
DEFAULT_TMP_PATH="${PWD}/tmp"
# Global build variables
CERTIFICATE_PATH=
CERTIFICATE_KEY_PATH=
CONTAINER_NAME=
DEFAULT=0
EXPOSE_PORTS=""
FILENAME=
IMAGE_TAG=
MOUNT_PATHS=""
NGINX_CONF_PATH=
NGINX_CONF_MOUNT_PATH=""
NGINX_PATH=
NOCD=0
NOCLEAN=0
NOEXPOSE=0
OPENSSL_FIPS_CDROM_PATH=
OPENSSL_FIPS_DIR=
OPENSSL_FIPS_EXTRACT_PATH=
OPENSSL_FIPS_VERSION=
QUIET=""
RUN=0
SSL_CERTS_MOUNT_PATH=""
SSL_PRIVATE_MOUNT_PATH=""
##############################################################################
# FUNCTIONS
##############################################################################
##############################################################################
# Prints a description of valid command-line arguments
# Globals:
# None
# Arguments:
# None
# Returns:
# None
##############################################################################
print_help () {
printf "\n\
--default Use default settings; do not prompt user for input \n\
--openssl-fips-cdrom-path Path for the volume (CD-ROM) with the source \n\
--openssl-fips-version Version of the source to install \n\
--ssl-cert-path Path for SSL certificate(s) \n\
--ssl-key-path Path for SSL certificate key(s) \n\
--nginx-conf-path Path for Nginx conf directory to use in deployment \n\
--image-tag Tag for the Docker image to be built \n\
--run Attempt to run the Docker image when finished \n\
--name If the --run option exists, the container will have this\n\
name
--port If the --run option exists, the container will expose \n\
this port (can include as many as needed, e.g. \n\
--port 8080:80 --port 4443:443) \n\
--no-expose If the --run option exists, don't expose any ports \n\
--nginx-conf-mount-path If the --run option exists, mount this path as the Nginx\n\
conf folder
--ssl-cert-mount-path If the --run option exists, mount this path as the SSL \n\
certs folder. \n\
--ssl-private-mount-path If the --run option exists, mount this path as the SSL \n\
private folder. If --ssl-cert-mount-path is defined,\n\
but this option is not, then --ssl-cert-mount-path \n\
will also be mounted as the SSL private folder \n\
--no-clean Don't clean up temporary files after build \n\
--no-cd Don't prompt user for any CD-ROM related information, \n\
and just use the provided FIPS object module source \n\
--quiet Try to repress verbose Docker output \n\
--help Show this help\n\n"
exit 1
}
# Parse command line arguments
while getopts "h:-:" opt; do
case "$opt" in
-)
case "${OPTARG}" in
help)
print_help
break
;;
no-clean)
NOCLEAN=1
;;
run)
printf "${NC}An attempt will be made to run a container after the image is built.\n"
RUN=1
;;
no-expose)
NOEXPOSE=1
;;
no-cd)
printf "${NC}No attempt will be made to locate the OpenSSL Software Foundation CD-ROM.\n"
NOCD=1
;;
port)
val=${!OPTIND}
printf "${NC}Forwarding ports: $val\n"
EXPOSE_PORTS="$EXPOSE_PORTS -p $val"
OPTIND=$(( $OPTIND + 1 ))
;;
quiet)
QUIET="-q"
;;
default)
printf "${NC}Skipping manual build configuration and using default settings.${NC}\n"
DEFAULT=1
break
;;
openssl-fips-cdrom-path)
val="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
if [ -d "$val" ]
then
OPENSSL_FIPS_CDROM_PATH="$val"
printf "\n${NC}Using OpenSSL FIPS CD-ROM path: $val${NC}\n" >&2;
else
printf "\n${RED}Could not locate path: $val${NC}\n"
fi
;;
ssl-cert-path)
val="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
if [ -d "$val" ]
then
CERTIFICATE_PATH="$val"
printf "\n${NC}Using SSL certificate(s) path: $val${NC}\n" >&2;
else
printf "\n${RED}Could not locate path: $val${NC}\n"
fi
;;
ssl-key-path)
val="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
if [ -d "$val" ]
then
CERTIFICATE_KEY_PATH="$val"
printf "\n${NC}Using SSL certificate key(s) path: $val${NC}\n" >&2;
else
printf "\n${RED}Could not locate path: $val${NC}\n"
fi
;;
nginx-conf-path)
val="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
if [ -d "$val" ]
then
NGINX_CONF_PATH="$val"
printf "\n${NC}Using Nginx conf folder path: $val${NC}\n" >&2;
else
printf "\n${RED}Could not locate path: $val${NC}\n"
fi
;;
openssl-fips-version)
OPENSSL_FIPS_VERSION="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
printf "${NC}Using OpenSSL FIPS version: $OPENSSL_FIPS_VERSION ${NC}\n" >&2;
;;
image-tag)
IMAGE_TAG="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
printf "${NC}Using Docker image tag: $IMAGE_TAG${NC}\n" >&2;
;;
nginx-conf-mount-path)
val="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
if [ -d "$val" ]
then
NGINX_CONF_MOUNT_PATH="$val"
printf "${NC}Will mount Nginx conf folder path: $val${NC}\n" >&2;
else
printf "\n${RED}Could not locate path: $val${NC}\n"
fi
;;
ssl-certs-mount-path)
val="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
if [ -d "$val" ]
then
SSL_CERTS_MOUNT_PATH="$val"
printf "${NC}Will mount SSL certs folder path: $val${NC}\n" >&2;
else
printf "\n${RED}Could not locate path: $val${NC}\n"
fi
;;
ssl-private-mount-path)
val="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
if [ -d "$val" ]
then
SSL_PRIVATE_MOUNT_PATH="$val"
printf "${NC}Will mount SSL private folder path: $val${NC}\n" >&2;
else
printf "\n${RED}Could not locate path: $val${NC}\n"
fi
;;
name)
CONTAINER_NAME="${!OPTIND}"
OPTIND=$(( $OPTIND + 1 ))
printf "${NC}Using Docker container name: $CONTAINER_NAME${NC}\n" >&2;
;;
*)
if [ "$OPTERR" = 1 ] && [ "${optspec:0:1}" != ":" ]
then
printf "\n${RED}Unknown option --${OPTARG}${NC}\n" >&2
fi
;;
esac;;
esac
done
##############################################################################
# Get user OS type and set default paths acccordingly
# Globals:
# DEFAULT_OPENSSL_FIPS_CDROM_PATH
# OS
# Arguments:
# None
# Returns:
# None
##############################################################################
get_os () {
if [ "$(uname)" == "Darwin" ]
then
OS="Mac OS X"
DEFAULT_OPENSSL_FIPS_CDROM_PATH="/Volumes/OpenSSL"
elif [ "$(expr substr $(uname -s) 1 5)" == "Linux" ]
then
OS="Linux"
DEFAULT_OPENSSL_FIPS_CDROM_PATH="/media/cdrom"
elif [ "$(expr substr $(uname -s) 1 10)" == "MINGW32_NT" ]
then
printf "\n${RED}Windows is not supported.${NC}"
fi
}
##############################################################################
# Remove temporary folders
# Globals:
# DEFAULT_NGINX_PATH
# DEFAULT_TMP_PATH
# NOCLEAN
# Arguments:
# None
# Returns:
# None
##############################################################################
clean_up () {
# Remove temporary folder if it exists
if [ -d "$DEFAULT_TMP_PATH" ] && ( [ $NOCLEAN -eq 0 ] || [ $# -eq 0 ] )
then
printf "\n${NC}Removing existing temporary folder...\n"
rm -rf $DEFAULT_TMP_PATH
if [ $? -ne 0 ]
then
printf "${RED}Failed to remove existing temporary folder. ${NC}\n"
exit 1
else
printf "${GREEN}Temporary folder successfully removed.${NC}\n"
fi
fi
# Remove Nginx Makefile if it exists
if [ -s "$DEFAULT_NGINX_PATH/Makefile" ] && ( [ $NOCLEAN -eq 0 ] || [ $# -eq 0 ] )
then
printf "\n${NC}Removing existing Nginx Makefile..."
rm $DEFAULT_NGINX_PATH/Makefile
if [ $? -ne 0 ]
then
printf "\n${RED}Failed to remove Nginx Makefile. ${NC}\n"
exit 1
else
printf "\n${GREEN}Nginx Makefile successfully removed.${NC}"
fi
fi
dangling=$(docker images --quiet --filter "dangling=true")
if [ -n "$dangling" ] && ( [ $NOCLEAN -eq 0 ] || [ $# -eq 0 ] )
then
printf "\nRemoving dangling Docker images...\n"
sleep 2 # Docker needs time to delete related containers
docker rmi $(docker images --quiet --filter "dangling=true")
fi
# If passed an error code, exit
if [ ${1+x} ]
then
exit $1
fi
}
##############################################################################
# Prints informational messages (if provided), and calls clean_up if passed
# an error code
# Globals:
# None
# Arguments:
# None
# Returns:
# None
##############################################################################
clean_up_on_error () {
if [ $1 -ne 0 ]
then
if [ -n "$3" ]
then
printf "${RED}$3${NC}\n"
fi
clean_up $1
elif [ -n "$2" ]
then
printf "${GREEN}$2${NC}\n"
fi
}
##############################################################################
# Create a new temporary folder for the source code
# Globals:
# NOCLEAN
# DEFAULT_TMP_PATH
# Arguments:
# None
# Returns:
# None
##############################################################################
set_up () {
printf "${NC}Creating new temporary folder...\n"
mkdir $DEFAULT_TMP_PATH
clean_up_on_error $? \
"Temporary folder successfully created: ${BOLD}$DEFAULT_TMP_PATH" \
"Failed to create new temporary folder: ${BOLD}$DEFAULT_TMP_PATH"
mkdir $DEFAULT_TMP_PATH/certs
clean_up_on_error $? \
"Temporary folder successfully created: ${BOLD}$DEFAULT_TMP_PATH/certs" \
"Failed to create new temporary folder: ${BOLD}$DEFAULT_TMP_PATH/certs"
mkdir $DEFAULT_TMP_PATH/private
clean_up_on_error $? \
"Temporary folder successfully created: ${BOLD}$DEFAULT_TMP_PATH/private" \
"Failed to create new temporary folder: ${BOLD}$DEFAULT_TMP_PATH/private"
}
##############################################################################
# Prompt the user to provide a name for their Docker container
# Globals:
# CONTAINER_NAME
# DEFAULT_CONTAINER_NAME
# Arguments:
# None
# Returns:
# None
##############################################################################
get_container_name () {
printf "\nPlease enter a name for your container. It ${BOLD}must${NC}${NC} be unique.\n"
read -ep "${BOLD}(default: $DEFAULT_CONTAINER_NAME): ${NC}${NC}" container_name
if [ -z $container_name ]
then
CONTAINER_NAME="$DEFAULT_CONTAINER_NAME"
else
CONTAINER_NAME="$container_name"
fi
}
##############################################################################
# Run Docker using the built image
# Globals:
# CONTAINER_NAME
# IMAGE_TAG
# Arguments:
# None
# Returns:
# None
##############################################################################
run_docker () {
docker run \
-d \
--name $CONTAINER_NAME \
$MOUNT_PATHS \
$EXPOSE_PORTS \
$IMAGE_TAG & \
wait
clean_up
exit 0
}
##############################################################################
# Build the Nginx Docker image with the OpenSSL/FIPS source code
# Globals:
# DEFAULT_LIB_PATH
# DEFAULT_SRC_PATH
# IMAGE_TAG
# USER
# Arguments:
# {String} Location of the extracted OpenSSL FIPS object module source code
# Returns:
# None
##############################################################################
build_docker_image () {
if [ -z $IMAGE_TAG ]
then
printf "\nPlease provide a tag for your Docker image.\n"
read -ep "${BOLD}(default: $DEFAULT_IMAGE_TAG): ${NC}${NC}" IMAGE_TAG
if [ -z $IMAGE_TAG ]
then
IMAGE_TAG="$DEFAULT_IMAGE_TAG"
fi
fi
printf "\nTagging image as: ${BOLD}$IMAGE_TAG${NC}${NC}\n"
printf "Building image using OpenSSL FIPS object module source code path:\n\t${BOLD}$1${NC}${NC}\n\n"
if [ "$QUIET" == "-q" ]
then
printf "Since the --quiet${NC} option was set, there won't be much output. Please be patient...\n"
fi
docker build \
$QUIET \
-t $IMAGE_TAG \
--build-arg OPENSSL_FIPS_PATH=$1 \
--build-arg SRC_PATH=$DEFAULT_SRC_PATH \
--build-arg LIB_PATH=$DEFAULT_LIB_PATH .
clean_up_on_error $? \
"${BOLD}You did it, buddy!" \
"There was a problem building the Docker image"
printf "\n${GREEN}${BOLD}You did it, buddy!${NC}${NC}\n"
if [ $RUN -eq 1 ]
then
if [ -z $CONTAINER_NAME ]
then
get_container_name
fi
if [ -z "$EXPOSE_PORTS" ]
then
if [ $NOEXPOSE -eq 0 ]
then
EXPOSE_PORTS="$DEFAULT_EXPOSE_PORTS"
else
EXPOSE_PORTS=""
fi
fi
if [ -n "$NGINX_CONF_MOUNT_PATH" ]
then
MOUNT_PATHS="$MOUNT_PATHS -v $NGINX_CONF_MOUNT_PATH:/usr/local/nginx/conf"
fi
if [ -n "$SSL_CERTS_MOUNT_PATH" ]
then
MOUNT_PATHS="$MOUNT_PATHS -v $SSL_CERTS_MOUNT_PATH:/usr/local/ssl/certs"
if [ -n "$SSL_PRIVATE_MOUNT_PATH" ]
then
MOUNT_PATHS="$MOUNT_PATHS -v $SSL_PRIVATE_MOUNT_PATH:/usr/local/ssl/private"
else
MOUNT_PATHS="$MOUNT_PATHS -v $SSL_CERTS_MOUNT_PATH:/usr/local/ssl/private"
fi
fi
run_docker
fi
clean_up
exit 0
}
##############################################################################
# Prompt the user for the OpenSSL Software Foundation CD path
# Globals:
# DEFAULT_OPENSSL_FIPS_CDROM_PATH
# OPENSSL_FIPS_CDROM_PATH
# Arguments:
# None
# Returns:
# None
##############################################################################
get_cdrom_volume_path () {
printf "\nPlease enter the path for your OpenSSL FIPS object module CD.\n"
read -ep "${BOLD}($OS default: $DEFAULT_OPENSSL_FIPS_CDROM_PATH): ${NC}${NC}" openssl_path
if [ -z $openssl_path ]
then
OPENSSL_FIPS_CDROM_PATH="$DEFAULT_OPENSSL_FIPS_CDROM_PATH"
else
if [ -d $openssl_path ]
then
OPENSSL_FIPS_CDROM_PATH="$openssl_path"
else
printf "\n${RED}Path not found: $openssl_path.${NC} Let's try that again.\n"
get_cdrom_volume_path
fi
fi
}
##############################################################################
# Prompt the user for the OpenSSL FIPS object module version number
# Globals:
# FILENAME
# OPENSSL_FIPS_VERSION
# Arguments:
# None
# Returns:
# None
##############################################################################
get_version () {
printf "\nPlease enter the version of the OpenSSL FIPS object module you would like\nto compile with your build.\n"
read -ep "${BOLD}(default: $DEFAULT_OPENSSL_FIPS_VERSION): ${NC}${NC}" OPENSSL_FIPS_VERSION
if [ -Z $OPENSSL_FIPS_VERSION ]
then
OPENSSL_FIPS_VERSION="$DEFAULT_OPENSSL_FIPS_VERSION"
fi
FILENAME="openssl-fips-$OPENSSL_FIPS_VERSION.tar.gz"
}
##############################################################################
# Extracts the FIPS module source code, then calls build_docker_image
# Globals:
# DEFAULT_TMP_PATH
# FILENAME
# OPENSSL_FIPS_CDROM_PATH
# OPENSSL_FIPS_EXTRACT_PATH
# OPENSSL_FIPS_VERSION
# Arguments:
# None
# Returns:
# None
##############################################################################
get_source_code () {
printf "\nExtracting OpenSSL FIPS object module ${BOLD}$OPENSSL_FIPS_VERSION${NC} source code from ${BOLD}$OPENSSL_FIPS_CDROM_PATH/$FILENAME${NC}...\n"
tar -xvf $OPENSSL_FIPS_CDROM_PATH/$FILENAME -C $DEFAULT_TMP_PATH
clean_up_on_error $? \
"Finished getting ${BOLD}OpenSSL FIPS object module $OPENSSL_FIPS_VERSION${NC}${GREEN} source." \
"Failed to copy and extract source code."
OPENSSL_FIPS_EXTRACT_PATH=${FILENAME%.tar.gz}
build_docker_image $OPENSSL_FIPS_EXTRACT_PATH
}
##############################################################################
# Generates self-signed SSL certificates and keys
# Globals:
# CERTIFICATE_PATH
# CERTIFICATE_KEY_PATH
# DEFAULT_CERTIFICATE_PATH
# DEFAULT_CERTIFICATE_KEY_PATH
# Arguments:
# None
# Returns:
# None
##############################################################################
generate_ssl_certificate () {
# Ensure that OpenSSL is installed
command -v openssl >/dev/null 2>&1 || {
printf >&2 "${RED}You need to install OpenSSL to generate self-signed SSL certificates.${NC}\n"
clean_up 1
}
openssl req -nodes -new -x509 -keyout $DEFAULT_CERTIFICATE_KEY_PATH/server.key -out $DEFAULT_CERTIFICATE_PATH/server.crt
clean_up_on_error $? \
"Finished generating ${RED}${BOLD}self-signed${NC}${BOLD} SSL certificate and key${NC}${GREEN}." \
"Failed to generate SSL certificate and key."
CERTIFICATE_PATH="$DEFAULT_CERTIFICATE_PATH"
CERTIFICATE_KEY_PATH="$DEFAULT_CERTIFICATE_PATH"
}
##############################################################################
# Copies SSL certificate key(s) to the temporary folder for building
# Globals:
# CERTIFICATE_KEY_PATH
# DEFAULT_CERTIFICATE_PATH
# Arguments:
# None
# Returns:
# None
##############################################################################
get_ssl_certificate_key () {
printf "\nPlease enter the path for your SSL certificate ${BOLD}key(s)${NC}.\n"
read -ep "${BOLD}(default: $DEFAULT_CERTIFICATE_KEY_PATH): ${NC}${NC}" key_path
if [ -z $key_path ]
then
CERTIFICATE_KEY_PATH="$DEFAULT_CERTIFICATE_KEY_PATH"
else
if [ -d $key_path ]
then
CERTIFICATE_KEY_PATH="$key_path"
else
printf "\n${RED}Path not found: $key_path.${NC} Let's try that again.\n"
get_ssl_certificate_key
fi
fi
}
##############################################################################
# Copies SSL certificate(s) to the temporary folder for building
# Globals:
# CERTIFICATE_PATH
# DEFAULT_CERTIFICATE_PATH
# Arguments:
# None
# Returns:
# None
##############################################################################
get_ssl_certificate () {
if [ -z $CERTIFICATE_PATH ]
then
while true
do
printf "\n${NC}Will you be providing your own ${BOLD}SSL certificate and key${NC}${NC}? ${BOLD}(y/n)\n"
read -ep "(default: y): ${NC}${NC}" yn
case $yn in
[Nn]* )
printf "\n${RED}${BOLD}WARNING:${NC}${RED} Self-signed certificates should not be used in production.${NC}\n"
read -resp $"Press any key to generate a self-signed SSL certificate and key..." -n1 key
generate_ssl_certificate
break
;;
* )
printf "\nPlease enter the path for your SSL certificate(s).\n"
read -ep "${BOLD}(default: $DEFAULT_CERTIFICATE_PATH): ${NC}${NC}" cert_path
if [ -z $cert_path ]
then
CERTIFICATE_PATH="$DEFAULT_CERTIFICATE_PATH"
get_ssl_certificate_key
else
if [ -d $cert_path ]
then
CERTIFICATE_PATH="$cert_path"
get_ssl_certificate_key
else
printf "\n${RED}Path not found: $cert_path.${NC} Let's try that again.\n"
get_ssl_certificate
fi
fi
break
;;
esac
done
fi
cp -R $CERTIFICATE_PATH $DEFAULT_TMP_PATH/
clean_up_on_error $? \
"Finished copying SSL certificate(s) folder to temporary location." \
"Failed to copy SSL certificate(s) folder to temporary location."
cp -R $CERTIFICATE_KEY_PATH $DEFAULT_TMP_PATH/
clean_up_on_error $? \
"Finished copying SSL certificate key(s) folder to temporary location." \
"Failed to copy SSL certificate key(s) folder to temporary location."
}
##############################################################################
# Copies files from a particular path to a temporary location
# Globals:
# DEFAULT_NGINX_CONF_PATH
# DEFAULT_TMP_PATH
# NGINX_CONF_PATH
# Arguments:
# None
# Returns:
# None
##############################################################################
get_nginx_conf () {
if [ -z $NGINX_CONF_PATH ]
then
printf "\nPlease enter the path for the Nginx conf folder you would like to use.\n"
read -p "${BOLD}(default: $DEFAULT_NGINX_CONF_PATH): ${NC}${NC}" conf_path
if [ -z $conf_path ]
then
NGINX_CONF_PATH="$DEFAULT_NGINX_CONF_PATH"
else
if [ -d $conf_path ]
then
NGINX_CONF_PATH="$conf_path"
else
printf "\n${RED}Path not found: $conf_path.${NC} Let's try that again.\n"
get_cdrom_volume_path
fi
fi
fi
mkdir $DEFAULT_TMP_PATH/conf \
&& cp $NGINX_CONF_PATH/* $DEFAULT_TMP_PATH/conf
clean_up_on_error $? \
"Finished copying Nginx conf folder to temporary location." \
"Failed to copy Nginx conf folder to temporary location."
}
##############################################################################
# Get Nginx-related paths and copy files if necessary
# Globals:
# NGINX_CONF_MOUNT_PATH
# SSL_CERTS_MOUNT_PATH
# Arguments:
# None
# Returns:
# None
##############################################################################
get_nginx_paths () {
if [ -z $NGINX_CONF_MOUNT_PATH ]
then
get_nginx_conf
fi
if [ -z $SSL_CERTS_MOUNT_PATH ]
then
get_ssl_certificate
else
$CERTIFICATE_PATH=$SSL_CERTS_MOUNT_PATH
$CERTIFICATE_KEY_PATH=$SSL_PRIVATE_MOUNT_PATH
get_ssl_certificate
fi
}
##############################################################################
# If default flag is used, handle for the case that there is no CD-ROM in the
# default path
# Globals:
# DEFAULT_OPENSSL_FIPS_DIR
# Arguments:
# None
# Returns:
# None
##############################################################################
handle_no_cd () {
printf "\n${RED}${BOLD}WARNING:${NC}${RED} This image will not be FIPS-compliant.${NC}\n"
read -resp $"Press any key to continue..." -n1 key
get_nginx_paths
build_docker_image $DEFAULT_OPENSSL_FIPS_DIR
}
##############################################################################
# Attempts a graceful clean-up if Ctrl+C interrupt is detected
# Globals:
# None
# Arguments:
# None
# Returns:
# None
##############################################################################
function ctrl_c () {
if [ $NOCLEAN -eq 0 ]
then
printf "\n${BOLD}Attempting graceful clean-up.${NC}${NC}\n"
clean_up 1
else
printf "\n${RED}${BOLD}Would have attempted graceful clean-up, but --no-clean${RED} option was set.${NC}${NC}\n"
printf "Temporary files and dangling Docker images have ${BOLD}not${NC}${NC} been deleted.\n"
exit 1
fi
}
##############################################################################
# MAIN
##############################################################################
# Get the user OS
get_os
# Remove any existing scaffolding
clean_up
# Create temporary folder(s)
set_up
if [ $DEFAULT -eq 0 ] && [ -z $OPENSSL_FIPS_CDROM_PATH ]
then
if [ $NOCD -eq 0 ]
then
printf "\n\n${NC}This ${BOLD}Dockerfile${NC}${NC} will build an image including ${BOLD}Nginx${NC}${NC} compiled with \
${BOLD}OpenSSL${NC}${NC}\nand the ${BOLD}OpenSSL FIPS object module${NC}${NC} running on the latest Ubuntu. To be\nFIPS \
compliant, the OpenSSL FIPS object module source code must be copied\nfrom a ${BOLD}CD provided by the OpenSSL Software Foundation${NC}${NC}."
while true
do
printf "\n\n${NC}Do you have a CD provided by the OpenSSL Software Foundation? ${BOLD}(y/n)\n"
read -ep "(default: y): ${NC}${NC}" yn
case $yn in
[Nn]* )
handle_no_cd
break
;;
* )
if [ -z "$OPENSSL_FIPS_CDROM_PATH" -a "${OPENSSL_FIPS_CDROM_PATH+x}" = "x" ]
then
get_cdrom_volume_path
fi
if [ -z "$OPENSSL_FIPS_VERSION" -a "${OPENSSL_FIPS_VERSION+x}" = "x" ]
then
get_version
fi
get_nginx_paths
get_source_code
break
;;
esac
done
else
printf "${NC}Since --no-cd${NC} option is present, skipping CD-ROM configuration.\n"
handle_no_cd
fi
else
if [ -z $OPENSSL_FIPS_CDROM_PATH ]
then
OPENSSL_FIPS_CDROM_PATH="$DEFAULT_OPENSSL_FIPS_CDROM_PATH"
printf "${NC}Using OpenSSL Software Foundation CD-ROM path: $OPENSSL_FIPS_CDROM_PATH\n"
fi
if [ -z $NGINX_CONF_PATH ]
then
NGINX_CONF_PATH="$DEFAULT_NGINX_CONF_PATH"
printf "${NC}Using Nginx conf folder path: $NGINX_CONF_PATH\n"
fi
if [ -z $CERTIFICATE_PATH ]
then
CERTIFICATE_PATH="$DEFAULT_CERTIFICATE_PATH"
printf "${NC}Using SSL certificate(s) path: $CERTIFICATE_PATH\n"
fi
if [ -z $CERTIFICATE_KEY_PATH ]
then
CERTIFICATE_KEY_PATH="$DEFAULT_CERTIFICATE_KEY_PATH"
printf "${NC}Using SSL certificate key(s) path: $CERTIFICATE_KEY_PATH\n"
fi
if [ -z $FILENAME ]
then
FILENAME="openssl-fips-$DEFAULT_OPENSSL_FIPS_VERSION.tar.gz"
printf "${NC}Using OpenSSL FIPS object module version: $DEFAULT_OPENSSL_FIPS_VERSION\n"
fi
if [ -z $IMAGE_TAG ]
then
IMAGE_TAG="$DEFAULT_IMAGE_TAG"
printf "${NC}Using Docker image tag: $IMAGE_TAG\n"
fi
if [ -d "$OPENSSL_FIPS_CDROM_PATH" ]
then
get_nginx_paths
get_source_code
else
handle_no_cd
fi
fi