Method: Sensu::Utilities#eval_attribute_value

Defined in:
lib/sensu/utilities.rb

#eval_attribute_value(object, raw_eval_string, raw_value) ⇒ TrueClass, FalseClass

Ruby ‘eval()` a string containing an expression, within the scope/context of a sandbox. This method is for attribute values starting with “eval:”, with the Ruby expression following the colon. A single variable is provided to the expression, `value`, equal to the corresponding object attribute value. Dot notation tokens in the expression, e.g. `:::mysql.user:::`, are substituted with the corresponding object attribute values prior to evaluation. The expression is expected to return a boolean value.

Parameters:

  • object (Hash)
  • raw_eval_string (String)

    containing the Ruby expression to be evaluated.

  • raw_value (Object)

    of the corresponding object attribute value.

Returns:

  • (TrueClass, FalseClass)


274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/sensu/utilities.rb', line 274

def eval_attribute_value(object, raw_eval_string, raw_value)
  eval_string = process_eval_string(object, raw_eval_string)
  unless eval_string.nil?
    begin
      value = Marshal.load(Marshal.dump(raw_value))
      !!Sandbox.eval(eval_string, value)
    rescue StandardError, SyntaxError => error
      @logger.error("attribute value eval error", {
        :object => object,
        :raw_eval_string => raw_eval_string,
        :raw_value => raw_value,
        :error => error.to_s
      })
      false
    end
  else
    false
  end
end