tcell_agent 2.7.0 → 2.7.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (33) hide show
  1. checksums.yaml +4 -4
  2. data/lib/tcell_agent/agent.rb +1 -2
  3. data/lib/tcell_agent/instrumentation.rb +0 -192
  4. data/lib/tcell_agent/policies/policies_manager.rb +1 -17
  5. data/lib/tcell_agent/policies/policy_polling.rb +1 -2
  6. data/lib/tcell_agent/policies/policy_types.rb +0 -1
  7. data/lib/tcell_agent/rails/database.rb +49 -0
  8. data/lib/tcell_agent/rails/middleware/headers_middleware.rb +1 -1
  9. data/lib/tcell_agent/rails/railties/tcell_agent_database_railties.rb +81 -0
  10. data/lib/tcell_agent/rails/railties/tcell_agent_railties.rb +0 -1
  11. data/lib/tcell_agent/rails/routes.rb +0 -8
  12. data/lib/tcell_agent/rust/libtcellagent-alpine.so +0 -0
  13. data/lib/tcell_agent/rust/libtcellagent-x64.dll +0 -0
  14. data/lib/tcell_agent/rust/libtcellagent.dylib +0 -0
  15. data/lib/tcell_agent/rust/libtcellagent.so +0 -0
  16. data/lib/tcell_agent/sensor_events/util/sanitizer_utilities.rb +0 -17
  17. data/lib/tcell_agent/version.rb +1 -1
  18. data/lib/tcell_agent.rb +5 -3
  19. data/spec/lib/tcell_agent/policies/policies_manager_spec.rb +5 -16
  20. data/spec/lib/tcell_agent/rails/database.rb +60 -0
  21. data/spec/lib/tcell_agent/rails/middleware/tcell_body_proxy_spec.rb +2 -2
  22. data/spec/support/force_logger_mocking.rb +0 -8
  23. metadata +6 -16
  24. data/lib/tcell_agent/policies/dataloss_policy.rb +0 -304
  25. data/lib/tcell_agent/rails/dlp/process_request.rb +0 -83
  26. data/lib/tcell_agent/rails/dlp.rb +0 -410
  27. data/lib/tcell_agent/rails/dlp_handler.rb +0 -63
  28. data/lib/tcell_agent/sensor_events/dlp.rb +0 -53
  29. data/lib/tcell_agent/sinatra.rb +0 -38
  30. data/spec/lib/tcell_agent/policies/dataloss_policy_spec.rb +0 -222
  31. data/spec/lib/tcell_agent/rails/dlp_spec.rb +0 -1040
  32. data/spec/lib/tcell_agent/rails/logger_spec.rb +0 -169
  33. data/spec/lib/tcell_agent/sensor_events/dlp_spec.rb +0 -14
@@ -1,1040 +0,0 @@
1
- require 'spec_helper'
2
-
3
- module TCellAgent
4
- module DLP
5
- class SomeColumn
6
- attr_accessor :name
7
-
8
- def initialize(name = nil)
9
- @name = name
10
- end
11
- end
12
-
13
- class SomeModel
14
- class << self
15
- def table_name
16
- 'some_models'
17
- end
18
-
19
- def columns
20
- [SomeColumn.new('id'), SomeColumn.new('name'), SomeColumn.new('email')]
21
- end
22
-
23
- def connection_config
24
- {
25
- :database => 'something/test_database'
26
- }
27
- end
28
- end
29
- end
30
-
31
- describe '.instrument_pluck' do
32
- context 'with empty results' do
33
- it 'should skip over instrumentation' do
34
- expect(TCellAgent).to_not receive(:configuration)
35
-
36
- TCellAgent::DLP.instrument_pluck([], [:id], SomeModel)
37
- end
38
- end
39
-
40
- context 'with no dlp policy' do
41
- it 'should skip over instrumentation' do
42
- configuration = double(
43
- 'configuration',
44
- {
45
- :enabled => true,
46
- :should_instrument? => true,
47
- :should_intercept_requests? => true
48
- }
49
- )
50
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
51
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(nil)
52
-
53
- TCellAgent::DLP.instrument_pluck([10], [:id], SomeModel)
54
- end
55
- end
56
-
57
- context 'with dlp policy disabled' do
58
- it 'should skip over instrumentation' do
59
- configuration = double(
60
- 'configuration',
61
- {
62
- :enabled => true,
63
- :should_instrument? => true,
64
- :should_intercept_requests? => true
65
- }
66
- )
67
- dataloss_policy = double(
68
- 'dataloss_policy',
69
- {
70
- :enabled => false
71
- }
72
- )
73
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
74
- expect(TCellAgent).to receive(:policy).with(
75
- TCellAgent::PolicyTypes::DATALOSS
76
- ).and_return(dataloss_policy)
77
-
78
- TCellAgent::DLP.instrument_pluck([10], [:id], SomeModel)
79
- end
80
- end
81
-
82
- context 'with dlp policy enabled' do
83
- context 'with database_discovery_enabled' do
84
- it 'should send model columns to tcell service' do
85
- configuration = double(
86
- 'configuration',
87
- {
88
- :enabled => true,
89
- :should_instrument? => true,
90
- :should_intercept_requests? => true,
91
- :max_data_ex_db_records_per_request => 1
92
- }
93
- )
94
- dataloss_policy = double(
95
- 'dataloss_policy',
96
- {
97
- :enabled => true,
98
- :database_discovery_enabled => true
99
- }
100
- )
101
- request_env = double('request_env', {})
102
- tcell_context = double(
103
- 'tcell_context',
104
- {
105
- :route_id => 'ma_route_id',
106
- :database_result_sizes => []
107
- }
108
- )
109
-
110
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
111
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
112
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
113
- :fetch
114
- ).and_return(request_env)
115
- expect(request_env).to receive(:[]).with(
116
- TCellAgent::Instrumentation::TCELL_ID
117
- ).and_return(tcell_context)
118
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
119
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
120
- ).and_return(nil)
121
-
122
- expect(TCellAgent).to receive(:discover_database_fields).with(
123
- 'ma_route_id', 'test_database', '*', 'some_models', ['id']
124
- )
125
-
126
- TCellAgent::DLP.instrument_pluck([10], [:id], SomeModel)
127
-
128
- expect(tcell_context.database_result_sizes).to eq([1])
129
- end
130
- end
131
-
132
- context 'with database_discovery_enabled == false' do
133
- context 'with zero columns passed to pluck' do
134
- context 'that doesn\'t have any rules set' do
135
- it 'should not send anything to tcell' do
136
- configuration = double(
137
- 'configuration',
138
- {
139
- :enabled => true,
140
- :should_instrument? => true,
141
- :should_intercept_requests? => true,
142
- :max_data_ex_db_records_per_request => 1000
143
- }
144
- )
145
- dataloss_policy = double(
146
- 'dataloss_policy',
147
- {
148
- :enabled => true,
149
- :database_discovery_enabled => false
150
- }
151
- )
152
- request_env = double('request_env', {})
153
- tcell_context = double(
154
- 'tcell_context',
155
- {
156
- :route_id => 'ma_route_id',
157
- :database_result_sizes => []
158
- }
159
- )
160
-
161
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
162
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
163
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
164
- :fetch
165
- ).and_return(request_env)
166
- expect(request_env).to receive(:[]).with(
167
- TCellAgent::Instrumentation::TCELL_ID
168
- ).and_return(tcell_context)
169
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
170
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
171
- ).and_return(nil)
172
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
173
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
174
- ).and_return(nil)
175
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
176
- 'test_database', '*', 'some_models', 'email', 'ma_route_id'
177
- ).and_return(nil)
178
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
179
-
180
- expect(TCellAgent).to_not receive(:discover_database_fields)
181
-
182
- TCellAgent::DLP.instrument_pluck([10], [], SomeModel)
183
-
184
- expect(tcell_context.database_result_sizes).to eq([1])
185
- end
186
- end
187
-
188
- context 'that has a rule set' do
189
- it 'should add a response filter' do
190
- configuration = double(
191
- 'configuration',
192
- {
193
- :enabled => true,
194
- :should_instrument? => true,
195
- :should_intercept_requests? => true,
196
- :max_data_ex_db_records_per_request => 1000
197
- }
198
- )
199
- dataloss_policy = double(
200
- 'dataloss_policy',
201
- {
202
- :enabled => true,
203
- :database_discovery_enabled => false
204
- }
205
- )
206
- request_env = double('request_env', {})
207
- tcell_context = double(
208
- 'tcell_context',
209
- {
210
- :route_id => 'ma_route_id',
211
- :database_result_sizes => []
212
- }
213
- )
214
- id_rule = double('id_rule')
215
-
216
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
217
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
218
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
219
- :fetch
220
- ).and_return(request_env)
221
- expect(request_env).to receive(:[]).with(
222
- TCellAgent::Instrumentation::TCELL_ID
223
- ).and_return(tcell_context)
224
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
225
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
226
- ).and_return([id_rule])
227
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
228
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
229
- ).and_return(nil)
230
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
231
- 'test_database', '*', 'some_models', 'email', 'ma_route_id'
232
- ).and_return(nil)
233
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
234
-
235
- expect(TCellAgent).to_not receive(:discover_database_fields)
236
- expect(tcell_context).to receive(:add_response_db_filter).with(
237
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
238
- )
239
-
240
- TCellAgent::DLP.instrument_pluck([[10, 'name', 'email']], [], SomeModel)
241
-
242
- expect(tcell_context.database_result_sizes).to eq([1])
243
- end
244
- end
245
-
246
- context 'that has two rules set' do
247
- it 'should add a response filter' do
248
- configuration = double(
249
- 'configuration',
250
- {
251
- :enabled => true,
252
- :should_instrument? => true,
253
- :should_intercept_requests? => true,
254
- :max_data_ex_db_records_per_request => 1000
255
- }
256
- )
257
- dataloss_policy = double(
258
- 'dataloss_policy',
259
- {
260
- :enabled => true,
261
- :database_discovery_enabled => false
262
- }
263
- )
264
- request_env = double('request_env', {})
265
- tcell_context = double(
266
- 'tcell_context',
267
- {
268
- :route_id => 'ma_route_id',
269
- :database_result_sizes => []
270
- }
271
- )
272
- id_rule = double('id_rule')
273
- id_rule_dos = double('id_rule_dos')
274
-
275
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
276
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
277
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
278
- :fetch
279
- ).and_return(request_env)
280
- expect(request_env).to receive(:[]).with(
281
- TCellAgent::Instrumentation::TCELL_ID
282
- ).and_return(tcell_context)
283
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
284
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
285
- ).and_return([id_rule, id_rule_dos])
286
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
287
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
288
- ).and_return(nil)
289
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
290
- 'test_database', '*', 'some_models', 'email', 'ma_route_id'
291
- ).and_return(nil)
292
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
293
-
294
- expect(TCellAgent).to_not receive(:discover_database_fields)
295
- expect(tcell_context).to receive(:add_response_db_filter).with(
296
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
297
- )
298
- expect(tcell_context).to receive(:add_response_db_filter).with(
299
- 10, id_rule_dos, 'test_database', '*', 'some_models', 'id'
300
- )
301
-
302
- TCellAgent::DLP.instrument_pluck([[10, 'name', 'email']], [], SomeModel)
303
-
304
- expect(tcell_context.database_result_sizes).to eq([1])
305
- end
306
- end
307
- end
308
-
309
- context 'with one column passed to pluck' do
310
- context 'that doesn\'t have any rules set' do
311
- it 'should not send anything to tcell' do
312
- configuration = double(
313
- 'configuration',
314
- {
315
- :enabled => true,
316
- :should_instrument? => true,
317
- :should_intercept_requests? => true,
318
- :max_data_ex_db_records_per_request => 1000
319
- }
320
- )
321
- dataloss_policy = double(
322
- 'dataloss_policy',
323
- {
324
- :enabled => true,
325
- :database_discovery_enabled => false
326
- }
327
- )
328
- request_env = double('request_env', {})
329
- tcell_context = double(
330
- 'tcell_context',
331
- {
332
- :route_id => 'ma_route_id',
333
- :database_result_sizes => []
334
- }
335
- )
336
-
337
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
338
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
339
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
340
- :fetch
341
- ).and_return(request_env)
342
- expect(request_env).to receive(:[]).with(
343
- TCellAgent::Instrumentation::TCELL_ID
344
- ).and_return(tcell_context)
345
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
346
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
347
- ).and_return(nil)
348
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
349
-
350
- expect(TCellAgent).to_not receive(:discover_database_fields)
351
-
352
- TCellAgent::DLP.instrument_pluck([10], [:id], SomeModel)
353
-
354
- expect(tcell_context.database_result_sizes).to eq([1])
355
- end
356
- end
357
-
358
- context 'that has a rule set' do
359
- it 'should add a response filter' do
360
- configuration = double(
361
- 'configuration',
362
- {
363
- :enabled => true,
364
- :should_instrument? => true,
365
- :should_intercept_requests? => true,
366
- :max_data_ex_db_records_per_request => 1000
367
- }
368
- )
369
- dataloss_policy = double(
370
- 'dataloss_policy',
371
- {
372
- :enabled => true,
373
- :database_discovery_enabled => false
374
- }
375
- )
376
- request_env = double('request_env', {})
377
- tcell_context = double(
378
- 'tcell_context',
379
- {
380
- :route_id => 'ma_route_id',
381
- :database_result_sizes => []
382
- }
383
- )
384
- id_rule = double('id_rule')
385
-
386
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
387
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
388
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
389
- :fetch
390
- ).and_return(request_env)
391
- expect(request_env).to receive(:[]).with(
392
- TCellAgent::Instrumentation::TCELL_ID
393
- ).and_return(tcell_context)
394
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
395
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
396
- ).and_return([id_rule])
397
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
398
-
399
- expect(TCellAgent).to_not receive(:discover_database_fields)
400
- expect(tcell_context).to receive(:add_response_db_filter).with(
401
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
402
- )
403
-
404
- TCellAgent::DLP.instrument_pluck([10], [:id], SomeModel)
405
-
406
- expect(tcell_context.database_result_sizes).to eq([1])
407
- end
408
- end
409
-
410
- context 'that has two rules set' do
411
- it 'should add a response filter' do
412
- configuration = double(
413
- 'configuration',
414
- {
415
- :enabled => true,
416
- :should_instrument? => true,
417
- :should_intercept_requests? => true,
418
- :max_data_ex_db_records_per_request => 1000
419
- }
420
- )
421
- dataloss_policy = double(
422
- 'dataloss_policy',
423
- {
424
- :enabled => true,
425
- :database_discovery_enabled => false
426
- }
427
- )
428
- request_env = double('request_env', {})
429
- tcell_context = double(
430
- 'tcell_context',
431
- {
432
- :route_id => 'ma_route_id',
433
- :database_result_sizes => []
434
- }
435
- )
436
- id_rule = double('id_rule')
437
- id_rule_dos = double('id_rule_dos')
438
-
439
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
440
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
441
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
442
- :fetch
443
- ).and_return(request_env)
444
- expect(request_env).to receive(:[]).with(
445
- TCellAgent::Instrumentation::TCELL_ID
446
- ).and_return(tcell_context)
447
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
448
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
449
- ).and_return([id_rule, id_rule_dos])
450
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
451
-
452
- expect(TCellAgent).to_not receive(:discover_database_fields)
453
- expect(tcell_context).to receive(:add_response_db_filter).with(
454
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
455
- )
456
- expect(tcell_context).to receive(:add_response_db_filter).with(
457
- 10, id_rule_dos, 'test_database', '*', 'some_models', 'id'
458
- )
459
-
460
- TCellAgent::DLP.instrument_pluck([10], [:id], SomeModel)
461
-
462
- expect(tcell_context.database_result_sizes).to eq([1])
463
- end
464
- end
465
-
466
- context 'that is namespaced' do
467
- it 'should add a response filter' do
468
- configuration = double(
469
- 'configuration',
470
- {
471
- :enabled => true,
472
- :should_instrument? => true,
473
- :should_intercept_requests? => true,
474
- :max_data_ex_db_records_per_request => 1000
475
- }
476
- )
477
- dataloss_policy = double(
478
- 'dataloss_policy',
479
- {
480
- :enabled => true,
481
- :database_discovery_enabled => false
482
- }
483
- )
484
- request_env = double('request_env', {})
485
- tcell_context = double(
486
- 'tcell_context',
487
- {
488
- :route_id => 'ma_route_id',
489
- :database_result_sizes => []
490
- }
491
- )
492
- id_rule = double('id_rule')
493
- id_rule_dos = double('id_rule_dos')
494
-
495
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
496
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
497
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
498
- :fetch
499
- ).and_return(request_env)
500
- expect(request_env).to receive(:[]).with(
501
- TCellAgent::Instrumentation::TCELL_ID
502
- ).and_return(tcell_context)
503
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
504
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
505
- ).and_return([id_rule, id_rule_dos])
506
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
507
- expect(tcell_context).to receive(:add_response_db_filter).with(
508
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
509
- )
510
- expect(tcell_context).to receive(:add_response_db_filter).with(
511
- 10, id_rule_dos, 'test_database', '*', 'some_models', 'id'
512
- )
513
-
514
- expect(TCellAgent).to_not receive(:discover_database_fields)
515
-
516
- TCellAgent::DLP.instrument_pluck([10], ['some_models.id'], SomeModel)
517
-
518
- expect(tcell_context.database_result_sizes).to eq([1])
519
- end
520
- end
521
- end
522
-
523
- context 'with two columns passed to pluck' do
524
- context 'that doesn\'t have any rules set' do
525
- it 'should not send anything to tcell' do
526
- configuration = double(
527
- 'configuration',
528
- {
529
- :enabled => true,
530
- :should_instrument? => true,
531
- :should_intercept_requests? => true,
532
- :max_data_ex_db_records_per_request => 1000
533
- }
534
- )
535
- dataloss_policy = double(
536
- 'dataloss_policy',
537
- {
538
- :enabled => true,
539
- :database_discovery_enabled => false
540
- }
541
- )
542
- request_env = double('request_env', {})
543
- tcell_context = double(
544
- 'tcell_context',
545
- {
546
- :route_id => 'ma_route_id',
547
- :database_result_sizes => []
548
- }
549
- )
550
-
551
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
552
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
553
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
554
- :fetch
555
- ).and_return(request_env)
556
- expect(request_env).to receive(:[]).with(
557
- TCellAgent::Instrumentation::TCELL_ID
558
- ).and_return(tcell_context)
559
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
560
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
561
- ).and_return(nil)
562
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
563
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
564
- ).and_return(nil)
565
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
566
-
567
- expect(TCellAgent).to_not receive(:discover_database_fields)
568
-
569
- TCellAgent::DLP.instrument_pluck([[10, 'dies'], [20, 'veinte']], %i[id name], SomeModel)
570
-
571
- expect(tcell_context.database_result_sizes).to eq([2])
572
- end
573
- end
574
-
575
- context 'that has a rule set' do
576
- it 'should add a response filter' do
577
- configuration = double(
578
- 'configuration',
579
- {
580
- :enabled => true,
581
- :should_instrument? => true,
582
- :should_intercept_requests? => true,
583
- :max_data_ex_db_records_per_request => 1000
584
- }
585
- )
586
- dataloss_policy = double(
587
- 'dataloss_policy',
588
- {
589
- :enabled => true,
590
- :database_discovery_enabled => false
591
- }
592
- )
593
- request_env = double('request_env', {})
594
- tcell_context = double(
595
- 'tcell_context',
596
- {
597
- :route_id => 'ma_route_id',
598
- :database_result_sizes => []
599
- }
600
- )
601
- id_rule = double('id_rule')
602
-
603
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
604
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
605
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
606
- :fetch
607
- ).and_return(request_env)
608
- expect(request_env).to receive(:[]).with(
609
- TCellAgent::Instrumentation::TCELL_ID
610
- ).and_return(tcell_context)
611
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
612
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
613
- ).and_return([id_rule])
614
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
615
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
616
- ).and_return(nil)
617
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
618
-
619
- expect(TCellAgent).to_not receive(:discover_database_fields)
620
- expect(tcell_context).to receive(:add_response_db_filter).with(
621
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
622
- )
623
- expect(tcell_context).to receive(:add_response_db_filter).with(
624
- 20, id_rule, 'test_database', '*', 'some_models', 'id'
625
- )
626
-
627
- TCellAgent::DLP.instrument_pluck([[10, 'dies'], [20, 'veinte']], %i[id name], SomeModel)
628
-
629
- expect(tcell_context.database_result_sizes).to eq([2])
630
- end
631
-
632
- context 'that is namespaced' do
633
- it 'should not send anything to tcell' do
634
- configuration = double(
635
- 'configuration',
636
- {
637
- :enabled => true,
638
- :should_instrument? => true,
639
- :should_intercept_requests? => true,
640
- :max_data_ex_db_records_per_request => 1000
641
- }
642
- )
643
- dataloss_policy = double(
644
- 'dataloss_policy',
645
- {
646
- :enabled => true,
647
- :database_discovery_enabled => false
648
- }
649
- )
650
- request_env = double('request_env', {})
651
- tcell_context = double(
652
- 'tcell_context',
653
- {
654
- :route_id => 'ma_route_id',
655
- :database_result_sizes => []
656
- }
657
- )
658
-
659
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
660
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
661
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
662
- :fetch
663
- ).and_return(request_env)
664
- expect(request_env).to receive(:[]).with(
665
- TCellAgent::Instrumentation::TCELL_ID
666
- ).and_return(tcell_context)
667
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
668
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
669
- ).and_return(nil)
670
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
671
-
672
- expect(TCellAgent).to_not receive(:discover_database_fields)
673
-
674
- TCellAgent::DLP.instrument_pluck([10], ['some_other_models.name', 'some_models.id'], SomeModel)
675
-
676
- expect(tcell_context.database_result_sizes).to eq([1])
677
- end
678
- end
679
- end
680
-
681
- context 'that has two rules set' do
682
- it 'should add a response filter' do
683
- configuration = double(
684
- 'configuration',
685
- {
686
- :enabled => true,
687
- :should_instrument? => true,
688
- :should_intercept_requests? => true,
689
- :max_data_ex_db_records_per_request => 1000
690
- }
691
- )
692
- dataloss_policy = double(
693
- 'dataloss_policy',
694
- {
695
- :enabled => true,
696
- :database_discovery_enabled => false
697
- }
698
- )
699
- request_env = double('request_env', {})
700
- tcell_context = double(
701
- 'tcell_context',
702
- {
703
- :route_id => 'ma_route_id',
704
- :database_result_sizes => []
705
- }
706
- )
707
- id_rule = double('id_rule')
708
- id_rule_dos = double('id_rule_dos')
709
-
710
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
711
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
712
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
713
- :fetch
714
- ).and_return(request_env)
715
- expect(request_env).to receive(:[]).with(
716
- TCellAgent::Instrumentation::TCELL_ID
717
- ).and_return(tcell_context)
718
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
719
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
720
- ).and_return([id_rule, id_rule_dos])
721
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
722
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
723
- ).and_return(nil)
724
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
725
-
726
- expect(TCellAgent).to_not receive(:discover_database_fields)
727
- expect(tcell_context).to receive(:add_response_db_filter).with(
728
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
729
- )
730
- expect(tcell_context).to receive(:add_response_db_filter).with(
731
- 10, id_rule_dos, 'test_database', '*', 'some_models', 'id'
732
- )
733
- expect(tcell_context).to receive(:add_response_db_filter).with(
734
- 20, id_rule, 'test_database', '*', 'some_models', 'id'
735
- )
736
- expect(tcell_context).to receive(:add_response_db_filter).with(
737
- 20, id_rule_dos, 'test_database', '*', 'some_models', 'id'
738
- )
739
-
740
- TCellAgent::DLP.instrument_pluck([[10, 'dies'], [20, 'veinte']], %i[id name], SomeModel)
741
-
742
- expect(tcell_context.database_result_sizes).to eq([2])
743
- end
744
- end
745
- end
746
- end
747
- end
748
- end
749
-
750
- describe '.instrument_find_by_sql' do
751
- context 'with empty results' do
752
- it 'should skip over instrumentation' do
753
- expect(TCellAgent).to_not receive(:configuration)
754
-
755
- TCellAgent::DLP.instrument_find_by_sql([])
756
- end
757
- end
758
-
759
- context 'with no dlp policy' do
760
- it 'should skip over instrumentation' do
761
- configuration = double(
762
- 'configuration',
763
- {
764
- :enabled => true,
765
- :should_instrument? => true,
766
- :should_intercept_requests? => true
767
- }
768
- )
769
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
770
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(nil)
771
-
772
- TCellAgent::DLP.instrument_find_by_sql([SomeModel.new])
773
- end
774
- end
775
-
776
- context 'with dlp policy disabled' do
777
- it 'should skip over instrumentation' do
778
- configuration = double(
779
- 'configuration',
780
- {
781
- :enabled => true,
782
- :should_instrument? => true,
783
- :should_intercept_requests? => true
784
- }
785
- )
786
- dataloss_policy = double(
787
- 'dataloss_policy',
788
- {
789
- :enabled => false
790
- }
791
- )
792
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
793
- expect(TCellAgent).to receive(:policy).with(
794
- TCellAgent::PolicyTypes::DATALOSS
795
- ).and_return(dataloss_policy)
796
-
797
- TCellAgent::DLP.instrument_find_by_sql([SomeModel.new])
798
- end
799
- end
800
-
801
- context 'with dlp policy enabled' do
802
- context 'with database_discovery_enabled' do
803
- it 'should send model columns to tcell service' do
804
- configuration = double(
805
- 'configuration',
806
- {
807
- :enabled => true,
808
- :should_instrument? => true,
809
- :should_intercept_requests? => true,
810
- :max_data_ex_db_records_per_request => 1
811
- }
812
- )
813
- dataloss_policy = double(
814
- 'dataloss_policy',
815
- {
816
- :enabled => true,
817
- :database_discovery_enabled => true
818
- }
819
- )
820
- request_env = double('request_env', {})
821
- tcell_context = double(
822
- 'tcell_context',
823
- {
824
- :route_id => 'ma_route_id',
825
- :database_result_sizes => []
826
- }
827
- )
828
-
829
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
830
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
831
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
832
- :fetch
833
- ).and_return(request_env)
834
- expect(request_env).to receive(:[]).with(
835
- TCellAgent::Instrumentation::TCELL_ID
836
- ).and_return(tcell_context)
837
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
838
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
839
- ).and_return(nil)
840
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
841
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
842
- ).and_return(nil)
843
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
844
- 'test_database', '*', 'some_models', 'email', 'ma_route_id'
845
- ).and_return(nil)
846
-
847
- expect(TCellAgent).to receive(:discover_database_fields).with(
848
- 'ma_route_id', 'test_database', '*', 'some_models', %w[id name email]
849
- )
850
-
851
- TCellAgent::DLP.instrument_find_by_sql([SomeModel.new])
852
-
853
- expect(tcell_context.database_result_sizes).to eq([1])
854
- end
855
- end
856
-
857
- context 'with database_discovery_enabled == false' do
858
- context 'that doesn\'t have any rules set' do
859
- it 'should not send anything to tcell' do
860
- configuration = double(
861
- 'configuration',
862
- {
863
- :enabled => true,
864
- :should_instrument? => true,
865
- :should_intercept_requests? => true,
866
- :max_data_ex_db_records_per_request => 1000
867
- }
868
- )
869
- dataloss_policy = double(
870
- 'dataloss_policy',
871
- {
872
- :enabled => true,
873
- :database_discovery_enabled => false
874
- }
875
- )
876
- request_env = double('request_env', {})
877
- tcell_context = double(
878
- 'tcell_context',
879
- {
880
- :route_id => 'ma_route_id',
881
- :database_result_sizes => []
882
- }
883
- )
884
-
885
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
886
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
887
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
888
- :fetch
889
- ).and_return(request_env)
890
- expect(request_env).to receive(:[]).with(
891
- TCellAgent::Instrumentation::TCELL_ID
892
- ).and_return(tcell_context)
893
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
894
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
895
- ).and_return(nil)
896
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
897
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
898
- ).and_return(nil)
899
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
900
- 'test_database', '*', 'some_models', 'email', 'ma_route_id'
901
- ).and_return(nil)
902
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
903
-
904
- expect(TCellAgent).to_not receive(:discover_database_fields)
905
-
906
- TCellAgent::DLP.instrument_find_by_sql([SomeModel.new])
907
-
908
- expect(tcell_context.database_result_sizes).to eq([1])
909
- end
910
- end
911
-
912
- context 'that has a rule set' do
913
- it 'should add a response filter' do
914
- configuration = double(
915
- 'configuration',
916
- {
917
- :enabled => true,
918
- :should_instrument? => true,
919
- :should_intercept_requests? => true,
920
- :max_data_ex_db_records_per_request => 1000
921
- }
922
- )
923
- dataloss_policy = double(
924
- 'dataloss_policy',
925
- {
926
- :enabled => true,
927
- :database_discovery_enabled => false
928
- }
929
- )
930
- request_env = double('request_env', {})
931
- tcell_context = double(
932
- 'tcell_context',
933
- {
934
- :route_id => 'ma_route_id',
935
- :database_result_sizes => []
936
- }
937
- )
938
- id_rule = double('id_rule')
939
- some_model = SomeModel.new
940
-
941
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
942
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
943
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
944
- :fetch
945
- ).and_return(request_env)
946
- expect(request_env).to receive(:[]).with(
947
- TCellAgent::Instrumentation::TCELL_ID
948
- ).and_return(tcell_context)
949
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
950
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
951
- ).and_return([id_rule])
952
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
953
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
954
- ).and_return(nil)
955
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
956
- 'test_database', '*', 'some_models', 'email', 'ma_route_id'
957
- ).and_return(nil)
958
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
959
-
960
- expect(TCellAgent).to_not receive(:discover_database_fields)
961
- expect(some_model).to receive(:[]).with(:id).and_return(10)
962
- expect(tcell_context).to receive(:add_response_db_filter).with(
963
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
964
- )
965
-
966
- TCellAgent::DLP.instrument_find_by_sql([some_model])
967
-
968
- expect(tcell_context.database_result_sizes).to eq([1])
969
- end
970
- end
971
-
972
- context 'that has two rules set' do
973
- it 'should add a response filter' do
974
- configuration = double(
975
- 'configuration',
976
- {
977
- :enabled => true,
978
- :should_instrument? => true,
979
- :should_intercept_requests? => true,
980
- :max_data_ex_db_records_per_request => 1000
981
- }
982
- )
983
- dataloss_policy = double(
984
- 'dataloss_policy',
985
- {
986
- :enabled => true,
987
- :database_discovery_enabled => false
988
- }
989
- )
990
- request_env = double('request_env', {})
991
- tcell_context = double(
992
- 'tcell_context',
993
- {
994
- :route_id => 'ma_route_id',
995
- :database_result_sizes => []
996
- }
997
- )
998
- id_rule = double('id_rule')
999
- id_rule_dos = double('id_rule_dos')
1000
- some_model = SomeModel.new
1001
-
1002
- allow(TCellAgent).to receive(:configuration).and_return(configuration)
1003
- expect(TCellAgent).to receive(:policy).with(TCellAgent::PolicyTypes::DATALOSS).and_return(dataloss_policy)
1004
- expect(TCellAgent::Instrumentation::Rails::Middleware::ContextMiddleware::THREADS).to receive(
1005
- :fetch
1006
- ).and_return(request_env)
1007
- expect(request_env).to receive(:[]).with(
1008
- TCellAgent::Instrumentation::TCELL_ID
1009
- ).and_return(tcell_context)
1010
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
1011
- 'test_database', '*', 'some_models', 'id', 'ma_route_id'
1012
- ).and_return([id_rule, id_rule_dos])
1013
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
1014
- 'test_database', '*', 'some_models', 'name', 'ma_route_id'
1015
- ).and_return(nil)
1016
- expect(dataloss_policy).to receive(:get_actions_for_table).with(
1017
- 'test_database', '*', 'some_models', 'email', 'ma_route_id'
1018
- ).and_return(nil)
1019
- expect(dataloss_policy).to_not receive(:get_actions_for_table)
1020
-
1021
- expect(TCellAgent).to_not receive(:discover_database_fields)
1022
- expect(some_model).to receive(:[]).with(:id).and_return(10)
1023
- expect(some_model).to receive(:[]).with(:id).and_return(10)
1024
- expect(tcell_context).to receive(:add_response_db_filter).with(
1025
- 10, id_rule, 'test_database', '*', 'some_models', 'id'
1026
- )
1027
- expect(tcell_context).to receive(:add_response_db_filter).with(
1028
- 10, id_rule_dos, 'test_database', '*', 'some_models', 'id'
1029
- )
1030
-
1031
- TCellAgent::DLP.instrument_find_by_sql([some_model])
1032
-
1033
- expect(tcell_context.database_result_sizes).to eq([1])
1034
- end
1035
- end
1036
- end
1037
- end
1038
- end
1039
- end
1040
- end