-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_config.rb
116 lines (101 loc) · 4.11 KB
/
check_config.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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
# frozen_string_literal: true
##
# ErrataParser config schema
#
ERRATAPARSER_ALIASES_SCHEMA = { type: Hash, mandatory: false, child: {
'releases' => { type: Hash, mandatory: false, child: {
type: Array, mandatory: true, child: { type: String, mandatory: true }
} }
} }.freeze
ERRATAPARSER_WHITELIST_SCHEMA = { type: Hash, mandatory: false, child: {
'releases' => { type: Array, mandatory: false, child: {
type: String, mandatory: true
} },
'components' => { type: Array, mandatory: false, child: {
type: String, mandatory: true
} },
'architectures' => { type: Array, mandatory: false, child: {
type: String, mandatory: true
} }
} }.freeze
ERRATAPARSER_REPOSITORY_SCHEMA = { type: Hash, mandatory: true, child: {
'repo_url' => { type: String, mandatory: true },
'credentials' => { type: Hash, mandatory: false, child: {
'user' => { type: String, mandatory: true },
'pass' => { type: String, mandatory: true }
} },
'releases' => { type: Array, mandatory: true, child: {
type: String, mandatory: true
} }
} }.freeze
ERRATAPARSER_CONFIG_SCHEMA = {
'tempdir' => { type: String, mandatory: false },
'debian' => { type: Hash, mandatory: false, child: {
'dsa_list_url' => { type: String, mandatory: true },
'dla_list_url' => { type: String, mandatory: false },
'cve_list_url' => { type: String, mandatory: true },
'repositories' => {
type: Array, mandatory: true, child: ERRATAPARSER_REPOSITORY_SCHEMA
},
'aliases' => ERRATAPARSER_ALIASES_SCHEMA,
'whitelists' => ERRATAPARSER_WHITELIST_SCHEMA,
'special-kernel-pkg-collection' => { type: Array, mandatory: false, child: {
type: String, mandatory: true
} }
} },
'ubuntu' => { type: Hash, mandatory: false, child: {
'usn_list_url' => { type: String, mandatory: true },
'repository' => ERRATAPARSER_REPOSITORY_SCHEMA,
'aliases' => ERRATAPARSER_ALIASES_SCHEMA,
'whitelists' => ERRATAPARSER_WHITELIST_SCHEMA
} },
'ubuntu-esm' => { type: Hash, mandatory: false, child: {
'usn_list_url' => { type: String, mandatory: true },
'repositories' => { type: Array, mandatory: true, child: ERRATAPARSER_REPOSITORY_SCHEMA },
'aliases' => ERRATAPARSER_ALIASES_SCHEMA,
'whitelists' => ERRATAPARSER_WHITELIST_SCHEMA
} }
}.freeze
# takes config (cfg) as a ruby-hash and a schema-hash like above.
# problems will be reported by throwing exceptions
def check_config_hash(cfg, schema, path='')
if schema.key?(:type)
# Hash with not custom keys of fixed schema
cfg.each do |k, v|
local_path = "#{path}.#{k}"
raise "#{local_path}: has type #{v.class.name} but should be #{schema[:type]}" if v.class != schema[:type]
check_config_child(v, schema, local_path) if schema.key?(:child)
end
else
keylist = cfg.keys
# iterate all possible entries
schema.each do |k, v|
local_path = "#{path}.#{k}"
raise "#{path}: Mandatory key #{k.inspect} not found in config" if v[:mandatory] && !cfg.key?(k)
next unless cfg.key? k
raise "#{local_path}: has type #{cfg[k].class.name} but should be #{v[:type]}" if cfg[k].class != v[:type]
check_config_child(cfg[k], v, local_path) if v.key?(:child)
keylist.delete(k)
end
raise "Found unknown keys (not defined in schema) at #{path.inspect}: #{keylist.join(', ')}" unless keylist.empty?
end
end
def check_config_array(cfg, schema, path='')
child_type = schema[:type]
raise "#{path}: List is not allowed to be empty" if schema[:mandatory] && cfg.empty?
cfg.each_index do |i|
raise "#{path}: Item##{i} has type #{cfg[i].class.name} but should be #{child_type}" if cfg[i].class != child_type
check_config_child(cfg[i], schema, path) if schema.key?(:child)
end
end
def check_config_child(cfg, schema, path='')
# recursively check Hashes
check_config_hash(cfg, schema[:child], path) if schema[:type] == Hash
# check Array elements
check_config_array(cfg, schema[:child], path) if schema[:type] == Array
end
if $PROGRAM_NAME == __FILE__
require 'json'
config_path = ARGV[0] || 'default_config.json'
check_config_hash(JSON.parse(File.read(config_path)), ERRATAPARSER_CONFIG_SCHEMA)
end