-
Notifications
You must be signed in to change notification settings - Fork 130
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from regevnoam1/StaticScan
Static scan
- Loading branch information
Showing
34 changed files
with
10,771 additions
and
11 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"name": "Python: KubiScan", | ||
"type": "debugpy", | ||
"request": "launch", | ||
"program": "${workspaceFolder}/KubiScan.py", | ||
"args": ["-rs","-r"], | ||
"console": "integratedTerminal", | ||
"pythonPath": "python3" | ||
} | ||
] | ||
} |
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
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
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,9 @@ | ||
from abc import ABC, abstractmethod | ||
|
||
|
||
class BaseApiClient(ABC): | ||
|
||
@abstractmethod | ||
def list_roles_for_all_namespaces(self): | ||
pass | ||
|
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,15 @@ | ||
from .static_api_client import StaticApiClient | ||
from .api_client import RegularApiClient | ||
|
||
class ApiClientFactory: | ||
@staticmethod | ||
def get_client(use_static=False, input_file=None): | ||
if use_static: | ||
return StaticApiClient(input_file=input_file) | ||
else: | ||
return RegularApiClient() | ||
|
||
|
||
#api_client = ApiClientFactory.get_client(use_static=True, input_file="/home/noamr/Documents/KubiScan/combined.json") | ||
#api_client = ApiClientFactory.get_client() | ||
#print(api_client.list_roles_for_all_namespaces()) |
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,10 @@ | ||
# config.py | ||
|
||
class Config: | ||
api_client = None | ||
|
||
def set_api_client(client): | ||
Config.api_client = client | ||
|
||
def get_api_client(): | ||
return Config.api_client |
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,61 @@ | ||
import json | ||
import yaml | ||
import os | ||
from .base_client_api import BaseApiClient | ||
|
||
class StaticApiClient(BaseApiClient): | ||
def __init__(self, input_file): | ||
self.combined_data = self._load_combined_file(input_file) | ||
self.all_roles =self.get_resources('Role') | ||
self.all_cluster_roles = self.get_resources('ClusterRole') | ||
self.ll_role_bindings = self.get_resources('RoleBinding') | ||
self.all_cluster_role_bindings = self.get_resources('ClusterRoleBinding') | ||
self.all_secrets =self.get_resources('Secret') | ||
self.all_pods = self.get_resources('Pod') | ||
|
||
def _load_combined_file(self, input_file): | ||
# Determine the file format based on the file extension | ||
_, file_extension = os.path.splitext(input_file) | ||
file_format = 'json' if file_extension.lower() == '.json' else 'yaml' if file_extension.lower() == '.yaml' else None | ||
|
||
if not file_format: | ||
print("Unsupported file extension. Only '.yaml' and '.json' are supported.") | ||
return None | ||
|
||
try: | ||
with open(input_file, 'r') as file: | ||
if file_format == "yaml": | ||
return yaml.safe_load(file) | ||
elif file_format == "json": | ||
return json.load(file) | ||
except FileNotFoundError: | ||
print(f"File not found: {input_file}") | ||
return None | ||
except Exception as e: | ||
print(f"Error reading file: {e}") | ||
return None | ||
|
||
def get_resources(self, kind): | ||
try: | ||
# Initialize an empty list to collect the resources of the specified kind. | ||
resources = [] | ||
|
||
# Since combined_data is a list of dictionaries, each containing an 'items' key. | ||
for entry in self.combined_data: | ||
# Check if 'items' key exists and it contains a list of dictionaries. | ||
if 'items' in entry and isinstance(entry['items'], list): | ||
# Extend the list of resources with those that match the specified 'kind'. | ||
resources.extend(item for item in entry['items'] if item.get('kind') == kind) | ||
return resources | ||
|
||
except TypeError: # Catch type errors if data structures are not as expected | ||
print("Error processing data. Check the structure of the JSON file.") | ||
return [] | ||
|
||
def list_roles_for_all_namespaces(self): | ||
return self.all_roles | ||
|
||
|
||
# Example usage | ||
#static_api_client = StaticApiClient(input_file="C:\\Users\\noamr\\Documents\\GitHub\\KubiScan\\combined.json") | ||
#print(len(static_api_client.all_secrets)) |
Oops, something went wrong.