Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:kubectl discovery #543

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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]
14 changes: 8 additions & 6 deletions kube_hunter/modules/discovery/kubectl.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import logging
import subprocess

Expand Down Expand Up @@ -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):
Expand Down
13 changes: 13 additions & 0 deletions tests/discovery/test_kubectl.py
Original file line number Diff line number Diff line change
@@ -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