暫無描述

manage_case_templates_routes.py 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. # IRIS Source Code
  2. # Copyright (C) 2024 - DFIR-IRIS
  3. # contact@dfir-iris.org
  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 json
  19. from flask import Blueprint
  20. from flask import request
  21. from flask_login import current_user
  22. from marshmallow import ValidationError
  23. from app import db
  24. from app.datamgmt.manage.manage_case_templates_db import get_case_templates_list
  25. from app.datamgmt.manage.manage_case_templates_db import get_case_template_by_id
  26. from app.datamgmt.manage.manage_case_templates_db import validate_case_template
  27. from app.datamgmt.manage.manage_case_templates_db import delete_case_template_by_id
  28. from app.models.models import CaseTemplate
  29. from app.models.authorization import Permissions
  30. from app.iris_engine.utils.tracker import track_activity
  31. from app.schema.marshables import CaseTemplateSchema
  32. from app.blueprints.access_controls import ac_requires_case_identifier
  33. from app.blueprints.access_controls import ac_api_requires
  34. from app.blueprints.responses import response_error
  35. from app.blueprints.responses import response_success
  36. manage_case_templates_rest_blueprint = Blueprint('manage_case_templates_rest', __name__)
  37. @manage_case_templates_rest_blueprint.route('/manage/case-templates/list', methods=['GET'])
  38. @ac_api_requires()
  39. def list_case_templates():
  40. """Show a list of case templates
  41. Returns:
  42. Response: List of case templates
  43. """
  44. case_templates = get_case_templates_list()
  45. # Return the attributes
  46. return response_success("", data=case_templates)
  47. @manage_case_templates_rest_blueprint.route('/manage/case-templates/add', methods=['POST'])
  48. @ac_api_requires(Permissions.case_templates_write)
  49. @ac_requires_case_identifier()
  50. def add_case_template(caseid):
  51. data = request.get_json()
  52. if not data:
  53. return response_error("Invalid request")
  54. case_template_json = data.get('case_template_json')
  55. if not case_template_json:
  56. return response_error("Invalid request")
  57. try:
  58. case_template_dict = json.loads(case_template_json)
  59. except Exception as e:
  60. return response_error("Invalid JSON", data=str(e))
  61. try:
  62. logs = validate_case_template(case_template_dict, update=False)
  63. if logs is not None:
  64. return response_error("Found errors in case template", data=logs)
  65. except Exception as e:
  66. return response_error("Found errors in case template", data=str(e))
  67. try:
  68. case_template_dict["created_by_user_id"] = current_user.id
  69. case_template_data = CaseTemplateSchema().load(case_template_dict)
  70. case_template = CaseTemplate(**case_template_data)
  71. db.session.add(case_template)
  72. db.session.commit()
  73. except Exception as e:
  74. return response_error("Could not add case template into DB", data=str(e))
  75. track_activity(f"Case template '{case_template.name}' added", caseid=caseid, ctx_less=True)
  76. return response_success("Added successfully", data=CaseTemplateSchema().dump(case_template))
  77. @manage_case_templates_rest_blueprint.route('/manage/case-templates/update/<int:cur_id>', methods=['POST'])
  78. @ac_api_requires(Permissions.case_templates_write)
  79. def update_case_template(cur_id):
  80. if not request.is_json:
  81. return response_error("Invalid request")
  82. case_template = get_case_template_by_id(cur_id)
  83. if not case_template:
  84. return response_error(f"Invalid Case template ID {cur_id}")
  85. data = request.get_json()
  86. updated_case_template_json = data.get('case_template_json')
  87. if not updated_case_template_json:
  88. return response_error("Invalid request")
  89. try:
  90. updated_case_template_dict = json.loads(updated_case_template_json)
  91. except Exception as e:
  92. return response_error("Invalid JSON", data=str(e))
  93. try:
  94. logs = validate_case_template(updated_case_template_dict, update=True)
  95. if logs is not None:
  96. return response_error("Found errors in case template", data=logs)
  97. except Exception as e:
  98. return response_error("Found errors in case template", data=str(e))
  99. case_template_schema = CaseTemplateSchema()
  100. try:
  101. # validate the request data and load it into an instance of the `CaseTemplate` object
  102. case_template_data = case_template_schema.load(updated_case_template_dict, partial=True)
  103. # update the existing `case_template` object with the new data
  104. case_template.update_from_dict(case_template_data)
  105. # commit the changes to the database
  106. db.session.commit()
  107. except ValidationError as error:
  108. return response_error("Could not validate case template", data=str(error))
  109. return response_success("Case template updated")
  110. @manage_case_templates_rest_blueprint.route('/manage/case-templates/delete/<int:case_template_id>', methods=['POST'])
  111. @ac_api_requires(Permissions.case_templates_write)
  112. @ac_requires_case_identifier()
  113. def delete_case_template(case_template_id, caseid):
  114. case_template = get_case_template_by_id(case_template_id)
  115. if case_template is None:
  116. return response_error('Case template not found')
  117. case_template_name = case_template.name
  118. delete_case_template_by_id(case_template_id)
  119. db.session.commit()
  120. track_activity(f"Case template '{case_template_name}' deleted", caseid=caseid, ctx_less=True)
  121. return response_success("Deleted successfully")