Skip to content

Commit e771218

Browse files
fix(k8s): handle Kubernetes kubeconfig content correctly (#5967)
Co-authored-by: Sergio Garcia <[email protected]>
1 parent c627a3e commit e771218

File tree

1 file changed

+32
-23
lines changed

1 file changed

+32
-23
lines changed

prowler/providers/kubernetes/kubernetes_provider.py

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
from typing import Union
23

34
from colorama import Fore, Style
45
from kubernetes.client.exceptions import ApiException
@@ -74,14 +75,14 @@ def __init__(
7475
fixer_config: dict = {},
7576
mutelist_path: str = None,
7677
mutelist_content: dict = {},
77-
kubeconfig_content: dict = None,
78+
kubeconfig_content: Union[dict, str] = None,
7879
):
7980
"""
8081
Initializes the KubernetesProvider instance.
8182
8283
Args:
8384
kubeconfig_file (str): Path to the kubeconfig file.
84-
kubeconfig_content (dict): Content of the kubeconfig file.
85+
kubeconfig_content (str or dict): Content of the kubeconfig file.
8586
context (str): Context name.
8687
namespace (list): List of namespaces.
8788
config_content (dict): Audit configuration.
@@ -224,15 +225,15 @@ def mutelist(self) -> KubernetesMutelist:
224225
@staticmethod
225226
def setup_session(
226227
kubeconfig_file: str = None,
227-
kubeconfig_content: dict = None,
228+
kubeconfig_content: Union[dict, str] = None,
228229
context: str = None,
229230
) -> KubernetesSession:
230231
"""
231232
Sets up the Kubernetes session.
232233
233234
Args:
234235
kubeconfig_file (str): Path to the kubeconfig file.
235-
kubeconfig_content (dict): Content of the kubeconfig file.
236+
kubeconfig_content (str or dict): Content of the kubeconfig file.
236237
context (str): Context name.
237238
238239
Returns:
@@ -243,14 +244,20 @@ def setup_session(
243244
KubernetesInvalidProviderIdError: If the provider ID is invalid.
244245
KubernetesSetUpSessionError: If an error occurs while setting up the session.
245246
"""
246-
logger.info(f"Using kubeconfig file: {kubeconfig_file}")
247247
try:
248248
if kubeconfig_content:
249-
config.load_kube_config_from_dict(
250-
safe_load(kubeconfig_content), context=context
251-
)
252-
249+
logger.info("Using kubeconfig content...")
250+
config_data = safe_load(kubeconfig_content)
251+
config.load_kube_config_from_dict(config_data, context=context)
252+
if context:
253+
contexts = config_data.get("contexts", [])
254+
for context_item in contexts:
255+
if context_item["name"] == context:
256+
context = context_item
257+
else:
258+
context = config_data.get("contexts", [])[0]
253259
else:
260+
logger.info(f"Using kubeconfig file: {kubeconfig_file}...")
254261
kubeconfig_file = (
255262
kubeconfig_file if kubeconfig_file else "~/.kube/config"
256263
)
@@ -273,17 +280,19 @@ def setup_session(
273280
return KubernetesSession(
274281
api_client=client.ApiClient(), context=context
275282
)
276-
if context:
277-
contexts = config.list_kube_config_contexts(
278-
config_file=kubeconfig_file
279-
)[0]
280-
for context_item in contexts:
281-
if context_item["name"] == context:
282-
context = context_item
283-
else:
284-
context = config.list_kube_config_contexts(config_file=kubeconfig_file)[
285-
1
286-
]
283+
if context:
284+
contexts = config.list_kube_config_contexts(
285+
config_file=kubeconfig_file
286+
)[0]
287+
for context_item in contexts:
288+
if context_item["name"] == context:
289+
context = context_item
290+
else:
291+
# If no context is provided, use the active context in the kubeconfig file
292+
# The first element is the list of contexts, the second is the active context
293+
context = config.list_kube_config_contexts(
294+
config_file=kubeconfig_file
295+
)[1]
287296
return KubernetesSession(api_client=client.ApiClient(), context=context)
288297

289298
except parser.ParserError as parser_error:
@@ -318,7 +327,7 @@ def setup_session(
318327
@staticmethod
319328
def test_connection(
320329
kubeconfig_file: str = "~/.kube/config",
321-
kubeconfig_content: dict = None,
330+
kubeconfig_content: Union[dict, str] = None,
322331
namespace: str = None,
323332
provider_id: str = None,
324333
raise_on_exception: bool = True,
@@ -328,7 +337,7 @@ def test_connection(
328337
329338
Args:
330339
kubeconfig_file (str): Path to the kubeconfig file.
331-
kubeconfig_content (dict): Content of the kubeconfig file.
340+
kubeconfig_content (str or dict): Content of the kubeconfig file.
332341
namespace (str): Namespace name.
333342
provider_id (str): Provider ID to use, in this case, the Kubernetes context.
334343
raise_on_exception (bool): Whether to raise an exception on error.
@@ -352,7 +361,7 @@ def test_connection(
352361
... )
353362
- Using the kubeconfig content:
354363
>>> connection = KubernetesProvider.test_connection(
355-
... kubeconfig_content={"kubecofig": "content"},
364+
... kubeconfig_content="kubeconfig content",
356365
... namespace="default",
357366
... provider_id="my-context",
358367
... raise_on_exception=True,

0 commit comments

Comments
 (0)