Skip to content

Commit

Permalink
ssh: Decouple password and passphrase.
Browse files Browse the repository at this point in the history
Introduce new options `passphrase` and `ask_passphrase`.

Depends on updating config_schema on `dvc` repo.
  • Loading branch information
daavoo committed Oct 13, 2022
1 parent 3718421 commit 3663507
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
20 changes: 11 additions & 9 deletions dvc_ssh/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@

@wrap_with(threading.Lock())
@memoize
def ask_password(host, user, port):
def ask_password(host, user, port, desc):
return getpass.getpass(
"Enter a private key passphrase or a password for "
f"host '{host}' port '{port}' user '{user}':\n"
f"Enter a {desc} for " f"host '{host}' port '{port}' user '{user}':\n"
)


Expand Down Expand Up @@ -62,13 +61,16 @@ def _prepare_credentials(self, **config):
or DEFAULT_PORT
)

if config.get("ask_password") and config.get("password") is None:
config["password"] = ask_password(
login_info["host"], login_info["username"], login_info["port"]
)
for option in ("password", "passphrase"):
login_info[option] = config.get(option, None)

login_info["password"] = config.get("password")
login_info["passphrase"] = config.get("password")
if config.get(f"ask_{option}") and login_info[option] is None:
login_info[option] = ask_password(
login_info["host"],
login_info["username"],
login_info["port"],
option,
)

raw_keys = []
if config.get("keyfile"):
Expand Down
15 changes: 15 additions & 0 deletions dvc_ssh/tests/test_prepare_credentials.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest

from dvc_ssh import SSHFileSystem


@pytest.mark.parametrize("password", [None, "foo"])
@pytest.mark.parametrize("passphrase", [None, "bar"])
def test_passphrase(mocker, password, passphrase):
connect = mocker.patch("asyncssh.connect")

kwargs = {"password": password, "passphrase": passphrase}
_ = SSHFileSystem(host="foo", **kwargs).fs

assert connect.call_args[1]["password"] == password
assert connect.call_args[1]["passphrase"] == passphrase

0 comments on commit 3663507

Please sign in to comment.