如何读取厨师食谱中的加密数据包?
How to read an encrypted data bag in a chef recipe?
我正在尝试从厨师食谱中读取加密数据包,但我遇到了一些问题。
我是这样创建的secret
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
我获得加密值)
我可以尝试什么来获得有关错误的更多信息并解决它?
谢谢,
米歇尔
为什么要读取一个ruby_block里面的数据包?您必须从主要上下文中阅读它,如 https://docs.chef.io/data_bags.html#load-with-recipe-dsl
中所述
404错误表示,数据包在服务器上不存在。 (可能是#1引起的)
读取加密数据包是完全透明的,如果你使用默认的秘密文件位置。您可以坚持使用 data_bag
或 data_bag_item
方法。
If 'secret' is not specified, the chef-client will look for a secret at the path specified by the encrypted_data_bag_secret setting in the client.rb file.
您可以从厨师食谱中读取数据包如下:Chef::EncryptedDataBagItem.load('data_bag_name','data_bag_item_name')
我正在尝试从厨师食谱中读取加密数据包,但我遇到了一些问题。
我是这样创建的secret
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
我获得加密值)
我可以尝试什么来获得有关错误的更多信息并解决它?
谢谢, 米歇尔
为什么要读取一个ruby_block里面的数据包?您必须从主要上下文中阅读它,如 https://docs.chef.io/data_bags.html#load-with-recipe-dsl
中所述
404错误表示,数据包在服务器上不存在。 (可能是#1引起的)
读取加密数据包是完全透明的,如果你使用默认的秘密文件位置。您可以坚持使用
data_bag
或data_bag_item
方法。
If 'secret' is not specified, the chef-client will look for a secret at the path specified by the encrypted_data_bag_secret setting in the client.rb file.
您可以从厨师食谱中读取数据包如下:Chef::EncryptedDataBagItem.load('data_bag_name','data_bag_item_name')