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

manage_attributes_routes.py 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 import db
  21. from app.datamgmt.manage.manage_attribute_db import update_all_attributes
  22. from app.datamgmt.manage.manage_attribute_db import validate_attribute
  23. from app.models.authorization import Permissions
  24. from app.models.models import CustomAttribute
  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_attributes_rest_blueprint = Blueprint('manage_attributes_rest', __name__)
  29. @manage_attributes_rest_blueprint.route('/manage/attributes/list')
  30. @ac_api_requires(Permissions.server_administrator)
  31. def list_attributes():
  32. # Get all attributes
  33. attributes = CustomAttribute.query.with_entities(
  34. CustomAttribute.attribute_id,
  35. CustomAttribute.attribute_content,
  36. CustomAttribute.attribute_display_name,
  37. CustomAttribute.attribute_description,
  38. CustomAttribute.attribute_for
  39. ).all()
  40. data = [row._asdict() for row in attributes]
  41. # Return the attributes
  42. return response_success("", data=data)
  43. @manage_attributes_rest_blueprint.route('/manage/attributes/update/<int:cur_id>', methods=['POST'])
  44. @ac_api_requires(Permissions.server_administrator)
  45. def update_attribute(cur_id):
  46. if not request.is_json:
  47. return response_error("Invalid request")
  48. attribute = CustomAttribute.query.filter(CustomAttribute.attribute_id == cur_id).first()
  49. if not attribute:
  50. return response_error(f"Invalid Attribute ID {cur_id}")
  51. data = request.get_json()
  52. attr_content = data.get('attribute_content')
  53. if not attr_content:
  54. return response_error("Invalid request")
  55. attr_contents, logs = validate_attribute(attr_content)
  56. if len(logs) > 0:
  57. return response_error("Found errors in attribute", data=logs)
  58. previous_attribute = attribute.attribute_content
  59. attribute.attribute_content = attr_contents
  60. db.session.commit()
  61. # Now try to update every attributes by merging the updated ones
  62. complete_overwrite = data.get('complete_overwrite')
  63. complete_overwrite = complete_overwrite if complete_overwrite else False
  64. partial_overwrite = data.get('partial_overwrite')
  65. partial_overwrite = partial_overwrite if partial_overwrite else False
  66. update_all_attributes(attribute.attribute_for, partial_overwrite=partial_overwrite,
  67. complete_overwrite=complete_overwrite, previous_attribute=previous_attribute)
  68. return response_success("Attribute updated")