2015-06-07 19:41:54 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Dominik Richter
|
|
|
|
# license: All rights reserved
|
|
|
|
|
|
|
|
class VulcanoBaseRule
|
2015-06-19 22:00:53 +00:00
|
|
|
def initialize(id, opts, &block)
|
2015-06-07 19:41:54 +00:00
|
|
|
@id = id
|
2015-06-10 15:21:03 +00:00
|
|
|
@impact = nil
|
2015-08-09 18:29:59 +00:00
|
|
|
@__code = ""
|
2015-08-09 20:31:51 +00:00
|
|
|
@__block = block
|
2015-06-10 15:21:03 +00:00
|
|
|
@title = nil
|
|
|
|
@desc = nil
|
2015-06-19 22:00:53 +00:00
|
|
|
# not changeable by the user:
|
|
|
|
@profile_id = nil
|
|
|
|
@checks = []
|
|
|
|
# evaluate the given definition
|
2015-06-19 14:32:11 +00:00
|
|
|
self.instance_eval(&block) if block_given?
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def id(v = nil)
|
|
|
|
# never overwrite the ID
|
|
|
|
@id
|
|
|
|
end
|
|
|
|
|
|
|
|
def impact(v = nil)
|
|
|
|
@impact = v unless v.nil?
|
|
|
|
@impact
|
|
|
|
end
|
|
|
|
|
|
|
|
def title(v = nil)
|
|
|
|
@title = v unless v.nil?
|
|
|
|
@title
|
|
|
|
end
|
|
|
|
|
|
|
|
def desc(v = nil)
|
|
|
|
@desc = v unless v.nil?
|
|
|
|
@desc
|
|
|
|
end
|
2015-06-19 22:00:53 +00:00
|
|
|
|
|
|
|
def self.merge dst, src
|
|
|
|
if src.id != dst.id
|
|
|
|
# TODO: register an error, this case should not happen
|
|
|
|
return
|
|
|
|
end
|
|
|
|
sp = src.instance_variable_get(:@profile_id)
|
|
|
|
dp = dst.instance_variable_get(:@profile_id)
|
|
|
|
if sp != dp
|
|
|
|
# TODO: register an error, this case should not happen
|
|
|
|
return
|
|
|
|
end
|
|
|
|
# merge all fields
|
|
|
|
dst.impact(src.impact) if src.impact != nil
|
|
|
|
dst.title(src.title) if src.title != nil
|
|
|
|
dst.desc(src.desc) if src.desc != nil
|
|
|
|
# merge indirect fields
|
|
|
|
# checks defined in the source will completely eliminate
|
|
|
|
# all checks that were defined in the destination
|
|
|
|
sc = src.instance_variable_get(:@checks)
|
|
|
|
unless sc.nil? || sc.empty?
|
|
|
|
dst.instance_variable_set(:@checks, sc)
|
|
|
|
end
|
|
|
|
end
|
2015-06-19 22:20:44 +00:00
|
|
|
|
|
|
|
# Get the full id consisting of profile id + rule id
|
|
|
|
# for the rule. If the rule's profile id is empty,
|
|
|
|
# the given profile_id will be used instead and also
|
|
|
|
# set for the rule.
|
2015-08-13 00:29:23 +00:00
|
|
|
def self.full_id profile_id, rule
|
|
|
|
if rule.is_a?(String) or rule.nil?
|
|
|
|
rid = rule
|
|
|
|
else
|
|
|
|
# As the profile context is exclusively pulled with a
|
|
|
|
# profile ID, attach it to the rule if necessary.
|
|
|
|
rid = rule.instance_variable_get(:@id)
|
|
|
|
if rid.nil?
|
|
|
|
# TODO: Message about skipping this rule
|
|
|
|
# due to missing ID
|
|
|
|
return nil
|
|
|
|
end
|
2015-06-19 22:20:44 +00:00
|
|
|
end
|
|
|
|
pid = rule.instance_variable_get(:@profile_id)
|
|
|
|
if pid.nil?
|
|
|
|
rule.instance_variable_set(:@profile_id, profile_id)
|
|
|
|
pid = profile_id
|
|
|
|
end
|
2015-08-13 00:29:23 +00:00
|
|
|
if pid.nil? or pid.empty?
|
|
|
|
return rid
|
|
|
|
else
|
|
|
|
return "#{pid}/#{rid}"
|
|
|
|
end
|
2015-06-19 22:20:44 +00:00
|
|
|
end
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|