Skip to content

Commit

Permalink
check file existence in try/catch block instead of using os.path.is_file
Browse files Browse the repository at this point in the history
  • Loading branch information
wyattrees committed Sep 26, 2024
1 parent dddea20 commit 0b84635
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions anvil-python/anvil/commands/haproxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

import ipaddress
import logging
import os.path
from typing import Any, Callable, List

from rich.console import Console
Expand Down Expand Up @@ -48,38 +47,38 @@
def validate_cert_file(filepath: str) -> None:
if filepath == "":
return
if not os.path.isfile(filepath):
raise ValueError(f"{filepath} does not exist")
try:
with open(filepath) as f:
if "BEGIN CERTIFICATE" not in f.read():
raise ValueError("Invalid certificate file")
except FileNotFoundError:
raise ValueError(f"{filepath} does not exist")
except PermissionError:
raise ValueError(f"Permission denied when trying to read {filepath}")


def validate_key_file(filepath: str) -> None:
if filepath == "":
return
if not os.path.isfile(filepath):
raise ValueError(f"{filepath} does not exist")
try:
with open(filepath) as f:
if "BEGIN PRIVATE KEY" not in f.read():
raise ValueError("Invalid key file")
except FileNotFoundError:
raise ValueError(f"{filepath} does not exist")
except PermissionError:
raise ValueError(f"Permission denied when trying to read {filepath}")


def validate_cacert_chain(filepath: str) -> None:
if filepath == "":
return
if not os.path.isfile(filepath):
raise ValueError(f"{filepath} does not exist")
try:
# just make sure we can open the file
with open(filepath):
pass
except FileNotFoundError:
raise ValueError(f"{filepath} does not exist")
except PermissionError:
raise ValueError(f"Permission denied when trying to read {filepath}")

Expand Down

0 comments on commit 0b84635

Please sign in to comment.