Class: Datadog::AppSec::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/datadog/appsec/component.rb

Overview

Core-pluggable component for AppSec

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(processor, telemetry) ⇒ Component

Returns a new instance of Component.



80
81
82
83
84
85
# File 'lib/datadog/appsec/component.rb', line 80

def initialize(processor, telemetry)
  @processor = processor
  @telemetry = telemetry

  @mutex = Mutex.new
end

Instance Attribute Details

#processorObject (readonly)

Returns the value of attribute processor.



78
79
80
# File 'lib/datadog/appsec/component.rb', line 78

def processor
  @processor
end

#telemetryObject (readonly)

Returns the value of attribute telemetry.



78
79
80
# File 'lib/datadog/appsec/component.rb', line 78

def telemetry
  @telemetry
end

Class Method Details

.build_appsec_component(settings, telemetry:) ⇒ Object



13
14
15
16
17
18
19
20
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
# File 'lib/datadog/appsec/component.rb', line 13

def build_appsec_component(settings, telemetry:)
  return if !settings.respond_to?(:appsec) || !settings.appsec.enabled

  ffi_version = Gem.loaded_specs['ffi'] && Gem.loaded_specs['ffi'].version
  unless ffi_version
    Datadog.logger.warn('FFI gem is not loaded, AppSec will be disabled.')
    telemetry.error('AppSec: Component not loaded, due to missing FFI gem')

    return
  end

  if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.3') && ffi_version < Gem::Version.new('1.16.0')
    Datadog.logger.warn(
      'AppSec is not supported in Ruby versions above 3.3.0 when using `ffi` versions older than 1.16.0, ' \
      'and will be forcibly disabled due to a memory leak in `ffi`. ' \
      'Please upgrade your `ffi` version to 1.16.0 or higher.'
    )
    telemetry.error('AppSec: Component not loaded, ffi version is leaky with ruby > 3.3.0')

    return
  end

  processor = create_processor(settings, telemetry)

  # We want to always instrument user events when AppSec is enabled.
  # There could be cases in which users use the DD_APPSEC_ENABLED Env variable to
  # enable AppSec, in that case, Devise is already instrumented.
  # In the case that users do not use DD_APPSEC_ENABLED, we have to instrument it,
  # hence the lines above.
  devise_integration = Datadog::AppSec::Contrib::Devise::Integration.new
  settings.appsec.instrument(:devise) unless devise_integration.patcher.patched?

  new(processor, telemetry)
end

Instance Method Details

#reconfigure(ruleset:, telemetry:) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/datadog/appsec/component.rb', line 87

def reconfigure(ruleset:, telemetry:)
  @mutex.synchronize do
    new_processor = Processor.new(ruleset: ruleset, telemetry: telemetry)

    if new_processor && new_processor.ready?
      old_processor = @processor

      @telemetry = telemetry
      @processor = new_processor

      old_processor.finalize if old_processor
    end
  end
end

#reconfigure_lock(&block) ⇒ Object



102
103
104
# File 'lib/datadog/appsec/component.rb', line 102

def reconfigure_lock(&block)
  @mutex.synchronize(&block)
end

#shutdown!Object



106
107
108
109
110
111
112
113
# File 'lib/datadog/appsec/component.rb', line 106

def shutdown!
  @mutex.synchronize do
    if processor && processor.ready?
      processor.finalize
      @processor = nil
    end
  end
end