Skip to content

Enables a simple HTTP proxy to generate JWT keys from a posted username

Notifications You must be signed in to change notification settings

nandoabreu/jwt-flask-proxy

Repository files navigation

jwt-flask-proxy

This project enables a JWT access key generator in an HTTP proxy written in Python 3.6 with Flask framework.

 
 

README Map

Set-up and install

I recommend using virtualenv to run py manually.

Install the requirements:

$ python3 -m pip install -r requirements.txt

Note: The proxy/config.py file should not be in a public repo, but once the secret key is public, I kept the file to facilitate the review. Normally, I would instruct copying and editing proxy/config.py.tpl.

Running the proxy server

By default, the proxy runs on port 5000. This can be changed in the config file.

$ python3 -m proxy

At this point, we should be able to browse: http://localhost:5000/
Please remember to hit Ctrl+c to stop the web server when done.

Automatic tests

Python tests are available using unittest/PyUnit via Makefile or manually.
Note: depending on how the proxy was started, may require sudo chmod o+w logs/* (not if in production).

  • Run make test to install requirements and run the tests.
  • Or install requirements and manually run python3 -m unittest tests/test_*.

An extra basic test is available in Bash script using curl and it tests proxy running manually, containerised or composed:

$ bash tests/curl-post.bash

The class to generate JWT

With the python console and the class that generates JWT, we can get a Token:

$ python3
>>> from proxy.Token import Token
>>> user = 'fernando@github.com'
>>> t = Token(user)
>>> t.jwt

Containerise with the Dockerfile

I assume that you have docker engine running. If not, please see Get Docker.

If you rather run the proxy in a single container, run:

$ docker run --rm -d -p 5000:5000 --name proxy $(docker build -f Dockerfile -t proxy . -q)

To know IP and Port to the containerised app:

$ docker inspect proxy | grep -e IPAddr.*[0-9] -e HostPort | sed 's/[^0-9\.]//g' | sort -u

After this, we should be able to browse: http://<container IP>:5000/

To stop container and clean image, use:

$ docker stop proxy && docker image rm proxy

Run the Docker compose

I assume that you have docker compose installed. If not, please see Install Docker Compose.

There are Makefile rules to simplify this option. See the list of commands:

  • $ make to build and run (up) the application.
    • or run $ make build; make run (note: run already calls build).
  • $ make stop and make start to start the container.
  • $ make rm to remove compose service, container, image.

The default HTTP proxy PORT is 5000 and set in .env. The port can be changed:

$ HTTP_PORT=8080 make up

Logs

By default, logs are recorded in the 'logs' directory in the project's root. However, if you containerise the proxy, logs will be inside the container. And if you docker compose, the container will use the hosts' dirs as in Running the proxy.

Please see docs/logs if you wish to access samples of the generated logs.

Documentation

Please try from python console:

$ python3
>>> import proxy
>>> help(proxy)

Or try from command line:

$ python3 -c "import proxy; help(proxy.config)"

All documentation can be found in docs.

To do

  • Document separation of server from proxy._main_
  • Configure rotation of log files.
  • Review docstring in db.py.