Skip to content
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

[ARM] Fix: az bicep install: Use CA bundle environment variables if set #26013

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 4 additions & 7 deletions src/azure-cli/azure/cli/command_modules/resource/_bicep.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import platform
import subprocess
import json
import certifi

from json.decoder import JSONDecodeError
from contextlib import suppress
Expand All @@ -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 (
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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:
Expand All @@ -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"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
zhoxing-ms marked this conversation as resolved.
Show resolved Hide resolved

ensure_bicep_installation(cli_ctx, release_tag="v0.14.85", stdout=False)

Expand Down
Loading