87fe4e34c189d0263f24ca746af559848b97e41c
[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', 'absent' or 'refreshed'. Using 'refreshed' will make keys auto
19 #   update when they have expired (assuming a new key exists on the key server).
20 #
21 # @param content
22 #   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.
23 #
24 # @param source
25 #   Specifies the location of an existing GPG key file to copy. Valid options: a string containing a URL (ftp://, http://, or https://) or 
26 #   an absolute path.
27 #
28 # @param server
29 #   Specifies a keyserver to provide the GPG key. Valid options: a string containing a domain name or a full URL (http://, https://,
30 #   hkp:// or hkps://). The hkps:// protocol is currently only supported on Ubuntu 18.04.
31 #
32 # @param weak_ssl
33 #    Specifies whether strict SSL verification on a https URL should be disabled. Valid options: true or false.
34 #
35 # @param options
36 #   Passes additional options to `apt-key adv --keyserver-options`.
37 #
38 define apt::key (
39   Pattern[/\A(0x)?[0-9a-fA-F]{8}\Z/, /\A(0x)?[0-9a-fA-F]{16}\Z/, /\A(0x)?[0-9a-fA-F]{40}\Z/] $id                        = $title,
40   Enum['present', 'absent', 'refreshed'] $ensure                                                                        = present,
41   Optional[String] $content                                                                                             = undef,
42   Optional[Pattern[/\Ahttps?:\/\//, /\Aftp:\/\//, /\A\/\w+/]] $source                                                   = undef,
43   Pattern[/\A((hkp|hkps|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?(\/[a-zA-Z\d\-_.]+)*\/?$/] $server = $::apt::keyserver,
44   Boolean $weak_ssl                                                                                                     = false,
45   Optional[String] $options                                                                                             = $::apt::key_options,
46   ) {
47
48   case $ensure {
49     /^(refreshed|present)$/: {
50       if defined(Anchor["apt_key ${id} absent"]){
51         fail(translate('key with id %{_id} already ensured as absent'), {'_id' => id})
52       }
53
54       if !defined(Anchor["apt_key ${id} present"]) {
55         apt_key { $title:
56           ensure   => present,
57           refresh  => $ensure == 'refreshed',
58           id       => $id,
59           source   => $source,
60           content  => $content,
61           server   => $server,
62           weak_ssl => $weak_ssl,
63           options  => $options,
64         } -> anchor { "apt_key ${id} present": }
65
66         case $facts['os']['name'] {
67           'Debian': {
68             if versioncmp($facts['os']['release']['major'], '9') >= 0 {
69               ensure_packages(['gnupg'])
70               Apt::Key<| title == $title |>
71             }
72           }
73           'Ubuntu': {
74             if versioncmp($facts['os']['release']['full'], '17.04') >= 0 {
75               ensure_packages(['gnupg'])
76               Apt::Key<| title == $title |>
77             }
78           }
79           default: { }
80         }
81       }
82     }
83
84     absent: {
85       if defined(Anchor["apt_key ${id} present"]){
86         fail(translate('key with id %{_id} already ensured as present', {'_id' => id}))
87       }
88
89       if !defined(Anchor["apt_key ${id} absent"]){
90         apt_key { $title:
91           ensure   => $ensure,
92           id       => $id,
93           source   => $source,
94           content  => $content,
95           server   => $server,
96           weak_ssl => $weak_ssl,
97           options  => $options,
98         } -> anchor { "apt_key ${id} absent": }
99       }
100     }
101
102     default: {
103       fail translate('Invalid \'ensure\' value \'%{_ensure}\' for apt::key', {'_ensure' => ensure})
104     }
105   }
106 }