Often I find I need to either write a public key to a machine using a chef attribute (sometimes it's even a private key). This happens for various different reasons while in development. It is definitely not the best solution and I would never suggest releasing a cookbook with this functionality. However, these things do happen and getting those keys into an attribute is reasonably straight forward.
For example lets take the following key:
-----BEGIN PUBLIC KEY-----
-----END PUBLIC KEY-----
I want to put this key into an attribute like so:
node['test_cookbook']['insecure']['my_key'] = 'someverylongkey'
The question here is, how do I escape the key? The answer is actually very simple. Make sure your key is in a file and run the following command:
chef exec ruby -e 'p ARGF.read' thekeyfile.txt
The output of this command should be suitable to store in your attribute and be written to a file without breaking the key.
This is not a good way to store keys. Please use a secret store for this (such as encrypted databags / Vault).
Ruby Developer specialising in Continuous Delivery and PaaS solutions