Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
## 2024-05-15 - Python Native Directory Traversal vs External Subprocesses
**Learning:** Shelling out to external utilities like `find` via `os.system` or `subprocess` within Python scripts introduces significant overhead due to subshell spawning and external process execution. This is especially true when performing multiple independent `find` calls on the same directory tree.
**Action:** Use native Python libraries like `os.walk` or `os.scandir` combined with `os.chmod`, `os.chown`, etc., to traverse directories in a single pass and eliminate subprocess overhead. This reduces execution time and improves cross-platform compatibility.
## 2024-05-16 - Python Chown -R Optimization
**Learning:** Shelling out to `chown -R` via `os.system` creates a subshell overhead and an unnecessary extra pass over the directory structure when another `os.walk` loop already traverses the same directory.
**Action:** Use native Python libraries like `os.walk` to combine both permission changes and ownership changes (`os.lchown`) into a single traversal pass, improving Python script performance significantly.
38 changes: 25 additions & 13 deletions cidata_ingest.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,20 +80,32 @@ def decrypt_secrets_archive(enc_file):
return False

# Correct ownership and permissions of home directory files
print("Correcting ownership to user shane (1000:1000)...")
os.system("chown -R 1000:1000 /target/home/shane/")

# Enforce strict 600 permissions for ingested secrets
# ⚡ Bolt: Replaced external find/chmod subprocesses with native python os.walk.
# Impact: Avoids spawning multiple subshells and redundant directory traversals, significantly speeding up ingestion.
# ⚡ Bolt: Unified directory traversal for both ownership and permissions.
# Impact: Eliminates subshell spawn overhead for `chown -R` and prevents redundant I/O passes.
# Uses `os.lchown` instead of `os.chown` to prevent following symlinks.
print("Correcting ownership to user shane (1000:1000) and enforcing permissions...")
target_dir = '/target/home/shane/'

try:
os.lchown(target_dir, 1000, 1000)
except OSError as e:
print(f"Warning: could not set ownership for {target_dir}: {e}")

for root, dirs, files in os.walk(target_dir):
for d in dirs:
dir_path = os.path.join(root, d)
try:
os.lchown(dir_path, 1000, 1000)
except OSError as e:
print(f"Warning: could not set ownership for {dir_path}: {e}")
for file in files:
if file in ('rclone.conf', 'claude.json'):
try:
os.chmod(os.path.join(root, file), 0o600)
except OSError as e:
print(f"Warning: could not set permissions for {file}: {e}")
file_path = os.path.join(root, file)
try:
os.lchown(file_path, 1000, 1000)
if file in ('rclone.conf', 'claude.json'):
os.chmod(file_path, 0o600)
except OSError as e:
print(f"Warning: could not set ownership/permissions for {file_path}: {e}")

print("Secrets decryption and ingestion completed successfully.")
return True
Expand Down Expand Up @@ -129,10 +141,10 @@ def _write_single_file(file_info):
user, group = owner.split(':')
uid = 1000 if user == 'shane' else 0
gid = 1000 if group == 'shane' else 0
os.chown(target_path, uid, gid)
os.lchown(target_path, uid, gid)
parent_dir = os.path.dirname(target_path)
while parent_dir != '/target/home' and parent_dir != '/target' and parent_dir != '/':
os.chown(parent_dir, uid, gid)
os.lchown(parent_dir, uid, gid)
parent_dir = os.path.dirname(parent_dir)
except Exception as e:
print(f"Warning: could not set ownership for {target_path}: {e}")
Expand Down