Azure / azure-cli

Azure Command-Line Interface
MIT License
4.02k stars 2.99k forks source link

Uses of getpass.getuser() should guard against username not being available #2100

Closed weinong closed 5 years ago

weinong commented 7 years ago

I am using docker image. With the default root user, everything is fine. However, when I use non-root user, it's not able to load some module.

$ sudo docker run -u 1000:1000 -v /home/weinong:/home/weinong -w /home/weinong -e HOME=/home/weinong -it azuresdk/azure-cli-python bash

$ az vm -h
Error loading command module 'vm'
derekbekoe commented 7 years ago

What's the error from az --debug?

weinong commented 7 years ago

DEBUG: Command arguments ['vm', '-h'] DEBUG: Installed command modules ['acr', 'acs', 'appservice', 'batch', 'cloud', 'component', 'configure', 'container', 'context', 'feedback', 'iot', 'keyvault', 'network', 'profile', 'redis', 'resource', 'role', 'sql', 'storage', 'taskhelp', 'vm'] DEBUG: Registered application event handler 'CommandTableParams.Loaded' at <function add_id_parameters at 0x7f20efd3f598> DEBUG: Registered application event handler 'CommandTable.Loaded' at <function add_id_parameters at 0x7f20efd3f598> DEBUG: Loaded module 'acr' in 0.258 seconds. DEBUG: Loaded module 'acs' in 0.108 seconds. DEBUG: Loaded module 'appservice' in 0.052 seconds. DEBUG: Loaded module 'batch' in 0.019 seconds. DEBUG: Loaded module 'cloud' in 0.001 seconds. DEBUG: Loaded module 'component' in 0.001 seconds. DEBUG: Loaded module 'configure' in 0.001 seconds. DEBUG: Loaded module 'container' in 0.001 seconds. DEBUG: Loaded module 'context' in 0.002 seconds. DEBUG: Loaded module 'feedback' in 0.001 seconds. DEBUG: Loaded module 'iot' in 0.017 seconds. DEBUG: Loaded module 'keyvault' in 0.040 seconds. DEBUG: Loaded module 'network' in 0.123 seconds. DEBUG: Loaded module 'profile' in 0.002 seconds. DEBUG: Loaded module 'redis' in 0.008 seconds. DEBUG: Loaded module 'resource' in 0.004 seconds. DEBUG: Loaded module 'role' in 0.009 seconds. DEBUG: Loaded module 'sql' in 0.021 seconds. DEBUG: Loaded module 'storage' in 0.046 seconds. DEBUG: Loaded module 'taskhelp' in 0.001 seconds. ERROR: Error loading command module 'vm' DEBUG: Traceback (most recent call last): File "/usr/local/lib/python3.5/site-packages/azure/cli/core/commands/init.py", line 247, in get_command_table import_module('azure.cli.command_modules.' + mod).load_commands() File "/usr/local/lib/python3.5/site-packages/azure/cli/command_modules/vm/init.py", line 14, in load_commands import azure.cli.command_modules.vm.commands # pylint: disable=redefined-outer-name File "/usr/local/lib/python3.5/site-packages/azure/cli/command_modules/vm/commands.py", line 71, in cli_command(name, 'vm create', custom_path.format('create_vm'), transform=transform_vm_create_output) File "/usr/local/lib/python3.5/site-packages/azure/cli/core/commands/init.py", line 284, in cli_command client_factory, no_wait_param, confirmation=confirmation) File "/usr/local/lib/python3.5/site-packages/azure/cli/core/commands/init.py", line 366, in create_command arguments_loader=arguments_loader, description_loader=description_loader) File "/usr/local/lib/python3.5/site-packages/azure/cli/core/commands/init.py", line 183, in init if description_loader and CliCommand._should_load_description() \ File "/usr/local/lib/python3.5/site-packages/azure/cli/core/commands/init.py", line 363, in description_loader return extract_full_summary_from_signature(get_op_handler(operation)) File "/usr/local/lib/python3.5/site-packages/azure/cli/core/commands/init.py", line 291, in get_op_handler op = import_module(mod_to_import) File "/usr/local/lib/python3.5/importlib/init.py", line 126, in import_module return _bootstrap._gcd_import(name[level:], package, level) File "/usr/local/lib/python3.5/site-packages/azure/cli/command_modules/vm/custom.py", line 1152, in authentication_type=None, admin_password=None, admin_username=getpass.getuser(), File "/usr/local/lib/python3.5/getpass.py", line 170, in getuser return pwd.getpwuid(os.getuid())[0] KeyError: 'getpwuid(): uid not found: 1000'

DEBUG: Loaded all modules in 0.717 seconds. (note: there's always an overhead with the first module loaded) DEBUG: Application event 'CommandTable.Loaded' with event data {'command_table': OrderedDict([('network lb probe show', <azure.cli.core.commands.CliCommand object at 0x7f20ef107cf8>), ('storage container lease release', <azure.cli.core.commands.CliCommand object at 0x7f20eeec8cc0>), ('batch account show', <azure.cli.core.commands.CliCommand object at 0x7f20ef65c470>), ('role assignment list', <azure.cli.core.commands.CliCommand object at 0x7f20ef00fe48>), ('storage blob copy start-batch', <azure.cli.core.commands.CliCommand object at 0x7f20eeedd470>), [...] DEBUG: Application event 'CommandParser.Loaded' with event data {'parser': AzCliCommandParser(prog='az', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)} DEBUG: Application event 'CommandTableParams.Loaded' with event data {'command_table': OrderedDict([('network lb probe show', <azure.cli.core.commands.CliCommand object at 0x7f20ef107cf8>), ('storage container lease release', <azure.cli.core.commands.CliCommand object at 0x7f20eeec8cc0>), ('batch account show', <azure.cli.core.commands.CliCommand object at 0x7f20ef65c470>), ('role assignment list', <azure.cli.core.commands.CliCommand object at 0x7f20ef00fe48>), ('storage blob copy start-batch', <azure.cli.core.commands.CliCommand object at 0x7f20eeedd470>), [...] DEBUG: Checking PyPI for modules using query '{'author_email': 'azpycli', 'author': 'Microsoft Corporation'}' DEBUG: Found 24 result(s) DEBUG: Found module that matches 'vm' - {'summary': 'Microsoft Azure Command-Line Tools VM Command Module', '_pypi_ordering': False, 'version': '0.1.1b3', 'name': 'azure-cli-vm'} WARNING: Install the 'vm' module with 'az component update --add vm' ERROR: az: error: argument _command_package: invalid choice: 'vm' (choose from 'network', 'storage', 'batch', 'role', 'keyvault', 'account', 'acs', 'resource', 'policy', 'acr', 'tag', 'sql', 'iot', 'appservice', 'group', 'component', 'context', 'ad', 'redis', 'cloud', 'feature', 'feedback', 'configure', 'container', 'provider', 'login', 'logout', 'taskhelp') usage: az [-h] [--output {json,tsv,list,table,jsonc}] [--verbose] [--debug] [--query JMESPATH] {network,storage,batch,role,keyvault,account,acs,resource,policy,acr,tag,sql,iot,appservice,group,component,context,ad,redis,cloud,feature,feedback,configure,container,provider,login,logout,taskhelp} ...

weinong commented 7 years ago

the same thing also happens when using my own Docker image where the cli is installed through pip and .deb.

derekbekoe commented 7 years ago

Try setting the USER environment variable with -e USER=weinong

The CLI attempts to get the current logged in user. When you do -u in the Docker command, the name is not set. https://docs.python.org/2/library/getpass.html#getpass.getuser

weinong commented 7 years ago

nice. it does the trick. though, can we guard it so that it doesn't simply bail out even when we are not doing any vm stuffs?

derekbekoe commented 7 years ago

Changed the title of the issue to reflect the real issue. Thanks for the feedback and bug report.

getpass.getuser() can throw an exception. The CLI should catch this and not prevent a module from being loaded. Consider wrapping our usage of getpass.getuser() in a central place in the CLI.