-
Notifications
You must be signed in to change notification settings - Fork 3.4k
[ACR] az acr login: harden binary resolution and credential passing #33373
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lizMSFT
wants to merge
1
commit into
Azure:dev
Choose a base branch
from
lizMSFT:zoeyli/acr/fix_docker_path_validation
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+250
−44
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
138 changes: 138 additions & 0 deletions
138
src/azure-cli/azure/cli/command_modules/acr/tests/latest/test_acr_docker_path_validation.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,138 @@ | ||
| # -------------------------------------------------------------------------------------------- | ||
| # Copyright (c) Microsoft Corporation. All rights reserved. | ||
| # Licensed under the MIT License. See License.txt in the project root for license information. | ||
| # -------------------------------------------------------------------------------------------- | ||
|
|
||
| import os | ||
| import unittest | ||
| from unittest import mock | ||
|
|
||
| from azure.cli.core.azclierror import CLIError | ||
| from azure.cli.command_modules.acr.custom import _validate_command_path | ||
|
|
||
|
|
||
| class TestValidateCommandPath(unittest.TestCase): | ||
| """Tests for _validate_command_path CWD validation logic.""" | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_binary_in_cwd_is_blocked(self, mock_getcwd, mock_which): | ||
| """A docker binary located directly in CWD should be rejected.""" | ||
| mock_getcwd.return_value = '/home/user/repo' | ||
| mock_which.return_value = '/home/user/repo/docker' | ||
|
|
||
| with self.assertRaises(CLIError) as ctx: | ||
| _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertIn('Refusing to use', str(ctx.exception)) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_binary_in_system_path_is_allowed(self, mock_getcwd, mock_which): | ||
| """A docker binary in a system directory should be allowed.""" | ||
| mock_getcwd.return_value = '/home/user/repo' | ||
| mock_which.return_value = '/usr/bin/docker' | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertFalse(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_binary_in_subdirectory_of_cwd_is_allowed(self, mock_getcwd, mock_which): | ||
| """A docker binary in a subdirectory of CWD should be allowed (only direct CWD is blocked).""" | ||
| mock_getcwd.return_value = '/home/user/repo' | ||
| mock_which.return_value = '/home/user/repo/tools/docker' | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertFalse(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_binary_at_root_cwd_is_allowed(self, mock_getcwd, mock_which): | ||
| """When CWD is filesystem root, system docker should not be blocked.""" | ||
| mock_getcwd.return_value = '/' | ||
| mock_which.return_value = '/usr/bin/docker' | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertFalse(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| def test_binary_not_found_is_allowed(self, mock_which): | ||
| """When docker is not found at all, validation should pass (let downstream handle it).""" | ||
| mock_which.return_value = None | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertFalse(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_diagnostics_mode_returns_true_on_unsafe(self, mock_getcwd, mock_which): | ||
| """In diagnostics mode, should return True (unsafe) instead of raising.""" | ||
| mock_getcwd.return_value = '/home/user/repo' | ||
| mock_which.return_value = '/home/user/repo/docker' | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=True) | ||
| self.assertTrue(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_diagnostics_mode_returns_false_on_safe(self, mock_getcwd, mock_which): | ||
| """In diagnostics mode, should return False (safe) for system-path docker.""" | ||
| mock_getcwd.return_value = '/home/user/repo' | ||
| mock_which.return_value = '/usr/bin/docker' | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=True) | ||
| self.assertFalse(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_windows_cwd_binary_is_blocked(self, mock_getcwd, mock_which): | ||
| """On Windows, a docker.exe in CWD should be blocked (case-insensitive).""" | ||
| mock_getcwd.return_value = 'C:\\Users\\dev\\repo' | ||
| mock_which.return_value = 'C:\\Users\\dev\\repo\\docker.exe' | ||
|
|
||
| with self.assertRaises(CLIError): | ||
| _validate_command_path('docker', is_diagnostics_context=False) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_windows_system_docker_is_allowed(self, mock_getcwd, mock_which): | ||
| """On Windows, Docker Desktop in Program Files should be allowed.""" | ||
| mock_getcwd.return_value = 'C:\\Users\\dev\\repo' | ||
| mock_which.return_value = 'C:\\Program Files\\Docker\\docker.exe' | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertFalse(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_windows_root_cwd_does_not_block_system_docker(self, mock_getcwd, mock_which): | ||
| """When CWD is C:\\, Docker at C:\\Program Files should not be blocked.""" | ||
| mock_getcwd.return_value = 'C:\\' | ||
| mock_which.return_value = 'C:\\Program Files\\Docker\\docker.exe' | ||
|
|
||
| result = _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertFalse(result) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_error_message_includes_override_instruction(self, mock_getcwd, mock_which): | ||
| """Error message should tell user how to override with DOCKER_COMMAND.""" | ||
| mock_getcwd.return_value = '/home/user/repo' | ||
| mock_which.return_value = '/home/user/repo/docker' | ||
|
|
||
| with self.assertRaises(CLIError) as ctx: | ||
| _validate_command_path('docker', is_diagnostics_context=False) | ||
| self.assertIn('DOCKER_COMMAND', str(ctx.exception)) | ||
| self.assertIn('absolute path', str(ctx.exception)) | ||
|
|
||
| @mock.patch('azure.cli.command_modules.acr.custom.os.path.realpath') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.shutil.which') | ||
| @mock.patch('azure.cli.command_modules.acr.custom.os.getcwd') | ||
| def test_symlink_in_cwd_pointing_to_system_binary_is_blocked(self, mock_getcwd, mock_which, mock_realpath): | ||
| """A symlink in CWD pointing to /usr/bin/docker should still be blocked (uses abspath, not realpath).""" | ||
| mock_getcwd.return_value = '/home/user/repo' | ||
| mock_which.return_value = '/home/user/repo/docker' | ||
| mock_realpath.return_value = '/usr/bin/docker' | ||
|
|
||
| with self.assertRaises(CLIError): | ||
| _validate_command_path('docker', is_diagnostics_context=False) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.