Skip to content

Commit

Permalink
Fix handling of data bags (#5)
Browse files Browse the repository at this point in the history
Signed-off-by: Thomas Heinen <[email protected]>
  • Loading branch information
thheinen authored Apr 9, 2020
1 parent 501160a commit de909e2
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.3.4

- Fix handling if data_bags_path is unset
- Fix lookup of data_bags to support `suites`, `provisioner` and `verifier` level

## v0.3.3

- Fix nested responses to allow access by String/Symbol
Expand Down
27 changes: 20 additions & 7 deletions lib/inspec-chef/input.rb
Original file line number Diff line number Diff line change
Expand Up @@ -115,16 +115,24 @@ def stringify(result)

# ========================================================================
# Interfacing with Inspec and Chef
#

# Reach for Kitchen data and return its evaluated config
# @todo DI
def kitchen_provisioner_config
# Access to kitchen data
def kitchen
require "binding_of_caller"
kitchen = binding.callers.find { |b| b.frame_description == "verify" }.receiver
binding.callers.find { |b| b.frame_description == "verify" }.receiver
end

# Return provisioner config
def kitchen_provisioner_config
kitchen.provisioner.send(:provided_config)
end

# Return verifier config
def kitchen_verifier_config
kitchen.verifier.send(:provided_config)
end

# Get plugin specific configuration
def plugin_conf
inspec_config.fetch_plugin_config("inspec-chef")
Expand All @@ -147,7 +155,7 @@ def scan_target
def connect_to_chef_server
# From within TestKitchen we need no Chef Server connection
if inside_testkitchen?
logger.info "Running from TestKitchen, using provisioner settings instead of Chef Server"
logger.info "Running from TestKitchen, using static settings instead of Chef Server"

# Only connect once
elsif !server_connected?
Expand Down Expand Up @@ -191,8 +199,7 @@ def get_databag_item(databag, item)

chef_server.data_bag_item.fetch(item, bag: databag).data
else
config = kitchen_provisioner_config
filename = File.join(config[:data_bags_path], databag, item + ".json")
filename = File.join(data_bags_path, databag, item + ".json")

begin
return JSON.load(File.read(filename))
Expand All @@ -202,6 +209,12 @@ def get_databag_item(databag, item)
end
end

# Calculate lookup path for databags within TestKitchen
def data_bags_path
# These can occur on suite-level, provisioner-level, verifier or at the default location
kitchen_provisioner_config[:data_bags_path] || kitchen_verifier_config[:data_bags_path] || File.join('test', 'data_bags')
end

# Retrieve attributes of a node
def get_attributes(node)
unless inside_testkitchen?
Expand Down
2 changes: 1 addition & 1 deletion lib/inspec-chef/version.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@
# to learn the current version.
module InspecPlugins
module Chef
VERSION = "0.3.3".freeze
VERSION = "0.3.4".freeze
end
end

0 comments on commit de909e2

Please sign in to comment.