Class: Inspec::Profile

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec/profile.rb

Overview

rubocop:disable Metrics/ClassLength

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = nil) ⇒ Profile

Returns a new instance of Profile.



20
21
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
# File 'lib/inspec/profile.rb', line 20

def initialize(options = nil)
  @options = options || {}
  @profile_id = options[:id] || nil
  @params = {}
  @logger = options[: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 unless @metadata.nil?

  @params[:rules] = rules = {}
  @runner = Runner.new(
    id: @profile_id,
    backend: :mock,
  )
  @runner.add_tests([@path])
  @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

#metadataObject (readonly)

Returns the value of attribute metadata.



18
19
20
# File 'lib/inspec/profile.rb', line 18

def 
  @metadata
end

#paramsObject (readonly)

Returns the value of attribute params.



17
18
19
# File 'lib/inspec/profile.rb', line 17

def params
  @params
end

Class Method Details

.from_path(path, options = nil) ⇒ Object



10
11
12
13
14
15
# File 'lib/inspec/profile.rb', line 10

def self.from_path(path, options = nil)
  opt = {}
  options.each { |k, v| opt[k.to_sym] = v } unless options.nil?
  opt[:path] = path
  Profile.new(opt)
end

Instance Method Details

#checkBoolean

Check if the profile is internall well-structured. The logger will be used to print information on errors and warnings which are found.

Returns:

  • (Boolean)

    true if no errors were found, false otherwise



81
82
83
84
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
# File 'lib/inspec/profile.rb', line 81

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 @params[:name].to_s.empty?
    error.call('No profile name defined')
  elsif !(@params[:name].to_s =~ %r{^\S+\/\S+$})
    error.call('Profile name must be defined as: OWNER/ID')
  end

  warn.call('No version defined') if @params[:name].to_s.empty?
  warn.call('No title defined') if @params[:name].to_s.empty?
  warn.call('No maintainer defined') if @params[:name].to_s.empty?
  warn.call('No supports defined') if @params[:name].empty?
  @logger.info 'Metadata OK.' if no_warnings

  no_warnings = true
  if @params[:name].empty?
    warn.call('No rules were found.')
  else
    @logger.debug "Found #{@params[:name].length} 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?
      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

#infoObject



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/inspec/profile.rb', line 53

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