diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5d9ac748..f0ca1b1f 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,11 +1,11 @@ --- repos: - repo: https://github.com/psf/black - rev: stable + rev: 23.9.1 hooks: - id: black - - repo: https://gitlab.com/pycqa/flake8 - rev: 3.7.9 + - repo: https://github.com/pycqa/flake8 + rev: 6.1.0 hooks: - id: flake8 additional_dependencies: [flake8-bugbear] diff --git a/kube_hunter/modules/discovery/kubectl.py b/kube_hunter/modules/discovery/kubectl.py index 38e52e07..f0a55e81 100644 --- a/kube_hunter/modules/discovery/kubectl.py +++ b/kube_hunter/modules/discovery/kubectl.py @@ -1,3 +1,4 @@ +import json import logging import subprocess @@ -32,14 +33,15 @@ def get_kubectl_binary_version(self): version = None try: # kubectl version --client does not make any connection to the cluster/internet whatsoever. - version_info = subprocess.check_output("kubectl version --client", stderr=subprocess.STDOUT) - if b"GitVersion" in version_info: - # extracting version from kubectl output - version_info = version_info.decode() - start = version_info.find("GitVersion") - version = version_info[start + len("GitVersion':\"") : version_info.find('",', start)] + version_info = subprocess.check_output( + ["kubectl", "version", "--client", "-o", "json"], stderr=subprocess.STDOUT + ) + version_info = json.loads(version_info) + version = version_info["clientVersion"]["gitVersion"] except Exception: logger.debug("Could not find kubectl client") + else: + logger.debug(f"Found kubectl client: {version}") return version def execute(self): diff --git a/tests/discovery/test_kubectl.py b/tests/discovery/test_kubectl.py new file mode 100644 index 00000000..03579bc7 --- /dev/null +++ b/tests/discovery/test_kubectl.py @@ -0,0 +1,13 @@ +# flake8: noqa: E402 + +from kube_hunter.conf import Config, set_config + +set_config(Config()) + +from kube_hunter.modules.discovery.kubectl import KubectlClientDiscovery + + +def test_kubectl_discovery(): + discovery = KubectlClientDiscovery(None) + version = discovery.get_kubectl_binary_version() + assert version is not None