-
Notifications
You must be signed in to change notification settings - Fork 584
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add a new dependency on Kubernetes package * Add and store a new flag about automatic nodes discovery from a pod * Implement the listing of nodes * Add tests to cover the k8s node listing * Fix the k8s listing test to ensure the load incluster function is actually called * Add more help to the k8s node discovery flags, and cross-reference them. * Add a note on the Kubernetes auto-discovery in the main README file * Move the kubernetes discovery from conf to modules/discovery * When running with --pods, run the Kubernetes auto discovery * Also mention that the auto discovery is always on when using --pod Co-authored-by: Mikolaj Pawlikowski <[email protected]>
- Loading branch information
Showing
8 changed files
with
97 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import logging | ||
import kubernetes | ||
|
||
|
||
def list_all_k8s_cluster_nodes(kube_config=None, client=None): | ||
logger = logging.getLogger(__name__) | ||
try: | ||
if kube_config: | ||
logger.info("Attempting to use kubeconfig file: %s", kube_config) | ||
kubernetes.config.load_kube_config(config_file=kube_config) | ||
else: | ||
logger.info("Attempting to use in cluster Kubernetes config") | ||
kubernetes.config.load_incluster_config() | ||
except kubernetes.config.config_exception.ConfigException: | ||
logger.exception("Failed to initiate Kubernetes client") | ||
return | ||
|
||
try: | ||
if client is None: | ||
client = kubernetes.client.CoreV1Api() | ||
ret = client.list_node(watch=False) | ||
logger.info("Listed %d nodes in the cluster" % len(ret.items)) | ||
for item in ret.items: | ||
for addr in item.status.addresses: | ||
yield addr.address | ||
except: | ||
logger.exception("Failed to list nodes from Kubernetes") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from kube_hunter.conf import Config, set_config | ||
|
||
set_config(Config()) | ||
|
||
from kube_hunter.modules.discovery.kubernetes_client import list_all_k8s_cluster_nodes | ||
from unittest.mock import MagicMock, patch | ||
|
||
|
||
|
||
def test_client_yields_ips(): | ||
client = MagicMock() | ||
response = MagicMock() | ||
client.list_node.return_value = response | ||
response.items = [MagicMock(), MagicMock()] | ||
response.items[0].status.addresses = [MagicMock(), MagicMock()] | ||
response.items[0].status.addresses[0].address = "127.0.0.1" | ||
response.items[0].status.addresses[1].address = "127.0.0.2" | ||
response.items[1].status.addresses = [MagicMock()] | ||
response.items[1].status.addresses[0].address = "127.0.0.3" | ||
|
||
with patch('kubernetes.config.load_incluster_config') as m: | ||
output = list(list_all_k8s_cluster_nodes(client=client)) | ||
m.assert_called_once() | ||
|
||
assert output == ["127.0.0.1", "127.0.0.2", "127.0.0.3"] | ||
|
||
|
||
def test_client_uses_kubeconfig(): | ||
with patch('kubernetes.config.load_kube_config') as m: | ||
list(list_all_k8s_cluster_nodes(kube_config="/location", client=MagicMock())) | ||
m.assert_called_once_with(config_file="/location") |