module TCellAgent::Instrumentation::Rails::DLPHandler

Public Class Methods

get_handler_and_context(request, response_headers) click to toggle source
# File lib/tcell_agent/rails/dlp_handler.rb, line 36
def self.get_handler_and_context(request, response_headers)
  dlp_handler = nil
  tcell_context = nil

  TCellAgent::Instrumentation.safe_block('DLP Handler get handler and context') do
    if TCellAgent.configuration.should_instrument? &&
       TCellAgent.configuration.should_intercept_requests? &&
       TCellAgent::Utils::Rails.processable_response?(response_headers)

      # do all this work so that dlp doesn't run at all unless it's on and there
      # are rules to run
      dlp_policy = TCellAgent.policy(TCellAgent::PolicyTypes::DATALOSS)
      if dlp_policy && dlp_policy.get_actions_for_session_id
        tcell_context = request.env[TCellAgent::Instrumentation::TCELL_ID]
        if tcell_context && tcell_context.session_id
          dlp_handler = proc { |tc, resp|
            handle_dlp!(tc, resp)
          }
        end
      end
    end
  end

  [dlp_handler, tcell_context]
end
handle_dlp!(tcell_context, response) click to toggle source
# File lib/tcell_agent/rails/dlp_handler.rb, line 28
def self.handle_dlp!(tcell_context, response)
  TCellAgent::Instrumentation.safe_block('Running DLP Logging Filters') do
    tcell_context.filter_body!(response)
  end

  response
end
report_and_redact_now(dlp_handler, tcell_context, rack_body, content_length) click to toggle source
# File lib/tcell_agent/rails/dlp_handler.rb, line 8
def self.report_and_redact_now(dlp_handler, tcell_context, rack_body, content_length)
  TCellAgent::Instrumentation.safe_block('Handling DLP Report and Redact Now') do
    if dlp_handler
      new_content_length = 0
      new_body = []
      rack_body.each do |str|
        dlp_handler.call(tcell_context, str)
        new_body << str
        new_content_length += str.bytesize
      end
      rack_body.close if rack_body.respond_to?(:close)

      rack_body = new_body
      content_length = new_content_length
    end
  end

  [rack_body, content_length]
end