From 38b57e8f8bee896a24dfa754fa992ad92f9330bb Mon Sep 17 00:00:00 2001 From: Cara Salter Date: Fri, 2 Dec 2022 20:10:47 -0500 Subject: manage/peer: ENROLL PEERS HELL YEAH --- app/__init__.py | 2 -- app/manage/__init__.py | 49 ++++++++++++++++++++++++++++++-- app/static/gen/style.css | 67 +++++++++++++++++++++++++++++++++++--------- app/static/scss/style.scss | 25 +++++++++++++++++ app/templates/enroll_id.html | 11 ++++++++ 5 files changed, 135 insertions(+), 19 deletions(-) create mode 100644 app/templates/enroll_id.html diff --git a/app/__init__.py b/app/__init__.py index 37c992e..f213cc6 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -38,6 +38,4 @@ def create_app(): app.register_blueprint(meta.bp) app.register_blueprint(manage.bp) - print(app.url_map) - return app diff --git a/app/manage/__init__.py b/app/manage/__init__.py index afa9343..b590803 100644 --- a/app/manage/__init__.py +++ b/app/manage/__init__.py @@ -1,12 +1,13 @@ -from flask import Blueprint, render_template, request, flash, redirect, url_for +from flask import Blueprint, abort, render_template, request, flash, redirect, url_for from flask_login import login_required, current_user import ulid import flask +import ipaddress from datetime import datetime, timedelta from app import db -from app.database import EnrollRequest, Network +from app.database import EnrollRequest, Network, Peer from .forms import NewNetworkForm @@ -68,6 +69,7 @@ exactly it goes that it's fine. """ @bp.route("/enroll_start", methods=["GET"]) +@login_required def enroll_start(): """ this function only creates a new enrollment request and gives the ID back to @@ -97,4 +99,45 @@ def enroll_start(): db.session.add(en_req) db.session.commit() - return en_req.to_json() + return render_template("enroll_id.html", id=en_req.id) + +@bp.route("/enroll_end", methods=["POST"]) +def finish_enroll(): + """ + The thought here is that the client would POST the enroll key in the last + route and a public key, and this creates the Peer struct before returning it + as JSON for the client to use + + We should take in a network ID argument so we know what CIDR to put this IP + address in. + """ + json = request.get_json() + + network = Network.query.filter_by(id=str(json['network_id'])).first() + if network is None: + abort(404) + + network = ipaddress.IPv4Network(network.subnet) + + en_req = EnrollRequest.query.filter_by(id=str(json['enroll_id'])).first() + if en_req is None: + abort(404) + + peers = db.session.execute(db.select(Peer)).scalars() + + in_network = filter(lambda p: + ipaddress.IPv4Network(p.addr).subnet_of(network), peers) + for ip in network.hosts(): + if ip in in_network: + continue + else: + # This is an IP that we can use! + peer = Peer(id=str(ulid.ulid()),addr=str(ip), description=json['hostname'], public_key=json['public_key'], owner_id=en_req.user) + db.session.add(peer) + db.session.commit() + + db.session.delete(en_req) + + return {"ip": peer.addr, "id": peer.id} + + abort(400) diff --git a/app/static/gen/style.css b/app/static/gen/style.css index 5159ee1..199802d 100644 --- a/app/static/gen/style.css +++ b/app/static/gen/style.css @@ -1,14 +1,17 @@ body { background: #282828; color: #ebdbb2; - font-family: monospace; } + font-family: monospace; +} a a:active, a:visited { - color: #458588; } + color: #458588; +} .container { margin: auto; - width: 60%; } + width: 60%; +} button, input[type=submit] { @@ -16,15 +19,18 @@ input[type=submit] { background-color: #458588; border-color: #458588; border: none; - margin: 0.5rem; } + margin: 0.5rem; +} button.accent { background-color: #d79921; - border-color: #d79921; } + border-color: #d79921; +} h1, h2, h3, h4, h5, h6 { border-bottom: 1px solid; - width: 50%; } + width: 50%; +} .navbar { list-style-type: none; @@ -33,16 +39,19 @@ h1, h2, h3, h4, h5, h6 { border-bottom: 1px solid; margin-bottom: 2rem; padding-bottom: 0.4rem; - text-align: center; } + text-align: center; +} .navbar-item { display: inline; - margin-right: 1rem; } + margin-right: 1rem; +} .flashes { list-style-type: none; display: flex; - justify-content: center; } + justify-content: center; +} .message { width: 80%; @@ -51,21 +60,51 @@ h1, h2, h3, h4, h5, h6 { background-color: #d79921; padding: 0.2rem; font-size: large; - color: black; } + color: black; +} form { - width: 40%; } + width: 40%; +} label, input { margin-bottom: 0.5rem; margin-top: 0.5rem; - display: inline-block; } + display: inline-block; +} label { width: 40%; - text-align: left; } + text-align: left; +} label + input { width: 40%; - margin: 0 30% 0 4%; } + margin: 0 30% 0 4%; +} + +table { + border-collapse: collapse; + border-spacing: 10px; + width: 50%; +} + +table td { + padding-top: 0.5rem; + padding-left: 1.5rem; +} + +td, th { + border-left: 1px solid #ebdbb2; + border-bottom: 1px solid #ebdbb2; + width: 1.5rem; +} + +tr:last-child td { + border-bottom: none; +} + +td:first-child, th:first-child { + border-left: none; +} diff --git a/app/static/scss/style.scss b/app/static/scss/style.scss index 7c8760c..801572d 100644 --- a/app/static/scss/style.scss +++ b/app/static/scss/style.scss @@ -96,3 +96,28 @@ label+input { margin: 0 30% 0 4%; } +// Tables +table { + border-collapse: collapse; + border-spacing: 10px; + width: 50%; + td { + padding-top: 0.5rem; + padding-left: 1.5rem; + } +} + +td, th { + border-left: 1px solid $color-fg; + border-bottom: 1px solid $color-fg; + width: 1.5rem; +} +tr:last-child { + td { + border-bottom: none; + } +} + +td:first-child, th:first-child { + border-left: none; +} diff --git a/app/templates/enroll_id.html b/app/templates/enroll_id.html new file mode 100644 index 0000000..b8ae628 --- /dev/null +++ b/app/templates/enroll_id.html @@ -0,0 +1,11 @@ +{% extends 'base.html' %} + + +{% block content %} +

Enroll New Device

+ +

Your enroll ID is {{ id }}, please go back and paste this +into the client!

+ +

You can now close this page.

+{% endblock %} -- cgit v1.2.3