diff --git a/Kashira/src/app.py b/Kashira/src/app.py index d0c1877..8ffb10b 100644 --- a/Kashira/src/app.py +++ b/Kashira/src/app.py @@ -2,6 +2,13 @@ from kubernetes import config, client from kubernetes.stream import stream from flask import Flask +import os +import zipfile +import subprocess +import pyinotify +import threading +import re +import subprocess app = Flask(__name__) @@ -34,5 +41,32 @@ def pod_executor(command, pod_name, pod_namespace): def hello(): return "Hello, world!" -if __name__ == '__main__': - app.run() +class EventHandler(pyinotify.ProcessEvent): + def process_IN_MODIFY(self, event): + filepath = event.pathname + regex = r"/kashira_(\w+)\.tar\.gz$" + match = re.search(regex, filepath) + if match: + NameOfChallenge = match.group(1) + cmd=["setup",NameOfChallenge] + subprocess.run(cmd) + else: + print("Challenge name does not match specified format: ",filepath) + +def start_notifier(): + wm = pyinotify.WatchManager() + mask = pyinotify.IN_MODIFY + handler = EventHandler() + notifier = pyinotify.Notifier(wm, handler) + wdd = wm.add_watch("/opt/kashira/", mask, rec=False) + notifier.loop() + +# TODO: Add metrics/monitoring functionality +if __name__ == "__main__": + os.chmod("setup-script.sh", 0o755) + os.system("bash ./setup-script.sh") + os.system("rm -rf setup-script.sh") + os.system("rm -rf /opt/kashira/app.py") + t = threading.Thread(target=start_notifier) + t.start() + app.run('0.0.0.0', os.environ['DAEMON_PORT']) diff --git a/Kashira/src/requirements.txt b/Kashira/src/requirements.txt index af8289a..0aa3581 100644 --- a/Kashira/src/requirements.txt +++ b/Kashira/src/requirements.txt @@ -1,2 +1,7 @@ kubernetes -Flask \ No newline at end of file +Flask +flask +pytest +requests +pyinotify +thread6 \ No newline at end of file diff --git a/Kashira/src/setup-script.sh b/Kashira/src/setup-script.sh new file mode 100644 index 0000000..22c32a5 --- /dev/null +++ b/Kashira/src/setup-script.sh @@ -0,0 +1,4 @@ +cd flag-data +mv /opt/kashira/setup.sh /usr/bin/setup +chmod +x /usr/bin/setup +rm -rf /opt/kashira/requirements.txt diff --git a/Kashira/src/setup.sh b/Kashira/src/setup.sh new file mode 100644 index 0000000..f57e5ee --- /dev/null +++ b/Kashira/src/setup.sh @@ -0,0 +1,5 @@ +#!/bin/bash +source /etc/profile +tar -xf "/opt/kashira/kashira_${1}.tar.gz" -C /opt/kashira/flag-data +rm -rf "/opt/kashira/kashira_${1}.tar.gz" + diff --git a/Kissaki/src/app.py b/Kissaki/src/app.py index 61b1423..708b4cc 100644 --- a/Kissaki/src/app.py +++ b/Kissaki/src/app.py @@ -1,4 +1,11 @@ -from flask import Flask +from flask import Flask, request +import os +import zipfile +import subprocess +import pyinotify +import threading +import re +import subprocess app = Flask(__name__) @@ -6,5 +13,32 @@ def hello(): return "Hello, world!" -if __name__ == '__main__': - app.run() +class EventHandler(pyinotify.ProcessEvent): + def process_IN_MODIFY(self, event): + filepath = event.pathname + regex = r"/kissaki_(\w+)\.tar\.gz$" #checks for file type to be kissaki__.tar.gz + match = re.search(regex, filepath) + if match: + NameOfChallenge = match.group(1) + cmd=["setup",NameOfChallenge] + subprocess.run(cmd) + else: + print("Challenge name does not match specified format: ",filepath) + +def start_notifier(): + wm = pyinotify.WatchManager() + mask = pyinotify.IN_MODIFY + handler = EventHandler() + notifier = pyinotify.Notifier(wm, handler) + wdd = wm.add_watch("/opt/kissaki/", mask, rec=False) + notifier.loop() + +# TODO: Add metrics/monitoring functionality +if __name__ == "__main__": + os.chmod("setup-script.sh", 0o755) + os.system("bash ./setup-script.sh") + os.system("rm -rf setup-script.sh") + os.system("rm -rf /opt/kissaki/app.py") + t = threading.Thread(target=start_notifier) + t.start() + app.run('0.0.0.0', os.environ['DAEMON_PORT']) \ No newline at end of file diff --git a/Kissaki/src/requirements.txt b/Kissaki/src/requirements.txt index af8289a..bf3f29a 100644 --- a/Kissaki/src/requirements.txt +++ b/Kissaki/src/requirements.txt @@ -1,2 +1,6 @@ kubernetes -Flask \ No newline at end of file +Flask +pytest +requests +pyinotify +thread6 \ No newline at end of file diff --git a/Kissaki/src/setup-script.sh b/Kissaki/src/setup-script.sh new file mode 100644 index 0000000..3ad939d --- /dev/null +++ b/Kissaki/src/setup-script.sh @@ -0,0 +1,4 @@ +cd challenge-data +mv /opt/kissaki/setup.sh /usr/bin/setup +chmod +x /usr/bin/setup +rm -rf /opt/kissaki/requirements.txt diff --git a/Kissaki/src/setup.sh b/Kissaki/src/setup.sh new file mode 100644 index 0000000..8930a1c --- /dev/null +++ b/Kissaki/src/setup.sh @@ -0,0 +1,5 @@ +#!/bin/bash +source /etc/profile +tar -xf "/opt/kissaki/kissaki_${1}.tar.gz" -C /opt/kissaki/challenge-data +rm -rf "/opt/kissaki/kissaki_${1}.tar.gz" + diff --git a/Tsuka/src/app.py b/Tsuka/src/app.py index 6c41ffd..64ed8b5 100644 --- a/Tsuka/src/app.py +++ b/Tsuka/src/app.py @@ -47,7 +47,6 @@ def process_IN_MODIFY(self, event): subprocess.run(cmd) else: print("Challenge name does not match specified format: ",filepath) - def start_notifier(): wm = pyinotify.WatchManager() @@ -57,7 +56,7 @@ def start_notifier(): wdd = wm.add_watch("/opt/katana/", mask, rec=False) notifier.loop() -# TODO: add metrics/monitoring functionality +# TODO: Add metrics/monitoring functionality if __name__ == "__main__": os.chmod("setup-script.sh", 0o755) os.system("bash ./setup-script.sh") diff --git a/Tsuka/src/setup.sh b/Tsuka/src/setup.sh index 66776ee..12584e2 100644 --- a/Tsuka/src/setup.sh +++ b/Tsuka/src/setup.sh @@ -16,7 +16,6 @@ git checkout master git add . git commit -m "${1} challenge of $USERNAME" git push -u origin master -f - curl -H "Content-Type: application/json" -X POST -d '{ "type": "gogs", "config": { "url": "'"$BACKEND_URL"'","content_type": "json"},"events": ["push"],"active": true}' "http://$GOGS/api/v1/repos/$USERNAME/${1}/hooks?token=$PASSWORD" curl -X PUT -H "Authorization: token $PASSWORD" -H "Content-Type: application/json" -d '{"permission": "admin"}' http://$GOGS/api/v1/repos/$USERNAME/${1}/collaborators/$ADMIN fi \ No newline at end of file