From 2f60ab68713ba2d867919a1589f9768c2f73ce98 Mon Sep 17 00:00:00 2001 From: Pavlos Parissis Date: Thu, 2 Mar 2023 10:54:30 +0100 Subject: [PATCH] Catch OSError exceptions MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit If a user passes a socket file that does not exist then the is_unix_socket will raise an exception : ➜ haproxytool acl -F /var/lib/haproxy/statss -s /etc/haproxy/block_traffic_header_names.list Traceback (most recent call last): File "/home/pavlos/.virtualenvs/haproxy/bin/haproxytool", line 8, in sys.exit(main()) File "/home/pavlos/.virtualenvs/haproxy/lib/python3.9/site-packages/haproxytool/cli.py", line 41, in main call_main(sub_cmd) File "/home/pavlos/.virtualenvs/haproxy/lib/python3.9/site-packages/haproxytool/acl.py", line 107, in main hap = haproxy_object(arguments) File "/home/pavlos/.virtualenvs/haproxy/lib/python3.9/site-packages/haproxytool/utils.py", line 57, in haproxy_object hap = haproxy.HAProxy(socket_file=arguments['--file'], File "/home/pavlos/.virtualenvs/haproxy/lib/python3.9/site-packages/haproxyadmin/haproxy.py", line 117, in __init__ elif (socket_file and is_unix_socket(socket_file) and File "/home/pavlos/.virtualenvs/haproxy/lib/python3.9/site-packages/haproxyadmin/utils.py", line 157, in is_unix_socket mode = os.stat(path).st_mode FileNotFoundError: [Errno 2] No such file or directory: '/var/lib/haproxy/statss' Let's avoid this situation by catching OSError class exceptions. --- haproxyadmin/utils.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/haproxyadmin/utils.py b/haproxyadmin/utils.py index 4eeb4d0..5e78388 100644 --- a/haproxyadmin/utils.py +++ b/haproxyadmin/utils.py @@ -154,7 +154,10 @@ def is_unix_socket(path): :type path: ``string`` :rtype: ``bool`` """ - mode = os.stat(path).st_mode + try: + mode = os.stat(path).st_mode + except OSError: + return False return stat.S_ISSOCK(mode)