[Kimchi-devel] [PATCH] [Wok 1/3] Include user IP address in request log messages

Lucio Correia luciojhc at linux.vnet.ibm.com
Thu Jun 9 18:24:13 UTC 2016


* Allow advanced log searches by IP address
* Add RECORD_TEMPLATE_DICT to avoid versioning errors
with older log entries, which don't have IP address.
* Remove duplicated comment line

Signed-off-by: Lucio Correia <luciojhc at linux.vnet.ibm.com>
---
 docs/API/logs.md        |  1 +
 src/wok/control/base.py | 10 ++++++----
 src/wok/reqlogger.py    | 17 ++++++++++++++---
 src/wok/root.py         |  6 ++++--
 4 files changed, 25 insertions(+), 9 deletions(-)

diff --git a/docs/API/logs.md b/docs/API/logs.md
index f808e1a..fcfb3b2 100644
--- a/docs/API/logs.md
+++ b/docs/API/logs.md
@@ -13,6 +13,7 @@
         * req: Filter entries by type of request: "DELETE", "POST", "PUT".
                "GET" requests are not logged.
         * user: Filter entries by user that performed the request.
+        * ip: Filter entries by user IP address, i.e. 127.0.0.1
         * date: Filter entries by date of record in the format "YYYY-MM-DD"
         * download: Generate text file for download of search results.
 
diff --git a/src/wok/control/base.py b/src/wok/control/base.py
index 4b459ed..419179a 100644
--- a/src/wok/control/base.py
+++ b/src/wok/control/base.py
@@ -146,7 +146,8 @@ class Resource(object):
                     msg,
                     app=get_plugin_from_request(),
                     req=method,
-                    user=cherrypy.session.get(USER_NAME, 'N/A')
+                    user=cherrypy.session.get(USER_NAME, 'N/A'),
+                    ip=cherrypy.request.remote.ip
                 ).log()
 
                 if destructive is False or \
@@ -227,7 +228,8 @@ class Resource(object):
                 msg,
                 app=get_plugin_from_request(),
                 req=method,
-                user=cherrypy.session.get(USER_NAME, 'N/A')
+                user=cherrypy.session.get(USER_NAME, 'N/A'),
+                ip=cherrypy.request.remote.ip
             ).log()
 
         return result
@@ -374,7 +376,6 @@ class Collection(object):
                 except Exception as e:
                     # In case of errors when fetching a resource info, pass and
                     # log the error, so, other resources are returned
-                    # log the error, so, other resources are returned.
                     # Encoding error message as ident is also encoded value.
                     # This has to be done to avoid unicode error,
                     # as combination of encoded and unicode value results into
@@ -455,7 +456,8 @@ class Collection(object):
                     msg,
                     app=get_plugin_from_request(),
                     req=method,
-                    user=cherrypy.session.get(USER_NAME, 'N/A')
+                    user=cherrypy.session.get(USER_NAME, 'N/A'),
+                    ip=cherrypy.request.remote.ip
                 ).log()
 
                 return result
diff --git a/src/wok/reqlogger.py b/src/wok/reqlogger.py
index bbd1bd0..2ec62b8 100644
--- a/src/wok/reqlogger.py
+++ b/src/wok/reqlogger.py
@@ -34,10 +34,20 @@ from wok.utils import ascii_dict, remove_old_files
 
 
 # Log search setup
-FILTER_FIELDS = ['app', 'date', 'download', 'req', 'user', 'time']
+FILTER_FIELDS = ['app', 'date', 'download', 'ip', 'req', 'user', 'time']
 LOG_DOWNLOAD_URI = "/data/logs/%s"
 LOG_DOWNLOAD_TIMEOUT = 6
-LOG_FORMAT = "[%(date)s %(time)s] %(req)-6s %(app)-11s %(user)s: %(message)s\n"
+LOG_FORMAT = "[%(date)s %(time)s] %(req)-6s %(app)-11s %(ip)-15s %(user)s: " \
+             "%(message)s\n"
+RECORD_TEMPLATE_DICT = {
+    'date': '',
+    'time': '',
+    'req': '',
+    'app': '',
+    'ip': '',
+    'user': '',
+    'message': '',
+}
 SECONDS_PER_HOUR = 360
 
 # Log handler setup
@@ -86,7 +96,8 @@ class RequestParser(object):
 
             with fd:
                 for record in sortedList:
-                    asciiRecord = ascii_dict(record)
+                    asciiRecord = RECORD_TEMPLATE_DICT
+                    asciiRecord.update(ascii_dict(record))
                     fd.write(LOG_FORMAT % asciiRecord)
 
                 fd.close()
diff --git a/src/wok/root.py b/src/wok/root.py
index 29ea657..902e203 100644
--- a/src/wok/root.py
+++ b/src/wok/root.py
@@ -172,7 +172,8 @@ class WokRoot(Root):
                 msg,
                 app=get_plugin_from_request(),
                 req=method,
-                user=cherrypy.session.get(auth.USER_NAME, 'N/A')
+                user=cherrypy.session.get(auth.USER_NAME, 'N/A'),
+                ip=cherrypy.request.remote.ip
             ).log()
 
         return json.dumps(user_info)
@@ -187,7 +188,8 @@ class WokRoot(Root):
             msg,
             app=get_plugin_from_request(),
             req=method,
-            user=params['username']
+            user=params['username'],
+            ip=cherrypy.request.remote.ip
         ).log()
 
         auth.logout()
-- 
1.9.1




More information about the Kimchi-devel mailing list