Note
By contributing to this project, you agree to abide by our Code of Conduct.
This is the code that powers the SecureDrop.org website. It is built with Wagtail and served at securedrop.org.
| Environment | Status |
|---|---|
| Production | |
| Development |
The installation instructions below assume you have the following software on your machine:
When you want to play with the environment, you will be using
docker compose. Your guide to understand all the nuances of
docker compose can be found in the official
docs. To start the
environment, run the following your first run:
# Starts up the environment (records your host UID in .env on the way,
# so a bare `docker compose up` works afterwards too)
just dev
# Inject development data (also only needs to be run once)
just createdevdata
# install pre-commit and set up hooks
pip install pre-commit
pre-commit installYou should be able to hit the web server interface by running
just open-browser:
just open-browserIf have problems starting the application locally, check the Troubleshooting section below.
To perform a quick spot check to ensure all tests are passing you can run:
just testThis runs the Django test suite inside the django container with
coverage, and reports a coverage summary (with a 70% floor) when it
finishes.
To quickly run Python linting you can use:
just lintTo run a subset of tests directly:
docker compose exec django ./manage.py test <app_or_path> --noinputBrowser-level tests live in e2e/ and run with
Playwright.
There are a few kinds of tests, selectable by tag:
@smoke, a quick test to ensure a particular page renders@functional, behavioral and other tests that are run as part of our QA process@visual, compare screenshots against baseline images to test for visual regressions
One test can have multiple tags.
To run the tests against deterministic data in the production image, first bring up the QA stack:
# In place of `just dev`
just qa-upThis runs the production image with production settings -- gunicorn,
whitenoise and DEBUG off -- so there is no autoreload and no asset
watcher. Re-run just qa-up to pick up a change, or use just dev
while iterating.
Then, separately, run the full browser test suite:
# Full suite
just qa
# Or run all tests tagged with "smoke"
just qa --grep smoke
# Or run a specific test with a specific browser
just qa --project=chromium tests/smoke/homepage.spec.tsSome deployed environments require authentication before we can access them. To run tests against a deployed environment, first (if necessary),
just qa-auth stagingThis will open Playwright in UI mode, access the URL, and save the
session information it captures to e2e/.auth/[ENV NAME].json. This
requires Playwright to be installed locally:
npx playwright installThen end-to-end tests can be run against that environment:
# Just run tests against a subdomain of securedrop.org
just qa stagingPlease note, visual tests have unique baseline images per-environment.
When a change is intended to alter the rendered page, the visual
baselines will need updating. just qa-update takes the same arguments
as just qa:
just qa-update
# Or, for a deployed environment
just qa-update stagingScreenshots differ between browsers and platforms, so baselines recorded
locally are only comparable when recorded in the same container CI uses.
Please ensure baselines for the seed data are updated through
just qa-update and not with Playwright running on the host.
To reset your database back to its initial state, run:
just reset-dbThis removes the postgresql container and re-seeds it by running
createdevdata.
Sometimes when dependencies are changed or a Docker image needs to be updated for other reasons, the containers will need to be manually triggered to rebuild. These commands, listed in order of destructiveness can resolve most container issues:
docker compose up --buildAdding the --build flag tells Docker Compose to detect and update any
images that require new changes. You can safely add the --build flag
under most circumstances without adverse effects.
docker compose up --build --force-recreateAdding the --force-recreate flag tells Docker Compose to recreate all
containers that are part of the application. Note that this only
recreates containers from existing images; it does not rebuild the
images themselves, so it won't help if a cached image layer (e.g. a
stale pip install or npm install step) is the problem.
docker compose build --no-cacheIf the above two commands don't help, the build cache itself may be
stale. --no-cache throws out Docker's build cache and rebuilds every
image layer from scratch (slow, but thorough). Follow it with
docker compose up to start the freshly built images.
If none of the above fix the issues you're encountering, ensure all
docker containers are stopped (Ctrl-C if containers are running in a
shell, docker compose kill if they are running detached) and run the
following commands. These commands will remove all images and containers
and rebuild from scratch. Any data in your database will be wiped.
docker compose rm
docker compose up --buildIf you want to use the PDB program for debugging, it is possible. First, add this line to an area of the code you wish to debug:
import ipdb
ipdb.set_trace()Second, attach to the running Django container. This must be done in a shell, and it is within this attached shell that you will be able to interact with the debugger. Run:
just attachOnce you have done this, you can load the page that will run the code
with your import ipdb and the debugger will activate in the shell you
attached. To detach from the shell without stopping the container press
Control+P followed by Control+Q.
For local development, it is possible to enable the Django Debug
Toolbar by
setting the ENABLE_DEBUG_TOOLBAR environment variable when starting
docker compose:
ENABLE_DEBUG_TOOLBAR=1 docker compose upSee the documentation for more information about how to use this tool to explore template information or SQL queries. Note that when the toolbar is running, performance of the local server may be affected.
To work on any upstream dependency in-place during development, you can
clone the dependency into the develop-pkgs/ subdirectory. When you
start the django and node containers after doing so, a Python
package (providing a pyproject.toml) will be installed editable, and
any Node package (providing a package.json) will be built and watched,
so they can be worked on in-place while running the full securedrop.org
Django project.
You can mimic a production environment where django is deployed with
gunicorn, a reverse nginx proxy, and debug mode off using the
prod-docker-compose.yaml file. Note that build time for this
container takes much longer than the developer environment:
Run it via just prod, which builds the image first
(just build-prod builds alone):
just prodAll subsequent docker compose commands will need that explicit -f flag
pointing to the production-like compose file.
It is not run using live-code refresh, so it's not a great dev environment but is good for replicating issues that would come up in production.
To connect to the database, use the following credentials:
- username -
securedrop - password -
securedroppassword - dbname -
securedropdb - the host/port can be determined by running
docker compose port postgresql 5432
Drop a Postgres database dump into the root of the repo and rename it to
import.db. To import it into a running dev session (ensure
docker compose up has already been started) run just import-db. Note
that this will not pull in images that are referenced from an external
site backup.
When developing, it is often required to switch branches. These different branches can have mutually incompatible changes to the database, which can render the application inoperable. It is therefore helpful to be able to easily restore the database to a known-good state when making experimental changes. There are two commands provided to assist in this.
just save-db: Saves a snapshot of the current state of the database to
a file in the db-snapshots folder. This file is named for the
currently checked-out git branch.
just restore-db: Restores the most recent snapshot for the currently
checked-out git branch. If none can be found, that is, just save-db
has never been run for the current branch, this command will do nothing.
If a saved database is found, all data in database will be replaced with
that from the file. Note that this command will terminate all
connections to the database and delete all data there, so care is
encouraged.
Workflow suggestions. I find it helpful to have one snapshot for each
active branch I'm working on or reviewing, as well as for master.
Checking out a new branch and running its migrations should be followed
by running just save-db to give you a baseline to return to when
needed.
When checking out a new branch after working on another, it can be helpful to restore your snapshot from master, so that the migrations for the new branch, which were presumably based off of master, will have a clean starting point.
We use Poetry to manage Python dependencies.
You don't need to install it locally: the just recipes run a pinned
poetry in a throwaway container built from the Containerfile's poetry
stage. A host install works fine for day-to-day poetry add / poetry remove;
follow up with just lock, and CI's just lock-check catches anything missed.
The container images install poetry.lock directly, verifying hashes: the
prod image takes the application dependencies, the dev image adds the dev
group. Poetry itself is bootstrapped from
ci/containers/bootstrap-requirements.txt, a hash-pinned file rendered from the
lock's bootstrap group; commit it alongside poetry.lock.
Edit pyproject.toml -- dependencies under [project] for the
application, dev under [dependency-groups] for tooling -- and then run:
just lockThis is also the route for changing a requirement (i.e. restricting its version number) or removing one.
just lock leaves an already-locked package alone as long as it still
satisfies its constraint. To raise versions without editing pyproject.toml:
just lock-upgrade package-namewill update the package named package-name to the latest version allowed by
the constraints in pyproject.toml, and rewrite the bootstrap file. Naming no
package upgrades everything.
just lock-check fails if poetry.lock no longer matches
pyproject.toml, or if the bootstrap file no longer matches the lock, so
neither can go stale unnoticed; CI runs it as Lint:Lockfiles.
Dependabot PRs update poetry.lock but not the bootstrap file, so one
that bumps Poetry, or a package it depends on, fails Lint:Lockfiles on arrival.
Check out the branch, run just lock, and push.
You can log in to the Wagtail interface at /admin with the following
credentials:
- username -
test - password -
test
In order to ensure that all commands are run in the same environment, we have added a just lint command that checks Python code (ruff), SASS (stylelint), SVGs (svgo), PNGs (oxipng). It also runs bandit and the migration check. This is done in the container, rather than on your local env.
Use just ruff-fix to apply ruff's fixes and formatting in place.
Run just on its own to list every available recipe.
In addition to the management commands provided by Django and Wagtail, the project has a set of its own custom management commands. All commands listed should be prefaced by docker compose exec django ./manage.py.
These commands are meant to be used once at the beginning of
development. They can be run individually or all at once using the
createdevdata command. They should not be run in production as they
create fake data.
-
createdevdata [--delete]Runs all of the other
create*commands and creates fake data. Thedeleteflag deletes the current homepage and creates a new one. -
createqadataBuilds the small, deterministic site that the Playwright tests in
e2e/expect, tearing down existing content first. The QA stack (just qa-up) runs it automatically; tests that need particular content should add it here rather than depend oncreatedevdata. -
createblogdata <number_of_posts>Creates a blog index page and the indicated number of posts.
-
createdirectory <number_of_instances>Creates a directory page and theindicated number of SecureDrop instances.
-
createresultgroups [--delete]Creates the initial text for the scan results shown on the details page of a securedrop instance. The
deleteflag removes current result groups and result states. -
createfootersettingsCreates the initial default text, menus, and buttons for the footer.
-
createnavmenu [--delete]Creates the main nav menu and links it to the appropriate pages. Creates a
DirectoryPage,BlogIndexPage, andMarketingIndexPageif they do not yet exist. Thedeleteflag destroys the existing nav menu. -
createsearchmenus [--delete]Creates default search menus. The
deleteflag destroys any existing search menus.
-
scan [securedrops]Scan one or more SecureDrop landing pages (specified by space-separated domain names) for security. By default, scans all pages in the directory.
-
update_docs_index [--rebuild]Crawl the SecureDrop documentation pages on
https://docs.securedrop.org/en/stable/and update the correspondingSearchDocumententries. Pass--rebuildto this command to delete existing entries for documentation pages before fetching new data, which is useful if out-of-date information or pages are in the index. Rebuild is usually the behavior that you will want. Note that this command depends on a particular arrangement and format of HTML and links on the above 3rd party web URL. If these change in the future, then the command will potentially fail and report zero or only a few documents indexed. -
update_wagtail_index [--rebuild]Crawl Wagtail pages and create
SearchDocuments for each one. This command should only be run once when the repo is initialized, as thereafterSearchDocuments will be updated viaget_search_contentwhich is run when pages are created, updated, or deleted. Note that if pages are changed outside of the Wagtail interface, their search documents will not be updated and this command will need to be run again. Pass--rebuildto this command to delete existing entries for Wagtail pages before fetching new data, which is useful if out-of-date information or pages are in the index.