apt::key: Rename $key to $id to match apt_key.
[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 # [*id*]
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 # [*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 # [*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 # [*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 # [*options*]
50 #   _default_: +undef+
51 #
52 #   Additional options to pass on to `apt-key adv --keyserver-options`.
53 define apt::key (
54   $id      = $title,
55   $ensure  = present,
56   $content = undef,
57   $source  = undef,
58   $server  = undef,
59   $options = undef,
60 ) {
61
62   validate_re($id, ['\A(0x)?[0-9a-fA-F]{8}\Z', '\A(0x)?[0-9a-fA-F]{16}\Z', '\A(0x)?[0-9a-fA-F]{40}\Z'])
63   validate_re($ensure, ['\Aabsent|present\Z',])
64
65   if $content {
66     validate_string($content)
67   }
68
69   if $source {
70     validate_re($source, ['\Ahttps?:\/\/', '\Aftp:\/\/', '\A\/\w+'])
71   }
72
73   if $server {
74     validate_re($server,['\A((hkp|http|https):\/\/)?([a-z\d])([a-z\d-]{0,61}\.)+[a-z\d]+(:\d{2,5})?$'])
75   }
76
77   if $options {
78     validate_string($options)
79   }
80
81   case $ensure {
82     present: {
83       if defined(Anchor["apt_key ${id} absent"]){
84         fail("key with id ${id} already ensured as absent")
85       }
86
87       if !defined(Anchor["apt_key ${id} present"]) {
88         apt_key { $title:
89           ensure  => $ensure,
90           id      => $id,
91           source  => $source,
92           content => $content,
93           server  => $server,
94           options => $options,
95         } ->
96         anchor { "apt_key ${id} present": }
97       }
98     }
99
100     absent: {
101       if defined(Anchor["apt_key ${id} present"]){
102         fail("key with id ${id} already ensured as present")
103       }
104
105       if !defined(Anchor["apt_key ${id} absent"]){
106         apt_key { $title:
107           ensure  => $ensure,
108           id      => $id,
109           source  => $source,
110           content => $content,
111           server  => $server,
112           options => $options,
113         } ->
114         anchor { "apt_key ${id} absent": }
115       }
116     }
117
118     default: {
119       fail "Invalid 'ensure' value '${ensure}' for apt::key"
120     }
121   }
122 }