33# PID file to use for an initialization immudb instance.
44INIT_PID_FILE=/var/run/immudb/init.pid
55
6+ # Directory in which temporary files may be created.
7+ TEMP_DIR=" /tmp"
8+ TEMP_USER_PASSWORD_FILE=${TEMP_DIR} /user-pass
9+ IMMUDB_ADMIN_PASSWORD_FILE=${TEMP_DIR} /admin-pass
10+
11+ # Cleanup temporary password files.
12+ CLEANUP () {
13+ if [ -f " $TEMP_USER_PASSWORD_FILE " ]
14+ then
15+ rm $TEMP_USER_PASSWORD_FILE
16+ fi
17+ if [ -f " $IMMUDB_ADMIN_PASSWORD_FILE " ]
18+ then
19+ rm $IMMUDB_ADMIN_PASSWORD_FILE
20+ fi
21+ }
22+
623# Trap interrupts.
724# This is required to exit the waiting loops in the init function.
8- trap " echo aborted; exit;" INT TERM
25+ trap " echo aborted; CLEANUP; exit;" INT TERM
926
1027INIT () {
1128 # If an initial user or database to be created is configured,
@@ -20,29 +37,49 @@ INIT() {
2037 then
2138 CREATE_DATABASE=1
2239 fi
23- CREATE_USER=0
24- if [ -n " $IMMUDB_USER " ] && [ -n " $IMMUDB_PASSWORD " ]
25- then
26- CREATE_USER=1
27- fi
2840
2941 # Check if a secret file for the user password was specified.
3042 if [ -n " $IMMUDB_PASSWORD_FILE " ]
3143 then
32- if [ -f " $IMMUDB_PASSWORD_FILE " ]
44+ if [ ! -f " $IMMUDB_PASSWORD_FILE " ]
3345 then
34- IMMUDB_PASSWORD=$( cat " $IMMUDB_PASSWORD_FILE " | tr -d \d\r )
35- else
3646 echo " file ${IMMUDB_PASSWORD_FILE} specified for IMMUDB_PASSWORD_FILE does not exist"
3747 return 1
3848 fi
49+ else
50+ # If no password file was specified, create a temporary file with
51+ # the content of the IMMUDB_PASSWORD variable.
52+ if [ -n " $IMMUDB_PASSWORD " ]
53+ then
54+ if ! echo " $IMMUDB_PASSWORD " > $TEMP_USER_PASSWORD_FILE
55+ then
56+ echo " creating temporary password file to create IMMUDB_USER failed"
57+ return 1
58+ fi
59+ IMMUDB_PASSWORD_FILE=$TEMP_USER_PASSWORD_FILE
60+ fi
61+ fi
62+
63+ CREATE_USER=0
64+ if [ -n " $IMMUDB_USER " ] && [ -n " $IMMUDB_PASSWORD_FILE " ]
65+ then
66+ CREATE_USER=1
3967 fi
4068
41- # Run immudb on localhost .
69+ # Check if a user and or a database should be initialized .
4270 if [ $CREATE_USER -eq 0 ] && [ $CREATE_DATABASE -eq 0 ]
4371 then
4472 return
4573 fi
74+
75+ # Create temporary file with admin password to issue immuadmin commands.
76+ if ! echo " $IMMUDB_ADMIN_PASSWORD " > $IMMUDB_ADMIN_PASSWORD_FILE
77+ then
78+ echo " creating temporary password file with immudb admin passsword failed"
79+ return 1
80+ fi
81+
82+ # Start immudb on localhost to setup the database and user.
4683 echo " starting init immudb instance on localhost."
4784 if ! $@ --address 127.0.0.1 -d --pidfile $INIT_PID_FILE
4885 then
@@ -51,20 +88,19 @@ INIT() {
5188 fi
5289
5390 # Wait until the server is running.
54- until immuadmin status
91+ until immuadmin status --password-file $IMMUDB_ADMIN_PASSWORD_FILE --non-interactive
5592 do
5693 echo " waiting for immudb instance on localhost to be ready"
5794 sleep 1
5895 done
5996
60- # Log into the immudb instance.
61- echo -n " ${IMMUDB_ADMIN_PASSWORD} " | immuadmin login immudb
62-
6397 # Create it, if it is not the database created by default.
6498 if [ $CREATE_DATABASE -eq 1 ]
6599 then
66100 echo " creating user database ${IMMUDB_DATABASE@ Q} "
67- if ! immuadmin database create " ${IMMUDB_DATABASE} "
101+ if ! immuadmin database create " ${IMMUDB_DATABASE} " \
102+ --password-file $IMMUDB_ADMIN_PASSWORD_FILE \
103+ --non-interactive
68104 then
69105 echo " creating database ${IMMUDB_DATABASE@ Q} failed"
70106 return 1
@@ -73,41 +109,20 @@ INIT() {
73109 # Create a user if configured and grant it access to the user database.
74110 if [ $CREATE_USER -eq 1 ]
75111 then
76- echo " creating user ${IMMUDB_USER@ Q} "
77- # Assemble expect script to create the user.
78- CREATE_USER_SCRIPT_FORMAT='
79- spawn immuadmin user create "%q" readwrite "%q"
80- expect "Choose a password for %q:"
81- send "%q\r"
82- expect {
83- timeout { exit 1 }
84- eof { exit 1 }
85- "Password does not meet the requirements." { exit 1 }
86- "Confirm password:"
87- }
88- send "%q\r"
89- catch wait result
90- exit [lindex $result 3]
91- '
92- printf -v CREATE_USER_SCRIPT " ${CREATE_USER_SCRIPT_FORMAT} " \
93- " ${IMMUDB_USER} " " ${USER_DATABASE} " \
94- " ${IMMUDB_USER} " \
95- " ${IMMUDB_PASSWORD} " \
96- " ${IMMUDB_PASSWORD} "
97- # Execute the expect script.
98- if ! expect -c " $CREATE_USER_SCRIPT "
112+ if ! immuadmin user create " ${IMMUDB_USER} " readwrite " ${IMMUDB_DATABASE} " \
113+ --new-password-file $IMMUDB_PASSWORD_FILE \
114+ --password-file $IMMUDB_ADMIN_PASSWORD_FILE \
115+ --non-interactive
99116 then
100117 echo " creating user ${IMMUDB_USER@ Q} failed"
101118 return 1
102119 fi
103120 fi
104-
121+
105122 # Stop the init instance.
106123 if [ -f $INIT_PID_FILE ]
107124 then
108125 echo " stopping init immudb instance on localhost."
109- # Log out of the instance.
110- immuadmin logout
111126 # Kill the immudb instance.
112127 INIT_PID=$( cat $INIT_PID_FILE )
113128 kill $INIT_PID
@@ -127,9 +142,13 @@ then
127142 echo " initialilzing database"
128143 if ! INIT $@
129144 then
145+ # Cleanup temporary files.
146+ CLEANUP
130147 echo " initializing database failed"
131148 exit 1
132149 fi
150+ # Cleanup temporary files.
151+ CLEANUP
133152fi
134153
135154exec $@
0 commit comments