aboutsummaryrefslogtreecommitdiff
path: root/app/manage/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/manage/__init__.py')
-rw-r--r--app/manage/__init__.py49
1 files changed, 46 insertions, 3 deletions
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)