-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathcreate_study_builder_superadmin.sh
executable file
·81 lines (68 loc) · 2.4 KB
/
create_study_builder_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
#!/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 study builder superadmin.
# Run like:
# $ ./scripts/create_study_builder_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)
echo "Inserting/updating superadmin user for 'study_builder'"
echo "USE \`fda_hphc\`;" >> ${TMPFILE}
# hash password with BCrypt, default strength of 10.
# Replace 2y with 2a to make the algorithm consistent with Spring Security.
HASH=`htpasswd -bnBC 10 "" "${PWD}" | tr -d ':\n' | sed 's/$2y/$2a/'`
# e.g. N8K7zYrc0F
TOKEN=`cat /dev/urandom | LC_ALL=C tr -dc 'a-zA-Z0-9' | fold -w 10 | head -n 1`
# e.g ja67Ll
ACCESS_CODE=`cat /dev/urandom | LC_ALL=C tr -dc 'a-z0-9' | fold -w 6 | head -n 1`
# e.g. 2018-01-18 14:36:41
DATETIME=`date +"%F %T"`
if [[ "$OSTYPE" == "darwin"* ]]; then
EXPIRY_DATETIME=`date -v -91d +"%F %T"`
else # linux
EXPIRY_DATETIME=`date -d -91days +"%F %T"`
fi
echo "DELETE FROM user_permission_mapping WHERE user_id=1;" >> ${TMPFILE}
echo "REPLACE into users
(
user_id, first_name, last_name, email, password, role_id, created_by,
created_date, modified_by, modified_date, status, accountNonExpired, accountNonLocked, credentialsNonExpired, password_expiry_datetime,
security_token, token_expiry_date, token_used, force_logout, access_level)
VALUES
(
1, 'Account', 'Manager', '${EMAIL}', '${HASH}', 1, 1,
'${DATETIME}', 1, '${DATETIME}', 1, 1, 1, 1, '${EXPIRY_DATETIME}',
'${TOKEN}', '${EXPIRY_DATETIME}', 0, 'N', 'SUPERADMIN');
" >> ${TMPFILE}
echo "INSERT INTO user_permission_mapping (user_id, permission_id) VALUES
(1, 1),
(1, 2),
(1, 4),
(1, 5),
(1, 6),
(1, 7),
(1, 8);
" >> ${TMPFILE}
# Upload TMPFILE to GCS.
GCS_FILE=gs://${SQL_IMPORT_BUCKET}/study_builder_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}