From d7df38fc95401623751e28d9a7fe502fde525f4c Mon Sep 17 00:00:00 2001 From: danielsagi Date: Thu, 12 May 2022 22:12:31 +0300 Subject: [PATCH] Fix: Removed automatic import of handler object (#506) * removed automatic import of handler object in events package and renamed handler.py to event_handler.py to solve name collision --- kube_hunter/README.md | 6 +++--- kube_hunter/__main__.py | 2 +- kube_hunter/core/events/__init__.py | 1 - kube_hunter/core/events/{handler.py => event_handler.py} | 0 kube_hunter/core/types/hunters.py | 2 +- kube_hunter/modules/discovery/apiserver.py | 2 +- kube_hunter/modules/discovery/dashboard.py | 2 +- kube_hunter/modules/discovery/etcd.py | 2 +- kube_hunter/modules/discovery/hosts.py | 2 +- kube_hunter/modules/discovery/kubectl.py | 2 +- kube_hunter/modules/discovery/kubelet.py | 2 +- kube_hunter/modules/discovery/ports.py | 2 +- kube_hunter/modules/discovery/proxy.py | 2 +- kube_hunter/modules/hunting/aks.py | 2 +- kube_hunter/modules/hunting/apiserver.py | 2 +- kube_hunter/modules/hunting/capabilities.py | 2 +- kube_hunter/modules/hunting/certificates.py | 2 +- kube_hunter/modules/hunting/cves.py | 2 +- kube_hunter/modules/hunting/dashboard.py | 2 +- kube_hunter/modules/hunting/etcd.py | 2 +- kube_hunter/modules/hunting/kubelet.py | 2 +- kube_hunter/modules/hunting/mounts.py | 2 +- kube_hunter/modules/hunting/proxy.py | 2 +- kube_hunter/modules/hunting/secrets.py | 2 +- kube_hunter/modules/report/collector.py | 2 +- tests/core/test_handler.py | 2 +- tests/core/test_subscribe.py | 2 +- tests/discovery/test_apiserver.py | 2 +- tests/discovery/test_hosts.py | 2 +- tests/hunting/test_apiserver_hunter.py | 2 +- tests/hunting/test_certificates.py | 2 +- tests/hunting/test_cvehunting.py | 2 +- tests/hunting/test_kubelet.py | 2 +- 33 files changed, 33 insertions(+), 34 deletions(-) rename kube_hunter/core/events/{handler.py => event_handler.py} (100%) diff --git a/kube_hunter/README.md b/kube_hunter/README.md index 8c4b70f..de5fa0c 100644 --- a/kube_hunter/README.md +++ b/kube_hunter/README.md @@ -76,7 +76,7 @@ in order to prevent circular dependency bug. Following the above example, let's figure out the imports: ```python from kube_hunter.core.types import Hunter -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import OpenPortEvent @@ -206,7 +206,7 @@ __Make sure to return the event from the execute method, or the event will not g For example, if you don't want to hunt services found on a localhost IP, you can create the following module, in the `kube_hunter/modules/report/` ```python -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Service, EventFilterBase @handler.subscribe(Service) @@ -222,7 +222,7 @@ That means other Hunters that are subscribed to this Service will not get trigge That opens up a wide variety of possible operations, as this not only can __filter out__ events, but you can actually __change event attributes__, for example: ```python -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.types import InformationDisclosure from kube_hunter.core.events.types import Vulnerability, EventFilterBase diff --git a/kube_hunter/__main__.py b/kube_hunter/__main__.py index 9fda820..e091663 100755 --- a/kube_hunter/__main__.py +++ b/kube_hunter/__main__.py @@ -39,7 +39,7 @@ set_config(config) # Running all other registered plugins before execution pm.hook.load_plugin(args=args) -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import HuntFinished, HuntStarted from kube_hunter.modules.discovery.hosts import RunningAsPodEvent, HostScanEvent from kube_hunter.modules.report import get_reporter, get_dispatcher diff --git a/kube_hunter/core/events/__init__.py b/kube_hunter/core/events/__init__.py index 8f98709..a8c20ab 100644 --- a/kube_hunter/core/events/__init__.py +++ b/kube_hunter/core/events/__init__.py @@ -1,3 +1,2 @@ # flake8: noqa: E402 -from .handler import EventQueue, handler from . import types diff --git a/kube_hunter/core/events/handler.py b/kube_hunter/core/events/event_handler.py similarity index 100% rename from kube_hunter/core/events/handler.py rename to kube_hunter/core/events/event_handler.py diff --git a/kube_hunter/core/types/hunters.py b/kube_hunter/core/types/hunters.py index 0d7e410..ede3c13 100644 --- a/kube_hunter/core/types/hunters.py +++ b/kube_hunter/core/types/hunters.py @@ -19,7 +19,7 @@ class HunterBase: def publish_event(self, event): # Import here to avoid circular import from events package. # imports are cached in python so this should not affect runtime - from ..events import handler # noqa + from ..events.event_handler import handler # noqa handler.publish_event(event, caller=self) diff --git a/kube_hunter/modules/discovery/apiserver.py b/kube_hunter/modules/discovery/apiserver.py index af9f4e1..729a55d 100644 --- a/kube_hunter/modules/discovery/apiserver.py +++ b/kube_hunter/modules/discovery/apiserver.py @@ -2,7 +2,7 @@ import logging import requests from kube_hunter.core.types import Discovery -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import OpenPortEvent, Service, Event, EventFilterBase from kube_hunter.conf import get_config diff --git a/kube_hunter/modules/discovery/dashboard.py b/kube_hunter/modules/discovery/dashboard.py index ac7e610..bdbc400 100644 --- a/kube_hunter/modules/discovery/dashboard.py +++ b/kube_hunter/modules/discovery/dashboard.py @@ -3,7 +3,7 @@ import logging import requests from kube_hunter.conf import get_config -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Event, OpenPortEvent, Service from kube_hunter.core.types import Discovery diff --git a/kube_hunter/modules/discovery/etcd.py b/kube_hunter/modules/discovery/etcd.py index 291e09b..d96d396 100644 --- a/kube_hunter/modules/discovery/etcd.py +++ b/kube_hunter/modules/discovery/etcd.py @@ -1,4 +1,4 @@ -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Event, OpenPortEvent, Service from kube_hunter.core.types import Discovery diff --git a/kube_hunter/modules/discovery/hosts.py b/kube_hunter/modules/discovery/hosts.py index 760211e..86c4a48 100644 --- a/kube_hunter/modules/discovery/hosts.py +++ b/kube_hunter/modules/discovery/hosts.py @@ -9,7 +9,7 @@ from netifaces import AF_INET, ifaddresses, interfaces, gateways from kube_hunter.conf import get_config from kube_hunter.modules.discovery.kubernetes_client import list_all_k8s_cluster_nodes -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Event, NewHostEvent, Vulnerability from kube_hunter.core.types import Discovery, AWS, Azure, InstanceMetadataApiTechnique diff --git a/kube_hunter/modules/discovery/kubectl.py b/kube_hunter/modules/discovery/kubectl.py index 5415b1c..38e52e0 100644 --- a/kube_hunter/modules/discovery/kubectl.py +++ b/kube_hunter/modules/discovery/kubectl.py @@ -2,7 +2,7 @@ import logging import subprocess from kube_hunter.core.types import Discovery -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import HuntStarted, Event logger = logging.getLogger(__name__) diff --git a/kube_hunter/modules/discovery/kubelet.py b/kube_hunter/modules/discovery/kubelet.py index 9b9ac1a..e790899 100644 --- a/kube_hunter/modules/discovery/kubelet.py +++ b/kube_hunter/modules/discovery/kubelet.py @@ -5,7 +5,7 @@ from enum import Enum from kube_hunter.conf import get_config from kube_hunter.core.types import Discovery -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import OpenPortEvent, Event, Service urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) diff --git a/kube_hunter/modules/discovery/ports.py b/kube_hunter/modules/discovery/ports.py index c1c355e..db128c9 100644 --- a/kube_hunter/modules/discovery/ports.py +++ b/kube_hunter/modules/discovery/ports.py @@ -2,7 +2,7 @@ import logging from socket import socket from kube_hunter.core.types import Discovery -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import NewHostEvent, OpenPortEvent logger = logging.getLogger(__name__) diff --git a/kube_hunter/modules/discovery/proxy.py b/kube_hunter/modules/discovery/proxy.py index 1b595a4..8394b83 100644 --- a/kube_hunter/modules/discovery/proxy.py +++ b/kube_hunter/modules/discovery/proxy.py @@ -3,7 +3,7 @@ import requests from kube_hunter.conf import get_config from kube_hunter.core.types import Discovery -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Service, Event, OpenPortEvent logger = logging.getLogger(__name__) diff --git a/kube_hunter/modules/hunting/aks.py b/kube_hunter/modules/hunting/aks.py index 6c0d0a9..a0cf9f5 100644 --- a/kube_hunter/modules/hunting/aks.py +++ b/kube_hunter/modules/hunting/aks.py @@ -5,7 +5,7 @@ import requests from kube_hunter.conf import get_config from kube_hunter.modules.hunting.kubelet import ExposedPodsHandler, SecureKubeletPortHunter -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Event, Vulnerability from kube_hunter.core.types import Hunter, ActiveHunter, MountServicePrincipalTechnique, Azure diff --git a/kube_hunter/modules/hunting/apiserver.py b/kube_hunter/modules/hunting/apiserver.py index 8af3dd8..2096678 100644 --- a/kube_hunter/modules/hunting/apiserver.py +++ b/kube_hunter/modules/hunting/apiserver.py @@ -5,7 +5,7 @@ import requests from kube_hunter.conf import get_config from kube_hunter.modules.discovery.apiserver import ApiServer -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Vulnerability, Event, K8sVersionDisclosure from kube_hunter.core.types import Hunter, ActiveHunter, KubernetesCluster from kube_hunter.core.types.vulnerabilities import ( diff --git a/kube_hunter/modules/hunting/capabilities.py b/kube_hunter/modules/hunting/capabilities.py index 656618b..c1ca7d6 100644 --- a/kube_hunter/modules/hunting/capabilities.py +++ b/kube_hunter/modules/hunting/capabilities.py @@ -2,7 +2,7 @@ import socket import logging from kube_hunter.modules.discovery.hosts import RunningAsPodEvent -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Event, Vulnerability from kube_hunter.core.types import Hunter, ARPPoisoningTechnique, KubernetesCluster diff --git a/kube_hunter/modules/hunting/certificates.py b/kube_hunter/modules/hunting/certificates.py index ee60c98..fa8397b 100644 --- a/kube_hunter/modules/hunting/certificates.py +++ b/kube_hunter/modules/hunting/certificates.py @@ -4,7 +4,7 @@ import base64 import re from kube_hunter.core.types import Hunter, KubernetesCluster, GeneralSensitiveInformationTechnique -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Vulnerability, Event, Service logger = logging.getLogger(__name__) diff --git a/kube_hunter/modules/hunting/cves.py b/kube_hunter/modules/hunting/cves.py index b0740a3..137a370 100644 --- a/kube_hunter/modules/hunting/cves.py +++ b/kube_hunter/modules/hunting/cves.py @@ -2,7 +2,7 @@ import logging from packaging import version from kube_hunter.conf import get_config -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import K8sVersionDisclosure, Vulnerability, Event from kube_hunter.core.types import ( diff --git a/kube_hunter/modules/hunting/dashboard.py b/kube_hunter/modules/hunting/dashboard.py index 956e120..445b8cb 100644 --- a/kube_hunter/modules/hunting/dashboard.py +++ b/kube_hunter/modules/hunting/dashboard.py @@ -4,7 +4,7 @@ import requests from kube_hunter.conf import get_config from kube_hunter.core.types import Hunter, AccessK8sDashboardTechnique, KubernetesCluster -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Vulnerability, Event from kube_hunter.modules.discovery.dashboard import KubeDashboardEvent diff --git a/kube_hunter/modules/hunting/etcd.py b/kube_hunter/modules/hunting/etcd.py index f087567..3a4dd6d 100644 --- a/kube_hunter/modules/hunting/etcd.py +++ b/kube_hunter/modules/hunting/etcd.py @@ -2,7 +2,7 @@ import logging import requests from kube_hunter.conf import get_config -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Vulnerability, Event, OpenPortEvent from kube_hunter.core.types import ( ActiveHunter, diff --git a/kube_hunter/modules/hunting/kubelet.py b/kube_hunter/modules/hunting/kubelet.py index c67336c..6f9e8f6 100644 --- a/kube_hunter/modules/hunting/kubelet.py +++ b/kube_hunter/modules/hunting/kubelet.py @@ -9,7 +9,7 @@ import urllib3 import uuid from kube_hunter.conf import get_config -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Vulnerability, Event, K8sVersionDisclosure from kube_hunter.core.types import ( Hunter, diff --git a/kube_hunter/modules/hunting/mounts.py b/kube_hunter/modules/hunting/mounts.py index 8eb42d7..8714699 100644 --- a/kube_hunter/modules/hunting/mounts.py +++ b/kube_hunter/modules/hunting/mounts.py @@ -3,7 +3,7 @@ import re import uuid from kube_hunter.conf import get_config -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Event, Vulnerability from kube_hunter.core.types import ActiveHunter, Hunter, KubernetesCluster, HostPathMountPrivilegeEscalationTechnique from kube_hunter.modules.hunting.kubelet import ( diff --git a/kube_hunter/modules/hunting/proxy.py b/kube_hunter/modules/hunting/proxy.py index 0570d3d..ae417f8 100644 --- a/kube_hunter/modules/hunting/proxy.py +++ b/kube_hunter/modules/hunting/proxy.py @@ -4,7 +4,7 @@ import requests from enum import Enum from kube_hunter.conf import get_config -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Event, Vulnerability, K8sVersionDisclosure from kube_hunter.core.types import ( ActiveHunter, diff --git a/kube_hunter/modules/hunting/secrets.py b/kube_hunter/modules/hunting/secrets.py index 36e09d7..81d34bb 100644 --- a/kube_hunter/modules/hunting/secrets.py +++ b/kube_hunter/modules/hunting/secrets.py @@ -1,7 +1,7 @@ import logging import os -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import Vulnerability, Event from kube_hunter.core.types import Hunter, KubernetesCluster, AccessContainerServiceAccountTechnique from kube_hunter.modules.discovery.hosts import RunningAsPodEvent diff --git a/kube_hunter/modules/report/collector.py b/kube_hunter/modules/report/collector.py index 3843020..f601838 100644 --- a/kube_hunter/modules/report/collector.py +++ b/kube_hunter/modules/report/collector.py @@ -2,7 +2,7 @@ import logging import threading from kube_hunter.conf import get_config -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import ( Event, Service, diff --git a/tests/core/test_handler.py b/tests/core/test_handler.py index 46baab5..3639505 100644 --- a/tests/core/test_handler.py +++ b/tests/core/test_handler.py @@ -4,7 +4,7 @@ from kube_hunter.conf import Config, set_config, get_config set_config(Config(active=True)) -from kube_hunter.core.events.handler import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.modules.discovery.apiserver import ApiServiceDiscovery from kube_hunter.modules.discovery.dashboard import KubeDashboard as KubeDashboardDiscovery from kube_hunter.modules.discovery.etcd import EtcdRemoteAccess as EtcdRemoteAccessDiscovery diff --git a/tests/core/test_subscribe.py b/tests/core/test_subscribe.py index 6dc0fc8..094eaeb 100644 --- a/tests/core/test_subscribe.py +++ b/tests/core/test_subscribe.py @@ -3,7 +3,7 @@ import time from kube_hunter.conf import Config, set_config from kube_hunter.core.types import Hunter from kube_hunter.core.events.types import Event, Service -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler counter = 0 first_run = True diff --git a/tests/discovery/test_apiserver.py b/tests/discovery/test_apiserver.py index dba49ea..83d4e98 100644 --- a/tests/discovery/test_apiserver.py +++ b/tests/discovery/test_apiserver.py @@ -8,7 +8,7 @@ set_config(Config()) from kube_hunter.modules.discovery.apiserver import ApiServer, ApiServiceDiscovery from kube_hunter.core.events.types import Event -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler counter = 0 diff --git a/tests/discovery/test_hosts.py b/tests/discovery/test_hosts.py index c9ce2a2..35fcd14 100644 --- a/tests/discovery/test_hosts.py +++ b/tests/discovery/test_hosts.py @@ -6,7 +6,7 @@ from kube_hunter.modules.discovery.hosts import ( HostDiscoveryHelpers, ) from kube_hunter.core.types import Hunter -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler import json import requests_mock import pytest diff --git a/tests/hunting/test_apiserver_hunter.py b/tests/hunting/test_apiserver_hunter.py index 774d485..ff8c360 100644 --- a/tests/hunting/test_apiserver_hunter.py +++ b/tests/hunting/test_apiserver_hunter.py @@ -23,7 +23,7 @@ from kube_hunter.modules.hunting.apiserver import ApiServerPassiveHunterFinished from kube_hunter.modules.hunting.apiserver import CreateANamespace, DeleteANamespace from kube_hunter.modules.discovery.apiserver import ApiServer from kube_hunter.core.types import ExposedSensitiveInterfacesTechnique, AccessK8sApiServerTechnique -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler counter = 0 diff --git a/tests/hunting/test_certificates.py b/tests/hunting/test_certificates.py index 9697545..2edfdd2 100644 --- a/tests/hunting/test_certificates.py +++ b/tests/hunting/test_certificates.py @@ -5,7 +5,7 @@ set_config(Config()) from kube_hunter.core.events.types import Event from kube_hunter.modules.hunting.certificates import CertificateDiscovery, CertificateEmail -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler def test_CertificateDiscovery(): diff --git a/tests/hunting/test_cvehunting.py b/tests/hunting/test_cvehunting.py index df5047b..1a378ca 100644 --- a/tests/hunting/test_cvehunting.py +++ b/tests/hunting/test_cvehunting.py @@ -5,7 +5,7 @@ from kube_hunter.conf import Config, set_config set_config(Config()) -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.core.events.types import K8sVersionDisclosure from kube_hunter.modules.hunting.cves import ( K8sClusterCveHunter, diff --git a/tests/hunting/test_kubelet.py b/tests/hunting/test_kubelet.py index dcbce44..ed13955 100644 --- a/tests/hunting/test_kubelet.py +++ b/tests/hunting/test_kubelet.py @@ -3,7 +3,7 @@ import requests_mock import urllib.parse import uuid -from kube_hunter.core.events import handler +from kube_hunter.core.events.event_handler import handler from kube_hunter.modules.hunting.kubelet import ( AnonymousAuthEnabled, ExposedExistingPrivilegedContainersViaSecureKubeletPort,