Skip to content

Commit 5f2fa16

Browse files
committed
tcf/login: fix asking password for each server
Only query passwords once, unless we do an special answer (ask) and then it splits to per-server Signed-off-by: Inaky Perez-Gonzalez <[email protected]>
1 parent 907a502 commit 5f2fa16

File tree

1 file changed

+58
-12
lines changed

1 file changed

+58
-12
lines changed

tcfl/ui_cli_users.py

Lines changed: 58 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,10 @@
4242

4343

4444

45-
def _credentials_get(domain: str, aka: str, cli_args: argparse.Namespace):
45+
def _credentials_get_global(cli_args: argparse.Namespace):
4646
# env general
4747
user_env = os.environ.get("TCF_USER", None)
4848
password_env = os.environ.get("TCF_PASSWORD", None)
49-
# server specific
50-
user_env_aka = os.environ.get("TCF_USER_" + aka, None)
51-
password_env_aka = os.environ.get("TCF_PASSWORD_" + aka, None)
5249

5350
# from commandline
5451
user_cmdline = cli_args.username
@@ -57,11 +54,6 @@ def _credentials_get(domain: str, aka: str, cli_args: argparse.Namespace):
5754
# default to what came from environment
5855
user = user_env
5956
password = password_env
60-
# override with server specific from envrionment
61-
if user_env_aka:
62-
user = user_env_aka
63-
if password_env_aka:
64-
password = password_env_aka
6557
# override with what came from the command line
6658
if user_cmdline:
6759
user = user_cmdline
@@ -74,13 +66,66 @@ def _credentials_get(domain: str, aka: str, cli_args: argparse.Namespace):
7466
"Cannot obtain login name and"
7567
" -q was given (can't ask); "
7668
" please specify a login name or use environment"
77-
" TCF_USER[_AKA]")
69+
" TCF_USER[_<AKA>]")
70+
if not sys.stdout.isatty():
71+
raise RuntimeError(
72+
"Cannot obtain login name and"
73+
" terminal is not a TTY (can't ask); "
74+
" please specify a login name or use environment"
75+
" TCF_USER[_<AKA>]")
76+
user = input(f'Login for all servers [{getpass.getuser()}]'
77+
' (use *ask* for server-specific): ')
78+
if user == "": # default to LOGIN name
79+
user = getpass.getuser()
80+
print("I: defaulting to login name '{user}'")
81+
elif user == "ask":
82+
user = None
83+
84+
if user and not password:
85+
if cli_args.quiet:
86+
raise RuntimeError(
87+
"Cannot obtain password and"
88+
" -q was given (can't ask); "
89+
" please specify a login name or use environment"
90+
" TCF_PASSWORD[_<AKA>]")
91+
if not sys.stdout.isatty():
92+
raise RuntimeError(
93+
"Cannot obtain password and"
94+
" terminal is not a TTY (can't ask); "
95+
" please specify a login name or use environment"
96+
" TCF_PASSWORD[_<AKA>]")
97+
password = getpass.getpass(f"Password for {user} (on all servers): ")
98+
return user, password
99+
100+
101+
102+
def _credentials_get(domain: str, aka: str, user: str, password: str,
103+
cli_args: argparse.Namespace):
104+
# server specific
105+
user_env_aka = os.environ.get("TCF_USER_" + aka, None)
106+
password_env_aka = os.environ.get("TCF_PASSWORD_" + aka, None)
107+
108+
# override with server specific from envrionment
109+
if user_env_aka:
110+
user = user_env_aka
111+
if password_env_aka:
112+
password = password_env_aka
113+
# we don't override from the commandline, since we did it in
114+
# _credentials_get_global()
115+
116+
if not user:
117+
if cli_args.quiet:
118+
raise RuntimeError(
119+
"Cannot obtain login name and"
120+
" -q was given (can't ask); "
121+
" please specify a login name or use environment"
122+
" TCF_USER[_<AKA>]")
78123
if not sys.stdout.isatty():
79124
raise RuntimeError(
80125
"Cannot obtain login name and"
81126
" terminal is not a TTY (can't ask); "
82127
" please specify a login name or use environment"
83-
" TCF_USER[_AKA]")
128+
" TCF_USER[_<AKA>]")
84129
user = input('Login for %s [%s]: ' \
85130
% (domain, getpass.getuser()))
86131
if user == "": # default to LOGIN name
@@ -124,10 +169,11 @@ def _cmdline_login(cli_args: argparse.Namespace):
124169
logged = False
125170
servers = tcfl.server_c.servers
126171
# we only ask on the terminal HERE!
172+
user, password = _credentials_get_global(cli_args)
127173
credentials = {}
128174
for server_name, server in servers.items():
129175
credentials[server.aka] = \
130-
_credentials_get(server.url, server.aka, cli_args)
176+
_credentials_get(server.url, server.aka, user, password, cli_args)
131177

132178
r = tcfl.servers.run_fn_on_each_server(
133179
servers, _login, credentials,

0 commit comments

Comments
 (0)