tcell_agent 1.1.3 → 1.1.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (118) hide show
  1. checksums.yaml +4 -4
  2. data/bin/tcell_agent +10 -2
  3. data/lib/tcell_agent.rb +3 -3
  4. data/lib/tcell_agent/agent.rb +42 -52
  5. data/lib/tcell_agent/agent/event_processor.rb +129 -162
  6. data/lib/tcell_agent/agent/fork_pipe_manager.rb +57 -62
  7. data/lib/tcell_agent/agent/policy_manager.rb +83 -104
  8. data/lib/tcell_agent/agent/policy_types.rb +24 -29
  9. data/lib/tcell_agent/agent/route_manager.rb +36 -46
  10. data/lib/tcell_agent/agent/static_agent.rb +19 -21
  11. data/lib/tcell_agent/api.rb +23 -28
  12. data/lib/tcell_agent/appsensor/injections_reporter.rb +7 -11
  13. data/lib/tcell_agent/authlogic.rb +7 -7
  14. data/lib/tcell_agent/cmdi.rb +22 -23
  15. data/lib/tcell_agent/config/unknown_options.rb +71 -69
  16. data/lib/tcell_agent/configuration.rb +187 -191
  17. data/lib/tcell_agent/devise.rb +13 -15
  18. data/lib/tcell_agent/hooks/login_fraud.rb +1 -1
  19. data/lib/tcell_agent/instrumentation.rb +120 -124
  20. data/lib/tcell_agent/logger.rb +29 -45
  21. data/lib/tcell_agent/patches.rb +5 -5
  22. data/lib/tcell_agent/policies/dataloss_policy.rb +263 -288
  23. data/lib/tcell_agent/policies/http_redirect_policy.rb +25 -37
  24. data/lib/tcell_agent/policies/http_tx_policy.rb +48 -52
  25. data/lib/tcell_agent/policies/login_fraud_policy.rb +15 -20
  26. data/lib/tcell_agent/policies/policy.rb +0 -2
  27. data/lib/tcell_agent/policies/rust_policies.rb +24 -29
  28. data/lib/tcell_agent/rails.rb +2 -3
  29. data/lib/tcell_agent/rails/auth/authlogic.rb +2 -2
  30. data/lib/tcell_agent/rails/auth/devise.rb +2 -2
  31. data/lib/tcell_agent/rails/auth/doorkeeper.rb +2 -2
  32. data/lib/tcell_agent/rails/better_ip.rb +12 -16
  33. data/lib/tcell_agent/rails/csrf_exception.rb +4 -7
  34. data/lib/tcell_agent/rails/dlp.rb +208 -107
  35. data/lib/tcell_agent/rails/dlp/process_request.rb +37 -47
  36. data/lib/tcell_agent/rails/dlp_handler.rb +9 -11
  37. data/lib/tcell_agent/rails/js_agent_insert.rb +11 -14
  38. data/lib/tcell_agent/rails/middleware/body_filter_middleware.rb +8 -7
  39. data/lib/tcell_agent/rails/middleware/context_middleware.rb +4 -5
  40. data/lib/tcell_agent/rails/middleware/global_middleware.rb +5 -8
  41. data/lib/tcell_agent/rails/middleware/headers_middleware.rb +24 -27
  42. data/lib/tcell_agent/rails/on_start.rb +5 -5
  43. data/lib/tcell_agent/rails/responses.rb +7 -9
  44. data/lib/tcell_agent/rails/routes.rb +62 -81
  45. data/lib/tcell_agent/rails/routes/grape.rb +25 -30
  46. data/lib/tcell_agent/rails/routes/route_id.rb +9 -14
  47. data/lib/tcell_agent/rails/settings_reporter.rb +44 -33
  48. data/lib/tcell_agent/rails/tcell_body_proxy.rb +15 -18
  49. data/lib/tcell_agent/routes/table.rb +31 -33
  50. data/lib/tcell_agent/rust/{libtcellagent-1.3.0.dylib → libtcellagent-1.3.1.dylib} +0 -0
  51. data/lib/tcell_agent/rust/{libtcellagent-1.3.0.so → libtcellagent-1.3.1.so} +0 -0
  52. data/lib/tcell_agent/rust/{libtcellagent-alpine-1.3.0.so → libtcellagent-alpine-1.3.1.so} +0 -0
  53. data/lib/tcell_agent/rust/models.rb +32 -37
  54. data/lib/tcell_agent/rust/tcellagent-1.3.1.dll +0 -0
  55. data/lib/tcell_agent/rust/whisperer.rb +101 -104
  56. data/lib/tcell_agent/sensor_events/app_config.rb +7 -7
  57. data/lib/tcell_agent/sensor_events/appsensor_event.rb +26 -27
  58. data/lib/tcell_agent/sensor_events/appsensor_meta_event.rb +20 -88
  59. data/lib/tcell_agent/sensor_events/command_injection.rb +52 -80
  60. data/lib/tcell_agent/sensor_events/discovery.rb +27 -27
  61. data/lib/tcell_agent/sensor_events/dlp.rb +50 -56
  62. data/lib/tcell_agent/sensor_events/honeytokens.rb +9 -9
  63. data/lib/tcell_agent/sensor_events/metrics.rb +20 -21
  64. data/lib/tcell_agent/sensor_events/patches.rb +10 -12
  65. data/lib/tcell_agent/sensor_events/sensor.rb +32 -36
  66. data/lib/tcell_agent/sensor_events/server_agent.rb +130 -127
  67. data/lib/tcell_agent/sensor_events/util/sanitizer_utilities.rb +60 -80
  68. data/lib/tcell_agent/sensor_events/util/utils.rb +3 -5
  69. data/lib/tcell_agent/servers/passenger.rb +5 -9
  70. data/lib/tcell_agent/servers/puma.rb +18 -27
  71. data/lib/tcell_agent/servers/rails_server.rb +5 -9
  72. data/lib/tcell_agent/servers/thin.rb +2 -4
  73. data/lib/tcell_agent/servers/unicorn.rb +18 -27
  74. data/lib/tcell_agent/servers/webrick.rb +2 -4
  75. data/lib/tcell_agent/settings_reporter.rb +126 -0
  76. data/lib/tcell_agent/sinatra.rb +24 -26
  77. data/lib/tcell_agent/start_background_thread.rb +21 -142
  78. data/lib/tcell_agent/system_info.rb +4 -3
  79. data/lib/tcell_agent/tcell_context.rb +150 -0
  80. data/lib/tcell_agent/userinfo.rb +3 -3
  81. data/lib/tcell_agent/utils/io.rb +19 -24
  82. data/lib/tcell_agent/utils/params.rb +9 -15
  83. data/lib/tcell_agent/utils/queue_with_timeout.rb +26 -32
  84. data/lib/tcell_agent/utils/strings.rb +4 -6
  85. data/lib/tcell_agent/version.rb +1 -1
  86. data/spec/lib/tcell_agent/agent/policy_manager_spec.rb +5 -5
  87. data/spec/lib/tcell_agent/agent/static_agent_spec.rb +7 -7
  88. data/spec/lib/tcell_agent/cmdi_spec.rb +21 -21
  89. data/spec/lib/tcell_agent/hooks/login_fraud_spec.rb +29 -24
  90. data/spec/lib/tcell_agent/instrumentation_spec.rb +4 -4
  91. data/spec/lib/tcell_agent/patches_spec.rb +8 -8
  92. data/spec/lib/tcell_agent/policies/appsensor_policy_spec.rb +23 -23
  93. data/spec/lib/tcell_agent/policies/patches_policy_spec.rb +2 -2
  94. data/spec/lib/tcell_agent/rails/csrf_exception_spec.rb +69 -0
  95. data/spec/lib/tcell_agent/rails/dlp_spec.rb +1039 -0
  96. data/spec/lib/tcell_agent/rails/js_agent_insert_spec.rb +271 -0
  97. data/spec/lib/tcell_agent/rails/logger_spec.rb +5 -5
  98. data/spec/lib/tcell_agent/rails/middleware/appsensor_middleware_spec.rb +3 -3
  99. data/spec/lib/tcell_agent/rails/middleware/dlp_middleware_spec.rb +4 -4
  100. data/spec/lib/tcell_agent/rails/middleware/global_middleware_spec.rb +5 -5
  101. data/spec/lib/tcell_agent/rails/middleware/redirect_middleware_spec.rb +1 -1
  102. data/spec/lib/tcell_agent/rails/middleware/tcell_body_proxy_spec.rb +11 -8
  103. data/spec/lib/tcell_agent/rails/responses_spec.rb +2 -2
  104. data/spec/lib/tcell_agent/rails/routes/grape_spec.rb +2 -2
  105. data/spec/lib/tcell_agent/rails/routes/route_id_spec.rb +1 -1
  106. data/spec/lib/tcell_agent/rails/routes/routes_spec.rb +4 -4
  107. data/spec/lib/tcell_agent/rust/models_spec.rb +83 -75
  108. data/spec/lib/tcell_agent/rust/whisperer_spec.rb +14 -14
  109. data/spec/lib/tcell_agent/sensor_events/appsensor_meta_event_spec.rb +19 -70
  110. data/spec/lib/tcell_agent/sensor_events/sessions_metric_spec.rb +1 -1
  111. data/spec/lib/tcell_agent/settings_reporter_spec.rb +162 -0
  112. data/spec/lib/tcell_agent/tcell_context_spec.rb +154 -0
  113. data/spec/spec_helper.rb +5 -0
  114. metadata +18 -10
  115. data/lib/tcell_agent/appsensor/meta_data.rb +0 -132
  116. data/lib/tcell_agent/patches/meta_data.rb +0 -59
  117. data/lib/tcell_agent/rust/tcellagent-1.3.0.dll +0 -0
  118. data/spec/lib/tcell_agent/appsensor/meta_data_spec.rb +0 -71
@@ -1,13 +1,14 @@
1
+ require 'tcell_agent/tcell_context'
2
+
1
3
  module TCellAgent
2
4
  module Instrumentation
3
-
4
5
  module Patches
5
6
  def self.block?(request)
6
- TCellAgent::Instrumentation.safe_block("Checking patches blocking") do
7
- rust_policies = TCellAgent.policy(TCellAgent::PolicyTypes::Rust)
7
+ TCellAgent::Instrumentation.safe_block('Checking patches blocking') do
8
+ rust_policies = TCellAgent.policy(TCellAgent::PolicyTypes::RUST)
8
9
 
9
10
  if rust_policies && rust_policies.patches_enabled
10
- meta_data = TCellAgent::Patches::MetaData.build(request)
11
+ meta_data = TCellAgent::MetaData.from_request(request)
11
12
  block_request = rust_policies.block_request?(meta_data)
12
13
  request.env[TCellAgent::Instrumentation::TCELL_ID].patches_blocking_triggered = block_request
13
14
 
@@ -18,6 +19,5 @@ module TCellAgent
18
19
  false
19
20
  end
20
21
  end
21
-
22
22
  end
23
23
  end
@@ -1,322 +1,297 @@
1
1
  require 'set'
2
2
  require 'tcell_agent/policies/policy'
3
3
 
4
-
5
4
  module TCellAgent
6
- module Policies
7
- class DataLossPolicy < Policy
8
- class FilterActions
9
- attr_accessor :body_event
10
- attr_accessor :body_redact
11
- attr_accessor :body_hash
12
-
13
- attr_accessor :log_event
14
- attr_accessor :log_redact
15
- attr_accessor :log_hash
16
-
17
- attr_accessor :action_id
18
- end
19
-
20
- class RequestProtectionManager
21
- FORM = "form"
22
- COOKIE = "cookie"
23
- HEADER = "header"
5
+ module Policies
6
+ class DataLossPolicy < Policy # rubocop:disable Metrics/ClassLength
7
+ class FilterActions
8
+ attr_accessor :body_event
9
+ attr_accessor :body_redact
10
+ attr_accessor :body_hash
11
+
12
+ attr_accessor :log_event
13
+ attr_accessor :log_redact
14
+ attr_accessor :log_hash
15
+
16
+ attr_accessor :action_id
17
+ end
18
+
19
+ class RequestProtectionManager
20
+ FORM = 'form'.freeze
21
+ COOKIE = 'cookie'.freeze
22
+ HEADER = 'header'.freeze
23
+ end
24
+
25
+ attr_accessor :enabled
26
+ attr_accessor :session_id_filter_actions
27
+ attr_accessor :request_filter_actions
28
+ attr_accessor :database_filter_actions
29
+
30
+ attr_accessor :policy_id
31
+
32
+ attr_accessor :table_field_actions
33
+ attr_accessor :session_id_actions
34
+ attr_accessor :database_actions
35
+
36
+ attr_accessor :database_discovery_enabled
37
+
38
+ attr_accessor :field_redact_body
39
+ attr_accessor :field_alerts
40
+
41
+ def initialize
42
+ init_options
43
+ end
44
+
45
+ def init_options
46
+ @enabled = false
47
+ @policy_id = nil
48
+
49
+ @table_field_actions = {}
50
+ @session_id_actions = []
51
+
52
+ @database_discovery_enabled = false
53
+
54
+ @field_redact_body = Set.new # ["work_infos.SSN"].to_set #
55
+ @field_alerts = Set.new
56
+
57
+ @session_id_filter_actions = nil
58
+ @request_filter_actions = {
59
+ RequestProtectionManager::FORM => Hash.new { |h, k| h[k] = Hash.new { |i_h, i_k| i_h[i_k] = Set.new } },
60
+ RequestProtectionManager::COOKIE => Hash.new { |h, k| h[k] = Hash.new { |i_h, i_k| i_h[i_k] = Set.new } },
61
+ RequestProtectionManager::HEADER => Hash.new { |h, k| h[k] = Hash.new { |i_h, i_k| i_h[i_k] = Set.new } }
62
+ }
63
+ @database_actions = Hash.new do |h, k|
64
+ h[k] = Hash.new do |d_h, d_k|
65
+ d_h[d_k] = Hash.new do |s_h, s_k|
66
+ s_h[s_k] = Hash.new do |t_h, t_k|
67
+ t_h[t_k] = Hash.new do |f_h, f_k|
68
+ f_h[f_k] = Set.new
69
+ end
70
+ end
24
71
  end
72
+ end
73
+ end
25
74
 
26
- attr_accessor :enabled
27
- attr_accessor :session_id_filter_actions
28
- attr_accessor :request_filter_actions
29
- attr_accessor :database_filter_actions
30
-
31
- # {
32
- # {"context":[]}
33
- # }
34
-
35
- attr_accessor :policy_id
36
-
37
- attr_accessor :table_field_actions
38
- attr_accessor :session_id_actions
39
- attr_accessor :database_actions
40
-
41
- attr_accessor :database_discovery_enabled
42
-
43
- attr_accessor :field_redact_body
44
- attr_accessor :field_alerts
45
-
46
- def initialize
47
- self.init_options
48
- end
75
+ @log_actions = nil
76
+ end
49
77
 
50
- def init_options
51
- @enabled = false
52
- @policy_id = nil
78
+ def get_actions_for_session_id(_route_id = nil)
79
+ @session_id_filter_actions
80
+ end
53
81
 
54
- @table_field_actions = {}
55
- @session_id_actions = []
82
+ def actions_for_form_parameter?
83
+ !@request_filter_actions[RequestProtectionManager::FORM].empty?
84
+ end
56
85
 
57
- @database_discovery_enabled = false
86
+ def actions_for_headers?
87
+ !@request_filter_actions[RequestProtectionManager::HEADER].empty?
88
+ end
58
89
 
59
- @field_redact_body = Set.new #["work_infos.SSN"].to_set #
60
- @field_alerts = Set.new
90
+ def actions_for_cookie?
91
+ !@request_filter_actions[RequestProtectionManager::COOKIE].empty?
92
+ end
61
93
 
62
- @session_id_filter_actions = nil
63
- @request_filter_actions = {
64
- RequestProtectionManager::FORM=>Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Set.new}},
65
- RequestProtectionManager::COOKIE=>Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Set.new}},
66
- RequestProtectionManager::HEADER=>Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Set.new}}
67
- }
68
- @database_actions = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Hash.new{|h,k| h[k] = Set.new}}}}}
94
+ def get_actions_for_cookie(cookie_name, route_id = nil)
95
+ get_actions_for_request(RequestProtectionManager::COOKIE, cookie_name, route_id)
96
+ end
69
97
 
70
- @log_actions = nil
71
- end
98
+ def get_actions_for_header(header_name, route_id = nil)
99
+ get_actions_for_request(RequestProtectionManager::HEADER, header_name.downcase, route_id)
100
+ end
72
101
 
73
- def get_actions_for_session_id(route_id=nil)
74
- return @session_id_filter_actions
75
- end
102
+ def get_actions_for_form_parameter(parameter_name, route_id = nil)
103
+ get_actions_for_request(RequestProtectionManager::FORM, parameter_name.downcase, route_id)
104
+ end
76
105
 
77
- def has_actions_for_form_parameter?
78
- return @request_filter_actions[RequestProtectionManager::FORM].size > 0
106
+ def get_actions_for_request(context, variable, route_id = nil)
107
+ return nil if context.nil? || variable.nil?
108
+ route_id = '*' if route_id.nil?
109
+ if context != RequestProtectionManager::COOKIE
110
+ variable = variable.downcase
111
+ end
112
+ actions = Set.new
113
+ if @request_filter_actions.key?(context)
114
+ if @request_filter_actions[context].key?(route_id)
115
+ if @request_filter_actions[context][route_id].key?(variable)
116
+ actions.merge(@request_filter_actions[context][route_id][variable])
79
117
  end
80
-
81
- def has_actions_for_headers?
82
- return @request_filter_actions[RequestProtectionManager::HEADER].size > 0
118
+ end
119
+ if route_id != '*' && @request_filter_actions[context].key?('*')
120
+ if @request_filter_actions[context]['*'].key?(variable)
121
+ actions.merge(@request_filter_actions[context]['*'][variable])
83
122
  end
84
-
85
- def has_actions_for_cookie?
86
- return @request_filter_actions[RequestProtectionManager::COOKIE].size > 0
123
+ end
124
+ end
125
+ return nil if actions.size <= 0
126
+ actions
127
+ end
128
+
129
+ def get_actions_for_table(database, schema, table, field, route_id = '*')
130
+ route_id = '*' if route_id.nil?
131
+ actions = Set.new
132
+ [database, '*'].each do |d|
133
+ next if @database_actions.key?(d) == false
134
+ [schema, '*'].each do |s|
135
+ next if @database_actions[d].key?(s) == false
136
+ [table, '*'].each do |t|
137
+ next if @database_actions[d][s].key?(t) == false
138
+ [field, '*'].each do |f|
139
+ next if @database_actions[d][s][t].key?(f) == false
140
+ route_id_rules = @database_actions[d][s][t][f]
141
+ if route_id_rules.key?(route_id)
142
+ actions.merge(@database_actions[d][s][t][f][route_id])
143
+ end
144
+ if route_id != '*' && route_id_rules.key?('*')
145
+ actions.merge(@database_actions[d][s][t][f]['*'])
146
+ end
147
+ end
87
148
  end
149
+ end
150
+ end
151
+ return nil if actions.empty?
152
+ actions
153
+ end
154
+
155
+ def get_actions_for(table, field)
156
+ actions = Set.new
157
+ key = "#{table}.#{field}"
158
+ actions.merge(@table_field_actions.fetch(key, [].to_set))
159
+ actions
160
+ end
161
+
162
+ def self.actions_from_json(options)
163
+ actions = nil
164
+ if options.key?('log')
165
+ if options['log'].include? 'redact'
166
+ actions ||= FilterActions.new
167
+ actions.log_redact = true
168
+ end
169
+ if options['log'].include? 'event'
170
+ actions ||= FilterActions.new
171
+ actions.log_event = true
172
+ end
173
+ if options['log'].include? 'hash'
174
+ actions ||= FilterActions.new
175
+ actions.log_hash = true
176
+ end
177
+ end
178
+ if options.key?('body')
179
+ if options['body'].include? 'redact'
180
+ actions ||= FilterActions.new
181
+ actions.body_redact = true
182
+ end
183
+ if options['body'].include? 'event'
184
+ actions ||= FilterActions.new
185
+ actions.body_event = true
186
+ end
187
+ if options['body'].include? 'hash'
188
+ actions ||= FilterActions.new
189
+ actions.body_hash = true
190
+ end
191
+ end
192
+ actions
193
+ end
88
194
 
89
- def get_actions_for_cookie(cookie_name, route_id=nil)
90
- get_actions_for_request(RequestProtectionManager::COOKIE, cookie_name, route_id)
91
- end
195
+ def self.from_json(policy_json)
196
+ return nil unless policy_json
92
197
 
93
- def get_actions_for_header(header_name, route_id=nil)
94
- get_actions_for_request(RequestProtectionManager::HEADER, header_name.downcase, route_id)
95
- end
198
+ policy = DataLossPolicy.new
199
+ policy.policy_id = policy_json['policy_id']
200
+ raise 'Policy ID missing' unless policy.policy_id
96
201
 
97
- def get_actions_for_form_parameter(parameter_name, route_id=nil)
98
- get_actions_for_request(RequestProtectionManager::FORM, parameter_name.downcase, route_id)
99
- end
202
+ data_json = (policy_json['data'] || {})
100
203
 
101
- def get_actions_for_request(context, variable, route_id=nil)
102
- if (context == nil || variable == nil)
103
- return nil
104
- end
105
- if (route_id == nil)
106
- route_id = "*"
107
- end
108
- if (context != RequestProtectionManager::COOKIE)
109
- variable = variable.downcase
110
- end
111
- actions = Set.new
112
- if @request_filter_actions.has_key?(context)
113
- if @request_filter_actions[context].has_key?(route_id)
114
- if @request_filter_actions[context][route_id].has_key?(variable)
115
- actions.merge(@request_filter_actions[context][route_id][variable])
116
- end
117
- end
118
- if route_id != "*" && @request_filter_actions[context].has_key?("*")
119
- if @request_filter_actions[context]["*"].has_key?(variable)
120
- actions.merge(@request_filter_actions[context]["*"][variable])
121
- end
122
- end
123
- end
124
- if (actions.size <= 0)
125
- return nil
126
- end
127
- return actions
128
- end
204
+ if data_json.key?('data_discovery')
205
+ data_discovery_json = data_json['data_discovery']
206
+ policy.database_discovery_enabled = data_discovery_json.fetch('database_enabled', false)
207
+ policy.enabled = policy.database_discovery_enabled
208
+ end
129
209
 
130
- def get_actions_for_table(database, schema, table, field, route_id="*")
131
- if route_id == nil
132
- route_id = "*"
133
- end
134
- actions = Set.new
135
- [database, "*"].each do |d|
136
- if (@database_actions.has_key?(d) == false)
137
- next
138
- end
139
- [schema, "*"].each do |s|
140
- if (@database_actions[d].has_key?(s) == false)
141
- next
142
- end
143
- [table, "*"].each do |t|
144
- if (@database_actions[d][s].has_key?(t) == false)
145
- next
146
- end
147
- [field, "*"].each do |f|
148
- if (@database_actions[d][s][t].has_key?(f) == false)
149
- next
150
- end
151
- route_id_rules = @database_actions[d][s][t][f]
152
- if (route_id_rules.has_key?(route_id))
153
- actions.merge(@database_actions[d][s][t][f][route_id])
154
- end
155
- if (route_id != "*" && route_id_rules.has_key?("*"))
156
- actions.merge(@database_actions[d][s][t][f]["*"])
157
- end
158
- end
159
- end
160
- end
161
- end
162
- if (actions.size == 0)
163
- return nil
164
- end
165
- actions
166
- end
210
+ if data_json.key?('session_id_protections')
211
+ session_id_protection = data_json['session_id_protections']
212
+ rule_id = session_id_protection.fetch('id', nil)
213
+ filter_actions = DataLossPolicy.actions_from_json(session_id_protection)
214
+ unless filter_actions.nil?
215
+ policy.enabled = true
216
+ filter_actions.action_id = rule_id
217
+ policy.session_id_filter_actions = filter_actions
218
+ end
219
+ end
167
220
 
168
- def get_actions_for(table, field)
169
- actions = Set.new
170
- key = "#{table}.#{field}"
171
- actions.merge(@table_field_actions.fetch(key,[].to_set))
172
- return actions
221
+ if data_json.key?('request_protections')
222
+ data_json['request_protections'].each do |protection|
223
+ context = protection.fetch('variable_context', nil)
224
+ variables = protection.fetch('variables', nil)
225
+ scope = protection.fetch('scope', 'global')
226
+ rule_id = protection.fetch('id', nil)
227
+ options = protection.fetch('actions', nil)
228
+ route_ids = []
229
+
230
+ if scope == 'global'
231
+ route_ids = ['*']
232
+ elsif scope == 'route'
233
+ route_ids = protection.fetch('route_ids', [])
234
+ else
235
+ next
173
236
  end
174
237
 
175
- def self.actions_from_json(options)
176
- actions = nil
177
- if options.has_key?("log")
178
- if (options["log"].include? "redact")
179
- actions ||= FilterActions.new
180
- actions.log_redact = true
181
- end
182
- if (options["log"].include? "event")
183
- actions ||= FilterActions.new
184
- actions.log_event = true
185
- end
186
- if (options["log"].include? "hash")
187
- actions ||= FilterActions.new
188
- actions.log_hash = true
189
- end
190
- end
191
- if options.has_key?("body")
192
- if (options["body"].include? "redact")
193
- actions ||= FilterActions.new
194
- actions.body_redact = true
195
- end
196
- if (options["body"].include? "event")
197
- actions ||= FilterActions.new
198
- actions.body_event = true
199
- end
200
- if (options["body"].include? "hash")
201
- actions ||= FilterActions.new
202
- actions.body_hash = true
203
- end
204
- end
205
- actions
206
- end
207
-
208
- def self.from_json(policy_json)
209
- if (!policy_json)
210
- return nil
211
- end
212
-
213
- policy = DataLossPolicy.new
214
- if policy_json.has_key?("policy_id")
215
- policy.policy_id = policy_json["policy_id"]
238
+ next unless context && policy.request_filter_actions.key?(context) && variables && options
239
+ filter_actions = DataLossPolicy.actions_from_json(options)
240
+ next if filter_actions.nil?
241
+ policy.enabled = true
242
+ filter_actions.action_id = rule_id
243
+ variables.each do |variable|
244
+ route_ids.each do |route_id|
245
+ if context == RequestProtectionManager::COOKIE
246
+ # Case sensitive variable name
247
+ policy.request_filter_actions[context][route_id][variable].add(filter_actions)
216
248
  else
217
- raise "Policy ID missing"
249
+ policy.request_filter_actions[context][route_id][variable.downcase].add(filter_actions)
218
250
  end
251
+ end
252
+ end
253
+ end
254
+ end
219
255
 
220
- if policy_json.has_key?("data")
221
- data_json = policy_json["data"]
222
- if data_json.has_key?("data_discovery")
223
- data_discovery_json = data_json["data_discovery"]
224
- policy.database_discovery_enabled = data_discovery_json.fetch('database_enabled', false)
225
- policy.enabled = policy.database_discovery_enabled
226
- end
227
- if data_json.has_key?("session_id_protections")
228
- session_id_protection = data_json["session_id_protections"]
229
- rule_id = session_id_protection.fetch("id",nil)
230
- filter_actions = DataLossPolicy.actions_from_json(session_id_protection)
231
- if filter_actions != nil
232
- policy.enabled = true
233
- filter_actions.action_id = rule_id
234
- policy.session_id_filter_actions = filter_actions
235
- end
236
- end
237
-
238
- if data_json.has_key?("request_protections")
239
- data_json["request_protections"].each do |protection|
240
- context = protection.fetch('variable_context', nil)
241
- variables = protection.fetch('variables', nil)
242
- scope = protection.fetch('scope', "global")
243
- rule_id = protection.fetch("id",nil)
244
- options = protection.fetch('actions', nil)
245
- route_ids = []
246
-
247
- if (scope == "global")
248
- route_ids = ["*"]
249
- elsif (scope == "route")
250
- route_ids = protection.fetch("route_ids",[])
251
- else
252
- next
253
- end
254
-
255
- if context && policy.request_filter_actions.has_key?(context) && variables && options
256
- filter_actions = DataLossPolicy.actions_from_json(options)
257
- if filter_actions != nil
258
- policy.enabled = true
259
- filter_actions.action_id = rule_id
260
- variables.each do |variable|
261
- route_ids.each do |route_id|
262
- if (context == RequestProtectionManager::COOKIE)
263
- # Case sensitive variable name
264
- policy.request_filter_actions[context][route_id][variable].add(filter_actions)
265
- else
266
- policy.request_filter_actions[context][route_id][variable.downcase].add(filter_actions)
267
- end
268
- end
269
- end
270
- end
271
- end
272
- end
273
- end
274
-
275
- if data_json.has_key?("db_protections")
276
- protections = data_json["db_protections"]
277
- if protections
278
- protections.each do |protection_json|
279
- scope = protection_json.fetch("scope",nil)
280
- _databases = protection_json.fetch("databases",["*"])
281
- _schemas = protection_json.fetch("schemas",["*"])
282
- _tables = protection_json.fetch("tables",["*"])
283
- _fields = protection_json.fetch("fields",nil)
284
- rule_id = protection_json.fetch("id",nil)
285
- actions = protection_json.fetch("actions",{})
286
- filter_actions = DataLossPolicy.actions_from_json(actions)
287
- _route_ids = ["*"]
288
-
289
- if scope != nil && scope != "global"
290
- if scope=="route"
291
- _route_ids = protection_json.fetch("route_ids",[])
292
- end
293
- end
294
- if _fields == nil
295
- next
296
- elsif filter_actions == nil
297
- next
298
- end
299
-
300
- policy.enabled = true
301
- filter_actions.action_id = rule_id
302
- _databases.each do |_database|
303
- _schemas.each do |_schema|
304
- _tables.each do |_table|
305
- _fields.each do |_field|
306
- _route_ids.each do |_route_id|
307
- policy.database_actions[_database][_schema][_table][_field][_route_id].add(filter_actions)
308
- end
309
- end
310
- end
311
- end
312
- end
313
- end
314
- end
256
+ if data_json.key?('db_protections')
257
+ protections = data_json['db_protections']
258
+ if protections
259
+ protections.each do |protection_json|
260
+ scope = protection_json.fetch('scope', nil)
261
+ databases = protection_json.fetch('databases', ['*'])
262
+ schemas = protection_json.fetch('schemas', ['*'])
263
+ tables = protection_json.fetch('tables', ['*'])
264
+ fields = protection_json.fetch('fields', nil)
265
+ rule_id = protection_json.fetch('id', nil)
266
+ actions = protection_json.fetch('actions', {})
267
+ filter_actions = DataLossPolicy.actions_from_json(actions)
268
+ route_ids = ['*']
269
+
270
+ if !scope.nil? && scope != 'global' && scope == 'route'
271
+ route_ids = protection_json.fetch('route_ids', [])
272
+ end
273
+
274
+ next if fields.nil? || filter_actions.nil?
275
+
276
+ policy.enabled = true
277
+ filter_actions.action_id = rule_id
278
+ databases.each do |database|
279
+ schemas.each do |schema|
280
+ tables.each do |table|
281
+ fields.each do |field|
282
+ route_ids.each do |route_id|
283
+ policy.database_actions[database][schema][table][field][route_id].add(filter_actions)
284
+ end
315
285
  end
286
+ end
316
287
  end
317
-
318
- return policy
288
+ end
319
289
  end
290
+ end
320
291
  end
292
+
293
+ policy
294
+ end
321
295
  end
296
+ end
322
297
  end