feat(profiles): implement slice 3 profile system, flock concurrency, and sanitize archiver
This commit is contained in:
1 parent
66ca179937
commit
646bd9d145
8 files changed
+536
-69
No files matched your search
@@ -29,6 +29,15 @@ def is_socket_or_fifo(filepath: str) -> bool:
|
||||
except OSError:
|
||||
return True
|
||||
|
||||
def is_sanitized_target(filepath: str) -> bool:
|
||||
"""Check if file contains secrets and must be omitted during sanitized export."""
|
||||
name = os.path.basename(filepath)
|
||||
if name in ("shadow", "gshadow", "opasswd", "shadow-", "gshadow-"):
|
||||
return True
|
||||
if name.startswith("ssh_host_") and "key" in name and not name.endswith(".pub"):
|
||||
return True
|
||||
return False
|
||||
|
||||
class SnapshotArchiver:
|
||||
"""Manages creation, listing, and verification of snapshot archives."""
|
||||
|
||||
@@ -50,17 +59,22 @@ class SnapshotArchiver:
|
||||
def collect_file_list(
|
||||
self,
|
||||
paths: List[str],
|
||||
exclude_patterns: Optional[List[str]] = None
|
||||
) -> Tuple[List[Tuple[str, str]], Dict[str, str], int]:
|
||||
exclude_patterns: Optional[List[str]] = None,
|
||||
sanitize: bool = False,
|
||||
one_file_system: bool = True,
|
||||
max_file_size_mb: int = 50
|
||||
) -> Tuple[List[Tuple[str, str]], Dict[str, str], int, List[str]]:
|
||||
"""
|
||||
Scan paths and compute pre-archive hashes and metadata.
|
||||
Returns:
|
||||
- list of (filesystem_abs_path, tar_rel_path)
|
||||
- dict of {tar_rel_path: sha256_hash}
|
||||
- total uncompressed bytes
|
||||
- list of sanitized/redacted relative paths
|
||||
"""
|
||||
file_entries: List[Tuple[str, str]] = []
|
||||
files_sha256: Dict[str, str] = {}
|
||||
sanitized_files: List[str] = []
|
||||
total_bytes = 0
|
||||
exclude_patterns = exclude_patterns or []
|
||||
|
||||
@@ -69,9 +83,19 @@ class SnapshotArchiver:
|
||||
if not os.path.exists(abs_base):
|
||||
continue
|
||||
|
||||
try:
|
||||
base_st = os.lstat(abs_base)
|
||||
base_dev = base_st.st_dev
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
if os.path.isfile(abs_base) or os.path.islink(abs_base):
|
||||
rel_name = os.path.join("data", os.path.basename(abs_base))
|
||||
if sanitize and is_sanitized_target(abs_base):
|
||||
sanitized_files.append(rel_name)
|
||||
continue
|
||||
|
||||
if not is_socket_or_fifo(abs_base):
|
||||
rel_name = os.path.join("data", os.path.basename(abs_base))
|
||||
file_entries.append((abs_base, rel_name))
|
||||
if not os.path.islink(abs_base):
|
||||
sha = calculate_file_sha256(abs_base)
|
||||
@@ -84,26 +108,57 @@ class SnapshotArchiver:
|
||||
# Filter subdirectories in place
|
||||
dirs[:] = [d for d in dirs if not any(p in d for p in exclude_patterns)]
|
||||
|
||||
# Check one-file-system boundary for subdirectories
|
||||
if one_file_system:
|
||||
allowed_dirs = []
|
||||
for d in dirs:
|
||||
dir_path = os.path.join(root, d)
|
||||
try:
|
||||
if os.lstat(dir_path).st_dev == base_dev:
|
||||
allowed_dirs.append(d)
|
||||
except OSError:
|
||||
pass
|
||||
dirs[:] = allowed_dirs
|
||||
|
||||
for filename in files:
|
||||
abs_filepath = os.path.join(root, filename)
|
||||
if is_socket_or_fifo(abs_filepath):
|
||||
if any(p in filename for p in exclude_patterns):
|
||||
continue
|
||||
|
||||
# One file system boundary check
|
||||
try:
|
||||
st = os.lstat(abs_filepath)
|
||||
if one_file_system and st.st_dev != base_dev:
|
||||
continue
|
||||
except OSError:
|
||||
continue
|
||||
|
||||
# Large file check (> 50 MB)
|
||||
if max_file_size_mb > 0 and st.st_size > (max_file_size_mb * 1024 * 1024):
|
||||
continue
|
||||
|
||||
# Calculate archive path under data/
|
||||
rel_to_base = os.path.relpath(abs_filepath, os.path.dirname(abs_base))
|
||||
arc_path = os.path.join("data", rel_to_base)
|
||||
|
||||
# Diagnostic sanitization check
|
||||
if sanitize and is_sanitized_target(abs_filepath):
|
||||
sanitized_files.append(arc_path)
|
||||
continue
|
||||
|
||||
if is_socket_or_fifo(abs_filepath):
|
||||
continue
|
||||
|
||||
file_entries.append((abs_filepath, arc_path))
|
||||
|
||||
if not os.path.islink(abs_filepath):
|
||||
try:
|
||||
sha = calculate_file_sha256(abs_filepath)
|
||||
files_sha256[arc_path] = sha
|
||||
total_bytes += os.path.getsize(abs_filepath)
|
||||
total_bytes += st.st_size
|
||||
except (OSError, IOError):
|
||||
pass
|
||||
|
||||
return file_entries, files_sha256, total_bytes
|
||||
return file_entries, files_sha256, total_bytes, sanitized_files
|
||||
|
||||
def create_snapshot_archive(
|
||||
self,
|
||||
|
||||
Reference in new issue
Block a user