diff --git a/connection.py b/connection.py index c398c53..167978f 100644 --- a/connection.py +++ b/connection.py @@ -12,6 +12,16 @@ class Connection: self.verb = "" self.verb_details = "" + def log(self): + return {"conn_id": self.conn_id, + "tls": self.tls, + "client": self.client, + "time": self.time, + "fd": self.fd, + "op": self.op, + "verb": self.verb, + "verb_details": self.verb_details} + # Something happened, this method's job is to update the context def add_event(self, event): self.time = event['time'] @@ -29,6 +39,10 @@ class Connection: else: raise Exception('Failed to parse: {}'.format(verb_details)) + def add_tls(self, verb_details): + if verb_details.startswith('established'): + self.tls = True + def add_rest(self, rest): self.fd = "" self.op = "" @@ -50,8 +64,12 @@ class Connection: self.verb = match[3] self.verb_details = match[4] + # Some verbs have a special impact on a connection, so + # we handle those here to update that context. if self.verb == "ACCEPT": self.add_accept(self.verb_details) + elif self.verb == "TLS": + self.add_tls(self.verb_details) else: raise Exception('Failed to parse: {}'.format(rest)) diff --git a/test_connection.py b/test_connection.py index 7f303ae..5e0cbdf 100644 --- a/test_connection.py +++ b/test_connection.py @@ -148,3 +148,30 @@ class TestConnection(): assert connection.op == 1 assert connection.verb == "RESULT" assert connection.verb_details == 'tag=97 err=49 text=' + + def test_add_event_log(self): + event = {'time': 'Oct 26 12:46:58', + 'server': 'ldap.example.com', + 'process': 'slapd[11086]', + 'conn': '6862452', + 'rest': 'fd=34 ACCEPT from IP=192.168.1.1:56822 (IP=0.0.0.0:389)'} + + connection = Connection(1245) + connection.add_event(event) + assert connection.time == event["time"] + assert connection.server == event["server"] + assert connection.process == event["process"] + assert connection.conn_id == 1245 + assert connection.fd == 34 + assert connection.op == "" + assert connection.verb == "ACCEPT" + assert connection.verb_details == "from IP=192.168.1.1:56822 (IP=0.0.0.0:389)" + assert connection.client == "192.168.1.1" + assert connection.log() == {'client': '192.168.1.1', + 'conn_id': 1245, + 'fd': 34, + 'op': '', + 'time': 'Oct 26 12:46:58', + 'tls': False, + 'verb': 'ACCEPT', + 'verb_details': 'from IP=192.168.1.1:56822 (IP=0.0.0.0:389)'}