Introduce logger.py module.

This commit is contained in:
Sebastian Lenzlinger 2024-05-07 19:55:58 +02:00
parent 577ac9e5cf
commit 7b27ee7fa9
6 changed files with 73 additions and 38 deletions

43
.idea/workspace.xml generated
View File

@ -4,19 +4,13 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="7a3ac8e1-7fbf-4aa7-9cf9-a51d7ade8503" name="Changes" comment="Refactor subcommands, config etc.">
<change afterPath="$PROJECT_DIR$/code/iottb/utils/utils.py" afterDir="false" />
<list default="true" id="7a3ac8e1-7fbf-4aa7-9cf9-a51d7ade8503" name="Changes" comment="UNTESTED REFACTORING:&#10;Move more functionality into Metadata Model classes to ensure data is available and better passable between functions.">
<change afterPath="$PROJECT_DIR$/code/iottb/logger.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/__main__.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/__main__.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/cli.py" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/definitions.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/definitions.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/models/capture_metadata_model.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/models/capture_metadata_model.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/models/device_metadata_model.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/models/device_metadata_model.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/subcommands/add_device.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/subcommands/add_device.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/subcommands/capture.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/subcommands/capture.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/subcommands/config.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/utils/capture_utils.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/utils/capture_metadata_utils.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/utils/capture_metadata_utils.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/utils/device_metadata_utils.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/utils/device_metadata_utils.py" afterDir="false" />
<change beforePath="$PROJECT_DIR$/code/iottb/utils/tcpdump_utils.py" beforeDir="false" afterPath="$PROJECT_DIR$/code/iottb/utils/tcpdump_utils.py" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -56,12 +50,13 @@
"ASKED_MARK_IGNORED_FILES_AS_EXCLUDED": "true",
"ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
"Python.__init__.executor": "Run",
"Python.__main__.executor": "Run",
"Python.__main__.executor": "Debug",
"Python.iotdb.executor": "Debug",
"Python.main.executor": "Run",
"RunOnceActivity.ShowReadmeOnStart": "true",
"SHARE_PROJECT_CONFIGURATION_FILES": "true",
"git-widget-placeholder": "cli-dev",
"last_opened_file_path": "/home/slnopriv/projects/2024-bsc-sebastian-lenzlinger/code/iottb/models/device_metadata_model.py",
"last_opened_file_path": "/home/slnopriv/projects/2024-bsc-sebastian-lenzlinger/code/iottb/logger.py",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
"node.js.selected.package.eslint": "(autodetect)",
@ -104,7 +99,7 @@
<option name="INPUT_FILE" value="" />
<method v="2" />
</configuration>
<configuration name="__main__" type="PythonConfigurationType" factoryName="Python" nameIsGenerated="true">
<configuration name="__main__" type="PythonConfigurationType" factoryName="Python" temporary="true" nameIsGenerated="true">
<module name="2024-bsc-sebastian-lenzlinger" />
<option name="ENV_FILES" value="" />
<option name="INTERPRETER_OPTIONS" value="" />
@ -113,13 +108,13 @@
<env name="PYTHONUNBUFFERED" value="1" />
</envs>
<option name="SDK_HOME" value="" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/code/kydcap" />
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$/code/iottb" />
<option name="IS_MODULE_SDK" value="true" />
<option name="ADD_CONTENT_ROOTS" value="true" />
<option name="ADD_SOURCE_ROOTS" value="true" />
<EXTENSION ID="PythonCoverageRunConfigurationExtension" runner="coverage.py" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/code/kydcap/__main__.py" />
<option name="PARAMETERS" value="init-device-root --dynamic" />
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/code/iottb/__main__.py" />
<option name="PARAMETERS" value="add --root /tmp/test --guided" />
<option name="SHOW_COMMAND_LINE" value="false" />
<option name="EMULATE_TERMINAL" value="false" />
<option name="MODULE_MODE" value="false" />
@ -133,6 +128,7 @@
</list>
<recent_temporary>
<list>
<item itemvalue="Python.__main__" />
<item itemvalue="Python.__init__" />
</list>
</recent_temporary>
@ -159,7 +155,7 @@
<workItem from="1714616237168" duration="6135000" />
<workItem from="1714850899817" duration="2659000" />
<workItem from="1714917763516" duration="9796000" />
<workItem from="1715078660090" duration="21450000" />
<workItem from="1715078660090" duration="25014000" />
</task>
<task id="LOCAL-00001" summary="Add code for capture testbed. This is a huge commit. End of day sync...">
<option name="closed" value="true" />
@ -233,7 +229,15 @@
<option name="project" value="LOCAL" />
<updated>1714924463148</updated>
</task>
<option name="localTasksCounter" value="10" />
<task id="LOCAL-00010" summary="UNTESTED REFACTORING:&#10;Move more functionality into Metadata Model classes to ensure data is available and better passable between functions.">
<option name="closed" value="true" />
<created>1715101138312</created>
<option name="number" value="00010" />
<option name="presentableId" value="LOCAL-00010" />
<option name="project" value="LOCAL" />
<updated>1715101138312</updated>
</task>
<option name="localTasksCounter" value="11" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -292,9 +296,12 @@
<MESSAGE value="SYNC" />
<MESSAGE value="Refactor various names." />
<MESSAGE value="Refactor subcommands, config etc." />
<option name="LAST_COMMIT_MESSAGE" value="Refactor subcommands, config etc." />
<MESSAGE value="UNTESTED REFACTORING:&#10;Move more functionality into Metadata Model classes to ensure data is available and better passable between functions." />
<option name="LAST_COMMIT_MESSAGE" value="UNTESTED REFACTORING:&#10;Move more functionality into Metadata Model classes to ensure data is available and better passable between functions." />
</component>
<component name="com.intellij.coverage.CoverageDataManagerImpl">
<SUITE FILE_PATH="coverage/2024_bsc_sebastian_lenzlinger$__main__.coverage" NAME="__main__ Coverage Results" MODIFIED="1715103831289" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/code/iottb" />
<SUITE FILE_PATH="coverage/2024_bsc_sebastian_lenzlinger$iotdb.coverage" NAME="iotdb Coverage Results" MODIFIED="1715103593519" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/code/iottb" />
<SUITE FILE_PATH="coverage/2024_bsc_sebastian_lenzlinger$__init__.coverage" NAME="__init__ Coverage Results" MODIFIED="1714619300966" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/code/kydcap" />
<SUITE FILE_PATH="coverage/2024_bsc_sebastian_lenzlinger$main.coverage" NAME="__main__ Coverage Results" MODIFIED="1714619560177" SOURCE_PROVIDER="com.intellij.coverage.DefaultCoverageFileProvider" RUNNER="coverage.py" COVERAGE_BY_TEST_ENABLED="false" COVERAGE_TRACING_ENABLED="false" WORKING_DIRECTORY="$PROJECT_DIR$/code/kydcap" />
</component>

28
code/iottb/logger.py Normal file
View File

@ -0,0 +1,28 @@
import logging
import sys
from logging.handlers import RotatingFileHandler
def setup_logging():
logger_obj = logging.getLogger('iottbLogger')
logger_obj.setLevel(logging.INFO)
file_handler = RotatingFileHandler('iottb.log')
console_handler = logging.StreamHandler(sys.stdout)
file_handler.setLevel(logging.DEBUG)
console_handler.setLevel(logging.INFO)
file_fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
console_fmt = logging.Formatter('%(name)s - %(levelname)s - %(message)s')
file_handler.setFormatter(file_fmt)
console_handler.setFormatter(console_fmt)
logger_obj.addHandler(file_handler)
logger_obj.addHandler(console_handler)
return logger_obj
logger = setup_logging()

View File

@ -13,7 +13,7 @@ from iottb.models.device_metadata_model import DeviceMetadata
class CaptureMetadata(BaseModel):
# Required Fields
device_metadata = Field(DeviceMetadata, exclude=True)
device_metadata: DeviceMetadata = Field(exclude=True)
capture_id: uuid.UUID = Field(default_factory=lambda: str(uuid.uuid4()))
capture_dir: Path
capture_file: str
@ -27,7 +27,7 @@ class CaptureMetadata(BaseModel):
packet_count: Optional[int]
pcap_filter: str = ""
tcpdump_command: str = ""
interface = Field(str, default="")
interface: str = ""
# Optional Fields
device_ip_address: Optional[str] = "No IP Address set"

View File

@ -33,8 +33,8 @@ class DeviceMetadata(BaseModel):
super().__init__(**data)
self.device_name = device_name
self.device_short_name = device_name.lower().replace(" ", "_")
assert dir_contains_device_metadata(device_root_dir), \
f"Directory {device_root_dir} is missing a {DEVICE_METADATA_FILE} file"
# assert dir_contains_device_metadata(device_root_dir), \
# f"Directory {device_root_dir} is missing a {DEVICE_METADATA_FILE} file"
self.device_root_dir = device_root_dir
def get_device_id(self) -> str:
@ -82,11 +82,10 @@ class DeviceMetadata(BaseModel):
self.date_updated = datetime.now().strftime('%d-%m-%YT%H:%M:%S')
@classmethod
def load_from_json(cls, root_path: Path):
assert root_path.is_file()
assert root_path.name == DEVICE_METADATA_FILE
assert dir_contains_device_metadata(root_path)
device_meta_filename = root_path / DEVICE_METADATA_FILE
def load_from_json(cls, device_file_path: Path):
assert device_file_path.is_file(), f"{device_file_path} is not a file"
assert device_file_path.name == DEVICE_METADATA_FILE, f"{device_file_path} is not a {DEVICE_METADATA_FILE}"
device_meta_filename = device_file_path
with device_meta_filename.open('r') as file:
metadata_json = json.load(file)
metadata_model_obj = cls.model_validate_json(metadata_json)
@ -122,6 +121,7 @@ def dir_contains_device_metadata(dir_path: Path):
return False
else:
meta_file_path = dir_path / DEVICE_METADATA_FILE
print(f"Device metadata file path {str(meta_file_path)}")
if not meta_file_path.is_file():
return False
else:

View File

@ -8,23 +8,23 @@ from iottb.utils.device_metadata_utils import *
def setup_init_device_root_parser(subparsers):
parser = subparsers.add_parser("add-device", aliases=["add-device-root", "add"])
parser.add_argument("--root", type=pathlib.Path, default=pathlib.Path.cwd())
parser.add_argument("--root_dir", type=pathlib.Path, default=pathlib.Path.cwd())
group = parser.add_mutually_exclusive_group()
group.add_argument("--guided", action="store_true", help="Guided setup", default=False)
group.add_argument("name", action="store", type=str, help="name of device")
group.add_argument("--name", action="store", type=str, help="name of device")
parser.set_defaults(func=handle_add)
def handle_add(args):
print("Entered add-device-root")
metadata = None
if args.guided:
metadata = guided_setup(args.root_dir)
else:
device_name = args.name
args.root_dir.mkdir(parents=True, exist_ok=True)
args.root_dir.chdir()
metadata = DeviceMetadata(device_name)
metadata = DeviceMetadata(device_name, args.root_dir)
file_path = args.root_dir / DEVICE_METADATA_FILE
response = input(f"Confirm device metadata: {metadata.model_dump()} [y/N]")
@ -50,10 +50,10 @@ def guided_setup(device_root) -> DeviceMetadata:
device_name = input("Please enter name of device: ")
if device_name == "" or device_name is None:
print("Name cannot be empty")
response = input(f"Confirm device name: {device_name} [y/N]")
response = input(f"Confirm device name: {device_name} [y/N] ")
assert response.lower() in definitions.AFFIRMATIVE_USER_RESPONSE.add(""), f"{response.upper()} not supported"
assert device_name != ""
assert device_name is not None
return DeviceMetadata(device_name)
return DeviceMetadata(device_name, device_root)

View File

@ -12,8 +12,8 @@ def setup_capture_parser(subparsers):
# metadata args
parser.add_argument("-a", "--ip-address", help="IP address of the device to sniff", dest="device_ip")
# tcpdump args
parser.add_argument("device-root", help="Root folder for device to sniff", dest="device_folder",
type=Path, required=True, default=Path.cwd())
parser.add_argument("device_root", help="Root folder for device to sniff",
type=Path, default=Path.cwd())
parser.add_argument("-s", "--safe", help="Ensure correct device root folder before sniffing", action="store_true")
parser.add_argument("--app", help="Application name to sniff", dest="app_name", default=None)
@ -73,16 +73,16 @@ def run_tcpdump(cmd):
try:
p = subprocess.run(cmd, capture_output=True, text=True, check=True)
if p.returncode != 0:
print(p.stderr)
print(f"Error running tcpdump {p.stderr}")
else:
print(p.stdout)
print(f"tcpdump run successfully\n: {p.stdout}")
except KeyboardInterrupt:
pass
def handle_capture(args):
assert args.device_root is not None, f"Device root directory is required"
assert dir_contains_device_metadata(args.device_root)
assert dir_contains_device_metadata(args.device_root), f"Device metadata file '{args.device_root}' does not exist"
# get device metadata
if args.safe and not dir_contains_device_metadata(args.device_root):
print(f"Supplied folder contains no device metadata. "