Skip to content

Commit 369897f

Browse files
authored
Merge pull request #497 from Hritik14/docker_bugfix
Improve Docker configuration, add Makefile and refactor CI tests
2 parents 05fcc64 + b69dee2 commit 369897f

35 files changed

Lines changed: 237 additions & 154 deletions

.dockerignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
.git
2+
.github
3+
4+
venv

.github/workflows/main.yml

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,50 +7,28 @@ jobs:
77
runs-on: ubuntu-latest
88

99
steps:
10-
- name: Check out repository code
10+
- name: Checkout code
1111
uses: actions/checkout@v2
1212

13-
- name: Set up Python 3.8
13+
- name: Set up Python 3.8.11
1414
uses: actions/setup-python@v2
1515
with:
16-
python-version: 3.8
17-
18-
- name: Restore cache
19-
uses: actions/cache@v2
20-
with:
21-
path: .venv
22-
key: ${{ runner.os }}-venv-${{ hashFiles('**/requirements*.txt') }}
23-
restore-keys: |
24-
${{ runner.os }}-venv-
16+
python-version: 3.8.11
2517

2618
- name: Install dependencies
2719
run: |
28-
sudo apt install python3-dev postgresql libpq-dev build-essential libxml2-dev libxslt1-dev postgresql ncat
29-
python -m pip install --upgrade pip
20+
sudo apt-get install -y postgresql python3-dev libpq-dev build-essential
21+
make dev envfile
3022
31-
- uses: syphar/restore-virtualenv@v1
32-
id: cache-virtualenv
33-
with:
34-
requirement_files: requirements*.txt # this is optional
23+
- name: Validate code format
24+
run: make check
3525

36-
- uses: syphar/restore-pip-download-cache@v1
37-
if: steps.cache-virtualenv.outputs.cache-hit != 'true'
38-
39-
- run: pip install -r requirements.txt -r requirements-dev.txt
40-
if: steps.cache-virtualenv.outputs.cache-hit != 'true'
41-
4226
- name: Setup database
43-
env:
44-
PGPASSWORD: vulnerablecode
4527
run: |
4628
sudo systemctl start postgresql
47-
sudo -Eu postgres psql -c "CREATE ROLE vulnerablecode WITH PASSWORD '$PGPASSWORD' NOSUPERUSER CREATEDB NOCREATEROLE INHERIT LOGIN;"
48-
sudo systemctl status postgresql
49-
createdb --encoding=utf-8 --owner=vulnerablecode --user=vulnerablecode \
50-
--host=localhost --port=5432 vulnerablecode
29+
make postgres
5130
5231
- name: Run tests
53-
run: python -m pytest -v -m "not webtest"
32+
run: make test
5433
env:
55-
DJANGO_DEV: 1
5634
GH_TOKEN: 1

.github/workflows/upstream_test.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,4 @@ jobs:
4848
POSTGRES_HOST: localhost
4949
VC_DB_USER: postgres
5050
POSTGRES_PORT: 5432
51-
DJANGO_DEV: 1
52-
GH_TOKEN: 1
51+
GH_TOKEN: 1

.travis.yml

Lines changed: 0 additions & 30 deletions
This file was deleted.

Dockerfile

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
FROM python@sha256:e9b7e3b4e9569808066c5901b8a9ad315a9f14ae8d3949ece22ae339fff2cad0
1+
FROM python:3.8
22

3-
# PYTHONUNBUFFERED=1 ensures that the python output is set straight
4-
# to the terminal without buffering it first
3+
# Force unbuffered stdout and stderr (i.e. they are flushed to terminal immediately)
54
ENV PYTHONUNBUFFERED 1
6-
RUN mkdir /vulnerablecode
7-
WORKDIR /vulnerablecode
8-
ADD . /vulnerablecode/
9-
RUN pip install -r requirements.txt && \
10-
DJANGO_DEV=1 python manage.py collectstatic
115

12-
LABEL "base_image": "pkg:docker/python@sha256%3Ae9b7e3b4e9569808066c5901b8a9ad315a9f14ae8d3949ece22ae339fff2cad0"
13-
LABEL "dockerfile_url": "https://github.com/nexB/vulnerablecode/blob/develop/Dockerfile"
14-
LABEL "homepage_url": "https://github.com/nexB/vulnerablecode"
15-
LABEL "license": "Apache-2.0"
6+
RUN mkdir /opt/vulnerablecode && \
7+
mkdir -p /var/vulnerablecode/static/
8+
WORKDIR /opt/vulnerablecode
9+
COPY . .
10+
RUN python -m pip install --upgrade pip && \
11+
pip install -r requirements.txt

Makefile

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
#
3+
# http://nexb.com and https://github.com/nexB/scancode.io
4+
# The ScanCode.io software is licensed under the Apache License version 2.0.
5+
# Data generated with ScanCode.io is provided as-is without warranties.
6+
# ScanCode is a trademark of nexB Inc.
7+
#
8+
# You may not use this software except in compliance with the License.
9+
# You may obtain a copy of the License at: http://apache.org/licenses/LICENSE-2.0
10+
# Unless required by applicable law or agreed to in writing, software distributed
11+
# under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12+
# CONDITIONS OF ANY KIND, either express or implied. See the License for the
13+
# specific language governing permissions and limitations under the License.
14+
#
15+
# Data Generated with ScanCode.io is provided on an "AS IS" BASIS, WITHOUT WARRANTIES
16+
# OR CONDITIONS OF ANY KIND, either express or implied. No content created from
17+
# ScanCode.io should be considered or used as legal advice. Consult an Attorney
18+
# for any legal advice.
19+
#
20+
# ScanCode.io is a free software code scanning tool from nexB Inc. and others.
21+
# Visit https://github.com/nexB/scancode.io for support and download.
22+
# Modified for VulnerableCode use
23+
24+
# Python version can be specified with `$ PYTHON_EXE=python3.x make conf`
25+
PYTHON_EXE?=python3
26+
VENV=venv
27+
ACTIVATE?=. ${VENV}/bin/activate;
28+
VIRTUALENV_PYZ=etc/thirdparty/virtualenv.pyz
29+
BLACK_ARGS=-l 100 .
30+
# Do not depend on Python to generate the SECRET_KEY
31+
GET_SECRET_KEY=`base64 /dev/urandom | head -c50`
32+
# Customize with `$ make envfile ENV_FILE=/etc/vulnerablecode/.env`
33+
ENV_FILE=.env
34+
# Customize with `$ make postgres VULNERABLECODE_DB_PASSWORD=YOUR_PASSWORD`
35+
VULNERABLECODE_DB_PASSWORD=vulnerablecode
36+
37+
# Use sudo for postgres, but only on Linux
38+
UNAME := $(shell uname)
39+
ifeq ($(UNAME), Linux)
40+
SUDO_POSTGRES=sudo -u postgres
41+
else
42+
SUDO_POSTGRES=
43+
endif
44+
45+
virtualenv:
46+
@echo "-> Bootstrap the virtualenv with PYTHON_EXE=${PYTHON_EXE}"
47+
@${PYTHON_EXE} ${VIRTUALENV_PYZ} --never-download --no-periodic-update ${VENV}
48+
49+
conf: virtualenv
50+
@echo "-> Install dependencies"
51+
@${ACTIVATE} pip install -r requirements.txt
52+
53+
dev: conf
54+
@echo "-> Configure and install development dependencies"
55+
@${ACTIVATE} pip install -r requirements-dev.txt
56+
57+
envfile:
58+
@echo "-> Create the .env file and generate a secret key"
59+
@if test -f ${ENV_FILE}; then echo ".env file exists already"; exit 1; fi
60+
@mkdir -p $(shell dirname ${ENV_FILE}) && touch ${ENV_FILE}
61+
@echo SECRET_KEY=\"${GET_SECRET_KEY}\" > ${ENV_FILE}
62+
63+
check:
64+
@echo "-> Run black validation"
65+
@${ACTIVATE} black --check ${BLACK_ARGS}
66+
67+
black:
68+
@echo "-> Apply black code formatter"
69+
${VENV}/bin/black ${BLACK_ARGS}
70+
71+
valid: black
72+
73+
clean:
74+
@echo "-> Clean the Python env"
75+
rm -rm ${VENV}
76+
77+
migrate:
78+
@echo "-> Apply database migrations"
79+
${ACTIVATE} ./manage.py migrate
80+
81+
postgres:
82+
@echo "-> Configure PostgreSQL database"
83+
@echo "-> Create database user 'vulnerablecode'"
84+
${SUDO_POSTGRES} createuser --no-createrole --no-superuser --login --inherit --createdb vulnerablecode || true
85+
${SUDO_POSTGRES} psql -c "alter user vulnerablecode with encrypted password '${VULNERABLECODE_DB_PASSWORD}';" || true
86+
@echo "-> Drop 'vulnerablecode' database"
87+
${SUDO_POSTGRES} dropdb vulnerablecode || true
88+
@echo "-> Create 'vulnerablecode' database"
89+
${SUDO_POSTGRES} createdb --encoding=utf-8 --owner=vulnerablecode vulnerablecode
90+
@$(MAKE) migrate
91+
92+
sqlite:
93+
@echo "-> Configure SQLite database"
94+
@echo VULNERABLECODE_DB_ENGINE=\"django.db.backends.sqlite3\" >> ${ENV_FILE}
95+
@echo VULNERABLECODE_DB_NAME=\"sqlite3.db\" >> ${ENV_FILE}
96+
@$(MAKE) migrate
97+
98+
run:
99+
${ACTIVATE} ./manage.py runserver
100+
101+
test:
102+
@echo "-> Run the test suite"
103+
${ACTIVATE} ${PYTHON_EXE} -m pytest -v -m "not webtest"
104+
105+
package: conf
106+
@echo "-> Create a VulnerableCode package for offline installation"
107+
@echo "-> Fetch dependencies in thirdparty/ for offline installation"
108+
rm -rf thirdparty && mkdir thirdparty
109+
${VENV}/bin/pip download -r requirements.txt --no-cache-dir --dest thirdparty
110+
@echo "-> Create package in dist/ for offline installation"
111+
${VENV}/bin/python setup.py sdist
112+
113+
install: virtualenv
114+
@echo "-> Install and configure the Python env with base dependencies, offline"
115+
${VENV}/bin/pip install --upgrade --no-index --no-cache-dir --find-links=thirdparty -e .
116+
117+
.PHONY: virtualenv conf dev envfile install check valid clean migrate postgres sqlite run test package

README.rst

Lines changed: 9 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -90,29 +90,10 @@ First clone the source code::
9090
cd vulnerablecode
9191

9292

93-
94-
9593
Using Docker Compose
96-
~~~~~~~~~~~~~~~~~~~~
97-
98-
An easy way to set up VulnerableCode is with docker containers and docker
99-
compose. For this you need to have the following installed.
100-
101-
- Docker Engine. Find instructions to install it
102-
`here <https://docs.docker.com/get-docker/>`__
103-
- Docker Compose. Find instructions to install it
104-
`here <https://docs.docker.com/compose/install/#install-compose>`__
105-
106-
Use ``sudo docker-compose up`` to start VulnerableCode. Then access
107-
VulnerableCode at http://localhost:8000/ or at http://127.0.0.1:8000/
108-
109-
**Important**: Don't forget to run ``sudo docker-compose up -d --no-deps --build web`` to sync your instance after every ``git pull``.
110-
111-
112-
Use ``sudo docker-compose exec web bash`` to access the VulnerableCode
113-
container. From here you can access ``manage.py`` and run management commands
114-
to import data as specified below.
94+
---------------------
11595

96+
Please find the docker documentation in `Docker Installation <docs/docker_installation.rst>`__
11697

11798
Without Docker Compose
11899
~~~~~~~~~~~~~~~~~~~~~~
@@ -159,11 +140,13 @@ for this purpose::
159140

160141
SECRET_KEY=$(python -c "from django.core.management import utils; print(utils.get_random_secret_key())")
161142

162-
You will also need to setup the VC_ALLOWED_HOSTS environment variable to match the hostname where the app is deployed::
143+
You will also need to setup the `ALLOWED_HOSTS` array inside `vulnerablecode/settings.py` according to
144+
[django specifications](https://docs.djangoproject.com/en/3.2/ref/settings/#allowed-hosts). One example would be:
145+
.. code-block:: python
163146
164-
VC_ALLOWED_HOSTS=vulnerablecode.your.domain.example.com
147+
ALLOWED_HOSTS = ['vulnerablecode.your.domain.example.com']
165148
166-
You can specify several host by separating them with a colon `:`
149+
You can specify several hosts by separating them with a comma (`,`)
167150

168151
Using Nix
169152
~~~~~~~~~
@@ -213,6 +196,8 @@ Use these commands to run code style checks and the test suite::
213196
python -m pytest
214197

215198

199+
.. _Data import:
200+
216201
Data import
217202
-----------
218203

@@ -266,7 +251,6 @@ If you want to run the import periodically, you can use a systemd timer::
266251

267252
[Service]
268253
Type=oneshot
269-
Environment="DJANGO_DEV=1"
270254
ExecStart=/path/to/venv/bin/python /path/to/vulnerablecode/manage.py import --all
271255

272256
$ cat ~/.config/systemd/user/vulnerablecode.timer

docker-compose.yml

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,41 @@
11
version: '3'
22

33
services:
4-
web:
5-
environment:
6-
- DJANGO_DEV=1
7-
- VC_DB_HOST=db
4+
db:
5+
image: postgres
6+
env_file:
7+
- docker.env
8+
volumes:
9+
- db_data:/var/lib/postgresql/data/
10+
11+
vulnerablecode:
812
build: .
9-
command: bash -c "python manage.py migrate && python manage.py runserver 0.0.0.0:8000"
10-
container_name: "vulnerablecode"
13+
command: /bin/sh -c "
14+
./manage.py migrate &&
15+
./manage.py collectstatic --no-input --clear &&
16+
gunicorn vulnerablecode.wsgi:application -u nobody -g nogroup --bind :8000 --timeout 600 --workers 2"
17+
env_file:
18+
- docker.env
1119
volumes:
12-
- .:/vulnerablecode
13-
ports:
14-
- "8000:8000"
20+
- static:/var/vulnerablecode/static/
21+
restart: on-failure
1522
depends_on:
1623
- db
17-
db:
18-
image: postgres
19-
environment:
20-
- POSTGRES_DB=vulnerablecode
21-
- POSTGRES_USER=vulnerablecode
22-
- POSTGRES_PASSWORD=vulnerablecode
24+
25+
nginx:
26+
image: nginx
27+
env_file:
28+
- docker.env
29+
volumes:
30+
- static:/var/vulnerablecode/static/
31+
- ./etc/nginx/templates/:/etc/nginx/templates/
32+
ports:
33+
- ${NGINX_PORT:-8000}:80
34+
depends_on:
35+
- vulnerablecode
36+
37+
38+
volumes:
39+
static:
40+
db_data:
41+

docker.env

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
POSTGRES_DB=vulnerablecode
2+
POSTGRES_USER=vulnerablecode
3+
POSTGRES_PASSWORD=vulnerablecode
4+
5+
DJANGO_SETTINGS_MODULE=vulnerablecode.settings
6+
VULNERABLECODE_DB_HOST=db
7+
8+
GUNICORN_SERVER=vulnerablecode

0 commit comments

Comments
 (0)