Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fetching from pass manager for multiline entries #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
27 changes: 15 additions & 12 deletions syncall/app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,19 +304,22 @@ def fetch_from_pass_manager(password_path: str, allow_fail=False) -> str | None:

passwd = None
try:
passwd = read_gpg_token(pass_full_path)
except subprocess.CalledProcessError as err:
# pass format allows additional metadata (username, url, etc.)
# the password itself is a single string on the first line
passwd = read_gpg_token(pass_full_path).split("\n")[0]
except (subprocess.CalledProcessError, AttributeError) as err:
if not allow_fail:
logger.error(
"\n".join(
[
f"Couldn't read {password_path} from pass\n\nFull path:"
f" {pass_full_path}",
non_empty("stdout", err.stdout.decode("utf-8"), join_with=": "),
non_empty("stderr", err.stderr.decode("utf-8"), join_with=": "),
],
),
)
log_lines = [
f"Couldn't read {password_path} from pass\n\nFull path: {pass_full_path}",
]

if hasattr(err, "stdout") and hasattr(err, "stderr"):
log_lines += [
non_empty("stdout", err.stdout.decode("utf-8"), join_with=": "),
non_empty("stderr", err.stderr.decode("utf-8"), join_with=": "),
]

logger.error("\n".join(log_lines))
sys.exit(1)

return passwd
Expand Down
18 changes: 18 additions & 0 deletions tests/test_app_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from syncall.app_utils import (
cache_or_reuse_cached_combination,
fetch_app_configuration,
fetch_from_pass_manager,
inform_about_combination_name_usage,
report_toplevel_exception,
)
Expand Down Expand Up @@ -98,3 +99,20 @@ def test_cache_or_reuse_cached_combination(fs, caplog, mock_prefs_manager):
assert "Loading cached configuration" in caplog.text
assert "1__2__3" in caplog.text
caplog.clear()


def test_fetch_from_pass_manager():
with patch("syncall.app_utils.valid_path", return_value="password_store"):
with patch(
"syncall.app_utils.read_gpg_token",
return_value="foobar\nusername: foo@bar",
):
assert fetch_from_pass_manager("password_store") == "foobar"

with patch("syncall.app_utils.read_gpg_token", return_value=""):
assert fetch_from_pass_manager("password_store") == ""

with patch("syncall.app_utils.read_gpg_token", return_value=None):
with pytest.raises(SystemExit):
assert fetch_from_pass_manager("password_store")
assert fetch_from_pass_manager("password_store", allow_fail=True) is None