forked from DefectDojo/django-DefectDojo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsetup.bash
executable file
·317 lines (289 loc) · 11 KB
/
setup.bash
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
#!/bin/bash
NONE='\033[00m'
RED='\033[01;31m'
GREEN='\033[01;32m'
YELLOW='\033[01;33m'
PURPLE='\033[01;35m'
CYAN='\033[01;36m'
WHITE='\033[01;37m'
BOLD='\033[1m'
UNDERLINE='\033[4m'
#Supported databases
MYSQL=1
POSTGRES=2
function prompt_db_type() {
read -p "Select database type: 1.) MySQL or 2.) Postgres: " DBTYPE
if [ "$DBTYPE" == '1' ] || [ "$DBTYPE" == '2' ] ; then
echo "Setting up database"
else
echo "Please enter 1 or 2"
prompt_db_type
fi
}
# Get MySQL details
function get_db_details() {
# Allow script to be called non-interactively using:
# export AUTO_DOCKER=yes && /opt/django-DefectDojo/setup.bash
if [ "$AUTO_DOCKER" != "yes" ]; then
# Run interactively
read -p "MySQL host: " SQLHOST
read -p "MySQL port: " SQLPORT
read -p "MySQL user (should already exist): " SQLUSER
stty -echo
read -p "Password for user: " SQLPWD; echo
stty echo
read -p "Database name (should NOT exist): " DBNAME
else
# Set the root password for mysql - install has it blank
mysql -uroot -e "SET PASSWORD = PASSWORD('Cu3zehoh7eegoogohdoh1the');"
# Default values for a automated Docker install
echo "Setting default values for MySQL install"
SQLHOST="localhost"
SQLPORT="3306"
SQLUSER="root"
SQLPWD="Cu3zehoh7eegoogohdoh1the"
DBNAME="dojodb"
fi
if mysql -fs -h "$SQLHOST" -P "$SQLPORT" -u"$SQLUSER" -p"$SQLPWD" "$DBNAME" >/dev/null 2>&1 </dev/null; then
echo "Database $DBNAME already exists!"
echo
read -p "Drop database $DBNAME? [Y/n] " DELETE
if [[ ! $DELETE =~ ^[nN]$ ]]; then
mysqladmin -f --host="$SQLHOST" --port="$SQLPORT" --user="$SQLUSER" --password="$SQLPWD" drop "$DBNAME"
mysqladmin --host="$SQLHOST" --port="$SQLPORT" --user="$SQLUSER" --password="$SQLPWD" create "$DBNAME"
else
echo "Error! Must supply an empty database to proceed."
echo
get_db_details
fi
else
if mysqladmin --host="$SQLHOST" --port="$SQLPORT" --user="$SQLUSER" --password="$SQLPWD" create $DBNAME; then
echo "Created database $DBNAME."
else
echo "Error! Failed to create database $DBNAME. Check your credentials."
echo
get_db_details
fi
fi
}
function get_postgres_db_details() {
read -p "Postgres host: " SQLHOST
read -p "Postgres port: " SQLPORT
read -p "Postgres user (should already exist): " SQLUSER
stty -echo
read -p "Password for user: " SQLPWD; echo
stty echo
read -p "Database name (should NOT exist): " DBNAME
if [ "$( PGPASSWORD=$SQLPWD psql -h $SQLHOST -p $SQLPORT -U $SQLUSER -tAc "SELECT 1 FROM pg_database WHERE datname='$DBNAME'" )" = '1' ]
then
echo "Database $DBNAME already exists!"
echo
read -p "Drop database $DBNAME? [Y/n] " DELETE
if [[ ! $DELETE =~ ^[nN]$ ]]; then
PGPASSWORD=$SQLPWD dropdb $DBNAME -h $SQLHOST -p $SQLPORT -U $SQLUSER
PGPASSWORD=$SQLPWD createdb $DBNAME -h $SQLHOST -p $SQLPORT -U $SQLUSER
else
read -p "Try and install anyway? [Y/n] " INSTALL
if [[ $INSTALL =~ ^[nN]$ ]]; then
echo
get_postgres_db_details
fi
fi
else
PGPASSWORD=$SQLPWD createdb $DBNAME -h $SQLHOST -p $SQLPORT -U $SQLUSER
if [ $? = 0 ]
then
echo "Created database $DBNAME."
else
echo "Error! Failed to create database $DBNAME. Check your credentials."
echo
get_postgres_db_details
fi
fi
}
echo "Welcome to DefectDojo! This is a quick script to get you up and running."
echo
# Allow script to be called non-interactively using:
# export AUTO_DOCKER=yes && /opt/django-DefectDojo/setup.bash
if [ "$AUTO_DOCKER" != "yes" ]; then
prompt_db_type
else
# Default to MySQL install
DBTYPE=$MYSQL
fi
echo
echo "NEED SUDO PRIVILEGES FOR NEXT STEPS!"
echo
echo "Attempting to install required packages..."
echo
# Set up packages via Yum / APT
YUM_CMD=$(which yum)
APT_GET_CMD=$(which apt-get)
BREW_CMD=$(which brew)
if [[ ! -z "$YUM_CMD" ]]; then
curl -sL https://rpm.nodesource.com/setup | sudo bash -
wget https://dl.yarnpkg.com/rpm/yarn.repo -O /etc/yum.repos.d/yarn.repo
sudo yum install gcc python-devel python-setuptools python-pip nodejs yarn wkhtmltopdf npm
if [ "$DBTYPE" == $MYSQL ]; then
echo "Installing MySQL client"
sudo yum install libmysqlclient-dev mysql-server mysql-devel MySQL-python
elif [ "$DBTYPE" == $POSTGRES ]; then
echo "Installing Postgres client"
sudo yum install libpq-dev postgresql postgresql-contrib libmysqlclient-dev
fi
sudo yum groupinstall 'Development Tools'
elif [[ ! -z "$APT_GET_CMD" ]]; then
if [ "$DBTYPE" == $MYSQL ]; then
echo "Installing MySQL client"
sudo apt-get -y install libmysqlclient-dev mysql-server
elif [ "$DBTYPE" == $POSTGRES ]; then
echo "Installing Postgres client"
sudo apt-get -y install libpq-dev postgresql postgresql-contrib libmysqlclient-dev
fi
sudo apt-get install -y curl apt-transport-https
#Yarn
curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | sudo apt-key add -
echo "deb https://dl.yarnpkg.com/debian/ stable main" | sudo tee /etc/apt/sources.list.d/yarn.list
#Node
curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash
sudo apt-get update && sudo apt-get install -y apt-transport-https libjpeg-dev gcc libssl-dev python-dev python-pip nodejs yarn wkhtmltopdf build-essential
elif [[ ! -z "$BREW_CMD" ]]; then
brew install gcc openssl python node npm yarn Caskroom/cask/wkhtmltopdf
if [ "$DBTYPE" == $MYSQL ]; then
echo "Installing MySQL client"
brew install mysql
elif [ "$DBTYPE" == $POSTGRES ]; then
echo "Installing Postgres client"
brew install postgresql
fi
else
echo "ERROR! OS not supported. Try the Vagrant option."
exit 1;
fi
echo
if [ "$DBTYPE" == $MYSQL ]; then
echo "Installing MySQL client"
get_db_details
elif [ "$DBTYPE" == $POSTGRES ]; then
get_postgres_db_details
fi
unset HISTFILE
if [[ ! -z "$BREW_CMD" ]]; then
LC_CTYPE=C
fi
SECRET=`cat /dev/urandom | LC_CTYPE=C tr -dc "a-zA-Z0-9" | head -c 128`
# Allow script to be called non-interactively using:
# export AUTO_DOCKER=yes && /opt/django-DefectDojo/setup.bash
if [ "$AUTO_DOCKER" != "yes" ]; then
cp dojo/settings.dist.py dojo/settings.py
else
# locate to the install directory first
cd /opt/django-DefectDojo/
cp dojo/settings.dist.py dojo/settings.py
fi
# Save MySQL details in settings file
if [[ ! -z $BREW_CMD ]]; then
sed -i '' "s/MYSQLHOST/$SQLHOST/g" dojo/settings.py
sed -i '' "s/MYSQLPORT/$SQLPORT/g" dojo/settings.py
sed -i '' "s/MYSQLUSER/$SQLUSER/g" dojo/settings.py
sed -i '' "s/MYSQLPWD/$SQLPWD/g" dojo/settings.py
sed -i '' "s/MYSQLDB/$DBNAME/g" dojo/settings.py
sed -i '' "s#DOJODIR#$PWD/dojo#g" dojo/settings.py
sed -i '' "s/DOJOSECRET/$SECRET/g" dojo/settings.py
sed -i '' "s#DOJO_MEDIA_ROOT#$PWD/media/#g" dojo/settings.py
sed -i '' "s#DOJO_STATIC_ROOT#$PWD/static/#g" dojo/settings.py
if [ "$DBTYPE" == '1' ]; then
sed -i '' "s/BACKENDDB/django.db.backends.mysql/g" dojo/settings.py
elif [ "$DBTYPE" == '2' ]; then
sed -i '' "s/BACKENDDB/django.db.backends.postgresql_psycopg2/g" dojo/settings.py
fi
else
sed -i "s/MYSQLHOST/$SQLHOST/g" dojo/settings.py
sed -i "s/MYSQLPORT/$SQLPORT/g" dojo/settings.py
sed -i "s/MYSQLUSER/$SQLUSER/g" dojo/settings.py
sed -i "s/MYSQLPWD/$SQLPWD/g" dojo/settings.py
sed -i "s/MYSQLDB/$DBNAME/g" dojo/settings.py
sed -i "s#DOJODIR#$PWD/dojo#g" dojo/settings.py
sed -i "s/DOJOSECRET/$SECRET/g" dojo/settings.py
sed -i "s#DOJO_MEDIA_ROOT#$PWD/media/#g" dojo/settings.py
sed -i "s#DOJO_STATIC_ROOT#$PWD/static/#g" dojo/settings.py
if [ "$DBTYPE" == '1' ]; then
sed -i "s/BACKENDDB/django.db.backends.mysql/g" dojo/settings.py
elif [ "$DBTYPE" == '2' ]; then
sed -i "s/BACKENDDB/django.db.backends.postgresql_psycopg2/g" dojo/settings.py
fi
fi
# Detect Python version
PYV=`python -c "import sys;t='{v[0]}.{v[1]}'.format(v=list(sys.version_info[:2]));sys.stdout.write(t)";`
if [[ "$PYV"<"2.7" ]]; then
echo "ERROR: DefectDojo requires Python 2.7+"
exit 1;
else
echo "Leaving Django 1.8.10 requirement"
fi
# Detect if we're in a a virtualenv
if python -c 'import sys; print sys.real_prefix' 2>/dev/null; then
pip install .
python manage.py makemigrations dojo
python manage.py makemigrations --merge --noinput
python manage.py migrate
echo -e "${GREEN}${BOLD}Create Dojo superuser:"
tput sgr0
python manage.py createsuperuser
python manage.py loaddata product_type
python manage.py loaddata test_type
python manage.py loaddata development_environment
python manage.py loaddata system_settings
python manage.py installwatson
python manage.py buildwatson
else
pip install .
python manage.py makemigrations dojo
python manage.py makemigrations --merge --noinput
python manage.py migrate
# Allow script to be called non-interactively using:
# export AUTO_DOCKER=yes && /opt/django-DefectDojo/setup.bash
if [ "$AUTO_DOCKER" != "yes" ]; then
echo -e "${GREEN}${BOLD}Create Dojo superuser:"
tput sgr0
python manage.py createsuperuser
else
# non-interactively setup the superuser
python manage.py createsuperuser --noinput --username=admin --email='ed@example.com'
/opt/django-DefectDojo/docker/setup-superuser.expect
fi
python manage.py loaddata product_type
python manage.py loaddata test_type
python manage.py loaddata development_environment
python manage.py loaddata system_settings
python manage.py installwatson
python manage.py buildwatson
fi
if [ "$AUTO_DOCKER" == "yes" ]; then
echo "Creating dojo user"
adduser --disabled-password --gecos "DefectDojo" dojo
chown -R dojo:dojo /opt/django-DefectDojo
su - dojo -c 'cd /opt/django-DefectDojo/components && yarn && cd ..'
else
cd components && yarn && cd ..
fi
python manage.py collectstatic --noinput
echo "=============================================================================="
echo
echo "SUCCESS! Now edit your settings.py file in the 'dojo' directory to complete the installation."
echo
echo "We suggest you consider changing the following defaults:"
echo
echo " DEBUG = True # you should set this to False when you are ready for production."
echo " Uncomment the following lines if you enabled SSL/TLS on your server:"
echo " SESSION_COOKIE_SECURE = True"
echo " CSRF_COOKIE_SECURE = True"
echo " SECURE_PROXY_SSL_HEADER = ('HTTP_X_FORWARDED_PROTO', 'https')"
echo " SECURE_SSL_REDIRECT = True"
echo " SECURE_BROWSER_XSS_FILTER = True"
echo " django.middleware.security.SecurityMiddleware"
echo
echo "When you're ready to start the DefectDojo server, type in this directory:"
echo
echo " python manage.py runserver"
echo