key.pp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. # == Define: apt::key
  2. #
  3. # The apt::key defined type allows for keys to be added to apt's keyring
  4. # which is used for package validation. This defined type uses the apt_key
  5. # native type to manage keys. This is a simple wrapper around apt_key with
  6. # a few safeguards in place.
  7. #
  8. # === Parameters
  9. #
  10. # [*key*]
  11. # _default_: +$title+, the title/name of the resource
  12. #
  13. # Is a GPG key ID. This key ID is validated with a regex enforcing it
  14. # to only contain valid hexadecimal characters, be precisely 8 or 16
  15. # characters long and optionally prefixed with 0x.
  16. #
  17. # [*ensure*]
  18. # _default_: +present+
  19. #
  20. # The state we want this key in, may be either one of:
  21. # * +present+
  22. # * +absent+
  23. #
  24. # [*key_content*]
  25. # _default_: +undef+
  26. #
  27. # This parameter can be used to pass in a GPG key as a
  28. # string in case it cannot be fetched from a remote location
  29. # and using a file resource is for other reasons inconvenient.
  30. #
  31. # [*key_source*]
  32. # _default_: +undef+
  33. #
  34. # This parameter can be used to pass in the location of a GPG
  35. # key. This URI can take the form of a:
  36. # * +URL+: ftp, http or https
  37. # * +path+: absolute path to a file on the target system.
  38. #
  39. # [*key_server*]
  40. # _default_: +undef+
  41. #
  42. # The keyserver from where to fetch our GPG key. It can either be a domain
  43. # name or url. It defaults to
  44. # undef which results in apt_key's default keyserver being used,
  45. # currently +keyserver.ubuntu.com+.
  46. #
  47. # [*key_options*]
  48. # _default_: +undef+
  49. #
  50. # Additional options to pass on to `apt-key adv --keyserver-options`.
  51. define apt::key (
  52. $key = $title,
  53. $ensure = present,
  54. $key_content = undef,
  55. $key_source = undef,
  56. $key_server = undef,
  57. $key_options = undef,
  58. ) {
  59. validate_re($key, ['\A(0x)?[0-9a-fA-F]{8}\Z', '\A(0x)?[0-9a-fA-F]{16}\Z'])
  60. validate_re($ensure, ['\Aabsent|present\Z',])
  61. if $key_content {
  62. validate_string($key_content)
  63. }
  64. if $key_source {
  65. validate_re($key_source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+'])
  66. }
  67. if $key_server {
  68. validate_re($key_server,['\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,4})?$'])
  69. }
  70. if $key_options {
  71. validate_string($key_options)
  72. }
  73. case $ensure {
  74. present: {
  75. if defined(Anchor["apt_key ${key} absent"]){
  76. fail("key with id ${key} already ensured as absent")
  77. }
  78. if !defined(Anchor["apt_key ${key} present"]) {
  79. apt_key { $title:
  80. ensure => $ensure,
  81. id => $key,
  82. source => $key_source,
  83. content => $key_content,
  84. server => $key_server,
  85. keyserver_options => $key_options,
  86. } ->
  87. anchor { "apt_key ${key} present": }
  88. }
  89. }
  90. absent: {
  91. if defined(Anchor["apt_key ${key} present"]){
  92. fail("key with id ${key} already ensured as present")
  93. }
  94. if !defined(Anchor["apt_key ${key} absent"]){
  95. apt_key { $title:
  96. ensure => $ensure,
  97. id => $key,
  98. source => $key_source,
  99. content => $key_content,
  100. server => $key_server,
  101. keyserver_options => $key_options,
  102. } ->
  103. anchor { "apt_key ${key} absent": }
  104. }
  105. }
  106. default: {
  107. fail "Invalid 'ensure' value '${ensure}' for apt::key"
  108. }
  109. }
  110. }