inspec-chef 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: afe50e575ce5384475bd4c1f0ba240df92727baf14cde3a96f35e2a474eceb8c
4
- data.tar.gz: 6f51ad5775fa43d5ea7eff553608f91f24de4e3248ffcd8722fd92c4e393fba7
3
+ metadata.gz: f13512640633f549c0a190e61f94cc2044c9d551401ccb4371e8f1cd37cd102b
4
+ data.tar.gz: a47a60292470aad2385de9697c024f7457883fc64eedff78b455a79f7406b367
5
5
  SHA512:
6
- metadata.gz: 645ab50c1f28a99b7b545d205aa460a819bcdc1380853ed6cbdf0538a53dfb140dd08d0733dab722c9a2cc9b9f4198ae808ff5fe0efb8a2e5ac05ca7e7d2b01a
7
- data.tar.gz: e7a43170a680e81d11416e72aca2b81e85aa961ca6120fe44e2d4a0c366cebab152741336d3ff5e4ad9f06b914e66de73790ca3ae67bf59f53a4cd832b32491d
6
+ metadata.gz: e848365caa7f68c3a0f4509284a530f62095274540787201a458c39fdd077862765261b46d95acc9267874580c0f9045914172f5a53da284114e889831639936
7
+ data.tar.gz: 4ff246d1449d0266ebb3c8bb9774f7f77f0ff209f04cb9263defb492bc628d0ba964740c8fcd803a011507dca113a842a15e8797496b7319ff76cceaef8f91ce
data/README.md CHANGED
@@ -21,7 +21,7 @@ the plugin from RubyGems and install/register it with InSpec.
21
21
 
22
22
  You can verify successful installation via `inspec plugin list`
23
23
 
24
- ## Configuration
24
+ ## Configuration for Chef Infra Server
25
25
 
26
26
  Each plugin option may be set either as an environment variable, or as a plugin
27
27
  option in your Chef InSpec configuration file at ~/.inspec/config.json. For
@@ -51,6 +51,28 @@ This plugin supports the following options:
51
51
  | INSPEC_CHEF_CLIENT | chef_api_client | The name of the client of the Chef server to connect as |
52
52
  | INSPEC_CHEF_KEY | chef_api_key | Path to the private certificate identifying the node |
53
53
 
54
+ ## Configuration for TestKitchen
55
+
56
+ To allow dev/prod parity, this input plugin detects if it is called from within
57
+ TestKitchen. As these tests should not access the Chef Server (to provide the
58
+ needed test data instead of live data), it will then revert on using the
59
+ `data_bags_path` and `attributes` from kitchen's `provisioner` section:
60
+
61
+ ```yaml
62
+ suites:
63
+ - name: default
64
+ verifier:
65
+ load_plugins: true
66
+ data_bags_path: "test/integration/data_bags"
67
+ attributes:
68
+ java:
69
+ install_flavor: "oracle"
70
+ ```
71
+
72
+ Please note, that support for `load_plugins` is not available on versions 1.3.1
73
+ and below of the `kitchen-inspec` verifier plugin. Please check
74
+ [kitchen-inspec PR #247 on GitHub](https://github.com/inspec/kitchen-inspec/pull/247) for finding official versions supporting this feature.
75
+
54
76
  ## Usage
55
77
 
56
78
  When this plugin is loaded, you can use databag items as inputs:
@@ -36,4 +36,6 @@ Gem::Specification.new do |spec|
36
36
 
37
37
  spec.add_dependency "chef-api", "~> 0.10"
38
38
  spec.add_dependency "jmespath", "~> 1.4"
39
+
40
+ spec.add_development_dependency "bump", "~> 0.8"
39
41
  end
@@ -16,15 +16,17 @@ module InspecPlugins::Chef
16
16
  def initialize
17
17
  @plugin_conf = Inspec::Config.cached.fetch_plugin_config("inspec-chef")
18
18
 
19
- @chef_endpoint = fetch_plugin_setting("endpoint")
20
- @chef_client = fetch_plugin_setting("client")
21
- @chef_api_key = fetch_plugin_setting("key")
19
+ unless Inspec::Config.cached.final_options.logger.is_a?(Kitchen::Logger)
20
+ @chef_endpoint = fetch_plugin_setting("endpoint")
21
+ @chef_client = fetch_plugin_setting("client")
22
+ @chef_api_key = fetch_plugin_setting("key")
22
23
 
23
- if chef_endpoint.nil? || chef_client.nil? || chef_api_key.nil?
24
- raise "ERROR: Need configuration of chef endpoint, client name and api key."
25
- end
24
+ if chef_endpoint.nil? || chef_client.nil? || chef_api_key.nil?
25
+ raise "ERROR: Need configuration of chef endpoint, client name and api key."
26
+ end
26
27
 
27
- connect_to_chef_server
28
+ connect_to_chef_server
29
+ end
28
30
  end
29
31
 
30
32
  # Fetch method used for Input plugins
@@ -44,6 +46,19 @@ module InspecPlugins::Chef
44
46
 
45
47
  private
46
48
 
49
+ # Check if this is called from within TestKitchen
50
+ def inside_testkitchen?
51
+ !! defined?(::Kitchen::Logger)
52
+ end
53
+
54
+ # Reach for Kitchen data and return its evaluated config
55
+ def kitchen_provisioner_config
56
+ require 'binding_of_caller'
57
+ kitchen = binding.callers.find { |b| b.frame_description == 'verify' }.receiver
58
+
59
+ kitchen.provisioner.send(:provided_config)
60
+ end
61
+
47
62
  # Get plugin setting via environment, config file or default
48
63
  def fetch_plugin_setting(setting_name, default = nil)
49
64
  env_var_name = "INSPEC_CHEF_#{setting_name.upcase}"
@@ -62,14 +77,33 @@ module InspecPlugins::Chef
62
77
 
63
78
  # Retrieve a Databag item from Chef Server
64
79
  def get_databag_item(databag, item)
65
- chef_api.data_bag_item.fetch(item, bag: databag).data
80
+ unless inside_testkitchen?
81
+ unless chef_api.data_bags.any? { |k| k.name == databag }
82
+ raise format('Databag "%s" not found on Chef Infra Server', databag)
83
+ end
84
+
85
+ chef_api.data_bag_item.fetch(item, bag: databag).data
86
+ else
87
+ config = kitchen_provisioner_config
88
+ filename = File.join(config[:data_bags_path], databag, item + '.json')
89
+
90
+ begin
91
+ contents = JSON.load(File.read(filename))
92
+ rescue
93
+ raise format('Error accessing databag file %s, check TestKitchen configuration', filename)
94
+ end
95
+ end
66
96
  end
67
97
 
68
98
  # Retrieve attributes of a node
69
99
  def get_attributes(node)
70
- data = get_search(:node, "name:#{node}")
100
+ unless inside_testkitchen?
101
+ data = get_search(:node, "name:#{node}")
71
102
 
72
- merge_attributes(data)
103
+ merge_attributes(data)
104
+ else
105
+ kitchen_provisioner_config[:attributes]
106
+ end
73
107
  end
74
108
 
75
109
  # Low-level Chef search expression
@@ -5,6 +5,6 @@
5
5
  # to learn the current version.
6
6
  module InspecPlugins
7
7
  module Chef
8
- VERSION = "0.1.0".freeze
8
+ VERSION = "0.2.0".freeze
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inspec-chef
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Heinen
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-01-08 00:00:00.000000000 Z
11
+ date: 2020-01-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: chef-api
@@ -38,6 +38,20 @@ dependencies:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
40
  version: '1.4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bump
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.8'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.8'
41
55
  description: This plugin allows InSpec 'inputs' to be provided by Chef Server.
42
56
  email:
43
57
  - theinen@tecracer.de