Python Flask middleware to ensure all requests have a request ID header.
Purpose
This middleware ensures all request made to a Flask application includes a Request ID header (X-Request-ID),
containing at least one unique value.
Request IDs
or Correlation IDs can be used when logging errors and allows users to trace requests through multiple layers such as
load balancers and reverse proxies. The request ID header value may consist of multiple IDs encoded according to
RFC 2616.
Installation
This package can be installed using Pip from PyPi:
$ pip install flask-request-id-header
Usage
This middleware has no required parameters and by default will add a request header to requests where one does not
already exist, or append a unique value where existing request IDs do not contain at least one UUID (version 4) value.
A minimal application would look like this:
from flask import Flask
from flask_request_id_header.middleware import RequestID
app = Flask(__name__)
RequestID(app)
@app.route('/')
def hello_world():
return 'hello world'
Optionally a Flask config option, REQUEST_ID_UNIQUE_VALUE_PREFIX, can be set whereby any request ID beginning with
this prefix be treated as unique, preventing an additional value being appended.
A docker container ran through Docker Compose is used as a development environment for this project defined . It
includes development only dependencies listed in requirements.txt, a local Flask application in app.py and
Integration tests.
Ensure classes and methods are defined within the flask_request_id_header package.
Ensure Integration tests are written for any new feature, or changes to existing features.
If you have access to the BAS GitLab instance, pull the Docker image from the BAS Docker Registry:
$ docker login docker-registry.data.bas.ac.uk
$ docker-compose pull
# To run the local Flask application using the Flask development server
$ docker-compose up
# To start a shell
$ docker-compose run app ash
Code Style
PEP-8 style and formatting guidelines must be used for this project, with the exception of the 80 character line limit.
Development Python dependencies should be declared in requirements.txt to be included in the development environment.
Runtime Python dependencies should be declared in requirements.txt and setup.py to also be installed as dependencies
of this package in other applications.
All dependencies should be periodically reviewed and update as new versions are released.
$ docker-compose run app ash
$ pip install [dependency]==
# this will display a list of available versions, add the latest to `requirements.txt` and or `setup.py`
$ exit
$ docker-compose down
$ docker-compose build
If you have access to the BAS GitLab instance, push the Docker image to the BAS Docker Registry:
To ensure the security of this API, all dependencies are checked against
Snyk for vulnerabilities.
Warning: Snyk relies on known vulnerabilities and can't check for issues that are not in it's database. As with all
security tools, Snyk is an aid for spotting common mistakes, not a guarantee of secure code.
Some vulnerabilities have been ignored in this project, see .snyk for definitions and the
Dependency exceptions section for more information.
Through Continuous Integration, on each commit current dependencies are tested and a snapshot
uploaded to Snyk. This snapshot is then monitored for vulnerabilities.
Dependency vulnerability exceptions
This project contains known vulnerabilities that have been ignored for a specific reason.
indirect dependency, required through the bandit package
severity is rated high
risk judged to be low as we don't use the Yaml module in this application
ignored for 1 year for re-review
Static security scanning
To ensure the security of this API, source code is checked against Bandit for issues
such as not sanitising user inputs or using weak cryptography.
Warning: Bandit is a static analysis tool and can't check for issues that are only be detectable when running the
application. As with all security tools, Bandit is an aid for spotting common mistakes, not a guarantee of secure code.
This project uses integration tests to ensure features work as expected and to guard against regressions and
vulnerabilities.
The Python UnitTest library is used for running tests using Flask's
test framework. Test cases are defined in files within tests/ and are automatically loaded when using the
test Flask CLI command included in the local Flask application in the development environment.
Both source and binary versions of the package are build using SetupTools, which
can then be published to the Python package index for use in other
applications. Package settings are defined in setup.py.
This project is built and published to PyPi automatically through Continuous Deployment.
To build the source and binary artefacts for this project manually:
$ docker-compose run app ash
# build package to /build, /dist and /flask_reverse_proxy_fix.egg-info
$ python setup.py sdist bdist_wheel
$ exit
$ docker-compose down
To publish built artefacts for this project manually to PyPi testing:
$ docker-compose run app ash
$ python -m twine upload --repository-url https://test.pypi.org/legacy/ dist/*
# project then available at: https://test.pypi.org/project/flask-request-id-header/
$ exit
$ docker-compose down
You may use and re-use this software and associated documentation files free of charge in any format or medium, under
the terms of the Open Government Licence v3.0.