From 5334f30edf7d139f2b4b25c339d9c212d2b805bc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?F=C3=A9lix=20Aime?= Date: Wed, 17 Feb 2021 20:16:39 +0100 Subject: [PATCH] Adding GUI update feature (server-side) --- install.sh | 5 +++ server/frontend/app/blueprints/update.py | 19 ++++++++ server/frontend/app/classes/update.py | 56 ++++++++++++++++++++++++ server/frontend/main.py | 2 + update.sh | 15 ++++--- 5 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 server/frontend/app/blueprints/update.py create mode 100644 server/frontend/app/classes/update.py diff --git a/install.sh b/install.sh index 5db0208..41c123a 100644 --- a/install.sh +++ b/install.sh @@ -104,6 +104,10 @@ create_directory() { cp -Rf ./* /usr/share/tinycheck } +get_version() { + git tag | tail -n 1 > /usr/share/tinycheck/VERSION +} + generate_certificate() { # Generating SSL certificate for the backend. echo -e "[+] Generating SSL certificate for the backend" @@ -444,6 +448,7 @@ else check_operating_system check_interfaces create_directory + get_version set_userlang set_credentials set_kioskmode diff --git a/server/frontend/app/blueprints/update.py b/server/frontend/app/blueprints/update.py new file mode 100644 index 0000000..242c1e9 --- /dev/null +++ b/server/frontend/app/blueprints/update.py @@ -0,0 +1,19 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from flask import jsonify, Blueprint +from app.classes.update import Update + +update_bp = Blueprint("update", __name__) + + +@update_bp.route("/check", methods=["GET"]) +def check(): + """ Check the presence of new version """ + return jsonify(Update().check_version()) + + +@update_bp.route("/process", methods=["GET"]) +def process(): + """ Check the presence of new version """ + return jsonify(Update().update_instance()) diff --git a/server/frontend/app/classes/update.py b/server/frontend/app/classes/update.py new file mode 100644 index 0000000..8b79e94 --- /dev/null +++ b/server/frontend/app/classes/update.py @@ -0,0 +1,56 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +from app.utils import read_config +import subprocess as sp +import requests +import json +import os +import re + + +class Update(object): + + def __init__(self): + self.project_url = "https://api.github.com/repos/KasperskyLab/TinyCheck/tags" + self.app_path = "/usr/share/tinycheck" + return None + + def check_version(self): + """ + Check if a new version of TinyCheck is available + by quering the Github api and comparing the last + tag with the VERSION file content. + """ + if read_config(("frontend", "update")): + res = requests.get(self.project_url) + res = json.loads(res.content.decode("utf8")) + + with open(os.path.join(self.app_path, "VERSION")) as f: + if f.read() != res[0]["name"]: + return {"status": True, + "message": "A new version is available"} + else: + return {"status": True, + "message": "This is the latest version"} + else: + return {"status": False, + "message": "You don't have rights to do this operation."} + + def update_instance(self): + """ + Update the instance by executing + the update script. + """ + if read_config(("frontend", "update")): + try: + os.chdir(self.app_path) + sp.Popen(["bash", os.path.join(self.app_path, "update.sh")]) + return {"status": True, + "message": "Update successfully launched"} + except: + return {"status": False, + "message": "Issue during the update"} + else: + return {"status": False, + "message": "You don't have rights to do this operation."} diff --git a/server/frontend/main.py b/server/frontend/main.py index b15bfe7..76d2240 100644 --- a/server/frontend/main.py +++ b/server/frontend/main.py @@ -8,6 +8,7 @@ from app.blueprints.device import device_bp from app.blueprints.analysis import analysis_bp from app.blueprints.save import save_bp from app.blueprints.misc import misc_bp +from app.blueprints.update import update_bp from app.utils import read_config app = Flask(__name__, template_folder="../../app/frontend/dist") @@ -42,6 +43,7 @@ app.register_blueprint(device_bp, url_prefix='/api/device') app.register_blueprint(analysis_bp, url_prefix='/api/analysis') app.register_blueprint(save_bp, url_prefix='/api/save') app.register_blueprint(misc_bp, url_prefix='/api/misc') +app.register_blueprint(update_bp, url_prefix='/api/update') if __name__ == '__main__': if read_config(("frontend", "remote_access")): diff --git a/update.sh b/update.sh index db9b728..96ec877 100644 --- a/update.sh +++ b/update.sh @@ -40,12 +40,7 @@ elif [ $PWD = "/tmp/tinycheck" ]; then cd /usr/share/tinycheck/app/frontend/ && npm install && npm run build cd /usr/share/tinycheck/app/backend/ && npm install && npm run build - echo "[+] Restarting services" - service tinycheck-backend restart - service tinycheck-frontend restart - service tinycheck-watchers restart - - # Updating configuration with new values. + echo "[+] Updating current configuration with new values." if ! grep -q reboot_option /usr/share/tinycheck/config.yaml; then sed -i 's/frontend:/frontend:\n reboot_option: true/g' /usr/share/tinycheck/config.yaml fi @@ -70,5 +65,13 @@ elif [ $PWD = "/tmp/tinycheck" ]; then sed -i "s/free_issuers:/free_issuers:\n - CN=R3,O=Let's Encrypt,C=US/g" /usr/share/tinycheck/config.yaml fi + echo "[+] Restarting services" + service tinycheck-backend restart + service tinycheck-frontend restart + service tinycheck-watchers restart + + echo "[+] Updating the TinyCheck version" + cd /tmp/tinycheck && git tag | tail -n 1 > /usr/share/tinycheck/VERSION + echo "[+] TinyCheck updated!" fi \ No newline at end of file