Module: Datadog::AppSec::Contrib::Sinatra::Gateway::Watcher

Defined in:
lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb

Overview

Watcher for Sinatra gateway events

Class Method Summary collapse

Class Method Details

.watchObject



15
16
17
18
19
20
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 15

def watch
  gateway = Instrumentation.gateway

  watch_request_dispatch(gateway)
  watch_request_routed(gateway)
end

.watch_request_dispatch(gateway = Instrumentation.gateway) ⇒ Object



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
62
63
64
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 22

def watch_request_dispatch(gateway = Instrumentation.gateway)
  gateway.watch('sinatra.request.dispatch', :appsec) do |stack, gateway_request|
    block = false

    event = nil
    scope = gateway_request.env[Datadog::AppSec::Ext::SCOPE_KEY]

    AppSec::Reactive::Operation.new('sinatra.request.dispatch') do |op|
      Rack::Reactive::RequestBody.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,
            request: gateway_request,
            actions: result.actions
          }

          if scope.service_entry_span
            scope.service_entry_span.set_tag('appsec.blocked', 'true') if result.actions.include?('block')
            scope.service_entry_span.set_tag('appsec.event', 'true')
          end

          scope.processor_context.events << event
        end
      end

      block = Rack::Reactive::RequestBody.publish(op, gateway_request)
    end

    next [nil, [[:block, event]]] if block

    ret, res = stack.call(gateway_request.request)

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

    [ret, res]
  end
end

.watch_request_routed(gateway = Instrumentation.gateway) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/datadog/appsec/contrib/sinatra/gateway/watcher.rb', line 66

def watch_request_routed(gateway = Instrumentation.gateway)
  gateway.watch('sinatra.request.routed', :appsec) do |stack, (gateway_request, gateway_route_params)|
    block = false

    event = nil
    scope = gateway_request.env[Datadog::AppSec::Ext::SCOPE_KEY]

    AppSec::Reactive::Operation.new('sinatra.request.routed') do |op|
      Sinatra::Reactive::Routed.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,
            request: gateway_request,
            actions: result.actions
          }

          if scope.service_entry_span
            scope.service_entry_span.set_tag('appsec.blocked', 'true') if result.actions.include?('block')
            scope.service_entry_span.set_tag('appsec.event', 'true')
          end

          scope.processor_context.events << event
        end
      end

      block = Sinatra::Reactive::Routed.publish(op, [gateway_request, gateway_route_params])
    end

    next [nil, [[:block, event]]] if block

    ret, res = stack.call(gateway_request.request)

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

    [ret, res]
  end
end