Module: Datadog::AppSec::Monitor::Gateway::Watcher

Defined in:
lib/datadog/appsec/monitor/gateway/watcher.rb

Overview

Watcher for Apssec internal events

Class Method Summary collapse

Class Method Details

.watchObject



14
15
16
17
18
# File 'lib/datadog/appsec/monitor/gateway/watcher.rb', line 14

def watch
  gateway = Instrumentation.gateway

  watch_user_id(gateway)
end

.watch_user_id(gateway = Instrumentation.gateway) ⇒ Object



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
# File 'lib/datadog/appsec/monitor/gateway/watcher.rb', line 20

def watch_user_id(gateway = Instrumentation.gateway)
  gateway.watch('identity.set_user', :appsec) do |stack, user|
    block = false
    event = nil
    scope = Datadog::AppSec.active_scope

    AppSec::Reactive::Operation.new('identity.set_user') do |op|
      Monitor::Reactive::SetUser.subscribe(op, scope.processor_context) do |result|
        if result.status == :match
          # TODO: should this hash be an Event instance instead?
          event = {
            waf_result: result,
            trace: scope.trace,
            span: scope.service_entry_span,
            user: user,
            actions: result.actions
          }

          # We want to keep the trace in case of security event
          scope.trace.keep! if scope.trace
          Datadog::AppSec::Event.tag_and_keep!(scope, result)
          scope.processor_context.events << event
        end
      end

      block = Monitor::Reactive::SetUser.publish(op, user)
    end

    throw(Datadog::AppSec::Ext::INTERRUPT, [nil, [[:block, event]]]) if block

    ret, res = stack.call(user)

    if event
      res ||= []
      res << [:monitor, event]
    end

    [ret, res]
  end
end