Skip to content

Commit

Permalink
Catch OSError exceptions
Browse files Browse the repository at this point in the history
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 <module>
    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.
  • Loading branch information
unixsurfer committed Mar 2, 2023
1 parent 1edd49a commit 2f60ab6
Showing 1 changed file with 4 additions and 1 deletion.
5 changes: 4 additions & 1 deletion haproxyadmin/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 2f60ab6

Please sign in to comment.