onion: add keys and certs to database

This commit is contained in:
Iain Learmonth 2022-11-09 13:36:12 +00:00
parent f603cb9101
commit d5824aa518
4 changed files with 107 additions and 6 deletions

View file

@ -4,9 +4,10 @@ from typing import Optional
from flask import flash, redirect, url_for, render_template, Response, Blueprint
from flask.typing import ResponseReturnValue
from flask_wtf import FlaskForm
from flask_wtf.file import FileAllowed, FileRequired
from sqlalchemy import exc
from wtforms import StringField, SelectField, SubmitField
from wtforms.validators import DataRequired, Length
from wtforms import StringField, SelectField, SubmitField, FileField
from wtforms.validators import DataRequired
from app.extensions import db
from app.models.base import Group
@ -18,9 +19,17 @@ bp = Blueprint("onion", __name__)
class NewOnionForm(FlaskForm): # type: ignore
domain_name = StringField('Domain Name', validators=[DataRequired()])
onion_name = StringField('Onion Name', validators=[DataRequired(), Length(min=56, max=56)],
description="Onion service hostname, excluding the .onion suffix")
# onion_name = StringField('Onion Name', validators=[DataRequired(), Length(min=56, max=56)],
# description="Onion service hostname, excluding the .onion suffix")
description = StringField('Description', validators=[DataRequired()])
onion_private_key = FileField('Onion Private Key', validators=[FileRequired()])
onion_public_key = FileField('Onion Public Key',
description="The onion hostname will be automatically calculated from the public key.",
validators=[FileRequired()])
tls_private_key = FileField('TLS Private Key (PEM format)',
description=("If no TLS key and certificate are provided, a self-signed certificate "
"will be generated."))
tls_public_key = FileField('TLS Certificate (PEM format)')
group = SelectField('Group', validators=[DataRequired()])
submit = SubmitField('Save Changes')
@ -28,6 +37,13 @@ class NewOnionForm(FlaskForm): # type: ignore
class EditOnionForm(FlaskForm): # type: ignore
description = StringField('Description', validators=[DataRequired()])
group = SelectField('Group', validators=[DataRequired()])
onion_private_key = FileField('Onion Private Key')
onion_public_key = FileField('Onion Public Key',
description="The onion hostname will be automatically calculated from the public key.")
tls_private_key = FileField('TLS Private Key (PEM format)',
description="If no file is submitted, the TLS key will remain unchanged.")
tls_public_key = FileField('TLS Certificate (PEM format)',
description="If no file is submitted, the TLS certificate will remain unchanged.")
submit = SubmitField('Save Changes')
@ -40,7 +56,17 @@ def onion_new(group_id: Optional[int] = None) -> ResponseReturnValue:
onion = Onion()
onion.group_id = form.group.data
onion.domain_name = form.domain_name.data
onion.onion_name = form.onion_name.data
# onion.onion_name = form.onion_name.data
for at in [
"onion_private_key",
"onion_public_key",
"tls_private_key",
"tls_public_key"
]:
print(f"testing {at}")
if form.__getattribute__(at).data is not None:
print(f"Setting {at}")
onion.__setattr__(at, form.__getattribute__(at).data.read())
onion.description = form.description.data
onion.created = datetime.utcnow()
onion.updated = datetime.utcnow()
@ -72,6 +98,16 @@ def onion_edit(onion_id: int) -> ResponseReturnValue:
if form.validate_on_submit():
onion.group_id = form.group.data
onion.description = form.description.data
for at in [
"onion_private_key",
"onion_public_key",
"tls_private_key",
"tls_public_key"
]:
print(f"testing {at}")
if form.__getattribute__(at).data is not None:
print(f"Setting {at}")
onion.__setattr__(at, form.__getattribute__(at).data.read())
onion.updated = datetime.utcnow()
try:
db.session.commit()