remove USER_FILE usage in ssl_decrypt.py and ssl_encrypt.py; switch to argparse for command-line arguments

This commit is contained in:
2025-05-21 21:29:21 +02:00
parent 4cdcfadbac
commit 5ac37ad9ad
7 changed files with 106 additions and 70 deletions

View File

@ -1,20 +1,33 @@
#!/usr/bin/python3
""" This Script encrypt Wireguardfiles for Wirepy users for more Security """
import argparse
from pathlib import Path
import pwd
import shutil
import subprocess
from subprocess import CompletedProcess
from subprocess import CompletedProcess, run
from wp_app_config import AppConfig
log_name = AppConfig.USER_FILE.read_text().strip()
parser = argparse.ArgumentParser()
parser.add_argument("--user", required=True, help="Username of the target file system")
args = parser.parse_args()
keyfile: Path = Path(f"/home/{log_name}/.config/wire_py/pbwgk.pem")
try:
# Retrieve UID and GID
user_info = pwd.getpwnam(args.user)
uid = user_info.pw_uid # User ID (e.g., 1000)
gid = user_info.pw_gid # Group ID (e.g., 1000)
except KeyError:
print(f"User '{args.user}' not found.")
exit(1)
target: Path = Path(f"/home/{log_name}/.config/wire_py/")
keyfile: Path = Path(f"/home/{args.user}/.config/wire_py/pbwgk.pem")
target: Path = Path(f"/home/{args.user}/.config/wire_py/")
if not keyfile.is_file():
process: CompletedProcess[str] = subprocess.run(
process: CompletedProcess[str] = run(
[
"openssl",
"rsa",
@ -43,7 +56,7 @@ if not keyfile.is_file():
else:
print(f"Error generate Publickey: Code: {process.returncode}")
shutil.chown(keyfile, 1000, 1000)
shutil.chown(keyfile, uid, gid)
# any() get True when directory is not empty
if AppConfig.TEMP_DIR.exists() and any(AppConfig.TEMP_DIR.iterdir()):
@ -51,7 +64,7 @@ if AppConfig.TEMP_DIR.exists() and any(AppConfig.TEMP_DIR.iterdir()):
for config_file in clear_files:
base_name = Path(config_file).stem
process: CompletedProcess[str] = subprocess.run(
process: CompletedProcess[str] = run(
[
"openssl",
"pkeyutl",