Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_setup(self):
from knack.prompting import NoTTYException
with self.assertRaises(NoTTYException):
cmd('setup')
except NoTTYException:
raise CLIError('Please specify --registry-password in order to use custom image registry.')
image_registry_credentials = [ImageRegistryCredential(server=registry_login_server,
username=registry_username,
password=registry_password)]
elif ACR_SERVER_DELIMITER in image.split("/")[0]:
if not registry_username:
try:
registry_username = prompt(msg='Image registry username: ')
except NoTTYException:
raise CLIError('Please specify --registry-username in order to use Azure Container Registry.')
if not registry_password:
try:
registry_password = prompt_pass(msg='Image registry password: ')
except NoTTYException:
raise CLIError('Please specify --registry-password in order to use Azure Container Registry.')
acr_server = image.split("/")[0] if image.split("/") else None
if acr_server:
image_registry_credentials = [ImageRegistryCredential(server=acr_server,
username=registry_username,
password=registry_password)]
elif registry_username and registry_password and SERVER_DELIMITER in image.split("/")[0]:
login_server = image.split("/")[0] if image.split("/") else None
if login_server:
image_registry_credentials = [ImageRegistryCredential(server=login_server,
username=registry_username,
password=registry_password)]
else:
raise CLIError('Failed to parse login server from image name; please explicitly specify --registry-server.')
password_usage_error = "incorrect usage for authentication-type 'password': " \
"[--admin-username USERNAME] --admin-password PASSWORD | " \
"[--admin-username USERNAME] --saved-secret SECRETNAME"
if namespace.ssh_key or namespace.generate_ssh_keys or (namespace.saved_secret and namespace.admin_password):
raise ValueError(password_usage_error)
# Respect user's provided saved secret name for password authentication
if namespace.saved_secret:
namespace.admin_password = "[[{}]]".format(namespace.saved_secret)
if not namespace.admin_password:
# prompt for admin password if not supplied
from knack.prompting import prompt_pass, NoTTYException
try:
namespace.admin_password = prompt_pass('Admin Password: ', confirm=True)
except NoTTYException:
raise CLIError('Please specify both username and password in non-interactive mode.')
elif namespace.authentication_type == 'ssh':
if namespace.os_type != 'Linux':
raise CLIError("incorrect authentication-type '{}' for os type '{}'".format(
namespace.authentication_type, namespace.os_type))
ssh_usage_error = "incorrect usage for authentication-type 'ssh': " \
"[--admin-username USERNAME] | " \
"[--admin-username USERNAME] --ssh-key KEY | " \
"[--admin-username USERNAME] --generate-ssh-keys | " \
"[--admin-username USERNAME] --saved-secret SECRETNAME"
if namespace.admin_password or (namespace.saved_secret and (namespace.ssh_key or namespace.generate_ssh_keys)):
raise ValueError(ssh_usage_error)
# Respect user's provided saved secret name for ssh authentication
if len(parts) == 1:
raise CLIError('usage error: --defaults STRING=STRING STRING=STRING ...')
cmd.cli_ctx.config.set_value(defaults_section, parts[0], _normalize_config_value(parts[1]))
return
if list_defaults:
with ConfiguredDefaultSetter(cmd.cli_ctx.config, scope.lower() == 'local'):
defaults_result = cmd.cli_ctx.config.items(cmd.cli_ctx.config.defaults_section_name)
return [x for x in defaults_result if x.get('value')]
# if nothing supplied, we go interactively
try:
print(MSG_INTRO)
_handle_global_configuration(cmd.cli_ctx.config)
print(MSG_CLOSING)
# TODO: log_telemetry('configure', **answers)
except NoTTYException:
raise CLIError('This command is interactive and no tty available.')
except (EOFError, KeyboardInterrupt):
print()
def handle_feedback(cmd):
try:
print(_MSG_INTR)
recent_commands = _display_recent_commands(cmd)
res = _prompt_issue(recent_commands)
if res:
print(_MSG_THNK)
return
except NoTTYException:
raise CLIError('This command is interactive, however no tty is available.')
except (EOFError, KeyboardInterrupt):
print()
try:
value = shell_safe_json_parse(value)
except Exception as ex: # pylint: disable=broad-except
logger.error(ex)
continue
result[param_name] = value
break
else:
try:
result[param_name] = prompt(prompt_str, help_string=description)
except NoTTYException:
result[param_name] = None
no_tty = True
break
if no_tty and fail_on_no_tty:
raise NoTTYException
return result
def _get_pat_token():
try:
token = prompt_pass('Token: ', confirm=False, help_string="The token (PAT) to authenticate with.")
while len(token) <= 1:
logger.warning('Please provide a PAT token.')
logger.warning('If you are using CTRL + V to paste the token, it won\'t work '
'on windows command prompt or powershell - '
'https://github.com/microsoft/knack/issues/160.')
logger.warning('Use right click or console menu to paste the token.')
token = prompt_pass('Token: ', confirm=False, help_string="The token (PAT) to authenticate with.")
except NoTTYException:
logger.info("Getting PAT token in non-interactive mode.")
token = sys.stdin.readline().rstrip()
return token
def set_deployment_user(cmd, user_name, password=None):
'''
Update deployment credentials.(Note, all webapps in your subscription will be impacted)
'''
client = web_client_factory(cmd.cli_ctx)
user = User(publishing_user_name=user_name)
if password is None:
try:
password = prompt_pass(msg='Password: ', confirm=True)
except NoTTYException:
raise CLIError('Please specify both username and password in non-interactive mode.')
user.publishing_password = password
return client.update_publishing_user(user)
def verify_is_a_tty():
if not sys.stdin.isatty():
logger.debug('No tty available.')
raise NoTTYException()
def _user_confirmed(confirmation, command_args):
if callable(confirmation):
return confirmation(command_args)
try:
if isinstance(confirmation, six.string_types):
return prompt_y_n(confirmation)
return prompt_y_n('Are you sure you want to perform this operation?')
except NoTTYException:
logger.warning('Unable to prompt for confirmation as no tty available. Use --yes.')
return False