2015-06-07 15:09:02 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Dominik Richter
|
|
|
|
# license: All rights reserved
|
2015-06-07 19:41:54 +00:00
|
|
|
require 'vulcano/base_rule'
|
2015-06-07 15:09:02 +00:00
|
|
|
|
2015-06-19 18:11:26 +00:00
|
|
|
module Vulcano
|
|
|
|
class Rule < VulcanoBaseRule
|
|
|
|
include Serverspec::Helper::Type
|
|
|
|
extend Serverspec::Helper::Type
|
|
|
|
include RSpec::Core::DSL
|
|
|
|
|
|
|
|
# Override RSpec methods to add
|
|
|
|
# IDs to each example group
|
|
|
|
# TODO: remove this once IDs are in rspec-core
|
|
|
|
def describe(sth, &block)
|
2015-06-19 19:17:56 +00:00
|
|
|
@checks.push(['describe', [sth], block])
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
# redirect all regular method calls to the
|
|
|
|
# core DSL (which is attached to the class)
|
|
|
|
def method_missing(m, *a, &b)
|
2015-06-19 20:18:54 +00:00
|
|
|
Vulcano::Rule.__send__(m, *a, &b)
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
2015-06-07 15:09:02 +00:00
|
|
|
end
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module Vulcano::DSL
|
2015-06-19 20:18:54 +00:00
|
|
|
def rule id, opts = {}, &block
|
2015-06-19 22:00:53 +00:00
|
|
|
__register_rule Vulcano::Rule.new(id, opts, &block)
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 22:20:44 +00:00
|
|
|
def skip_rule id
|
|
|
|
__unregister_rule id
|
|
|
|
end
|
|
|
|
|
2015-06-19 18:11:26 +00:00
|
|
|
def require_rules id, &block
|
2015-06-19 20:18:54 +00:00
|
|
|
::Vulcano::DSL.load_spec_files_for_profile self, id, false, &block
|
2015-06-07 15:09:02 +00:00
|
|
|
end
|
2015-06-07 17:20:16 +00:00
|
|
|
|
2015-06-19 18:11:26 +00:00
|
|
|
def include_rules id, &block
|
2015-06-19 20:18:54 +00:00
|
|
|
::Vulcano::DSL.load_spec_files_for_profile self, id, true, &block
|
2015-06-18 15:32:40 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 19:43:04 +00:00
|
|
|
# Register a given rule with RSpec and
|
|
|
|
# let it run. This happens after everything
|
|
|
|
# else is merged in.
|
|
|
|
def self.execute_rule r
|
|
|
|
checks = r.instance_variable_get(:@checks)
|
|
|
|
id = r.instance_variable_get(:@id)
|
|
|
|
checks.each do |m,a,b|
|
|
|
|
cres = ::Vulcano::Rule.__send__(m, *a, &b)
|
2015-06-19 20:18:54 +00:00
|
|
|
if m == 'describe'
|
2015-06-19 19:43:04 +00:00
|
|
|
set_rspec_ids(cres, id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-07 17:20:16 +00:00
|
|
|
private
|
|
|
|
|
2015-06-19 22:00:53 +00:00
|
|
|
# merge two rules completely; all defined
|
|
|
|
# fields from src will be overwritten in dst
|
|
|
|
def self.merge_rules dst, src
|
|
|
|
VulcanoBaseRule::merge dst, src
|
|
|
|
end
|
|
|
|
|
2015-06-19 19:17:56 +00:00
|
|
|
# Attach an ID attribute to the
|
|
|
|
# metadata of all examples
|
|
|
|
# TODO: remove this once IDs are in rspec-core
|
2015-06-19 19:43:04 +00:00
|
|
|
def self.set_rspec_ids(obj, id)
|
2015-06-19 19:17:56 +00:00
|
|
|
obj.examples.each {|ex|
|
|
|
|
ex.metadata[:id] = id
|
|
|
|
}
|
|
|
|
obj.children.each {|c|
|
2015-06-19 19:43:04 +00:00
|
|
|
self.set_rspec_ids(c, id)
|
2015-06-19 19:17:56 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-06-19 22:00:53 +00:00
|
|
|
def self.load_spec_file_for_profile profile_id, file, rule_registry
|
2015-06-19 20:18:54 +00:00
|
|
|
raw = File::read(file)
|
|
|
|
# TODO: error-handling
|
|
|
|
|
2015-06-19 22:00:53 +00:00
|
|
|
ctx = Vulcano::ProfileContext.new(profile_id, rule_registry)
|
2015-06-19 20:18:54 +00:00
|
|
|
ctx.instance_eval(raw, file, 1)
|
|
|
|
end
|
|
|
|
|
2015-06-19 22:00:53 +00:00
|
|
|
def self.load_spec_files_for_profile bind_context, profile_id, include_all, &block
|
2015-06-19 20:18:54 +00:00
|
|
|
# get all spec files
|
2015-06-19 22:00:53 +00:00
|
|
|
files = get_spec_files_for_profile profile_id
|
2015-06-19 20:18:54 +00:00
|
|
|
# load all rules from spec files
|
2015-06-19 22:00:53 +00:00
|
|
|
rule_registry = {}
|
|
|
|
files.each do |file|
|
|
|
|
load_spec_file_for_profile(profile_id, file, rule_registry)
|
|
|
|
end
|
2015-06-19 20:18:54 +00:00
|
|
|
|
2015-06-19 22:00:53 +00:00
|
|
|
# interpret the block and create a set of rules from it
|
|
|
|
block_registry = {}
|
|
|
|
if block_given?
|
|
|
|
ctx = Vulcano::ProfileContext.new(profile_id, block_registry)
|
|
|
|
ctx.instance_eval(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
# if all rules are not included, select only the ones
|
|
|
|
# that were defined in the block
|
|
|
|
unless include_all
|
|
|
|
remove = rule_registry.keys - block_registry.keys
|
|
|
|
remove.each{|key| rule_registry.delete(key)}
|
|
|
|
end
|
|
|
|
|
|
|
|
# merge the rules in the block_registry (adjustments) with
|
|
|
|
# the rules in the rule_registry (included)
|
|
|
|
block_registry.each do |id,r|
|
|
|
|
org = rule_registry[id]
|
|
|
|
if org.nil?
|
|
|
|
# TODO: print error because we write alter a rule that doesn't exist
|
2015-06-19 22:20:44 +00:00
|
|
|
elsif r.nil?
|
|
|
|
rule_registry.delete(id)
|
2015-06-19 22:00:53 +00:00
|
|
|
else
|
|
|
|
merge_rules(org, r)
|
|
|
|
end
|
|
|
|
end
|
2015-06-19 20:18:54 +00:00
|
|
|
|
|
|
|
# finally register all combined rules
|
2015-06-19 22:00:53 +00:00
|
|
|
rule_registry.each do |id, rule|
|
2015-06-19 20:18:54 +00:00
|
|
|
bind_context.__register_rule rule
|
|
|
|
end
|
2015-06-19 19:52:57 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 19:43:04 +00:00
|
|
|
def self.get_spec_files_for_profile id
|
2015-06-19 18:11:26 +00:00
|
|
|
base_path = '/etc/vulcanosec/tests'
|
|
|
|
path = File.join( base_path, id )
|
|
|
|
# find all files to be included
|
|
|
|
files = []
|
|
|
|
if File::directory? path
|
|
|
|
# include all library paths, if they exist
|
|
|
|
libdir = File::join(path, 'lib')
|
|
|
|
if File::directory? libdir and !$LOAD_PATH.include?(libdir)
|
|
|
|
$LOAD_PATH.unshift(libdir)
|
|
|
|
end
|
|
|
|
files = Dir[File.join(path, 'spec','*_spec.rb')]
|
|
|
|
end
|
|
|
|
return files
|
2015-06-07 17:20:16 +00:00
|
|
|
end
|
|
|
|
|
2015-06-07 15:09:02 +00:00
|
|
|
end
|
2015-06-18 15:32:40 +00:00
|
|
|
|
2015-06-19 20:18:54 +00:00
|
|
|
module Vulcano
|
|
|
|
class ProfileContext
|
2015-06-19 22:00:53 +00:00
|
|
|
|
2015-06-19 20:18:54 +00:00
|
|
|
include Vulcano::DSL
|
2015-06-19 22:00:53 +00:00
|
|
|
def initialize profile_id, profile_registry
|
2015-06-19 20:18:54 +00:00
|
|
|
@profile_id = profile_id
|
2015-06-19 22:00:53 +00:00
|
|
|
@rules = profile_registry
|
2015-06-19 20:18:54 +00:00
|
|
|
end
|
2015-06-19 22:00:53 +00:00
|
|
|
|
2015-06-19 22:20:44 +00:00
|
|
|
def __unregister_rule id
|
|
|
|
full_id = "#{@profile_id}/#{id}"
|
|
|
|
@rules[full_id] = nil
|
|
|
|
end
|
|
|
|
|
2015-06-19 22:00:53 +00:00
|
|
|
def __register_rule r
|
2015-06-19 22:20:44 +00:00
|
|
|
# get the full ID
|
|
|
|
full_id = VulcanoBaseRule::full_id(r, @profile_id)
|
|
|
|
if full_id.nil?
|
|
|
|
# TODO error
|
2015-06-19 22:00:53 +00:00
|
|
|
return
|
|
|
|
end
|
|
|
|
# add the rule to the registry
|
|
|
|
existing = @rules[full_id]
|
|
|
|
if existing.nil?
|
|
|
|
@rules[full_id] = r
|
|
|
|
else
|
|
|
|
VulcanoBaseRule::merge(existing, r)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-19 20:18:54 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2015-06-19 19:43:04 +00:00
|
|
|
module Vulcano::GlobalDSL
|
2015-06-19 19:52:57 +00:00
|
|
|
def __register_rule r
|
2015-06-19 19:43:04 +00:00
|
|
|
::Vulcano::DSL.execute_rule(r)
|
|
|
|
end
|
2015-06-19 22:20:44 +00:00
|
|
|
def __unregister_rule id
|
|
|
|
end
|
2015-06-19 19:43:04 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 18:11:26 +00:00
|
|
|
module Vulcano::DSLHelper
|
|
|
|
def self.bind_dsl(scope)
|
|
|
|
(class << scope; self; end).class_exec do
|
|
|
|
include Vulcano::DSL
|
2015-06-19 19:43:04 +00:00
|
|
|
include Vulcano::GlobalDSL
|
2015-06-18 15:32:40 +00:00
|
|
|
end
|
|
|
|
end
|
2015-06-19 18:11:26 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
::Vulcano::DSLHelper.bind_dsl(self)
|