module TCellAgent::Instrumentation::Rails

Public Class Methods

create_tcell_route(route) click to toggle source
# File lib/tcell_agent/rails/routes.rb, line 79
def self.create_tcell_route(route)
  return TCellRoute5.new(route) if route && ::Rails::VERSION::MAJOR >= 5
  return TCellRoute4.new(route) if route && ::Rails::VERSION::MAJOR < 5

  TCellRoute.new
end
framework_details() click to toggle source
# File lib/tcell_agent/rails/settings_reporter.rb, line 9
def self.framework_details
  { 'app_framework' => 'Rails',
    'app_framework_version' => ::Rails.version }
end
instrument_route(route) click to toggle source
# File lib/tcell_agent/rails/routes.rb, line 96
def self.instrument_route(route)
  return unless TCellAgent.configuration.should_instrument?

  tcell_route = create_tcell_route(route)

  return unless tcell_route.report?

  if tcell_route.grape_route?
    TCellAgent::Instrumentation.instrument_grape_api(tcell_route.route_path, tcell_route.grape_routes)

  else
    tcell_route.route_methods.each do |route_method|
      route_id =
        TCellAgent::SensorEvents::Util.calculate_route_id(route_method, tcell_route.route_path_raw)
      TCellAgent.send_event(
        TCellAgent::SensorEvents::AppRoutesSensorEvent.new(
          tcell_route.route_path, route_method, route_id, nil, tcell_route.route_destination
        )
      )
    end
  end
end
instrument_routes() click to toggle source
# File lib/tcell_agent/rails/routes.rb, line 86
def self.instrument_routes
  return unless TCellAgent.configuration.should_instrument?

  return unless ::Rails.application

  ::Rails.application.routes.routes.each do |route|
    instrument_route(route)
  end
end
send_settings() click to toggle source
# File lib/tcell_agent/rails/settings_reporter.rb, line 14
def self.send_settings
  TCellAgent::Instrumentation.safe_block('Reporting Rails settings') do
    rails_config = ::Rails.application.config

    # Defaults to true
    csrf_protection = rails_config.action_controller.allow_forgery_protection || true
    TCellAgent.send_event(
      TCellAgent::SensorEvents::AppConfigSettingEvent.new(
        'Rails', 'core', '', 'csrf_protection', csrf_protection
      )
    )

    # Defaults to false if nil
    mass_assignment_allowed = rails_config.action_controller.permit_all_parameters || false
    TCellAgent.send_event(
      TCellAgent::SensorEvents::AppConfigSettingEvent.new(
        'Rails', 'core', '', 'mass_assignment_allowed', mass_assignment_allowed
      )
    )

    # Defaults to never
    session_expire = rails_config.session_options[:expire_after] || -1
    TCellAgent.send_event(
      TCellAgent::SensorEvents::AppConfigSettingEvent.new(
        'Rails', 'session', '', 'timeout', session_expire
      )
    )
  end
end

Public Instance Methods

add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true) click to toggle source
# File lib/tcell_agent/rails/routes.rb, line 122
def add_route(app, conditions = {}, requirements = {}, defaults = {}, name = nil, anchor = true)
  route = tcell_add_route(app, conditions, requirements, defaults, name, anchor)

  TCellAgent::Instrumentation::Rails.instrument_route(route)

  route
end
call(env) click to toggle source
# File lib/tcell_agent/rails/routes.rb, line 218
def call(env)
  env['PATH_INFO'] = ActionDispatch::Journey::Router::Utils.normalize_path(env['PATH_INFO'])

  if TCellAgent.configuration.should_intercept_requests?
    TCellAgent::Instrumentation.safe_block('Determining Rails Route ID') do
      _match, parameters, route = find_routes(env).first

      TCellAgent::Instrumentation::RouteId.update_context(env, parameters, route)
    end

    tcell_request = Rack::Request.new(env)

    if TCellAgent::Instrumentation::Patches.block?(tcell_request)
      return [403, {}, []]
    end

    TCellAgent::DLP.handle_request_dlp_parameters(tcell_request)
  end

  tcell_call(env)
end
serve(req) click to toggle source
# File lib/tcell_agent/rails/routes.rb, line 193
def serve(req)
  if TCellAgent.configuration.should_intercept_requests?
    TCellAgent::Instrumentation.safe_block('Determining Rails Route ID') do
      _match, parameters, route = find_routes(req).first

      TCellAgent::Instrumentation::RouteId.update_context(req.env, parameters, route)
    end

    if TCellAgent::Instrumentation::Patches.block?(req)
      return [403, {}, []]
    end

    TCellAgent::DLP.handle_request_dlp_parameters(req)
  end

  tcell_serve(req)
end
tcell_around_filter_routes() { || ... } click to toggle source
# File lib/tcell_agent/rails/routes.rb, line 138
def tcell_around_filter_routes
  if TCellAgent.configuration.should_intercept_requests?
    TCellAgent::Instrumentation.safe_block('Determining Rails Route ID') do
      _match, parameters, route = ::Rails.application.routes.router.recognize(request) { |r, _| r }.first

      TCellAgent::Instrumentation::RouteId.update_context(env, parameters, route)
    end

    if TCellAgent::Instrumentation::Patches.block?(request)
      return head(403)
    end

    TCellAgent::DLP.handle_request_dlp_parameters(request)
  end

  yield
end