Mikyjpeg Mikyjpeg - 11 days ago 4
Ruby Question

How to read an encrypted data bag in a chef recipe?

I'm trying to read an encrypted data bag from a chef recipe but I'm experiencing some problems.

I created the secret in this way

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


As you can see I put the encrypted version in a separated data bag for testing purpose. The resulting databag is like this:

{
"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"
}
}


Then I'm working with my recipe with this snippet:

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


When I try to run the kitchen with this recipe I obtain this error:

[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 itself is not really helping:

>>>> 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


I know that the secret is correct (if I print it out it contains the exact same value of my secret file) and that the data bag is correct (if I try using
search( :test, "id:poweruser").first
I obtain the encrypted values)

What could I try to have more information about the error and resolve it?

Thanks,
Michele.

Answer
  1. Why do you want to read the data bag inside a ruby_block? You have to read it from the main context, as described in https://docs.chef.io/data_bags.html#load-with-recipe-dsl

  2. The 404 error says, that the data bag is not existant on the server. (might be caused by #1)

  3. Reading encrypted data bags is completely transparent, if you use the default secret file location. You can just stick to using the data_bag or data_bag_item methods.

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.

see https://docs.chef.io/data_bags.html