(FM-7318) - Added Puppet Strings formatting to documentation
[puppet-modules/puppetlabs-apt.git] / manifests / key.pp
1 # @summary Manages the GPG keys that Apt uses to authenticate packages. 
2 #
3 # @note 
4 #   The apt::key defined type makes use of the apt_key type, but includes extra functionality to help prevent duplicate keys.
5 #
6 # @example Declare Apt key for apt.puppetlabs.com source
7 #   apt::key { 'puppetlabs':
8 #     id      => '6F6B15509CF8E59E6E469F327F438280EF8D349F',
9 #     server  => 'hkps.pool.sks-keyservers.net',
10 #     options => 'http-proxy="http://proxyuser:proxypass@example.org:3128"',
11 #   }
12 #
13 # @param id
14 #   Specifies a GPG key to authenticate Apt package signatures. Valid options: a string containing a key ID (8 or 16 hexadecimal 
15 #   characters, optionally prefixed with "0x") or a full key fingerprint (40 hexadecimal characters).
16 #
17 # @param ensure
18 #   Specifies whether the key should exist. Valid options: 'present' and 'absent'.
19 #
20 # @param content
21 #   Supplies the entire GPG key. Useful in case the key can't be fetched from a remote location and using a file resource is inconvenient.
22 #
23 # @param source
24 #   Specifies the location of an existing GPG key file to copy. Valid options: a string containing a URL (ftp://, http://, or https://) or 
25 #   an absolute path.
26 #
27 # @param server
28 #   Specifies a keyserver to provide the GPG key. Valid options: a string containing a domain name or a full URL (http://, https://, or 
29 #   hkp://).
30 #
31 # @param options
32 #   Passes additional options to `apt-key adv --keyserver-options`.
33 #
34 define apt::key (
35     String $id                           = $title,
36     Enum['present', 'absent'] $ensure    = present,
37     Optional[String] $content            = undef,
38     Optional[String] $source             = undef,
39     String $server                       = $::apt::keyserver,
40     Optional[String] $options            = undef,
41     ) {
42
43   assert_type(
44     Pattern[
45       /\A(0x)?[0-9a-fA-F]{8}\Z/,
46       /\A(0x)?[0-9a-fA-F]{16}\Z/,
47       /\A(0x)?[0-9a-fA-F]{40}\Z/,
48     ], $id)
49
50   if $source {
51     assert_type(Pattern[/\Ahttps?:\/\//, /\Aftp:\/\//, /\A\/\w+/], $source)
52   }
53
54   if $server {
55     assert_type(Pattern[/\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$/], $server)
56   }
57
58   case $ensure {
59     present: {
60       if defined(Anchor["apt_key ${id} absent"]){
61         fail("key with id ${id} already ensured as absent")
62       }
63
64       if !defined(Anchor["apt_key ${id} present"]) {
65         apt_key { $title:
66           ensure  => $ensure,
67           id      => $id,
68           source  => $source,
69           content => $content,
70           server  => $server,
71           options => $options,
72         } -> anchor { "apt_key ${id} present": }
73
74         case $facts['os']['name'] {
75           'Debian': {
76             if versioncmp($facts['os']['release']['major'], '9') >= 0 {
77               ensure_packages(['dirmngr'])
78               Apt::Key<| title == $title |>
79             }
80           }
81           'Ubuntu': {
82             if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
83               ensure_packages(['dirmngr'])
84               Apt::Key<| title == $title |>
85             }
86           }
87           default: { }
88         }
89       }
90     }
91
92     absent: {
93       if defined(Anchor["apt_key ${id} present"]){
94         fail("key with id ${id} already ensured as present")
95       }
96
97       if !defined(Anchor["apt_key ${id} absent"]){
98         apt_key { $title:
99           ensure  => $ensure,
100           id      => $id,
101           source  => $source,
102           content => $content,
103           server  => $server,
104           options => $options,
105         } -> anchor { "apt_key ${id} absent": }
106       }
107     }
108
109     default: {
110       fail "Invalid 'ensure' value '${ensure}' for apt::key"
111     }
112   }
113 }