Add a scoped web-node service for website files
This commit is contained in:
1 parent
651ee0fbe1
commit
d54e3924ac
7 files changed
+508
-10
No files matched your search
@@ -0,0 +1,14 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Coulobre website file operation socket
|
||||||
|
|
||||||
|
[Socket]
|
||||||
|
ListenStream=/run/coulobre-website-files.sock
|
||||||
|
SocketUser=root
|
||||||
|
SocketGroup=coulobre-file-requests
|
||||||
|
SocketMode=0660
|
||||||
|
Accept=yes
|
||||||
|
MaxConnections=16
|
||||||
|
RemoveOnStop=true
|
||||||
|
|
||||||
|
[Install]
|
||||||
|
WantedBy=sockets.target
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
[Unit]
|
||||||
|
Description=Coulobre website file operation
|
||||||
|
CollectMode=inactive-or-failed
|
||||||
|
|
||||||
|
[Service]
|
||||||
|
Type=exec
|
||||||
|
User=root
|
||||||
|
WorkingDirectory=/opt/coulobre
|
||||||
|
UMask=0027
|
||||||
|
ExecStart=/opt/coulobre/bin/coulobre-web-node files-request
|
||||||
|
StandardInput=socket
|
||||||
|
StandardOutput=socket
|
||||||
|
StandardError=journal
|
||||||
|
NoNewPrivileges=true
|
||||||
|
CapabilityBoundingSet=CAP_SYS_CHROOT CAP_SETUID CAP_SETGID CAP_DAC_READ_SEARCH
|
||||||
|
PrivateTmp=true
|
||||||
|
PrivateDevices=true
|
||||||
|
ProtectSystem=strict
|
||||||
|
ProtectHome=true
|
||||||
|
ReadWritePaths=/srv/coulobre/web
|
||||||
|
RestrictAddressFamilies=AF_UNIX
|
||||||
|
RuntimeMaxSec=2min
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
import hashlib
|
import hashlib
|
||||||
import json
|
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
import socket
|
import socket
|
||||||
@@ -10,6 +9,7 @@ from shutil import copyfileobj
|
|||||||
from tempfile import TemporaryDirectory
|
from tempfile import TemporaryDirectory
|
||||||
from uuid import UUID
|
from uuid import UUID
|
||||||
|
|
||||||
|
from . import node_stream
|
||||||
from .mail_node import MailboxSpec
|
from .mail_node import MailboxSpec
|
||||||
|
|
||||||
BACKUP_RELAY = "/run/coulobre-mail-archive-backup.sock"
|
BACKUP_RELAY = "/run/coulobre-mail-archive-backup.sock"
|
||||||
@@ -89,19 +89,16 @@ def valid_size(size):
|
|||||||
|
|
||||||
|
|
||||||
def read_header(source):
|
def read_header(source):
|
||||||
body = source.readline(HEADER_LIMIT + 1)
|
|
||||||
if not body.endswith(b"\n") or len(body) > HEADER_LIMIT:
|
|
||||||
raise MailArchiveConnectionError("Incomplete mailbox archive header.")
|
|
||||||
try:
|
try:
|
||||||
return json.loads(body.decode("utf-8"))
|
return node_stream.read_header(
|
||||||
except (ValueError, RecursionError):
|
source, maximum=HEADER_LIMIT, label="mailbox archive"
|
||||||
raise MailArchiveConnectionError(
|
)
|
||||||
"Invalid mailbox archive header."
|
except ValueError as error:
|
||||||
) from None
|
raise MailArchiveConnectionError(str(error)) from None
|
||||||
|
|
||||||
|
|
||||||
def encode_header(fields):
|
def encode_header(fields):
|
||||||
return json.dumps(fields, separators=(",", ":")).encode("utf-8") + b"\n"
|
return node_stream.encode_header(fields)
|
||||||
|
|
||||||
|
|
||||||
def receive_archive(source, archive, size, *, digest=None):
|
def receive_archive(source, archive, size, *, digest=None):
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
import json
|
||||||
|
|
||||||
|
STREAM_CHUNK_SIZE = 1024 * 1024
|
||||||
|
|
||||||
|
|
||||||
|
def encode_header(fields):
|
||||||
|
return json.dumps(fields, separators=(",", ":")).encode("utf-8") + b"\n"
|
||||||
|
|
||||||
|
|
||||||
|
def read_header(source, *, maximum, label):
|
||||||
|
body = source.readline(maximum + 1)
|
||||||
|
if len(body) > maximum or not body.endswith(b"\n"):
|
||||||
|
raise ValueError(f"Incomplete {label} header.")
|
||||||
|
try:
|
||||||
|
return json.loads(body.decode("utf-8"))
|
||||||
|
except (ValueError, RecursionError):
|
||||||
|
raise ValueError(f"Invalid {label} header.") from None
|
||||||
@@ -494,10 +494,18 @@ def main(argv=None):
|
|||||||
restore_site.add_argument("archive", type=Path)
|
restore_site.add_argument("archive", type=Path)
|
||||||
restore_site.add_argument("--file", help="Restore one document-root path.")
|
restore_site.add_argument("--file", help="Restore one document-root path.")
|
||||||
restore_site.add_argument("--replace", action="store_true")
|
restore_site.add_argument("--replace", action="store_true")
|
||||||
|
commands.add_parser(
|
||||||
|
"files-request", help="Handle a scoped website file operation."
|
||||||
|
)
|
||||||
args = parser.parse_args(argv)
|
args = parser.parse_args(argv)
|
||||||
if args.command == "restore-site" and args.replace and args.file is None:
|
if args.command == "restore-site" and args.replace and args.file is None:
|
||||||
parser.error("--replace requires --file.")
|
parser.error("--replace requires --file.")
|
||||||
|
|
||||||
|
if args.command == "files-request":
|
||||||
|
from .web_node_files import serve_website_files
|
||||||
|
|
||||||
|
return serve_website_files()
|
||||||
|
|
||||||
if args.command == "apply-request":
|
if args.command == "apply-request":
|
||||||
return serve_apply_request(
|
return serve_apply_request(
|
||||||
MANIFEST_VERSION,
|
MANIFEST_VERSION,
|
||||||
|
|||||||
@@ -0,0 +1,326 @@
|
|||||||
|
import ctypes
|
||||||
|
import errno
|
||||||
|
import fcntl
|
||||||
|
import heapq
|
||||||
|
import os
|
||||||
|
import signal
|
||||||
|
import stat
|
||||||
|
import sys
|
||||||
|
from contextlib import contextmanager, suppress
|
||||||
|
from uuid import uuid4
|
||||||
|
|
||||||
|
from . import web_node_apply as web
|
||||||
|
from .node_stream import STREAM_CHUNK_SIZE, encode_header, read_header
|
||||||
|
from .web_node_access import website_suspension_path
|
||||||
|
from .website_files import (
|
||||||
|
LIST_PAGE_SIZE,
|
||||||
|
REQUEST_HEADER_LIMIT,
|
||||||
|
TEMPORARY_PREFIX,
|
||||||
|
WebsiteFileRequest,
|
||||||
|
directory_parts,
|
||||||
|
validate_entry_name,
|
||||||
|
)
|
||||||
|
|
||||||
|
OPEN_DIRECTORY = os.O_RDONLY | os.O_DIRECTORY | os.O_NOFOLLOW
|
||||||
|
RENAME_NOREPLACE = 1
|
||||||
|
PROBLEMS = {
|
||||||
|
errno.EDQUOT: "The website storage quota has been reached.",
|
||||||
|
errno.ENOSPC: "The web node has no space for this upload.",
|
||||||
|
errno.EEXIST: "A file or folder with that name already exists.",
|
||||||
|
errno.ENOENT: "The selected file or folder is no longer available.",
|
||||||
|
errno.ENOTEMPTY: "Empty the folder before removing it.",
|
||||||
|
errno.ELOOP: "The path must point to a regular website file or folder.",
|
||||||
|
errno.ENOTDIR: "The selected folder is unavailable.",
|
||||||
|
errno.EACCES: "The website user cannot access this entry.",
|
||||||
|
errno.EPERM: "The website user cannot change this entry.",
|
||||||
|
}
|
||||||
|
|
||||||
|
# Resolve libc before entering the website filesystem.
|
||||||
|
renameat2 = getattr(ctypes.CDLL(None, use_errno=True), "renameat2", None)
|
||||||
|
if renameat2 is not None:
|
||||||
|
renameat2.argtypes = (
|
||||||
|
ctypes.c_int,
|
||||||
|
ctypes.c_char_p,
|
||||||
|
ctypes.c_int,
|
||||||
|
ctypes.c_char_p,
|
||||||
|
ctypes.c_uint,
|
||||||
|
)
|
||||||
|
renameat2.restype = ctypes.c_int
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def website_user(site):
|
||||||
|
if os.geteuid() != 0:
|
||||||
|
raise ValueError("Run the website files receiver as root.")
|
||||||
|
with web.locked_web_node(fcntl.LOCK_SH):
|
||||||
|
if os.path.lexists(website_suspension_path(site)):
|
||||||
|
raise ValueError("This website is suspended.")
|
||||||
|
try:
|
||||||
|
public, uid, web_gid = web.site_storage(site)
|
||||||
|
account = web.current_user(site.service_user)
|
||||||
|
if account is None or account.pw_uid != uid:
|
||||||
|
raise ValueError
|
||||||
|
except (OSError, ValueError):
|
||||||
|
raise ValueError(
|
||||||
|
"This website is unavailable on the web node."
|
||||||
|
) from None
|
||||||
|
file_descriptor = os.open(public, OPEN_DIRECTORY)
|
||||||
|
try:
|
||||||
|
root = os.fstat(file_descriptor)
|
||||||
|
if uid == 0 or (root.st_uid, root.st_gid) != (uid, web_gid):
|
||||||
|
raise ValueError(
|
||||||
|
"This website is unavailable on the web node."
|
||||||
|
)
|
||||||
|
os.fchdir(file_descriptor)
|
||||||
|
os.chroot(".")
|
||||||
|
os.chdir("/")
|
||||||
|
os.setgroups([web_gid])
|
||||||
|
os.setresgid(account.pw_gid, account.pw_gid, account.pw_gid)
|
||||||
|
os.setresuid(uid, uid, uid)
|
||||||
|
finally:
|
||||||
|
os.close(file_descriptor)
|
||||||
|
yield uid, web_gid, root.st_dev
|
||||||
|
|
||||||
|
|
||||||
|
def permitted(info, uid, device):
|
||||||
|
return (
|
||||||
|
info.st_uid == uid
|
||||||
|
and info.st_dev == device
|
||||||
|
and (
|
||||||
|
stat.S_ISDIR(info.st_mode)
|
||||||
|
or (stat.S_ISREG(info.st_mode) and info.st_nlink == 1)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
@contextmanager
|
||||||
|
def open_folder(path, uid, device):
|
||||||
|
folder = os.open("/", OPEN_DIRECTORY)
|
||||||
|
try:
|
||||||
|
for part in directory_parts(path):
|
||||||
|
child = os.open(part, OPEN_DIRECTORY, dir_fd=folder)
|
||||||
|
os.close(folder)
|
||||||
|
folder = child
|
||||||
|
if not permitted(os.fstat(folder), uid, device):
|
||||||
|
raise ValueError("The selected folder is unavailable.")
|
||||||
|
yield folder
|
||||||
|
finally:
|
||||||
|
os.close(folder)
|
||||||
|
|
||||||
|
|
||||||
|
def entry_info(folder, name, uid, device):
|
||||||
|
info = os.stat(name, dir_fd=folder, follow_symlinks=False)
|
||||||
|
if not permitted(info, uid, device):
|
||||||
|
raise ValueError("The selected website entry is unavailable.")
|
||||||
|
return info
|
||||||
|
|
||||||
|
|
||||||
|
def list_folder(folder, after, uid, device):
|
||||||
|
def candidates(entries):
|
||||||
|
for entry in entries:
|
||||||
|
if entry.name <= after:
|
||||||
|
continue
|
||||||
|
try:
|
||||||
|
validate_entry_name(entry.name)
|
||||||
|
info = entry.stat(follow_symlinks=False)
|
||||||
|
except (FileNotFoundError, ValueError):
|
||||||
|
continue
|
||||||
|
if permitted(info, uid, device):
|
||||||
|
yield entry.name, stat.S_ISDIR(info.st_mode), info.st_size
|
||||||
|
|
||||||
|
with os.scandir(folder) as entries:
|
||||||
|
items = heapq.nsmallest(LIST_PAGE_SIZE + 1, candidates(entries))
|
||||||
|
page = [
|
||||||
|
{
|
||||||
|
"name": name,
|
||||||
|
"is_directory": directory,
|
||||||
|
"size": 0 if directory else size,
|
||||||
|
}
|
||||||
|
for name, directory, size in items[:LIST_PAGE_SIZE]
|
||||||
|
]
|
||||||
|
after = page[-1]["name"] if len(items) > LIST_PAGE_SIZE else ""
|
||||||
|
return {"entries": page, "after": after}
|
||||||
|
|
||||||
|
|
||||||
|
def rename_entry(folder, source, target):
|
||||||
|
if renameat2 is None:
|
||||||
|
raise ValueError(
|
||||||
|
"This web node cannot rename files without replacing them."
|
||||||
|
)
|
||||||
|
result = renameat2(
|
||||||
|
folder,
|
||||||
|
os.fsencode(source),
|
||||||
|
folder,
|
||||||
|
os.fsencode(target),
|
||||||
|
RENAME_NOREPLACE,
|
||||||
|
)
|
||||||
|
if result:
|
||||||
|
error = ctypes.get_errno()
|
||||||
|
raise OSError(error, os.strerror(error))
|
||||||
|
|
||||||
|
|
||||||
|
def upload_file(folder, name, size, source, web_gid):
|
||||||
|
staged = TEMPORARY_PREFIX + uuid4().hex
|
||||||
|
descriptor = os.open(
|
||||||
|
staged,
|
||||||
|
os.O_WRONLY | os.O_CREAT | os.O_EXCL | os.O_NOFOLLOW,
|
||||||
|
0o600,
|
||||||
|
dir_fd=folder,
|
||||||
|
)
|
||||||
|
try:
|
||||||
|
with os.fdopen(descriptor, "wb") as output:
|
||||||
|
remaining = size
|
||||||
|
while remaining:
|
||||||
|
chunk = source.read(min(STREAM_CHUNK_SIZE, remaining))
|
||||||
|
if not chunk:
|
||||||
|
raise ValueError(
|
||||||
|
"The upload ended before the file was complete."
|
||||||
|
)
|
||||||
|
output.write(chunk)
|
||||||
|
remaining -= len(chunk)
|
||||||
|
if source.read(1):
|
||||||
|
raise ValueError("The upload exceeded its declared file size.")
|
||||||
|
output.flush()
|
||||||
|
os.fchown(output.fileno(), -1, web_gid)
|
||||||
|
os.fchmod(output.fileno(), 0o640)
|
||||||
|
os.fsync(output.fileno())
|
||||||
|
rename_entry(folder, staged, name)
|
||||||
|
os.fsync(folder)
|
||||||
|
finally:
|
||||||
|
with suppress(FileNotFoundError):
|
||||||
|
os.unlink(staged, dir_fd=folder)
|
||||||
|
|
||||||
|
|
||||||
|
def open_download(folder, name, uid, device):
|
||||||
|
if not stat.S_ISREG(entry_info(folder, name, uid, device).st_mode):
|
||||||
|
raise ValueError("Choose a file to download.")
|
||||||
|
descriptor = os.open(
|
||||||
|
name, os.O_RDONLY | os.O_NOFOLLOW | os.O_NONBLOCK, dir_fd=folder
|
||||||
|
)
|
||||||
|
source = os.fdopen(descriptor, "rb")
|
||||||
|
info = os.fstat(source.fileno())
|
||||||
|
if not stat.S_ISREG(info.st_mode) or not permitted(info, uid, device):
|
||||||
|
source.close()
|
||||||
|
raise ValueError(
|
||||||
|
"The file changed before download. Refresh the folder."
|
||||||
|
)
|
||||||
|
return source, info.st_size
|
||||||
|
|
||||||
|
|
||||||
|
def create_directory(folder, name, web_gid):
|
||||||
|
staged = TEMPORARY_PREFIX + uuid4().hex
|
||||||
|
os.mkdir(staged, 0o700, dir_fd=folder)
|
||||||
|
try:
|
||||||
|
descriptor = os.open(staged, OPEN_DIRECTORY, dir_fd=folder)
|
||||||
|
try:
|
||||||
|
os.fchown(descriptor, -1, web_gid)
|
||||||
|
os.fchmod(descriptor, 0o750)
|
||||||
|
finally:
|
||||||
|
os.close(descriptor)
|
||||||
|
rename_entry(folder, staged, name)
|
||||||
|
finally:
|
||||||
|
with suppress(FileNotFoundError):
|
||||||
|
os.rmdir(staged, dir_fd=folder)
|
||||||
|
|
||||||
|
|
||||||
|
def change_entry(request, folder, uid, web_gid, device):
|
||||||
|
if request.operation == "mkdir":
|
||||||
|
create_directory(folder, request.name, web_gid)
|
||||||
|
else:
|
||||||
|
info = entry_info(folder, request.name, uid, device)
|
||||||
|
if request.operation == "rename":
|
||||||
|
rename_entry(folder, request.name, request.target)
|
||||||
|
elif stat.S_ISDIR(info.st_mode):
|
||||||
|
os.rmdir(request.name, dir_fd=folder)
|
||||||
|
else:
|
||||||
|
os.unlink(request.name, dir_fd=folder)
|
||||||
|
os.fsync(folder)
|
||||||
|
|
||||||
|
|
||||||
|
def reply(output, identity, state, **data):
|
||||||
|
output.write(
|
||||||
|
encode_header(
|
||||||
|
{
|
||||||
|
"version": 1,
|
||||||
|
"identity": identity,
|
||||||
|
"state": state,
|
||||||
|
**data,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
output.flush()
|
||||||
|
|
||||||
|
|
||||||
|
def cancel_upload(signum, frame):
|
||||||
|
raise InterruptedError("The file request expired.")
|
||||||
|
|
||||||
|
|
||||||
|
def discard_upload(source, size):
|
||||||
|
while size:
|
||||||
|
chunk = source.read1(min(STREAM_CHUNK_SIZE, size))
|
||||||
|
if not chunk:
|
||||||
|
return
|
||||||
|
size -= len(chunk)
|
||||||
|
|
||||||
|
|
||||||
|
def serve_website_files():
|
||||||
|
source, output = sys.stdin.buffer, sys.stdout.buffer
|
||||||
|
request = None
|
||||||
|
identity = ""
|
||||||
|
started_download = False
|
||||||
|
signal.signal(signal.SIGTERM, cancel_upload)
|
||||||
|
try:
|
||||||
|
request = WebsiteFileRequest.from_manifest(
|
||||||
|
read_header(
|
||||||
|
source,
|
||||||
|
maximum=REQUEST_HEADER_LIMIT,
|
||||||
|
label="website file request",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
identity = request.identity()
|
||||||
|
with (
|
||||||
|
website_user(request.site) as (uid, web_gid, device),
|
||||||
|
open_folder(request.directory, uid, device) as folder,
|
||||||
|
):
|
||||||
|
if request.operation == "download":
|
||||||
|
file, remaining = open_download(
|
||||||
|
folder, request.name, uid, device
|
||||||
|
)
|
||||||
|
with file:
|
||||||
|
reply(output, identity, "ready", size=remaining)
|
||||||
|
started_download = True
|
||||||
|
while remaining:
|
||||||
|
chunk = file.read(min(STREAM_CHUNK_SIZE, remaining))
|
||||||
|
if not chunk:
|
||||||
|
return 1
|
||||||
|
output.write(chunk)
|
||||||
|
remaining -= len(chunk)
|
||||||
|
output.flush()
|
||||||
|
return 0
|
||||||
|
if request.operation == "list":
|
||||||
|
result = list_folder(folder, request.after, uid, device)
|
||||||
|
else:
|
||||||
|
if request.operation == "upload":
|
||||||
|
upload_file(
|
||||||
|
folder, request.name, request.size, source, web_gid
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
change_entry(request, folder, uid, web_gid, device)
|
||||||
|
result = {}
|
||||||
|
reply(output, identity, "ready", **result)
|
||||||
|
return 0
|
||||||
|
except (OSError, ValueError) as error:
|
||||||
|
if not started_download:
|
||||||
|
problem = (
|
||||||
|
str(error)
|
||||||
|
if isinstance(error, ValueError)
|
||||||
|
else PROBLEMS.get(
|
||||||
|
error.errno,
|
||||||
|
"The web node could not complete this file operation.",
|
||||||
|
)
|
||||||
|
)
|
||||||
|
with suppress(OSError):
|
||||||
|
reply(output, identity, "failed", problem=problem)
|
||||||
|
if request is not None and request.operation == "upload":
|
||||||
|
discard_upload(source, request.size)
|
||||||
|
return 1
|
||||||
@@ -0,0 +1,114 @@
|
|||||||
|
import hashlib
|
||||||
|
from dataclasses import dataclass
|
||||||
|
|
||||||
|
from .node_stream import encode_header
|
||||||
|
from .web_node import WebSite
|
||||||
|
|
||||||
|
FILE_SOCKET = "/run/coulobre-website-files.sock"
|
||||||
|
UPLOAD_LIMIT = 64 * 1024 * 1024
|
||||||
|
REQUEST_HEADER_LIMIT = 32 * 1024
|
||||||
|
RESPONSE_HEADER_LIMIT = 192 * 1024
|
||||||
|
LIST_PAGE_SIZE = 100
|
||||||
|
TEMPORARY_PREFIX = ".coulobre-upload-"
|
||||||
|
OPERATIONS = {"list", "upload", "download", "rename", "delete", "mkdir"}
|
||||||
|
|
||||||
|
|
||||||
|
def validate_entry_name(name):
|
||||||
|
if (
|
||||||
|
not isinstance(name, str)
|
||||||
|
or not name
|
||||||
|
or not name.isprintable()
|
||||||
|
or name in {".", ".."}
|
||||||
|
or "/" in name
|
||||||
|
or "\\" in name
|
||||||
|
or name.startswith(TEMPORARY_PREFIX)
|
||||||
|
or len(name.encode("utf-8")) > 255
|
||||||
|
):
|
||||||
|
raise ValueError("Enter a valid file or folder name.")
|
||||||
|
|
||||||
|
|
||||||
|
def directory_parts(path):
|
||||||
|
if not isinstance(path, str) or (path and not path.isprintable()):
|
||||||
|
raise ValueError("Choose a folder within the website document root.")
|
||||||
|
if len(path.encode("utf-8")) > 4096:
|
||||||
|
raise ValueError("The folder path is too long.")
|
||||||
|
parts = tuple(path.split("/")) if path else ()
|
||||||
|
for part in parts:
|
||||||
|
validate_entry_name(part)
|
||||||
|
return parts
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass(frozen=True)
|
||||||
|
class WebsiteFileRequest:
|
||||||
|
site: WebSite
|
||||||
|
operation: str
|
||||||
|
directory: str = ""
|
||||||
|
name: str = ""
|
||||||
|
target: str = ""
|
||||||
|
size: int | None = None
|
||||||
|
after: str = ""
|
||||||
|
|
||||||
|
def __post_init__(self):
|
||||||
|
if (
|
||||||
|
not isinstance(self.operation, str)
|
||||||
|
or self.operation not in OPERATIONS
|
||||||
|
):
|
||||||
|
raise ValueError("Choose a supported website file operation.")
|
||||||
|
directory_parts(self.directory)
|
||||||
|
if self.operation != "list":
|
||||||
|
validate_entry_name(self.name)
|
||||||
|
if self.operation == "rename":
|
||||||
|
validate_entry_name(self.target)
|
||||||
|
if self.target == self.name:
|
||||||
|
raise ValueError("Choose a different name.")
|
||||||
|
if self.operation == "upload" and (
|
||||||
|
type(self.size) is not int or not 0 <= self.size <= UPLOAD_LIMIT
|
||||||
|
):
|
||||||
|
raise ValueError("Uploads can be at most 64 MiB.")
|
||||||
|
if self.operation == "list" and self.after != "":
|
||||||
|
validate_entry_name(self.after)
|
||||||
|
if (
|
||||||
|
(self.operation == "list" and self.name != "")
|
||||||
|
or (self.operation != "rename" and self.target != "")
|
||||||
|
or (self.operation != "upload" and self.size is not None)
|
||||||
|
or (self.operation != "list" and self.after != "")
|
||||||
|
):
|
||||||
|
raise ValueError("Unexpected fields for this file operation.")
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def from_manifest(cls, data):
|
||||||
|
if (
|
||||||
|
not isinstance(data, dict)
|
||||||
|
or set(data)
|
||||||
|
!= {
|
||||||
|
"version",
|
||||||
|
"site",
|
||||||
|
"operation",
|
||||||
|
"directory",
|
||||||
|
"name",
|
||||||
|
"target",
|
||||||
|
"size",
|
||||||
|
"after",
|
||||||
|
}
|
||||||
|
or type(data["version"]) is not int
|
||||||
|
or data["version"] != 1
|
||||||
|
):
|
||||||
|
raise ValueError("Expected a version 1 website file request.")
|
||||||
|
args = {key: value for key, value in data.items() if key != "version"}
|
||||||
|
args["site"] = WebSite.from_manifest(data["site"])
|
||||||
|
return cls(**args)
|
||||||
|
|
||||||
|
def manifest(self):
|
||||||
|
return {
|
||||||
|
"version": 1,
|
||||||
|
"site": self.site.manifest(),
|
||||||
|
"operation": self.operation,
|
||||||
|
"directory": self.directory,
|
||||||
|
"name": self.name,
|
||||||
|
"target": self.target,
|
||||||
|
"size": self.size,
|
||||||
|
"after": self.after,
|
||||||
|
}
|
||||||
|
|
||||||
|
def identity(self):
|
||||||
|
return hashlib.sha256(encode_header(self.manifest())).hexdigest()
|
||||||
Reference in new issue
Block a user