Serialize SSH and RSA private keys to store them in a Chef Data Bag

To send RSA private keys to instances, store them in encrypted data bags. The data bag item is a JSON file that contains keys and values inline.

To place the multi-line RSA key into the value part of the JSON file, we need to replace the new line characters with the “\n” text.

In Atom

on Mac and Windows

  1. Open the RSA key file in Atom,
  2. Press Command-F on Mac, Ctrl-F on Windows to open the Find and Replace window,
  3. On the right side click the Use Regex button,
  4. In the search field enter
    \r\n
  5. In the replace with field enter
    \\n
  6. Press the Replace All button

In Visual Studio Code

on Mac and Windows

  • Press Command-F on Mac, Ctrl-F on Windows to open the Find dialog
  • Select the Use Regular Expression button
  • Enter \n into the find, \\n into the replace field

In Notepad++

on Windows

  • Open the RSA key file in Notepad++,
  • In the Search menu select Replace…,
  • Select Extended mode in the Search Mode section,
  • Enter \r\n to the Find what text box ( if the key was generated on a Windows computer using GitBash, search for \n )
  • Enter \\n  to the Replace with text box
  • Press the Replace All button

You can place the single line key into any encrypted Data Bag file. See Data Bags on Data Bag encryption.

Leave a comment

Leave a Reply