Creating audit logs

The logs that our application is currently writing are banking transactions. Another level of logging may be needed to analyze operations the controller is performing. This can service either performance analysis, post-mortem analysis, and for generating audit reports.

To do this, let's code an aspect that logs all of controllers (except for the log operation itself).

  1. Code a simple aspect that logs anything except the log method using a copy of the controller.
    class AuditInterceptor(MethodInterceptor):
    def __init__(self, controller):
    self.controller = controller
    def invoke(self, invocation):
    results = invocation.proceed()
    if invocation.method_name != "log":
    self.controller.log("AUDIT", -1, "Method: %s Args: %s" % (
    invocation.method_name, invocation.args))
    return results
    

    For this code to work, add the following import statement:

    from springpython.aop import *
    
  2. Update the definition of the controller in the application context in order to embed this interceptor.
    @Object
    def controller(self):
    target = SpringBankController(self.factory())
    return ProxyFactoryObject(
    target=target,
    interceptors=AuditInterceptor(target))
    

    This update to the application context smoothly replaces the SpringBankController with a ProxyFactoryObject. This proxy points at the real controller object, while also plugging in an instance of the AuditInterceptor we just coded.

    Also notice that the AuditInterceptor needs a copy of the controller. However, using self.controller() like all the other methods would generate a recursive stack error.

  3. Let's use MySQL's command-line interface to look at the log table after several operations.

    This provides a nice view of what happened on a transaction level, and also on a lower level from an auditing perspective.

    It is left as an exercise for the reader to change the advice, so that only when a manager is viewing an account history does it write an audit log entry. It is also an exercise to build a view for the supervisor to view these audit trails.

Creating audit logs

This provides a nice view of what happened on a transaction level, and also on a lower level from an auditing perspective.

It is left as an exercise for the reader to change the advice, so that only when a manager is viewing an account history does it write an audit log entry. It is also an exercise to build a view for the supervisor to view these audit trails.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.219.63.95