diff options
| author | Jan Tuomi <jan.tuomi@eficode.com> | 2018-12-18 19:36:29 +0200 |
|---|---|---|
| committer | Jan Tuomi <jan.tuomi@eficode.com> | 2018-12-18 19:36:29 +0200 |
| commit | 39a842833aa87cd4f2c497127293720cb6a5222d (patch) | |
| tree | f8086a0387780a35c8d0d9b5229f4c735c5984ca /remote-https-runner | |
| parent | 2cdf86d8c613b1cd27449c09ef1063f29c98d6d0 (diff) | |
Restructure directory
Diffstat (limited to 'remote-https-runner')
| -rw-r--r-- | remote-https-runner/Dockerfile | 12 | ||||
| -rw-r--r-- | remote-https-runner/__init__.py | 0 | ||||
| -rw-r--r-- | remote-https-runner/config.yaml | 3 | ||||
| -rwxr-xr-x | remote-https-runner/remote-https-runner (renamed from remote-https-runner) | 0 | ||||
| -rw-r--r-- | remote-https-runner/requirements.txt | 8 | ||||
| -rw-r--r-- | remote-https-runner/runner.py | 78 |
6 files changed, 101 insertions, 0 deletions
diff --git a/remote-https-runner/Dockerfile b/remote-https-runner/Dockerfile new file mode 100644 index 0000000..2d01eac --- /dev/null +++ b/remote-https-runner/Dockerfile @@ -0,0 +1,12 @@ +FROM python + +RUN python --version + +ARG configfile +ADD requirements.txt . +RUN pip install -r requirements.txt +ADD ${configfile} remote-https-runner *.py ./ +ENV FLASK_APP=runner.py +EXPOSE 5000 + +CMD ["./remote-https-runner"]
\ No newline at end of file diff --git a/remote-https-runner/__init__.py b/remote-https-runner/__init__.py new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/remote-https-runner/__init__.py diff --git a/remote-https-runner/config.yaml b/remote-https-runner/config.yaml new file mode 100644 index 0000000..f148545 --- /dev/null +++ b/remote-https-runner/config.yaml @@ -0,0 +1,3 @@ +example: + auth: "super secret password" + command: "echo ${MESSAGE} | tee output.txt" diff --git a/remote-https-runner b/remote-https-runner/remote-https-runner index 5821986..5821986 100755 --- a/remote-https-runner +++ b/remote-https-runner/remote-https-runner diff --git a/remote-https-runner/requirements.txt b/remote-https-runner/requirements.txt new file mode 100644 index 0000000..0703955 --- /dev/null +++ b/remote-https-runner/requirements.txt @@ -0,0 +1,8 @@ +Click==7.0 +Flask==1.0.2 +gunicorn==19.9.0 +itsdangerous==1.1.0 +Jinja2==2.10 +MarkupSafe==1.1.0 +PyYAML==3.13 +Werkzeug==0.14.1 diff --git a/remote-https-runner/runner.py b/remote-https-runner/runner.py new file mode 100644 index 0000000..c550555 --- /dev/null +++ b/remote-https-runner/runner.py @@ -0,0 +1,78 @@ +import os +import sys +import subprocess +import time +from flask import Flask, Response, request, jsonify +app = Flask(__name__) + +import logging +logging.basicConfig(format='%(asctime)s %(levelname)s - %(message)s', level=logging.INFO) + +from yaml import load, dump +if "CONFIG_PATH" in os.environ: + filename = os.environ["CONFIG_PATH"] +else: + filename = "config.yaml" + +try: + with open(filename) as f: + text = f.read() + config = load(text) +except: + print("Could not open file {}".format(filename)) + sys.exit(1) + +config_keys = config.keys() + +@app.route("/<key>", methods=["POST"]) +def key(key): + logging.info("User requested route \"{}\"".format(key)) + if not key in config_keys: + logging.warning("Route \"{}\" does not exist!".format(key)) + return Response("", status=404) + + options = config[key] + auth_header = request.headers.get('Authorization') + protocol = request.url.split("://")[0] + logging.info("User uses protocol {}".format(protocol)) + #if protocol != "https": + # return Response("HTTP requests not allowed! Use HTTPS!", status=400) + + auth = options["auth"] + if auth != auth_header: + logging.warning("User request rejected due to incorrect Authorization header!") + return Response("", status=401) + + if request.content_type != "application/json": + logging.warning("User request rejected due to incorrect content type (must be application/json)!") + return Response("", status=401) + + content = request.json + + cmd = options["command"] + for content_key in content: + cmd = cmd.replace("${" + str(content_key) + "}", content[content_key]) + + logging.info("User runs command:") + logging.info(cmd) + + start = time.time() + output = subprocess.check_output(cmd, shell=True) + end = time.time() + + output = output.decode("utf-8") if type(output) == bytes else output + duration = "{:.2f}".format(end - start) + + logging.info("Command output:") + logging.info(output) + logging.info("Command duration: {}".format(duration)) + + return jsonify({ + "command": cmd, + "output": str(output), + "duration": duration + }) + +@app.route("/") +def hello(): + return "Welcome to remote HTTPS runner API!" |
