-
Notifications
You must be signed in to change notification settings - Fork 1
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 #22 from krystianbajno/feature/shared-cache-memory…
…-async feature/shared-cache-memory-async
- Loading branch information
Showing
22 changed files
with
523 additions
and
451 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
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,71 @@ | ||
import threading | ||
from typing import Dict, Callable, Optional | ||
|
||
from services.cache.loaders.github_poc_data_loader import load_github_poc_data | ||
from services.cache.loaders.nist_data_loader import load_nist_data | ||
from services.cache.loaders.cisa_kev_data_loader import load_cisa_kev_data | ||
from services.cache.loaders.trickest_cve_data_loader import load_trickest_cve_data | ||
|
||
class CacheManager: | ||
def __init__(self, config: Dict): | ||
self.config = config | ||
self.cache_data = {} | ||
self.cache_events = {} | ||
self.loading_threads = [] | ||
self.load_caches() | ||
|
||
def load_caches(self): | ||
providers_config = self.config.get('providers', {}) | ||
enrichment_config = self.config.get('enrichment', {}).get('sources', {}) | ||
|
||
provider_loaders = { | ||
'NistCachedAPI': ('nist_cached', load_nist_data), | ||
'CISAKEVAPI': ('cisa_kev', load_cisa_kev_data), | ||
'GitHubCachedAPI': ('github_poc_cached', load_github_poc_data), | ||
} | ||
|
||
enrichment_loaders = { | ||
'nist_cached': load_nist_data, | ||
'cisa_kev': load_cisa_kev_data, | ||
'github_poc_cached': load_github_poc_data, | ||
'trickest_cve_github_cached': load_trickest_cve_data, | ||
} | ||
|
||
loaders_to_use = {} | ||
|
||
for provider_name, enabled in providers_config.items(): | ||
if enabled and provider_name in provider_loaders: | ||
cache_key, loader_func = provider_loaders[provider_name] | ||
loaders_to_use[cache_key] = loader_func | ||
|
||
for source_name, enabled in enrichment_config.items(): | ||
if enabled and source_name in enrichment_loaders: | ||
if source_name not in loaders_to_use: | ||
loaders_to_use[source_name] = enrichment_loaders[source_name] | ||
|
||
for cache_key, loader_func in loaders_to_use.items(): | ||
self.cache_events[cache_key] = threading.Event() | ||
thread = threading.Thread(target=self._load_data, args=(cache_key, loader_func)) | ||
self.loading_threads.append(thread) | ||
thread.start() | ||
|
||
def _load_data(self, name: str, loader_func: Callable): | ||
data = loader_func() | ||
with threading.Lock(): | ||
self.cache_data[name] = data | ||
self.cache_events[name].set() | ||
|
||
def is_data_ready(self, plugin_name: str) -> bool: | ||
return self.cache_events.get(plugin_name, threading.Event()).is_set() | ||
|
||
def wait_for_data(self, plugin_name: str, timeout: Optional[float] = None): | ||
event = self.cache_events.get(plugin_name) | ||
if event: | ||
event.wait(timeout=timeout) | ||
|
||
def get_data(self, plugin_name: str): | ||
return self.cache_data.get(plugin_name) | ||
|
||
def ensure_all_data_loaded(self): | ||
for thread in self.loading_threads: | ||
thread.join() |
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,56 @@ | ||
import os | ||
import time | ||
import json | ||
import httpx | ||
|
||
from terminal.cli import print_greyed_out | ||
|
||
def load_cisa_kev_data(): | ||
cache_dir = 'dataset' | ||
cache_file = os.path.join(cache_dir, 'cisa_kev_cache.json') | ||
cache_duration = 600 # 10 minutes | ||
|
||
if not os.path.exists(cache_dir): | ||
try: | ||
os.makedirs(cache_dir) | ||
print_greyed_out(f"[+] CISA_KEV_DATA_LOADER: Created cache directory at '{cache_dir}'.") | ||
except Exception as e: | ||
print_greyed_out(f"[!] CISA_KEV_DATA_LOADER: Failed to create cache directory '{cache_dir}': {e}") | ||
|
||
def is_cache_valid(): | ||
if os.path.exists(cache_file): | ||
cache_mtime = os.path.getmtime(cache_file) | ||
current_time = time.time() | ||
return (current_time - cache_mtime) < cache_duration | ||
return False | ||
|
||
def download_and_cache(): | ||
try: | ||
print_greyed_out("[*] CISA_KEV_DATA_LOADER: Downloading CISA KEV catalog...") | ||
response = httpx.get( | ||
"https://www.cisa.gov/sites/default/files/feeds/known_exploited_vulnerabilities.json", | ||
timeout=15 | ||
) | ||
if response.status_code == 200: | ||
data = response.json() | ||
with open(cache_file, 'w', encoding='utf-8') as f: | ||
json.dump(data, f, ensure_ascii=False, indent=4) | ||
print_greyed_out("[+] CISA_KEV_DATA_LOADER: CISA KEV catalog downloaded and cached.") | ||
return data | ||
else: | ||
print_greyed_out(f"[!] CISA_KEV_DATA_LOADER: Failed to fetch CISA KEV catalog. Status code: {response.status_code}") | ||
except Exception as e: | ||
print_greyed_out(f"[!] CISA_KEV_DATA_LOADER: Error fetching CISA KEV data: {e}") | ||
return {} | ||
|
||
if is_cache_valid(): | ||
try: | ||
with open(cache_file, 'r', encoding='utf-8') as f: | ||
data = json.load(f) | ||
print_greyed_out("[+] CISA_KEV_DATA_LOADER: Loaded CISA KEV data from cache.") | ||
return data | ||
except Exception as e: | ||
print_greyed_out(f"[!] CISA_KEV_DATA_LOADER: Error reading cache file '{cache_file}': {e}") | ||
return download_and_cache() | ||
else: | ||
return download_and_cache() |
Oops, something went wrong.