Class: RegexConfigHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/moesif_rack/regex_config_helper.rb

Instance Method Summary collapse

Constructor Details

#initialize(debug) ⇒ RegexConfigHelper

Returns a new instance of RegexConfigHelper.



6
7
8
# File 'lib/moesif_rack/regex_config_helper.rb', line 6

def initialize(debug)
  @debug = debug
end

Instance Method Details

#fetch_sample_rate_on_regex_match(regex_configs, config_mapping) ⇒ Object



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
# File 'lib/moesif_rack/regex_config_helper.rb', line 61

def fetch_sample_rate_on_regex_match(regex_configs, config_mapping)
  # Function to fetch the sample rate and determine if request needs to be block or not
  # Args:
  #  - regex_configs: Regex configs
  #  - config_mapping: Config associated with the request
  # Return:
  #  - sample_rate: Sample rate

  # Iterate through the list of regex configs
  regex_configs.each do |regex_rule|
    # Fetch the sample rate
    sample_rate = regex_rule['sample_rate']
    # Fetch the conditions
    conditions = regex_rule['conditions']
    # Bool flag to determine if the regex conditions are matched
    regex_matched = false
    # Create a table to hold the conditions mapping (path and value)
    condition_table = {}

    # Iterate through the regex rule conditions and map the path and value
    conditions.each do |condition|
      # Add condition path -> value to the condition table
      condition_table[condition['path']] = condition['value']
    end

    # Iterate through conditions table and perform `and` operation between each conditions
    condition_table.each do |path, values|
      # Check if the path exists in the request config mapping
      if !config_mapping[path].nil?
        # Fetch the value of the path in request config mapping
        event_data = config_mapping[path]

        # Perform regex matching with event value
        regex_matched = regex_match(event_data, values)
      else
        # Path does not exists in request config mapping, so no need to match regex condition rule
        regex_matched = false
      end

      # If one of the rule does not match, skip the condition & avoid matching other rules for the same condition
      break unless regex_matched
    end

    # If regex conditions matched, return sample rate
    return sample_rate if regex_matched
  end

  # If regex conditions are not matched, return sample rate as None and will use default sample rate
  nil
end

#prepare_config_mapping(event) ⇒ Object



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
# File 'lib/moesif_rack/regex_config_helper.rb', line 10

def prepare_config_mapping(event)
  # Function to prepare config mapping
  # Params:
  #  - event: Event to be logged
  # Return:
  #  - regex_config: Regex config mapping
  regex_config = {}

  # Config mapping for request.verb
  if defined? event.request.verb
    regex_config['request.verb'] = event.request.verb
  end

  # Config mapping for request.uri
  if defined? event.request.uri
    extracted = %r{https*://[^/]+(/[^?]+)}.match(event.request.uri)
    route_mapping = if !extracted.nil?
                      extracted.captures[0]
                    else
                      '/'
                    end
    regex_config['request.route'] = route_mapping
  end

  # Config mapping for request.ip_address
  if defined? event.request.ip_address
    regex_config['request.ip_address'] = event.request.ip_address
  end

  # Config mapping for response.status
  if defined? event.response.status
    regex_config['response.status'] = event.response.status
  end

  regex_config
end

#regex_match(event_value, condition_value) ⇒ Object



47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/moesif_rack/regex_config_helper.rb', line 47

def regex_match(event_value, condition_value)
  # Function to perform the regex matching with event value and condition value
  # Params:
  #  - event_value: Value associated with event (request)
  #  - condition_value: Value associated with the regex config condition
  # Return:
  #  - regex_matched: Regex matched value to determine if the regex match was successful

  extracted = Regexp.new(condition_value).match(event_value)
  return if extracted.nil?

  extracted.to_s
end