Class: Inspec::Profile
- Inherits:
-
Object
- Object
- Inspec::Profile
- Defined in:
- lib/inspec/profile.rb
Overview
rubocop:disable Metrics/ClassLength
Instance Attribute Summary collapse
-
#metadata ⇒ Object
readonly
Returns the value of attribute metadata.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
Class Method Summary collapse
Instance Method Summary collapse
-
#archive(opts) ⇒ Object
generates a archive of a folder profile.
-
#check ⇒ Boolean
Check if the profile is internall well-structured.
- #info ⇒ Object
-
#initialize(options = nil) ⇒ Profile
constructor
A new instance of Profile.
- #rules_count ⇒ Object
Constructor Details
#initialize(options = nil) ⇒ Profile
Returns a new instance of Profile.
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 |
# File 'lib/inspec/profile.rb', line 22 def initialize( = nil) @options = || {} @params = {} @logger = [:logger] || Logger.new(nil) @path = @options[:path] fail 'Cannot read an empty path.' if @path.nil? || @path.empty? fail "Cannot find directory #{@path}" unless File.directory?(@path) @metadata = @params = @metadata.params # use the id from parameter, name or fallback to nil @profile_id = [:id] || params[:name] || nil @params[:rules] = rules = {} @runner = Runner.new( id: @profile_id, backend: :mock, ) @runner.add_tests([@path], @options) @runner.rules.each do |id, rule| file = rule.instance_variable_get(:@__file) rules[file] ||= {} rules[file][id] = { title: rule.title, desc: rule.desc, impact: rule.impact, checks: rule.instance_variable_get(:@checks), code: rule.instance_variable_get(:@__code), group_title: rule.instance_variable_get(:@__group_title), } end end |
Instance Attribute Details
#metadata ⇒ Object (readonly)
Returns the value of attribute metadata.
20 21 22 |
# File 'lib/inspec/profile.rb', line 20 def @metadata end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
18 19 20 |
# File 'lib/inspec/profile.rb', line 18 def params @params end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
19 20 21 |
# File 'lib/inspec/profile.rb', line 19 def path @path end |
Class Method Details
Instance Method Details
#archive(opts) ⇒ Object
generates a archive of a folder profile
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
# File 'lib/inspec/profile.rb', line 141 def archive(opts) # rubocop:disable Metrics/AbcSize check_result = check if check_result && !opts.ignore_errors == false @logger.info 'Profile check failed. Please fix the profile before generating an archive.' return false end profile_name = @params[:name] ext = opts[:zip] ? 'zip' : 'tar.gz' slug = profile_name.downcase.strip.tr(' ', '-').gsub(/[^\w-]/, '_') archive = Pathname.new(File.dirname(__FILE__)).join('../..', "#{slug}.#{ext}") # check if file exists otherwise overwrite the archive if archive.exist? && !opts[:overwrite] @logger.info "Archive #{archive} exists already. Use --overwrite." return false end # remove existing archive File.delete(archive) if archive.exist? @logger.info "Profile check finished. Generate archive #{archive}." # find all files files = Dir.glob("#{path}/**/*") # filter files that should not be part of the profile # TODO ignore all .files, but add the files to debug output # map absolute paths to relative paths files = files.collect { |f| Pathname.new(f).relative_path_from(Pathname.new(path)).to_s } # display all files that will be part of the archive @logger.debug 'Add the following files to archive:' files.each { |f| @logger.debug ' ' + f } if opts[:zip] # generate zip archive require 'inspec/archive/zip' zag = Inspec::Archive::ZipArchiveGenerator.new zag.archive(path, files, archive) else # generate tar archive require 'inspec/archive/tar' tag = Inspec::Archive::TarArchiveGenerator.new tag.archive(path, files, archive) end @logger.info 'Finished archive generation.' true end |
#check ⇒ Boolean
Check if the profile is internall well-structured. The logger will be used to print information on errors and warnings which are found.
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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
# File 'lib/inspec/profile.rb', line 85 def check # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity no_errors = true no_warnings = true warn = lambda { |msg| @logger.warn(msg) no_warnings = false } error = lambda { |msg| @logger.error(msg) no_warnings = no_errors = false } @logger.info "Checking profile in #{@path}" if Pathname.new(path).join('metadata.rb').exist? warn.call('The use of `metadata.rb` is deprecated. Use `inspec.yml`.') end @logger.info 'Metadata OK.' if @metadata.valid? # check if the profile is using the old test directory instead of the # new controls directory if Pathname.new(path).join('test').exist? && !Pathname.new(path).join('controls').exist? warn.call('Profile uses deprecated `test` directory, rename it to `controls`.') end count = rules_count if count == 0 warn.call('No controls or tests were defined.') else @logger.info("Found #{count} rules.") end # iterate over hash of groups @params[:rules].each do |group, rules_array| @logger.debug "Verify all rules in #{group}" rules_array.each do |id, rule| error.call('Avoid rules with empty IDs') if id.nil? or id.empty? next if id.start_with? '(generated ' warn.call("Rule #{id} has no title") if rule[:title].to_s.empty? warn.call("Rule #{id} has no description") if rule[:desc].to_s.empty? warn.call("Rule #{id} has impact > 1.0") if rule[:impact].to_f > 1.0 warn.call("Rule #{id} has impact < 0.0") if rule[:impact].to_f < 0.0 warn.call("Rule #{id} has no tests defined") if rule[:checks].nil? or rule[:checks].empty? end end @logger.info 'Rule definitions OK.' if no_warnings no_errors end |
#info ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/inspec/profile.rb', line 57 def info res = @params.dup rules = {} res[:rules].each do |gid, group| next if gid.to_s.empty? path = gid.sub(File.join(@path, ''), '') rules[path] = { title: path, rules: {} } group.each do |id, rule| next if id.to_s.empty? data = rule.dup data.delete(:checks) data[:impact] ||= 0.5 data[:impact] = 1.0 if data[:impact] > 1.0 data[:impact] = 0.0 if data[:impact] < 0.0 rules[path][:rules][id] = data # TODO: temporarily flatten the group down; replace this with # proper hierarchy later on rules[path][:title] = data[:group_title] end end res[:rules] = rules res end |
#rules_count ⇒ Object
136 137 138 |
# File 'lib/inspec/profile.rb', line 136 def rules_count @params[:rules].values.map { |hm| hm.values.length }.inject(:+) || 0 end |