Provide a function to generate secrets

master
kaiyou 7 years ago
parent 08e95c08ca
commit b18a1d5217

@ -5,6 +5,8 @@ import json
import os import os
import jinja2 import jinja2
import uuid import uuid
import string
import random
app = flask.Flask(__name__) app = flask.Flask(__name__)
@ -12,15 +14,29 @@ flask_bootstrap.Bootstrap(app)
db = redis.StrictRedis(host='redis', port=6379, db=0) db = redis.StrictRedis(host='redis', port=6379, db=0)
def render_flavor(flavor, template, *args, **kwargs): def render_flavor(flavor, template, data):
return flask.render_template( return flask.render_template(
os.path.join(flavor, template), os.path.join(flavor, template),
*args, **kwargs **data
)
def secret(length=16):
charset = string.ascii_uppercase + string.digits
return ''.join(
random.SystemRandom().choice(charset)
for _ in range(length)
) )
def build_app(setup_path): def build_app(setup_path):
app.jinja_env.trim_blocks = True
app.jinja_env.lstrip_blocks = True
app.jinja_env.globals.update(dict(
secret=secret
))
versions = [ versions = [
path for path in os.listdir(setup_path) path for path in os.listdir(setup_path)
if os.path.isdir(os.path.join(setup_path, path)) if os.path.isdir(os.path.join(setup_path, path))
@ -43,14 +59,16 @@ def build_app(setup_path):
@bp.route("/submit", methods=["POST"]) @bp.route("/submit", methods=["POST"])
def submit(): def submit():
uid = uuid.uuid4() uid = uuid.uuid4()
db.set(uid, json.dumps(flask.request.form)) data = flask.request.form.copy()
data.update(dict(uid=uid, version=version))
db.set(uid, json.dumps(data))
return flask.redirect(flask.url_for('.setup', uid=uid)) return flask.redirect(flask.url_for('.setup', uid=uid))
@bp.route("/setup/<uid>", methods=["GET"]) @bp.route("/setup/<uid>", methods=["GET"])
def setup(uid): def setup(uid):
data = json.loads(db.get(uid)) data = json.loads(db.get(uid))
flavor = data.get("flavor", "compose") flavor = data.get("flavor", "compose")
rendered = render_flavor(flavor, "setup.html", uid=uid, data=data) rendered = render_flavor(flavor, "setup.html", data)
return flask.render_template("setup.html", contents=rendered) return flask.render_template("setup.html", contents=rendered)
@bp.route("/file/<uid>/<filepath>", methods=["GET"]) @bp.route("/file/<uid>/<filepath>", methods=["GET"])
@ -58,7 +76,7 @@ def build_app(setup_path):
data = json.loads(db.get(uid)) data = json.loads(db.get(uid))
flavor = data.get("flavor", "compose") flavor = data.get("flavor", "compose")
return flask.Response( return flask.Response(
render_flavor(flavor, filepath, uid=uid, data=data), render_flavor(flavor, filepath, data),
mimetype="application/text" mimetype="application/text"
) )
@ -66,7 +84,6 @@ def build_app(setup_path):
print("Serving version {}".format(version)) print("Serving version {}".format(version))
if __name__ == "__main__": if __name__ == "__main__":
build_app("/tmp/mailutest") build_app("/tmp/mailutest")
app.run(debug=True) app.run(debug=True)

Loading…
Cancel
Save