fix: async subprocess + httpx — no more event loop blocking during scans
This commit is contained in:
@@ -1,25 +1,14 @@
|
||||
"""Sonatype Nexus REST API client."""
|
||||
"""Sonatype Nexus REST API client using httpx async."""
|
||||
|
||||
import hashlib
|
||||
import os
|
||||
import subprocess
|
||||
|
||||
import httpx
|
||||
|
||||
from guarddog_nexus.config import config
|
||||
from guarddog_nexus.logging_setup import log
|
||||
|
||||
SUPPORTED_EXTENSIONS = (".tar.gz", ".tgz", ".whl", ".zip")
|
||||
PACKAGE_FILE_PATTERNS = ("packages/",)
|
||||
|
||||
|
||||
def get_ecosystem_from_format(fmt: str) -> str | None:
|
||||
mapping = {
|
||||
"pypi": "pypi",
|
||||
"npm": "npm",
|
||||
"rubygems": "rubygems",
|
||||
"go": "go",
|
||||
"raw": None,
|
||||
}
|
||||
return mapping.get(fmt.lower() if fmt else "")
|
||||
|
||||
|
||||
def extract_pypi_info(asset_path: str) -> tuple[str, str] | None:
|
||||
@@ -33,31 +22,28 @@ def extract_pypi_info(asset_path: str) -> tuple[str, str] | None:
|
||||
return None
|
||||
|
||||
|
||||
def download_asset(download_url: str, dest_dir: str) -> str | None:
|
||||
"""Download an asset from Nexus using curl (available in Docker)."""
|
||||
async def download_asset(download_url: str, dest_dir: str) -> str | None:
|
||||
"""Download an asset from Nexus using async httpx."""
|
||||
dest_path = os.path.join(dest_dir, os.path.basename(download_url.split("?")[0]))
|
||||
try:
|
||||
result = subprocess.run(
|
||||
[
|
||||
"curl",
|
||||
"-sfSL",
|
||||
"-u",
|
||||
f"{config.nexus_username}:{config.nexus_password}",
|
||||
"-o",
|
||||
dest_path,
|
||||
download_url,
|
||||
],
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=120,
|
||||
)
|
||||
if result.returncode != 0:
|
||||
log.warning("Failed to download %s: %s", download_url, result.stderr)
|
||||
|
||||
auth = httpx.BasicAuth(config.nexus_username, config.nexus_password)
|
||||
async with httpx.AsyncClient(auth=auth, timeout=120, follow_redirects=True) as client:
|
||||
try:
|
||||
response = await client.get(download_url)
|
||||
response.raise_for_status()
|
||||
with open(dest_path, "wb") as f:
|
||||
f.write(response.content)
|
||||
return dest_path
|
||||
except Exception as e:
|
||||
log.warning("Failed to download %s: %s", download_url, e)
|
||||
return None
|
||||
return dest_path
|
||||
except Exception as e:
|
||||
log.error("Download error for %s: %s", download_url, e)
|
||||
return None
|
||||
|
||||
|
||||
async def nexus_get(path: str) -> httpx.Response:
|
||||
"""Make an authenticated GET request to Nexus REST API."""
|
||||
auth = httpx.BasicAuth(config.nexus_username, config.nexus_password)
|
||||
async with httpx.AsyncClient(auth=auth, timeout=30) as client:
|
||||
return await client.get(f"{config.nexus_url.rstrip('/')}{path}")
|
||||
|
||||
|
||||
def compute_sha256(filepath: str) -> str:
|
||||
|
||||
Reference in New Issue
Block a user