Added SSL support

Closes #1
This commit is contained in:
Felipe M 2021-01-19 11:13:50 +01:00
parent e3a829df44
commit 5c5578974d
3 changed files with 42 additions and 4 deletions

View File

@ -6,3 +6,21 @@ deploy it with the python:3.6 builder and it should work out of the box.
It provides several routes for testing on the index page.
Feel free to send patches.
## Configuration
| Variable name | Default | Description |
| --- | --- | --- |
| `SERVE_SSL` | `off` | Use to tell the app to serve SSL instead of plain text. [Check below](#serving-using-ssl)
## Serving using SSL
There are two ways of enabling SSL on this test application, either with an on-the-fly self-signed certificate provided by werkzeug or by providing the certificates using a secret on a fixed path.
### Serving using an on-the-fly certificate
Just set the `SERVE_SSL` variable to `adhoc`.
### Serving using certificates provided by a secret
Set the `SERVE_SSL` variable to `secret` and mount a secret with a `tls.key` and `tls.crt` under `/tmp/app`.

25
app.py
View File

@ -2,8 +2,9 @@ import copy
import json
import logging
import os
import sys
import ssl
import subprocess
import sys
import time
import requests
@ -12,6 +13,24 @@ from flask_httpauth import HTTPBasicAuth
from werkzeug.routing import Rule
from werkzeug.security import generate_password_hash, check_password_hash
# SSL configuration
SERVE_SSL = os.environ.get("SERVE_SSL", "off")
SERVE_SSL_ALLOWED = {"off", "adhoc", "secret"}
assert SERVE_SSL in SERVE_SSL_ALLOWED, f"SSL_MODE is not set to a valid value: {SERVE_SSL_ALLOWED}"
options = {}
if SERVE_SSL == "adhoc":
options = {
"ssl_context": SERVE_SSL,
}
if SERVE_SSL == "secret":
options = {
"ssl_context": ("/tmp/app/tls.crt", "/tmp/app/tls.key"),
}
app = Flask(__name__)
app.url_map.add(Rule("/request", endpoint="request"))
logging.basicConfig(format="%(message)s", level="INFO")
@ -274,7 +293,7 @@ def items_view():
Returns a JSON list with the items specified by the `items_number` parameter.
"""
items_number = request.args.get("items_number", 1)
items_number = request.args.get("issl_contexttems_number", 1)
item = {"this": "is", "a": "json", "big": "body"}
response_body = [copy.copy(item) for i in range(0, int(items_number))]
@ -312,4 +331,4 @@ def test_redirect_view():
if __name__ == "__main__":
app.run(debug=True, port=8080, host="0.0.0.0")
app.run(debug=True, port=8080, host="0.0.0.0", **options)

View File

@ -1,3 +1,4 @@
flask
Flask-HTTPAuth
requests
requests
cryptography