Hi
Short version: Why can Chef decrypt an encrypted data bag item only when the secret has been read with Chef::EncryptedDataBagItem.load_secret('secret_file')
and not, when IO.read('secret_file')
has been used?
Long version…
According to the data bags documentation, it should be possible to do this:
data_bag_item('bag', 'item', IO.read('secret_file'))
But when I do exactly this in my recipe, Chef is unable to decrypt the data bag; in the log, there’s then this:
[2017-08-31T13:37:06+02:00] ERROR: Error decrypting data bag value: invalid hmac. Most likely the provided key is incorrect
It seems, that I have to use this instead:
data_bag_item('bag', 'item', Chef::EncryptedDataBagItem.load_secret('secret_file'))
Ie. Chef::EncryptedDataBagItem.load_secret()
vs. IO.read()
.
You can find the pretty small source code at
There are two branches: master with data_bag_item('bag', 'item', Chef::EncryptedDataBagItem.load_secret('secret_file'))
and secret_io_read with data_bag_item('bag', 'item', IO.read('secret_file'))
.
I’m using Chef Zero (local mode) v12.21.4 on Ubuntu 16.04. Chef is invoked like so:
sudo /usr/bin/chef-client -j /opt/addale-kitchen/nodes/system.json -c /opt/addale-kitchen/client.rb
You need to copy the secret file to /etc/chef/ew.icinga-client.secret
(this is just dummy data, so I don’t care that the secret has been made public now).
In my debug recipe, I write out the secret data to files /tmp/secret_io
and /tmp/secret_edbi
. Doing a diff
on those files reveals:
$ diff -U0 /tmp/secret_*
--- /tmp/secret_edbi 2017-08-31 13:23:47.051027793 +0200
+++ /tmp/secret_io 2017-08-31 13:23:47.059027793 +0200
@@ -21 +21 @@
-YkNpVkFSdnFCT2RESjhPZGFkc3ZZV3hqaEhsUVAyYmpTSGZNcWJwVQo=
\ No newline at end of file
+YkNpVkFSdnFCT2RESjhPZGFkc3ZZV3hqaEhsUVAyYmpTSGZNcWJwVQo=
In secret_edbi
(filled with Chef::EncryptedDataBagItem…
), a newline is missing at the end of the file.
The encrypted data bag item has been created like so:
$ knife data bag create ew-icinga-client system --local-mode --config-option data_bag_path=./data_bags --secret-file ./.data_bags/ew-icinga-client.secret
On the system, where “kinfe data bag create …
” ran:
$ md5sum ./.data_bags/ew-icinga-client.secret
968daf308b1b26487277563b00a5f643 ./.data_bags/ew-icinga-client.secret
On the system, where chef-client ran:
$ md5sum /tmp/secret_*
c9a4357e60430cd04d93c1003dcb7441 /tmp/secret_edbi
968daf308b1b26487277563b00a5f643 /tmp/secret_io
So, the correct file/contents would’ve been /tmp/secret_io
, but when I use this content, the encrypted data bag item cannot be decrypted.
Why is that so?
Thanks,
Alexander