Brak opisu

manage_server_settings_routes.py 3.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 request
  21. from app import app
  22. from app import celery
  23. from app import db
  24. from app.datamgmt.manage.manage_srv_settings_db import get_srv_settings
  25. from app.iris_engine.backup.backup import backup_iris_db
  26. from app.iris_engine.updater.updater import remove_periodic_update_checks
  27. from app.iris_engine.updater.updater import setup_periodic_update_checks
  28. from app.iris_engine.utils.tracker import track_activity
  29. from app.models.authorization import Permissions
  30. from app.schema.marshables import ServerSettingsSchema
  31. from app.blueprints.access_controls import ac_api_requires
  32. from app.blueprints.responses import response_error
  33. from app.blueprints.responses import response_success
  34. from dictdiffer import diff
  35. manage_server_settings_rest_blueprint = Blueprint('manage_server_settings_rest', __name__)
  36. @manage_server_settings_rest_blueprint.route('/manage/server/backups/make-db', methods=['GET'])
  37. @ac_api_requires(Permissions.server_administrator)
  38. def manage_make_db_backup():
  39. has_error, logs = backup_iris_db()
  40. if has_error:
  41. rep = response_error('Backup failed', data=logs)
  42. else:
  43. rep = response_success('Backup done', data=logs)
  44. return rep
  45. @manage_server_settings_rest_blueprint.route('/manage/settings/update', methods=['POST'])
  46. @ac_api_requires(Permissions.server_administrator)
  47. def manage_update_settings():
  48. if not request.is_json:
  49. return response_error('Invalid request')
  50. srv_settings_schema = ServerSettingsSchema()
  51. server_settings = get_srv_settings()
  52. original_update_check = server_settings.enable_updates_check
  53. try:
  54. original_settings = srv_settings_schema.dump(server_settings)
  55. new_settings = request.get_json()
  56. differences = list(diff(original_settings, new_settings))
  57. changes = [{difference[1]: difference[2]} for difference in differences if difference[0] == 'change']
  58. srv_settings_sc = srv_settings_schema.load(request.get_json(), instance=server_settings)
  59. db.session.commit()
  60. if original_update_check != srv_settings_sc.enable_updates_check:
  61. if srv_settings_sc.enable_updates_check:
  62. setup_periodic_update_checks(celery)
  63. else:
  64. remove_periodic_update_checks()
  65. if srv_settings_sc:
  66. track_activity(f"Server settings updated: {changes}")
  67. app.config['SERVER_SETTINGS'] = srv_settings_schema.dump(server_settings)
  68. return response_success("Server settings updated", app.config['SERVER_SETTINGS'])
  69. except marshmallow.exceptions.ValidationError as e:
  70. return response_error(msg="Data error", data=e.messages)