Class: Contrast::Agent::Patching::Policy::PolicyNode Abstract

Inherits:
Object
  • Object
show all
Includes:
Components::Scope::InstanceMethods
Defined in:
lib/contrast/agent/patching/policy/policy_node.rb

Overview

This class is abstract.

This class functions to translate our policy.json into an actionable Ruby object, allowing for dynamic patching over hardcoded patching.

Constant Summary collapse

JSON_METHOD_VISIBILITY =
'method_visibility'
JSON_PROPERTIES =
'properties'
JSON_METHOD_NAME =
'method_name'
JSON_METHOD_SCOPE =
'scope'
JSON_CLASS_NAME =

The keys used to read from policy.json to create the individual policy nodes. These are common across node types

'class_name'
JSON_INSTANCE_METHOD =
'instance_method'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Components::Scope::InstanceMethods

#contrast_enter_method_scopes!, #contrast_exit_method_scopes!, #with_app_scope, #with_contrast_scope, #with_deserialization_scope, #with_split_scope

Constructor Details

#initialize(policy_hash = {}) ⇒ PolicyNode

Returns a new instance of PolicyNode.



25
26
27
28
29
30
31
32
33
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 25

def initialize policy_hash = {}
  @class_name = policy_hash[JSON_CLASS_NAME]
  @instance_method = policy_hash[JSON_INSTANCE_METHOD]
  @method_name = policy_hash[JSON_METHOD_NAME]
  @method_scope = policy_hash[JSON_METHOD_SCOPE]
  @method_visibility = policy_hash[JSON_METHOD_VISIBILITY]
  @properties = policy_hash[JSON_PROPERTIES]
  symbolize
end

Instance Attribute Details

#class_nameObject

Name of the class in which the method is being invoked.



36
37
38
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 36

def class_name
  @class_name
end

#instance_methodObject

Check for instance method.

Returns:

  • true | false



40
41
42
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 40

def instance_method
  @instance_method
end

#method_nameObject

The symbol representation of the invoked method.



42
43
44
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 42

def method_name
  @method_name
end

#method_scopeObject (readonly)

Scope of the method parsed from our JSON policy.



48
49
50
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 48

def method_scope
  @method_scope
end

#method_visibilityObject

Visibility of the invoked method [Private, Public, Protected]



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

def method_visibility
  @method_visibility
end

#propertiesObject (readonly)

Properties parsed from our JSON policy.



46
47
48
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 46

def properties
  @properties
end

Instance Method Details

#featureObject

Raises:

  • (NoMethodError)

    This is being raised if any of the implementing subclasses does not have that method implemented, but is being called on.



58
59
60
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 58

def feature
  raise(NoMethodError, 'specify the name of the feature for which this node patches')
end

#idObject



62
63
64
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 62

def id
  @_id ||= "#{ feature }:#{ node_class }:#{ class_name }#{ instance_method? ? '#' : '.' }#{ method_name }"
end

#instance_method?Boolean

just turns this into a ruby-ism

Returns:

  • (Boolean)


94
95
96
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 94

def instance_method?
  instance_method
end

#node_classObject

Raises:

  • (NoMethodError)

    This is being raised if any of the implementing subclasses does not have that method implemented, but is being called on.



52
53
54
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 52

def node_class
  raise(NoMethodError, 'specify the type of the feature for which this node patches')
end

#validateObject

Don’t let nodes be created that will be missing things we need later on. Really, if they don’t have these things, they couldn’t have done their jobs anyway.

Raises:

  • (ArgumentError)

    Validates if the created nodes have everything that we’ll need now or later on.



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/contrast/agent/patching/policy/policy_node.rb', line 71

def validate
  unless class_name
    raise(ArgumentError, "#{ node_class } #{ id } did not have a proper class name. Unable to create.")
  end
  unless method_name
    raise(ArgumentError, "#{ node_class } #{ id } did not have a proper method name. Unable to create.")
  end
  unless method_name.is_a?(Symbol)
    raise(ArgumentError, "#{ node_class } #{ id } has a non symbol @method_name value. Unable to create.")
  end

  unless method_visibility.is_a?(Symbol)
    raise(ArgumentError,
          "#{ node_class } #{ id } has a non symbol @method_visibility value. Unable to create.")
  end
  unless method_scope.nil? || Contrast::Agent::Scope.valid_scope?(method_scope)
    raise(ArgumentError, "#{ node_class } #{ id } requires an undefined scope. Unable to create.")
  end

  nil
end