ssh_authorized_key.pp 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. # wrapper to have some defaults.
  2. define sshd::ssh_authorized_key(
  3. $ensure = 'present',
  4. $type = 'ssh-dss',
  5. $key = 'absent',
  6. $user = '',
  7. $target = undef,
  8. $options = 'absent',
  9. $override_builtin = undef
  10. ){
  11. if ($ensure=='present') and ($key=='absent') {
  12. fail("You have to set \$key for Sshd::Ssh_authorized_key[${name}]!")
  13. }
  14. $real_user = $user ? {
  15. false => $name,
  16. '' => $name,
  17. default => $user,
  18. }
  19. case $target {
  20. undef,'': {
  21. case $real_user {
  22. 'root': { $real_target = '/root/.ssh/authorized_keys' }
  23. default: { $real_target = "/home/${real_user}/.ssh/authorized_keys" }
  24. }
  25. }
  26. default: {
  27. $real_target = $target
  28. }
  29. }
  30. # The ssh_authorized_key built-in function (in 2.7.23 at least)
  31. # will not write an authorized_keys file for a mortal user to
  32. # a directory they don't have write permission to, puppet attempts to
  33. # create the file as the user specified with the user parameter and fails.
  34. # Since ssh will refuse to use authorized_keys files not owned by the
  35. # user, or in files/directories that allow other users to write, this
  36. # behavior is deliberate in order to prevent typical non-working
  37. # configurations. However, it also prevents the case of puppet, running
  38. # as root, writing a file owned by a mortal user to a common
  39. # authorized_keys directory such as one might specify in sshd_config with
  40. # something like
  41. # 'AuthorizedKeysFile /etc/ssh/authorized_keys/%u'
  42. # So we provide a way to override the built-in and instead just install
  43. # via a file resource. There is no additional security risk here, it's
  44. # nothing a user can't already do by writing their own file resources,
  45. # we still depend on the filesystem permissions to keep things safe.
  46. if $override_builtin {
  47. $header = "# HEADER: This file is managed by Puppet.\n"
  48. if $options == 'absent' {
  49. info("not setting any option for ssh_authorized_key: ${name}")
  50. $content = "${header}${type} ${key}\n"
  51. } else {
  52. $content = "${header}${options} ${type} ${key}\n"
  53. }
  54. file { $real_target:
  55. ensure => $ensure,
  56. content => $content,
  57. owner => $real_user,
  58. mode => '0600',
  59. }
  60. } else {
  61. if $options == 'absent' {
  62. info("not setting any option for ssh_authorized_key: ${name}")
  63. } else {
  64. $real_options = $options
  65. }
  66. ssh_authorized_key{$name:
  67. ensure => $ensure,
  68. type => $type,
  69. key => $key,
  70. user => $real_user,
  71. target => $real_target,
  72. options => $real_options,
  73. }
  74. }
  75. }