diff --git a/.kitchen.yml b/.kitchen.yml index 9586f4d..c74f95c 100644 --- a/.kitchen.yml +++ b/.kitchen.yml @@ -51,6 +51,24 @@ suites: inspec_tests: - path: ./test/integration/duplicates - path: ./test/integration/duplicates + - name: attributes_inline + run_list: + - recipe[os_prepare] + verifier: + inspec_tests: + - path: ./test/integration/attributes + attributes: + user: bob + password: secret + - name: attributes_file + run_list: + - recipe[os_prepare] + verifier: + inspec_tests: + - test/integration/attributes + # - path: ./test/integration/attributes + attrs: + - test/integration/profile-attribute.yml # before you are able to use the compliance plugin, you need to run # insecure is only required if you use self-signed certificates # $ inspec compliance login https://compliance.test --user admin --insecure --token '' diff --git a/.travis.yml b/.travis.yml index 10ad39a..d69b498 100644 --- a/.travis.yml +++ b/.travis.yml @@ -31,6 +31,10 @@ matrix: bundler_args: "--without guard tools" script: bundle exec rake $SUITE env: SUITE="test:integration" OS='supermarket' + - rvm: 2.3.1 + bundler_args: "--without guard tools" + script: bundle exec rake $SUITE + env: SUITE="test:integration" OS='attributes-inline attributes-file' - rvm: ruby-head allow_failures: - rvm: ruby-head diff --git a/README.md b/README.md index 40aa780..44b1c45 100644 --- a/README.md +++ b/README.md @@ -178,6 +178,29 @@ suites: compliance: base/ssh ``` +### Use attributes with your inspec profiles + +To run a profile with attributes defined inline, you can adapt your `.kitchen.yml`: + +```yaml + verifier: + inspec_tests: + - path: test/integration/attributes + attributes: + user: bob + password: secret +``` + +You can also define your attributes in an external file. Adapt your `.kitchen.yml` to point to that file: + +```yaml + verifier: + inspec_tests: + - path: test/integration/attributes + attrs: + - test/integration/profile-attribute.yml + ``` + ## Development After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment. diff --git a/kitchen-inspec.gemspec b/kitchen-inspec.gemspec index 3354bcb..8b3fa71 100644 --- a/kitchen-inspec.gemspec +++ b/kitchen-inspec.gemspec @@ -23,4 +23,5 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 2.1.0" spec.add_dependency "inspec", ">=0.22.0", "<2.0.0" spec.add_dependency "test-kitchen", "~> 1.6" + spec.add_dependency "hashie", "~> 3.4" end diff --git a/lib/kitchen/verifier/inspec.rb b/lib/kitchen/verifier/inspec.rb index e2af42e..2226c5a 100644 --- a/lib/kitchen/verifier/inspec.rb +++ b/lib/kitchen/verifier/inspec.rb @@ -64,7 +64,15 @@ def finalize_config!(instance) # (see Base#call) def call(state) logger.debug("Initialize InSpec") + + # gather connection options opts = runner_options(instance.transport, state) + + # add attributes + opts[:attrs] = config[:attrs] + opts[:attributes] = Hashie.stringify_keys config[:attributes] unless config[:attributes].nil? + + # initialize runner runner = ::Inspec::Runner.new(opts) # add each profile to runner diff --git a/test/integration/attributes/controls/example.rb b/test/integration/attributes/controls/example.rb new file mode 100644 index 0000000..9fb8169 --- /dev/null +++ b/test/integration/attributes/controls/example.rb @@ -0,0 +1,11 @@ +# encoding: utf-8 +val_user = attribute("user", default: "alice", description: "An identification for the user") +val_password = attribute("password", description: "A value for the password") + +describe val_user do + it { should eq "bob" } +end + +describe val_password do + it { should eq "secret" } +end diff --git a/test/integration/attributes/inspec.yml b/test/integration/attributes/inspec.yml new file mode 100644 index 0000000..e1a6a0e --- /dev/null +++ b/test/integration/attributes/inspec.yml @@ -0,0 +1,8 @@ +name: attributes +title: InSpec Profile +maintainer: The Authors +copyright: The Authors +copyright_email: you@example.com +license: All Rights Reserved +summary: An InSpec Compliance Profile +version: 0.1.0 diff --git a/test/integration/profile-attribute.yml b/test/integration/profile-attribute.yml new file mode 100644 index 0000000..7458333 --- /dev/null +++ b/test/integration/profile-attribute.yml @@ -0,0 +1,2 @@ +user: bob +password: secret