【问题标题】:inspec resources not identified检查未识别的资源
【发布时间】:2018-06-04 06:50:27
【问题描述】:

我正在使用带有 ruby​​ 的 inspec 测试框架进行基础设施测试。我在控件中写了一个测试

这是我的测试:

require 'aws-sdk'

credentials = Aws::AssumeRoleCredentials.new(
   role_arn: 'some_value',
   role_session_name: 'pipeline')

client_params = {
  region: 'ap-southeast-2',
  credentials: credentials
}

ec2_client = Aws::EC2::Resource.new(client_params)
instance = ec2_client.instances(filters: [{name:'tag:component', values: ['api', 'fxsnet', 'admin']}])


puts "ec2 Client is : #{ec2_client}"
puts "list of instances based on tag is: #{instance}"


instance.each do |i| 
  puts 'ID:    ' + i.id
  puts 'State: ' + i.state.name

#for each of the instance check if tmp file exist 
  describe file('/tmp') do                  # The actual test
    it { should exist }
  end 
end

但在执行时,我得到以下错误

An error occurred while loading ./inspec-infra-tests/controls/apiInstances.rb.
Failure/Error:
  describe file('/tmp') do                  # The actual test
    it { should exist }
  end

NoMethodError:
  undefined method `file' for main:Object
  Did you mean?  fail
# ./inspec-infra-tests/controls/apiInstances.rb:46:in `block in <top (required)>'
# ./inspec-infra-tests/controls/apiInstances.rb:35:in `<top (required)>'
No examples found.

0 examples, 0 failures, 0 passed

file InSpec 审计资源,用于测试所有系统文件类型,包括文件、目录、符号链接、命名管道、套接字等。

#InspecWithRuby #inspec #inspecResourcesNotIdentified #InspecResourcesNotFound 

【问题讨论】:

  • 你有什么问题?
  • undefined method 'file' ... 错误信息很清楚!你的测试应该做什么?你想测试什么方法?我投票结束这个问题,因为你不清楚你在问什么,因为你实际上只是发布了一条错误消息。
  • 我已经添加了测试......我正在尝试根据某些标签(如 api、fxsnet)在 aws 上获取 ec2 上的实例,然后尝试检查我是否可以运行任何命令他们喜欢在这里检查 /tmp 文件是否存在。我希望它现在清楚
  • 我仍然不知道该测试应该做什么,抱歉。暂时忘记所有其余的代码,只看一个单独的方法调用:file('/tmp')。该方法调用没有使用Aws::EC2::Resource 库。它应该做什么?
  • 也许应该是i.file('/tmp'),还是什么?不管它应该是什么,它应该在it 块内,而不是describe 行,但我不可能向您展示“工作”代码,直到您弄清楚该方法调用应该做什么。

标签: ruby infrastructure inspec


【解决方案1】:

在我看来,您正试图通过使用ruby 执行而不是使用inspec exec 运行来运行inspec 测试。我可以通过将您的测试粘贴到文件中来在本地重现:

inspec_example.rb

  describe file('/tmp') do                  # The actual test
    it { should exist }
  end

直接用 ruby​​ 执行

ruby inspec_example.rb 给出:

Traceback (most recent call last):
inspec_example.rb:1:in `<main>': undefined method `file' for main:Object (NoMethodError)
Did you mean?  fail

使用inspec exec 执行按预期工作:

inspec exec inspec_example.rb 给出:

Profile: tests from inspec_example.rb (tests from inspec_example.rb)
Version: (not specified)
Target:  local://

  File /tmp
     ✔  should exist

【讨论】:

  • 是的,您的简单 InSpec 测试确实测试了该文件;但是,您可以将 Ruby 代码添加到 InSpec 测试中,以便遍历一组文件并测试它们的存在,我怀疑这是 OP 试图做的。 InSpec 和 Ruby 不必像您所暗示的那样是非此即彼的;它们可以是/和。
  • @james.garriss 我从来没有暗示过这样的事情。我说直接使用ruby 而不是inspec exec 执行测试文件会导致给定的错误。在给出我的答案后,该问题也被编辑以包含他们的示例。以前它只包括看起来好像他们只是试图运行简单测试的错误。
【解决方案2】:

我不熟悉 AWS 开发工具包,但如果您想使用 InSpec 测试一组文件,您可以这样做:

myfiles = %w(temp.err temp.out)

control 'tmp-files-1' do
  title 'Test for a set of temp files.'
  myfiles.each do |myfile|
    describe file('/tmp/' + myfile) do
      it { should exist }
    end
  end
end

如果你执行这个控件,它将返回以下内容(假设这些文件实际存在于你的/tmp文件夹中:

  ✔  tmp-files-1: Test for a set of temp files.
     ✔  File /tmp/temp.err should exist
     ✔  File /tmp/temp.out should exist

我希望您可以采用此示例并使其适应您的 AWS 需求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-29
    • 2012-08-15
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-10
    相关资源
    最近更新 更多