暫無描述

dim_tasks.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # IRIS Source Code
  2. # Copyright (C) 2021 - Airbus CyberSecurity (SAS)
  3. # ir@cyberactionlab.net
  4. #
  5. # This program is free software; you can redistribute it and/or
  6. # modify it under the terms of the GNU Lesser General Public
  7. # License as published by the Free Software Foundation; either
  8. # version 3 of the License, or (at your option) any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. # Lesser General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU Lesser General Public License
  16. # along with this program; if not, write to the Free Software Foundation,
  17. # Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. import os
  19. from flask import Blueprint
  20. from flask import redirect
  21. from flask import render_template
  22. from flask import url_for
  23. from flask_wtf import FlaskForm
  24. import app
  25. from app.models.authorization import CaseAccessLevel
  26. from app.models.authorization import Permissions
  27. from app.blueprints.access_controls import ac_case_requires, ac_requires
  28. from app.blueprints.responses import response_error
  29. from iris_interface.IrisInterfaceStatus import IIStatus
  30. dim_tasks_blueprint = Blueprint(
  31. 'dim_tasks',
  32. __name__,
  33. template_folder='templates'
  34. )
  35. basedir = os.path.abspath(os.path.dirname(app.__file__))
  36. @dim_tasks_blueprint.route('/dim/tasks', methods=['GET'])
  37. @ac_requires(Permissions.standard_user)
  38. def dim_index(caseid: int, url_redir):
  39. if url_redir:
  40. return redirect(url_for('dim.dim_index', cid=caseid))
  41. form = FlaskForm()
  42. return render_template('dim_tasks.html', form=form)
  43. @dim_tasks_blueprint.route('/dim/tasks/status/<task_id>', methods=['GET'])
  44. @ac_case_requires(CaseAccessLevel.read_only, CaseAccessLevel.full_access)
  45. def task_status(task_id, caseid, url_redir):
  46. if url_redir:
  47. return response_error("Invalid request")
  48. task = app.celery.AsyncResult(task_id)
  49. try:
  50. tinfo = task.info
  51. except AttributeError:
  52. # Legacy task
  53. task_info = {
  54. 'Danger': 'This task was executed in a previous version of IRIS and the status cannot be read anymore.',
  55. 'Note': 'All the data readable by the current IRIS version is displayed in the table.',
  56. 'Additional information': 'The results of this tasks were stored in a pickled Class which does not exists '
  57. 'anymore in current IRIS version.'
  58. }
  59. return render_template("modal_task_info.html", data=task_info, task_id=task.id)
  60. task_info = {
  61. 'Task ID': task_id,
  62. 'Task finished on': task.date_done,
  63. 'Task state': task.state.lower(),
  64. 'Engine': task.name if task.name else "No engine. Unrecoverable shadow failure"}
  65. task_meta = task._get_task_meta()
  66. if task_meta.get('name') \
  67. and ('task_hook_wrapper' in task_meta.get('name') or 'pipeline_dispatcher' in task_meta.get('name')):
  68. task_info['Module name'] = task_meta.get('kwargs').get('module_name')
  69. task_info['Hook name'] = task_meta.get('kwargs').get('hook_name')
  70. task_info['User'] = task_meta.get('kwargs').get('init_user')
  71. task_info['Case ID'] = task_meta.get('kwargs').get('caseid')
  72. if isinstance(task.info, IIStatus):
  73. success = task.info.is_success()
  74. task_info['Logs'] = task.info.get_logs()
  75. else:
  76. success = None
  77. task_info['User'] = "Shadow Iris"
  78. task_info['Logs'] = ['Task did not returned a valid IIStatus object']
  79. if task_meta.get('traceback'):
  80. task_info['Traceback'] = task.traceback
  81. task_info['Success'] = "Success" if success else "Failure"
  82. return render_template("modal_task_info.html", data=task_info, task_id=task.id)