2016-02-05 07:38:45 +00:00
|
|
|
# encoding: utf-8
|
|
|
|
# author: Christoph Hartmann
|
|
|
|
# author: Dominik Richter
|
|
|
|
|
|
|
|
module Compliance
|
|
|
|
# stores configuration on local filesystem
|
|
|
|
class Configuration
|
|
|
|
def initialize
|
|
|
|
@config_path = File.join(Dir.home, '.inspec', 'compliance')
|
|
|
|
# ensure the directory is available
|
|
|
|
unless File.directory?(@config_path)
|
|
|
|
FileUtils.mkdir_p(@config_path)
|
|
|
|
end
|
|
|
|
# set config file path
|
|
|
|
@config_file = File.join(@config_path, '/config.json')
|
|
|
|
@config = {}
|
|
|
|
|
|
|
|
# load the data
|
|
|
|
get
|
|
|
|
end
|
|
|
|
|
|
|
|
# direct access to config
|
|
|
|
def [](key)
|
|
|
|
@config[key]
|
|
|
|
end
|
|
|
|
|
|
|
|
def []=(key, value)
|
|
|
|
@config[key] = value
|
|
|
|
end
|
|
|
|
|
2017-06-13 08:05:09 +00:00
|
|
|
def key?(key)
|
|
|
|
@config.key?(key)
|
|
|
|
end
|
|
|
|
|
2017-05-16 21:56:56 +00:00
|
|
|
def clean
|
|
|
|
@config = {}
|
|
|
|
end
|
|
|
|
|
2016-02-05 07:38:45 +00:00
|
|
|
# return the json data
|
|
|
|
def get
|
|
|
|
if File.exist?(@config_file)
|
|
|
|
file = File.read(@config_file)
|
|
|
|
@config = JSON.parse(file)
|
|
|
|
end
|
|
|
|
@config
|
|
|
|
end
|
|
|
|
|
|
|
|
# stores a hash to json
|
|
|
|
def store
|
|
|
|
File.open(@config_file, 'w') do |f|
|
2016-02-17 09:41:33 +00:00
|
|
|
f.chmod(0600)
|
2016-02-05 07:38:45 +00:00
|
|
|
f.write(@config.to_json)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# deletes data
|
|
|
|
def destroy
|
2016-04-13 14:54:29 +00:00
|
|
|
if File.exist?(@config_file)
|
|
|
|
File.delete(@config_file)
|
|
|
|
else
|
|
|
|
true
|
|
|
|
end
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
2016-04-04 15:29:13 +00:00
|
|
|
|
|
|
|
# return if the (stored) api version does not support a certain feature
|
|
|
|
def supported?(feature)
|
|
|
|
sup = version_with_support(feature)
|
|
|
|
|
2016-04-13 11:47:33 +00:00
|
|
|
# we do not know the version, therefore we do not know if its possible to use the feature
|
|
|
|
return if self['version'].nil? || self['version']['version'].nil?
|
|
|
|
|
2016-04-04 15:29:13 +00:00
|
|
|
if sup.is_a?(Array)
|
|
|
|
Gem::Version.new(self['version']['version']) >= sup[0] &&
|
|
|
|
Gem::Version.new(self['version']['version']) < sup[1]
|
|
|
|
else
|
|
|
|
Gem::Version.new(self['version']['version']) >= sup
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# exit 1 if the version of compliance that we're working with doesn't support odic
|
|
|
|
def legacy_check!(feature)
|
2017-11-21 07:49:41 +00:00
|
|
|
return if supported?(feature)
|
|
|
|
|
|
|
|
puts "This feature (#{feature}) is not available for legacy installations."
|
|
|
|
puts 'Please upgrade to a recent version of Chef Compliance.'
|
|
|
|
exit 1
|
2016-04-04 15:29:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# for a feature, returns either:
|
|
|
|
# - a version v0: v supports v0 iff v0 <= v
|
|
|
|
# - an array [v0, v1] of two versions: v supports [v0, v1] iff v0 <= v < v1
|
|
|
|
def version_with_support(feature)
|
|
|
|
case feature.to_sym
|
|
|
|
when :oidc
|
|
|
|
Gem::Version.new('0.16.19')
|
|
|
|
else
|
|
|
|
Gem::Version.new('0.0.0')
|
|
|
|
end
|
|
|
|
end
|
2016-02-05 07:38:45 +00:00
|
|
|
end
|
|
|
|
end
|