mirror of
https://github.com/prowler-cloud/prowler.git
synced 2026-07-24 13:01:56 +00:00
refactor(image): remove dead code from registry adapter layer
Remove unused detect_registry_type() function and _ECR_PATTERN constant from factory.py, along with their corresponding tests. Move requests import behind TYPE_CHECKING guard in oci_adapter.py and dockerhub_adapter.py since it is only used for type annotations.
This commit is contained in:
@@ -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"
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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")
|
||||
|
||||
Reference in New Issue
Block a user