Skip to content

Instantly share code, notes, and snippets.

@velobuff
Forked from derwiki/README.md
Created March 9, 2016 21:51
Show Gist options
  • Save velobuff/800b3335cdde116cb75a to your computer and use it in GitHub Desktop.
Save velobuff/800b3335cdde116cb75a to your computer and use it in GitHub Desktop.

Revisions

  1. @derwiki derwiki revised this gist Feb 26, 2016. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Adding an audit log to your Rails app

    If you have any sort of administrative interface on your web site, you can easily imagine an intruder gaining access and mucking about. How do you know the extent of the damage? Adding an audit log to your app is one quick solution. An audit log should record a few things:

    - controller entry points with parameter values
  2. @derwiki derwiki revised this gist Feb 26, 2016. 1 changed file with 7 additions and 0 deletions.
    7 changes: 7 additions & 0 deletions README.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    If you have any sort of administrative interface on your web site, you can easily imagine an intruder gaining access and mucking about. How do you know the extent of the damage? Adding an audit log to your app is one quick solution. An audit log should record a few things:

    - controller entry points with parameter values
    - permanent information about the user, like user_id
    - transient information about the user, like IP and user_agent

    Using the Rails framework, this is as simple as adding a `before_action` to your admin controllers. Here’s a basic version that I’m using in production.
  3. @derwiki derwiki revised this gist Feb 26, 2016. 1 changed file with 6 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions admin_controller.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,6 @@
    require 'audit_log'

    class AdminController < ApplicationController
    include AuditLog
    before_action :auto_log
    end
  4. @derwiki derwiki created this gist Feb 26, 2016.
    25 changes: 25 additions & 0 deletions audit_log.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    require 'geoip'

    module AuditLog
    def geoip
    @@geoip ||= GeoIP.new(File.join(Rails.root, '/lib/GeoIPCity.dat'))
    end

    def auto_log
    rails_action = "#{ params[:controller] }##{ params[:action] }"
    rails_params = params.except(:controller, :action)

    details = {
    :logger => 'AuditLog',
    :action => rails_action,
    :ip_address => request.remote_ip,
    :geo_ip => geoip.city(request.remote_ip).to_h,
    :user_id => current_user&.id,
    :user_email => current_user&.email,
    :user_name => current_user&.name,
    :params => rails_params,
    :user_agent => request.user_agent
    }
    Rails.logger.info MultiJson.dump(details)
    end
    end