From 8a97b21b76d326b7cc6faaae85831fd864aad10b Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Wed, 15 Sep 2021 15:26:57 +0300 Subject: [PATCH] ssh: ask password before setting it (#6615) --- dvc/fs/ssh.py | 10 +++++----- tests/unit/fs/test_ssh.py | 6 ++++++ 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/dvc/fs/ssh.py b/dvc/fs/ssh.py index b5741cb56..9966d147c 100644 --- a/dvc/fs/ssh.py +++ b/dvc/fs/ssh.py @@ -67,6 +67,11 @@ class SSHFileSystem(FSSpecWrapper): or self.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"] + ) + login_info["password"] = config.get("password") login_info["passphrase"] = config.get("password") @@ -101,11 +106,6 @@ class SSHFileSystem(FSSpecWrapper): login_info["agent_forwarding"] = config.get("agent_forwarding", True) login_info["proxy_command"] = user_ssh_config.get("ProxyCommand") - if config.get("ask_password") and login_info["password"] is None: - login_info["password"] = ask_password( - login_info["host"], login_info["username"], login_info["port"] - ) - # We are going to automatically add stuff to known_hosts # something like paramiko's AutoAddPolicy() login_info["known_hosts"] = None diff --git a/tests/unit/fs/test_ssh.py b/tests/unit/fs/test_ssh.py index a7372ddd6..60712e9b1 100644 --- a/tests/unit/fs/test_ssh.py +++ b/tests/unit/fs/test_ssh.py @@ -43,6 +43,12 @@ def test_init(): assert fs.fs_args["password"] == fs.fs_args["passphrase"] == "xxx" +def test_ssh_ask_password(mocker): + mocker.patch("dvc.fs.ssh.ask_password", return_value="fish") + fs = SSHFileSystem(user="test", host="2.2.2.2", ask_password=True) + assert fs.fs_args["password"] == fs.fs_args["passphrase"] == "fish" + + mock_ssh_config = """ Host example.com User ubuntu