2
2
3
3
set -o errexit # Exit the script with an error if any of the commands fail
4
4
5
- # Environment variables used as input:
6
- # CLIENT_PEM Path to mongo -orchestration's client.pem: must be set.
7
- # MONGO_X509_CLIENT_P12 Filename for client certificate in p12 format
8
- #
9
- # Environment variables produced as output:
10
- # MONGODB_X509_CLIENT_P12_PATH Absolute path to client certificate in p12 format
11
- # MONGO_X509_CLIENT_CERTIFICATE_PASSWORD Password for client certificate
12
-
13
-
14
- CLIENT_PEM=${CLIENT_PEM:- nil}
15
- MONGO_X509_CLIENT_P12=${MONGO_X509_CLIENT_P12:- client.p12}
16
- MONGO_X509_CLIENT_CERTIFICATE_PASSWORD=${MONGO_X509_CLIENT_CERTIFICATE_PASSWORD:- Picard-Alpha-Alpha-3-0-5}
17
-
18
- CLIENT_NO_USER_PEM=${CLIENT_NO_USER_PEM:- nil}
19
- MONGO_X509_CLIENT_NO_USER_P12=${MONGO_X509_CLIENT_NO_USER_P12:- client_no_user.p12}
20
- MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD=${MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD:- Picard-Alpha-Alpha-3-0-5}
21
-
22
- function create_p12 {
23
- local PEM_FILE=$1
24
- local P12_FILE=$2
25
- local PASSWORD=$3
26
-
27
- if [[ ! -f " $PEM_FILE " ]]; then
28
- echo " Warning: PEM file '$PEM_FILE ' does not exist. Skipping generation of '$P12_FILE '."
29
- return 1
30
- fi
31
-
32
- openssl pkcs12 -export -keypbe PBE-SHA1-3DES -certpbe PBE-SHA1-3DES -macalg sha1 -in " $PEM_FILE " \
33
- -out " $P12_FILE " \
34
- -name " Drivers Client Certificate" \
35
- -password " pass:${PASSWORD} "
36
- }
5
+ # Input environment variables
6
+ : " ${CLIENT_PEM_VAR_NAME:= " CLIENT_PEM" } " # Name of the input variable for the client.pem file
7
+ : " ${OUTPUT_VAR_PREFIX:= " MONGO_X509_CLIENT" } " # Prefix for output environment variables
8
+ : " ${CERTIFICATE_NAME:= " Drivers Client Certificate" } " # Name for the exported certificate
9
+
10
+ # todo, need to take the input name for client file and password, and not use the convoluted system we have
11
+ # I think I need to add those to the input environment variables and then use those in the export down here (where the default values for output variables are)
12
+
13
+ CLIENT_PEM=${! CLIENT_PEM_VAR_NAME:- nil}
14
+ OUT_CLIENT_P12_VAR=" ${OUTPUT_VAR_PREFIX} _CLIENT_P12"
15
+ OUT_CLIENT_PASSWORD_VAR=" ${OUTPUT_VAR_PREFIX} _CERTIFICATE_PASSWORD"
16
+ OUT_CLIENT_PATH_VAR=" ${OUTPUT_VAR_PREFIX} _CERTIFICATE_PATH"
17
+
18
+ # Default values for output variables (can be overridden via the environment)
19
+ export " ${OUT_CLIENT_P12_VAR} " =" ${! OUT_CLIENT_P12_VAR:- client.p12} "
20
+ export " ${OUT_CLIENT_PASSWORD_VAR} " =" ${! OUT_CLIENT_PASSWORD_VAR:- Picard-Alpha-Alpha-3-0-5} "
21
+
22
+ if [[ " $CLIENT_PEM " == " nil" ]]; then
23
+ echo " Error: ${CLIENT_PEM_VAR_NAME} must be set."
24
+ exit 1
25
+ fi
37
26
38
- function get_realpath {
39
- local FILE=$1
40
- if [[ " $OS " =~ MAC| Mac| mac ]]; then
41
- # realpath function for Mac OS
42
- function realpath() {
43
- OURPWD=$PWD
44
- cd " $( dirname " $1 " ) "
27
+ P12_FILENAME=${! OUT_CLIENT_P12_VAR}
28
+ CERT_PASSWORD=${! OUT_CLIENT_PASSWORD_VAR}
29
+
30
+ openssl pkcs12 -export -keypbe PBE-SHA1-3DES -certpbe PBE-SHA1-3DES -macalg sha1 -in " ${CLIENT_PEM} " \
31
+ -out " ${P12_FILENAME} " \
32
+ -name " ${CERTIFICATE_NAME} " \
33
+ -password " pass:${CERT_PASSWORD} "
34
+
35
+ # Determine path using realpath (compatible across macOS, Linux, and Windows)
36
+ if [[ " $OS " =~ MAC| Mac| mac ]]; then
37
+ # Functionality to mimic `realpath` on macOS
38
+ function realpath() {
39
+ OURPWD=$PWD
40
+ cd " $( dirname " $1 " ) "
41
+ LINK=$( readlink " $( basename " $1 " ) " )
42
+ while [ " $LINK " ]; do
43
+ cd " $( dirname " $LINK " ) "
45
44
LINK=$( readlink " $( basename " $1 " ) " )
46
- while [ " $LINK " ]; do
47
- cd " $( dirname " $LINK " ) "
48
- LINK=$( readlink " $( basename " $1 " ) " )
49
- done
50
- REALPATH=" $PWD /$( basename " $1 " ) "
51
- cd " $OURPWD "
52
- echo " $REALPATH "
53
- }
54
- fi
45
+ done
46
+ REALPATH=" $PWD /$( basename " $1 " ) "
47
+ cd " $OURPWD "
48
+ echo " $REALPATH "
49
+ }
50
+ fi
55
51
56
- if [[ " $OS " =~ Windows| windows ]]; then
57
- echo " $( cygpath -w " $FILE " ) "
58
- else
59
- echo " $( realpath " $FILE " ) "
60
- fi
61
- }
52
+ CERT_PATH=$( realpath " ${P12_FILENAME} " )
62
53
63
- # Create the primary client's p12 certificate if the PEM file exists
64
- if create_p12 " $CLIENT_PEM " " $MONGO_X509_CLIENT_P12 " " $MONGO_X509_CLIENT_CERTIFICATE_PASSWORD " ; then
65
- MONGO_X509_CLIENT_CERTIFICATE_PATH=$( get_realpath " $MONGO_X509_CLIENT_P12 " )
66
- export MONGO_X509_CLIENT_CERTIFICATE_PATH
67
- export MONGO_X509_CLIENT_CERTIFICATE_PASSWORD
68
- echo " Primary certificate path: $MONGO_X509_CLIENT_CERTIFICATE_PATH "
69
- echo " Primary certificate password: $MONGO_X509_CLIENT_CERTIFICATE_PASSWORD "
70
- else
71
- echo " Skipping primary certificate creation."
54
+ if [[ " $OS " =~ Windows| windows ]]; then
55
+ CERT_PATH=$( cygpath -w " ${CERT_PATH} " )
72
56
fi
73
57
74
- # Create the secondary "No User" client's p12 certificate if the PEM file exists
75
- if create_p12 " $CLIENT_NO_USER_PEM " " $MONGO_X509_CLIENT_NO_USER_P12 " " $MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD " ; then
76
- MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH=$( get_realpath " $MONGO_X509_CLIENT_NO_USER_P12 " )
77
- export MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH
78
- export MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD
79
- echo " Secondary ('No User') certificate path: $MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PATH "
80
- echo " Secondary ('No User') certificate password: $MONGO_X509_CLIENT_NO_USER_CERTIFICATE_PASSWORD "
81
- else
82
- echo " Skipping secondary ('No User') certificate creation."
83
- fi
58
+ export " ${OUT_CLIENT_PATH_VAR} " =" ${CERT_PATH} "
59
+
60
+ echo " Exported variables:"
61
+ echo " ${OUT_CLIENT_P12_VAR} =${! OUT_CLIENT_P12_VAR} "
62
+ echo " ${OUT_CLIENT_PASSWORD_VAR} =${! OUT_CLIENT_PASSWORD_VAR} "
63
+ echo " ${OUT_CLIENT_PATH_VAR} =${! OUT_CLIENT_PATH_VAR} "
0 commit comments