Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
Upcoming Release (TBD)
======================

Features
--------
* Added explicit error handle to get_password_from_file with EAFP.


1.30.0 (2025/04/19)
===================

Expand Down
1 change: 1 addition & 0 deletions mycli/AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ Contributors:
* Abirami P
* Adam Chainz
* Aljosha Papsch
* Allrob
* Andy Teijelo Pérez
* Angelo Lupo
* Artem Bezsmertnyi
Expand Down
22 changes: 16 additions & 6 deletions mycli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@

SUPPORT_INFO = "Home: http://mycli.net\n" "Bug tracker: https://github.com/dbcli/mycli/issues"

class PasswordFileError(Exception):
"""Base exception for errors related to reading password files."""
pass

class MyCli(object):
default_prompt = "\\t \\u@\\h:\\d> "
Expand Down Expand Up @@ -536,14 +539,21 @@ def _connect():
sys.exit(1)

def get_password_from_file(self, password_file):
password_from_file = None
if password_file:
if (os.path.isfile(password_file) or stat.S_ISFIFO(os.stat(password_file).st_mode)) and os.access(password_file, os.R_OK):
try:
with open(password_file) as fp:
password_from_file = fp.readline()
password_from_file = password_from_file.rstrip().lstrip()

return password_from_file
password = fp.readline().strip()
if not password:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this introduce a new constraint?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you referring to if not? If so, I thought it makes sense to raise an error when the content is empty

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, raising an error when the password is empty changes the functionality, which I suppose we should hold constant for this PR. Looking closer, I would even strip() the newline more precisely but again that is out of scope.

Because the empty string is a valid password, as are passwords containing whitespace. Whether they are advisable is a separate Q.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I updated the code.

raise PasswordFileError(f"Password file '{password_file}' is empty or contains only whitespace")
return password
except FileNotFoundError:
raise PasswordFileError(f"Password file '{password_file}' not found") from None
except PermissionError:
raise PasswordFileError(f"Permission denied reading password file '{password_file}'") from None
except IsADirectoryError:
raise PasswordFileError(f"Path '{password_file}' is a directory, not a file") from None
except OSError as e:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also advisable to catch RuntimeError ? I'm unsure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's useful, maybe I can leave this last exception generic to handle all other types of exceptions? what do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your decision!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

raise PasswordFileError(f"Error reading password file '{password_file}': {str(e)}") from None

def handle_editor_command(self, text):
r"""Editor command is any query that is prefixed or suffixed by a '\e'.
Expand Down