如何阅读厨师食谱中的加密数据包?

时间:2016-11-24 11:46:56

标签: ruby encryption chef databags

我试图从厨师食谱中读取加密数据包,但我遇到了一些问题。

我以这种方式创造了秘密

openssl rand -base64 512 > test/integration/default/encrypted_data_bag_secret
knife data bag from file test ../data_bags/system_user/poweruser.json --secret-file test/integration/default/encrypted_data_bag_secret
knife data bag show test ccbuildub -Fj > ../data_bags/test/poweruser.json

如您所见,我将加密版本放在一个单独的数据包中以进行测试。得到的数据库就像这样:

{
  "id": "poweruser",
  "username": {
    "encrypted_data": "48y/E3yzDWXvbD/J72tZuvS5jpDwnA8RvI6bJhnQHFU=\n",
    "iv": "O+WKxIAXH1pDTBqGluGCsw==\n",
    "version": 1,
    "cipher": "aes-256-cbc"
  },
  "password": {
    "encrypted_data": "JHHSoaIm9b8FxCOfTfrQRb0YmoabCcTcXXx/0nwU/j3gQCXd1kEHbxpEHZMB\n92A6\n",
    "iv": "Cn5ct0xt4PKHptz+WmbFmw==\n",
    "version": 1,
    "cipher": "aes-256-cbc"
  },
  "domain": {
    "encrypted_data": "bhFJ9FRTlIuo1s7JZEs79SkNSxDw+9OJtBxkpMdoxOorYqxBbo6lZA3+BP3u\njWk1\n",
    "iv": "BvOD0l4ohTwOJka74rlFVw==\n",
    "version": 1,
    "cipher": "aes-256-cbc"
  }
}

然后我使用这个片段处理我的食谱:

ruby_block "do something" do
    block do
        secret = Chef::EncryptedDataBagItem.load_secret(Chef::Config[:encrypted_data_bag_secret])
        decryptedObject = Chef::EncryptedDataBagItem.load("test","poweruser")
        print "DECRYPT : " + decryptedObject.to_s
       [...]
    end
end

当我尝试使用此配方运行厨房时,我收到此错误:

[2016-11-24T11:32:23+00:00] ERROR: ruby_block[do something] (SPMLinuxBuilder::mountTiny line 11) had an error: Net::HTTPServerException: 404 "Not Found"

Stacktrace本身并没有真正起作用:

>>>> Caused by Net::HTTPServerException: 404 "Not Found"
/opt/chef/embedded/lib/ruby/2.3.0/net/http/response.rb:120:in `error!'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/http.rb:150:in `request'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/http.rb:115:in `get'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/data_bag_item.rb:155:in `load'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/encrypted_data_bag_item.rb:123:in `load'
/tmp/kitchen/cache/cookbooks/SPMLinuxBuilder/recipes/mountTiny.rb:14:in `block (2 levels) in from_file'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/provider/ruby_block.rb:35:in `block in action_run'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/mixin/why_run.rb:52:in `add_action'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/provider.rb:176:in `converge_by'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/provider/ruby_block.rb:34:in `action_run'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/provider.rb:145:in `run_action'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/resource.rb:622:in `run_action'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/runner.rb:69:in `run_action'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/runner.rb:97:in `block (2 levels) in converge'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/runner.rb:97:in `each'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/runner.rb:97:in `block in converge'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/resource_collection/resource_list.rb:94:in `block in execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/resource_collection/stepable_iterator.rb:114:in `call_iterator_block'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/resource_collection/stepable_iterator.rb:85:in `step'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/resource_collection/stepable_iterator.rb:103:in `iterate'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/resource_collection/stepable_iterator.rb:55:in `each_with_index'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/resource_collection/resource_list.rb:92:in `execute_each_resource'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/runner.rb:96:in `converge'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/client.rb:669:in `block in converge'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/client.rb:664:in `catch'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/client.rb:664:in `converge'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/client.rb:703:in `converge_and_save'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/client.rb:283:in `run'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application.rb:294:in `block in fork_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application.rb:282:in `fork'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application.rb:282:in `fork_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application.rb:247:in `block in run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/local_mode.rb:44:in `with_server_connectivity'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application.rb:235:in `run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application/client.rb:464:in `sleep_then_run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application/client.rb:451:in `block in interval_run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application/client.rb:450:in `loop'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application/client.rb:450:in `interval_run_chef_client'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application/client.rb:434:in `run_application'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application.rb:59:in `run'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/lib/chef/application/solo.rb:226:in `run'
/opt/chef/embedded/lib/ruby/gems/2.3.0/gems/chef-12.16.42/bin/chef-solo:25:in `<top (required)>'
/opt/chef/bin/chef-solo:54:in `load'
/opt/chef/bin/chef-solo:54:in `<main>'[vagrant@default-centos-72 ~]$ exit

我知道秘密是正确的(如果我打印出来它包含我的秘密文件的完全相同的值)并且数据包是正确的(如果我尝试使用search( :test, "id:poweruser").first我获取加密值)

我可以尝试获取有关错误的更多信息并解决它?

谢谢, 米歇尔。

2 个答案:

答案 0 :(得分:1)

  1. 为什么要读取ruby_block中的数据包?您必须从主要上下文中读取它,如https://docs.chef.io/data_bags.html#load-with-recipe-dsl

  2. 中所述
  3. 404错误说,服务器上的数据包不存在。 (可能由#1引起)

  4. 如果使用默认的机密文件位置,则读取加密数据包是完全透明的。您可以坚持使用data_bagdata_bag_item方法。

  5.   

    如果未指定'secret',chef-client将在client.rb文件中的encrypted_data_bag_secret设置指定的路径中查找秘密。

    请参阅https://docs.chef.io/data_bags.html

答案 1 :(得分:0)

您可以从厨师食谱中读取数据包,如下所示:Chef::EncryptedDataBagItem.load('data_bag_name','data_bag_item_name')