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



118
119
120
121
# File 'lib/inspec/metadata.rb', line 118

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



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

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

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



161
162
163
164
165
166
167
168
169
# File 'lib/inspec/metadata.rb', line 161

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



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/inspec/metadata.rb', line 148

def self.from_ref(ref, contents, profile_id, logger = nil)
  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



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

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



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

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



123
124
125
126
127
128
# File 'lib/inspec/metadata.rb', line 123

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

#valid?Boolean

Returns:

  • (Boolean)


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

def valid?
  is_valid = true
  %w{ name version }.each do |field|
    next unless params[field.to_sym].nil?
    @logger.error("Missing profile #{field} in #{ref}")
    is_valid = false
  end
  %w{ title summary maintainer copyright }.each do |field|
    next unless params[field.to_sym].nil?
    @logger.warn("Missing profile #{field} in #{ref}")
    is_valid = false
  end
  is_valid && @missing_methods.empty?
end