key.pp 3.4 KB

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