Skip to content

Commit 4e889cb

Browse files
committed
Clean up IDP/RP docker configuration to use it during development
- Dockerfile now only is concerned with building the container. Everything else (volumes, port configuration, environment variables for the application) is defined via compose - Add a sample env file to allow easy configuration of different setups during development. - Moved dependencies defined on requirements.txt from idp folder into dev dependencies from pyproject - Added documentation to set up those services with Docker
1 parent 48f4d54 commit 4e889cb

File tree

11 files changed

+93
-64
lines changed

11 files changed

+93
-64
lines changed

.env

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

.env.sample.docker

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
DEBUG=0 # Change to any non-zero number to enable debug mode

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,6 @@ db.sqlite3
5757
venv/
5858

5959
/tests/app/idp/static
60+
61+
# files with environment variables
62+
.env

AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Peter Karman
9999
Peter McDonald
100100
Petr Dlouhý
101101
pySilver
102+
Raphael Lullis
102103
Rodney Richardson
103104
Rustem Saiargaliev
104105
Rustem Saiargaliev

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
## [unreleased]
99
### Added
1010
* #1506 Support for Wildcard Origin and Redirect URIs
11+
12+
### Changed
13+
* #1548 Docker and docker-compose to run test applications
14+
1115
<!--
1216
### Changed
1317
### Deprecated

Dockerfile

Lines changed: 18 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,6 @@
44
# tests/app/idp. This way we build images with the source
55
# code from the repos for validation before publishing packages.
66

7-
FROM python:3.11.6-slim as builder
8-
9-
ENV PYTHONDONTWRITEBYTECODE 1
10-
ENV PYTHONUNBUFFERED 1
11-
12-
ENV DEBUG=False
13-
ENV ALLOWED_HOSTS="*"
14-
ENV TEMPLATES_DIRS="/data/templates"
15-
ENV STATIC_ROOT="/data/static"
16-
ENV DATABASE_URL="sqlite:////data/db.sqlite3"
17-
18-
RUN apt-get update
19-
# Build Deps
20-
RUN apt-get install -y --no-install-recommends gcc libc-dev python3-dev git openssh-client libpq-dev file libev-dev
21-
# bundle code in a virtual env to make copying to the final image without all the upstream stuff easier.
22-
RUN python -m venv /opt/venv
23-
ENV PATH="/opt/venv/bin:$PATH"
24-
# need to update pip and setuptools for pep517 support required by gevent.
25-
RUN pip install --upgrade pip
26-
RUN pip install --upgrade setuptools
27-
COPY . /code
28-
WORKDIR /code/tests/app/idp
29-
RUN pip install -r requirements.txt
30-
RUN pip install gunicorn
31-
RUN python manage.py collectstatic --noinput
32-
33-
34-
357
FROM python:3.11.6-slim
368

379
# allow embed sha1 at build time as release.
@@ -41,27 +13,29 @@ LABEL org.opencontainers.image.authors="https://jazzband.co/projects/django-oaut
4113
LABEL org.opencontainers.image.source="https://github.com/jazzband/django-oauth-toolkit"
4214
LABEL org.opencontainers.image.revision=${GIT_SHA1}
4315

16+
RUN pip install --upgrade pip setuptools uv
4417

4518
ENV SENTRY_RELEASE=${GIT_SHA1}
19+
ENV PYTHONDONTWRITEBYTECODE 1
20+
ENV PYTHONUNBUFFERED 1
21+
ENV UV_CACHE_DIR /var/cache/uv
22+
ENV UV_PROJECT_ENVIRONMENT /opt/dot/venv
23+
24+
RUN apt-get update
25+
# Build Deps
26+
RUN apt-get install -y --no-install-recommends libc-dev python3-dev file libev-dev
27+
# bundle code in a virtual env to make copying to the final image without all the upstream stuff easier.
28+
ENV PYTHONPATH="/code/tests/app/idp:$PYTHONPATH"
29+
# need to update pip and setuptools for pep517 support required by gevent.
4630

47-
# disable debug mode, but allow all hosts by default when running in docker
48-
ENV DEBUG=False
49-
ENV ALLOWED_HOSTS="*"
50-
ENV TEMPLATES_DIRS="/data/templates"
51-
ENV STATIC_ROOT="/data/static"
52-
ENV DATABASE_URL="sqlite:////data/db.sqlite3"
5331

32+
WORKDIR /code
33+
COPY ./pyproject.toml /code
34+
COPY ./uv.lock /code
35+
COPY ./oauth2_provider /code/oauth2_provider
5436

37+
RUN uv sync --extra dev
5538

39+
COPY ./tests /code/tests
5640

57-
COPY --from=builder /opt/venv /opt/venv
58-
ENV PATH="/opt/venv/bin:$PATH"
59-
COPY --from=builder /code /code
60-
RUN mkdir -p /code/tests/app/idp/static /code/tests/app/idp/templates
6141
WORKDIR /code/tests/app/idp
62-
RUN apt-get update && apt-get install -y \
63-
libpq5 \
64-
&& rm -rf /var/lib/apt/lists/*
65-
EXPOSE 80
66-
VOLUME ["/data" ]
67-
CMD ["gunicorn", "idp.wsgi:application", "-w 4 -b 0.0.0.0:80 --chdir=/code --worker-tmp-dir /dev/shm --timeout 120 --error-logfile '-' --log-level debug --access-logfile '-'"]

docker-compose.yml

Lines changed: 37 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,50 @@
1-
volumes:
2-
idp-data:
3-
1+
x-idp-network: &idp-network
2+
networks:
3+
- idp-net
44

55
x-idp: &idp
6+
<<: *idp-network
67
image: django-oauth-toolkit/idp
8+
build: .
9+
stdin_open: true
10+
tty: true
711
volumes:
12+
- ./:/code
813
- idp-data:/data
14+
- static-data:/var/oauth/static
15+
command: uv run django-admin runserver 0.0.0.0:80
16+
17+
environment: &fediverser-service-environment
18+
DJANGO_SETTINGS_MODULE: idp.settings
19+
STATIC_ROOT: /var/oauth/static
20+
ALLOWED_HOSTS: "*"
21+
TEMPLATES_DIRS: /code/tests/app/idp/templates
22+
DATABASE_URL: sqlite:////data/db.sqlite3
23+
24+
env_file:
25+
- .env
926

1027
services:
28+
idp-collectstatic:
29+
<<: *idp
30+
command: uv run django-admin collectstatic --noinput
31+
restart: on-failure
32+
1133
idp-migrate:
1234
<<: *idp
13-
build: .
14-
command: python manage.py migrate
35+
command: uv run django-admin migrate
36+
depends_on:
37+
- idp-collectstatic
1538

1639
idp-loaddata:
1740
<<: *idp
18-
command: python manage.py loaddata fixtures/seed.json
41+
command: uv run django-admin loaddata fixtures/seed.json
1942
depends_on:
2043
idp-migrate:
2144
condition: service_completed_successfully
2245

2346
idp:
2447
<<: *idp
25-
command: gunicorn idp.wsgi:application -w 4 -b 0.0.0.0:80 --chdir=/code --timeout 120 --error-logfile '-' --log-level debug --access-logfile '-'
2648
ports:
2749
# map to dev port.
2850
- "8000:80"
@@ -37,4 +59,11 @@ services:
3759
# map to dev port.
3860
- "5173:3000"
3961
depends_on:
40-
- idp
62+
- idp
63+
64+
networks:
65+
idp-net:
66+
67+
volumes:
68+
idp-data:
69+
static-data:

docs/contributing.rst

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,30 @@ This is a `Jazzband <https://jazzband.co>`_ project. By contributing you agree t
1212
Setup
1313
=====
1414

15-
Fork ``django-oauth-toolkit`` repository on `GitHub <https://github.com/jazzband/django-oauth-toolkit>`_ and follow these steps:
1615

17-
* Create a virtualenv and activate it
18-
* Clone your repository locally
16+
Fork ``django-oauth-toolkit`` repository on `GitHub
17+
<https://github.com/jazzband/django-oauth-toolkit>`_ and clone to your
18+
development machine. The recommended method to manage your development
19+
environment is to use `uv <https://docs.astral.sh/uv/>`_, but you can
20+
also use pip directly to manage the virtual environment.
21+
22+
23+
Running the test IDP and RP applications (Docker)
24+
=================================================
25+
26+
During development, it might be helpful to run an application that
27+
actually exercises the many authentication flows. We provide a django
28+
project that can be set up to run as the Identity Provider (IDP) and a
29+
separate Javascript application that works as the Relying Party (RP).
30+
If you have docker and docker-compose configured, you can get them
31+
both the services running with a few simple steps:
32+
33+
- make a copy of the ``.env.sample.docker`` to ``.env`` and make changes as desired
34+
- Run ``docker-compose up -d --build``
35+
36+
The IDP service will be available on `http://localhost:8000` and the
37+
RP service will be available on `http://localhost:5173`
38+
1939

2040
Issues
2141
======

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ dev = [
4545
"pytest",
4646
"pytest-cov",
4747
"m2r",
48-
"sphinx-rtd-theme",
48+
"sphinx-rtd-theme",
49+
"django-cors-headers",
50+
"django-environ",
4951
]
5052

5153
[project.urls]

tests/app/idp/idp/urls.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1616
"""
1717

18+
from django.conf import settings
19+
from django.conf.urls.static import static
1820
from django.contrib import admin
1921
from django.urls import include, path
2022

2123

22-
urlpatterns = [
24+
urlpatterns = static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) + [
2325
path("admin/", admin.site.urls),
2426
path("o/", include("oauth2_provider.urls", namespace="oauth2_provider")),
2527
path("accounts/", include("django.contrib.auth.urls")),

tests/app/idp/requirements.txt

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

0 commit comments

Comments
 (0)