Module: Datadog::AppSec::Contrib::ActiveRecord::Patcher

Defined in:
lib/datadog/appsec/contrib/active_record/patcher.rb

Overview

AppSec patcher module for ActiveRecord

Class Method Summary collapse

Class Method Details

.patchObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/datadog/appsec/contrib/active_record/patcher.rb', line 21

def patch
  # Rails 7.0 intruduced new on-load hooks for sqlite3 and postgresql adapters
  # The load hook for mysql2 adapter was introduced in Rails 7.1
  #
  # If the adapter is not loaded when the :active_record load hook is called,
  # we need to add a load hook for the adapter
  ActiveSupport.on_load :active_record do
    if defined?(::ActiveRecord::ConnectionAdapters::SQLite3Adapter)
      ::Datadog::AppSec::Contrib::ActiveRecord::Patcher.patch_sqlite3_adapter
    else
      ActiveSupport.on_load :active_record_sqlite3adapter do
        ::Datadog::AppSec::Contrib::ActiveRecord::Patcher.patch_sqlite3_adapter
      end
    end

    if defined?(::ActiveRecord::ConnectionAdapters::Mysql2Adapter)
      ::Datadog::AppSec::Contrib::ActiveRecord::Patcher.patch_mysql2_adapter
    else
      ActiveSupport.on_load :active_record_mysql2adapter do
        ::Datadog::AppSec::Contrib::ActiveRecord::Patcher.patch_mysql2_adapter
      end
    end

    if defined?(::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter)
      ::Datadog::AppSec::Contrib::ActiveRecord::Patcher.patch_postgresql_adapter
    else
      ActiveSupport.on_load :active_record_postgresqladapter do
        ::Datadog::AppSec::Contrib::ActiveRecord::Patcher.patch_postgresql_adapter
      end
    end
  end
end

.patch_mysql2_adapterObject



66
67
68
69
70
71
72
73
74
75
76
# File 'lib/datadog/appsec/contrib/active_record/patcher.rb', line 66

def patch_mysql2_adapter
  instrumentation_module = if ::ActiveRecord.gem_version >= Gem::Version.new('7.1')
                             Instrumentation::InternalExecQueryAdapterPatch
                           elsif ::ActiveRecord.gem_version.segments.first == 4
                             Instrumentation::Rails4ExecQueryAdapterPatch
                           else
                             Instrumentation::ExecQueryAdapterPatch
                           end

  ::ActiveRecord::ConnectionAdapters::Mysql2Adapter.prepend(instrumentation_module)
end

.patch_postgresql_adapterObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/datadog/appsec/contrib/active_record/patcher.rb', line 78

def patch_postgresql_adapter
  instrumentation_module = if ::ActiveRecord.gem_version.segments.first == 4
                             Instrumentation::Rails4ExecuteAndClearAdapterPatch
                           else
                             Instrumentation::ExecuteAndClearAdapterPatch
                           end

  if defined?(::ActiveRecord::ConnectionAdapters::JdbcAdapter)
    instrumentation_module = if ::ActiveRecord.gem_version >= Gem::Version.new('7.1')
                               Instrumentation::InternalExecQueryAdapterPatch
                             elsif ::ActiveRecord.gem_version.segments.first == 4
                               Instrumentation::Rails4ExecQueryAdapterPatch
                             else
                               Instrumentation::ExecQueryAdapterPatch
                             end
  end

  ::ActiveRecord::ConnectionAdapters::PostgreSQLAdapter.prepend(instrumentation_module)
end

.patch_sqlite3_adapterObject



54
55
56
57
58
59
60
61
62
63
64
# File 'lib/datadog/appsec/contrib/active_record/patcher.rb', line 54

def patch_sqlite3_adapter
  instrumentation_module = if ::ActiveRecord.gem_version >= Gem::Version.new('7.1')
                             Instrumentation::InternalExecQueryAdapterPatch
                           elsif ::ActiveRecord.gem_version.segments.first == 4
                             Instrumentation::Rails4ExecQueryAdapterPatch
                           else
                             Instrumentation::ExecQueryAdapterPatch
                           end

  ::ActiveRecord::ConnectionAdapters::SQLite3Adapter.prepend(instrumentation_module)
end

.patched?Boolean

Returns:

  • (Boolean)


13
14
15
# File 'lib/datadog/appsec/contrib/active_record/patcher.rb', line 13

def patched?
  Patcher.instance_variable_get(:@patched)
end

.target_versionObject



17
18
19
# File 'lib/datadog/appsec/contrib/active_record/patcher.rb', line 17

def target_version
  Integration.version
end