Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions dissect/target/helpers/network_managers.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
from __future__ import annotations

import logging
import re
from collections import defaultdict
from configparser import ConfigParser, MissingSectionHeaderError
from io import StringIO
from re import compile, sub
from typing import Any, Callable, Iterable, Match, Optional, Union
from typing import Any, Callable, Iterable, Match, Optional

from defusedxml import ElementTree

from dissect.target.exceptions import PluginError
from dissect.target.helpers.fsutil import TargetPath
from dissect.target.target import Target

log = logging.getLogger(__name__)

try:
from ruamel.yaml import YAML

Expand Down Expand Up @@ -60,7 +65,7 @@ def create_config(self, path: TargetPath) -> Optional[dict]:
"""

if not path.exists() or path.is_dir():
self.target.log.debug("Failed to get config file %s", path)
log.debug("Failed to get config file %s", path)
config = None

if self.name == "netplan":
Expand All @@ -85,7 +90,7 @@ def _parse_netplan_config(self, path: TargetPath) -> Optional[dict]:
if HAS_YAML:
return self.parser(path.open("rb"))
else:
self.target.log.error("Failed to parse %s. Cannot import ruamel.yaml", self.name)
log.error("Failed to parse %s. Cannot import ruamel.yaml", self.name)
return None

def _parse_wicked_config(self, path: TargetPath) -> dict:
Expand Down Expand Up @@ -278,7 +283,7 @@ def translate(self, value: Any, option: str) -> str:
if option in translation_values and value:
return translation_key

def _get_option(self, config: dict, option: str, section: Optional[str] = None) -> Union[str, Callable]:
def _get_option(self, config: dict, option: str, section: Optional[str] = None) -> Optional[str | Callable]:
"""Internal function to get arbitrary options values from a parsed (non-translated) dictionary.

Args:
Expand All @@ -289,8 +294,13 @@ def _get_option(self, config: dict, option: str, section: Optional[str] = None)
Returns:
Value(s) corrensponding to that network configuration option.
"""
if not config:
log.error("Cannot get option %s: No config to parse", option)
return

if section:
config = config[section]
config = config.get(section, {})

for key, value in config.items():
if key == option:
return value
Expand Down Expand Up @@ -364,7 +374,7 @@ def parse(self) -> None:
if self.registered:
self.config = self.parser.parse()
else:
self.target.log.error("Network manager %s is not registered. Cannot parse config.", self.name)
log.error("Network manager %s is not registered. Cannot parse config.", self.name)

@property
def interface(self) -> set:
Expand Down