moesif_rack 1.4.12 → 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: 3799b11682e81edcbfbdba3161dcbf93054be9ef44d90973d52e156ed1b12f8e
4
- data.tar.gz: 6894a3d3b39dddf42542b8fa5265e71b8b156d1d049ecdc0dcdc5c4faf469bc2
3
+ metadata.gz: a4dc902308676d3b9bb9a23a61f9fc166024b0848d06e026f98a68be8de336b7
4
+ data.tar.gz: 898963aa24db49d6772f4a037d2919650c93e989e9ba735db8ac5e0c6b65bc02
5
5
  SHA512:
6
- metadata.gz: 89aa06ce4366280d076cac4db01734fa3a4565f7694e6f5151a309f97cfe31dd646151f025b83b5364f0319ba9a58b9095fbee07fd6e60e5136559538f9fb720
7
- data.tar.gz: 93b2c0db052a1d5b95a0f3ef34afa2de0ccf82d448cf6921d45151da3196837b23ed467b018d0b45dc7d3fbd8afe88cd452ed2977de71f5461d0542117f128d9
6
+ metadata.gz: dfca38cca1df0bf26113b8c7198f478dd178f0cf925c2103972b4770e6bcfa032eaa9079801079224cc7127d6166d499e73bc826062835675efe3ff642ba8fd9
7
+ data.tar.gz: 89d1b57bddaa065bc338aad7c92451405ad6a077bfc0e17635da7e42412681a922a22a2395b15f4a478969a4c63114fea582b693523504a87c3f1aec76862c50
data/README.md CHANGED
@@ -529,7 +529,7 @@ response = MoesifRack::MoesifMiddleware.new(@app, @options).update_companies_bat
529
529
  1. Manually clone the git repo
530
530
  2. From terminal/cmd navigate to the root directory of the middleware.
531
531
  3. Invoke 'gem install moesif_rack'
532
- 4. Add your own application id to 'test/moesif_rack_test.rb'. You can find your Application Id from [_Moesif Dashboard_](https://www.moesif.com/) -> _Top Right Menu_ -> _Installation_
532
+ 4. Add your own application id to 'test/moesif_rack_test.rb'. You can find your Application Id from [_Moesif Dashboard_](https://www.moesif.com/) -> _Bottom Left Menu_ -> _Installation_
533
533
  5. Invoke 'ruby test/moesif_rack_test.rb'
534
534
  6. Invoke 'ruby -I test test/moesif_rack_test.rb -n test_capture_outgoing' to test capturing outgoing API calls from your app to third parties like Stripe, Github or to your own dependencies.
535
535
 
@@ -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)
@@ -45,17 +47,17 @@ class AppConfig
45
47
  else
46
48
  @helpers.log_debug 'Response body is nil, assuming default behavior'
47
49
  # Response body is nil, so assuming default behavior
48
- return nil, 100, Time.now.utc
50
+ return nil, nil, Time.now.utc
49
51
  end
50
52
  rescue => exception
51
53
  @helpers.log_debug 'Error while parsing the configuration object, assuming default behavior'
52
54
  @helpers.log_debug exception.to_s
53
55
  # Assuming default behavior
54
- return nil, 100, Time.now.utc
56
+ return nil, nil, Time.now.utc
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
 
@@ -167,6 +167,9 @@ module MoesifRack
167
167
  req = Rack::Request.new(env)
168
168
  complex_copy = env.dup
169
169
 
170
+ # Filter hash to only have keys of type string
171
+ complex_copy = complex_copy.select { |k, v| k.is_a? String }
172
+
170
173
  req_headers = {}
171
174
  complex_copy.select {|k,v| k.start_with?('HTTP_', 'CONTENT_') }.each do |key, val|
172
175
  new_key = key.sub(/^HTTP_/, '')
@@ -283,7 +286,7 @@ module MoesifRack
283
286
  random_percentage = Random.rand(0.00..100.00)
284
287
 
285
288
  begin
286
- 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)
287
290
  @helpers.log_debug "Using sample rate #{sampling_percentage}"
288
291
  rescue => exception
289
292
  @helpers.log_debug 'Error while getting sampling percentage, assuming default behavior'
@@ -338,6 +341,8 @@ module MoesifRack
338
341
  @helpers.log_debug exception.to_s
339
342
  @helpers.log_debug exception.backtrace
340
343
  end
344
+ else
345
+ @helpers.log_debug "Skipped Event using should_skip configuration option."
341
346
  end
342
347
 
343
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'
@@ -117,7 +117,8 @@ class MoesifRackTest < Test::Unit::TestCase
117
117
  @api_client = MoesifApi::MoesifAPIClient.new(@options['application_id'])
118
118
  @api_controller = @api_client.api
119
119
  @config = @app_config.get_config(@api_controller)
120
- @config_etag, @sampling_percentage, @last_updated_time = @app_config.parse_configuration(@config, @debug)
120
+ @config_body, @config_etag, @last_updated_time = @app_config.parse_configuration(@config)
121
+ @sampling_percentage = @app_config.get_sampling_percentage(nil, @config_body, nil, nil)
121
122
  assert_operator 100, :>=, @sampling_percentage
122
123
  end
123
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.12
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: 2021-07-02 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
@@ -17,40 +17,34 @@ dependencies:
17
17
  requirements:
18
18
  - - "~>"
19
19
  - !ruby/object:Gem::Version
20
- version: '3.1'
20
+ version: '3.5'
21
21
  - - ">="
22
22
  - !ruby/object:Gem::Version
23
- version: 3.1.5
23
+ version: 3.5.0
24
24
  type: :development
25
25
  prerelease: false
26
26
  version_requirements: !ruby/object:Gem::Requirement
27
27
  requirements:
28
28
  - - "~>"
29
29
  - !ruby/object:Gem::Version
30
- version: '3.1'
30
+ version: '3.5'
31
31
  - - ">="
32
32
  - !ruby/object:Gem::Version
33
- version: 3.1.5
33
+ version: 3.5.0
34
34
  - !ruby/object:Gem::Dependency
35
35
  name: moesif_api
36
36
  requirement: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.2'
41
- - - ">="
42
- - !ruby/object:Gem::Version
43
- version: 1.2.12
40
+ version: 1.2.14
44
41
  type: :runtime
45
42
  prerelease: false
46
43
  version_requirements: !ruby/object:Gem::Requirement
47
44
  requirements:
48
45
  - - "~>"
49
46
  - !ruby/object:Gem::Version
50
- version: '1.2'
51
- - - ">="
52
- - !ruby/object:Gem::Version
53
- version: 1.2.12
47
+ version: 1.2.14
54
48
  description: Rack/Rails middleware to log API calls to Moesif API analytics and monitoring
55
49
  email: xing@moesif.com
56
50
  executables: []
@@ -64,6 +58,7 @@ files:
64
58
  - lib/moesif_rack/client_ip.rb
65
59
  - lib/moesif_rack/helpers.rb
66
60
  - lib/moesif_rack/moesif_middleware.rb
61
+ - lib/moesif_rack/regex_config_helper.rb
67
62
  - lib/moesif_rack/update_company.rb
68
63
  - lib/moesif_rack/update_user.rb
69
64
  - moesif_capture_outgoing/httplog.rb
@@ -87,7 +82,7 @@ require_paths:
87
82
  - lib
88
83
  required_ruby_version: !ruby/object:Gem::Requirement
89
84
  requirements:
90
- - - "~>"
85
+ - - ">="
91
86
  - !ruby/object:Gem::Version
92
87
  version: '2.0'
93
88
  required_rubygems_version: !ruby/object:Gem::Requirement
@@ -96,7 +91,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
96
91
  - !ruby/object:Gem::Version
97
92
  version: '0'
98
93
  requirements: []
99
- rubygems_version: 3.1.4
94
+ rubygems_version: 3.3.3
100
95
  signing_key:
101
96
  specification_version: 4
102
97
  summary: moesif_rack