Class: OCTool::Parser

Inherits:
Object
  • Object
show all
Defined in:
lib/octool/parser.rb

Overview

Logic to wrap the kwalify parser.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Parser

Returns a new instance of Parser.



24
25
26
27
# File 'lib/octool/parser.rb', line 24

def initialize(path)
    @config_file = path
    die "#{File.expand_path(path)} not readable" unless File.readable?(path)
end

Class Method Details

.validate_schemasObject

Class method: check that schemas are valid.



30
31
32
33
34
35
36
# File 'lib/octool/parser.rb', line 30

def self.validate_schemas
    metavalidator = Kwalify::MetaValidator.instance
    kwalify = Kwalify::Yaml::Parser.new(metavalidator)
    Dir.glob("#{BASE_SCHEMA_DIR}/**/*.yaml").each do |schema|
        kwalify.parse_file(schema)
    end
end

Instance Method Details

#die(message = nil) ⇒ Object



38
39
40
41
# File 'lib/octool/parser.rb', line 38

def die(message = nil)
    puts '[FAIL] ' + message if message
    exit(1)
end

#include_data(path, type) ⇒ Object



84
85
86
87
88
# File 'lib/octool/parser.rb', line 84

def include_data(path, type)
    data = validate_file(path, type)
    data['type'] = type
    method("parsed_#{type}".to_sym).call(data)
end

#kwalifyer(type) ⇒ Object



54
55
56
57
58
59
# File 'lib/octool/parser.rb', line 54

def kwalifyer(type)
    schema_file = File.join(schema_dir, "#{type}.yaml")
    schema = Kwalify::Yaml.load_file(schema_file)
    validator = Kwalify::Validator.new(schema)
    Kwalify::Yaml::Parser.new(validator) { |p| p.data_binding = true }
end

#parsed_certification(cert) ⇒ Object



113
114
115
116
# File 'lib/octool/parser.rb', line 113

def parsed_certification(cert)
    cert['requires'].map! { |r| r['certification_key'] = cert['certification_key']; r }
    cert
end

#parsed_component(component) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/octool/parser.rb', line 90

def parsed_component(component)
    component['attestations'].map! do |a|
        # Add a "component_key" field to each attestation.
        a['component_key'] = component['component_key']
        a['satisfies'].map! do |s|
            # Add "attestation_key" to each control satisfied by this attestation.
            s['attestation_key'] = a['summary']
            # Add "component_key" to each control satisfied by this attestation.
            s['component_key'] = component['component_key']
            s
        end
        a
    end
    component
end

#parsed_standard(standard) ⇒ Object



106
107
108
109
110
111
# File 'lib/octool/parser.rb', line 106

def parsed_standard(standard)
    # Add 'standard_key' to each control family and to each control.
    standard['families'].map! { |f| f['standard_key'] = standard['standard_key']; f }
    standard['controls'].map! { |c| c['standard_key'] = standard['standard_key']; c }
    standard
end

#schema_dirObject



61
62
63
# File 'lib/octool/parser.rb', line 61

def schema_dir
    @schema_dir ||= File.join(BASE_SCHEMA_DIR, schema_version)
end

#schema_versionObject



65
66
67
68
69
70
# File 'lib/octool/parser.rb', line 65

def schema_version
    @schema_version ||= Kwalify::Yaml.load_file(@config_file)['schema_version']
rescue StandarError
    warn '[FAIL] Unable to read schema_version'
    exit(1)
end

#validate_dataObject Also known as: load_system

Validate and load data in one pass.



73
74
75
76
77
78
79
80
81
82
# File 'lib/octool/parser.rb', line 73

def validate_data
    base_dir = File.dirname(@config_file)
    config = validate_file(@config_file, 'config')
    sys = System.new(config)
    config['includes'].each do |inc|
        path = File.join(base_dir, inc['path'])
        sys.data << include_data(path, inc['type'])
    end
    sys
end

#validate_file(path, type) ⇒ Object Also known as: load_file



43
44
45
46
47
48
49
50
51
52
# File 'lib/octool/parser.rb', line 43

def validate_file(path, type)
    kwalify = kwalifyer(type)
    data = kwalify.parse_file(path)
    errors = kwalify.errors
    raise ValidationError.new(path, errors) unless errors.empty?

    data
rescue SystemCallError, Kwalify::SyntaxError, ValidationError => e
    die e.message
end