Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom resource not available, undefined local variable or method gordon_config #232

Closed
aiwilliams opened this issue Nov 12, 2015 · 20 comments
Labels
Type: Bug Feature not working as expected

Comments

@aiwilliams
Copy link

I've followed the setup instructions... gem install, simple file with example GordonConfig. Through debugging I see that the resource is listed in the registry. However, I am seeing:

app_server_spec.rb:19:in `load': undefined local variable or method `gordon_config' for #<#<Class:0x007fb204862a78>:0x007fb204862528> (NameError)
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/profile_context.rb:31:in `instance_eval'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/profile_context.rb:31:in `load'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:76:in `add_content'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:57:in `block in add_tests'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:56:in `each'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:56:in `add_tests'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/bin/inspec:77:in `exec'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/bin/inspec:109:in `<top (required)>'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/bin/inspec:23:in `load'
    from /Users/aiwilliams/.rbenv/versions/2.1.6/bin/inspec:23:in `<main>'
@chris-rock
Copy link
Contributor

@aiwilliams Where do you place the rb file of the resource?

@aiwilliams
Copy link
Author

@chris-rock I've placed it in the test.rb file, and I've tried it as a peer file and used require_relative "gordon_config.rb".

@erichs
Copy link

erichs commented Nov 12, 2015

👍 I get this also.

Given:

gordon_config.rb:
========

require 'yaml'

class GordonConfig < Inspec.resource(1)
  name 'gordon_config'

  def initialize
    @path = '/etc/gordon/config.yaml'
    @config = inspec.file(@path).content
    @params = YAML.load(@config)
  end

  def method_missing(name)
    @params[name.to_s]
  end
end

test.rb:
========

require_relative 'gordon_config'

describe port(80) do
    it { should_not be_listening }
end

describe gordon_config do
    its('Version') { should eq('1.0') }
end

inspec exec test.rb produces:

test.rb:7:in `load': undefined local variable or method `gordon_config' for #<#<Class:0x007fe4bb3f0eb0>:0x007fe4bb3f0c80> (NameError)
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/profile_context.rb:31:in `instance_eval'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/profile_context.rb:31:in `load'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:76:in `add_content'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:57:in `block in add_tests'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:56:in `each'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/lib/inspec/runner.rb:56:in `add_tests'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/bin/inspec:77:in `exec'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/command.rb:27:in `run'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/invocation.rb:126:in `invoke_command'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor.rb:359:in `dispatch'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/thor-0.19.1/lib/thor/base.rb:440:in `start'
        from /Users/erichs/.rbenv/versions/2.1.5/lib/ruby/gems/2.1.0/gems/inspec-0.9.2/bin/inspec:109:in `<top (required)>'
        from /Users/erichs/.rbenv/versions/2.1.5/bin/inspec:23:in `load'
        from /Users/erichs/.rbenv/versions/2.1.5/bin/inspec:23:in `<main>'

Running with:

❯ inspec version
0.9.2

and

❯ ruby --version
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin14.0]

@chris-rock
Copy link
Contributor

I will add an example that demonstrates the use of custom resources within the next days

@erichs
Copy link

erichs commented Nov 25, 2015

@chris-rock, thanks! I still get the above behavior on 0.9.5, so I must be doing something wrong.

@chris-rock
Copy link
Contributor

This PR add a lot of documentation about profiles and adds an example with gordon_config: https://github.com/chef/inspec/pull/252/files

It does not cover your use case that loads the gordon_config explicitly. I will add a specific test case for your use case with relative loading. I find some time tomorrow to work on this bug...

@chris-rock chris-rock added the Type: Bug Feature not working as expected label Nov 26, 2015
@chris-rock
Copy link
Contributor

@erichs I confirm this bug. At this point of time, plugins only work with InSpec profiles as proposed in #252

@arlimus
Copy link
Contributor

arlimus commented Nov 30, 2015

@erichs @aiwilliams : Thank you for reporting this!

As @chris-rock suggested, we can handle it very well in a test profile, i.e.:

.
|- libraries / custom_resource.rb
\- ...

See the updated readme: https://github.com/chef/inspec/blob/profile-structure/docs/profiles.rst

Would that solve your problem?

I'm slightly hesitant to allowing resource definitions at any position in the code, as it may make debugging the code harder in the long run (e.g. if profiles are inherited). If profiles solve this problem for you, we could go that route instead. If not, could you share why, or what you don't like about it?

@chris-rock
Copy link
Contributor

I'd like to add to @arlimus that I do not like my implementation of #274 Although its working, it comes with a lot of problems, that make it challenging to change the code in future. I would also prefer to remove the old way of extensions and support resource extensions via compliance profiles. They have a clear structure and allow us to introduce a clear separation of concerns.

@erichs
Copy link

erichs commented Nov 30, 2015

@arlimus @chris-rock, FWIW a directory structure convention for custom resources makes sense to me. 👍

@chris-rock
Copy link
Contributor

I also prefer to go with #252

@aiwilliams
Copy link
Author

@arlimus @chris-rock I have no strong preference! I was just following instructions, which I did not understand to mean that I should put the file in a particular place. Thanks for working out a solution.

@chris-rock
Copy link
Contributor

@aiwilliams @erichs Thanks for the feedback. Then we decide against #274 and vote for #252. Would be great if you could try our the PR and provide some feedback to the current implementation of compliance profiles.

@erichs
Copy link

erichs commented Nov 30, 2015

@chris-rock: using the code @a9ab812, and given the contents of /etc/gordon/config.yaml are:

---
- version: "1.0"

when executing:

~/repos/inspec profile-structure*
❯ bundle exec bin/inspec exec examples/profile

Then I get:

.F

Failures:

  1) gordon_config version
     Failure/Error: rspec_runner.run_specs(@tests.ordered_example_groups)

     TypeError:
       no implicit conversion of String into Integer
     # examples/profile/libraries/gordon_config.rb:15:in `[]'
     # examples/profile/libraries/gordon_config.rb:15:in `method_missing'
     # examples/profile/controls/gordon_spec.rb:18:in `block (3 levels) in load'
     # /Users/erichs/Dropbox (Personal)/repos/inspec/lib/inspec/runner.rb:97:in `run_with'
     # /Users/erichs/Dropbox (Personal)/repos/inspec/lib/inspec/runner.rb:93:in `run'

Finished in 0.00257 seconds (files took 0.17456 seconds to load)
2 examples, 1 failure

Failed examples:

rspec  # gordon_config version

@chris-rock
Copy link
Contributor

@erichs try to use the following config.yaml:

cat <<EOF > /etc/gordon/config.yaml
version: '1.0'
EOF

@erichs
Copy link

erichs commented Nov 30, 2015

Ah, thanks! @chris-rock, this appears to work fine.

@chris-rock
Copy link
Contributor

I agree that the default output of the gordon_config is not perfect.

@aiwilliams
Copy link
Author

I think I might be misunderstanding something in the new documentation. I have a project like this:

.
├── Gemfile
├── Gemfile.lock
└── app_server_spec.rb

bundle exec inspec exec app_server_spec.rb works well. Then I want to have my own "resource", such as http_response("http://someplace.com"). When I read the docs in master, it is unclear what I am supposed to do to create my own "resource". When I read the docs in #252, and see the words "profile" and "control", and then this:

examples/profile
 ├── README.md
 ├── controls
 │   ├── example_spec.rb
 │   └── gordon_spec.rb
 ├── libraries
 │   └── gordon_config.rb
 └── metadata.rb

I find I'm still confused. I suppose I would expect that I might need to create:

.
├── libraries
│   ├── http_response.rb
├── Gemfile
├── Gemfile.lock
└── app_server_spec.rb

Perhaps I'm confusing "resources", "controls", "profiles", "specs"? Hopefully my confusion can help clarify for some folks!

@chris-rock
Copy link
Contributor

@aiwilliams Thanks for bringing up this question. That means our documentation is not in a state that it should be.

Short answer to your question: Place app_server_spec.rb in a controls directory

I try to cover the wording. I will add this to the documentation as well.

  • resource - an InSpec resource is an implementation of an InSpec extension to ease testing. e.g. package is an InSpec resource that is shipped with the core. Within a profile you could write your own resources.
  • controls - essentially a collection of tests, synonym for specs. This directory contains a collection of tests aka controls
  • profile - is a standalone test suite that collects controls and resources. A profile is independent of a devops tool and can be easily shared. This format is useful if you implement a compliance profile like NIST, CIS or in-house compliance profiles that need to be shared across departments.

@aiwilliams
Copy link
Author

Thanks @chris-rock, that is helpful. I think I was confused because my goal was to create a reusable resource, yet a discussion developed around reusable profiles. I just went back to master to review the documentation and it no longer discusses custom resources. I think I can close this Issue and assume there are forthcoming changes that will modify https://github.com/chef/inspec/blob/master/docs/readme.rst so that it is clear how one might develop and utilize custom resources in test/example_spec.rb.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Type: Bug Feature not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants