Skip to content

Commit 70e9041

Browse files
author
Tyler Gonsalves
committed
Merge of main and partial implementation of REST API for MFA
2 parents 5ca7823 + a5efae1 commit 70e9041

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+2235
-569
lines changed

.github/workflows/codeql.yml

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
# For most projects, this workflow file will not need changing; you simply need
2+
# to commit it to your repository.
3+
#
4+
# You may wish to alter this file to override the set of languages analyzed,
5+
# or to provide custom queries or build logic.
6+
#
7+
# ******** NOTE ********
8+
# We have attempted to detect the languages in your repository. Please check
9+
# the `language` matrix defined below to confirm you have the correct set of
10+
# supported CodeQL languages.
11+
#
12+
name: "CodeQL Advanced"
13+
14+
on:
15+
push:
16+
branches: [ "main" ]
17+
pull_request:
18+
branches: [ "main" ]
19+
schedule:
20+
- cron: '24 21 * * 1'
21+
22+
jobs:
23+
analyze:
24+
name: Analyze (${{ matrix.language }})
25+
# Runner size impacts CodeQL analysis time. To learn more, please see:
26+
# - https://gh.io/recommended-hardware-resources-for-running-codeql
27+
# - https://gh.io/supported-runners-and-hardware-resources
28+
# - https://gh.io/using-larger-runners (GitHub.com only)
29+
# Consider using larger runners or machines with greater resources for possible analysis time improvements.
30+
runs-on: ${{ (matrix.language == 'swift' && 'macos-latest') || 'ubuntu-latest' }}
31+
permissions:
32+
# required for all workflows
33+
security-events: write
34+
35+
# required to fetch internal or private CodeQL packs
36+
packages: read
37+
38+
# only required for workflows in private repositories
39+
actions: read
40+
contents: read
41+
42+
strategy:
43+
fail-fast: false
44+
matrix:
45+
include:
46+
- language: python
47+
build-mode: none
48+
# CodeQL supports the following values keywords for 'language': 'actions', 'c-cpp', 'csharp', 'go', 'java-kotlin', 'javascript-typescript', 'python', 'ruby', 'swift'
49+
# Use `c-cpp` to analyze code written in C, C++ or both
50+
# Use 'java-kotlin' to analyze code written in Java, Kotlin or both
51+
# Use 'javascript-typescript' to analyze code written in JavaScript, TypeScript or both
52+
# To learn more about changing the languages that are analyzed or customizing the build mode for your analysis,
53+
# see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/customizing-your-advanced-setup-for-code-scanning.
54+
# If you are analyzing a compiled language, you can modify the 'build-mode' for that language to customize how
55+
# your codebase is analyzed, see https://docs.github.com/en/code-security/code-scanning/creating-an-advanced-setup-for-code-scanning/codeql-code-scanning-for-compiled-languages
56+
steps:
57+
- name: Checkout repository
58+
uses: actions/checkout@v4
59+
60+
# Add any setup steps before running the `github/codeql-action/init` action.
61+
# This includes steps like installing compilers or runtimes (`actions/setup-node`
62+
# or others). This is typically only required for manual builds.
63+
# - name: Setup runtime (example)
64+
# uses: actions/setup-example@v1
65+
66+
# Initializes the CodeQL tools for scanning.
67+
- name: Initialize CodeQL
68+
uses: github/codeql-action/init@v3
69+
with:
70+
languages: ${{ matrix.language }}
71+
build-mode: ${{ matrix.build-mode }}
72+
# If you wish to specify custom queries, you can do so here or in a config file.
73+
# By default, queries listed here will override any specified in a config file.
74+
# Prefix the list here with "+" to use these queries and those in the config file.
75+
76+
# For more details on CodeQL's query packs, refer to: https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
77+
# queries: security-extended,security-and-quality
78+
79+
# If the analyze step fails for one of the languages you are analyzing with
80+
# "We were unable to automatically build your code", modify the matrix above
81+
# to set the build mode to "manual" for that language. Then modify this step
82+
# to build your code.
83+
# ℹ️ Command-line programs to run using the OS shell.
84+
# 📚 See https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsrun
85+
- if: matrix.build-mode == 'manual'
86+
shell: bash
87+
run: |
88+
echo 'If you are using a "manual" build mode for one or more of the' \
89+
'languages you are analyzing, replace this with the commands to build' \
90+
'your code, for example:'
91+
echo ' make bootstrap'
92+
echo ' make release'
93+
exit 1
94+
95+
- name: Perform CodeQL Analysis
96+
uses: github/codeql-action/analyze@v3
97+
with:
98+
category: "/language:${{matrix.language}}"

code/.env.example

Lines changed: 46 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,46 @@
1-
DJANGO_SECRET_KEY=
2-
DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1]
3-
4-
EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
5-
EMAIL_HOST=smtp.gmail.com
6-
EMAIL_PORT=587
7-
EMAIL_USE_TLS=True
8-
EMAIL_HOST_USER=email@gmail.com
9-
EMAIL_HOST_PASSWORD=app_password
1+
# Gunicorn Configuration
2+
GUNICORN_PORT=8000
3+
GUNICORN_WORKERS=2
4+
GUNICORN_TIMEOUT=60
5+
GUNICORN_LOG_LEVEL=info
6+
7+
# Django Security
8+
DJANGO_SECRET_KEY=''
9+
DJANGO_DEBUG=false
10+
DJANGO_ALLOWED_HOSTS=127.0.0.1,localhost,localhost:8080
11+
DJANGO_CSRF_TRUSTED_ORIGINS=http://127.0.0.1,http://localhost,http://localhost:8080
12+
13+
# Database
14+
SQL_ENGINE=django.db.backends.sqlite3
15+
SQL_DATABASE=db.sqlite3
16+
DJANGO_SQLITE_DIR=/sqlite
17+
18+
# API Configuration
19+
REACT_APP_API_URL=http://127.0.0.1:8080/api
20+
21+
# Localization
22+
DJANGO_LANGUAGE_CODE=en-us
23+
DJANGO_TIME_ZONE=UTC
24+
25+
# Email Configuration - Gmail
26+
DJANGO_EMAIL_BACKEND=django.core.mail.backends.smtp.EmailBackend
27+
DJANGO_EMAIL_HOST=smtp.gmail.com
28+
DJANGO_EMAIL_PORT=587
29+
DJANGO_EMAIL_USE_TLS=true
30+
DJANGO_EMAIL_USE_SSL=false
31+
DJANGO_EMAIL_HOST_USER=email@gmail.com
32+
DJANGO_EMAIL_HOST_PASSWORD=
33+
DJANGO_DEFAULT_FROM_EMAIL=MyMedic <email@gmail.com>
34+
35+
# Default Email Settings
36+
DJANGO_SERVER_EMAIL=email@gmail.com
37+
38+
# Project Configuration
39+
PROJECT_NAME=mymedic
40+
41+
# Admin Configuration
42+
DJANGO_ADMIN_NAME=Admin
43+
DJANGO_ADMIN_EMAIL=email@gmail.com
44+
DJANGO_SUPERUSER_USERNAME=admin
45+
DJANGO_SUPERUSER_EMAIL=admin@admin.com
46+
DJANGO_SUPERUSER_PASSWORD=admin

code/Dockerfile

Lines changed: 18 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,26 @@
1-
FROM python:3.13-alpine AS base
1+
FROM python:3.13-alpine
22

3-
FROM base AS builder
3+
RUN apk update \
4+
&& apk add --no-cache \
5+
gcc musl-dev python3-dev libffi-dev openssl-dev \
6+
nginx su-exec
47

5-
RUN apk update && apk --no-cache add python3-dev libpq-dev && mkdir /install
6-
WORKDIR /install
7-
COPY requirements.txt ./
8-
RUN pip install --no-cache-dir --prefix=/install -r ./requirements.txt
8+
RUN mkdir -p /usr/src/mymedic /sqlite /var/run/nginx
9+
WORKDIR /usr/src/mymedic
910

10-
FROM base
11+
COPY requirements.txt .
12+
RUN pip install --no-cache-dir -r requirements.txt
1113

12-
ARG USER=user
13-
ARG USER_UID=1001
14-
ARG PROJECT_NAME=mymedic
15-
ARG GUNICORN_PORT=8000
16-
ARG GUNICORN_WORKERS=2
17-
# the value is in seconds
18-
ARG GUNICORN_TIMEOUT=60
19-
ARG GUNICORN_LOG_LEVEL=info
20-
ARG DJANGO_BASE_DIR=/usr/src/$PROJECT_NAME
21-
ARG DJANGO_STATIC_ROOT=/var/www/static
22-
ARG DJANGO_MEDIA_ROOT=/var/www/media
23-
ARG DJANGO_SQLITE_DIR=/sqlite
14+
COPY . .
2415

25-
# if no superuser (currently: Admin, Admin, indhhra@bu.edu)
26-
ARG DJANGO_SUPERUSER_USERNAME=admin
27-
ARG DJANGO_SUPERUSER_PASSWORD=admin
28-
ARG DJANGO_SUPERUSER_EMAIL=admin@example.com
29-
ARG DJANGO_DEV_SERVER_PORT=8000
16+
COPY nginx.conf /etc/nginx/nginx.conf
3017

18+
RUN adduser -D -u 1001 user \
19+
&& chown -R user:user /usr/src/mymedic /sqlite \
20+
&& chmod +x /usr/src/mymedic/docker-entrypoint.sh \
21+
/usr/src/mymedic/docker-cmd.sh
3122

32-
ENV \
33-
USER=$USER \
34-
USER_UID=$USER_UID \
35-
PROJECT_NAME=$PROJECT_NAME \
36-
GUNICORN_PORT=$GUNICORN_PORT \
37-
GUNICORN_WORKERS=$GUNICORN_WORKERS \
38-
GUNICORN_TIMEOUT=$GUNICORN_TIMEOUT \
39-
GUNICORN_LOG_LEVEL=$GUNICORN_LOG_LEVEL \
40-
DJANGO_BASE_DIR=$DJANGO_BASE_DIR \
41-
DJANGO_STATIC_ROOT=$DJANGO_STATIC_ROOT \
42-
DJANGO_MEDIA_ROOT=$DJANGO_MEDIA_ROOT \
43-
DJANGO_SQLITE_DIR=$DJANGO_SQLITE_DIR \
44-
DJANGO_SUPERUSER_USERNAME=$DJANGO_SUPERUSER_USERNAME \
45-
DJANGO_SUPERUSER_PASSWORD=$DJANGO_SUPERUSER_PASSWORD \
46-
DJANGO_SUPERUSER_EMAIL=$DJANGO_SUPERUSER_EMAIL \
47-
DJANGO_DEV_SERVER_PORT=$DJANGO_DEV_SERVER_PORT
23+
EXPOSE 80 8000
4824

49-
COPY --from=builder /install /usr/local
50-
COPY docker-entrypoint.sh /
51-
COPY docker-cmd.sh /
52-
COPY $PROJECT_NAME $DJANGO_BASE_DIR
53-
54-
# User
55-
RUN chmod +x /docker-entrypoint.sh /docker-cmd.sh && \
56-
apk --no-cache add su-exec libpq-dev && \
57-
mkdir -p $DJANGO_STATIC_ROOT $DJANGO_MEDIA_ROOT $DJANGO_SQLITE_DIR && \
58-
adduser -s /bin/sh -D -u $USER_UID $USER && \
59-
chown -R $USER:$USER $DJANGO_BASE_DIR $DJANGO_STATIC_ROOT $DJANGO_MEDIA_ROOT $DJANGO_SQLITE_DIR
60-
61-
WORKDIR $DJANGO_BASE_DIR
62-
63-
RUN python manage.py collectstatic --noinput && \
64-
python manage.py makemigrations && \
65-
python manage.py migrate && \
66-
python manage.py createsuperuser --noinput
25+
ENTRYPOINT ["/usr/src/mymedic/docker-entrypoint.sh"]
26+
CMD ["/usr/src/mymedic/docker-cmd.sh"]

code/Readme.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,33 +17,31 @@ cp .env.example .env
1717
```
1818

1919
Edit `.env` and add a DJANGO_SECRET_KEY generated from [Django Secret Key Generator](https://djecrety.ir/).
20-
---
21-
22-
### 3. Build the Docker Image
23-
24-
```bash
25-
docker build -t mymedic .
26-
```
20+
Add the email and app passwords for the Gmail account.
2721

2822
---
2923

30-
### 4. Run the Server
24+
### 3. Run the Server
25+
26+
If Docker gives an error about permissions for executing docker-cmd or docker-entrypoint, run the following command to fix it:
3127

32-
#### Running Tests
3328
```bash
34-
docker run --rm mymedic pytest -v tests/
29+
chmod +x docker-cmd.sh
30+
chmod +x docker-entrypoint.sh
3531
```
3632

37-
#### Run in Development Mode
33+
#### Run in Production Mode
3834

3935
```bash
40-
docker compose -f docker-compose-dev.yml up -d
36+
docker compose up --build --force-recreate
4137
```
4238

43-
#### Run in Production Mode
39+
This binds the local website code into the container and serves it at `http://localhost:8080`.
40+
41+
#### To remove old containers and images, run:
4442

4543
```bash
46-
docker compose up -d
44+
docker compose down -v --remove-orphans
4745
```
4846

49-
This binds the local website code into the container and serves it at `http://127.0.0.1:8080`.
47+
---

code/docker-cmd.sh

100644100755
Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
#!/bin/sh
2+
set -e
3+
4+
MANAGE_PY=$(find / -type f -name manage.py -print -quit)
5+
if [ -z "$MANAGE_PY" ]; then
6+
echo "Error: manage.py not found under /" >&2
7+
exit 1
8+
fi
9+
cd "$(dirname "$MANAGE_PY")"
10+
11+
su-exec "$USER" python manage.py migrate --noinput
12+
113
su-exec "$USER" python manage.py collectstatic --noinput
214

3-
USER_EXISTS="from django.contrib.auth import get_user_model; User = get_user_model(); exit(User.objects.exists())"
4-
su-exec "$USER" python manage.py shell -c "$USER_EXISTS" && su-exec "$USER" python manage.py createsuperuser --noinput
15+
USER_EXISTS_CMD="from django.contrib.auth import get_user_model; U = get_user_model(); exit(0) if U.objects.exists() else exit(1)"
16+
if su-exec "$USER" python manage.py shell -c "$USER_EXISTS_CMD"; then
17+
echo "Superuser already exists"
18+
else
19+
su-exec "$USER" python manage.py createsuperuser --noinput \
20+
--username "$DJANGO_SUPERUSER_USERNAME" \
21+
--email "$DJANGO_SUPERUSER_EMAIL"
22+
fi
23+
24+
nginx
525

626
if [ "$1" = "--debug" ]; then
7-
exec su-exec "$USER" python manage.py runserver "0.0.0.0:$DJANGO_DEV_SERVER_PORT"
27+
exec su-exec "$USER" python manage.py runserver "0.0.0.0:${DJANGO_DEV_SERVER_PORT:-8000}"
828
else
9-
exec su-exec "$USER" gunicorn "$PROJECT_NAME.wsgi:application" \
10-
--bind "0.0.0.0:$GUNICORN_PORT" \
11-
--workers "$GUNICORN_WORKERS" \
12-
--timeout "$GUNICORN_TIMEOUT" \
13-
--log-level "$GUNICORN_LOG_LEVEL"
29+
exec su-exec "$USER" gunicorn "${PROJECT_NAME}.wsgi:application" \
30+
--bind "0.0.0.0:${GUNICORN_PORT:-8000}" \
31+
--workers "${GUNICORN_WORKERS:-3}" \
32+
--timeout "${GUNICORN_TIMEOUT:-30}" \
33+
--log-level "${GUNICORN_LOG_LEVEL:-info}"
1434
fi

code/docker-compose-dev.yml

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

0 commit comments

Comments
 (0)