Sin descripción

__init__.py 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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, request, Response
  19. from flask_login import current_user
  20. from app.blueprints.access_controls import ac_api_requires
  21. from app.blueprints.rest.endpoints import response_api_success, response_api_error
  22. from app.blueprints.rest.parsing import parse_comma_separated_identifiers
  23. from app.datamgmt.alerts.alerts_db import get_filtered_alerts
  24. from app.models.authorization import Permissions
  25. from app.schema.marshables import AlertSchema
  26. alerts_blueprint = Blueprint('alerts', __name__, url_prefix='/alerts')
  27. @alerts_blueprint.get('')
  28. @ac_api_requires(Permissions.alerts_read)
  29. def alerts_list_route() -> Response:
  30. """
  31. Get a list of alerts from the database
  32. Args:
  33. caseid (str): The case id
  34. returns:
  35. Response: The response
  36. """
  37. page = request.args.get('page', 1, type=int)
  38. per_page = request.args.get('per_page', 10, type=int)
  39. alert_ids_str = request.args.get('alert_ids')
  40. alert_ids = None
  41. if alert_ids_str:
  42. try:
  43. alert_ids = parse_comma_separated_identifiers(alert_ids_str)
  44. except ValueError:
  45. return response_api_error('Invalid alert id')
  46. alert_assets_str = request.args.get('alert_assets')
  47. alert_assets = None
  48. if alert_assets_str:
  49. try:
  50. alert_assets = [str(alert_asset)
  51. for alert_asset in alert_assets_str.split(',')]
  52. except ValueError:
  53. return response_api_error('Invalid alert asset')
  54. alert_iocs_str = request.args.get('alert_iocs')
  55. alert_iocs = None
  56. if alert_iocs_str:
  57. try:
  58. alert_iocs = [str(alert_ioc)
  59. for alert_ioc in alert_iocs_str.split(',')]
  60. except ValueError:
  61. return response_api_error('Invalid alert ioc')
  62. fields_str = request.args.get('fields')
  63. if fields_str:
  64. # Split into a list
  65. fields = [field.strip() for field in fields_str.split(',') if field.strip()]
  66. else:
  67. fields = None
  68. filtered_alerts = get_filtered_alerts(
  69. start_date=request.args.get('creation_start_date'),
  70. end_date=request.args.get('creation_end_date'),
  71. source_start_date=request.args.get('source_start_date'),
  72. source_end_date=request.args.get('source_end_date'),
  73. source_reference=request.args.get('source_reference'),
  74. title=request.args.get('alert_title'),
  75. description=request.args.get('alert_description'),
  76. status=request.args.get('alert_status_id', type=int),
  77. severity=request.args.get('alert_severity_id', type=int),
  78. owner=request.args.get('alert_owner_id', type=int),
  79. source=request.args.get('alert_source'),
  80. tags=request.args.get('alert_tags'),
  81. classification=request.args.get('alert_classification_id', type=int),
  82. client=request.args.get('alert_customer_id'),
  83. case_id=request.args.get('case_id', type=int),
  84. alert_ids=alert_ids,
  85. page=page,
  86. per_page=per_page,
  87. sort=request.args.get('sort'),
  88. custom_conditions=request.args.get('custom_conditions'),
  89. assets=alert_assets,
  90. iocs=alert_iocs,
  91. resolution_status=request.args.get('alert_resolution_id', type=int),
  92. current_user_id=current_user.id
  93. )
  94. if filtered_alerts is None:
  95. return response_api_error('Filtering error')
  96. # If fields are provided, use them in the schema
  97. if fields:
  98. try:
  99. alert_schema = AlertSchema(only=fields)
  100. except Exception as e:
  101. alert_schema = AlertSchema()
  102. else:
  103. alert_schema = AlertSchema()
  104. filtered_data = {
  105. 'total': filtered_alerts.total,
  106. 'data': alert_schema.dump(filtered_alerts, many=True),
  107. 'last_page': filtered_alerts.pages,
  108. 'current_page': filtered_alerts.page,
  109. 'next_page': filtered_alerts.next_num if filtered_alerts.has_next else None,
  110. }
  111. return response_api_success(data=filtered_data)