lint: reformat python code with black
This commit is contained in:
parent
331beb01b4
commit
a406a7974b
88 changed files with 2579 additions and 1608 deletions
128
app/api/onion.py
128
app/api/onion.py
|
@ -7,31 +7,37 @@ from flask import Blueprint, abort, jsonify, request
|
|||
from flask.typing import ResponseReturnValue
|
||||
from sqlalchemy import exc
|
||||
|
||||
from app.api.util import (DOMAIN_NAME_REGEX, MAX_ALLOWED_ITEMS,
|
||||
MAX_DOMAIN_NAME_LENGTH, ListFilter,
|
||||
get_single_resource, list_resources,
|
||||
validate_description)
|
||||
from app.api.util import (
|
||||
DOMAIN_NAME_REGEX,
|
||||
MAX_ALLOWED_ITEMS,
|
||||
MAX_DOMAIN_NAME_LENGTH,
|
||||
ListFilter,
|
||||
get_single_resource,
|
||||
list_resources,
|
||||
validate_description,
|
||||
)
|
||||
from app.extensions import db
|
||||
from app.models.base import Group
|
||||
from app.models.onions import Onion
|
||||
from app.util.onion import decode_onion_keys, onion_hostname
|
||||
from app.util.x509 import validate_tls_keys
|
||||
|
||||
api_onion = Blueprint('api_onion', __name__)
|
||||
api_onion = Blueprint("api_onion", __name__)
|
||||
|
||||
|
||||
@api_onion.route('/onion', methods=['GET'])
|
||||
@api_onion.route("/onion", methods=["GET"])
|
||||
def list_onions() -> ResponseReturnValue:
|
||||
domain_name_filter = request.args.get('DomainName')
|
||||
group_id_filter = request.args.get('GroupId')
|
||||
domain_name_filter = request.args.get("DomainName")
|
||||
group_id_filter = request.args.get("GroupId")
|
||||
|
||||
filters: List[ListFilter] = [
|
||||
(Onion.destroyed.is_(None))
|
||||
]
|
||||
filters: List[ListFilter] = [(Onion.destroyed.is_(None))]
|
||||
|
||||
if domain_name_filter:
|
||||
if len(domain_name_filter) > MAX_DOMAIN_NAME_LENGTH:
|
||||
abort(400, description=f"DomainName cannot exceed {MAX_DOMAIN_NAME_LENGTH} characters.")
|
||||
abort(
|
||||
400,
|
||||
description=f"DomainName cannot exceed {MAX_DOMAIN_NAME_LENGTH} characters.",
|
||||
)
|
||||
if not DOMAIN_NAME_REGEX.match(domain_name_filter):
|
||||
abort(400, description="DomainName contains invalid characters.")
|
||||
filters.append(Onion.domain_name.ilike(f"%{domain_name_filter}%"))
|
||||
|
@ -46,9 +52,9 @@ def list_onions() -> ResponseReturnValue:
|
|||
Onion,
|
||||
lambda onion: onion.to_dict(),
|
||||
filters=filters,
|
||||
resource_name='OnionsList',
|
||||
resource_name="OnionsList",
|
||||
max_allowed_items=MAX_ALLOWED_ITEMS,
|
||||
protective_marking='amber',
|
||||
protective_marking="amber",
|
||||
)
|
||||
|
||||
|
||||
|
@ -71,13 +77,26 @@ def create_onion() -> ResponseReturnValue:
|
|||
abort(400)
|
||||
|
||||
errors = []
|
||||
for field in ["DomainName", "Description", "OnionPrivateKey", "OnionPublicKey", "GroupId", "TlsPrivateKey",
|
||||
"TlsCertificate"]:
|
||||
for field in [
|
||||
"DomainName",
|
||||
"Description",
|
||||
"OnionPrivateKey",
|
||||
"OnionPublicKey",
|
||||
"GroupId",
|
||||
"TlsPrivateKey",
|
||||
"TlsCertificate",
|
||||
]:
|
||||
if not data.get(field):
|
||||
errors.append({"Error": f"{field}_missing", "Message": f"Missing required field: {field}"})
|
||||
errors.append(
|
||||
{
|
||||
"Error": f"{field}_missing",
|
||||
"Message": f"Missing required field: {field}",
|
||||
}
|
||||
)
|
||||
|
||||
onion_private_key, onion_public_key, onion_errors = decode_onion_keys(data["OnionPrivateKey"],
|
||||
data["OnionPublicKey"])
|
||||
onion_private_key, onion_public_key, onion_errors = decode_onion_keys(
|
||||
data["OnionPrivateKey"], data["OnionPublicKey"]
|
||||
)
|
||||
if onion_errors:
|
||||
errors.extend(onion_errors)
|
||||
|
||||
|
@ -85,23 +104,35 @@ def create_onion() -> ResponseReturnValue:
|
|||
return jsonify({"Errors": errors}), 400
|
||||
|
||||
if onion_private_key:
|
||||
existing_onion = db.session.query(Onion).where(
|
||||
Onion.onion_private_key == onion_private_key,
|
||||
Onion.destroyed.is_(None),
|
||||
).first()
|
||||
existing_onion = (
|
||||
db.session.query(Onion)
|
||||
.where(
|
||||
Onion.onion_private_key == onion_private_key,
|
||||
Onion.destroyed.is_(None),
|
||||
)
|
||||
.first()
|
||||
)
|
||||
if existing_onion:
|
||||
errors.append(
|
||||
{"Error": "duplicate_onion_key", "Message": "An onion service with this private key already exists."})
|
||||
{
|
||||
"Error": "duplicate_onion_key",
|
||||
"Message": "An onion service with this private key already exists.",
|
||||
}
|
||||
)
|
||||
|
||||
if "GroupId" in data:
|
||||
group = Group.query.get(data["GroupId"])
|
||||
if not group:
|
||||
errors.append({"Error": "group_id_not_found", "Message": "Invalid group ID."})
|
||||
errors.append(
|
||||
{"Error": "group_id_not_found", "Message": "Invalid group ID."}
|
||||
)
|
||||
|
||||
chain, san_list, tls_errors = validate_tls_keys(
|
||||
data["TlsPrivateKey"], data["TlsCertificate"], data.get("SkipChainVerification"),
|
||||
data["TlsPrivateKey"],
|
||||
data["TlsCertificate"],
|
||||
data.get("SkipChainVerification"),
|
||||
data.get("SkipNameVerification"),
|
||||
f"{onion_hostname(onion_public_key)}.onion"
|
||||
f"{onion_hostname(onion_public_key)}.onion",
|
||||
)
|
||||
|
||||
if tls_errors:
|
||||
|
@ -123,15 +154,21 @@ def create_onion() -> ResponseReturnValue:
|
|||
added=datetime.now(timezone.utc),
|
||||
updated=datetime.now(timezone.utc),
|
||||
cert_expiry=cert_expiry_date,
|
||||
cert_sans=",".join(san_list)
|
||||
cert_sans=",".join(san_list),
|
||||
)
|
||||
|
||||
try:
|
||||
db.session.add(onion)
|
||||
db.session.commit()
|
||||
return jsonify({"Message": "Onion service created successfully.", "Id": onion.id}), 201
|
||||
return (
|
||||
jsonify({"Message": "Onion service created successfully.", "Id": onion.id}),
|
||||
201,
|
||||
)
|
||||
except exc.SQLAlchemyError as e:
|
||||
return jsonify({"Errors": [{"Error": "database_error", "Message": str(e)}]}), 500
|
||||
return (
|
||||
jsonify({"Errors": [{"Error": "database_error", "Message": str(e)}]}),
|
||||
500,
|
||||
)
|
||||
|
||||
|
||||
class UpdateOnionRequest(TypedDict):
|
||||
|
@ -152,8 +189,19 @@ def update_onion(onion_id: int) -> ResponseReturnValue:
|
|||
|
||||
onion = Onion.query.get(onion_id)
|
||||
if not onion:
|
||||
return jsonify(
|
||||
{"Errors": [{"Error": "onion_not_found", "Message": f"No Onion service found with ID {onion_id}"}]}), 404
|
||||
return (
|
||||
jsonify(
|
||||
{
|
||||
"Errors": [
|
||||
{
|
||||
"Error": "onion_not_found",
|
||||
"Message": f"No Onion service found with ID {onion_id}",
|
||||
}
|
||||
]
|
||||
}
|
||||
),
|
||||
404,
|
||||
)
|
||||
|
||||
if "Description" in data:
|
||||
description = data["Description"]
|
||||
|
@ -161,7 +209,12 @@ def update_onion(onion_id: int) -> ResponseReturnValue:
|
|||
if validate_description(description):
|
||||
onion.description = description
|
||||
else:
|
||||
errors.append({"Error": "description_error", "Message": "Description field is invalid"})
|
||||
errors.append(
|
||||
{
|
||||
"Error": "description_error",
|
||||
"Message": "Description field is invalid",
|
||||
}
|
||||
)
|
||||
|
||||
tls_private_key_pem: Optional[str] = None
|
||||
tls_certificate_pem: Optional[str] = None
|
||||
|
@ -176,7 +229,9 @@ def update_onion(onion_id: int) -> ResponseReturnValue:
|
|||
tls_private_key_pem = onion.tls_private_key.decode("utf-8")
|
||||
|
||||
chain, san_list, tls_errors = validate_tls_keys(
|
||||
tls_private_key_pem, tls_certificate_pem, data.get("SkipChainVerification", False),
|
||||
tls_private_key_pem,
|
||||
tls_certificate_pem,
|
||||
data.get("SkipChainVerification", False),
|
||||
data.get("SkipNameVerification", False),
|
||||
f"{onion_hostname(onion.onion_public_key)}.onion",
|
||||
)
|
||||
|
@ -200,7 +255,10 @@ def update_onion(onion_id: int) -> ResponseReturnValue:
|
|||
db.session.commit()
|
||||
return jsonify({"Message": "Onion service updated successfully."}), 200
|
||||
except exc.SQLAlchemyError as e:
|
||||
return jsonify({"Errors": [{"Error": "database_error", "Message": str(e)}]}), 500
|
||||
return (
|
||||
jsonify({"Errors": [{"Error": "database_error", "Message": str(e)}]}),
|
||||
500,
|
||||
)
|
||||
|
||||
|
||||
@api_onion.route("/onion/<int:onion_id>", methods=["GET"])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue