Geen omschrijving

iocs.py 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. from flask import Blueprint
  19. from flask import request
  20. from app.blueprints.access_controls import ac_api_requires
  21. from app.blueprints.rest.endpoints import response_api_deleted
  22. from app.blueprints.rest.endpoints import response_api_error
  23. from app.blueprints.rest.endpoints import response_api_not_found
  24. from app.blueprints.rest.endpoints import response_api_success
  25. from app.business.errors import BusinessProcessingError
  26. from app.business.errors import ObjectNotFoundError
  27. from app.business.iocs import iocs_update
  28. from app.business.iocs import iocs_delete
  29. from app.business.iocs import iocs_get
  30. from app.iris_engine.access_control.utils import ac_fast_check_current_user_has_case_access
  31. from app.models.authorization import CaseAccessLevel
  32. from app.schema.marshables import IocSchemaForAPIV2
  33. from app.blueprints.access_controls import ac_api_return_access_denied
  34. iocs_blueprint = Blueprint('iocs_rest_v2',
  35. __name__,
  36. url_prefix='/iocs')
  37. @iocs_blueprint.delete('/<int:identifier>')
  38. @ac_api_requires()
  39. def delete_case_ioc(identifier):
  40. try:
  41. ioc = iocs_get(identifier)
  42. if not ac_fast_check_current_user_has_case_access(ioc.case_id, [CaseAccessLevel.full_access]):
  43. return ac_api_return_access_denied(caseid=ioc.case_id)
  44. iocs_delete(ioc)
  45. return response_api_deleted()
  46. except ObjectNotFoundError:
  47. return response_api_not_found()
  48. except BusinessProcessingError as e:
  49. return response_api_error(e.get_message())
  50. @iocs_blueprint.get('/<int:identifier>')
  51. @ac_api_requires()
  52. def get_case_ioc(identifier):
  53. ioc_schema = IocSchemaForAPIV2()
  54. try:
  55. ioc = iocs_get(identifier)
  56. if not ac_fast_check_current_user_has_case_access(ioc.case_id, [CaseAccessLevel.read_only, CaseAccessLevel.full_access]):
  57. return ac_api_return_access_denied(caseid=ioc.case_id)
  58. return response_api_success(ioc_schema.dump(ioc))
  59. except ObjectNotFoundError:
  60. return response_api_not_found()
  61. @iocs_blueprint.put('/<int:identifier>')
  62. @ac_api_requires()
  63. def update_ioc(identifier):
  64. ioc_schema = IocSchemaForAPIV2()
  65. try:
  66. ioc = iocs_get(identifier)
  67. if not ac_fast_check_current_user_has_case_access(ioc.case_id,
  68. [CaseAccessLevel.full_access]):
  69. return ac_api_return_access_denied(caseid=ioc.case_id)
  70. ioc, _ = iocs_update(ioc, request.get_json())
  71. return response_api_success(ioc_schema.dump(ioc))
  72. except ObjectNotFoundError:
  73. return response_api_not_found()
  74. except BusinessProcessingError as e:
  75. return response_api_error(e.get_message(), data=e.get_data())