<div dir="ltr">Hello,<br><div class="gmail_extra"><br><div class="gmail_quote">On Sat, Jul 18, 2015 at 2:23 AM, Christian Boltz <span dir="ltr"><<a href="mailto:apparmor@cboltz.de" target="_blank">apparmor@cboltz.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hello,<br>
<br>
add_event_to_tree() is a hard-to-test function because it hands over its<br>
result to add_to_tree().<br>
<br>
This patch converts add_event_to_tree() to a simple wrapper function and<br>
moves the main code into parse_event_for_tree() and map_log_type(). These<br>
two new functions return their results and are therefore easier to test.<br>
<br>
<br>
<br>
[ 77-split-logparser-add_event_to_tree.diff ]<br>
<br>
diff -ru '--exclude=.bzr' ../HEAD-patches-applied/utils/apparmor/logparser.py ./utils/apparmor/logparser.py<br>
--- utils/apparmor/logparser.py 2015-07-17 22:43:21.977879320 +0200<br>
+++ ./utils/apparmor/logparser.py       2015-07-17 22:45:14.380287480 +0200<br>
@@ -180,23 +181,35 @@<br>
         #print("log",self.log)<br>
<br>
     def add_event_to_tree(self, e):<br>
-        aamode = e.get('aamode', 'UNKNOWN')<br>
-        if e.get('type', False):<br>
+        e = self.parse_event_for_tree(e)<br>
+        if e is not None:<br>
+            (pid, parent, mode, details) = e<br>
+            self.add_to_tree(pid, parent, mode, details)<br>
+<br>
+    def map_log_type(self, type):<br></blockquote><div><br>"type" is a keyword in Python so using it/overriding it in this scope is not a good idea and we can probably avoid it. How about "log_type" instead?<br><br></div><div>I'm aware that "type" has been incorrectly used as a variable at other places, but two wrongs don't make a right ;-)<br><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
-            if re.search('(UNKNOWN\[1501\]|APPARMOR_AUDIT|1501)', e['type']):<br>
+            if re.search('(UNKNOWN\[1501\]|APPARMOR_AUDIT|1501)', type):<br>
                 aamode = 'AUDIT'<br>
-            elif re.search('(UNKNOWN\[1502\]|APPARMOR_ALLOWED|1502)', e['type']):<br>
+            elif re.search('(UNKNOWN\[1502\]|APPARMOR_ALLOWED|1502)', type):<br>
                 aamode = 'PERMITTING'<br>
-            elif re.search('(UNKNOWN\[1503\]|APPARMOR_DENIED|1503)', e['type']):<br>
+            elif re.search('(UNKNOWN\[1503\]|APPARMOR_DENIED|1503)', type):<br>
                 aamode = 'REJECTING'<br>
-            elif re.search('(UNKNOWN\[1504\]|APPARMOR_HINT|1504)', e['type']):<br>
+            elif re.search('(UNKNOWN\[1504\]|APPARMOR_HINT|1504)', type):<br>
                 aamode = 'HINT'<br>
-            elif re.search('(UNKNOWN\[1505\]|APPARMOR_STATUS|1505)', e['type']):<br>
+            elif re.search('(UNKNOWN\[1505\]|APPARMOR_STATUS|1505)', type):<br>
                 aamode = 'STATUS'<br>
-            elif re.search('(UNKNOWN\[1506\]|APPARMOR_ERROR|1506)', e['type']):<br>
+            elif re.search('(UNKNOWN\[1506\]|APPARMOR_ERROR|1506)', type):<br>
                 aamode = 'ERROR'<br>
             else:<br>
                 aamode = 'UNKNOWN'<br>
<br>
+            return aamode<br>
+<br>
+    def parse_event_for_tree(self, e):<br>
+        aamode = e.get('aamode', 'UNKNOWN')<br>
+<br>
+        if e.get('type', False):<br>
+            aamode = self.map_log_type(e['type'])<br>
+<br>
         if aamode in ['UNKNOWN', 'AUDIT', 'STATUS', 'ERROR']:<br>
             return None<br>
<br>
@@ -240,13 +254,13 @@<br>
             e['request_mask'], e['name2'] = log_str_to_mode(e['profile'], e['request_mask'], e['name2'])<br>
<br>
             if e.get('info', False) and e['info'] == 'mandatory profile missing':<br>
-                self.add_to_tree(e['pid'], e['parent'], 'exec',<br>
+                return(e['pid'], e['parent'], 'exec',<br>
                                  [profile, hat, aamode, 'PERMITTING', e['denied_mask'], e['name'], e['name2']])<br>
             elif (e.get('name2', False) and '//null-' in e['name2']) or e.get('name', False):<br>
-                self.add_to_tree(e['pid'], e['parent'], 'exec',<br>
+                return(e['pid'], e['parent'], 'exec',<br>
                                  [profile, hat, prog, aamode, e['denied_mask'], e['name'], ''])<br>
             else:<br>
-                self.debug_logger.debug('add_event_to_tree: dropped exec event in %s' % e['profile'])<br>
+                self.debug_logger.debug('parse_event_for_tree: dropped exec event in %s' % e['profile'])<br>
<br>
         elif ( e['operation'].startswith('file_') or e['operation'].startswith('inode_') or<br>
             e['operation'] in ['open', 'truncate', 'mkdir', 'mknod', 'chmod', 'rename_src',<br>
@@ -286,14 +300,14 @@<br>
                         self.throw_away_next_log_entry()<br>
<br>
             if is_domain_change:<br>
-                self.add_to_tree(e['pid'], e['parent'], 'exec',<br>
+                return(e['pid'], e['parent'], 'exec',<br>
                                  [profile, hat, prog, aamode, e['denied_mask'], e['name'], e['name2']])<br>
             else:<br>
-                self.add_to_tree(e['pid'], e['parent'], 'path',<br>
+                return(e['pid'], e['parent'], 'path',<br>
                                  [profile, hat, prog, aamode, e['denied_mask'], e['name'], ''])<br>
<br>
         elif e['operation'] == 'capable':<br>
-            self.add_to_tree(e['pid'], e['parent'], 'capability',<br>
+            return(e['pid'], e['parent'], 'capability',<br>
                              [profile, hat, prog, aamode, e['name'], ''])<br>
<br>
         elif e['operation'] == 'clone':<br>
@@ -317,10 +331,10 @@<br>
 #             self.pid[child] = arrayref<br>
<br>
         elif self.op_type(e['operation']) == 'net':<br>
-            self.add_to_tree(e['pid'], e['parent'], 'netdomain',<br>
+            return(e['pid'], e['parent'], 'netdomain',<br>
                              [profile, hat, prog, aamode, e['family'], e['sock_type'], e['protocol']])<br>
         elif e['operation'] == 'change_hat':<br>
-            self.add_to_tree(e['pid'], e['parent'], 'unknown_hat',<br>
+            return(e['pid'], e['parent'], 'unknown_hat',<br>
                              [profile, hat, aamode, hat])<br>
         else:<br>
             self.debug_logger.debug('UNHANDLED: %s' % e)<br>
<br></blockquote><div><span class="im"><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex"><br></blockquote></span><div>Looks fine to me. Haven't tested it though.<br><br>Thanks for the patch.<br><br>Acked-by: Kshitij Gupta <<a href="mailto:kgupta8592@gmail.com" target="_blank">kgupta8592@gmail.com</a>>.  </div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<br>
Regards,<br>
<br>
Christian Boltz<br>
<span class="HOEnZb"><font color="#888888">--<br>
A qualified candidate would display the following characteristics:<br>
[...] willing to apply the rules to everybody; primary goal is to<br>
safeguard quality, not friendship :)    You're even allowed to<br>
decline coolo's request! [Craig Gardner in opensuse-packaging]<br>
<br>
<br>
--<br>
AppArmor mailing list<br>
<a href="mailto:AppArmor@lists.ubuntu.com">AppArmor@lists.ubuntu.com</a><br>
Modify settings or unsubscribe at: <a href="https://lists.ubuntu.com/mailman/listinfo/apparmor" rel="noreferrer" target="_blank">https://lists.ubuntu.com/mailman/listinfo/apparmor</a><br>
</font></span></blockquote></div><br><br clear="all"><br>-- <br><div class="gmail_signature"><div dir="ltr"><div>Regards,<br><br></div>Kshitij Gupta<br></div></div>
</div></div>