Class: OCTool::Parser
- Inherits:
-
Object
- Object
- OCTool::Parser
- Defined in:
- lib/octool/parser.rb
Overview
Logic to wrap the kwalify parser.
Class Method Summary collapse
-
.validate_schemas ⇒ Object
Class method: check that schemas are valid.
Instance Method Summary collapse
- #die(message = nil) ⇒ Object
- #include_data(path, type) ⇒ Object
-
#initialize(path) ⇒ Parser
constructor
A new instance of Parser.
- #kwalifyer(type) ⇒ Object
- #parsed_certification(cert) ⇒ Object
- #parsed_component(component) ⇒ Object
- #parsed_standard(standard) ⇒ Object
- #schema_dir ⇒ Object
- #schema_version ⇒ Object
-
#validate_data ⇒ Object
(also: #load_system)
Validate and load data in one pass.
- #validate_file(path, type) ⇒ Object (also: #load_file)
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.(path)} not readable" unless File.readable?(path) end |
Class Method Details
.validate_schemas ⇒ Object
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 = Kwalify::MetaValidator.instance kwalify = Kwalify::Yaml::Parser.new() 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( = nil) puts '[FAIL] ' + if 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_dir ⇒ Object
61 62 63 |
# File 'lib/octool/parser.rb', line 61 def schema_dir @schema_dir ||= File.join(BASE_SCHEMA_DIR, schema_version) end |
#schema_version ⇒ Object
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_data ⇒ Object 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. end |