From 06f34f613df9066cd18404dd644a2ea8fba55c12 Mon Sep 17 00:00:00 2001 From: Arnaud de Bock Date: Tue, 4 Aug 2020 07:25:49 +0200 Subject: [PATCH] PostgreSQL support for Docker + Adding entrypoint to isolate build and run phases in Docker (#1490) * Add Postgresql support Co-authored-by: Ajin Abraham Co-authored-by: superpoussin22 --- Dockerfile | 12 ++++++++---- MobSF/settings.py | 8 ++++---- docker-compose.yml | 24 +++++++++++++++++++++--- scripts/entrypoint.sh | 8 ++++++++ scripts/postgres_support.sh | 4 +--- 5 files changed, 42 insertions(+), 14 deletions(-) mode change 100755 => 100644 MobSF/settings.py create mode 100644 scripts/entrypoint.sh diff --git a/Dockerfile b/Dockerfile index 03b35b74..29898ffa 100644 --- a/Dockerfile +++ b/Dockerfile @@ -86,6 +86,12 @@ RUN sed -i 's/USE_HOME = False/USE_HOME = True/g' MobSF/settings.py && \ # Postgres support is set to false by default ARG POSTGRES=False + +ENV POSTGRES_USER=postgres +ENV POSTGRES_PASSWORD=password +ENV POSTGRES_DB=mobsf +ENV POSTGRES_HOST=postgres + # Check if Postgres support needs to be enabled WORKDIR /root/Mobile-Security-Framework-MobSF/scripts RUN chmod +x postgres_support.sh; sync; ./postgres_support.sh $POSTGRES @@ -99,9 +105,7 @@ EXPOSE 8000 # MobSF Proxy EXPOSE 1337 -RUN python3 manage.py makemigrations && \ - python3 manage.py makemigrations StaticAnalyzer && \ - python3 manage.py migrate +RUN chmod 755 /root/Mobile-Security-Framework-MobSF/scripts/entrypoint.sh # Run MobSF -CMD ["gunicorn", "-b", "0.0.0.0:8000", "MobSF.wsgi:application", "--workers=1", "--threads=10", "--timeout=1800"] +CMD ["/root/Mobile-Security-Framework-MobSF/scripts/entrypoint.sh"] \ No newline at end of file diff --git a/MobSF/settings.py b/MobSF/settings.py old mode 100755 new mode 100644 index 2d8b4f25..db00aa57 --- a/MobSF/settings.py +++ b/MobSF/settings.py @@ -77,10 +77,10 @@ DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql_psycopg2', 'NAME': 'mobsf', - 'USER': 'postgres', - 'PASSWORD': '', - 'HOST': 'localhost', - 'PORT': '', + 'USER': os.environ['POSTGRES_USER'], + 'PASSWORD': os.environ['POSTGRES_PASSWORD'], + 'HOST': os.environ['POSTGRES_HOST'], + 'PORT': 5432, } } # End Postgres support diff --git a/docker-compose.yml b/docker-compose.yml index 4b414428..2ed3b3ee 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,21 +1,39 @@ -version: '3' +version: '3.8' services: postgres: image: "postgres:latest" restart: always volumes: - - /Postgres_data/var/lib/postgresql:/var/lib/postgresql + - $HOME/MobSF/postgresql_data:/var/lib/postgresql environment: - POSTGRES_USER=postgres - POSTGRES_PASSWORD=password - POSTGRES_DB=mobsf + ports: + - "5432:5432" + networks: + - mobsf + mobsf: + environment: + - POSTGRES_USER=postgres + - POSTGRES_PASSWORD=password + - POSTGRES_DB=mobsf + - POSTGRES_HOST=postgres build: context: . dockerfile: Dockerfile args: - POSTGRES=True volumes: - - /MobSF_data:/root/.MobSF + - $HOME/MobSF/mobsf_data:/root/.MobSF ports: - "8000:8000" + networks: + - mobsf + depends_on: + - postgres + links: + - "postgres" +networks: + mobsf: diff --git a/scripts/entrypoint.sh b/scripts/entrypoint.sh new file mode 100644 index 00000000..5752db96 --- /dev/null +++ b/scripts/entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +python3 manage.py makemigrations && \ +python3 manage.py makemigrations StaticAnalyzer && \ +python3 manage.py migrate + +gunicorn -b 0.0.0.0:8000 "MobSF.wsgi:application" --workers=1 --threads=10 --timeout=1800 diff --git a/scripts/postgres_support.sh b/scripts/postgres_support.sh index 9c920843..f46e570c 100644 --- a/scripts/postgres_support.sh +++ b/scripts/postgres_support.sh @@ -7,7 +7,5 @@ if [ "$POSTGRES" == True ]; then #Enable postgres support sed -i '/# Sqlite3 suport/,/# End Sqlite3 support/d' ../MobSF/settings.py && \ sed -i '/# Postgres DB - Install psycopg2/,/"""/d' ../MobSF/settings.py && \ - sed -i '/# End Postgres support/,/"""/d' ../MobSF/settings.py && \ - sed -i "s/'PASSWORD': '',/'PASSWORD': 'password',/" ../MobSF/settings.py && \ - sed -i "s/'HOST': 'localhost',/'HOST': 'postgres',/" ../MobSF/settings.py + sed -i '/# End Postgres support/,/"""/d' ../MobSF/settings.py fi