fix(keyringctl): avoid leaking unclosed file descriptors

This commit is contained in:
Levente Polyak 2021-10-25 19:39:20 +02:00
parent 10d54ac35d
commit 878752dc13
No known key found for this signature in database
GPG Key ID: FC1B547C8D8172C8

View File

@ -11,8 +11,8 @@ from re import sub
from shutil import copytree
from subprocess import PIPE
from subprocess import Popen
from tempfile import NamedTemporaryFile
from tempfile import mkdtemp
from tempfile import mkstemp
from typing import Dict
from typing import List
from typing import Optional
@ -993,15 +993,16 @@ def inspect_keyring(working_dir: Path, keyring_root: Path, sources: Optional[Lis
transform_username_to_keyring_path(keyring_dir=keyring_root / "packager", paths=sources)
transform_fingerprint_to_keyring_path(keyring_root=keyring_root, paths=sources)
keyring = Path(mkstemp(dir=working_dir, prefix="packet-", suffix=".asc")[1]).absolute()
export(working_dir=working_dir, keyring_root=keyring_root, sources=sources, output=keyring)
with NamedTemporaryFile(dir=working_dir, prefix="packet-", suffix=".asc") as keyring:
keyring_path = Path(keyring.name)
export(working_dir=working_dir, keyring_root=keyring_root, sources=sources, output=keyring_path)
fingerprints: Dict[Fingerprint, Username] = get_fingerprints_from_certificate_directory(
paths=[keyring_root / "packager"]
) | get_fingerprints_from_certificate_directory(paths=[keyring_root / "main"], postfix=" (main)")
return inspect(
packet=keyring,
packet=keyring_path,
certifications=True,
fingerprints=fingerprints,
)
@ -1037,17 +1038,20 @@ def verify(
for certificate in sorted(cert_paths):
print(f"Verify {certificate.name} owned by {certificate.parent.name}")
keyring = Path(
mkstemp(dir=working_dir, prefix=f"{certificate.parent.name}-{certificate.name}", suffix=".asc")[1]
).absolute()
with NamedTemporaryFile(
dir=working_dir, prefix=f"{certificate.parent.name}-{certificate.name}", suffix=".asc"
) as keyring:
keyring_path = Path(keyring.name)
export(
working_dir=working_dir,
keyring_root=keyring_root,
sources=[certificate],
output=keyring,
output=keyring_path,
)
if lint_hokey:
keyring_fd = Popen(("sq", "dearmor", f"{str(keyring)}"), stdout=PIPE)
keyring_fd = Popen(("sq", "dearmor", f"{str(keyring_path)}"), stdout=PIPE)
print(system(["hokey", "lint"], _stdin=keyring_fd.stdout), end="")
if lint_sq_keyring:
print(system(["sq-keyring-linter", f"{str(keyring)}"]), end="")
print(system(["sq-keyring-linter", f"{str(keyring_path)}"]), end="")