Class: DurableDecorator::Validator

Inherits:
Object
  • Object
show all
Defined in:
lib/durable_decorator/validator.rb

Constant Summary collapse

DECORATION_MODES =
['strict', 'soft']

Class Method Summary collapse

Class Method Details

.handle_soft_fault(clazz, method_name, expected_sha, provided_sha) ⇒ Object



33
34
35
36
# File 'lib/durable_decorator/validator.rb', line 33

def handle_soft_fault clazz, method_name, expected_sha, provided_sha
  Util.logger.warn "#{clazz}##{method_name} decoration uses an invalid SHA. The original method definition could have been tampered with!"
  Util.logger.warn "Expected SHA was #{expected_sha} but the provided SHA is #{provided_sha}"
end

.handle_strict_fault(clazz, method_name, expected_sha, provided_sha) ⇒ Object



29
30
31
# File 'lib/durable_decorator/validator.rb', line 29

def handle_strict_fault clazz, method_name, expected_sha, provided_sha
  raise TamperedDefinitionError, "Method SHA mismatch, the definition has been tampered with. #{expected_sha} is expected but #{provided_sha} was provided."
end

.log_sha_suggestion(clazz, method_name, expected_sha) ⇒ Object



25
26
27
# File 'lib/durable_decorator/validator.rb', line 25

def log_sha_suggestion clazz, method_name, expected_sha
  Util.logger.warn "#{clazz}##{method_name} definition's SHA is currently: #{expected_sha}. Consider sealing it against tampering."
end

.validate_decoration_meta(clazz, method_name, old_method, meta) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/durable_decorator/validator.rb', line 6

def validate_decoration_meta clazz, method_name, old_method, meta
  expected_sha = Util.method_sha(old_method)
  
  unless meta
    log_sha_suggestion clazz, method_name, expected_sha
    return
  end

  chill_meta = Util.symbolized_hash(meta)
  provided_mode = chill_meta[:mode]
  provided_sha = chill_meta[:sha]

  raise InvalidDecorationError, "The :mode provided is invalid. Possible modes are: #{DECORATION_MODES.join(", ")}" unless DECORATION_MODES.include? provided_mode
  if provided_mode == 'strict'
    raise InvalidDecorationError, "The SHA provided appears to be empty" unless provided_sha and !provided_sha.empty?
  end
  send("handle_#{chill_meta[:mode]}_fault", clazz, method_name, expected_sha, provided_sha) unless expected_sha == provided_sha
end

.validate_existing_definition(clazz, method_name) ⇒ Object



42
43
44
45
46
47
48
# File 'lib/durable_decorator/validator.rb', line 42

def validate_existing_definition clazz, method_name
  begin
    clazz.instance_method(method_name)
  rescue NameError => e
    raise UndefinedMethodError, "#{clazz}##{method_name} is not defined."
  end
end

.validate_method_arity(clazz, method_name, old_method, &block) ⇒ Object

Raises:



38
39
40
# File 'lib/durable_decorator/validator.rb', line 38

def validate_method_arity clazz, method_name, old_method, &block
  raise BadArityError, "Attempting to override #{clazz}'s #{method_name} with incorrect arity." if block.arity != old_method.arity and block.arity > 0 # See the #arity behavior disparity between 1.8- and 1.9+
end