Няма описание

manage_case_classifications_routes.py 6.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 marshmallow
  19. from flask import Blueprint
  20. from flask import Response
  21. from flask import request
  22. from app import db
  23. from app.datamgmt.manage.manage_case_classifications_db import get_case_classifications_list
  24. from app.datamgmt.manage.manage_case_classifications_db import get_case_classification_by_id
  25. from app.datamgmt.manage.manage_case_classifications_db import search_classification_by_name
  26. from app.iris_engine.utils.tracker import track_activity
  27. from app.models.authorization import Permissions
  28. from app.schema.marshables import CaseClassificationSchema
  29. from app.blueprints.access_controls import ac_api_requires
  30. from app.blueprints.responses import response_error
  31. from app.blueprints.responses import response_success
  32. manage_case_classification_rest_blueprint = Blueprint('manage_case_classifications_rest', __name__)
  33. @manage_case_classification_rest_blueprint.route('/manage/case-classifications/list', methods=['GET'])
  34. @ac_api_requires()
  35. def list_case_classifications() -> Response:
  36. """Get the list of case classifications
  37. Args:
  38. caseid (int): case id
  39. Returns:
  40. Flask Response object
  41. """
  42. l_cl = get_case_classifications_list()
  43. return response_success("", data=l_cl)
  44. @manage_case_classification_rest_blueprint.route('/manage/case-classifications/<int:classification_id>', methods=['GET'])
  45. @ac_api_requires()
  46. def get_case_classification(classification_id: int) -> Response:
  47. """Get a case classification
  48. Args:
  49. classification_id (int): case classification id
  50. caseid (int): case id
  51. Returns:
  52. Flask Response object
  53. """
  54. schema = CaseClassificationSchema()
  55. case_classification = get_case_classification_by_id(classification_id)
  56. if not case_classification:
  57. return response_error(f"Invalid case classification ID {classification_id}")
  58. return response_success("", data=schema.dump(case_classification))
  59. @manage_case_classification_rest_blueprint.route('/manage/case-classifications/update/<int:classification_id>',
  60. methods=['POST'])
  61. @ac_api_requires(Permissions.server_administrator)
  62. def update_case_classification(classification_id: int) -> Response:
  63. """Update a case classification
  64. Args:
  65. classification_id (int): case classification id
  66. caseid (int): case id
  67. Returns:
  68. Flask Response object
  69. """
  70. if not request.is_json:
  71. return response_error("Invalid request")
  72. case_classification = get_case_classification_by_id(classification_id)
  73. if not case_classification:
  74. return response_error(f"Invalid case classification ID {classification_id}")
  75. ccl = CaseClassificationSchema()
  76. try:
  77. ccls = ccl.load(request.get_json(), instance=case_classification)
  78. if ccls:
  79. track_activity(f"updated case classification {ccls.id}")
  80. return response_success("Case classification updated", ccl.dump(ccls))
  81. except marshmallow.exceptions.ValidationError as e:
  82. return response_error(msg="Data error", data=e.messages)
  83. return response_error("Unexpected error server-side. Nothing updated", data=case_classification)
  84. @manage_case_classification_rest_blueprint.route('/manage/case-classifications/add', methods=['POST'])
  85. @ac_api_requires(Permissions.server_administrator)
  86. def add_case_classification() -> Response:
  87. """Add a case classification
  88. Returns:
  89. Flask Response object
  90. """
  91. if not request.is_json:
  92. return response_error("Invalid request")
  93. ccl = CaseClassificationSchema()
  94. try:
  95. ccls = ccl.load(request.get_json())
  96. if ccls:
  97. db.session.add(ccls)
  98. db.session.commit()
  99. track_activity(f"added case classification {ccls.name}")
  100. return response_success("Case classification added", ccl.dump(ccls))
  101. except marshmallow.exceptions.ValidationError as e:
  102. return response_error(msg="Data error", data=e.messages)
  103. return response_error("Unexpected error server-side. Nothing added", data=None)
  104. @manage_case_classification_rest_blueprint.route('/manage/case-classifications/delete/<int:classification_id>',
  105. methods=['POST'])
  106. @ac_api_requires(Permissions.server_administrator)
  107. def delete_case_classification(classification_id: int) -> Response:
  108. """Delete a case classification
  109. Args:
  110. classification_id (int): case classification id
  111. caseid (int): case id
  112. Returns:
  113. Flask Response object
  114. """
  115. case_classification = get_case_classification_by_id(classification_id)
  116. if not case_classification:
  117. return response_error(f"Invalid case classification ID {classification_id}")
  118. db.session.delete(case_classification)
  119. db.session.commit()
  120. track_activity(f"deleted case classification {case_classification.name}")
  121. return response_success("Case classification deleted")
  122. @manage_case_classification_rest_blueprint.route('/manage/case-classifications/search', methods=['POST'])
  123. @ac_api_requires()
  124. def search_alert_status():
  125. if not request.is_json:
  126. return response_error("Invalid request")
  127. classification_name = request.json.get('classification_name')
  128. if classification_name is None:
  129. return response_error("Invalid classification name. Got None")
  130. exact_match = request.json.get('exact_match', False)
  131. # Search for classifications with a name that contains the specified search term
  132. classification = search_classification_by_name(classification_name, exact_match=exact_match)
  133. if not classification:
  134. return response_error("No classification found")
  135. # Serialize the case classification and return them in a JSON response
  136. schema = CaseClassificationSchema(many=True)
  137. return response_success("", data=schema.dump(classification))