2015-09-21 07:51:00 +00:00
|
|
|
# encoding: utf-8
|
2015-10-06 16:55:44 +00:00
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
2015-09-21 07:51:00 +00:00
|
|
|
|
2016-03-18 22:14:41 +00:00
|
|
|
require 'utils/object_traversal'
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
module Inspec::Resources
|
|
|
|
class JsonConfig < Inspec.resource(1)
|
|
|
|
name 'json'
|
|
|
|
desc 'Use the json InSpec audit resource to test data in a JSON file.'
|
|
|
|
example "
|
|
|
|
describe json('policyfile.lock.json') do
|
2016-04-16 11:47:41 +00:00
|
|
|
its(['cookbook_locks','omnibus','version']) { should eq('2.2.0') }
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2016-11-02 17:21:46 +00:00
|
|
|
|
2016-11-02 18:44:13 +00:00
|
|
|
describe json({ command: 'retrieve_data.py --json' }) do
|
2016-11-02 17:21:46 +00:00
|
|
|
its('state') { should eq('open') }
|
|
|
|
end
|
|
|
|
|
|
|
|
describe json({ content: '{\"item1\": { \"status\": \"available\" } }' }) do
|
|
|
|
its(['item1', 'status']) { should cmp 'available' }
|
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
"
|
2015-09-21 07:51:00 +00:00
|
|
|
|
2016-03-18 22:14:41 +00:00
|
|
|
include ObjectTraverser
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# make params readable
|
|
|
|
attr_reader :params
|
2015-09-21 07:51:00 +00:00
|
|
|
|
2016-11-02 20:04:59 +00:00
|
|
|
def initialize(opts)
|
|
|
|
@opts = opts
|
|
|
|
if opts.is_a?(Hash)
|
|
|
|
if opts.key?(:content)
|
|
|
|
@file_content = opts[:content]
|
|
|
|
elsif opts.key?(:command)
|
|
|
|
@command = inspec.command(opts[:command])
|
2016-11-02 16:25:26 +00:00
|
|
|
@file_content = @command.stdout
|
|
|
|
end
|
|
|
|
else
|
2016-11-02 20:04:59 +00:00
|
|
|
@path = opts
|
|
|
|
@file = inspec.file(@opts)
|
2016-11-02 16:25:26 +00:00
|
|
|
@file_content = @file.content
|
2015-11-24 12:10:16 +00:00
|
|
|
|
2016-11-02 16:25:26 +00:00
|
|
|
# check if file is available
|
|
|
|
if !@file.file?
|
2016-12-15 13:00:56 +00:00
|
|
|
skip_resource "Can't find file \"#{@path}\""
|
2016-11-02 16:25:26 +00:00
|
|
|
return @params = {}
|
|
|
|
end
|
2015-11-24 12:10:16 +00:00
|
|
|
|
2016-11-02 16:25:26 +00:00
|
|
|
# check if file is readable
|
2017-02-08 22:49:16 +00:00
|
|
|
if @file_content.nil? && !@file.empty?
|
2016-12-15 13:00:56 +00:00
|
|
|
skip_resource "Can't read file \"#{@path}\""
|
2016-11-02 16:25:26 +00:00
|
|
|
return @params = {}
|
|
|
|
end
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
@params = parse(@file_content)
|
2015-11-24 12:10:16 +00:00
|
|
|
end
|
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def parse(content)
|
|
|
|
require 'json'
|
|
|
|
JSON.parse(content)
|
|
|
|
end
|
2015-09-21 07:51:00 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def value(key)
|
|
|
|
extract_value(key, @params)
|
|
|
|
end
|
2015-09-21 07:51:00 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
# Shorthand to retrieve a parameter name via `#its`.
|
|
|
|
# Example: describe json('file') { its('paramX') { should eq 'Y' } }
|
|
|
|
#
|
|
|
|
# @param [String] name name of the field to retrieve
|
|
|
|
# @return [Object] the value stored at this position
|
|
|
|
def method_missing(*keys)
|
|
|
|
# catch bahavior of rspec its implementation
|
|
|
|
# @see https://github.com/rspec/rspec-its/blob/master/lib/rspec/its.rb#L110
|
|
|
|
keys.shift if keys.is_a?(Array) && keys[0] == :[]
|
|
|
|
value(keys)
|
|
|
|
end
|
2015-11-24 12:02:50 +00:00
|
|
|
|
2016-03-08 18:06:55 +00:00
|
|
|
def to_s
|
2016-11-02 20:04:59 +00:00
|
|
|
if @opts.is_a?(Hash) && @opts.key?(:content)
|
2016-11-02 17:21:46 +00:00
|
|
|
'Json content'
|
2016-11-02 16:25:26 +00:00
|
|
|
else
|
|
|
|
"Json #{@path}"
|
|
|
|
end
|
2016-03-08 18:06:55 +00:00
|
|
|
end
|
2015-09-21 07:51:00 +00:00
|
|
|
end
|
|
|
|
end
|