ElectionGuard Web API in python to demonstrate usage of electionguard-python in performing ballot encryption, casting, spoiling, and tallying.
Перейти к файлу
dependabot[bot] 3386e81755
Bump certifi from 2021.10.8 to 2023.7.22
Bumps [certifi](https://github.com/certifi/python-certifi) from 2021.10.8 to 2023.7.22.
- [Commits](https://github.com/certifi/python-certifi/compare/2021.10.08...2023.07.22)

---
updated-dependencies:
- dependency-name: certifi
  dependency-type: indirect
...

Signed-off-by: dependabot[bot] <support@github.com>
2023-07-25 18:58:40 +00:00
.github 🐛 Resolve Windows Docker Bug & 🐍 Upgrade to Python 3.9 (#181) 2021-12-08 14:58:22 -05:00
.vscode 🐛 #192 - Guardian 404 (#193) 2021-12-21 14:02:48 -05:00
app 🪲 Bug #219 tally fixup (#220) 2022-03-14 16:31:20 -04:00
docs 📝 Improve Documentation (#84) 2020-09-21 10:24:44 -04:00
images Start of Python API using Fast API 2020-09-14 11:22:31 -04:00
tests 📫 #222 postman election tests (#225) 2022-03-29 13:24:05 -04:00
.dockerignore Improved Dockerfile (#59) 2020-09-14 11:22:31 -04:00
.gitignore Tally Decryption (#161) 2021-08-17 15:31:48 -04:00
CODE_OF_CONDUCT.md Initial CODE_OF_CONDUCT.md commit 2019-11-13 00:47:10 -08:00
CONTRIBUTING.md Adjust Contributing and Update Templates 2021-09-20 15:22:13 -04:00
Dockerfile Upgrade dockerfile to python3.9 (#185) 2021-12-08 15:32:35 -05:00
LICENSE Initial LICENSE commit 2020-02-13 14:24:48 -05:00
Makefile docker-postman-test now works on Windows (#190) 2021-12-14 11:44:36 -05:00
README.md 🐛 #192 - Guardian 404 (#193) 2021-12-21 14:02:48 -05:00
SECURITY.md Initial SECURITY.md commit 2020-02-13 14:24:48 -05:00
docker-compose.azure.yml Add Submit Ballot API (#154) 2021-06-01 15:01:01 -04:00
docker-compose.dev.yml Feature/key ceremony (#159) 2021-06-28 09:28:20 -04:00
docker-compose.support.yml 🐛 Resolve Windows Docker Bug & 🐍 Upgrade to Python 3.9 (#181) 2021-12-08 14:58:22 -05:00
docker-compose.yml Prevent automatic shutdown in Docker/Linux (#119) 2020-10-12 13:55:05 -04:00
mkdocs.yml Feature/key ceremony (#159) 2021-06-28 09:28:20 -04:00
mongo-init.js 🛡. Add authentication and authorization (#172) 2021-11-19 09:40:01 -05:00
poetry.lock Bump certifi from 2021.10.8 to 2023.7.22 2023-07-25 18:58:40 +00:00
pyproject.toml 🪲 Bug #219 tally fixup (#220) 2022-03-14 16:31:20 -04:00

README.md

Microsoft Defending Democracy Program: ElectionGuard

🗳️ ElectionGuard Web API

docker version docker pulls Language grade: Python Total alerts Documentation Status license

The ElectionGuard Web API is a python-based application that provides a thin, stateless wrapper around the electionguard-python library to perform ballot encryption, casting, spoiling, and tallying. This API is implemented using FastAPI.

If you aren't familiar with ElectionGuard and its concepts, first take a stroll through the official documentation.

👯‍♀️ Two APIs in One

Before you begin you should be aware that the application can run in one of two modes:

  • guardian mode runs features used by Guardians (key ceremony actions, partial tally decryption, etc.)
  • mediator mode runs features used by Mediators (ballot encryption, casting, spoiling, etc.)

In practice, you will likely need to run at least one instance of each mode. We provide a single codebase and Docker image, but the mode can be set at runtime as described below.

Getting Started

This codebase can be run using one of three different approaches:

  1. 🐳 Running with Docker
  2. 🐳 Developing with Docker
  3. 🐍 Developing with Python

🐳 1. Running with Docker

This approach runs an official published image. This approach is not intended for development. It works on Windows, Mac, and Linux.

For convenience the Docker image is hosted on both Github Packages and DockerHub. You can choose whichever container image library works best for you.

1.1 Pulling from Github Packages

Note: GitHub Packages requires authentication to retrieve the package. This requires a GitHub Access Token and using docker login. Follow GitHub instructions.

# Pull the image from Github
docker pull docker.pkg.github.com/microsoft/electionguard-web-api/electionguard-web-api:main

# Start a container for the API in mediator mode, exposed on port 80 of the host machine
docker run -d -p 80:8000 --env API_MODE=mediator docker.pkg.github.com/microsoft/electionguard-web-api/electionguard-web-api:main

1.2 Pulling from DockerHub

Pulling from DockerHub is simpler as it requires no additional authentication.

# Pull the image from DockerHub
docker pull electionguard/electionguard-web-api:latest

# Start a container for the API in mediator mode, exposed on port 80 of the host machine
docker run -d -p 80:8000 --env API_MODE=mediator electionguard/electionguard-web-api:latest

🐳 2. Developing with Docker

Developing with Docker is the fastest approach for getting started because it has virtually no local dependencies (e.g. Python). This approach works on Windows, Mac, and Linux. It uses a Dockerfile and docker-compose.yml.

2.1 Prerequisities

🏃‍♀️ 2.2 Running 🏃‍♂️

To get started run both APIs at the same time:

make docker-run

Or run both APIs in development mode, with automatic reloading on file change:

make docker-dev

After either command, you will find the mediator API running at http://127.0.0.1:8000 and the guardian API at http://127.0.0.1:8001

🐍 3. Developing with Python

Developing with Python provides the fastest developer inner loop (speed from code changes to seeing effects of changes), but is more work to set up initially. It works on Mac and Linux. It also works via WSL 2 on Windows.

3.1. Windows Prerequisites

On Windows you can use an IDE of your choice in Windows, and run the make and Python commands in WSL which will expose API's in Windows. Developing with Python on Windows involves the following additional setup that is not required for Linux or Mac.

  1. Install WSL 2
  2. Install Ubuntu (other Linux distributions should also work with minor modifications to the instructions below)
  3. Install pyenv prerequisites. Technically you could just install Python and it would be simpler, but this approach will provide more flexibility. To install the prerequisites:
sudo apt-get update
sudo apt-get install make build-essential libssl-dev zlib1g-dev \
  libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
  libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev
  1. Install pyenv via pyenv-installer and add it the startup scripts:
curl https://pyenv.run | bash
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n eval "$(pyenv init -)"\nfi' >> ~/.bashrc
sed -Ei -e '/^([^#]|$)/ {a \
  export PYENV_ROOT="$HOME/.pyenv"
  a \
  export PATH="$PYENV_ROOT/bin:$PATH"
  a \
  ' -e ':a' -e '$!{n;ba};}' ~/.profile
echo 'eval "$(pyenv init --path)"' >>~/.profile

echo 'eval "$(pyenv init -)"' >> ~/.bashrc
  1. Restart shell
  2. Install Python 3.9 via pyenv
pyenv install 3.9.9
pyenv global 3.9.9

3.2 Mac/Linux Prerequisites

Install Python 3.9. We additionally recommend pyenv to assist with Python version management (see detailed instructions in the Windows section above starting with Step 3).

🏃‍♀️ 3.2 Running 🏃‍♂️

Using make, install and setup the environment:

make environment

Start the api as mediator

make start API_MODE=mediator

OR as guardian

make start API_MODE=guardian

Debugging Mac/Linux

For local debugging with Visual Studio Code, choose the Guardian Web API or Mediator Web API options from the dropdown in the Run menu. Once the server is up, you can easily hit your breakpoints.

If the code fails to run, make sure your Python interpreter is set to use your poetry environment.

Debugging Windows

With Visual Studio Code:

  1. Install the Remote WSL extension
  2. In the bottom left click the Green Icon and "New WSL Window using Distro", select Ubuntu
  3. F5
  4. Choose either Guardian Web API or Mediator Web API

🧪 Testing

End-to-end integration tests can be found in the /tests/integration folder. To see them in action, run:

make test-integration

A Postman collection is also available to test the API located in the /tests/postman folder. You can do a few things with this:

  • Import into Postman for easy manual testing.
  • Run locally with the Newman CLI.
  • Run the APIs and tests entirely in Docker by running:
    make docker-postman-test
    

📝 Documentation

FastApi defaultly has API documentation built in. The following is available after running:

Overviews of the API itself are available on:

🗄 Archived

As of 06/15/2020, the previous C wrapped implementation was transitioned to the python version. ElectionGuard development has transitioned to the ElectionGuard-Python Repo. The old version is available using the dotnet-api tag.

🤝 Contributing

This project encourages community contributions for development, testing, documentation, code review, and performance analysis, etc. For more information on how to contribute, see the contribution guidelines

Code of Conduct

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Reporting Issues

Please report any bugs, feature requests, or enhancements using the GitHub Issue Tracker. Please do not report any security vulnerabilities using the Issue Tracker. Instead, please report them to the Microsoft Security Response Center (MSRC) at https://msrc.microsoft.com/create-report. See the Security Documentation for more information.

Have Questions?

Electionguard would love for you to ask questions out in the open using GitHub Issues. If you really want to email the ElectionGuard team, reach out at electionguard@microsoft.com.

License

This repository is licensed under the MIT License