Sin descripción

manage_analysis_status_routes.py 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. from flask import Blueprint, request
  19. from werkzeug import Response
  20. from app.datamgmt.case.case_assets_db import get_compromise_status_dict
  21. from app.datamgmt.case.case_assets_db import get_case_outcome_status_dict
  22. from app.datamgmt.manage.manage_case_objs import search_analysis_status_by_name
  23. from app.models.models import AnalysisStatus
  24. from app.schema.marshables import AnalysisStatusSchema
  25. from app.blueprints.access_controls import ac_api_requires
  26. from app.blueprints.responses import response_error
  27. from app.blueprints.responses import response_success
  28. manage_analysis_status_rest_blueprint = Blueprint('manage_analysis_status_rest', __name__)
  29. @manage_analysis_status_rest_blueprint.route('/manage/analysis-status/list', methods=['GET'])
  30. @ac_api_requires()
  31. def list_anastatus():
  32. lstatus = AnalysisStatus.query.with_entities(
  33. AnalysisStatus.id,
  34. AnalysisStatus.name
  35. ).all()
  36. data = [row._asdict() for row in lstatus]
  37. return response_success("", data=data)
  38. @manage_analysis_status_rest_blueprint.route('/manage/compromise-status/list', methods=['GET'])
  39. @ac_api_requires()
  40. def list_compr_status():
  41. compro_status = get_compromise_status_dict()
  42. return response_success("", data=compro_status)
  43. @manage_analysis_status_rest_blueprint.route('/manage/outcome-status/list', methods=['GET'])
  44. @ac_api_requires()
  45. def list_outcome_status() -> Response:
  46. """Returns a list of outcome status
  47. Args:
  48. caseid (int): Case ID
  49. Returns:
  50. Response: Flask response object
  51. """
  52. outcome_status = get_case_outcome_status_dict()
  53. return response_success("", data=outcome_status)
  54. @manage_analysis_status_rest_blueprint.route('/manage/analysis-status/<int:cur_id>', methods=['GET'])
  55. @ac_api_requires()
  56. def view_anastatus(cur_id):
  57. lstatus = AnalysisStatus.query.with_entities(
  58. AnalysisStatus.id,
  59. AnalysisStatus.name
  60. ).filter(
  61. AnalysisStatus.id == cur_id
  62. ).first()
  63. if not lstatus:
  64. return response_error(f"Analysis status ID {cur_id} not found")
  65. return response_success("", data=lstatus._asdict())
  66. @manage_analysis_status_rest_blueprint.route('/manage/analysis-status/search', methods=['POST'])
  67. @ac_api_requires()
  68. def search_analysis_status():
  69. if not request.is_json:
  70. return response_error("Invalid request")
  71. analysis_status = request.json.get('analysis_status')
  72. if analysis_status is None:
  73. return response_error("Invalid analysis status. Got None")
  74. exact_match = request.json.get('exact_match', False)
  75. # Search for analysis status with a name that contains the specified search term
  76. analysis_status = search_analysis_status_by_name(analysis_status, exact_match=exact_match)
  77. if not analysis_status:
  78. return response_error("No analysis status found")
  79. # Serialize the analysis status and return them in a JSON response
  80. schema = AnalysisStatusSchema(many=True)
  81. return response_success("", data=schema.dump(analysis_status))
  82. # TODO : Add management of analysis status