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
|
|
|
|
class DummyVulcanoRule < VulcanoBaseRule
|
2015-06-10 15:37:51 +00:00
|
|
|
include DummyServerspecTypes
|
|
|
|
include DummyVulcanoTypes
|
2015-06-07 19:41:54 +00:00
|
|
|
def method_missing(m, *a, &b)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class SpecFile
|
|
|
|
include DummyServerspecTypes
|
|
|
|
include DummyVulcanoTypes
|
|
|
|
Log = ::Vulcano::Log.new()
|
|
|
|
|
|
|
|
def initialize path
|
|
|
|
@path = path
|
|
|
|
@rules = []
|
|
|
|
@raw = File::read(path)
|
2015-06-10 15:03:12 +00:00
|
|
|
@invalid_calls = []
|
2015-06-19 14:45:36 +00:00
|
|
|
self.instance_eval(@raw, path, 1)
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def vulcano_meta
|
|
|
|
# helper methods (which we don't expose)
|
2015-06-16 21:20:24 +00:00
|
|
|
def rule2dict(rule)
|
2015-06-10 15:23:36 +00:00
|
|
|
d = nil
|
|
|
|
d = rule.desc.gsub(/\s*\n\s*/, ' ').strip unless rule.desc.nil?
|
2015-06-07 19:41:54 +00:00
|
|
|
{
|
|
|
|
"impact" => rule.impact,
|
|
|
|
"title" => rule.title,
|
2015-06-10 15:23:36 +00:00
|
|
|
"desc" => d
|
2015-06-07 19:41:54 +00:00
|
|
|
}
|
|
|
|
end
|
2015-06-16 21:20:24 +00:00
|
|
|
def rules2dict(rules)
|
|
|
|
res = {}
|
2015-06-07 20:24:53 +00:00
|
|
|
rules.map do |rule|
|
2015-06-16 21:20:24 +00:00
|
|
|
nu = rule2dict(rule)
|
|
|
|
if res[rule.id].nil?
|
|
|
|
res[rule.id] = nu
|
2015-06-07 20:24:53 +00:00
|
|
|
else
|
|
|
|
Log.error(
|
|
|
|
"Not redefining rule id #{rule.id}:\n"+
|
2015-06-16 21:20:24 +00:00
|
|
|
"-- #{res[rule.id]}\n"+
|
2015-06-07 20:24:53 +00:00
|
|
|
"++ #{nu}\n"
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2015-06-16 21:20:24 +00:00
|
|
|
res
|
2015-06-07 20:24:53 +00:00
|
|
|
end
|
2015-06-07 19:41:54 +00:00
|
|
|
def mOr(m, other)
|
|
|
|
(m.nil? || m[1].nil?) ? other : m[1]
|
|
|
|
end
|
|
|
|
header = @raw.sub(/^[^#].*\Z/m,'')
|
2015-06-07 20:24:53 +00:00
|
|
|
|
2015-06-07 19:41:54 +00:00
|
|
|
{
|
|
|
|
"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
|
|
|
|
|
|
|
|
def rule id, &block
|
|
|
|
@rules.push(DummyVulcanoRule.new(id, &block))
|
|
|
|
end
|
|
|
|
|
|
|
|
def method_missing sth, *args
|
|
|
|
Log.warn "spec file doesn't support: #{sth} #{args.join(', ')}"
|
2015-06-10 15:03:12 +00:00
|
|
|
@invalid_calls.push([sth, args])
|
2015-06-07 19:41:54 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
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 self.from_file path
|
|
|
|
if !File::file?(path)
|
|
|
|
Log.error "Can't find spec file in #{path}"
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
return SpecFile.new(path)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|