From bd647b2eae1676946c5c120266a0f540d10e6e1c Mon Sep 17 00:00:00 2001
From: Martin van Es <martin@mrvanes.com>
Date: Thu, 27 Jan 2022 13:56:07 +0100
Subject: [PATCH] Make proxy domains configurable

---
 README.md            | 5 +++--
 mdproxy.py           | 6 +++---
 mdproxy.yaml.example | 5 +++++
 mdserver.py          | 4 ++--
 utils.py             | 4 ++--
 5 files changed, 15 insertions(+), 9 deletions(-)
 create mode 100644 mdproxy.yaml.example

diff --git a/README.md b/README.md
index 31f01da..694256e 100644
--- a/README.md
+++ b/README.md
@@ -22,13 +22,14 @@ Reloads metadata on inotify CLOSE_WRITE of metadata file.
 Serves and caches signed by domain signer from memory, on request
 
 ## ```mdproxy.py```
+Reads config from mdproxy.yaml configuration, see example.
 Caches signed and cached ```mdserver.py``` metadata requests
 
 ## Queries
 MDQ Queries can then be pointed at
 
-- ```http://mdserver:5001/sign/<entityid>```
-- ```http://mdproxy:5002/cache/<entityid>```
+- ```http://mdserver:5001/<domain>/entities/<entityid>```
+- ```http://mdproxy:5002/<domain>/entities/<entityid>```
 
 ## Bootstrap softHSM2
 This is a very brief summary of the successive commands to initialize softHSM2 for testing. Tested on Ubuntu 21.10.
diff --git a/mdproxy.py b/mdproxy.py
index 2848823..4a33146 100755
--- a/mdproxy.py
+++ b/mdproxy.py
@@ -6,14 +6,14 @@ from urllib.parse import unquote
 from dateutil import parser, tz
 from datetime import datetime
 
-from utils import hasher, Entity
+from utils import read_config, hasher, Entity
 
+config = read_config('mdproxy.yaml')
 app = Flask(__name__)
 
 
 # Find all IdP's in edugain metadata
 cached = {}
-signer_url = 'http://localhost:5001'
 
 
 @app.route('/<domain>/entities/<path:eid>', methods=['GET'])
@@ -35,7 +35,7 @@ def serve(domain, eid):
             return cached[domain][entityID].md
     else:
         print(f"request {entityID}")
-        data = requests.get(f"{signer_url}/{domain}/entities/{{sha1}}{entityID}").text
+        data = requests.get(f"{config[domain]['signer']}/{domain}/entities/{{sha1}}{entityID}").text
         try:
             parsed = ET.fromstring(data)
             validUntil = parsed.get('validUntil')
diff --git a/mdproxy.yaml.example b/mdproxy.yaml.example
new file mode 100644
index 0000000..eef5eda
--- /dev/null
+++ b/mdproxy.yaml.example
@@ -0,0 +1,5 @@
+---
+test:
+    signer: 'http://localhost:5001'
+foobar:
+    signer: 'http://localhost:5001'
diff --git a/mdserver.py b/mdserver.py
index b3dec1a..0ad714b 100755
--- a/mdserver.py
+++ b/mdserver.py
@@ -2,7 +2,7 @@
 from utils import read_config, Resource, Server
 from flask import Flask, Response
 
-config = read_config()
+config = read_config('mdserver.yaml')
 app = Flask(__name__)
 server = Server()
 
@@ -31,4 +31,4 @@ for domain, values in config.items():
 
 
 if __name__ == "__main__":
-    app.run(host='127.0.0.1', port=5001, debug=False)
+    app.run(host='0.0.0.0', port=5001, debug=False)
diff --git a/utils.py b/utils.py
index 860e3dd..fbfa82e 100755
--- a/utils.py
+++ b/utils.py
@@ -13,8 +13,8 @@ from signers import Signers
 # watch_manager = pyinotify.WatchManager()
 
 
-def read_config():
-    with open('mdserver.yaml') as f:
+def read_config(config):
+    with open(config) as f:
         config = yaml.safe_load(f)
     return config
 
-- 
GitLab