[show] replace shell=True, replace xml by lxml, replace exit by sys.exit#2666
[show] replace shell=True, replace xml by lxml, replace exit by sys.exit#2666maipbui merged 15 commits intosonic-net:masterfrom
Conversation
Signed-off-by: maipbui <maibui@microsoft.com>
show/main.py
Outdated
| if route_map_name is not None: | ||
| cmd += ' {}'.format(route_map_name) | ||
| cmd += '"' | ||
| cmd += [str(route_map_name)] |
There was a problem hiding this comment.
Could you make sure this is tested?
There was a problem hiding this comment.
To simplify:
cmd[-1] += ' {}'.format(route_map_name)
show/main.py
Outdated
| if prefix_list_name is not None: | ||
| cmd += ' {}'.format(prefix_list_name) | ||
| cmd += '"' | ||
| cmd += [str(prefix_list_name)] |
show/main.py
Outdated
| if prefix_list_name is not None: | ||
| cmd += ' {}'.format(prefix_list_name) | ||
| cmd += '"' | ||
| cmd += [str(prefix_list_name)] |
show/main.py
Outdated
| cmd2 = ['tail', "-" + str(lines)] | ||
|
|
||
| run_command(cmd, display_cmd=verbose) | ||
| if cmd1 is None and cmd2 is None: |
There was a problem hiding this comment.
You implemented a 2x2 conditions with 4 branches. This is not scalable if number of cmd increase. For example 10 cmd.
You can construct an array first, and feed the expanded array to function call. ref: https://stackoverflow.com/a/7745986/2514803 #Closed
There was a problem hiding this comment.
or cmdn could be defined as array elements from the beginning.
show/main.py
Outdated
| cmd0 = ['sudo', 'docker', 'ps'] | ||
| cmd1 = ['grep', 'bgp'] | ||
| cmd2 = ['awk', '{print$2}'] | ||
| cmd3 = ['cut', '-d', '-', '-f3'] |
show/main.py
Outdated
| cmd1 = ['grep', 'bgp'] | ||
| cmd2 = ['awk', '{print$2}'] | ||
| cmd3 = ['cut', '-d', '-', '-f3'] | ||
| cmd4 = ['cut', '-d', ':', "-f1"] |
show/plugins/barefoot.py
Outdated
| r'-maxdepth 1 -type d -name install_\*_profile ' + opts + '| sed ' | ||
| r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%', shell=True) | ||
| cmd0 = ['docker', 'exec', '-it', 'syncd', 'find', '/opt/bfn', '-mindepth', '1',\ | ||
| r'-maxdepth', '1', '-type', 'd', '-name', 'install_\*_profile', r'{}' % opts] |
There was a problem hiding this comment.
r'' is to bypass escaping special characters. Using opts directly will escape special characters.
There was a problem hiding this comment.
Could you be more specific?, r is applied to {} string only.
There was a problem hiding this comment.
for example, r'\n' will treated the string containing 2 characters: backslash \ and letter n. But '\n' will be treated as a newline character.
There was a problem hiding this comment.
fixed, raised another PR for barefoot https://github.com/sonic-net/sonic-utilities/pull/2699/files
| from sonic_py_common.general import getstatusoutput_noshell_pipe, check_output_pipe | ||
|
|
||
| @click.group() | ||
| def barefoot(): |
This file need to reviewed by Mellanox. Suggest further split into a smaller PR. In reply to: 1426293827 In reply to: 1426293827 In reply to: 1426293827 In reply to: 1426293827 In reply to: 1426293827 Refers to: show/plugins/mlnx.py:22 in 4d96b6e. [](commit_id = 4d96b6e, deletion_comment = False) |
show/main.py
Outdated
| if rc != 0: | ||
| sys.exit(rc) | ||
|
|
||
| def run_command_pipe(*args, display_cmd=False, return_cmd=False): |
Use string array instead of string for In reply to: 1426311506 In reply to: 1426311506 Refers to: show/platform.py:111 in 4d96b6e. [](commit_id = 4d96b6e, deletion_comment = False) |
Signed-off-by: maipbui <maibui@microsoft.com>
Signed-off-by: maipbui <maibui@microsoft.com>
Signed-off-by: maipbui <maibui@microsoft.com>
show/plugins/barefoot.py
Outdated
| r'-maxdepth 1 -type d -name install_\*_profile ' + opts + '| sed ' | ||
| r's%/opt/bfn/install_\\\(.\*\\\)_profile%\\1%', shell=True) | ||
| cmd0 = ['docker', 'exec', '-it', 'syncd', 'find', '/opt/bfn', '-mindepth', '1',\ | ||
| r'-maxdepth', '1', '-type', 'd', '-name', 'install_\*_profile', r'{}' % opts] |
Signed-off-by: maipbui <maibui@microsoft.com>
|
/azp run Azure.sonic-utilities |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Signed-off-by: Mai Bui <maibui@microsoft.com>
show/main.py
Outdated
| def logging(process, lines, follow, verbose): | ||
| """Show system log""" | ||
| if process and not re.match(r'^[a-zA-Z0-9\s]+$', process): | ||
| sys.exit('Process contains only number, alphabet, and whitespace.') |
There was a problem hiding this comment.
Offline discussed, this check is not accurate. Let's revert back, and please use another future PR to focus on this fix.
Signed-off-by: Mai Bui <maibui@microsoft.com>
Update sonic-utilities submodule pointer to include the following: * 5c9b217 Fix issue: out of range sflow polling interval is accepted and stored in config_db ([sonic-net#2847](sonic-net/sonic-utilities#2847)) * 72ca484 Add CLI configuration options for teamd retry count feature ([sonic-net#2642](sonic-net/sonic-utilities#2642)) * 359dfc0 [Clock] Implement clock CLI ([sonic-net#2793](sonic-net/sonic-utilities#2793)) * b316fc2 Add transceiver status CLI to show output from TRANSCEIVER_STATUS table ([sonic-net#2772](sonic-net/sonic-utilities#2772)) * dc59dbd Replace pickle by json ([sonic-net#2849](sonic-net/sonic-utilities#2849)) * a66f41c [show] replace shell=True, replace xml by lxml, replace exit by sys.exit ([sonic-net#2666](sonic-net/sonic-utilities#2666)) * 5750057 [utilities_common] replace shell=True ([sonic-net#2718](sonic-net/sonic-utilities#2718)) * 6e0ee3e [CRM][DASH] Extend CRM utility to support DASH resources. ([sonic-net#2800](sonic-net/sonic-utilities#2800)) * b2c29b0 [config] Generate sysinfo in single asic ([sonic-net#2856](sonic-net/sonic-utilities#2856)) Signed-off-by: dprital <drorp@nvidia.com>
…nic-utilities submodule on master (#15193) Dependency: sonic-net/sonic-utilities#2718 Why I did it This PR sonic-net/sonic-utilities#2718 reduce shell=True usage in utilities_common.cli.run_command() function. Work item tracking Microsoft ADO (number only): 15022050 How I did it Replace strings commands using utilities_common.cli.run_command() function to list of strings due to circular dependency, advance sonic-utilities submodule 72ca484 (HEAD -> master, upstream/master, upstream/HEAD) Add CLI configuration options for teamd retry count feature (sonic-net/sonic-utilities#2642) 359dfc0 [Clock] Implement clock CLI (sonic-net/sonic-utilities#2793) b316fc2 Add transceiver status CLI to show output from TRANSCEIVER_STATUS table (sonic-net/sonic-utilities#2772) dc59dbd Replace pickle by json (sonic-net/sonic-utilities#2849) a66f41c [show] replace shell=True, replace xml by lxml, replace exit by sys.exit (sonic-net/sonic-utilities#2666) 5750057 [utilities_common] replace shell=True (sonic-net/sonic-utilities#2718) 6e0ee3e [CRM][DASH] Extend CRM utility to support DASH resources. (sonic-net/sonic-utilities#2800) b2c29b0 [config] Generate sysinfo in single asic (sonic-net/sonic-utilities#2856)
…xit (sonic-net#2666) #### What I did `subprocess()` - when using with `shell=True` is dangerous. Using subprocess function without a static string can lead to command injection. `sys.exit` is better than `exit`, considered good to use in production code. Ref: https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used #### How I did it `subprocess()` - use `shell=False` instead, use list of strings Ref: [https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigation](https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigation) Replace `exit()` by `sys.exit()` #### How to verify it Pass UT Manual test Signed-off-by: Mai Bui <maibui@microsoft.com>
…nic-utilities submodule on master (sonic-net#15193) Dependency: sonic-net/sonic-utilities#2718 Why I did it This PR sonic-net/sonic-utilities#2718 reduce shell=True usage in utilities_common.cli.run_command() function. Work item tracking Microsoft ADO (number only): 15022050 How I did it Replace strings commands using utilities_common.cli.run_command() function to list of strings due to circular dependency, advance sonic-utilities submodule 72ca484 (HEAD -> master, upstream/master, upstream/HEAD) Add CLI configuration options for teamd retry count feature (sonic-net/sonic-utilities#2642) 359dfc0 [Clock] Implement clock CLI (sonic-net/sonic-utilities#2793) b316fc2 Add transceiver status CLI to show output from TRANSCEIVER_STATUS table (sonic-net/sonic-utilities#2772) dc59dbd Replace pickle by json (sonic-net/sonic-utilities#2849) a66f41c [show] replace shell=True, replace xml by lxml, replace exit by sys.exit (sonic-net/sonic-utilities#2666) 5750057 [utilities_common] replace shell=True (sonic-net/sonic-utilities#2718) 6e0ee3e [CRM][DASH] Extend CRM utility to support DASH resources. (sonic-net/sonic-utilities#2800) b2c29b0 [config] Generate sysinfo in single asic (sonic-net/sonic-utilities#2856)
Signed-off-by: maipbui maibui@microsoft.com
What I did
subprocess()- when using withshell=Trueis dangerous. Using subprocess function without a static string can lead to command injection.sys.exitis better thanexit, considered good to use in production code.Ref:
https://stackoverflow.com/questions/6501121/difference-between-exit-and-sys-exit-in-python
https://stackoverflow.com/questions/19747371/python-exit-commands-why-so-many-and-when-should-each-be-used
How I did it
subprocess()- useshell=Falseinstead, use list of strings Ref: https://semgrep.dev/docs/cheat-sheets/python-command-injection/#mitigationReplace
exit()bysys.exit()How to verify it
Pass UT
Manual test
Previous command output (if the output of a command-line utility has changed)
New command output (if the output of a command-line utility has changed)