Skip to content

Instantly share code, notes, and snippets.

@nellshamrell
Created April 18, 2019 21:09
Show Gist options
  • Save nellshamrell/0cf88bd3489129d128bd3e39cf5a4f03 to your computer and use it in GitHub Desktop.
Save nellshamrell/0cf88bd3489129d128bd3e39cf5a4f03 to your computer and use it in GitHub Desktop.

Troubleshooting using the http resource on an Azure load balancer

https://github.com/inspec/inspec/issues/3689 (includes replication steps)

Backtrace

inspec/lib/resources/http.rb

def body
  @worker.body
end

When running the http resource locally, @worker is set properly. When running the http resource on an Azure load balancer, @worker is set to nil.

@nellshamrell
Copy link
Author

Backtrace using pry

From: /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/resources/http.rb @ line 63 Inspec::Resources::Http#body:

    60:     def body
    61: require 'pry'
    62: binding.pry
 => 63:       @worker.body
    64:     end

[1] pry(#<#<Class:0x00007fb52c21c440>>)> backtrace
--> #0  Inspec::Resources::Http.body at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/resources/http.rb:63
    #1  block in Inspec::ProfileContext.block in load_with_context(context##<Class:0x00007fb52e15bdd8>, content#String, source#String, line#NilClass) at /Users/nellshamrell/Projects/test_inspec_profiles/nell_test_profile/controls/example.rb:17
    ͱ-- #2  BasicObject.instance_eval(*args) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/rule.rb:54
    #3  Inspec::Rule.initialize(id#String, profile_id#String, opts#Hash, &block#Proc) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/rule.rb:54
    ͱ-- #4  Class.new(*args) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/control_eval_context.rb:107
    #5  block (2 levels) in #<Class:0x00007fb52e15bdd8>.block (2 levels) in create(*args#Array, &block#Proc) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/control_eval_context.rb:107
    #6  Inspec::ProfileContext.load_with_context(context##<Class:0x00007fb52e15bdd8>, content#String, source#String, line#NilClass) at /Users/nellshamrell/Projects/test_inspec_profiles/nell_test_profile/controls/example.rb:7
    ͱ-- #7  BasicObject.instance_eval(*args) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/profile_context.rb:160
    #8  Inspec::ProfileContext.load_with_context(context##<Class:0x00007fb52e15bdd8>, content#String, source#String, line#NilClass) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/profile_context.rb:160
    #9  Inspec::ProfileContext.load_control_file(*args#Array) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/profile_context.rb:144
    #10 block in Inspec::Profile.block in collect_tests(include_list#Array) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/profile.rb:196
    ͱ-- #11 Hash.each at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/profile.rb:193
    #12 Inspec::Profile.collect_tests(include_list#Array) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/profile.rb:193
    #13 block in Inspec::Runner.block in load at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/runner.rb:105
    ͱ-- #14 Array.each at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/runner.rb:87
    #15 Inspec::Runner.load at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/runner.rb:87
    #16 Inspec::Runner.run(with#NilClass) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/runner.rb:116
    #17 Inspec::InspecCLI.exec(*targets#Array) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/inspec/cli.rb:269
    #18 Thor::Command.run(instance#Inspec::InspecCLI, args#Array) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor/command.rb:27
    #19 Thor::Invocation.invoke_command(command#Thor::Command, *args#Array) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor/invocation.rb:126
    #20 #<Class:Thor>.dispatch(meth#String, given_args#Array, given_opts#NilClass, config#Hash) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor.rb:387
    #21 Thor::Base::ClassMethods.start(given_args#Array, config#Hash) at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/thor-0.20.3/lib/thor/base.rb:466
    #22 <top (required)> at /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/bin/inspec:12
    ͱ-- #23 Kernel.load at /Users/nellshamrell/.rbenv/versions/2.6.1/bin/inspec:23
    #24 <main> at /Users/nellshamrell/.rbenv/versions/2.6.1/bin/inspec:23

From: /Users/nellshamrell/.rbenv/versions/2.6.1/lib/ruby/gems/2.6.0/gems/inspec-4.0.8/lib/resources/http.rb @ line 63 Inspec::Resources::Http#body:

    60:     def body
    61: require 'pry'
    62: binding.pry
 => 63:       @worker.body
    64:     end

[1] pry(#<#<Class:0x00007fb52c21c440>>)> 

@nellshamrell
Copy link
Author

nellshamrell commented Apr 18, 2019

This is where that error is being generated - in the instance_eval method
inspec/lib/inspec/rule.rb

     begin
        instance_eval(&block)
      rescue StandardError => e
        # We've encountered an exception while trying to eval the code inside the
        # control block. We need to prevent the exception from bubbling up, and
        # fail the control. Controls are failed by having a failed resource within
        # them; but since our control block is unsafe (and opaque) to us, let's
        # make a dummy and fail that.
        location = block.source_location.compact.join(':')
        describe 'Control Source Code Error' do
          # Rubocop thinks we are raising an exception - we're actually calling RSpec's fail()
          its(location) { fail e.message } # rubocop: disable Style/SignalException
      end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment