-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcreate_participant_manager_superadmin.sh
executable file
·94 lines (78 loc) · 3.24 KB
/
create_participant_manager_superadmin.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
#!/bin/bash
# Copyright 2020 Google LLC
#
# Use of this source code is governed by an MIT-style
# license that can be found in the LICENSE file or at
# https://opensource.org/licenses/MIT.
#
# Script to insert the first participant manager superadmin into auth server.
# Run like:
# $ ./scripts/create_participant_manager_superadmin.sh <prefix> <env> <email> <password>
if [ "$#" -ne 4 ]; then
echo 'Please provide deployment prefix and env, as well as superadmin email and password in the order of <prefix> <env> <email> <password>'
exit 1
fi
PREFIX=${1}
ENV=${2}
EMAIL="${3}"
PWD="${4}"
shift 4
set -e
DATA_PROJECT=${PREFIX}-${ENV}-data
SQL_IMPORT_BUCKET=${PREFIX}-${ENV}-mystudies-sql-import
TMPFILE=$(mktemp)
# Update first database
echo "USE \`oauth_server_hydra\`;" >> ${TMPFILE}
SALT=`printf "%s" uuidgen | iconv -t utf-8 | openssl dgst -sha512 | sed 's/^.* //'`
HASH=`printf "%s%s" $SALT $PWD | iconv -t utf-8 | openssl dgst -sha512 | sed 's/^.* //'`
if [[ "$OSTYPE" == "darwin"* ]]; then
DATE=`date -v +30d +"%F %T"`
TIMESTAMP=`date -v +30d +"%s%3N"`
else # linux
DATE=`date -d +30days +"%F %T"`
TIMESTAMP=`date -d +30days +"%s%3N"`
fi
echo "Inserting/updating superadmin user in 'oauth_server_hydra' database"
echo "REPLACE into users (id, app_id, email, status, temp_reg_id, user_id, user_info)
VALUES
('8ad16a8c74f823a10174f82c9a300001',
'PARTICIPANT MANAGER',
'${EMAIL}',
5,
'bd676334dd745c6afaa6547f9736a4c4df411a3ca2c4f514070daae31008cd9d',
'96494ebc2ae5ac344437ec19bfc0b09267a876015b277e1f6e9bfc871f578508',
'{ \"password\": { \"hash\": \"${HASH}\", \"salt\": \"${SALT}\", \"expire_timestamp\": ${TIMESTAMP},
\"password_history\": [{\"hash\": \"${HASH}\", \"salt\": \"${SALT}\", \"expire_timestamp\":${TIMESTAMP}}]}
}');
" >> ${TMPFILE}
# Upload TMPFILE to GCS.
GCS_FILE=gs://${SQL_IMPORT_BUCKET}/participant_manager_superadmin.sql
echo "Copying the sql file to ${GCS_FILE}"
gsutil mv ${TMPFILE} ${GCS_FILE}
# Import the GCS file to CloudSQL.
echo "Importing ${GCS_FILE} to CloudSQL."
gcloud sql import sql --project=${DATA_PROJECT} mystudies ${GCS_FILE}
gsutil rm ${GCS_FILE}
# Update second database
echo "USE \`mystudies_participant_datastore\`;" >> ${TMPFILE}
echo "Insert default location"
echo "REPLACE INTO locations
(id, custom_id, is_default, name, status)
VALUES
('1', 'location1', 'Y', 'Site', 1);
" >> ${TMPFILE}
SECURITY_CODE=`cat /dev/urandom | LC_ALL=C tr -dc 'a-z0-9' | fold -w 64 | head -n 1 | sed 's/^.* //'`
echo "Inserting/updating ur_admin_user record in 'mystudies_participant_datastore' database"
echo "REPLACE INTO ur_admin_user
(id, created_by, email, first_name, last_name, location_permission, security_code, security_code_expire_date, status, super_admin, ur_admin_auth_id)
VALUES
('c9d30d67-0477-4a8c-8490-0fa1e0300bd0', '1', '${EMAIL}', 'Admin', 'Admin', 1, '${SECURITY_CODE}', '${DATE}', 1, b'1', '96494ebc2ae5ac344437ec19bfc0b09267a876015b277e1f6e9bfc871f578508');
" >> ${TMPFILE}
# Upload TMPFILE to GCS.
GCS_FILE=gs://${SQL_IMPORT_BUCKET}/participant_manager_superadmin.sql
echo "Copying the sql file to ${GCS_FILE}"
gsutil mv ${TMPFILE} ${GCS_FILE}
# Import the GCS file to CloudSQL.
echo "Importing ${GCS_FILE} to CloudSQL."
gcloud sql import sql --project=${DATA_PROJECT} mystudies ${GCS_FILE}
gsutil rm ${GCS_FILE}