2023-05-20 17:49:51 -04:00
|
|
|
import logging
|
|
|
|
import json
|
|
|
|
from jsonschema import validate
|
|
|
|
from jsonschema.exceptions import ValidationError
|
|
|
|
from app.classes.models.server_permissions import EnumPermissionsServer
|
|
|
|
from app.classes.web.base_api_handler import BaseApiHandler
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
backup_patch_schema = {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2024-05-25 22:10:35 -04:00
|
|
|
"backup_name": {"type": "string", "minLength": 3},
|
2024-05-25 16:33:28 -04:00
|
|
|
"backup_location": {"type": "string", "minLength": 1},
|
2023-07-12 18:01:14 -04:00
|
|
|
"max_backups": {"type": "integer"},
|
2023-05-20 17:49:51 -04:00
|
|
|
"compress": {"type": "boolean"},
|
|
|
|
"shutdown": {"type": "boolean"},
|
2024-05-25 16:33:28 -04:00
|
|
|
"before": {"type": "string"},
|
|
|
|
"after": {"type": "string"},
|
2024-05-25 22:10:35 -04:00
|
|
|
"excluded_dirs": {"type": "array"},
|
2023-05-20 17:49:51 -04:00
|
|
|
},
|
|
|
|
"additionalProperties": False,
|
|
|
|
"minProperties": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
basic_backup_patch_schema = {
|
|
|
|
"type": "object",
|
|
|
|
"properties": {
|
2024-05-25 22:10:35 -04:00
|
|
|
"backup_name": {"type": "string", "minLength": 3},
|
2023-07-12 18:01:14 -04:00
|
|
|
"max_backups": {"type": "integer"},
|
2023-05-20 17:49:51 -04:00
|
|
|
"compress": {"type": "boolean"},
|
|
|
|
"shutdown": {"type": "boolean"},
|
2024-05-25 16:33:28 -04:00
|
|
|
"before": {"type": "string"},
|
|
|
|
"after": {"type": "string"},
|
2024-05-25 22:10:35 -04:00
|
|
|
"excluded_dirs": {"type": "array"},
|
2023-05-20 17:49:51 -04:00
|
|
|
},
|
|
|
|
"additionalProperties": False,
|
|
|
|
"minProperties": 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ApiServersServerBackupsIndexHandler(BaseApiHandler):
|
|
|
|
def get(self, server_id: str):
|
|
|
|
auth_data = self.authenticate_user()
|
|
|
|
if not auth_data:
|
|
|
|
return
|
2024-04-06 13:42:41 -04:00
|
|
|
mask = self.controller.server_perms.get_lowest_api_perm_mask(
|
|
|
|
self.controller.server_perms.get_user_permissions_mask(
|
2023-05-20 17:49:51 -04:00
|
|
|
auth_data[4]["user_id"], server_id
|
2024-04-06 13:42:41 -04:00
|
|
|
),
|
|
|
|
auth_data[5],
|
|
|
|
)
|
|
|
|
server_permissions = self.controller.server_perms.get_permissions(mask)
|
|
|
|
if EnumPermissionsServer.BACKUP not in server_permissions:
|
2023-05-20 17:49:51 -04:00
|
|
|
# if the user doesn't have Schedule permission, return an error
|
|
|
|
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
|
2024-05-25 16:33:28 -04:00
|
|
|
self.finish_json(
|
|
|
|
200, self.controller.management.get_backups_by_server(server_id)
|
|
|
|
)
|
2023-05-20 17:49:51 -04:00
|
|
|
|
2024-05-25 16:33:28 -04:00
|
|
|
def post(self, server_id: str):
|
2023-05-20 17:49:51 -04:00
|
|
|
auth_data = self.authenticate_user()
|
|
|
|
if not auth_data:
|
|
|
|
return
|
|
|
|
|
|
|
|
try:
|
|
|
|
data = json.loads(self.request.body)
|
|
|
|
except json.decoder.JSONDecodeError as e:
|
|
|
|
return self.finish_json(
|
|
|
|
400, {"status": "error", "error": "INVALID_JSON", "error_data": str(e)}
|
|
|
|
)
|
|
|
|
|
|
|
|
try:
|
2023-07-12 18:01:14 -04:00
|
|
|
if auth_data[4]["superuser"]:
|
|
|
|
validate(data, backup_patch_schema)
|
|
|
|
else:
|
|
|
|
validate(data, basic_backup_patch_schema)
|
2023-05-20 17:49:51 -04:00
|
|
|
except ValidationError as e:
|
|
|
|
return self.finish_json(
|
|
|
|
400,
|
|
|
|
{
|
|
|
|
"status": "error",
|
|
|
|
"error": "INVALID_JSON_SCHEMA",
|
|
|
|
"error_data": str(e),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
if server_id not in [str(x["server_id"]) for x in auth_data[0]]:
|
|
|
|
# if the user doesn't have access to the server, return an error
|
|
|
|
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
|
2024-04-06 13:42:41 -04:00
|
|
|
mask = self.controller.server_perms.get_lowest_api_perm_mask(
|
|
|
|
self.controller.server_perms.get_user_permissions_mask(
|
2023-05-20 17:49:51 -04:00
|
|
|
auth_data[4]["user_id"], server_id
|
2024-04-06 13:42:41 -04:00
|
|
|
),
|
|
|
|
auth_data[5],
|
|
|
|
)
|
|
|
|
server_permissions = self.controller.server_perms.get_permissions(mask)
|
|
|
|
if EnumPermissionsServer.BACKUP not in server_permissions:
|
2023-05-20 17:49:51 -04:00
|
|
|
# if the user doesn't have Schedule permission, return an error
|
|
|
|
return self.finish_json(400, {"status": "error", "error": "NOT_AUTHORIZED"})
|
2024-05-25 22:10:35 -04:00
|
|
|
data["server_id"] = server_id
|
|
|
|
if not data.get("excluded_dirs", None):
|
|
|
|
data["excluded_dirs"] = []
|
|
|
|
self.controller.management.add_backup_config(data)
|
2023-07-12 18:01:14 -04:00
|
|
|
return self.finish_json(200, {"status": "ok"})
|