|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'yaml' |
| 4 | +require 'json' |
| 5 | +require 'deep_merge' |
| 6 | + |
| 7 | +require 'scelint/version' |
| 8 | + |
| 9 | +module Scelint |
| 10 | + class Error < StandardError; end |
| 11 | + # Your code goes here... |
| 12 | + |
| 13 | + class Lint |
| 14 | + def initialize(paths = ['.']) |
| 15 | + @data = {} |
| 16 | + @errors = [] |
| 17 | + @warnings = [] |
| 18 | + |
| 19 | + merged_data = {} |
| 20 | + |
| 21 | + paths.each do |path| |
| 22 | + if File.directory?(path) |
| 23 | + [ |
| 24 | + 'SIMP/compliance_profiles', |
| 25 | + 'simp/compliance_profiles', |
| 26 | + ].each do |dir| |
| 27 | + ['yaml', 'json'].each do |type| |
| 28 | + Dir.glob("#{path}/#{dir}/**/*.#{type}").each do |file| |
| 29 | + @data[file] = parse(file) |
| 30 | + merged_data = merged_data.deep_merge!(@data[file]) |
| 31 | + end |
| 32 | + end |
| 33 | + end |
| 34 | + elsif File.exist?(path) |
| 35 | + @data[path] = parse(path) |
| 36 | + else |
| 37 | + raise "Can't find path '#{path}'" |
| 38 | + end |
| 39 | + end |
| 40 | + |
| 41 | + return nil if @data.empty? |
| 42 | + |
| 43 | + @data['merged data'] = merged_data |
| 44 | + |
| 45 | + @data.each do |file, data| |
| 46 | + lint(file, data) |
| 47 | + end |
| 48 | + |
| 49 | + @data |
| 50 | + end |
| 51 | + |
| 52 | + def parse(file) |
| 53 | + return @data[file] if @data[file] |
| 54 | + |
| 55 | + type = case file |
| 56 | + when %r{\.yaml$} |
| 57 | + 'yaml' |
| 58 | + when %r{\.json$} |
| 59 | + 'json' |
| 60 | + else |
| 61 | + nil |
| 62 | + end |
| 63 | + return YAML.safe_load(File.read(file)) if type == 'yaml' |
| 64 | + return JSON.parse(File.read(file)) if type == 'json' |
| 65 | + |
| 66 | + raise "Failed to determine file type of '#{file}'" |
| 67 | + end |
| 68 | + |
| 69 | + def files |
| 70 | + @data.keys - ['merged data'] |
| 71 | + end |
| 72 | + |
| 73 | + def warnings |
| 74 | + @warnings |
| 75 | + end |
| 76 | + |
| 77 | + def errors |
| 78 | + @errors |
| 79 | + end |
| 80 | + |
| 81 | + def check_version(file, data) |
| 82 | + @errors << "Version check failed in #{file}." unless data['version'] == '2.0.0' |
| 83 | + end |
| 84 | + |
| 85 | + def check_keys(file, data) |
| 86 | + ok = ['version', 'profiles', 'ce', 'checks'] |
| 87 | + |
| 88 | + data.keys.each do |key| |
| 89 | + @warnings << "Unexpected key '#{key}' found in #{file}." unless ok.include?(key) |
| 90 | + end |
| 91 | + end |
| 92 | + |
| 93 | + def check_title(file, data) |
| 94 | + @warnings << "Bad title #{data} in #{file}." unless data.is_a?(String) |
| 95 | + end |
| 96 | + |
| 97 | + def check_description(file, data) |
| 98 | + @warnings << "Bad description #{data} in #{file}." unless data.is_a?(String) |
| 99 | + end |
| 100 | + |
| 101 | + def check_controls(file, data) |
| 102 | + @warnings << "Bad controls #{data} in #{file}." unless data.is_a?(Hash) |
| 103 | + |
| 104 | + data.each do |key, value| |
| 105 | + @warnings << "Bad control #{key} in #{file}." unless key.is_a?(String) && value # Should be truthy |
| 106 | + end |
| 107 | + end |
| 108 | + |
| 109 | + def check_profile_ces(file, data) |
| 110 | + @warnings << "Bad ces #{data} in #{file}." unless data.is_a?(Hash) |
| 111 | + |
| 112 | + data.each do |key, value| |
| 113 | + @warnings << "Bad ce #{key} in #{file}." unless key.is_a?(String) && value.is_a?(TrueClass) |
| 114 | + end |
| 115 | + end |
| 116 | + |
| 117 | + def check_confine(file, data) |
| 118 | + @warnings << "Bad confine #{data} in #{file}." unless data.is_a?(Hash) |
| 119 | + end |
| 120 | + |
| 121 | + def check_identifiers(file, data) |
| 122 | + @warnings << "Bad identifiers #{data} in #{file}." unless data.is_a?(Hash) |
| 123 | + |
| 124 | + data.each do |key, value| |
| 125 | + @warnings << "Bad identifier #{key} in #{file}." unless key.is_a?(String) && value.is_a?(Array) |
| 126 | + value.each do |identifier| |
| 127 | + @warnings << "Bad identifier #{identifier} in #{file}." unless identifier.is_a?(String) |
| 128 | + end |
| 129 | + end |
| 130 | + end |
| 131 | + |
| 132 | + def check_oval_ids(file, data) |
| 133 | + @warnings << "Bad oval-ids #{data} in #{file}." unless data.is_a?(Array) |
| 134 | + |
| 135 | + data.each do |key| |
| 136 | + @warnings << "Bad oval-id #{key} in #{file}." unless key.is_a?(String) |
| 137 | + end |
| 138 | + end |
| 139 | + |
| 140 | + def check_imported_data(file, data) |
| 141 | + ok = ['checktext', 'fixtext'] |
| 142 | + |
| 143 | + data.each do |key, value| |
| 144 | + @warnings << "Unexpected key '#{key}' found in #{file}" unless ok.include?(key) |
| 145 | + |
| 146 | + @warnings << "Bad #{key} data in #{file}: '#{value}'" unless value.is_a?(String) |
| 147 | + end |
| 148 | + end |
| 149 | + |
| 150 | + def check_profiles(file, data) |
| 151 | + ok = [ |
| 152 | + 'title', |
| 153 | + 'description', |
| 154 | + 'controls', |
| 155 | + 'ces', |
| 156 | + 'confine', |
| 157 | + ] |
| 158 | + |
| 159 | + data.each do |profile, value| |
| 160 | + value.keys.each do |key| |
| 161 | + @warnings << "Unexpected key '#{key}' found in #{file} (profile '#{profile}')." unless ok.include?(key) |
| 162 | + end |
| 163 | + |
| 164 | + check_title(file, value['title']) unless value['title'].nil? |
| 165 | + check_description(file, value['description']) unless value['description'].nil? |
| 166 | + check_controls(file, value['controls']) unless value['controls'].nil? |
| 167 | + check_profile_ces(file, value['ces']) unless value['ces'].nil? |
| 168 | + check_confine(file, value['confine']) unless value['confine'].nil? |
| 169 | + end |
| 170 | + end |
| 171 | + |
| 172 | + def check_ce(file, data) |
| 173 | + ok = [ |
| 174 | + 'title', |
| 175 | + 'description', |
| 176 | + 'controls', |
| 177 | + 'identifiers', |
| 178 | + 'oval-ids', |
| 179 | + 'confine', |
| 180 | + 'imported_data', |
| 181 | + ] |
| 182 | + |
| 183 | + data.each do |ce, value| |
| 184 | + value.keys.each do |key| |
| 185 | + @warnings << "Unexpected key '#{key}' found in #{file} (CE '#{ce}')." unless ok.include?(key) |
| 186 | + end |
| 187 | + |
| 188 | + check_title(file, value['title']) unless value['title'].nil? |
| 189 | + check_description(file, value['description']) unless value['description'].nil? |
| 190 | + check_controls(file, value['controls']) unless value['controls'].nil? |
| 191 | + check_identifiers(file, value['identifiers']) unless value['identifiers'].nil? |
| 192 | + check_oval_ids(file, value['oval-ids']) unless value['oval-ids'].nil? |
| 193 | + check_confine(file, value['confine']) unless value['confine'].nil? |
| 194 | + check_imported_data(file, value['imported_data']) unless value['imported_data'].nil? |
| 195 | + end |
| 196 | + end |
| 197 | + |
| 198 | + def check_type(file, check, data) |
| 199 | + @warnings << "Unknown type '#{data}' found in #{file} (check '#{check}')." unless data == 'puppet-class-parameter' |
| 200 | + end |
| 201 | + |
| 202 | + def check_settings(file, check, data) |
| 203 | + ok = ['parameter', 'value'] |
| 204 | + |
| 205 | + data.keys.each do |key| |
| 206 | + @warnings << "Unexpected key '#{key}' found in #{file} (check '#{check}')." unless ok.include?(key) |
| 207 | + end |
| 208 | + end |
| 209 | + |
| 210 | + def check_check_ces(file, data) |
| 211 | + @warnings << "Bad ces #{data} in #{file}." unless data.is_a?(Array) |
| 212 | + |
| 213 | + data.each do |key| |
| 214 | + @warnings << "Bad ce #{key} in #{file}." unless key.is_a?(String) |
| 215 | + end |
| 216 | + end |
| 217 | + |
| 218 | + def check_checks(file, data) |
| 219 | + ok = [ |
| 220 | + 'type', |
| 221 | + 'settings', |
| 222 | + 'controls', |
| 223 | + 'identifiers', |
| 224 | + 'oval-ids', |
| 225 | + 'ces', |
| 226 | + 'confine', |
| 227 | + ] |
| 228 | + |
| 229 | + data.each do |check, value| |
| 230 | + value.keys.each do |key| |
| 231 | + @warnings << "Unexpected key '#{key}' found in #{file} (check '#{check}')." unless ok.include?(key) |
| 232 | + end |
| 233 | + |
| 234 | + check_type(file, check, value['type']) if value['type'] |
| 235 | + check_settings(file, check, value['settings']) if value['settings'] |
| 236 | + check_controls(file, value['controls']) unless value['controls'].nil? |
| 237 | + check_identifiers(file, value['identifiers']) unless value['identifiers'].nil? |
| 238 | + check_oval_ids(file, value['oval-ids']) unless value['oval-ids'].nil? |
| 239 | + check_check_ces(file, value['ces']) unless value['ces'].nil? |
| 240 | + check_confine(file, value['confine']) unless value['confine'].nil? |
| 241 | + end |
| 242 | + end |
| 243 | + |
| 244 | + def lint(file, data) |
| 245 | + check_version(file, data) |
| 246 | + check_keys(file, data) |
| 247 | + |
| 248 | + check_profiles(file, data['profiles']) if data['profiles'] |
| 249 | + check_ce(file, data['ce']) if data['ce'] |
| 250 | + check_checks(file, data['checks']) if data['checks'] |
| 251 | + end |
| 252 | + end |
| 253 | +end |
0 commit comments