diff --git a/prowler/providers/image/lib/registry/dockerhub_adapter.py b/prowler/providers/image/lib/registry/dockerhub_adapter.py index 154adc6331..be7af56599 100644 --- a/prowler/providers/image/lib/registry/dockerhub_adapter.py +++ b/prowler/providers/image/lib/registry/dockerhub_adapter.py @@ -3,8 +3,7 @@ from __future__ import annotations import re - -import requests +from typing import TYPE_CHECKING from prowler.lib.logger import logger from prowler.providers.image.exceptions.exceptions import ( @@ -14,6 +13,9 @@ from prowler.providers.image.exceptions.exceptions import ( ) from prowler.providers.image.lib.registry.base import RegistryAdapter +if TYPE_CHECKING: + import requests + _HUB_API = "https://hub.docker.com" _REGISTRY_HOST = "https://registry-1.docker.io" _AUTH_URL = "https://auth.docker.io/token" diff --git a/prowler/providers/image/lib/registry/factory.py b/prowler/providers/image/lib/registry/factory.py index 3ca380d4ad..5c0134fedd 100644 --- a/prowler/providers/image/lib/registry/factory.py +++ b/prowler/providers/image/lib/registry/factory.py @@ -11,9 +11,6 @@ from prowler.providers.image.lib.registry.oci_adapter import OciRegistryAdapter _DOCKER_HUB_PATTERN = re.compile( r"^(https?://)?(docker\.io|registry-1\.docker\.io)(/|$)", re.IGNORECASE ) -_ECR_PATTERN = re.compile( - r"^(https?://)?\d+\.dkr\.ecr\.[a-z0-9-]+\.amazonaws\.com(/|$)", re.IGNORECASE -) def create_registry_adapter( @@ -41,12 +38,3 @@ def create_registry_adapter( token=token, verify_ssl=verify_ssl, ) - - -def detect_registry_type(registry_url: str) -> str: - """Return a string identifying the detected registry type.""" - if _DOCKER_HUB_PATTERN.search(registry_url): - return "dockerhub" - if _ECR_PATTERN.search(registry_url): - return "ecr" - return "oci" diff --git a/prowler/providers/image/lib/registry/oci_adapter.py b/prowler/providers/image/lib/registry/oci_adapter.py index eb9e8dd546..9b5f97aee8 100644 --- a/prowler/providers/image/lib/registry/oci_adapter.py +++ b/prowler/providers/image/lib/registry/oci_adapter.py @@ -4,9 +4,9 @@ from __future__ import annotations import base64 import re +from typing import TYPE_CHECKING -import requests - +from prowler.lib.logger import logger from prowler.providers.image.exceptions.exceptions import ( ImageRegistryAuthError, ImageRegistryCatalogError, @@ -14,6 +14,9 @@ from prowler.providers.image.exceptions.exceptions import ( ) from prowler.providers.image.lib.registry.base import RegistryAdapter +if TYPE_CHECKING: + import requests + class OciRegistryAdapter(RegistryAdapter): """Adapter for registries implementing OCI Distribution Spec.""" @@ -155,7 +158,7 @@ class OciRegistryAdapter(RegistryAdapter): if decoded.startswith(f"{self.username}:"): return self.username, decoded[len(self.username) + 1 :] except Exception: - pass + logger.debug("Password is not a base64-encoded auth token, using as-is") return self.username, self.password def _authed_request(self, method: str, url: str, **kwargs) -> requests.Response: diff --git a/tests/providers/image/lib/registry/test_factory.py b/tests/providers/image/lib/registry/test_factory.py index 2370f7f85a..74aab82d02 100644 --- a/tests/providers/image/lib/registry/test_factory.py +++ b/tests/providers/image/lib/registry/test_factory.py @@ -1,42 +1,8 @@ from prowler.providers.image.lib.registry.dockerhub_adapter import DockerHubAdapter -from prowler.providers.image.lib.registry.factory import ( - create_registry_adapter, - detect_registry_type, -) +from prowler.providers.image.lib.registry.factory import create_registry_adapter from prowler.providers.image.lib.registry.oci_adapter import OciRegistryAdapter -class TestDetectRegistryType: - def test_generic_oci(self): - assert detect_registry_type("myregistry.io") == "oci" - - def test_generic_oci_with_https(self): - assert detect_registry_type("https://myregistry.io") == "oci" - - def test_docker_hub(self): - assert detect_registry_type("docker.io/myorg") == "dockerhub" - - def test_docker_hub_registry1(self): - assert detect_registry_type("registry-1.docker.io/myorg") == "dockerhub" - - def test_docker_hub_with_https(self): - assert detect_registry_type("https://docker.io/myorg") == "dockerhub" - - def test_ecr(self): - assert ( - detect_registry_type("123456789.dkr.ecr.us-east-1.amazonaws.com") == "ecr" - ) - - def test_ecr_with_https(self): - assert ( - detect_registry_type("https://123456789.dkr.ecr.us-east-1.amazonaws.com") - == "ecr" - ) - - def test_harbor(self): - assert detect_registry_type("harbor.example.com") == "oci" - - class TestCreateRegistryAdapter: def test_docker_hub_returns_dockerhub_adapter(self): adapter = create_registry_adapter("docker.io/myorg")