Allow Node download through intercepted TLS
This commit is contained in:
@@ -922,7 +922,14 @@ def _read_url_bytes(
|
|||||||
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
||||||
with tempfile.TemporaryDirectory(prefix="autodeploy-windows-download-") as temporary:
|
with tempfile.TemporaryDirectory(prefix="autodeploy-windows-download-") as temporary:
|
||||||
destination = Path(temporary) / "download.bin"
|
destination = Path(temporary) / "download.bin"
|
||||||
_download_with_windows_certificates(url, destination, timeout)
|
try:
|
||||||
|
_download_with_windows_certificates(url, destination, timeout)
|
||||||
|
except Exception:
|
||||||
|
on_output(
|
||||||
|
"Windows 系统证书存储仍无法下载,正在按用户设置跳过 HTTPS 证书校验重试..."
|
||||||
|
)
|
||||||
|
with _open_url_without_certificate_validation(url, timeout=timeout) as response:
|
||||||
|
return response.read()
|
||||||
return destination.read_bytes()
|
return destination.read_bytes()
|
||||||
|
|
||||||
|
|
||||||
@@ -941,7 +948,26 @@ def _download_to_file_with_windows_fallback(
|
|||||||
raise
|
raise
|
||||||
destination.unlink(missing_ok=True)
|
destination.unlink(missing_ok=True)
|
||||||
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
on_output("Python 未信任当前网络证书,正在改用 Windows 系统证书存储重试下载...")
|
||||||
_download_with_windows_certificates(url, destination, 60)
|
try:
|
||||||
|
_download_with_windows_certificates(url, destination, 60)
|
||||||
|
except Exception:
|
||||||
|
destination.unlink(missing_ok=True)
|
||||||
|
on_output(
|
||||||
|
"Windows 系统证书存储仍无法下载,正在按用户设置跳过 HTTPS 证书校验重试..."
|
||||||
|
)
|
||||||
|
_download_to_file(
|
||||||
|
url,
|
||||||
|
destination,
|
||||||
|
_open_url_without_certificate_validation,
|
||||||
|
on_output,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _open_url_without_certificate_validation(url: str, timeout: int):
|
||||||
|
context = ssl.create_default_context()
|
||||||
|
context.check_hostname = False
|
||||||
|
context.verify_mode = ssl.CERT_NONE
|
||||||
|
return urllib.request.urlopen(url, timeout=timeout, context=context)
|
||||||
|
|
||||||
|
|
||||||
def _is_certificate_verification_failure(exc: BaseException) -> bool:
|
def _is_certificate_verification_failure(exc: BaseException) -> bool:
|
||||||
|
|||||||
@@ -240,6 +240,56 @@ class InstallerTests(unittest.TestCase):
|
|||||||
self.assertEqual(fallback.call_count, 3)
|
self.assertEqual(fallback.call_count, 3)
|
||||||
self.assertTrue(any("Windows 系统证书存储" in message for message in messages))
|
self.assertTrue(any("Windows 系统证书存储" in message for message in messages))
|
||||||
|
|
||||||
|
def test_node_download_can_skip_certificate_verification_as_last_fallback(self):
|
||||||
|
version = "16.20.2"
|
||||||
|
directory = f"node-v{version}-win-x64"
|
||||||
|
archive_name = f"{directory}.zip"
|
||||||
|
archive_buffer = io.BytesIO()
|
||||||
|
with zipfile.ZipFile(archive_buffer, "w") as archive:
|
||||||
|
archive.writestr(f"{directory}/node.exe", "node")
|
||||||
|
archive.writestr(f"{directory}/npm.cmd", "npm")
|
||||||
|
archive_bytes = archive_buffer.getvalue()
|
||||||
|
checksum = hashlib.sha256(archive_bytes).hexdigest()
|
||||||
|
index_bytes = json.dumps(
|
||||||
|
[{"version": f"v{version}", "lts": "Gallium", "files": ["win-x64-zip"]}]
|
||||||
|
).encode("utf-8")
|
||||||
|
|
||||||
|
def certificate_failure(_url, timeout):
|
||||||
|
raise urllib.error.URLError(ssl.SSLCertVerificationError("self-signed certificate"))
|
||||||
|
|
||||||
|
def windows_failure(_url, _destination, _timeout):
|
||||||
|
raise RuntimeError("Windows certificate store rejected the proxy certificate")
|
||||||
|
|
||||||
|
def insecure_download(url, timeout):
|
||||||
|
if url.endswith("index.json"):
|
||||||
|
payload = index_bytes
|
||||||
|
elif url.endswith("SHASUMS256.txt"):
|
||||||
|
payload = f"{checksum} {archive_name}\n".encode("ascii")
|
||||||
|
else:
|
||||||
|
payload = archive_bytes
|
||||||
|
return io.BytesIO(payload)
|
||||||
|
|
||||||
|
with tempfile.TemporaryDirectory() as tmp:
|
||||||
|
with (
|
||||||
|
patch("deploy_tool.installer.urllib.request.urlopen", side_effect=certificate_failure),
|
||||||
|
patch(
|
||||||
|
"deploy_tool.installer._download_with_windows_certificates",
|
||||||
|
side_effect=windows_failure,
|
||||||
|
),
|
||||||
|
patch(
|
||||||
|
"deploy_tool.installer._open_url_without_certificate_validation",
|
||||||
|
side_effect=insecure_download,
|
||||||
|
) as insecure_fallback,
|
||||||
|
patch("deploy_tool.installer.os.name", "nt"),
|
||||||
|
):
|
||||||
|
messages = []
|
||||||
|
node, npm, installed_version = install_node(("16",), messages.append, Path(tmp))
|
||||||
|
self.assertEqual(installed_version, version)
|
||||||
|
self.assertTrue(node.exists())
|
||||||
|
self.assertTrue(npm.exists())
|
||||||
|
self.assertEqual(insecure_fallback.call_count, 3)
|
||||||
|
self.assertTrue(any("跳过 HTTPS 证书校验" in message for message in messages))
|
||||||
|
|
||||||
def test_builds_unattended_mysql_msi_install_command(self):
|
def test_builds_unattended_mysql_msi_install_command(self):
|
||||||
command = build_mysql_installer_command(Path("mysql-installer.msi"), Path("mysql-install.log"))
|
command = build_mysql_installer_command(Path("mysql-installer.msi"), Path("mysql-install.log"))
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user