Skip to content

Commit 1676053

Browse files
committed
Validate Hiera values
* Add methods for retrieving profile names and confines * Compile data the way that compliance_markup does and validate the results * Handle a few error cases Fixes simp#14
1 parent 9623f62 commit 1676053

File tree

2 files changed

+47
-5
lines changed

2 files changed

+47
-5
lines changed

Gemfile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,9 @@ group :tests do
1212
gem 'rubocop-rspec', '~> 2.13'
1313
gem 'rubocop-rake', '~> 0.6.0'
1414
end
15+
16+
group :development do
17+
gem 'pry', '~> 0.14.1'
18+
gem 'pry-byebug', '~> 3.10'
19+
gem 'rdoc', '~> 6.4'
20+
end

lib/scelint.rb

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,16 @@ def initialize(paths = ['.'])
3232
].each do |dir|
3333
['yaml', 'json'].each do |type|
3434
Dir.glob("#{path}/#{dir}/**/*.#{type}").each do |file|
35-
@data[file] = parse(file)
35+
this_file = parse(file)
36+
next if this_file.nil?
37+
@data[file] = this_file
3638
merged_data = merged_data.deep_merge!(@data[file])
3739
end
3840
end
3941
end
4042
elsif File.exist?(path)
41-
@data[path] = parse(path)
43+
this_file = parse(path)
44+
@data[path] = this_file unless this_file.nil?
4245
else
4346
raise "Can't find path '#{path}'"
4447
end
@@ -52,6 +55,8 @@ def initialize(paths = ['.'])
5255
lint(file, data)
5356
end
5457

58+
validate
59+
5560
@data # rubocop:disable Lint/Void
5661
end
5762

@@ -65,7 +70,7 @@ def parse(file)
6570
'json'
6671
else
6772
@errors << "#{file}: Failed to determine file type"
68-
nil
73+
return nil
6974
end
7075
begin
7176
return YAML.safe_load(File.read(file)) if type == 'yaml'
@@ -74,7 +79,7 @@ def parse(file)
7479
@errors << "#{file}: Failed to parse file: #{e.message}"
7580
end
7681

77-
{}
82+
nil
7883
end
7984

8085
def files
@@ -252,7 +257,6 @@ def check_remediation(file, check, remediation_section)
252257

253258
if remediation_section.is_a?(Hash)
254259
remediation_section.each do |section, value|
255-
# require 'pry-byebug'; binding.pry if section == 'disabled'
256260
case section
257261
when 'scan-false-positive', 'disabled'
258262
value.each do |reason|
@@ -368,6 +372,36 @@ def check_checks(file, data)
368372
end
369373
end
370374

375+
def profiles
376+
return nil unless @data.key?('merged data')
377+
return nil unless @data['merged data']['profiles'].is_a?(Hash)
378+
return @data['merged data']['profiles'].keys
379+
end
380+
381+
def confines
382+
retval = {}
383+
384+
@data.each do |key, value|
385+
next if key == 'merged data'
386+
next unless value.is_a?(Hash)
387+
388+
['profiles', 'ce', 'checks'].each do |type|
389+
next unless value.key?(type)
390+
next unless value[type].is_a?(Hash)
391+
392+
value[type].each do |_k, v|
393+
next unless v.is_a?(Hash)
394+
retval = retval.merge(v['confine']) if v.key?('confine')
395+
end
396+
end
397+
end
398+
399+
retval
400+
end
401+
402+
def validate
403+
end
404+
371405
def lint(file, data)
372406
check_version(file, data)
373407
check_keys(file, data)
@@ -376,6 +410,8 @@ def lint(file, data)
376410
check_ce(file, data['ce']) if data['ce']
377411
check_checks(file, data['checks']) if data['checks']
378412
check_controls(file, data['controls']) if data['controls']
413+
rescue => e
414+
@errors << "#{file}: #{e.message} (not a hash?)"
379415
end
380416
end
381417
end

0 commit comments

Comments
 (0)