Class: Inspec::Metadata
  
  
  
  
  
    - Inherits:
- 
      Object
      
        
        show all
      
    
    - Defined in:
- lib/inspec/metadata.rb
 
Overview
  
    
Extract metadata.rb information A Metadata object may be created and finalized with invalid data. This allows the check CLI command to analyse the issues. Use valid? to determine if the metadata is coherent.
   
 
  
  Instance Attribute Summary collapse
  
  
    
      Class Method Summary
      collapse
    
    
      
        - 
  
    
      .finalize(metadata, profile_id, options, logger = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .finalize_name(metadata, profile_id, original_target)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .finalize_supports(supports, logger)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .finalize_supports_elem(elem, logger)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .from_file(path, profile_id, logger = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .from_ref(ref, content, profile_id, logger = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .from_ruby(ref, content, profile_id, logger = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .from_yaml(ref, content, profile_id, logger = nil)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
- 
  
    
      .symbolize_keys(obj)  ⇒ Object 
    
    
  
  
  
  
  
  
  
  
  
    
  
      Instance Method Summary
      collapse
    
    
  
  Constructor Details
  
    
  
  
    #initialize(ref, logger = nil)  ⇒ Metadata 
  
  
  
  
    
Returns a new instance of Metadata.
   
 
  
  
    | 
19
20
21
22
23
24
25 | # File 'lib/inspec/metadata.rb', line 19
def initialize(ref, logger = nil)
  @ref = ref
  @logger = logger || Logger.new(nil)
  @content = ""
  @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
    
      
      
      
  
  
    #content  ⇒ Object 
  
  
  
  
    
Returns the value of attribute content.
   
 
  
  
    | 
18
19
20 | # File 'lib/inspec/metadata.rb', line 18
def content
  @content
end | 
 
    
      
      
      
  
  
    #params  ⇒ Object 
  
  
  
  
    
Returns the value of attribute params.
   
 
  
  
    | 
18
19
20 | # File 'lib/inspec/metadata.rb', line 18
def params
  @params
end | 
 
    
      
      
      
  
  
    #ref  ⇒ Object  
  
  
  
  
    
Returns the value of attribute ref.
   
 
  
  
    | 
17
18
19 | # File 'lib/inspec/metadata.rb', line 17
def ref
  @ref
end | 
 
    
   
  
    Class Method Details
    
      
  
  
    .finalize(metadata, profile_id, options, logger = nil)  ⇒ Object 
  
  
  
  
    | 
196
197
198
199
200
201
202
203
204
205
206
207 | # File 'lib/inspec/metadata.rb', line 196
def self.finalize(metadata, profile_id, options, logger = nil)
  return nil if metadata.nil?
  param = metadata.params || {}
  options ||= {}
  param["version"] = param["version"].to_s unless param["version"].nil?
  metadata.params = symbolize_keys(param)
  metadata.params[:supports] = finalize_supports(metadata.params[:supports], logger)
  finalize_name(metadata, profile_id, options[:target])
  metadata
end | 
 
    
      
  
  
    .finalize_name(metadata, profile_id, original_target)  ⇒ Object 
  
  
  
  
    | 
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194 | # File 'lib/inspec/metadata.rb', line 174
def self.finalize_name(metadata, profile_id, original_target)
    unless profile_id.to_s.empty?
    metadata.params[:name] = profile_id.to_s
    return
  end
    return unless metadata.params[:name].nil?
    return unless metadata.params[:title].nil?
        return if original_target.to_s.empty?
  metadata.params[:title] = "tests from #{original_target}"
  metadata.params[:name] = metadata.params[:title].gsub(%r{[\/\\]}, ".")
end | 
 
    
      
  
  
    .finalize_supports(supports, logger)  ⇒ Object 
  
  
  
  
    | 
166
167
168
169
170
171
172 | # File 'lib/inspec/metadata.rb', line 166
def self.finalize_supports(supports, logger)
  case x = supports
  when Hash   then [finalize_supports_elem(x, logger)]
  when Array  then x.map { |e| finalize_supports_elem(e, logger) }.compact
  when nil    then []
  end
end | 
 
    
      
  
  
    .finalize_supports_elem(elem, logger)  ⇒ Object 
  
  
  
  
    | 
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164 | # File 'lib/inspec/metadata.rb', line 146
def self.finalize_supports_elem(elem, logger)
  case x = elem
  when Hash
    x[:release] = x[:release].to_s if x[:release]
    x
  when Array
    logger.warn(
      "Failed to read supports entry that is an array. Please use "\
      "the `supports: {os-family: xyz}` syntax."
    )
    nil
  when nil then nil
  else
    Inspec.deprecate(:supports_syntax,
      "Do not use deprecated `supports: #{x}` syntax. Instead use:\n"\
      "supports:\n  - os-family: #{x}\n\n")
    { :'os-family' => x }   end
end | 
 
    
      
  
  
    .from_file(path, profile_id, logger = nil)  ⇒ Object 
  
  
  
  
    | 
239
240
241
242
243
244
245
246
247 | # File 'lib/inspec/metadata.rb', line 239
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, content, profile_id, logger = nil)  ⇒ Object 
  
  
  
  
    | 
224
225
226
227
228
229
230
231
232
233
234
235
236
237 | # File 'lib/inspec/metadata.rb', line 224
def self.from_ref(ref, content, profile_id, logger = nil)
      case File.basename(ref)
  when "inspec.yml"
    from_yaml(ref, content, profile_id, logger)
  when "metadata.rb"
    from_ruby(ref, content, 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, content, profile_id, logger = nil)  ⇒ Object 
  
  
  
  
    | 
217
218
219
220
221
222 | # File 'lib/inspec/metadata.rb', line 217
def self.from_ruby(ref, content, profile_id, logger = nil)
  res = Metadata.new(ref, logger)
  res.instance_eval(content, ref, 1)
  res.content = content
  finalize(res, profile_id, {}, logger)
end | 
 
    
      
  
  
    .from_yaml(ref, content, profile_id, logger = nil)  ⇒ Object 
  
  
  
  
    | 
209
210
211
212
213
214
215 | # File 'lib/inspec/metadata.rb', line 209
def self.from_yaml(ref, content, profile_id, logger = nil)
  require "erb"
  res = Metadata.new(ref, logger)
  res.params = YAML.load(ERB.new(content).result)
  res.content = content
  finalize(res, profile_id, {}, logger)
end | 
 
    
      
  
  
    .symbolize_keys(obj)  ⇒ Object 
  
  
  
  
    | 
135
136
137
138
139
140
141
142
143
144 | # File 'lib/inspec/metadata.rb', line 135
def self.symbolize_keys(obj)
  return obj.map { |i| symbolize_keys(i) } if obj.is_a?(Array)
  return obj unless obj.is_a?(Hash)
  obj.each_with_object({}) do |(k, v), h|
    v = symbolize_keys(v) if v.is_a?(Hash)
    v = symbolize_keys(v) if v.is_a?(Array)
    h[k.to_sym] = v
  end
end | 
 
    
   
  
    Instance Method Details
    
      
  
  
    #dependencies  ⇒ Object 
  
  
  
  
    | 
45
46
47 | # File 'lib/inspec/metadata.rb', line 45
def dependencies
  params[:depends] || []
end | 
 
    
      
  
  
    #inspec_requirement  ⇒ Object 
  
  
  
  
    | 
55
56
57
58
59 | # File 'lib/inspec/metadata.rb', line 55
def inspec_requirement
      Gem::Requirement.create(params[:inspec_version])
end | 
 
    
      
  
  
    #supports(sth, version = nil)  ⇒ Object 
  
  
  
  
    | 
49
50
51
52
53 | # File 'lib/inspec/metadata.rb', line 49
def supports(sth, version = nil)
      end | 
 
    
      
  
  
    | 
66
67
68 | # File 'lib/inspec/metadata.rb', line 66
def supports_platform?(backend)
  backend.platform.supported?(params[:supports])
end | 
 
    
      
  
  
    #supports_runtime?  ⇒ Boolean 
  
  
  
  
    | 
61
62
63
64 | # File 'lib/inspec/metadata.rb', line 61
def supports_runtime?
  running = Gem::Version.new(Inspec::VERSION)
  inspec_requirement.satisfied_by?(running)
end | 
 
    
      
  
  
    #unsupported  ⇒ Object 
  
  
  
  
    | 
131
132
133 | # File 'lib/inspec/metadata.rb', line 131
def unsupported
  @missing_methods
end | 
 
    
      
  
  
    #valid  ⇒ Object 
  
  
  
  
    
return all warn and errors
   
 
  
  
    | 
71
72
73
74
75
76
77
78
79
80
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 | # File 'lib/inspec/metadata.rb', line 71
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
  if %r{[\/\\]} =~ params[:name]
    errors.push("The profile name (#{params[:name]}) contains a slash" \
                  " which is not permitted. Please remove all slashes from `inspec.yml`.")
  end
    if !params[:version].nil? && !valid_version?(params[:version])
    errors.push("Version needs to be in SemVer format")
  end
  unless supports_runtime?
    warnings.push("The current inspec version #{Inspec::VERSION} cannot satisfy profile inspec_version constraint #{params[:inspec_version]}")
  end
  %w{title summary maintainer copyright license}.each do |field|
    next unless params[field.to_sym].nil?
    warnings.push("Missing profile #{field} in #{ref}")
  end
    if !params[:license].nil? && !valid_license?(params[:license])
    warnings.push("License '#{params[:license]}' needs to be in SPDX format or marked as 'Proprietary'. See https://spdx.org/licenses/.")
  end
  [errors, warnings]
end | 
 
    
      
  
  
    #valid?  ⇒ Boolean 
  
  
  
  
    | 
110
111
112
113 | # File 'lib/inspec/metadata.rb', line 110
def valid?
  errors, _warnings = valid
  errors.empty? && unsupported.empty?
end | 
 
    
      
  
  
    #valid_license?(value)  ⇒ Boolean 
  
  
  
  
    | 
122
123
124 | # File 'lib/inspec/metadata.rb', line 122
def valid_license?(value)
  value =~ /^Proprietary[,;]?\b/ || Spdx.valid_license?(value)
end | 
 
    
      
  
  
    #valid_version?(value)  ⇒ Boolean 
  
  
  
  
    | 
115
116
117
118
119
120 | # File 'lib/inspec/metadata.rb', line 115
def valid_version?(value)
  Semverse::Version.new(value)
  true
rescue Semverse::InvalidVersionFormat
  false
end |