Skip to content

Commit 8af1aee

Browse files
committed
remote: refactor ssh ask password code
1 parent 73eac3b commit 8af1aee

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

dvc/remote/ssh/__init__.py

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@
88
import os
99
import threading
1010
from concurrent.futures import ThreadPoolExecutor
11-
from contextlib import closing
12-
from contextlib import contextmanager
11+
from contextlib import closing, contextmanager
12+
13+
from funcy import memoize, wrap_with
1314

1415
import dvc.prompt as prompt
1516
from dvc.config import Config
@@ -24,8 +25,15 @@
2425
logger = logging.getLogger(__name__)
2526

2627

27-
saved_passwords = {}
28-
saved_passwords_lock = threading.Lock()
28+
@wrap_with(threading.Lock())
29+
@memoize
30+
def ask_password(host, user, port):
31+
return prompt.password(
32+
"Enter a private key passphrase or a password for "
33+
"host '{host}' port '{port}' user '{user}'".format(
34+
host=host, port=port, user=user
35+
)
36+
)
2937

3038

3139
class RemoteSSH(RemoteBASE):
@@ -120,21 +128,11 @@ def _try_get_ssh_config_keyfile(user_ssh_config):
120128
def ensure_credentials(self, path_info=None):
121129
if path_info is None:
122130
path_info = self.path_info
123-
host, user, port = path_info.host, path_info.user, path_info.port
131+
124132
# NOTE: we use the same password regardless of the server :(
125133
if self.ask_password and self.password is None:
126-
with saved_passwords_lock:
127-
server_key = (host, user, port)
128-
password = saved_passwords.get(server_key)
129-
130-
if password is None:
131-
saved_passwords[server_key] = password = prompt.password(
132-
"Enter a private key passphrase or a password for "
133-
"host '{host}' port '{port}' user '{user}'".format(
134-
host=host, port=port, user=user
135-
)
136-
)
137-
self.password = password
134+
host, user, port = path_info.host, path_info.user, path_info.port
135+
self.password = ask_password(host, user, port)
138136

139137
def ssh(self, path_info):
140138
self.ensure_credentials(path_info)

0 commit comments

Comments
 (0)