Class: YamlBenchmark

Inherits:
AuditBenchmark show all
Defined in:
lib/audit/lib/benchmark/yaml_benchmark.rb

Constant Summary collapse

CHECK_FILE_EXTENSION =
".check"
GROUP_FILE_EXTENSION =
".group"
GROUP_ID =
"ID"
GROUP_NAME =
"Name"
GROUP_DESCRIPTION =
"Description"
GROUP_CHILDREN =
"Children"
CHECK_ID =
"ID"
CHECK_SCRIPT =
"Script"
CHECK_NAME =
"Name"
CHECK_DEPENDENCIES =
"Depends"
CHECK_DESCRIPTION =
"Description"
CHECK_DURATION =
"Duration"
BENCHMARK_ID =
"BENCHMARK"

Instance Attribute Summary collapse

Attributes inherited from AuditBenchmark

#item_repository

Instance Method Summary collapse

Methods inherited from AuditBenchmark

#automatic_dependencies, #dependencies, #duration, #element, #execution_order, #rules, #to_hash

Constructor Details

#initialize(options) ⇒ YamlBenchmark

Returns a new instance of YamlBenchmark.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/audit/lib/benchmark/yaml_benchmark.rb', line 32

def initialize(options)
  raise "Need option :benchmark" unless options[:benchmark]
  
  if options[:logger]
    @logger = options[:logger]
  else
    @logger = Logger.new(STDOUT)
  end
  
  if !File.exist?(options[:benchmark]) then
    @logger.error("Specified benchmark file '#{options[:benchmark]}' does not exist") 
    raise "Specified benchmark file '#{options[:benchmark]}' does not exist"
  end
  
  @logger.info("Loading benchmark '#{options[:benchmark]}'")
  
  @item_repository = {}
  @benchmark_file = options[:benchmark]
  group_hashes = []
  check_hashes = []
  Zip::ZipFile.open(@benchmark_file, Zip::ZipFile::CREATE) do|zipfile|
    zipfile.each do|file|
      if file.name =~ /\.group$/ then
        @logger.debug {"Loading group '#{file.name}'"}
        
        hash = YAML::load( file.get_input_stream )
        raise "The group file '#{group_file}' does not contain the group's ID" unless hash[GROUP_ID]
        raise "The group file '#{group_file}' does not contain any children" unless hash[GROUP_CHILDREN]  

        @item_repository[hash[CHECK_ID]] = Group.new(hash[GROUP_ID], 
                                                   hash[GROUP_NAME], 
                                                   hash[GROUP_DESCRIPTION])
        group_hashes << hash # keep group hash for later, because we still need to add children to the group
      elsif file.name =~ /\.check$/ then
        @logger.debug {"Loading check '#{file.name}'"}
      
        hash = YAML::load( file.get_input_stream )
        raise "The check file '#{file.name}' does not contain the check's ID" unless hash[CHECK_ID]
        raise "The check file '#{file.name}' does not contain a check script" unless hash[CHECK_SCRIPT]

        @item_repository[hash[CHECK_ID]] = Check.new(hash[CHECK_ID], 
                                                   hash[CHECK_SCRIPT], 
                                                   hash[CHECK_NAME], 
                                                   [], 
                                                   hash[CHECK_DESCRIPTION],
                                                    hash[CHECK_DURATION])
        check_hashes << hash
      else
        @logger.info {"Ignoring unknown file '#{file.name}' in benchmark '#{@benchmark_file}'"}
        # unknown file, ignore
      end
    end
  end
  
  group_hashes.each do|group_hash|
    group = @item_repository[group_hash[GROUP_ID]]
    group_hash[GROUP_CHILDREN]. each do|child|
      raise ItemNotFoundException.new(child), "Item '#{child}' from group '#{group.id}' not found" unless @item_repository[child]
      item = @item_repository[child]
      raise BadItemClassErooxception, "Item '#{child}' from group '#{group.id}' has wrong item class '#{item.class.name}'" unless item.class == Check || item.class == Group
      
      group.children << item
    end
  end
  
  # set dependencies
  check_hashes.each do|check_hash|
    check = @item_repository[check_hash[CHECK_ID]]
    if check_hash[CHECK_DEPENDENCIES] then
      check_hash[CHECK_DEPENDENCIES].each do|dep|
        raise ItemNotFoundException.new(dep), "Item '#{dep}' which is depended on by check '#{check.id}' not found" unless @item_repository[dep]
        check.dependencies << @item_repository[dep]
      end
    end
  end
  
  benchmark = @item_repository[BENCHMARK_ID] or raise ItemNotFoundException.new("benchmark.group"), "Benchmark file benchmark.group not found in benchmark zipfile '#{benchmark_file}'"
  raise BadItemClassException, "Benchmark has wrong item class #{benchmark.class.name}, expected is Group" unless benchmark.class == Group
  @item_repository.delete(BENCHMARK_ID) #The benchmark group is not really an item, so remove it from the item set

  @id = benchmark.id
  @name = benchmark.name
  @description = benchmark.description
   @children = benchmark.children
   auto_deps = AutomaticDependencies.new(automatic_dependencies())
   @item_repository[auto_deps.id] = auto_deps
  @children = [auto_deps] + benchmark.children

end

Instance Attribute Details

#childrenObject (readonly)

Returns the value of attribute children.



30
31
32
# File 'lib/audit/lib/benchmark/yaml_benchmark.rb', line 30

def children
  @children
end

#descriptionObject (readonly)

Returns the value of attribute description.



29
30
31
# File 'lib/audit/lib/benchmark/yaml_benchmark.rb', line 29

def description
  @description
end

#idObject (readonly)

Returns the value of attribute id.



27
28
29
# File 'lib/audit/lib/benchmark/yaml_benchmark.rb', line 27

def id
  @id
end

#nameObject (readonly)

Returns the value of attribute name.



28
29
30
# File 'lib/audit/lib/benchmark/yaml_benchmark.rb', line 28

def name
  @name
end

Instance Method Details

#element_impl(name) ⇒ Object

get a raw benchmark element by name. This is used by other classes like script generators to retrieve information from the benchmark that was stored there for them.



124
125
126
# File 'lib/audit/lib/benchmark/yaml_benchmark.rb', line 124

def element_impl(name)
  return Zip::ZipFile.open(@benchmark_file, Zip::ZipFile::CREATE) {|zipfile| zipfile.read(name)}
end

#get_item(id) ⇒ Object

Raises:



128
129
130
131
# File 'lib/audit/lib/benchmark/yaml_benchmark.rb', line 128

def get_item(id)
  raise ItemNotFoundException.new(id), "Item #{id} not found" if @item_repository[id].nil?
  return @item_repository[id]
end