diff --git a/__init__.py b/__init__.py index b158b38..df9a207 100644 --- a/__init__.py +++ b/__init__.py @@ -134,5 +134,30 @@ def admin_list_mana(): user_mode=user_mode, sources=sources["data"]) + # Route to monitor & manage running instances + @page_blueprint.route('/admin/panel') + @admins_only + def admin_panel(): + + # Retrieve sourceId + user_mode = get_config("user_mode") + + if user_mode == "users": + query_sql = """select id from users;""" + + elif user_mode == "teams": + query_sql = """select id from teams;""" + + sources = db.session.execute(text(query_sql)).fetchall() + + # retrieve dynamic_ia_c_challenges + challenges = db.session.execute(text("""select id from dynamic_ia_c_challenge;""")) + + print(f"source = {sources}") + print(f" challengee = {challenges}") + + return render_template("chall_manager_panel.html", + sources=sources, + challenges=challenges) app.register_blueprint(page_blueprint) diff --git a/api.py b/api.py index f835257..2104ea6 100644 --- a/api.py +++ b/api.py @@ -63,6 +63,47 @@ def get(): 'message': json.loads(r.text), }} + + @staticmethod + @admins_only + def post(): + # retrieve all instance deployed by chall-manager + cm_api_url = get_config("chall-manager:chall-manager_api_url") + + ## mandatory + challengeId = request.args.get("challengeId") + sourceId = request.args.get("sourceId") + + payload = {} + + if not challengeId or not sourceId: + return {'success': False, 'data':{ + 'message': "Missing argument : challengeId or sourceId", + }} + + # TODO check user inputs + + url = f"{cm_api_url}/instance" + + payload['sourceId'] = sourceId + payload['challengeId'] = challengeId + + headers = { + "Content-type": "application/json" + } + + try: + r = requests.post(url, data = json.dumps(payload), headers=headers) + except requests.exceptions.RequestException as e : + return {'success': False, 'data':{ + 'message': f"An error occured while Plugins communication with Challmanager API : {e}", + }} + + + return {'success': True, 'data': { + 'message': json.loads(r.text), + }} + @staticmethod @admins_only def patch(): diff --git a/assets/instances.js b/assets/instances.js index 3edaad2..befb3bf 100644 --- a/assets/instances.js +++ b/assets/instances.js @@ -26,6 +26,19 @@ async function renew_instance(challengeId, sourceId) { return response; } +async function create_instance(challengeId, sourceId) { + let response = await CTFd.fetch("/api/v1/plugins/ctfd-chall-manager/admin/instance?challengeId=" + challengeId + "&sourceId=" + sourceId, { + method: "POST", + credentials: "same-origin", + headers: { + Accept: "application/json", + "Content-Type": "application/json" + } + }); + console.log(response) + response = await response.json(); + return response; +} $(".delete-instance").click(function (e) { e.preventDefault(); @@ -117,3 +130,30 @@ $('#instances-renew-button').click(function (e) { }); }); +$('#instances-create-button').click(function (e) { + let sourceId = $("input[data-source-id]:checked").map(function () { + return $(this).data("source-id"); + }); + + let challengeId = $("input[data-challenge-id]:checked").map(function () { + return $(this).data("challenge-id"); + }); + + let challengeIds = challengeId.toArray() + let sourceIds = sourceId.toArray() + + CTFd.ui.ezq.ezQuery({ + title: "Create instances", + body: `Are you sure you want to create the selected ${sourceId.length} instance(s)?`, + success: async function () { + for (let i=0; i< sourceId.length; i++){ + for (let j=0; j< challengeId.length; j++){ + console.log(challengeIds[j], sourceIds[i]) + create_instance(challengeIds[j], sourceIds[i]) // don't await and let CM deploys instances in // + } + } + location.reload(); + } + }); +}); + diff --git a/templates/chall_manager_config.html b/templates/chall_manager_config.html index 5f3f722..d0835a6 100644 --- a/templates/chall_manager_config.html +++ b/templates/chall_manager_config.html @@ -10,6 +10,9 @@
+
+
+
+ |
+ Source ID + |
---|---|
+
+
+
+ |
+
+
+
+ {% if user_mode == "teams" %}
+
+ {{ source.id }}
+
+ {% else %}
+
+ {{ source.id }}
+
+ {% endif %}
+
+ |
+ {% endfor %}
+
+
+
+
+
+ |
+ ID + |
---|---|
+
+
+
+ |
+
+ + + | + {% endfor %} + +