From a4f2669b44e017fc70e07785a540538dda4aa76f Mon Sep 17 00:00:00 2001 From: naisanzaa Date: Fri, 10 May 2024 21:18:00 +0800 Subject: [PATCH] wdutil: add client and config --- README.md | 2 +- automon/integrations/mac/wdutil/__init__.py | 2 + automon/integrations/mac/wdutil/client.py | 33 ++++++++++++++++ automon/integrations/mac/wdutil/config.py | 38 +++++++++++++++++++ automon/integrations/mac/wdutil/exceptions.py | 2 + .../integrations/mac/wdutil/tests/__init__.py | 0 .../mac/wdutil/tests/test_client_ready.py | 18 +++++++++ .../mac/wdutil/tests/test_config.py | 15 ++++++++ .../mac/wdutil/tests/test_wdutil.py | 15 ++++++++ env-example.sh | 3 ++ 10 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 automon/integrations/mac/wdutil/__init__.py create mode 100644 automon/integrations/mac/wdutil/client.py create mode 100644 automon/integrations/mac/wdutil/config.py create mode 100644 automon/integrations/mac/wdutil/exceptions.py create mode 100644 automon/integrations/mac/wdutil/tests/__init__.py create mode 100644 automon/integrations/mac/wdutil/tests/test_client_ready.py create mode 100644 automon/integrations/mac/wdutil/tests/test_config.py create mode 100644 automon/integrations/mac/wdutil/tests/test_wdutil.py diff --git a/README.md b/README.md index 0c9a4460..4118d999 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ Github issues and feature requests welcomed. | Google Cloud | google auth api
google people api
google sheets api | | Helpers | os
subprocess
threading
socket
datetime | | Logging | sentryio | -| MacOS | airport
macchanger | +| MacOS | airport
macchanger
wdutil | | Python | logging
requests | | SOAR | swimlane
splunk soar | | Recon | nmap | diff --git a/automon/integrations/mac/wdutil/__init__.py b/automon/integrations/mac/wdutil/__init__.py new file mode 100644 index 00000000..e58f8baf --- /dev/null +++ b/automon/integrations/mac/wdutil/__init__.py @@ -0,0 +1,2 @@ +from .client import WdutilClient +from .config import WdutilConfig diff --git a/automon/integrations/mac/wdutil/client.py b/automon/integrations/mac/wdutil/client.py new file mode 100644 index 00000000..4970597e --- /dev/null +++ b/automon/integrations/mac/wdutil/client.py @@ -0,0 +1,33 @@ +from automon import logging +from automon import os_is_mac +from automon.helpers import Run + +from .config import WdutilConfig + +log = logging.getLogger(__name__) +log.setLevel(level=logging.DEBUG) + + +class WdutilClient(object): + + def __init__(self, config: WdutilConfig = None, wdutil_path: str = None): + self.config = config or WdutilConfig(wdutil_path=wdutil_path) + self.wdutil = self.config.wdutil_path() + + self._runner = Run() + + def run(self, arg: str): + self.config.is_ready() + + command = f'echo {self.config.password} | sudo -S {self.wdutil} {arg}' + command = f'{self.wdutil} {arg}' + log.info(f'{command}') + self._runner.run(command=command, shell=True) + + def is_ready(self): + if self.config.is_ready(): + return True + return False + + def help(self): + return self.run('help') diff --git a/automon/integrations/mac/wdutil/config.py b/automon/integrations/mac/wdutil/config.py new file mode 100644 index 00000000..c9ebaec3 --- /dev/null +++ b/automon/integrations/mac/wdutil/config.py @@ -0,0 +1,38 @@ +from automon import Run +from automon import environ +from automon import logging +from automon import os_is_mac + +from .exceptions import * + +log = logging.getLogger(__name__) +log.setLevel(level=logging.DEBUG) + + +class WdutilConfig(object): + + def __init__(self, password: str = None, wdutil_path: str = None): + self.password = password or environ('WDUTIL_PASSWORD') + self._wdutil_path = wdutil_path + self._runner = Run() + + def is_ready(self): + if self.password and self.wdutil_path: + return True + + raise (WdutilConfigNotReady(f'missing WDUTIL_PASSWORD or missing wdutil')) + + def wdutil_path(self): + if os_is_mac(): + + if self._wdutil_path: + return self._wdutil_path + + if self._runner.which('wdutil'): + self._wdutil_path = self._runner.stdout.decode().strip() + log.info(str(dict( + wdutil_path=self._wdutil_path + ))) + return self._wdutil_path + + return False diff --git a/automon/integrations/mac/wdutil/exceptions.py b/automon/integrations/mac/wdutil/exceptions.py new file mode 100644 index 00000000..a3052770 --- /dev/null +++ b/automon/integrations/mac/wdutil/exceptions.py @@ -0,0 +1,2 @@ +class WdutilConfigNotReady(Exception): + pass diff --git a/automon/integrations/mac/wdutil/tests/__init__.py b/automon/integrations/mac/wdutil/tests/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/automon/integrations/mac/wdutil/tests/test_client_ready.py b/automon/integrations/mac/wdutil/tests/test_client_ready.py new file mode 100644 index 00000000..ed78b9f6 --- /dev/null +++ b/automon/integrations/mac/wdutil/tests/test_client_ready.py @@ -0,0 +1,18 @@ +import unittest + +from automon import os_is_mac +from automon.integrations.mac.wdutil import WdutilClient, WdutilConfig + + +class MyTestCase(unittest.TestCase): + if os_is_mac(): + if WdutilConfig().is_ready(): + + def test_something(self): + self.assertTrue( + WdutilClient().is_ready() + ) + + +if __name__ == '__main__': + unittest.main() diff --git a/automon/integrations/mac/wdutil/tests/test_config.py b/automon/integrations/mac/wdutil/tests/test_config.py new file mode 100644 index 00000000..856c4579 --- /dev/null +++ b/automon/integrations/mac/wdutil/tests/test_config.py @@ -0,0 +1,15 @@ +import unittest + +from automon import os_is_mac +from automon.integrations.mac.wdutil import WdutilConfig + + +class MyTestCase(unittest.TestCase): + if os_is_mac(): + def test_something(self): + with self.assertRaises(Exception): + WdutilConfig().is_ready() + + +if __name__ == '__main__': + unittest.main() diff --git a/automon/integrations/mac/wdutil/tests/test_wdutil.py b/automon/integrations/mac/wdutil/tests/test_wdutil.py new file mode 100644 index 00000000..366c9017 --- /dev/null +++ b/automon/integrations/mac/wdutil/tests/test_wdutil.py @@ -0,0 +1,15 @@ +import unittest + +from automon import os_is_mac +from automon.integrations.mac.wdutil import WdutilClient + + +class MyTestCase(unittest.TestCase): + + if os_is_mac(): + def test_something(self): + self.assertTrue(WdutilClient().wdutil) + + +if __name__ == '__main__': + unittest.main() diff --git a/env-example.sh b/env-example.sh index 2fab13a3..6437fc8d 100644 --- a/env-example.sh +++ b/env-example.sh @@ -138,3 +138,6 @@ LDAP_PORT= VDS_BIND_USER=cn=automon,ou=people,dc=automon,dc=com VDS_BASE_DN=dc=automon,dc=com VDS_PASSWORD= + +# Wdutil +WDUTIL_PASSWORD=