diff --git a/CHANGELOG.md b/CHANGELOG.md index c1f7efb..4768ae5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,7 +1,9 @@ # Changelog -# develop +# [v1.2.1](https://github.com/carvilsi/caetra/releases/tag/v1.2.1) (2026-07-17) + +- Refactor to import as a module - Adds name of HID for hid_interact shield # [v1.2.0](https://github.com/carvilsi/caetra/releases/tag/v1.2.0) (2026-03-18) diff --git a/caetra.py b/caetra.py index 2ce3a3b..e61bff9 100755 --- a/caetra.py +++ b/caetra.py @@ -3,18 +3,16 @@ import threading import subprocess import os -import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "./src/utils/")) -sys.path.append(os.path.join(os.path.dirname(__file__), "./src/")) - -from logger_setup import logger -from config_parser import config -import constants +from src.utils.logger_setup import logger +from src.utils.config_parser import config +import src.constants as constants def run_script(script_name): - subprocess.run(["python3", script_name]) + module = script_name[2:-3].replace(os.sep, ".") + + subprocess.run(["python3", "-m", module]) # Threading execute all the shields under shield directory @@ -27,6 +25,7 @@ def threading_excute_shields(): file.endswith(".py") and file != constants.SHIELD_DEPLOYING_SCRIPT and file != constants.SHIELD_STATUS_HANDLER_SCRIPT + and file != constants.PYTHON_MODULE_INIT ): shieldname = os.path.splitext(file)[0] if config["caetra"].get("shields_enabled") is not None: diff --git a/src/__init__.py b/src/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/constants.py b/src/constants.py index 98a7dae..3fa2435 100644 --- a/src/constants.py +++ b/src/constants.py @@ -1,6 +1,7 @@ SHIELD_PATH = "./src/shields/" SHIELD_DEPLOYING_SCRIPT = "deploying.py" SHIELD_STATUS_HANDLER_SCRIPT = "status_handler.py" +PYTHON_MODULE_INIT = "__init__.py" SHIELD_CONFIG_EXT = ".toml" CAETRA_SENDER_LABEL = "Shield" diff --git a/src/senders/__init__.py b/src/senders/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/senders/send_canary_dns_token.py b/src/senders/send_canary_dns_token.py index 359c6eb..caa72df 100644 --- a/src/senders/send_canary_dns_token.py +++ b/src/senders/send_canary_dns_token.py @@ -1,12 +1,8 @@ import base64 import socket import re -import os -import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils/")) - -from logger_setup import logger +from src.utils.logger_setup import logger DOT = "." MAGIC_STRING = "G69" diff --git a/src/senders/send_telegram_message_to_chat.py b/src/senders/send_telegram_message_to_chat.py index d430758..989254d 100644 --- a/src/senders/send_telegram_message_to_chat.py +++ b/src/senders/send_telegram_message_to_chat.py @@ -1,10 +1,7 @@ import requests -import sys -import os -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -from logger_setup import logger +from src.utils.logger_setup import logger HEADERS = {"Content-Type": "application/json"} diff --git a/src/senders/senders_handler.py b/src/senders/senders_handler.py index f3eef51..9486068 100644 --- a/src/senders/senders_handler.py +++ b/src/senders/senders_handler.py @@ -1,15 +1,10 @@ -import sys -import os - -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -import constants -from config_parser import config -from logger_setup import logger -from caetra_exceptions import ConfigurationError -from dict_handler import validate_dict_structure -from send_canary_dns_token import send_canary -from send_telegram_message_to_chat import send_telegram +import src.constants as constants +from src.utils.config_parser import config +from src.utils.logger_setup import logger +from src.caetra_exceptions import ConfigurationError +from src.utils.dict_handler import validate_dict_structure +from src.senders.send_canary_dns_token import send_canary +from src.senders.send_telegram_message_to_chat import send_telegram def check_send_config(senders_config): diff --git a/src/shields/__init__.py b/src/shields/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/ambient_light/__init__.py b/src/shields/ambient_light/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/ambient_light/ambient_light.py b/src/shields/ambient_light/ambient_light.py index d3bc2f9..3137489 100644 --- a/src/shields/ambient_light/ambient_light.py +++ b/src/shields/ambient_light/ambient_light.py @@ -1,24 +1,15 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ( +from src.shields.shields_common import ( + constants, + deploying, + send, + status_handler, + logger_shields, + log_shield_exception, + log_shield_exception_warn, ShieldConfigurationError, ConfigurationError, MaxActionReached, ) -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -import constants -import status_handler # shield name # must be same with in toml root config diff --git a/src/shields/blt_connect/__init__.py b/src/shields/blt_connect/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/blt_connect/blt_connect.py b/src/shields/blt_connect/blt_connect.py index 51be7b4..7c01f7b 100644 --- a/src/shields/blt_connect/blt_connect.py +++ b/src/shields/blt_connect/blt_connect.py @@ -1,19 +1,13 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ShieldConfigurationError, ConfigurationError -from logging_handler import log_shield_exception -from senders_handler import send -from format_utils import mac_address_format -import constants +from src.shields.shields_common import ( + constants, + deploying, + log_shield_exception, + logger_shields, + mac_address_format, + send, + ConfigurationError, + ShieldConfigurationError, +) # shield name # must be same with in toml root config diff --git a/src/shields/blt_disconnect/__init__.py b/src/shields/blt_disconnect/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/blt_disconnect/blt_disconnect.py b/src/shields/blt_disconnect/blt_disconnect.py index ab86427..bcc4290 100644 --- a/src/shields/blt_disconnect/blt_disconnect.py +++ b/src/shields/blt_disconnect/blt_disconnect.py @@ -1,19 +1,13 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ShieldConfigurationError, ConfigurationError -from logging_handler import log_shield_exception -from senders_handler import send -from format_utils import mac_address_format -import constants +from src.shields.shields_common import ( + constants, + deploying, + log_shield_exception, + logger_shields, + mac_address_format, + send, + ConfigurationError, + ShieldConfigurationError, +) # shield name # must be same with in toml root config diff --git a/src/shields/cd_dvd_rom/__init__.py b/src/shields/cd_dvd_rom/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/cd_dvd_rom/cd_dvd_rom.py b/src/shields/cd_dvd_rom/cd_dvd_rom.py index 2acfaeb..1616f33 100644 --- a/src/shields/cd_dvd_rom/cd_dvd_rom.py +++ b/src/shields/cd_dvd_rom/cd_dvd_rom.py @@ -1,24 +1,15 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ( - ShieldConfigurationError, +from src.shields.shields_common import ( + constants, + deploying, + log_shield_exception, + log_shield_exception_warn, + logger_shields, + send, + status_handler, ConfigurationError, MaxActionReached, + ShieldConfigurationError, ) -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -import constants -import status_handler # shield name # must be same with in toml root config diff --git a/src/shields/deploying.py b/src/shields/deploying.py index 8d21ae4..2d100ec 100644 --- a/src/shields/deploying.py +++ b/src/shields/deploying.py @@ -1,14 +1,11 @@ from bcc import BPF -from logger_setup import logger import tomllib import os -import sys -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -import constants -from caetra_exceptions import ShieldConfigurationError -from dict_handler import validate_dict_structure +from src.utils.logger_setup import logger +import src.constants as constants +from src.caetra_exceptions import ShieldConfigurationError +from src.utils.dict_handler import validate_dict_structure # returns a BPF program loaded and attached to kernel diff --git a/src/shields/hibernation/__init__.py b/src/shields/hibernation/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/hibernation/hibernation.py b/src/shields/hibernation/hibernation.py index 15ef2db..920cfb4 100644 --- a/src/shields/hibernation/hibernation.py +++ b/src/shields/hibernation/hibernation.py @@ -1,24 +1,14 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ( - ShieldConfigurationError, +from src.shields.shields_common import ( + constants, + deploying, + log_shield_exception, + logger_shields, + send, + status_handler, ConfigurationError, MaxRetriesReached, + ShieldConfigurationError, ) -from logging_handler import log_shield_exception -from senders_handler import send -import constants -import status_handler # shield name # must be same with in toml root config diff --git a/src/shields/hibernation/hibernation.toml b/src/shields/hibernation/hibernation.toml index 5da888a..4eb9fe9 100644 --- a/src/shields/hibernation/hibernation.toml +++ b/src/shields/hibernation/hibernation.toml @@ -8,7 +8,7 @@ # if this shield is enable # TODO: this is giving false positive -enable = false +enable = true # a description that will logged diff --git a/src/shields/hid_add_remove/__init__.py b/src/shields/hid_add_remove/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/hid_add_remove/hid_add_remove.py b/src/shields/hid_add_remove/hid_add_remove.py index a2498a9..d7f1ba2 100644 --- a/src/shields/hid_add_remove/hid_add_remove.py +++ b/src/shields/hid_add_remove/hid_add_remove.py @@ -1,25 +1,16 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ( - ShieldConfigurationError, +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + log_shield_exception_warn, + send, + status_handler, ConfigurationError, MaxActionReached, + ShieldConfigurationError, ShieldKernelSpaceCError, ) -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -import constants -import status_handler # from linux/hid.h HID_TYPE = { diff --git a/src/shields/hid_interact/__init__.py b/src/shields/hid_interact/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/hid_interact/hid_interact.py b/src/shields/hid_interact/hid_interact.py index ed5dbc7..01571c1 100644 --- a/src/shields/hid_interact/hid_interact.py +++ b/src/shields/hid_interact/hid_interact.py @@ -1,24 +1,15 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ( - ShieldConfigurationError, +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + log_shield_exception_warn, + send, + status_handler, ConfigurationError, MaxActionReached, + ShieldConfigurationError, ) -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -import constants -import status_handler # from linux/hid.h HID_TYPE = { diff --git a/src/shields/inet/__init__.py b/src/shields/inet/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/inet/inet.py b/src/shields/inet/inet.py index f88a9ff..3b52cf5 100644 --- a/src/shields/inet/inet.py +++ b/src/shields/inet/inet.py @@ -1,27 +1,18 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ( - ShieldConfigurationError, +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + log_shield_exception_warn, + mac_address_format, + send, + status_handler, ConfigurationError, MaxActionReached, MaxRetriesReached, NoInternetConnection, + ShieldConfigurationError, ) -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -from format_utils import mac_address_format -import constants -import status_handler # from linux/netdevice.h # only interesting events diff --git a/src/shields/input_event/__init__.py b/src/shields/input_event/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/input_event/input_event.py b/src/shields/input_event/input_event.py index 756c7a8..5d5ecd9 100644 --- a/src/shields/input_event/input_event.py +++ b/src/shields/input_event/input_event.py @@ -1,24 +1,15 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ( - ShieldConfigurationError, +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + log_shield_exception_warn, + send, + status_handler, ConfigurationError, MaxActionReached, + ShieldConfigurationError, ) -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -import constants -import status_handler # shield name # must be same with in toml root config diff --git a/src/shields/mmc/__init__.py b/src/shields/mmc/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/mmc/mmc.py b/src/shields/mmc/mmc.py index a771585..35445e5 100755 --- a/src/shields/mmc/mmc.py +++ b/src/shields/mmc/mmc.py @@ -1,18 +1,12 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ShieldConfigurationError, ConfigurationError -from logging_handler import log_shield_exception -from senders_handler import send -import constants +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + send, + ConfigurationError, + ShieldConfigurationError, +) # from linux/mmc/card.h # definition fo Multi Media Cards diff --git a/src/shields/power/__init__.py b/src/shields/power/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/power/power.py b/src/shields/power/power.py index d66090e..c2b3e6a 100644 --- a/src/shields/power/power.py +++ b/src/shields/power/power.py @@ -1,18 +1,12 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ShieldConfigurationError, ConfigurationError -from logging_handler import log_shield_exception -from senders_handler import send -import constants +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + send, + ConfigurationError, + ShieldConfigurationError, +) # from linux/power_supply.h POWER_SUPPLY_TYPE = { diff --git a/src/shields/shields_common.py b/src/shields/shields_common.py new file mode 100644 index 0000000..572deff --- /dev/null +++ b/src/shields/shields_common.py @@ -0,0 +1,45 @@ +""" +Common imports shared by shield scripts. +""" + +# Generic imports +import os +import sys + +# Caetra imports +import src.constants as constants +from src.caetra_exceptions import ( + ConfigurationError, + ShieldConfigurationError, + MaxActionReached, + MaxRetriesReached, + NoInternetConnection, + ShieldKernelSpaceCError, +) +from src.senders.senders_handler import send +from src.shields import deploying, status_handler +from src.utils.logger_setup import logger_shields +from src.utils.logging_handler import ( + log_shield_exception, + log_shield_exception_warn, +) +from src.utils.format_utils import mac_address_format + +__all__ = [ + "os", + "sys", + "constants", + "deploying", + "status_handler", + "logger_shields", + "ShieldConfigurationError", + "ConfigurationError", + "log_shield_exception", + "send", + "MaxActionReached", + "MaxRetriesReached", + "log_shield_exception_warn", + "NoInternetConnection", + "ShieldKernelSpaceCError", + "mac_address_format", +] diff --git a/src/shields/status_handler.py b/src/shields/status_handler.py index a89f5d0..dccc9b5 100644 --- a/src/shields/status_handler.py +++ b/src/shields/status_handler.py @@ -1,13 +1,9 @@ -import sys -import os import threading import requests # caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -import constants -from caetra_exceptions import MaxActionReached, MaxRetriesReached +import src.constants as constants +from src.caetra_exceptions import MaxActionReached, MaxRetriesReached class StatusHandler(object): diff --git a/src/shields/usb/__init__.py b/src/shields/usb/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/usb/usb.py b/src/shields/usb/usb.py index ed98e5d..1612b51 100755 --- a/src/shields/usb/usb.py +++ b/src/shields/usb/usb.py @@ -1,18 +1,12 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ShieldConfigurationError, ConfigurationError -from logging_handler import log_shield_exception -from senders_handler import send -import constants +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + send, + ConfigurationError, + ShieldConfigurationError, +) # shield name # must be same with in toml root config diff --git a/src/shields/webcam/__init__.py b/src/shields/webcam/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/shields/webcam/webcam.py b/src/shields/webcam/webcam.py index 5f14e8f..dbde472 100644 --- a/src/shields/webcam/webcam.py +++ b/src/shields/webcam/webcam.py @@ -1,20 +1,15 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ShieldConfigurationError, ConfigurationError, MaxActionReached -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -import constants -import status_handler +from src.shields.shields_common import ( + constants, + deploying, + logger_shields, + log_shield_exception, + log_shield_exception_warn, + send, + status_handler, + ConfigurationError, + MaxActionReached, + ShieldConfigurationError, +) # shield name # must be same with in toml root config @@ -31,6 +26,7 @@ status = status_handler.StatusHandler() + def bpf_main(): try: # shield configuration @@ -53,23 +49,25 @@ def shield_logic(cpu, data, size): event = b["events"].event(data) # get here the data for shield impl - webcam_data = ("pid:%d" % (event.pid)) + webcam_data = "pid:%d" % (event.pid) + + message = f"{constants.CAETRA_SENDER_LABEL}_{SHIELD_NAME.upper()} act: '{shield_config.get('action_label')}' limit_sending: {shield_config['features']['limit_sending']} data: {webcam_data}" - message = f"{constants.CAETRA_SENDER_LABEL}_{SHIELD_NAME.upper()} act: '{shield_config.get("action_label")}' limit_sending: {shield_config["features"]["limit_sending"]} data: { webcam_data }" - try: if shield_config["features"]["limit_sending"]: - status.can_be_sent(event.ts, shield_config["features"]["max_actions"], shield_config["features"]["cool_down_time"]) - + status.can_be_sent( + event.ts, + shield_config["features"]["max_actions"], + shield_config["features"]["cool_down_time"], + ) + send(message, shield_config) except ConfigurationError as e: log_shield_exception(e, SHIELD_NAME) except MaxActionReached as e: log_shield_exception_warn(e, SHIELD_NAME) else: - logger_shields.info( - f"{SHIELD_NAME} triggered and sent: {message}" - ) + logger_shields.info(f"{SHIELD_NAME} triggered and sent: {message}") finally: logger_shields.warning(f"{SHIELD_NAME} triggered: {message}") diff --git a/src/utils/__init__.py b/src/utils/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/src/utils/dict_handler.py b/src/utils/dict_handler.py index 01c7b0b..1dd1403 100644 --- a/src/utils/dict_handler.py +++ b/src/utils/dict_handler.py @@ -1,9 +1,5 @@ -import sys -import os - # caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -from caetra_exceptions import ConfigurationError +from src.caetra_exceptions import ConfigurationError def validate_dict_structure(expected_structure, data, structure_name): diff --git a/src/utils/logging_handler.py b/src/utils/logging_handler.py index 8d99454..a02c2c0 100644 --- a/src/utils/logging_handler.py +++ b/src/utils/logging_handler.py @@ -1,4 +1,4 @@ -from logger_setup import logger_shields, logger +from src.utils.logger_setup import logger_shields, logger def log_shield_exception(e, shield_name): diff --git a/tools/caetra_shield_generator/templates/ctr_bpf_py.jinja b/tools/caetra_shield_generator/templates/ctr_bpf_py.jinja index 2a52d2d..1990a10 100644 --- a/tools/caetra_shield_generator/templates/ctr_bpf_py.jinja +++ b/tools/caetra_shield_generator/templates/ctr_bpf_py.jinja @@ -1,19 +1,13 @@ -# generic imports -import sys -import os - -# caetra imports -sys.path.append(os.path.join(os.path.dirname(__file__), "../")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../..")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../utils")) -sys.path.append(os.path.join(os.path.dirname(__file__), "../../senders")) -from shields import deploying -from logger_setup import logger_shields -from caetra_exceptions import ShieldConfigurationError, ConfigurationError -from logging_handler import log_shield_exception, log_shield_exception_warn -from senders_handler import send -import constants +# minimun required imports +from src.shields.shields_common import ( + constants, + deploying, + log_shield_exception, + logger_shields, + send, + ConfigurationError, + ShieldConfigurationError, +) # shield name # must be same with in toml root config