Chef InSpec check an http request is failing [successfully]

447 views Asked by At

I am writing some infrastructure testing in Chef InSpec & am not sure how to go about testing that a url is not accessible publicly. I have the following code snippet which I am currently using

environments = {
  :ops => "ops",
}

control "verify-not-accessible-publicly" do
  impact 1.0
  title "verify we are not publicly accessible"
  environments.each do |_, env|
    uri = "http://#{env}.internal.example.com"
    begin
      result = http(uri, ssl_verify: true, open_timeout: 2, read_timeout: 5, max_redirects: 0)
    rescue => e
      unless e.class == Faraday::ConnectionFailed
        raise e
      end
    end
  end
end

This isn't working quite like I expect. I don't think the http(uri,...) block is actually executed until it is passed into a describe function.

Thanks

1

There are 1 answers

0
Mr. On

you should use http resource with a describe block and matchers

describe http('url', auth: {user: 'user', pass: 'test'}, params: {params}, method: 'method', headers: {headers}, data: data, open_timeout: 60, read_timeout: 60, ssl_verify: true, max_redirects: 3) do
  its('status') { should eq number }
  its('body') { should eq 'body' }
  its('headers.name') { should eq 'header' }
end