class ReloadAlgorithm(ProcessAlgorithm): def __init__(self): super(ReloadAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def reload(self): self.szig.reload() if self.szig.reload_result(): result = CommandResultSuccess("Reload successful") else: result = CommandResultFailure("Reload failed") return result def execute(self): error = self.errorHandling() if error != None: error.value = self.instance.process_name return error try: reloaded = self.reload() except SZIGError as e: reloaded = CommandResultFailure( "error while communicating through szig: %s" % e.msg) if not reloaded: reloaded.value = self.instance.process_name return reloaded
class ReloadAlgorithm(ProcessAlgorithm): def __init__(self): super(ReloadAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.strerror) def reload(self): self.szig.reload() if self.szig.reload_result(): result = CommandResultSuccess("Reload successful") else: result = CommandResultFailure("Reload failed") return result def execute(self): error = self.errorHandling() if error != None: error.value = self.instance.process_name return error try: reloaded = self.reload() except SZIGError as e: reloaded = CommandResultFailure("error while communicating through szig: %s" % e.msg) if not reloaded: reloaded.value = self.instance.process_name return reloaded
class CoredumpAlgorithm(ProcessAlgorithm): def __init__(self): super(CoredumpAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.strerror) return None def coredump(self): try: self.szig.coredump() except SZIGError as e: return CommandResultFailure(e.msg) return CommandResultSuccess("core successfully dumped") def execute(self): error = self.errorHandling() if error != None: return error try: return self.coredump() except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message)
class SzigWalkAlgorithm(ProcessAlgorithm): def __init__(self, root=""): self.root = root super(SzigWalkAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: # Ignore not running process return CommandResultSuccess(running.msg) try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return None def getChilds(self, node): child = self.szig.get_child(node) if child: result = {} result[child.split('.')[-1]] = self.walk(child) sibling = self.szig.get_sibling(child) while sibling: result[sibling.split('.')[-1]] = self.walk(sibling) sibling = self.szig.get_sibling(sibling) return result else: return None def walk(self, node): value = self.szig.get_value(node) if value != None: return value else: return self.getChilds(node) def execute(self): def _prepend_instance_name(tree): return {self.instance.process_name: tree} error = self.errorHandling() if error != None: return error try: if self.root: szig_dict = _prepend_instance_name( {self.root: self.walk(self.root)}) else: szig_dict = _prepend_instance_name(self.walk(self.root)) return CommandResultSuccess(value=szig_dict) except SZIGError as e: return CommandResultFailure( 'Error while communicating through szig: ' + e.msg)
def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: # Ignore not running process return CommandResultSuccess(running.msg) try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return None
class SzigWalkAlgorithm(ProcessAlgorithm): def __init__(self, root=""): self.root = root super(SzigWalkAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return None def getChilds(self, node): child = self.szig.get_child(node) if child: result = {} result[child.split('.')[-1]] = self.walk(child) sibling = self.szig.get_sibling(child) while sibling: result[sibling.split('.')[-1]] = self.walk(sibling) sibling = self.szig.get_sibling(sibling) return result else: return None def walk(self, node): value = self.szig.get_value(node) if value != None: return value else: return self.getChilds(node) def execute(self): def _prepend_instance_name(tree): return {self.instance.process_name : tree} error = self.errorHandling() if error != None: return error try: if self.root: szig_dict = _prepend_instance_name({self.root : self.walk(self.root)}) else: szig_dict = _prepend_instance_name(self.walk(self.root)) return CommandResultSuccess("", szig_dict) except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
class AuthorizeAlgorithm(ProcessAlgorithm): ACCEPT = True REJECT = False def __init__(self, behaviour, session_id, description): self.behaviour = behaviour self.session_id = session_id self.description = description super(AuthorizeAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return None def accept(self): try: result = self.szig.authorize_accept(self.session_id, self.description) return CommandResultSuccess(result) except SZIGError as e: return CommandResultFailure(e.msg) def reject(self): try: result = self.szig.authorize_reject(self.session_id, self.description) return CommandResultSuccess(result) except SZIGError as e: return CommandResultFailure(e.msg) def execute(self): error = self.errorHandling() if error != None: return error try: if self.behaviour: return self.accept() else: return self.reject() except SZIGError as e: return CommandResultFailure( "error while communicating through szig: %s" % e.msg)
def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: self.status.pid = "missing" self.status.running = "missing" self.status.threads_running = "missing" self.status.thread_number = "missing" self.status.thread_rate_avg1 = "missing" self.status.thread_rate_avg5 = "missing" self.status.thread_rate_avg15 = "missing" return self.status try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message)
class StopSessionAlgorithm(ProcessAlgorithm): def __init__(self, session_id): self.session_id = session_id super(StopSessionAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def execute(self): error = self.errorHandling() if error != None: return error try: result = self.szig.stop_session(self.session_id) return CommandResultSuccess(result) except SZIGError as e: return CommandResultFailure( 'Error while communicating through szig: ' + e.msg)
def test_szig_walk(self): handler_mock = HandlerMock szig = SZIG("", handler_mock) algorithm = SzigWalkAlgorithm() algorithm.szig = szig self.assertEquals(algorithm.walk(""), handler_mock().data) self.assertEquals(algorithm.walk("stats"), handler_mock().data["stats"])
class GUIStatusAlgorithm(ProcessAlgorithm): def __init__(self): super(GUIStatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: self.status.pid = "missing" self.status.running = "missing" self.status.threads_running = "missing" self.status.thread_number = "missing" self.status.thread_rate_avg1 = "missing" self.status.thread_rate_avg5 = "missing" self.status.thread_rate_avg15 = "missing" return self.status try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def gui_status(self): self.status.pid = self.getProcessPid(self.instance.process_name) self.status.running = "running" self.status.threads_running = self.szig.get_value( 'stats.threads_running') self.status.thread_number = self.szig.get_value('stats.thread_number') self.status.thread_rate_avg1 = self.szig.get_value( 'stats.thread_rate_avg1') self.status.thread_rate_avg5 = self.szig.get_value( 'stats.thread_rate_avg5') self.status.thread_rate_avg15 = self.szig.get_value( 'stats.thread_rate_avg15') return self.status def execute(self): self.status = GUIStatus(self.instance.process_name) error = self.errorHandling() if error != None: return error try: return self.gui_status() except SZIGError as e: return CommandResultFailure( "error while communicating through szig: %s" % e.msg)
class AuthorizeAlgorithm(ProcessAlgorithm): ACCEPT = True REJECT = False def __init__(self, behaviour, session_id, description): self.behaviour = behaviour self.session_id = session_id self.description = description super(AuthorizeAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return None def accept(self): try: result = self.szig.authorize_accept(self.session_id, self.description) return CommandResultSuccess(result) except SZIGError as e: return CommandResultFailure(e.msg) def reject(self): try: result = self.szig.authorize_reject(self.session_id, self.description) return CommandResultSuccess(result) except SZIGError as e: return CommandResultFailure(e.msg) def execute(self): error = self.errorHandling() if error != None: return error try: if self.behaviour: return self.accept() else: return self.reject() except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.strerror)
def execute(self): try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) try: return self.services() except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
class GUIStatusAlgorithm(ProcessAlgorithm): def __init__(self): super(GUIStatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: self.status.pid = "missing" self.status.running = "missing" self.status.threads_running = "missing" self.status.thread_number = "missing" self.status.thread_rate_avg1 = "missing" self.status.thread_rate_avg5 = "missing" self.status.thread_rate_avg15 = "missing" return self.status try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def gui_status(self): self.status.pid = self.getProcessPid(self.instance.process_name) self.status.running = "running" self.status.threads_running = self.szig.get_value('stats.threads_running') self.status.thread_number = self.szig.get_value('stats.thread_number') self.status.thread_rate_avg1 = self.szig.get_value('stats.thread_rate_avg1') self.status.thread_rate_avg5 = self.szig.get_value('stats.thread_rate_avg5') self.status.thread_rate_avg15 = self.szig.get_value('stats.thread_rate_avg15') return self.status def execute(self): self.status = GUIStatus(self.instance.process_name) error = self.errorHandling() if error != None: return error try: return self.gui_status() except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
class StatusAlgorithm(ProcessAlgorithm): def __init__(self): super(StatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) def status(self): status = ProcessStatus(self.instance.process_name) status.pid = self.getProcessPid(self.instance.process_name) status.threads = int(self.szig.get_value('stats.threads_running')) status.policy_file = self.szig.get_value('info.policy.file') status.timestamp_szig = self.szig.get_value('info.policy.file_stamp') status.reload_timestamp = self.szig.get_value( 'info.policy.reload_stamp') status.timestamp_os = os.path.getmtime(status.policy_file) status.reloaded = str(status.timestamp_szig) == str( status.timestamp_os).split('.')[0] return status def execute(self): error = self.errorHandling() if error != None: return error try: return self.status() except SZIGError as e: return CommandResultFailure( "error while communicating through szig: %s" % e.msg)
class StatusAlgorithm(ProcessAlgorithm): def __init__(self): super(StatusAlgorithm, self).__init__() def errorHandling(self): running = self.isRunning(self.instance.process_name) if not running: return running try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.strerror) def status(self): status = ProcessStatus(self.instance.process_name) status.pid = self.getProcessPid(self.instance.process_name) status.threads = int(self.szig.get_value('stats.threads_running')) status.policy_file = self.szig.get_value('info.policy.file') status.timestamp_szig = self.szig.get_value('info.policy.file_stamp') status.reload_timestamp = self.szig.get_value('info.policy.reload_stamp') status.timestamp_os = os.path.getmtime(status.policy_file) status.reloaded = str(status.timestamp_szig) == str(status.timestamp_os).split('.')[0] return status def execute(self): error = self.errorHandling() if error != None: return error try: return self.status() except SZIGError as e: return CommandResultFailure("error while communicating through szig: %s" % e.msg)
def setUp(self): handler_mock = HandlerMock test_policy_file = open('test_policy_file', 'w') test_policy_file.close() time_stamp = os.path.getmtime('test_policy_file') szig = SZIG("", handler_mock) szig.handler.data["info"]["policy"]["file"] = 'test_policy_file' szig.handler.data["info"]["policy"]["file_stamp"] = time_stamp szig.handler.data["info"]["policy"]["reload_stamp"] = time_stamp self.algorithm = StatusAlgorithm() self.algorithm.setInstance(Instance(name='testinstance', process_num=0)) self.algorithm.szig = szig self.algorithm.pidfiledir = self.test_dir = './var/run/zorp/' if not os.path.exists(self.test_dir): os.makedirs(self.test_dir) self.test_pid_file = 'zorp-testinstance#0.pid' pid_file = open(self.test_dir + self.test_pid_file, 'w') pid_file.write('123456') pid_file.close()
class TestSzig(unittest.TestCase): def setUp(self): self.szig = SZIG("", HandlerMock) def test_get_value(self): self.assertEquals(self.szig.get_value(""), None) self.assertEquals(self.szig.get_value("service"), None) self.assertEquals(self.szig.get_value("info.policy.file"), "/etc/zorp/policy.py") self.assertEquals(self.szig.get_value("stats.thread_number"), 5) self.assertEquals(self.szig.get_value("service.service_http_transparent.sessions_running"), 0) def test_get_sibling(self): self.assertEquals(self.szig.get_sibling("conns"), "info") self.assertEquals(self.szig.get_sibling("stats.threads_running"), "stats.thread_rate_max") self.assertEquals(self.szig.get_sibling("stats.thread_rate_max"), "stats.audit_number") self.assertEquals(self.szig.get_sibling("stats.thread_number"), None) def test_get_child(self): self.assertEquals(self.szig.get_child(""), "conns") self.assertEquals(self.szig.get_child("info"), "info.policy") self.assertEquals(self.szig.get_child("info.policy"), "info.policy.reload_stamp") self.assertEquals(self.szig.get_child("info.policy.reload_stamp"), None) def test_get_set_loglevel(self): loglevel = 6 self.szig.loglevel = loglevel self.assertEquals(self.szig.loglevel, loglevel) def test_get_set_logspec(self): logspec = "this is a logspec" self.szig.logspec = logspec self.assertEquals(self.szig.logspec, logspec) def test_get_set_deadlockcheck(self): deadlockcheck = False self.szig.deadlockcheck = deadlockcheck self.assertEquals(self.szig.deadlockcheck, deadlockcheck) def test_reload_and_reload_result(self): self.szig.reload() self.assertEquals(self.szig.reload_result(), True) def test_coredump(self): try: self.szig.coredump() self.assertTrue(False, "szig coredump should not work while not repaired") except: self.assertTrue(True, "szig coredump is not working yet")
def setUp(self): self.szig = SZIG("", HandlerMock)
class TestSzig(unittest.TestCase): def setUp(self): self.szig = SZIG("", HandlerMock) def test_get_value(self): self.assertEquals(self.szig.get_value(""), None) self.assertEquals(self.szig.get_value("service"), None) self.assertEquals(self.szig.get_value("info.policy.file"), "/etc/zorp/policy.py") self.assertEquals(self.szig.get_value("stats.thread_number"), 5) self.assertEquals( self.szig.get_value( "service.service_http_transparent.sessions_running"), 0) def test_get_sibling(self): self.assertEquals(self.szig.get_sibling("conns"), "info") self.assertEquals(self.szig.get_sibling("stats.threads_running"), "stats.thread_rate_max") self.assertEquals(self.szig.get_sibling("stats.thread_rate_max"), "stats.audit_number") self.assertEquals(self.szig.get_sibling("stats.thread_number"), None) def test_get_child(self): self.assertEquals(self.szig.get_child(""), "conns") self.assertEquals(self.szig.get_child("info"), "info.policy") self.assertEquals(self.szig.get_child("info.policy"), "info.policy.reload_stamp") self.assertEquals(self.szig.get_child("info.policy.reload_stamp"), None) def test_get_set_loglevel(self): loglevel = 6 self.szig.loglevel = loglevel self.assertEquals(self.szig.loglevel, loglevel) def test_get_set_logspec(self): logspec = "this is a logspec" self.szig.logspec = logspec self.assertEquals(self.szig.logspec, logspec) def test_get_set_deadlockcheck(self): deadlockcheck = False self.szig.deadlockcheck = deadlockcheck self.assertEquals(self.szig.deadlockcheck, deadlockcheck) def test_reload_and_reload_result(self): self.szig.reload() self.assertEquals(self.szig.reload_result(), True) def test_coredump(self): try: self.szig.coredump() self.assertTrue( False, "szig coredump should not work while not repaired") except: self.assertTrue(True, "szig coredump is not working yet")
def test_reload(self): handler_mock = HandlerMock szig = SZIG("", handler_mock) algorithm = ReloadAlgorithm() algorithm.szig = szig self.assertTrue(algorithm.reload())
def execute(self): try: self.szig = SZIG(self.instance.process_name) except IOError as e: return CommandResultFailure(e.message) return CommandResultSuccess("", self.get())
def setUp(self): handler_mock = HandlerMock szig = SZIG("", handler_mock) self.algorithm = DeadlockCheckAlgorithm() self.algorithm.szig = szig
def setUp(self): handler_mock = HandlerMock self.szig = SZIG("", handler_mock) self.data = handler_mock().data
def setUp(self): handler_mock = HandlerMock szig = SZIG("", handler_mock) self.algorithm = LogLevelAlgorithm() self.algorithm.szig = szig