Skip to content

Commit

Permalink
Dashboard hunter not working (#337)
Browse files Browse the repository at this point in the history
* Fix dashboard hunter regression
Fix #336.
Add tests for dashboard hunter

Co-authored-by: Yehuda Chikvashvili <[email protected]>
  • Loading branch information
mormamn and iyehuda authored Apr 13, 2020
1 parent 14d73e2 commit 4cb2c8b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
2 changes: 1 addition & 1 deletion kube_hunter/modules/hunting/dashboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, event):

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"]]

Expand Down
37 changes: 37 additions & 0 deletions tests/hunting/test_dashboard.py
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 4cb2c8b

Please sign in to comment.