Class: Inspec::Metadata

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

Overview

Extract metadata.rb information

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ref, logger = nil) ⇒ Metadata

Returns a new instance of Metadata.



13
14
15
16
17
18
# File 'lib/inspec/metadata.rb', line 13

def initialize(ref, logger = nil)
  @ref = ref
  @logger = logger || Logger.new(nil)
  @params = {}
  @missing_methods = []
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(sth, *args) ⇒ Object



126
127
128
129
# File 'lib/inspec/metadata.rb', line 126

def method_missing(sth, *args)
  @logger.warn "#{ref} doesn't support: #{sth} #{args}"
  @missing_methods.push(sth)
end

Instance Attribute Details

#paramsObject

Returns the value of attribute params.



12
13
14
# File 'lib/inspec/metadata.rb', line 12

def params
  @params
end

#refObject (readonly)

rubocop:disable Metrics/ClassLength



11
12
13
# File 'lib/inspec/metadata.rb', line 11

def ref
  @ref
end

Class Method Details

.finalize(metadata, profile_id) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/inspec/metadata.rb', line 142

def self.finalize(, profile_id)
  return nil if .nil?
  param = .params || {}
  param['name'] = profile_id.to_s unless profile_id.to_s.empty?
  param['version'] = param['version'].to_s unless param['version'].nil?
  .params = symbolize_keys(param)
  
end

.from_file(path, profile_id, logger = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
# File 'lib/inspec/metadata.rb', line 178

def self.from_file(path, profile_id, logger = nil)
  unless File.file?(path)
    logger ||= Logger.new(nil)
    logger.error "Can't find metadata file #{path}"
    return nil
  end

  from_ref(File.basename(path), File.read(path), profile_id, logger)
end

.from_ref(ref, contents, profile_id, logger = nil) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/inspec/metadata.rb', line 163

def self.from_ref(ref, contents, profile_id, logger = nil)
  # NOTE there doesn't have to exist an actual file, it may come from an
  # archive (i.e., contents)
  case File.basename(ref)
  when 'inspec.yml'
    from_yaml(ref, contents, profile_id, logger)
  when 'metadata.rb'
    from_ruby(ref, contents, profile_id, logger)
  else
    logger ||= Logger.new(nil)
    logger.error "Don't know how to handle metadata in #{ref}"
    nil
  end
end

.from_ruby(ref, contents, profile_id, logger = nil) ⇒ Object



157
158
159
160
161
# File 'lib/inspec/metadata.rb', line 157

def self.from_ruby(ref, contents, profile_id, logger = nil)
  res = Metadata.new(ref, logger)
  res.instance_eval(contents, ref, 1)
  finalize(res, profile_id)
end

.from_yaml(ref, contents, profile_id, logger = nil) ⇒ Object



151
152
153
154
155
# File 'lib/inspec/metadata.rb', line 151

def self.from_yaml(ref, contents, profile_id, logger = nil)
  res = Metadata.new(ref, logger)
  res.params = YAML.load(contents)
  finalize(res, profile_id)
end

.symbolize_keys(hash) ⇒ Object



135
136
137
138
139
140
# File 'lib/inspec/metadata.rb', line 135

def self.symbolize_keys(hash)
  hash.each_with_object({}) {|(k, v), h|
    v = symbolize_keys(v) if v.is_a?(Hash)
    h[k.to_sym] = v
  }
end

Instance Method Details

#is_supported(os, entry) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/inspec/metadata.rb', line 43

def is_supported(os, entry)
  name, family, release = support_fields(entry)

  # return true if the backend matches the supported OS's
  # fields act as masks, i.e. any value configured for os-name, os-family,
  # or release must be met by the backend; any field that is nil acts as
  # a glob expression i.e. is true

  # os name is both saved in :family and :name, so check both
  name_ok = name.nil? ||
            os[:name] == name || os[:family] == name

  family_check = family.to_s + '?'
  family_ok = family.nil? || os[:family] == family ||
              (
                os.respond_to?(family_check) &&
                # this call will return true if the family matches
                os.method(family_check).call
              )

  release_ok = release.nil? || os[:release] == release

  # we want to make sure that all matchers are true
  name_ok && family_ok && release_ok
end

#support_fields(entry) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/inspec/metadata.rb', line 69

def support_fields(entry)
  if entry.is_a?(Hash)
    try_support = self.class.symbolize_keys(entry)
    name = try_support[:'os-name'] || try_support[:os]
    family = try_support[:'os-family']
    release = try_support[:release]
  elsif entry.is_a?(String)
    @logger.warn(
      "Do not use deprecated `supports: #{entry}` syntax. Instead use "\
      "`supports: {os-family: #{entry}}`.")
    family = entry
  end

  [name, family, release]
end

#supports(sth, version = nil) ⇒ Object



37
38
39
40
41
# File 'lib/inspec/metadata.rb', line 37

def supports(sth, version = nil)
  # Ignore supports with metadata.rb. This file is legacy and the way it
  # it handles `supports` deprecated. A deprecation warning will be printed
  # already.
end

#supports_transport?(backend) ⇒ Boolean

Returns:

  • (Boolean)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/inspec/metadata.rb', line 85

def supports_transport?(backend)
  # make sure the supports field is always an array
  supp = params[:supports]
  supp = supp.is_a?(Hash) ? [supp] : Array(supp)

  # with no supports specified, always return true, as there are no
  # constraints on the supported backend; it is equivalent to putting
  # all fields into accept-all mode
  return true if supp.empty?

  found = supp.find do |entry|
    is_supported(backend.os, entry)
  end

  # finally, if we found a supported entry, we are good to go
  !found.nil?
end

#unsupportedObject



131
132
133
# File 'lib/inspec/metadata.rb', line 131

def unsupported
  @missing_methods
end

#validObject

return all warn and errors



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/inspec/metadata.rb', line 104

def valid
  errors = []
  warnings = []

  %w{ name version }.each do |field|
    next unless params[field.to_sym].nil?
    errors.push("Missing profile #{field} in #{ref}")
  end
  %w{ title summary maintainer copyright }.each do |field|
    next unless params[field.to_sym].nil?
    warnings.push("Missing profile #{field} in #{ref}")
  end

  [errors, warnings]
end

#valid?Boolean

returns true or false

Returns:

  • (Boolean)


121
122
123
124
# File 'lib/inspec/metadata.rb', line 121

def valid?
  errors, _warnings = valid
  errors.empty? && unsupported.empty?
end