moesif_rack 1.4.15 → 1.4.16

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b9533c0d146d3b8f0b3a6f8b5edf0f9cda27e7207fb34d92f7c24f07b33a4d38
4
- data.tar.gz: 233466faadd9f15e6bf22a65b3995a78c5eb9a752bf36c743440ad094486dc28
3
+ metadata.gz: a4dc902308676d3b9bb9a23a61f9fc166024b0848d06e026f98a68be8de336b7
4
+ data.tar.gz: 898963aa24db49d6772f4a037d2919650c93e989e9ba735db8ac5e0c6b65bc02
5
5
  SHA512:
6
- metadata.gz: 0236b79eb83085bfe54cca228792267031bea3edc6aa71cb556adef68ff46143c4087035396c8698b6502f7b93137444dd6a5337a060c9777b5e6933f4352af2
7
- data.tar.gz: '08bfc8a7fe975f9f6d5992a4bcf4b761852c361b30882577dc8e031cb066f78c5735cae1ba5315e4725623385cec5a6ebb057a48e1b67ea86dd327db25c754b8'
6
+ metadata.gz: dfca38cca1df0bf26113b8c7198f478dd178f0cf925c2103972b4770e6bcfa032eaa9079801079224cc7127d6166d499e73bc826062835675efe3ff642ba8fd9
7
+ data.tar.gz: 89d1b57bddaa065bc338aad7c92451405ad6a077bfc0e17635da7e42412681a922a22a2395b15f4a478969a4c63114fea582b693523504a87c3f1aec76862c50
@@ -4,12 +4,14 @@ require 'time'
4
4
  require 'zlib'
5
5
  require 'stringio'
6
6
  require_relative './helpers.rb'
7
+ require_relative './regex_config_helper.rb'
7
8
 
8
9
  class AppConfig
9
10
 
10
11
  def initialize debug
11
12
  @debug = debug
12
13
  @helpers = Helpers.new(debug)
14
+ @regex_config_helper = RegexConfigHelper.new(debug)
13
15
  end
14
16
 
15
17
  def get_config(api_controller)
@@ -55,7 +57,7 @@ class AppConfig
55
57
  end
56
58
  end
57
59
 
58
- def get_sampling_percentage(config_api_response, user_id, company_id)
60
+ def get_sampling_percentage(event_model, config_api_response, user_id, company_id)
59
61
  # Get sampling percentage
60
62
  begin
61
63
  # Check if response body is not nil
@@ -63,6 +65,17 @@ class AppConfig
63
65
  @helpers.log_debug("Getting sample rate for user #{user_id} company #{company_id}")
64
66
  @helpers.log_debug(config_api_response.to_s)
65
67
 
68
+ # Get Regex Sampling rate
69
+ regex_config = config_api_response.fetch('regex_config', nil)
70
+
71
+ if !regex_config.nil? and !event_model.nil?
72
+ config_mapping = @regex_config_helper.prepare_config_mapping(event_model)
73
+ regex_sample_rate = @regex_config_helper.fetch_sample_rate_on_regex_match(regex_config, config_mapping)
74
+ if !regex_sample_rate.nil?
75
+ return regex_sample_rate
76
+ end
77
+ end
78
+
66
79
  # Get user sample rate object
67
80
  user_sample_rate = config_api_response.fetch('user_sample_rate', nil)
68
81
 
@@ -87,6 +100,7 @@ class AppConfig
87
100
  end
88
101
  rescue => exception
89
102
  @helpers.log_debug 'Error while geting sampling percentage, assuming default behavior'
103
+ @helpers.log_debug exception.to_s
90
104
  return 100
91
105
  end
92
106
  end
@@ -2,13 +2,13 @@ require 'moesif_api'
2
2
  require 'json'
3
3
  require 'time'
4
4
  require 'base64'
5
+ require 'zlib'
6
+ require 'stringio'
5
7
  require_relative './client_ip.rb'
6
8
  require_relative './app_config.rb'
7
9
  require_relative './update_user.rb'
8
10
  require_relative './update_company.rb'
9
11
  require_relative './helpers.rb'
10
- require 'zlib'
11
- require 'stringio'
12
12
 
13
13
  module MoesifRack
14
14
 
@@ -286,7 +286,7 @@ module MoesifRack
286
286
  random_percentage = Random.rand(0.00..100.00)
287
287
 
288
288
  begin
289
- sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id)
289
+ sampling_percentage = @app_config.get_sampling_percentage(event_model, @config, event_model.user_id, event_model.company_id)
290
290
  @helpers.log_debug "Using sample rate #{sampling_percentage}"
291
291
  rescue => exception
292
292
  @helpers.log_debug 'Error while getting sampling percentage, assuming default behavior'
@@ -341,6 +341,8 @@ module MoesifRack
341
341
  @helpers.log_debug exception.to_s
342
342
  @helpers.log_debug exception.backtrace
343
343
  end
344
+ else
345
+ @helpers.log_debug "Skipped Event using should_skip configuration option."
344
346
  end
345
347
 
346
348
  [status, headers, body]
@@ -0,0 +1,119 @@
1
+ require 'moesif_api'
2
+
3
+ require_relative './helpers.rb'
4
+
5
+ class RegexConfigHelper
6
+
7
+ def initialize debug
8
+ @debug = debug
9
+ end
10
+
11
+ def prepare_config_mapping(event)
12
+ # Function to prepare config mapping
13
+ # Params:
14
+ # - event: Event to be logged
15
+ # Return:
16
+ # - regex_config: Regex config mapping
17
+ regex_config = {}
18
+
19
+ # Config mapping for request.verb
20
+ if defined? event.request.verb
21
+ regex_config["request.verb"] = event.request.verb
22
+ end
23
+
24
+ # Config mapping for request.uri
25
+ if defined? event.request.uri
26
+ extracted = /http[s]*:\/\/[^\/]+(\/[^?]+)/.match(event.request.uri)
27
+ if !extracted.nil?
28
+ route_mapping = extracted.captures[0]
29
+ else
30
+ route_mapping = '/'
31
+ end
32
+ regex_config["request.route"] = route_mapping
33
+ end
34
+
35
+ # Config mapping for request.ip_address
36
+ if defined? event.request.ip_address
37
+ regex_config["request.ip_address"] = event.request.ip_address
38
+ end
39
+
40
+ # Config mapping for response.status
41
+ if defined? event.response.status
42
+ regex_config["response.status"] = event.response.status
43
+ end
44
+
45
+ return regex_config
46
+
47
+ end
48
+
49
+ def regex_match(event_value, condition_value)
50
+ # Function to perform the regex matching with event value and condition value
51
+ # Params:
52
+ # - event_value: Value associated with event (request)
53
+ # - condition_value: Value associated with the regex config condition
54
+ # Return:
55
+ # - regex_matched: Regex matched value to determine if the regex match was successful
56
+
57
+ extracted = Regexp.new(condition_value).match(event_value)
58
+ if !extracted.nil?
59
+ return extracted.to_s
60
+ end
61
+ end
62
+
63
+ def fetch_sample_rate_on_regex_match(regex_configs, config_mapping)
64
+ # Function to fetch the sample rate and determine if request needs to be block or not
65
+ # Args:
66
+ # - regex_configs: Regex configs
67
+ # - config_mapping: Config associated with the request
68
+ # Return:
69
+ # - sample_rate: Sample rate
70
+
71
+ # Iterate through the list of regex configs
72
+ regex_configs.each { |regex_rule|
73
+ # Fetch the sample rate
74
+ sample_rate = regex_rule["sample_rate"]
75
+ # Fetch the conditions
76
+ conditions = regex_rule["conditions"]
77
+ # Bool flag to determine if the regex conditions are matched
78
+ regex_matched = false
79
+ # Create a table to hold the conditions mapping (path and value)
80
+ condition_table = {}
81
+
82
+ # Iterate through the regex rule conditions and map the path and value
83
+ conditions.each { |condition|
84
+ # Add condition path -> value to the condition table
85
+ condition_table[condition["path"]] = condition["value"]
86
+ }
87
+
88
+ # Iterate through conditions table and perform `and` operation between each conditions
89
+ condition_table.each do |path, values|
90
+
91
+ # Check if the path exists in the request config mapping
92
+ if !config_mapping[path].nil?
93
+ # Fetch the value of the path in request config mapping
94
+ event_data = config_mapping[path]
95
+
96
+ # Perform regex matching with event value
97
+ regex_matched = regex_match(event_data, values)
98
+ else
99
+ # Path does not exists in request config mapping, so no need to match regex condition rule
100
+ regex_matched = false
101
+ end
102
+
103
+ # If one of the rule does not match, skip the condition & avoid matching other rules for the same condition
104
+ if !regex_matched
105
+ break
106
+ end
107
+ end
108
+
109
+ # If regex conditions matched, return sample rate
110
+ if regex_matched
111
+ return sample_rate
112
+ end
113
+ }
114
+
115
+ # If regex conditions are not matched, return sample rate as None and will use default sample rate
116
+ return nil
117
+ end
118
+
119
+ end
@@ -176,7 +176,7 @@ module MoesifCaptureOutgoing
176
176
  begin
177
177
  @random_percentage = Random.rand(0.00..100.00)
178
178
  begin
179
- @sampling_percentage = @app_config.get_sampling_percentage(@config, event_model.user_id, event_model.company_id)
179
+ @sampling_percentage = @app_config.get_sampling_percentage(event_model, @config, event_model.user_id, event_model.company_id)
180
180
  rescue => exception
181
181
  if @debug
182
182
  puts 'Error while getting sampling percentage, assuming default behavior'
@@ -118,7 +118,7 @@ class MoesifRackTest < Test::Unit::TestCase
118
118
  @api_controller = @api_client.api
119
119
  @config = @app_config.get_config(@api_controller)
120
120
  @config_body, @config_etag, @last_updated_time = @app_config.parse_configuration(@config)
121
- @sampling_percentage = @app_config.get_sampling_percentage(@config_body, nil, nil)
121
+ @sampling_percentage = @app_config.get_sampling_percentage(nil, @config_body, nil, nil)
122
122
  assert_operator 100, :>=, @sampling_percentage
123
123
  end
124
124
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: moesif_rack
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.4.15
4
+ version: 1.4.16
5
5
  platform: ruby
6
6
  authors:
7
7
  - Moesif, Inc
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2022-02-03 00:00:00.000000000 Z
12
+ date: 2022-02-24 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: test-unit
@@ -58,6 +58,7 @@ files:
58
58
  - lib/moesif_rack/client_ip.rb
59
59
  - lib/moesif_rack/helpers.rb
60
60
  - lib/moesif_rack/moesif_middleware.rb
61
+ - lib/moesif_rack/regex_config_helper.rb
61
62
  - lib/moesif_rack/update_company.rb
62
63
  - lib/moesif_rack/update_user.rb
63
64
  - moesif_capture_outgoing/httplog.rb
@@ -90,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
90
91
  - !ruby/object:Gem::Version
91
92
  version: '0'
92
93
  requirements: []
93
- rubygems_version: 3.1.6
94
+ rubygems_version: 3.3.3
94
95
  signing_key:
95
96
  specification_version: 4
96
97
  summary: moesif_rack