Module: Datadog::AppSec::Contrib::Rack::Gateway::Watcher

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

Overview

Watcher for Rack gateway events

Class Method Summary collapse

Class Method Details

.watchObject



16
17
18
19
20
21
22
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 16

def watch
  gateway = Instrumentation.gateway

  watch_request(gateway)
  watch_response(gateway)
  watch_request_body(gateway)
end

.watch_request(gateway = Instrumentation.gateway) ⇒ Object



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
65
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 24

def watch_request(gateway = Instrumentation.gateway)
  gateway.watch('rack.request', :appsec) do |stack, gateway_request|
    block = false
    event = nil
    scope = gateway_request.env[Datadog::AppSec::Ext::SCOPE_KEY]

    AppSec::Reactive::Operation.new('rack.request') do |op|
      Rack::Reactive::Request.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::Request.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_body(gateway = Instrumentation.gateway) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 111

def watch_request_body(gateway = Instrumentation.gateway)
  gateway.watch('rack.request.body', :appsec) do |stack, gateway_request|
    block = false

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

    AppSec::Reactive::Operation.new('rack.request.body') 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_response(gateway = Instrumentation.gateway) ⇒ Object



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
109
# File 'lib/datadog/appsec/contrib/rack/gateway/watcher.rb', line 67

def watch_response(gateway = Instrumentation.gateway)
  gateway.watch('rack.response', :appsec) do |stack, gateway_response|
    block = false

    event = nil
    scope = gateway_response.scope

    AppSec::Reactive::Operation.new('rack.response') do |op|
      Rack::Reactive::Response.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,
            response: gateway_response,
            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::Response.publish(op, gateway_response)
    end

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

    ret, res = stack.call(gateway_response.response)

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

    [ret, res]
  end
end