diff options
author | Alistair Coles <alistairncoles@gmail.com> | 2018-06-11 13:19:05 +0100 |
---|---|---|
committer | Alistair Coles <alistairncoles@gmail.com> | 2018-06-11 17:25:21 +0100 |
commit | 33ad9fd4cc0ade9f0800a2815ee0ef514ae8f264 (patch) | |
tree | 54ee62d50f13e91a19eeab7bbc81f5fecc6b339b /swiftclient | |
parent | 2312182241d36c716e624a23acd51f2b0252e4aa (diff) | |
download | python-swiftclient-33ad9fd4cc0ade9f0800a2815ee0ef514ae8f264.tar.gz |
Add option for user to enter password
Add the --prompt option for the CLI which will cause the user to be
prompted to enter a password. Any password otherwise specified by
--key, --os-password or an environment variable will be ignored.
The swift client will exit with a warning if the password cannot be
entered without its value being echoed.
Closes-Bug: #1357562
Change-Id: I513647eed460007617f129691069c6fb1bfe62d7
Diffstat (limited to 'swiftclient')
-rwxr-xr-x | swiftclient/shell.py | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/swiftclient/shell.py b/swiftclient/shell.py index c15d7cf..e91a16f 100755 --- a/swiftclient/shell.py +++ b/swiftclient/shell.py @@ -17,11 +17,13 @@ from __future__ import print_function, unicode_literals import argparse +import getpass import io import json import logging import signal import socket +import warnings from os import environ, walk, _exit as os_exit from os.path import isfile, isdir, join @@ -1410,6 +1412,30 @@ class HelpFormatter(argparse.HelpFormatter): return action.dest +def prompt_for_password(): + """ + Prompt the user for a password. + + :raise SystemExit: if a password cannot be entered without it being echoed + to the terminal. + :return: the entered password. + """ + with warnings.catch_warnings(): + warnings.filterwarnings('error', category=getpass.GetPassWarning, + append=True) + try: + # temporarily set signal handling back to default to avoid user + # Ctrl-c leaving terminal in weird state + signal.signal(signal.SIGINT, signal.SIG_DFL) + return getpass.getpass() + except EOFError: + return None + except getpass.GetPassWarning: + exit('Input stream incompatible with --prompt option') + finally: + signal.signal(signal.SIGINT, immediate_exit) + + def parse_args(parser, args, enforce_requires=True): options, args = parser.parse_known_args(args or ['-h']) options = vars(options) @@ -1435,6 +1461,10 @@ def parse_args(parser, args, enforce_requires=True): if args and args[0] == 'tempurl': return options, args + # do this before process_options sets default auth version + if enforce_requires and options['prompt']: + options['key'] = options['os_password'] = prompt_for_password() + # Massage auth version; build out os_options subdict process_options(options) @@ -1506,6 +1536,7 @@ def main(arguments=None): [--os-key <client-certificate-key-file>] [--no-ssl-compression] [--force-auth-retry] + [--prompt] <subcommand> [--help] [<subcommand options>] Command-line interface to the OpenStack Swift API. @@ -1620,6 +1651,12 @@ Examples: default=False, help='Force a re-auth attempt on ' 'any error other than 401 unauthorized') + parser.add_argument('--prompt', + action='store_true', dest='prompt', + default=False, + help='Prompt user to enter a password which overrides ' + 'any password supplied via --key, --os-password ' + 'or environment variables.') os_grp = parser.add_argument_group("OpenStack authentication options") os_grp.add_argument('--os-username', |