summary refs log tree commit diff
diff options
context:
space:
mode:
authorJorik Schellekens <joriks@matrix.org>2019-08-02 19:05:06 +0100
committerJorik Schellekens <joriks@matrix.org>2019-08-28 15:59:53 +0100
commit9280882cb9c2ecbe421e249e9af553d1b5ff4afb (patch)
treeebddc5bc7fd49b585d668d6533f5afa060101689
parentmore UI (diff)
downloadsynapse-9280882cb9c2ecbe421e249e9af553d1b5ff4afb.tar.xz
Cert endpoints.
-rw-r--r--synapse_topology/controller/server/schemas.py18
-rw-r--r--synapse_topology/controller/server/server.py26
-rw-r--r--synapse_topology/model/__init__.py10
3 files changed, 53 insertions, 1 deletions
diff --git a/synapse_topology/controller/server/schemas.py b/synapse_topology/controller/server/schemas.py
index b841b18a84..8420ef62eb 100644
--- a/synapse_topology/controller/server/schemas.py
+++ b/synapse_topology/controller/server/schemas.py
@@ -15,3 +15,21 @@ BASE_CONFIG_SCHEMA = {
     },
     "required": ["server_name", "report_stats"],
 }
+
+CERT_PATHS_SCHEMA = {
+    "type": "object",
+    "properties": {
+        "cert_path": {"type": "string", "minlength": 1},
+        "cert_key_path": {"type": "string", "minlength": 1},
+    },
+    "required": ["cert_path", "cert_key_path"],
+}
+
+CERTS_SCHEMA = {
+    "type": "object",
+    "properties": {
+        "cert": {"type": "string", "minlength": 1},
+        "cert_key": {"type": "string", "minlength": 1},
+    },
+    "required": ["cert", "cert_key"],
+}
diff --git a/synapse_topology/controller/server/server.py b/synapse_topology/controller/server/server.py
index e0f0e10766..bb8af0c553 100644
--- a/synapse_topology/controller/server/server.py
+++ b/synapse_topology/controller/server/server.py
@@ -6,7 +6,12 @@ from synapse_topology import model
 from twisted.web.static import File
 
 from . import error_handlers
-from .schemas import BASE_CONFIG_SCHEMA, SERVERNAME_SCHEMA
+from .schemas import (
+    BASE_CONFIG_SCHEMA,
+    SERVERNAME_SCHEMA,
+    CERT_PATHS_SCHEMA,
+    CERTS_SCHEMA,
+)
 from .utils import validate_schema
 
 from . import app
@@ -61,3 +66,22 @@ with app.subroute("/config") as app:
         @app.route("/config/{}".format(config), methods=["POST"])
         def set_sub_config(request, sub_config):
             model.set_config(json.loads(request.content.read()), sub_config=config)
+
+
+@app.route("/testcertpaths", methods=["POST"])
+@validate_schema(CERT_PATHS_SCHEMA)
+def test_cert_paths(request, body):
+    result = {}
+    for path in ["cert_path", "cert_key_path"]:
+        try:
+            with open(body[path], "r"):
+                result[path + "_invalid"] = False
+        except:
+            result[path + "_invalid"] = True
+    return json.dumps(result)
+
+
+@app.route("/certs", methods=["POST"])
+@validate_schema(CERTS_SCHEMA)
+def upload_certs(request, body):
+    model.add_certs(**body)
diff --git a/synapse_topology/model/__init__.py b/synapse_topology/model/__init__.py
index 8132d9a31b..21a13125cf 100644
--- a/synapse_topology/model/__init__.py
+++ b/synapse_topology/model/__init__.py
@@ -82,3 +82,13 @@ def get_secret_key():
 
 def verify_yaml():
     pass
+
+
+def add_certs(cert, cert_key):
+    with open(
+        path.join(config_dir, get_server_name() + ".tls.crt"), "w"
+    ) as cert_file, open(
+        path.join(config_dir, get_server_name() + ".tls.key"), "w"
+    ) as key_file:
+        cert_file.write(cert)
+        key_file.write(cert_key)