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(security_engine:) ⇒ Component

Returns a new instance of Component.



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

def initialize(security_engine:)
  @security_engine = security_engine
end

Instance Attribute Details

#security_engineObject (readonly)

Returns the value of attribute security_engine.



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

def security_engine
  @security_engine
end

Class Method Details

.build_appsec_component(settings, telemetry:) ⇒ Object



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/datadog/appsec/component.rb', line 14

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

  ffi_version = 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 RubyVersion.is?(">= 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

  require_libddwaf(telemetry: telemetry)
  Datadog::AppSec::WAF.logger = Datadog.logger if Datadog.logger.debug? && settings.appsec.waf_debug

  # 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?

  security_engine = SecurityEngine::Engine.new(appsec_settings: settings.appsec, telemetry: telemetry)
  new(security_engine: security_engine)
# NOTE: At this point we should capture all possible exceptions and
#       gracefully fail component initialization, preventing propagation
#       into the host application code.
rescue Exception => e # standard:disable Lint/RescueException
  Datadog.logger.warn("AppSec is disabled: #{e.class}: #{e.message}; there may be additional logged errors above")

  # Not reporting to telemetry here because some of the rescued exceptions
  # have already been reported by the code that raised them
  # (e.g. SecurityEngine::Engine.new reports WAF init failures).
  # TODO: reconsider whether telemetry reporting belongs here
  # (single catch-all) or in the downstream code (as it is now).
  nil
end

Instance Method Details

#reconfigure!Object



87
88
89
# File 'lib/datadog/appsec/component.rb', line 87

def reconfigure!
  security_engine&.reconfigure!
end

#shutdown!Object



91
92
93
# File 'lib/datadog/appsec/component.rb', line 91

def shutdown!
  # no-op
end