-
Notifications
You must be signed in to change notification settings - Fork 373
/
watcher.rb
145 lines (121 loc) · 5.28 KB
/
watcher.rb
1
2
3
4
5
6
7
8
9
10
11
12
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
109
110
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
# frozen_string_literal: true
require_relative '../../../instrumentation/gateway'
require_relative '../../../reactive/engine'
require_relative '../reactive/request'
require_relative '../reactive/request_body'
require_relative '../reactive/response'
require_relative '../../../event'
module Datadog
module AppSec
module Contrib
module Rack
module Gateway
# Watcher for Rack gateway events
module Watcher
class << self
def watch
gateway = Instrumentation.gateway
watch_request(gateway)
watch_response(gateway)
watch_request_body(gateway)
end
def watch_request(gateway = Instrumentation.gateway)
gateway.watch('rack.request', :appsec) do |stack, gateway_request|
event = nil
scope = gateway_request.env[Datadog::AppSec::Ext::SCOPE_KEY]
engine = AppSec::Reactive::Engine.new
Rack::Reactive::Request.subscribe(engine, 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
}
# 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 = Rack::Reactive::Request.publish(engine, gateway_request)
next [nil, [[:block, event]]] if block
ret, res = stack.call(gateway_request.request)
if event
res ||= []
res << [:monitor, event]
end
[ret, res]
end
end
def watch_response(gateway = Instrumentation.gateway)
gateway.watch('rack.response', :appsec) do |stack, gateway_response|
event = nil
scope = gateway_response.scope
engine = AppSec::Reactive::Engine.new
Rack::Reactive::Response.subscribe(engine, 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
}
# 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 = Rack::Reactive::Response.publish(engine, gateway_response)
next [nil, [[:block, event]]] if block
ret, res = stack.call(gateway_response.response)
if event
res ||= []
res << [:monitor, event]
end
[ret, res]
end
end
def watch_request_body(gateway = Instrumentation.gateway)
gateway.watch('rack.request.body', :appsec) do |stack, gateway_request|
event = nil
scope = gateway_request.env[Datadog::AppSec::Ext::SCOPE_KEY]
engine = AppSec::Reactive::Engine.new
Rack::Reactive::RequestBody.subscribe(engine, 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
}
# 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 = Rack::Reactive::RequestBody.publish(engine, gateway_request)
next [nil, [[:block, event]]] if block
ret, res = stack.call(gateway_request.request)
if event
res ||= []
res << [:monitor, event]
end
[ret, res]
end
end
end
end
end
end
end
end
end