Class: Contrast::Agent::Patching::Policy::ModulePolicy

Inherits:
Object
  • Object
show all
Defined in:
lib/contrast/agent/patching/policy/module_policy.rb

Overview

This class is used to map a class to all policy nodes utilizing that class. It should be initialized using the create_module_policy method rather than new.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#deadzone_nodesObject

Returns the value of attribute deadzone_nodes.



49
50
51
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 49

def deadzone_nodes
  @deadzone_nodes
end

#inventory_nodesObject

Returns the value of attribute inventory_nodes.



49
50
51
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 49

def inventory_nodes
  @inventory_nodes
end

#propagator_nodesObject

Returns the value of attribute propagator_nodes.



49
50
51
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 49

def propagator_nodes
  @propagator_nodes
end

#protect_nodesObject

Returns the value of attribute protect_nodes.



49
50
51
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 49

def protect_nodes
  @protect_nodes
end

#source_nodesObject

Returns the value of attribute source_nodes.



49
50
51
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 49

def source_nodes
  @source_nodes
end

#trigger_nodesObject

Returns the value of attribute trigger_nodes.



49
50
51
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 49

def trigger_nodes
  @trigger_nodes
end

Class Method Details

.create_module_policy(module_name) ⇒ Contrast::Agent::Patching::Policy::ModulePolicy

Given the name of a module, create a :ModulePolicy for it using the Policy of each supported feature.

Parameters:

  • module_name (String)

    the name of the module to which the policy applies.

Returns:



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 19

def create_module_policy module_name
  module_policy = Contrast::Agent::Patching::Policy::ModulePolicy.new
  module_policy.source_nodes =      nodes_for_module(
      Contrast::Agent::Assess::Policy::Policy.instance.sources, module_name)
  module_policy.propagator_nodes =  nodes_for_module(
      Contrast::Agent::Assess::Policy::Policy.instance.propagators, module_name)
  module_policy.trigger_nodes =     nodes_for_module(
      Contrast::Agent::Assess::Policy::Policy.instance.triggers, module_name)
  module_policy.protect_nodes =     nodes_for_module(
      Contrast::Agent::Protect::Policy::Policy.instance.triggers, module_name)
  module_policy.inventory_nodes =   nodes_for_module(
      Contrast::Agent::Inventory::Policy::Policy.instance.triggers, module_name)
  module_policy.deadzone_nodes =    nodes_for_module(
      Contrast::Agent::Deadzone::Policy::Policy.instance.deadzones, module_name)
  module_policy
end

.nodes_for_module(nodes, class_name) ⇒ Array<Contrast::Agent::Patching::Policy::PolicyNode>

Find any of the given patchers that match this class’ names. Always returns an array, even if it’s empty.

Parameters:

Returns:



44
45
46
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 44

def nodes_for_module nodes, class_name
  nodes.select { |node| class_name == node.class_name }
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
58
59
60
61
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 52

def empty?
  return false if source_nodes.any?
  return false if propagator_nodes.any?
  return false if trigger_nodes.any?
  return false if inventory_nodes.any?
  return false if protect_nodes.any?
  return false if deadzone_nodes.any?

  true
end

#num_expected_patchesInteger

The number of expected patches for this policy is the sum of unique targeted methods for the Module to which this policy applies.

Returns:

  • (Integer)

    count of methods to be patched



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/contrast/agent/patching/policy/module_policy.rb', line 67

def num_expected_patches
  @_num_expected_patches ||= begin
    instance_methods = Set.new
    singleton_methods = Set.new
    sort_method_names(source_nodes, instance_methods, singleton_methods)
    sort_method_names(propagator_nodes, instance_methods, singleton_methods)
    sort_method_names(trigger_nodes, instance_methods, singleton_methods)
    sort_method_names(inventory_nodes, instance_methods, singleton_methods)
    sort_method_names(protect_nodes, instance_methods, singleton_methods)
    sort_method_names(deadzone_nodes, instance_methods, singleton_methods)
    instance_methods.length + singleton_methods.length
  end
end