From 4cb2c8bad9a4dde42e1746e5050dd02bc6b22bdf Mon Sep 17 00:00:00 2001 From: mormamn <61231073+mormamn@users.noreply.github.com> Date: Mon, 13 Apr 2020 04:06:13 +0300 Subject: [PATCH] Dashboard hunter not working (#337) * Fix dashboard hunter regression Fix #336. Add tests for dashboard hunter Co-authored-by: Yehuda Chikvashvili --- kube_hunter/modules/hunting/dashboard.py | 2 +- tests/hunting/test_dashboard.py | 37 ++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) create mode 100644 tests/hunting/test_dashboard.py diff --git a/kube_hunter/modules/hunting/dashboard.py b/kube_hunter/modules/hunting/dashboard.py index 1a537d8..48213d4 100644 --- a/kube_hunter/modules/hunting/dashboard.py +++ b/kube_hunter/modules/hunting/dashboard.py @@ -32,7 +32,7 @@ class KubeDashboard(Hunter): def get_nodes(self): logger.debug("Passive hunter is attempting to get nodes types of the cluster") - r = requests.get(f"http://{self.event.host}:{self.event.port}/api/v1/node", timeout=config.network_timwout) + r = requests.get(f"http://{self.event.host}:{self.event.port}/api/v1/node", timeout=config.network_timeout) if r.status_code == 200 and "nodes" in r.text: return [node["objectMeta"]["name"] for node in json.loads(r.text)["nodes"]] diff --git a/tests/hunting/test_dashboard.py b/tests/hunting/test_dashboard.py new file mode 100644 index 0000000..160e202 --- /dev/null +++ b/tests/hunting/test_dashboard.py @@ -0,0 +1,37 @@ +import json + +from types import SimpleNamespace +from requests_mock import Mocker +from kube_hunter.modules.hunting.dashboard import KubeDashboard + + +class TestKubeDashboard: + @staticmethod + def get_nodes_mock(result: dict, **kwargs): + with Mocker() as m: + m.get("http://mockdashboard:8000/api/v1/node", text=json.dumps(result), **kwargs) + hunter = KubeDashboard(SimpleNamespace(host="mockdashboard", port=8000)) + return hunter.get_nodes() + + @staticmethod + def test_get_nodes_with_result(): + nodes = {"nodes": [{"objectMeta": {"name": "node1"}}]} + expected = ["node1"] + actual = TestKubeDashboard.get_nodes_mock(nodes) + + assert expected == actual + + @staticmethod + def test_get_nodes_without_result(): + nodes = {"nodes": []} + expected = [] + actual = TestKubeDashboard.get_nodes_mock(nodes) + + assert expected == actual + + @staticmethod + def test_get_nodes_invalid_result(): + expected = None + actual = TestKubeDashboard.get_nodes_mock(dict(), status_code=404) + + assert expected == actual