-
-
Notifications
You must be signed in to change notification settings - Fork 216
/
Copy pathmanage_srv_settings_routes.py
144 lines (112 loc) · 5.45 KB
/
manage_srv_settings_routes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# IRIS Source Code
# Copyright (C) 2021 - Airbus CyberSecurity (SAS)
# ir@cyberactionlab.net
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 3 of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program; if not, write to the Free Software Foundation,
# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
import marshmallow
from flask import Blueprint
from flask import render_template
from flask import request
from flask import url_for
from flask_wtf import FlaskForm
from werkzeug.utils import redirect
from app import app
from app import celery
from app import db
from app.datamgmt.manage.manage_srv_settings_db import get_alembic_revision
from app.datamgmt.manage.manage_srv_settings_db import get_srv_settings
from app.iris_engine.backup.backup import backup_iris_db
from app.iris_engine.updater.updater import is_updates_available
from app.iris_engine.updater.updater import remove_periodic_update_checks
from app.iris_engine.updater.updater import setup_periodic_update_checks
from app.iris_engine.utils.tracker import track_activity
from app.models.authorization import Permissions
from app.schema.marshables import ServerSettingsSchema
from app.util import ac_api_requires
from app.util import ac_requires
from app.util import response_error
from app.util import response_success
from dictdiffer import diff
manage_srv_settings_blueprint = Blueprint(
'manage_srv_settings_blueprint',
__name__,
template_folder='templates'
)
@manage_srv_settings_blueprint.route('/manage/server/make-update', methods=['GET'])
@ac_requires(Permissions.server_administrator, no_cid_required=True)
def manage_update(caseid, url_redir):
if url_redir:
return redirect(url_for('manage_srv_settings_blueprint.manage_settings', cid=caseid))
# Return default page of case management
return render_template('manage_make_update.html')
@manage_srv_settings_blueprint.route('/manage/server/backups/make-db', methods=['GET'])
@ac_api_requires(Permissions.server_administrator)
def manage_make_db_backup():
has_error, logs = backup_iris_db()
if has_error:
rep = response_error('Backup failed', data=logs)
else:
rep = response_success('Backup done', data=logs)
return rep
@manage_srv_settings_blueprint.route('/manage/server/check-updates/modal', methods=['GET'])
@ac_requires(Permissions.server_administrator, no_cid_required=True)
def manage_check_updates_modal(caseid, url_redir):
if url_redir:
return redirect(url_for('manage_srv_settings_blueprint.manage_settings', cid=caseid))
has_updates, updates_content, _ = is_updates_available()
# Return default page of case management
return render_template('modal_server_updates.html', has_updates=has_updates, updates_content=updates_content)
@manage_srv_settings_blueprint.route('/manage/settings', methods=['GET'])
@ac_requires(Permissions.server_administrator, no_cid_required=True)
def manage_settings(caseid, url_redir):
if url_redir:
return redirect(url_for('manage_srv_settings_blueprint.manage_settings', cid=caseid))
form = FlaskForm()
server_settings = get_srv_settings()
versions = {
"iris_current": app.config.get('IRIS_VERSION'),
"api_min": app.config.get('API_MIN_VERSION'),
"api_current": app.config.get('API_MAX_VERSION'),
"interface_min": app.config.get('MODULES_INTERFACE_MIN_VERSION'),
"interface_current": app.config.get('MODULES_INTERFACE_MAX_VERSION'),
"db_revision": get_alembic_revision()
}
# Return default page of case management
return render_template('manage_srv_settings.html', form=form, settings=server_settings, versions=versions)
@manage_srv_settings_blueprint.route('/manage/settings/update', methods=['POST'])
@ac_api_requires(Permissions.server_administrator)
def manage_update_settings():
if not request.is_json:
return response_error('Invalid request')
srv_settings_schema = ServerSettingsSchema()
server_settings = get_srv_settings()
original_update_check = server_settings.enable_updates_check
try:
original_settings = srv_settings_schema.dump(server_settings)
new_settings = request.get_json()
differences = list(diff(original_settings, new_settings))
changes = [{difference[1]: difference[2]} for difference in differences if difference[0] == 'change']
srv_settings_sc = srv_settings_schema.load(request.get_json(), instance=server_settings)
db.session.commit()
if original_update_check != srv_settings_sc.enable_updates_check:
if srv_settings_sc.enable_updates_check:
setup_periodic_update_checks(celery)
else:
remove_periodic_update_checks()
if srv_settings_sc:
track_activity(f"Server settings updated: {changes}")
return response_success("Server settings updated", srv_settings_sc)
except marshmallow.exceptions.ValidationError as e:
return response_error(msg="Data error", data=e.messages)