RSA public/private keys in YAML

You can store your keys as text ("ASCII-armored" / base 64 encoded). From Wikipedia, the syntax for multiline strings in YAML is:

- title: An example multi-line string in YAML
  body : |
    This is a multi-line string.
    "special" metacharacters may
    appear here. The extent of this string is
    indicated by indentation.

You can add the ssh key in your yaml file like below -

---
ssh-key: |-
  -----BEGIN OPENSSH PRIVATE KEY-----
  b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAABFwAAAAdzc2gtcn
  NhAAAAAwEAAQAAAQEAssBRe91wZ0TJBIWK2V1NH/ourcFPb0cA4ln32a3j5QITMS3zhs/o
  euh8jPJ9eca93B+mfep5ly/UjwmDctGbwX54sJngE4Vuv5FgqctR8oHTxV+V18UdolBSsy
  yiAVycGUexN2yz7P5JBzwfOG3WEwNe4dNVzmFj51nXAlaX+MB+wLfrZfU1vQpqmU8Esiu+
  Hdab948qhmGlMepBw+M4Z7wVfgfz855ywxgL3NrVk2WhXxE9ng/jTLjHKkxKE/3sM/81wt
  bvjejPvEMeFPD2XXBZSPi7TpgOiMBWDPXUwbrDYH6S6J2HAvYgwDm1pdQZGrOLrTBVJJE/
  DX1KpYZJzQAAA9DSZOo10mTqNQAAAAdzc2gtcnNhAAABAQCywFF73XBnRMkEhYrZXU0f+i
  6twU9vRwDiWffZrePlAhMxLfOGz+h66HyM8n15xr3cH6Z96nmXL9SPCYNy0ZvBfniwmeBP
  hW6/kWCpy1HygdPFX5XXxR2iUFKzLKIBXJwZR7E3bLPs/kkHPB84bdYTA17h01XOYWPnWd
  cCVpf4wH7At+tl9TW9CmqZTwSyK74d1pv3jyqGYaVN6kHD4zhnvBV+B/PznnLDGAvc2tWT
  ZaFfET2eD+NMuMcqTEoT/ewz/zXC1u+N6M+8Qx4U8PZdcFlI+LtOmA6IwFYM9dTBusNgfp
  LonYcC9iDAObWl1Bkas4itMFUkkT8NfUqlhknNAAAAAwEAAQAAAQBN1kUlROX/cgp+t5Ag
  2uoMtKrC6tymPir6ZebxmTEVtfOZhML4v2wiqT4jOiy9bHecdQPQ7NuJpEBREPl2dCP4/B
  OeA0OUHSx+qtWG2oySp0oKNndPf/xJg+SfNR5OrX8j2v4mfmVTG9+9EMcfkWSY3uzgNWC1
  /967DXn9AKwomx8yszA7YY0vKanLPx5C14WtzMPSbfwYZoKV4ddBHAF/7JHXAXxMisc9Ud
  kziaS8SV4YJt7gSYKKMvzOEj+uiyk9DKoYWf6t++SQ93CUnZKLfhwYTUx/rsYt6ubblQeP
  IHI/j8LKiVz6nvyDt2NXSJ2Z2j0s6roREYgnLaqbjlPZAAAAgQC6DgqTehb2XTrg69D8lt
  Lyo5sutB3bTIHyg6GBSBW2qYh3D2PQaWdrcYe7WYGtp6OGmTgGcXX0DBCCTtrwTdxsPVeL
  XhC/HBY271v9T18Ur4h310iJWVPJ8I7TgJuaSsfui/04NcqcW5XwFy6DHDQKxNwDhEscwg
  wIaUrd8UYfkAAAAIEA552RMzZ5OCAEryh1OXnV3EeqhumsCbET6dpDOpSQnHss7u3CZ8d6
  2LwHQJ/fjwDcrMYwEUwDkNoZjhEmj1e5LVTLjRS02VBgjg7RnphpuaphPZ+CDNlq3Om5C9
  xW96+4eC9/T7SRaspF3FxhgtPUMI1beu1QnpL0jduT/GQSqaMAAACBAMWR+CLktU6cTBH7
  RnfnB2K7E8slA8/hSGUZJ35JXJj5XujQgaf2d8hi4Lmt8smBojaERlCxxx3B9hWVYRkwCM
  C8YRNCLnBgR2CCp27D0wuadL9aFITlx91GPytF9BKxzy949VaF6SEw9M86oouj362u/BvP
  CO7Hnjlg77HRNFXPAAAAFWxrYW1pcmVkZHlAdm13YXJlLmNvbQECAwQF
  -----END OPENSSH PRIVATE KEY-----

Please note that the SSH key is indented one level.


As of 27-Oct-2016, this is the first result in Google when searching for "yaml rsa key", so I would like to add an answer on the specific syntax needed for RSA keys in Yaml.

If you include the key in a single line in the yaml file, there are no issues. If you want to split it in different lines for readability and you cannot accept newlines in the generated string, the only option seems to be double quotes with escape codes.

In my case, I needed a generated single-line, no-spaces string, since the key definition was being used by a templating engine to insert the key into a script variable. The following syntax will join each line with no spaces (please note the '\' on each line for removing the newlines):

    yourKey: "-----BEGIN PUBLIC KEY-----\
      xxx...\
      yyy...\
      zzz...\
      -----END PUBLIC KEY-----"

    result: "-----BEGIN PUBLIC KEY-----xxx...yyy...zzz...-----END PUBLIC KEY-----"

A very good reference on the formats for literals can be found here on SO: https://stackoverflow.com/a/21699210/4228798

Tags:

Key

Rsa

Yaml