2015-06-07 19:41:54 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# copyright: 2015, Dominik Richter
|
|
|
|
# license: All rights reserved
|
|
|
|
require 'vulcano/base_rule'
|
|
|
|
require 'vulcano/log'
|
2015-06-19 14:45:36 +00:00
|
|
|
require 'verify/dummy'
|
2015-06-10 15:38:24 +00:00
|
|
|
# the user may use dynamic evaluations via pry
|
|
|
|
begin
|
|
|
|
require 'pry'
|
|
|
|
rescue LoadError
|
|
|
|
end
|
2015-06-07 19:41:54 +00:00
|
|
|
|
|
|
|
module Vulcano
|
2015-06-19 23:36:27 +00:00
|
|
|
class DummyContext
|
|
|
|
include Vulcano::DSL
|
|
|
|
|
|
|
|
def initialize path, profile_id
|
|
|
|
@path = path
|
|
|
|
@profile_id = profile_id
|
|
|
|
@rules = {}
|
|
|
|
@errors = []
|
|
|
|
end
|
|
|
|
|
2015-06-21 20:36:38 +00:00
|
|
|
def only_if *a, &b
|
|
|
|
# always ignore
|
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
# DSL methods
|
|
|
|
def __register_rule r
|
|
|
|
fid = VulcanoBaseRule.full_id r, @profile_id
|
|
|
|
if @rules[fid].nil?
|
|
|
|
@rules[fid] = r
|
|
|
|
else
|
|
|
|
@errors.push "Duplicate definition of rule #{fid}."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def __unregister_rule id
|
|
|
|
fid = "#{@profile_id}/#{id}"
|
|
|
|
if @rules.key? fid
|
|
|
|
@rules.delete(fid)
|
|
|
|
else
|
|
|
|
@errors.push "Failed to skip rule #{fid}, it isn't defined."
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# redefine require to find files in the context
|
|
|
|
# of this profile
|
|
|
|
def require sth
|
|
|
|
# ignore vulcano includes, we already have those
|
|
|
|
lib = File::expand_path( File.join @path, '..', '..', 'lib', "#{sth}.rb" )
|
|
|
|
if File::file? lib
|
|
|
|
require_relative lib
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing sth, *args
|
|
|
|
@errors.push "Don't understand method #{sth} ( #{args} )."
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
2015-06-19 23:36:27 +00:00
|
|
|
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
2015-06-19 23:36:27 +00:00
|
|
|
end
|
|
|
|
|
2015-06-07 19:41:54 +00:00
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
module Vulcano
|
2015-06-07 19:41:54 +00:00
|
|
|
class SpecFile
|
|
|
|
Log = ::Vulcano::Log.new()
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
attr_reader :errors, :rules
|
|
|
|
def initialize path, metadata
|
2015-06-07 19:41:54 +00:00
|
|
|
@rules = []
|
|
|
|
@raw = File::read(path)
|
2015-06-19 23:36:27 +00:00
|
|
|
@profile_id = metadata.dict['name']
|
2015-06-10 15:03:12 +00:00
|
|
|
@invalid_calls = []
|
2015-06-19 23:36:27 +00:00
|
|
|
|
|
|
|
ctx = DummyContext.new(path, @profile_id)
|
|
|
|
ctx.instance_eval(@raw, path, 1)
|
|
|
|
@errors = ctx.instance_variable_get(:@errors)
|
|
|
|
@rules = ctx.instance_variable_get(:@rules)
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
def metadata
|
2015-06-07 19:41:54 +00:00
|
|
|
header = @raw.sub(/^[^#].*\Z/m,'')
|
|
|
|
{
|
|
|
|
"title" => mOr(header.match(/^# title: (.*)$/), 'untitled'),
|
|
|
|
"copyright" => mOr(header.match(/^# copyright: (.*)$/), 'All rights reserved'),
|
2015-06-16 21:20:24 +00:00
|
|
|
"rules" => rules2dict(@rules)
|
2015-06-07 19:41:54 +00:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
def self.from_file path, metadata
|
|
|
|
if !File::file?(path)
|
|
|
|
Log.error "Can't find spec file in #{path}"
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return SpecFile.new(path, metadata)
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def rule2dict(rule)
|
|
|
|
d = nil
|
|
|
|
d = rule.desc.gsub(/\s*\n\s*/, ' ').strip unless rule.desc.nil?
|
|
|
|
{
|
|
|
|
"impact" => rule.impact,
|
|
|
|
"title" => rule.title,
|
|
|
|
"desc" => d
|
|
|
|
}
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
def rules2dict(rules)
|
|
|
|
res = {}
|
|
|
|
rules.map do |id, rule|
|
|
|
|
nu = rule2dict(rule)
|
|
|
|
if res[id].nil?
|
|
|
|
res[id] = nu
|
|
|
|
else
|
|
|
|
Log.error(
|
|
|
|
"Not redefining rule id #{id}:\n"+
|
|
|
|
"-- #{res[id]}\n"+
|
|
|
|
"++ #{nu}\n"
|
|
|
|
)
|
|
|
|
end
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
2015-06-19 23:36:27 +00:00
|
|
|
res
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
2015-06-19 23:36:27 +00:00
|
|
|
def mOr(m, other)
|
|
|
|
(m.nil? || m[1].nil?) ? other : m[1]
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
2015-06-22 14:18:40 +00:00
|
|
|
|
|
|
|
def os *a, &b
|
|
|
|
{}
|
|
|
|
end
|