Module: Contrast::Agent::Patching::Policy::MethodPolicyExtend

Included in:
MethodPolicy
Defined in:
lib/contrast/agent/patching/policy/method_policy_extend.rb

Overview

This class is used to map each method to the trigger node that applies to it

Instance Method Summary collapse

Instance Method Details

#build_method_policy(method_name, module_policy, instance_method) ⇒ Contrast::Agent::Patching::Policy::MethodPolicy

Given a Contrast::Agent::Patching::Policy::ModulePolicy, parse out its information for the given method in order to construct a Contrast::Agent::Patching::Policy::MethodPolicy

Parameters:

  • method_name (Symbol)

    the name of the method for this policy

  • module_policy (Contrast::Agent::Patching::Policy::ModulePolicy)

    the entire policy for this module

  • instance_method (Boolean)

    true if this method is an instance method

Returns:



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
46
# File 'lib/contrast/agent/patching/policy/method_policy_extend.rb', line 20

def build_method_policy method_name, module_policy, instance_method
  source_node =      find_method_node(module_policy.source_nodes, method_name, instance_method)
  propagation_node = find_method_node(module_policy.propagator_nodes, method_name, instance_method)
  trigger_node =     find_method_node(module_policy.trigger_nodes, method_name, instance_method)
  protect_node =     find_method_node(module_policy.protect_nodes, method_name, instance_method)
  inventory_node =   find_method_node(module_policy.inventory_nodes, method_name, instance_method)
  deadzone_node =    find_method_node(module_policy.deadzone_nodes, method_name, instance_method)
  method_visibility = find_visibility(source_node, propagation_node, trigger_node, protect_node,
                                      inventory_node, deadzone_node)
  method_policy = MethodPolicy.new({
                                       method_name: method_name,
                                       method_visibility: method_visibility,
                                       instance_method: instance_method,
                                       source_node: source_node,
                                       propagation_node: propagation_node,
                                       trigger_node: trigger_node,
                                       protect_node: protect_node,
                                       inventory_node: inventory_node,
                                       deadzone_node: deadzone_node
                                   })

  return method_policy unless check_method_policy_nodes_empty?(source_node, propagation_node, trigger_node,
                                                               protect_node, inventory_node, deadzone_node)

  create_new_node(module_policy, method_policy) if module_policy.deadzone_nodes&.any?
  method_policy
end

#check_method_policy_nodes_empty?(source_node, propagation_node, trigger_node, protect_node, inventory_node, deadzone_node) ⇒ Boolean

Returns:

  • (Boolean)


60
61
62
63
64
65
66
67
68
69
70
# File 'lib/contrast/agent/patching/policy/method_policy_extend.rb', line 60

def check_method_policy_nodes_empty?(source_node,
                                     propagation_node,
                                     trigger_node,
                                     protect_node,
                                     inventory_node,
                                     deadzone_node)
  return false unless source_node.nil? && propagation_node.nil? && trigger_node.nil? && protect_node.nil? &&
      inventory_node.nil? && deadzone_node.nil?

  true
end

#find_method_node(nodes, method_name, is_instance_method) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/contrast/agent/patching/policy/method_policy_extend.rb', line 48

def find_method_node nodes, method_name, is_instance_method
  return unless nodes

  nodes.find do |node|
    node.instance_method? == is_instance_method && node.method_name == method_name
  end
end

#find_visibility(*nodes) ⇒ Object



56
57
58
# File 'lib/contrast/agent/patching/policy/method_policy_extend.rb', line 56

def find_visibility *nodes
  nodes.find { |node| node }&.method_visibility
end