Encrypted Data Bags in Chef
Theres a little more to using encrypted data bags with Chef than meets the eye.
This post is using chef zero and Windows, although Linux shouldn’t be too different.
- Prerequisites
- Creating a data bags
- Creating and managing your secret key (on windows)
- Version mismatches
Prerequisites
- ChefDK
- Somewhere to provision machines
That’s about it really - This post doesn’t cover provisioning, but I have another post which does here.
Creating a data bags
You can use knife to setup your encrypted data bags
Knife Zero
If you’re like me and prefer to develop against chef-zero, you can use knife-zero with the -z
option load stuff into your data bags. IF you need any configs (which sadly it doesn’t seem you can pass in via command line), you can just write a knife.rb
and point your knife to that config with the -c <my file>
option. I use knifezero.rb
as the filename since it’s now clear that that’s the config for my knife zero.
Creating and loading the data into the data bag
The bag looks like:
The knifezero.rb
looks like:
This sets the encryption scheme to version 2.
Creating and managing your secret key (on windows)
Creating a key
You can simply use the command below froma Chef Development Kit Powershell session. ChefDK has an embedded OpenSSL which it uses.
Keep this key safe and secret - no checking into VC or public access, etc.
Copying the key
Version mismatches
Knife defaults to version 1 encrypted data bags where as chef client defaults to version 2. Your knife.rb sets knife to upgrade to version 2 encrypted data bags.
If you have a version mismatch, Chef infuriatingly reports “Bad Decrypt” and suggests that maybe you have a bad key.
If you get such an error and you’re pretty sure that the key is in fact correct, chances are you actually have a version mismatch on your