Class: InspecPlugins::Chef::Input

Inherits:
Object
  • Object
show all
Defined in:
lib/inspec-chef/input.rb

Constant Summary collapse

VALID_PATTERNS =
[
  Regexp.new("^databag://[^/]+/[^/]+/.+$"),
  Regexp.new("^node://[^/]*/attributes/.+$"),
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#chef_serverObject (readonly)

Returns the value of attribute chef_server.



15
16
17
# File 'lib/inspec-chef/input.rb', line 15

def chef_server
  @chef_server
end

#inspec_configObject



22
23
24
# File 'lib/inspec-chef/input.rb', line 22

def inspec_config
  @inspec_config ||= Inspec::Config.cached
end

#loggerObject



26
27
28
# File 'lib/inspec-chef/input.rb', line 26

def logger
  @logger ||= Inspec::Log
end

Instance Method Details

#fetch(_profile_name, input_uri) ⇒ Object

Fetch method used for Input plugins



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/inspec-chef/input.rb', line 34

def fetch(_profile_name, input_uri)
  logger.trace format("Inspec-Chef received query for input %<uri>s", uri: input_uri)
  return nil unless valid_plugin_input?(input_uri)

  logger.debug format("Inspec-Chef input schema detected")

  connect_to_chef_server

  input = parse_input(input_uri)
  if input[:type] == :databag
    data = get_databag_item(input[:object], input[:item])
  elsif input[:type] == :node && input[:item] == "attributes"
    # Search Chef node name, if no host given explicitly
    input[:object] = get_clientname(scan_target) unless input[:object] || inside_testkitchen?

    data = get_attributes(input[:object])
  end

  # Quote components to allow "-" as part of search query.
  # @see https://github.com/jmespath/jmespath.rb/issues/12
  expression = input[:query].map { |component| '"' + component + '"' }.join(".")
  result = JMESPath.search(expression, data)
  raise format("Could not resolve value for %s, check if databag/item or attribute exist", input_uri) unless result

  stringify(result)
end