def test_start(self, request): ax_url = get_url(request) c = Connect(url=ax_url, certwarn=False, **get_key_creds(request)) c.start() assert "Connected" in format(c) assert "Connected" in repr(c) props = [ "activity_logs", "adapters", "dashboard", "devices", "enforcements", "instances", "meta", "remote_support", "settings_global", "settings_gui", "settings_ip", "settings_lifecycle", "signup", "system_roles", "system_users", "users", ] for prop in props: prop_attr = getattr(c, prop) assert format(prop_attr) assert repr(prop_attr)
def test_other_exc(self, request): """Pass.""" c = Connect(url="127.0.0.1", key=BAD_CRED, secret=BAD_CRED, certwarn=False) c._http.CONNECT_TIMEOUT = 1 c._auth._creds = None with pytest.raises(ConnectError): c.start()
def test_other_exc(self, request): c = Connect(url="127.0.0.1", key=BAD_CRED, secret=BAD_CRED, certwarn=False) c.HTTP.CONNECT_TIMEOUT = 1 c.AUTH._creds = None with pytest.raises(ConnectError): c.start()
def test_connect_error(self): c = Connect(url="https://127.0.0.1:3919", key=BAD_CRED, secret=BAD_CRED, certwarn=False) c.HTTP.CONNECT_TIMEOUT = 1 with pytest.raises(ConnectError) as exc: c.start() assert isinstance(exc.value.exc, requests.ConnectionError)
def test_invalid_creds(self, request): ax_url = get_url(request) c = Connect(url=ax_url, key=BAD_CRED, secret=BAD_CRED, certwarn=False) c.HTTP.CONNECT_TIMEOUT = 1 with pytest.raises(ConnectError) as exc: c.start() assert isinstance(exc.value.exc, InvalidCredentials)
def test_invalid_creds_nowrap(self, request): """Pass.""" ax_url = get_url(request) c = Connect( url=ax_url, key=BAD_CRED, secret=BAD_CRED, certwarn=False, wraperror=False ) c._http.CONNECT_TIMEOUT = 1 with pytest.raises(InvalidCredentials): c.start()
def test_connect_timeout(self): """Pass.""" c = Connect(url="127.0.0.99", key=BAD_CRED, secret=BAD_CRED, certwarn=False) c._http.CONNECT_TIMEOUT = 1 with pytest.raises(ConnectError) as exc: c.start() if IS_LINUX: assert isinstance(exc.value.exc, requests.ConnectionError) else: assert isinstance(exc.value.exc, requests.ConnectTimeout)
def main(): """PARSE AND VALIDATE INTEGRATION PARAMS.""" params: dict = demisto.params() command: str = demisto.command() url: str = params["ax_url"] key: str = params["ax_key"] secret: str = params["ax_secret"] certverify: bool = not params.get("insecure", False) handle_proxy() # noqa: F821, F405 demisto.debug(f"Command being called is {command}") args: dict = demisto.args() try: client = Connect( url=url, key=key, secret=secret, certverify=certverify, certwarn=False, ) return_results(run_command(client, args, command)) # noqa: F821, F405 except Exception as exc: demisto.error(traceback.format_exc()) msg: List[str] = [f"Failed to execute {command} command", "Error:", str(exc)] return_error("\n".join(msg)) # noqa: F821, F405
def test_reason(self): """Pass.""" exc = Exception("badwolf") reason = Connect._get_exc_reason(exc) assert format(reason) == "badwolf"
def main(): """PARSE AND VALIDATE INTEGRATION PARAMS.""" params: dict = demisto.params() command: str = demisto.command() url: str = params["ax_url"] key: str = params["ax_key"] secret: str = params["ax_secret"] certverify: bool = not params.get("insecure", False) handle_proxy() # noqa: F821, F405 demisto.debug(f"Command being called is {command}") try: client = Connect( url=url, key=key, secret=secret, certverify=certverify, certwarn=False, ) if command == "test-module": result = test_module(client=client) return_results(result) # noqa: F821, F405 elif command == "axonius-get-devices-by-savedquery": results = get_by_sq(api_obj=client.devices) return_results(results) # noqa: F821, F405 elif command == "axonius-get-users-by-savedquery": results = get_by_sq(api_obj=client.users) return_results(results) # noqa: F821, F405 elif command == "axonius-get-users-by-mail": results = get_by_value(api_obj=client.users, method_name="mail") return_results(results) # noqa: F821, F405 elif command == "axonius-get-users-by-username": results = get_by_value(api_obj=client.users, method_name="username") return_results(results) # noqa: F821, F405 elif command == "axonius-get-devices-by-hostname": results = get_by_value(api_obj=client.devices, method_name="hostname") return_results(results) # noqa: F821, F405 elif command == "axonius-get-devices-by-ip": results = get_by_value(api_obj=client.devices, method_name="ip") return_results(results) # noqa: F821, F405 elif command == "axonius-get-devices-by-mac": results = get_by_value(api_obj=client.devices, method_name="mac") return_results(results) # noqa: F821, F405 except Exception as exc: demisto.error(traceback.format_exc()) msg: List[str] = [ f"Failed to execute {command} command", "Error:", str(exc) ] return_error("\n".join(msg)) # noqa: F821, F405
def test_no_start(self, request): ax_url = get_url(request) c = Connect(url=ax_url, key=BAD_CRED, secret=BAD_CRED) assert "Not connected" in format(c) assert "Not connected" in repr(c) assert c.HANDLER_FILE is None assert c.HANDLER_CON is None
def test_no_start(self, request): """Pass.""" ax_url = get_url(request) c = Connect(url=ax_url, key=BAD_CRED, secret=BAD_CRED) assert "Not connected" in format(c) assert "Not connected" in repr(c) assert c._handler_file is None assert c._handler_con is None
def test_no_start_logs(self, request): """Pass.""" ax_url = get_url(request) c = Connect( url=ax_url, key=BAD_CRED, secret=BAD_CRED, log_console=True, log_file=True ) assert "Not connected" in format(c) assert "Not connected" in repr(c) assert isinstance(c._handler_file, logging.Handler) assert isinstance(c._handler_con, logging.Handler)
def test_no_start_logs(self, request): ax_url = get_url(request) c = Connect(url=ax_url, key=BAD_CRED, secret=BAD_CRED, log_console=True, log_file=True) assert "Not connected" in format(c) assert "Not connected" in repr(c) assert isinstance(c.HANDLER_FILE, logging.Handler) assert isinstance(c.HANDLER_CON, logging.Handler)
def test_start(self, request): ax_url = get_url(request) c = Connect(url=ax_url, certwarn=False, **get_key_creds(request)) c.start() assert "Connected" in format(c) assert "Connected" in repr(c) format(c.system.settings_lifecycle) format(c.system.settings_gui) format(c.system.settings_core) format(c.system.users) format(c.system.roles) format(c.instances) format(c.dashboard) format(c.system.meta) format(c.enforcements) format(c.users) format(c.devices) format(c.adapters)
def test_module(client: Connect) -> str: """Tests Axonius API Client connectivity.""" client.start() return "ok"
fieldnames=fields, extrasaction="ignore", quoting=csv.QUOTE_ALL) writer.writerow(dict(zip(fields, fields))) for result in results: for k, v in result.items(): if isinstance(v, list): result[k] = " - " + "\n - ".join([str(x) for x in v]) writer.writerow(result) print(f"wrote results to {path}") def jdump(obj, **kwargs): """JSON dump utility.""" print(json_reload(obj, **kwargs)) j = jdump if __name__ == "__main__": AX_URL = os.environ["AX_URL"] AX_KEY = os.environ["AX_KEY"] AX_SECRET = os.environ["AX_SECRET"] ctx = Connect(url=AX_URL, key=AX_KEY, secret=AX_SECRET, certwarn=False) ctx.start() devices = ctx.devices assets = get_assets(devices) results = parse_assets(assets) write_csv(results)
def j(obj, **kwargs): """JSON dump utility.""" print(json_reload(obj, **kwargs)) if __name__ == "__main__": # read the API key, API secret, and URL from a ".env" file load_dotenv() AX_URL = os.environ["AX_URL"] AX_KEY = os.environ["AX_KEY"] AX_SECRET = os.environ["AX_SECRET"] # create a client using the url, key, and secret client = Connect(url=AX_URL, key=AX_KEY, secret=AX_SECRET) # start the client, will perform login to URL using key & secret client.start() # work with device assets devices = client.devices # work with user assets users = client.users # work with adapters and adapter connections adapters = client.adapters # work with enforcements enforcements = client.enforcements