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