diff --git a/src/azure-cli/azure/cli/command_modules/resource/_bicep.py b/src/azure-cli/azure/cli/command_modules/resource/_bicep.py index 351e561a951..eb1e4096acb 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/_bicep.py +++ b/src/azure-cli/azure/cli/command_modules/resource/_bicep.py @@ -9,7 +9,6 @@ import platform import subprocess import json -import certifi from json.decoder import JSONDecodeError from contextlib import suppress @@ -18,7 +17,6 @@ import requests import semver -from urllib.request import urlopen from knack.log import get_logger from azure.cli.core.api import get_config_dir from azure.cli.core.azclierror import ( @@ -131,10 +129,11 @@ def ensure_bicep_installation(cli_ctx, release_tag=None, target_platform=None, s print(f"Installing Bicep CLI {release_tag}...") else: print("Installing Bicep CLI...") - os.environ.setdefault("CURL_CA_BUNDLE", certifi.where()) - request = urlopen(_get_bicep_download_url(system, release_tag, target_platform=target_platform)) + + download_url = _get_bicep_download_url(system, release_tag, target_platform=target_platform) + response = requests.get(download_url, verify=_requests_verify) with open(installation_path, "wb") as f: - f.write(request.read()) + f.write(response.content) os.chmod(installation_path, os.stat(installation_path).st_mode | stat.S_IXUSR | stat.S_IXGRP | stat.S_IXOTH) @@ -179,7 +178,6 @@ def is_bicepparam_file(file_path): def get_bicep_available_release_tags(): try: - os.environ.setdefault("CURL_CA_BUNDLE", certifi.where()) response = requests.get("https://aka.ms/BicepReleases", verify=_requests_verify) return [release["tag_name"] for release in response.json()] except IOError as err: @@ -188,7 +186,6 @@ def get_bicep_available_release_tags(): def get_bicep_latest_release_tag(): try: - os.environ.setdefault("CURL_CA_BUNDLE", certifi.where()) response = requests.get("https://aka.ms/BicepLatestRelease", verify=_requests_verify) response.raise_for_status() return response.json()["tag_name"] diff --git a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py index 52eaa051f28..ecba94c909f 100644 --- a/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py +++ b/src/azure-cli/azure/cli/command_modules/resource/tests/latest/test_resource_bicep.py @@ -38,11 +38,11 @@ def test_run_bicep_command_raise_error_if_not_installed_and_not_auto_install(sel @mock.patch("os.stat") @mock.patch("io.BufferedWriter") @mock.patch("azure.cli.command_modules.resource._bicep.open") - @mock.patch("azure.cli.command_modules.resource._bicep.urlopen") @mock.patch("os.path.exists") @mock.patch("os.path.dirname") @mock.patch("os.path.isfile") - def test_use_bicep_cli_from_path_false_after_install(self, isfile_stub, dirname_stub, exists_stub, urlopen_stub, open_stub, buffered_writer_stub, stat_stub, chmod_stub): + @mock.patch('requests.request', autospec=True) + def test_use_bicep_cli_from_path_false_after_install(self, isfile_stub, dirname_stub, exists_stub, mock_requests_get, open_stub, buffered_writer_stub, stat_stub, chmod_stub): isfile_stub.return_value = False dirname_stub.return_value = "tmp" exists_stub.return_value = True @@ -54,10 +54,11 @@ def test_use_bicep_cli_from_path_false_after_install(self, isfile_stub, dirname_ chmod_stub.return_value = None - response = mock.Mock() - response.getcode.return_value = 200 - response.read.return_value = b"test" - urlopen_stub.return_value = response + response = mock.MagicMock() + response.headers = {} + response.status_code = 200 + response.content = b"test" + mock_requests_get.return_value = response ensure_bicep_installation(cli_ctx, release_tag="v0.14.85", stdout=False)