【问题标题】:Chef Inspec: undefined local variable or method `aws_region'Chef Inspec:未定义的局部变量或方法“aws_region”
【发布时间】:2019-11-08 17:49:23
【问题描述】:

我正在测试基本的Chef Inspec 代码。我正在使用 API 调用从 Mac 运行它:

inspec exec sg-disallow-ftp.rb -t aws://

这是配置文件的 Chef 代码:

title 'Test AWS Security Groups Across All Regions For an Account Disallow FTP'

control 'aws-multi-region-security-group-ftp-1.0' do

  impact 1.0
  title 'Ensure AWS Security Groups disallow FTP ingress from 0.0.0.0/0.'

  aws_region.region_names.each do |region|
    aws_security_groups(aws_region: region).group_ids.each do |security_group_id|
      describe aws_security_group(aws_region: region, group_id: security_group_id) do
        it { should exist }
        it { should_not allow_in(ipv4_range: '0.0.0.0/0', port: 21) }
      end
    end
  end
end

我收到此错误:

×  aws-multi-region-security-group-ftp-1.0: Ensure AWS Security Groups disallow FTP ingress from 0.0.0.0/0.
     ×  Control Source Code Error sg-disallow-ftp.rb:3 
     undefined local variable or method `aws_region' for #<#<Class:0x00007fc35a095158>:0x00007fc356ebd568>

【问题讨论】:

  • 看起来是 aws_region.region_names.each 在名为 aws_region 的范围内找不到任何东西。也许您缺少requiregem
  • 所以当你说需要的时候你能解释一下我是新来的inspec

标签: amazon-web-services chef-infra inspec


【解决方案1】:

所以最后发现我必须在配置文件目录中运行 inspect vendor --overwrite 并执行测试

【讨论】:

    【解决方案2】:

    您似乎缺少一些配置。

    我假设您创建了一个名为 awsinspec profile。您可以利用inspec init创建配置文件

    $ inspec init profile aws
    

    创建配置文件后,您需要指定 inspec-awsuse the resources 的依赖关系。您将在 aws/inspec.yml 文件中执行此操作,该文件如下所示:

    name: aws
    title: InSpec Profile
    maintainer: The Authors
    copyright: The Authors
    copyright_email: you@example.com
    license: Apache-2.0
    summary: An InSpec Compliance Profile
    version: 0.1.0
    supports:
      platform: aws
    inspec_version: '>= 4.6.9'
    depends:
      - name: inspec-aws
        url: https://github.com/inspec/inspec-aws/archive/v1.3.2.tar.gz
    

    你应该准备好了。

    您可以通过将inspec shell 与资源包结合使用来验证它是否有效,如下所示:

    $ inspec shell --depends aws -t aws://
    

    【讨论】:

    • 谢谢,这会试一试
    • 我尝试使用这个命令 inspec exec rodney-profile -t aws:// 执行配置文件,我得到这个“error/opt/inspec/embedded/lib/ruby/2.6.0/open -uri.rb:378:in `open_http': 404 Not Found (OpenURI::HTTPError)" 我在 MAC OS 上运行 inspec 4.18.24 版本。我使用 brew 安装了 Inspec
    • 我也尝试运行此命令,我得到了 inspec shell --depends ${name of profile} -t aws:// 并收到此错误消息 No file provider for the provided path
    • 因此,如果我从控制目录运行 chef inspec,我可以成功运行 inspec 测试。 inspec exec iam.rb -t aws:// 并执行得很好。但是尝试从配置文件运行是行不通的。 inspec exec iam -t aws:// 得到错误 404 Not Found (OpenUIR::HTTPError)
    • 我尝试运行一个简单的测试describe aws_regions do its('region_names') { should include 'eu-west-2' } end error undefined local variable or method `aws_regions' for #:0x00007f9b39
    猜你喜欢
    • 2017-01-26
    • 2018-04-19
    • 2015-01-01
    • 2013-12-17
    • 2016-04-29
    • 2018-04-21
    相关资源
    最近更新 更多