【问题标题】:Handling attributes in InSpec在 InSpec 中处理属性
【发布时间】:2019-01-29 23:11:59
【问题描述】:

我试图创建一些基本的检查测试来验证一组 HTTP URL。我开始的方式是这样的-

control 'http-url-checks' do
  impact 1.0
  title 'http-url-checks'
  desc '
   Specify the URLs which need to be up and working.
  '
  tag 'http-url-checks'

  describe http('http://example.com') do
    its('status') { should eq 200 }
    its('body') { should match /abc/ }
    its('headers.name') { should eq 'header' }
  end

  describe http('http://example.net') do
    its('status') { should eq 200 }
    its('body') { should match /abc/ }
    its('headers.name') { should eq 'header' }
  end
end

我们注意到,这些 URL 在控件中是硬编码的,并不好玩。我想将它们移动到某种“属性”文件中,并在控制文件中循环它们。

我的尝试是使用配置文件中的“文件”文件夹结构。我创建了一个文件 - httpurls.yml 并在其中包含以下内容 -

- url: http://example.com
- url: http://example.net

..在我的控制文件中,我有构造 -

  my_urls = yaml(content: inspec.profile.file('httpurls.yml')).params

  my_urls.each do |s|
    describe http(s['url']) do
      its('status') { should eq 200 }
    end
  end

但是,当我执行合规性配置文件时,我收到一个错误 - 'httpurls.yml not found'(虽然不确定确切的错误消息)。以下是我的合规配置文件的文件夹结构。

我做错了什么?

有没有更好的方法来实现我想要做的事情?

【问题讨论】:

  • 要取消标记chef,因为这似乎与作为项目的 Chef 无关。鉴于没有大量独立的 InSpec 用户,我强烈建议注册 Chef community slack 并将这个问题也放到 #inspec 频道中。
  • 当然。也尝试一下!
  • "虽然不确定确切的错误消息" ー所以在发布 SO 之前请确保并在问题中包含相关消息。・您在问题中发布的代码没有任何问题。
  • 实际上,@techraf 有,因为他是硬编码的值,他想改为变量。进行此更改将使他的 InSpec 配置文件更加灵活和可重用。也许这不是“错误”,但也可以说它并不完全“正确”。

标签: inspec


【解决方案1】:

秘诀是使用本页底部附近定义的配置文件属性:

https://www.inspec.io/docs/reference/profiles/

首先,创建一个配置文件属性 YML 文件。我的名字是profile-attribute.yml

其次,将值数组放入 YML 文件中,如下所示:

urls:
  - http://example.com
  - http://example.net

第三,在 InSpec 测试的顶部创建一个属性:

my_urls = attribute('urls', description: 'The URLs that I am validating.')

第四,在 InSpec 测试中使用您的属性:

my_urls.each do |s|
  describe http(s['url']) do
    its('status') { should eq 200 }
  end
end

最后,当您调用 InSpec 测试时,使用 --attrs 指向您的 YML 文件:

inspec exec mytest.rb --reporter=cli --attrs profile-attribute.yml

【讨论】:

    【解决方案2】:

    还有另一种使用文件的方法(而不是配置文件属性和--attrs 标志)。您可以使用 JSON 或 YAML。

    首先,创建 JSON 和/或 YAML 文件并将它们放在 files 目录中。 JSON 文件的一个简单示例可能如下所示:

    {
        "urls": ["https://www.google.com", "https://www.apple.com"]
    }
    

    YAML 文件的一个简单示例可能如下所示:

    urls:
    - https://www.google.com
    - https://www.apple.com
    

    其次,在 InSpec 文件顶部包含代码以读取和解析 JSON 和/或 YAML,如下所示:

    jsoncontent = inspec.profile.file("tmp.json")
    jsonparams = JSON.parse(jsoncontent)
    jsonurls = jsonparams['urls']
    
    yamlcontent = inspec.profile.file("tmp.yaml")
    yamlparams = YAML.load(yamlcontent)
    yamlurls = yamlparams['urls']
    

    第三,在 InSpec 测试中使用变量,如下所示:

    jsonurls.each do |jsonurl|
      describe http(jsonurl) do
        puts "json url is " + jsonurl
        its('status') { should eq 200 }
      end
    end
    
    yamlurls.each do |yamlurl|
      describe http(yamlurl) do
        puts "yaml url is " + yamlurl
        its('status') { should eq 200 }
      end
    end
    

    (注意:puts 行用于调试。)

    结果如你所愿:

    json url is https://www.google.com
    json url is https://www.apple.com
    yaml url is https://www.google.com
    yaml url is https://www.apple.com
    
    Profile: InSpec Profile (inspec-file-test)
    Version: 0.1.0
    Target:  local://
    
      http GET on https://www.google.com
         ✔  status should eq 200
      http GET on https://www.apple.com
         ✔  status should eq 200
      http GET on https://www.google.com
         ✔  status should eq 200
      http GET on https://www.apple.com
         ✔  status should eq 200
    

    【讨论】:

      猜你喜欢
      • 2010-11-01
      • 2014-06-05
      • 1970-01-01
      • 2012-03-20
      • 1970-01-01
      • 1970-01-01
      • 2022-11-11
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多