Merge pull request #323 from cmurphy/master
[puppet-modules/puppetlabs-apt.git] / manifests / key.pp
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
60   validate_re($key, ['\A(0x)?[0-9a-fA-F]{8}\Z', '\A(0x)?[0-9a-fA-F]{16}\Z'])
61   validate_re($ensure, ['\Aabsent|present\Z',])
62
63   if $key_content {
64     validate_string($key_content)
65   }
66
67   if $key_source {
68     validate_re($key_source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+'])
69   }
70
71   if $key_server {
72     validate_re($key_server,['\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,4})?$'])
73   }
74
75   if $key_options {
76     validate_string($key_options)
77   }
78
79   case $ensure {
80     present: {
81       if defined(Anchor["apt_key ${key} absent"]){
82         fail("key with id ${key} already ensured as absent")
83       }
84
85       if !defined(Anchor["apt_key ${key} present"]) {
86         apt_key { $title:
87           ensure            => $ensure,
88           id                => $key,
89           source            => $key_source,
90           content           => $key_content,
91           server            => $key_server,
92           keyserver_options => $key_options,
93         } ->
94         anchor { "apt_key ${key} present": }
95       }
96     }
97
98     absent: {
99       if defined(Anchor["apt_key ${key} present"]){
100         fail("key with id ${key} already ensured as present")
101       }
102
103       if !defined(Anchor["apt_key ${key} absent"]){
104         apt_key { $title:
105           ensure            => $ensure,
106           id                => $key,
107           source            => $key_source,
108           content           => $key_content,
109           server            => $key_server,
110           keyserver_options => $key_options,
111         } ->
112         anchor { "apt_key ${key} absent": }
113       }
114     }
115
116     default: {
117       fail "Invalid 'ensure' value '${ensure}' for apt::key"
118     }
119   }
120 }